diff options
Diffstat (limited to 'src')
74 files changed, 430 insertions, 354 deletions
diff --git a/src/asn1/asn1_obj.h b/src/asn1/asn1_obj.h index a77f55c79..967aa6d3d 100644 --- a/src/asn1/asn1_obj.h +++ b/src/asn1/asn1_obj.h @@ -11,6 +11,7 @@ #include <botan/asn1_int.h> #include <botan/asn1_oid.h> +#include <botan/asn1_str.h> #include <botan/alg_id.h> #include <vector> #include <map> @@ -63,57 +64,6 @@ class BOTAN_DLL X509_Time : public ASN1_Object }; /** -* Simple String -*/ -class BOTAN_DLL ASN1_String : public ASN1_Object - { - public: - void encode_into(class DER_Encoder&) const; - void decode_from(class BER_Decoder&); - - std::string value() const; - std::string iso_8859() const; - - ASN1_Tag tagging() const; - - ASN1_String(const std::string& = ""); - ASN1_String(const std::string&, ASN1_Tag); - private: - std::string iso_8859_str; - ASN1_Tag tag; - }; - -/** -* Distinguished Name -*/ -class BOTAN_DLL X509_DN : public ASN1_Object - { - public: - void encode_into(class DER_Encoder&) const; - void decode_from(class BER_Decoder&); - - std::multimap<OID, std::string> get_attributes() const; - std::vector<std::string> get_attribute(const std::string&) const; - - std::multimap<std::string, std::string> contents() const; - - void add_attribute(const std::string&, const std::string&); - void add_attribute(const OID&, const std::string&); - - static std::string deref_info_field(const std::string&); - - void do_decode(const MemoryRegion<byte>&); - MemoryVector<byte> get_bits() const; - - X509_DN(); - X509_DN(const std::multimap<OID, std::string>&); - X509_DN(const std::multimap<std::string, std::string>&); - private: - std::multimap<OID, ASN1_String> dn_info; - MemoryVector<byte> dn_bits; - }; - -/** * Alternative Name */ class BOTAN_DLL AlternativeName : public ASN1_Object @@ -149,10 +99,6 @@ bool BOTAN_DLL operator>=(const X509_Time&, const X509_Time&); bool BOTAN_DLL operator<(const X509_Time&, const X509_Time&); bool BOTAN_DLL operator>(const X509_Time&, const X509_Time&); -bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&); -bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&); -bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&); - /* * Helper Functions */ diff --git a/src/asn1/asn1_oid.cpp b/src/asn1/asn1_oid.cpp index ffe583b90..750eb90f7 100644 --- a/src/asn1/asn1_oid.cpp +++ b/src/asn1/asn1_oid.cpp @@ -50,10 +50,10 @@ void OID::clear() std::string OID::as_string() const { std::string oid_str; - for(u32bit j = 0; j != id.size(); ++j) + for(size_t i = 0; i != id.size(); ++i) { - oid_str += std::to_string(id[j]); - if(j != id.size() - 1) + oid_str += std::to_string(id[i]); + if(i != id.size() - 1) oid_str += '.'; } return oid_str; @@ -66,8 +66,8 @@ bool OID::operator==(const OID& oid) const { if(id.size() != oid.id.size()) return false; - for(u32bit j = 0; j != id.size(); ++j) - if(id[j] != oid.id[j]) + for(size_t i = 0; i != id.size(); ++i) + if(id[i] != oid.id[i]) return false; return true; } @@ -111,11 +111,11 @@ bool operator<(const OID& a, const OID& b) return true; if(oid1.size() > oid2.size()) return false; - for(u32bit j = 0; j != oid1.size(); ++j) + for(size_t i = 0; i != oid1.size(); ++i) { - if(oid1[j] < oid2[j]) + if(oid1[i] < oid2[i]) return true; - if(oid1[j] > oid2[j]) + if(oid1[i] > oid2[i]) return false; } return false; @@ -132,18 +132,18 @@ void OID::encode_into(DER_Encoder& der) const MemoryVector<byte> encoding; encoding.push_back(40 * id[0] + id[1]); - for(u32bit j = 2; j != id.size(); ++j) + for(size_t i = 2; i != id.size(); ++i) { - if(id[j] == 0) + if(id[i] == 0) encoding.push_back(0); else { - u32bit blocks = high_bit(id[j]) + 6; + size_t blocks = high_bit(id[i]) + 6; blocks = (blocks - (blocks % 7)) / 7; - for(u32bit k = 0; k != blocks - 1; ++k) - encoding.push_back(0x80 | ((id[j] >> 7*(blocks-k-1)) & 0x7F)); - encoding.push_back(id[j] & 0x7F); + for(size_t j = 0; j != blocks - 1; ++j) + encoding.push_back(0x80 | ((id[i] >> 7*(blocks-j-1)) & 0x7F)); + encoding.push_back(id[i] & 0x7F); } } der.add_object(OBJECT_ID, UNIVERSAL, encoding); @@ -166,15 +166,15 @@ void OID::decode_from(BER_Decoder& decoder) id.push_back(obj.value[0] / 40); id.push_back(obj.value[0] % 40); - u32bit j = 0; - while(j != obj.value.size() - 1) + size_t i = 0; + while(i != obj.value.size() - 1) { u32bit component = 0; - while(j != obj.value.size() - 1) + while(i != obj.value.size() - 1) { - ++j; - component = (component << 7) + (obj.value[j] & 0x7F); - if(!(obj.value[j] & 0x80)) + ++i; + component = (component << 7) + (obj.value[i] & 0x7F); + if(!(obj.value[i] & 0x80)) break; } id.push_back(component); diff --git a/src/asn1/asn1_str.cpp b/src/asn1/asn1_str.cpp index 892a44472..5faf9546d 100644 --- a/src/asn1/asn1_str.cpp +++ b/src/asn1/asn1_str.cpp @@ -5,7 +5,7 @@ * Distributed under the terms of the Botan license */ -#include <botan/asn1_obj.h> +#include <botan/asn1_str.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/charset.h> @@ -45,9 +45,9 @@ ASN1_Tag choose_encoding(const std::string& str, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - for(u32bit j = 0; j != str.size(); ++j) + for(size_t i = 0; i != str.size(); ++i) { - if(!IS_PRINTABLE[static_cast<byte>(str[j])]) + if(!IS_PRINTABLE[static_cast<byte>(str[i])]) { if(type == "utf8") return UTF8_STRING; if(type == "latin1") return T61_STRING; diff --git a/src/asn1/asn1_str.h b/src/asn1/asn1_str.h new file mode 100644 index 000000000..ba43b0e94 --- /dev/null +++ b/src/asn1/asn1_str.h @@ -0,0 +1,38 @@ +/* +* ASN.1 string type +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_ASN1_STRING_H__ +#define BOTAN_ASN1_STRING_H__ + +#include <botan/asn1_int.h> + +namespace Botan { + +/** +* Simple String +*/ +class BOTAN_DLL ASN1_String : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::string value() const; + std::string iso_8859() const; + + ASN1_Tag tagging() const; + + ASN1_String(const std::string& = ""); + ASN1_String(const std::string&, ASN1_Tag); + private: + std::string iso_8859_str; + ASN1_Tag tag; + }; + +} + +#endif diff --git a/src/asn1/asn1_tm.cpp b/src/asn1/asn1_tm.cpp index 65eb96646..6e6868972 100644 --- a/src/asn1/asn1_tm.cpp +++ b/src/asn1/asn1_tm.cpp @@ -62,7 +62,7 @@ void X509_Time::set_to(const std::string& time_str) std::vector<std::string> params; std::string current; - for(u32bit j = 0; j != time_str.size(); ++j) + for(size_t j = 0; j != time_str.size(); ++j) { if(Charset::is_digit(time_str[j])) current += time_str[j]; @@ -109,17 +109,17 @@ void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag) if(t_spec[t_spec.size()-1] != 'Z') throw Invalid_Argument("Invalid time encoding: " + t_spec); - const u32bit YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4; + const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4; std::vector<std::string> params; std::string current; - for(u32bit j = 0; j != YEAR_SIZE; ++j) + for(size_t j = 0; j != YEAR_SIZE; ++j) current += t_spec[j]; params.push_back(current); current.clear(); - for(u32bit j = YEAR_SIZE; j != t_spec.size() - 1; ++j) + for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j) { current += t_spec[j]; if(current.size() == 2) diff --git a/src/asn1/ber_dec.cpp b/src/asn1/ber_dec.cpp index 7f0459e22..e98ab3b61 100644 --- a/src/asn1/ber_dec.cpp +++ b/src/asn1/ber_dec.cpp @@ -16,7 +16,7 @@ namespace { /* * BER decode an ASN.1 type tag */ -u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) +size_t decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) { byte b; if(!ber->read_byte(b)) @@ -32,10 +32,10 @@ u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) return 1; } - u32bit tag_bytes = 1; + size_t tag_bytes = 1; class_tag = ASN1_Tag(b & 0xE0); - u32bit tag_buf = 0; + size_t tag_buf = 0; while(true) { if(!ber->read_byte(b)) @@ -53,12 +53,12 @@ u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag) /* * Find the EOC marker */ -u32bit find_eoc(DataSource*); +size_t find_eoc(DataSource*); /* * BER decode an ASN.1 length field */ -u32bit decode_length(DataSource* ber, u32bit& field_size) +size_t decode_length(DataSource* ber, size_t& field_size) { byte b; if(!ber->read_byte(b)) @@ -72,9 +72,9 @@ u32bit decode_length(DataSource* ber, u32bit& field_size) if(field_size > 5) throw BER_Decoding_Error("Length field is too large"); - u32bit length = 0; + size_t length = 0; - for(u32bit j = 0; j != field_size - 1; ++j) + for(size_t i = 0; i != field_size - 1; ++i) { if(get_byte(0, length) != 0) throw BER_Decoding_Error("Field length overflow"); @@ -88,22 +88,22 @@ u32bit decode_length(DataSource* ber, u32bit& field_size) /* * BER decode an ASN.1 length field */ -u32bit decode_length(DataSource* ber) +size_t decode_length(DataSource* ber) { - u32bit dummy; + size_t dummy; return decode_length(ber, dummy); } /* * Find the EOC marker */ -u32bit find_eoc(DataSource* ber) +size_t find_eoc(DataSource* ber) { SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data; while(true) { - const u32bit got = ber->peek(&buffer[0], buffer.size(), data.size()); + const size_t got = ber->peek(&buffer[0], buffer.size(), data.size()); if(got == 0) break; @@ -113,16 +113,16 @@ u32bit find_eoc(DataSource* ber) DataSource_Memory source(data); data.clear(); - u32bit length = 0; + size_t length = 0; while(true) { ASN1_Tag type_tag, class_tag; - u32bit tag_size = decode_tag(&source, type_tag, class_tag); + size_t tag_size = decode_tag(&source, type_tag, class_tag); if(type_tag == NO_OBJECT) break; - u32bit length_size = 0; - u32bit item_size = decode_length(&source, length_size); + size_t length_size = 0; + size_t item_size = decode_length(&source, length_size); source.discard_next(item_size); length += item_size + length_size + tag_size; @@ -205,7 +205,7 @@ BER_Object BER_Decoder::get_next_object() if(next.type_tag == NO_OBJECT) return next; - u32bit length = decode_length(source); + size_t length = decode_length(source); next.value.resize(length); if(source->read(&next.value[0], length) != length) throw BER_Decoding_Error("Value truncated"); @@ -266,7 +266,7 @@ BER_Decoder::BER_Decoder(DataSource& src) /* * BER_Decoder Constructor */ -BER_Decoder::BER_Decoder(const byte data[], u32bit length) +BER_Decoder::BER_Decoder(const byte data[], size_t length) { source = new DataSource_Memory(data, length); owns = true; @@ -416,11 +416,11 @@ BER_Decoder& BER_Decoder::decode(BigInt& out, if(negative) { - for(u32bit j = obj.value.size(); j > 0; --j) - if(obj.value[j-1]--) + for(size_t i = obj.value.size(); i > 0; --i) + if(obj.value[i-1]--) break; - for(u32bit j = 0; j != obj.value.size(); ++j) - obj.value[j] = ~obj.value[j]; + for(size_t i = 0; i != obj.value.size(); ++i) + obj.value[i] = ~obj.value[i]; } out = BigInt(&obj.value[0], obj.value.size()); diff --git a/src/asn1/ber_dec.h b/src/asn1/ber_dec.h index 296d11037..79a15a298 100644 --- a/src/asn1/ber_dec.h +++ b/src/asn1/ber_dec.h @@ -77,7 +77,7 @@ class BOTAN_DLL BER_Decoder BER_Decoder& operator=(const BER_Decoder&) = delete; BER_Decoder(DataSource&); - BER_Decoder(const byte[], u32bit); + BER_Decoder(const byte[], size_t); BER_Decoder(const MemoryRegion<byte>&); BER_Decoder(const BER_Decoder&); ~BER_Decoder(); diff --git a/src/asn1/der_enc.cpp b/src/asn1/der_enc.cpp index fa53aa921..1d12ed5f6 100644 --- a/src/asn1/der_enc.cpp +++ b/src/asn1/der_enc.cpp @@ -31,12 +31,12 @@ SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) encoded_tag.push_back(static_cast<byte>(type_tag | class_tag)); else { - u32bit blocks = high_bit(type_tag) + 6; + size_t blocks = high_bit(type_tag) + 6; blocks = (blocks - (blocks % 7)) / 7; encoded_tag.push_back(class_tag | 0x1F); - for(u32bit k = 0; k != blocks - 1; ++k) - encoded_tag.push_back(0x80 | ((type_tag >> 7*(blocks-k-1)) & 0x7F)); + for(size_t i = 0; i != blocks - 1; ++i) + encoded_tag.push_back(0x80 | ((type_tag >> 7*(blocks-i-1)) & 0x7F)); encoded_tag.push_back(type_tag & 0x7F); } @@ -46,17 +46,19 @@ SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag) /* * DER encode an ASN.1 length field */ -SecureVector<byte> encode_length(u32bit length) +SecureVector<byte> encode_length(size_t length) { SecureVector<byte> encoded_length; if(length <= 127) encoded_length.push_back(static_cast<byte>(length)); else { - const u32bit top_byte = significant_bytes(length); + const size_t top_byte = significant_bytes(length); + encoded_length.push_back(static_cast<byte>(0x80 | top_byte)); - for(u32bit j = 4-top_byte; j != 4; ++j) - encoded_length.push_back(get_byte(j, length)); + + for(size_t i = sizeof(length) - top_byte; i != sizeof(length); ++i) + encoded_length.push_back(get_byte(i, length)); } return encoded_length; } @@ -73,8 +75,8 @@ SecureVector<byte> DER_Encoder::DER_Sequence::get_contents() if(type_tag == SET) { std::sort(set_contents.begin(), set_contents.end()); - for(u32bit j = 0; j != set_contents.size(); ++j) - contents += set_contents[j]; + for(size_t i = 0; i != set_contents.size(); ++i) + contents += set_contents[i]; set_contents.clear(); } @@ -90,7 +92,7 @@ SecureVector<byte> DER_Encoder::DER_Sequence::get_contents() /* * Add an encoded value to the SEQUENCE/SET */ -void DER_Encoder::DER_Sequence::add_bytes(const byte data[], u32bit length) +void DER_Encoder::DER_Sequence::add_bytes(const byte data[], size_t length) { if(type_tag == SET) set_contents.push_back(SecureVector<byte>(data, length)); @@ -183,7 +185,7 @@ DER_Encoder& DER_Encoder::raw_bytes(const MemoryRegion<byte>& val) /* * Write raw bytes into the stream */ -DER_Encoder& DER_Encoder::raw_bytes(const byte bytes[], u32bit length) +DER_Encoder& DER_Encoder::raw_bytes(const byte bytes[], size_t length) { if(subsequences.size()) subsequences[subsequences.size()-1].add_bytes(bytes, length); @@ -238,7 +240,7 @@ DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, /* * Encode this object */ -DER_Encoder& DER_Encoder::encode(const byte bytes[], u32bit length, +DER_Encoder& DER_Encoder::encode(const byte bytes[], size_t length, ASN1_Tag real_type) { return encode(bytes, length, real_type, real_type, UNIVERSAL); @@ -277,10 +279,10 @@ DER_Encoder& DER_Encoder::encode(const BigInt& n, BigInt::encode(&contents[extra_zero], n); if(n < 0) { - for(u32bit j = 0; j != contents.size(); ++j) - contents[j] = ~contents[j]; - for(u32bit j = contents.size(); j > 0; --j) - if(++contents[j-1]) + for(size_t i = 0; i != contents.size(); ++i) + contents[i] = ~contents[i]; + for(size_t i = contents.size(); i > 0; --i) + if(++contents[i-1]) break; } @@ -301,7 +303,7 @@ DER_Encoder& DER_Encoder::encode(const MemoryRegion<byte>& bytes, /* * DER encode an OCTET STRING or BIT STRING */ -DER_Encoder& DER_Encoder::encode(const byte bytes[], u32bit length, +DER_Encoder& DER_Encoder::encode(const byte bytes[], size_t length, ASN1_Tag real_type, ASN1_Tag type_tag, ASN1_Tag class_tag) { @@ -342,7 +344,7 @@ DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) * Write the encoding of the byte(s) */ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, - const byte rep[], u32bit length) + const byte rep[], size_t length) { SecureVector<byte> buffer; buffer += encode_tag(type_tag, class_tag); @@ -359,7 +361,7 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const MemoryRegion<byte>& rep_buf) { const byte* rep = &rep_buf[0]; - const u32bit rep_len = rep_buf.size(); + const size_t rep_len = rep_buf.size(); return add_object(type_tag, class_tag, rep, rep_len); } @@ -370,7 +372,7 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string& rep_str) { const byte* rep = reinterpret_cast<const byte*>(rep_str.data()); - const u32bit rep_len = rep_str.size(); + const size_t rep_len = rep_str.size(); return add_object(type_tag, class_tag, rep, rep_len); } diff --git a/src/asn1/der_enc.h b/src/asn1/der_enc.h index ae10b4bc8..b3fce389d 100644 --- a/src/asn1/der_enc.h +++ b/src/asn1/der_enc.h @@ -30,7 +30,7 @@ class BOTAN_DLL DER_Encoder DER_Encoder& start_explicit(u16bit); DER_Encoder& end_explicit(); - DER_Encoder& raw_bytes(const byte[], u32bit); + DER_Encoder& raw_bytes(const byte[], size_t); DER_Encoder& raw_bytes(const MemoryRegion<byte>&); DER_Encoder& encode_null(); @@ -38,7 +38,7 @@ class BOTAN_DLL DER_Encoder DER_Encoder& encode(u32bit); DER_Encoder& encode(const BigInt&); DER_Encoder& encode(const MemoryRegion<byte>&, ASN1_Tag); - DER_Encoder& encode(const byte[], u32bit, ASN1_Tag); + DER_Encoder& encode(const byte[], size_t, ASN1_Tag); DER_Encoder& encode(bool, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); DER_Encoder& encode(u32bit, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); @@ -46,7 +46,7 @@ class BOTAN_DLL DER_Encoder ASN1_Tag = CONTEXT_SPECIFIC); DER_Encoder& encode(const MemoryRegion<byte>&, ASN1_Tag, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); - DER_Encoder& encode(const byte[], u32bit, ASN1_Tag, + DER_Encoder& encode(const byte[], size_t, ASN1_Tag, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); template<typename T> @@ -60,15 +60,15 @@ class BOTAN_DLL DER_Encoder template<typename T> DER_Encoder& encode_list(const std::vector<T>& values) { - for(u32bit j = 0; j != values.size(); ++j) - encode(values[j]); + for(size_t i = 0; i != values.size(); ++i) + encode(values[i]); return (*this); } DER_Encoder& encode(const ASN1_Object&); DER_Encoder& encode_if(bool, DER_Encoder&); - DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const byte[], u32bit); + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const byte[], size_t); DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const MemoryRegion<byte>&); DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const std::string&); DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, byte); @@ -78,7 +78,7 @@ class BOTAN_DLL DER_Encoder public: ASN1_Tag tag_of() const; SecureVector<byte> get_contents(); - void add_bytes(const byte[], u32bit); + void add_bytes(const byte[], size_t); DER_Sequence(ASN1_Tag, ASN1_Tag); private: ASN1_Tag type_tag, class_tag; diff --git a/src/asn1/info.txt b/src/asn1/info.txt index 7ed025e68..e190ad9ea 100644 --- a/src/asn1/info.txt +++ b/src/asn1/info.txt @@ -6,13 +6,13 @@ load_on auto alg_id.cpp asn1_alt.cpp asn1_att.cpp -asn1_dn.cpp asn1_int.cpp asn1_oid.cpp asn1_str.cpp asn1_tm.cpp ber_dec.cpp der_enc.cpp +x509_dn.cpp </source> <header:public> @@ -20,8 +20,10 @@ alg_id.h asn1_int.h asn1_obj.h asn1_oid.h +asn1_str.h ber_dec.h der_enc.h +x509_dn.h </header:public> <requires> diff --git a/src/asn1/asn1_dn.cpp b/src/asn1/x509_dn.cpp index 7d6e43bcc..0deed1a70 100644 --- a/src/asn1/asn1_dn.cpp +++ b/src/asn1/x509_dn.cpp @@ -5,7 +5,7 @@ * Distributed under the terms of the Botan license */ -#include <botan/asn1_obj.h> +#include <botan/x509_dn.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/parsing.h> diff --git a/src/asn1/x509_dn.h b/src/asn1/x509_dn.h new file mode 100644 index 000000000..c4fc2d17b --- /dev/null +++ b/src/asn1/x509_dn.h @@ -0,0 +1,54 @@ +/* +* X.509 Distinguished Name +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_X509_DN_H__ +#define BOTAN_X509_DN_H__ + +#include <botan/asn1_int.h> +#include <botan/asn1_oid.h> +#include <botan/asn1_str.h> +#include <map> + +namespace Botan { + +/** +* Distinguished Name +*/ +class BOTAN_DLL X509_DN : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + + std::multimap<OID, std::string> get_attributes() const; + std::vector<std::string> get_attribute(const std::string&) const; + + std::multimap<std::string, std::string> contents() const; + + void add_attribute(const std::string&, const std::string&); + void add_attribute(const OID&, const std::string&); + + static std::string deref_info_field(const std::string&); + + void do_decode(const MemoryRegion<byte>&); + MemoryVector<byte> get_bits() const; + + X509_DN(); + X509_DN(const std::multimap<OID, std::string>&); + X509_DN(const std::multimap<std::string, std::string>&); + private: + std::multimap<OID, ASN1_String> dn_info; + MemoryVector<byte> dn_bits; + }; + +bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&); +bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&); + +} + +#endif diff --git a/src/block/aes/aes.cpp b/src/block/aes/aes.cpp index 88439cf98..1530af965 100644 --- a/src/block/aes/aes.cpp +++ b/src/block/aes/aes.cpp @@ -521,8 +521,8 @@ void AES::encrypt_n(const byte in[], byte out[], size_t blocks) const out[14] = SE[get_byte(2, B1)] ^ ME[14]; out[15] = SE[get_byte(3, B2)] ^ ME[15]; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -611,8 +611,8 @@ void AES::decrypt_n(const byte in[], byte out[], size_t blocks) const out[14] = SD[get_byte(2, B1)] ^ MD[14]; out[15] = SD[get_byte(3, B0)] ^ MD[15]; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -681,7 +681,7 @@ u32bit AES::S(u32bit input) /* * AES Constructor */ -AES::AES(u32bit key_size) : BlockCipher(16, key_size), +AES::AES(u32bit key_size) : BlockCipher_Fixed_Block_Size(key_size), EK(56), ME(16), DK(56), MD(16) { if(key_size != 16 && key_size != 24 && key_size != 32) diff --git a/src/block/aes/aes.h b/src/block/aes/aes.h index d62413f5b..6fa0ccaff 100644 --- a/src/block/aes/aes.h +++ b/src/block/aes/aes.h @@ -15,17 +15,19 @@ namespace Botan { /** * Rijndael aka AES */ -class BOTAN_DLL AES : public BlockCipher +class BOTAN_DLL AES : public BlockCipher_Fixed_Block_Size<16> { public: + std::string name() const { return "AES"; } + void encrypt_n(const byte in[], byte out[], size_t blocks) const; void decrypt_n(const byte in[], byte out[], size_t blocks) const; void clear(); - std::string name() const { return "AES"; } BlockCipher* clone() const { return new AES; } - AES() : BlockCipher(16, 16, 32, 8), EK(56), ME(16), DK(56), MD(16) + AES() : BlockCipher_Fixed_Block_Size(16, 32, 8), + EK(56), ME(16), DK(56), MD(16) { ROUNDS = 14; } /** diff --git a/src/block/aes_ssse3/aes_ssse3.h b/src/block/aes_ssse3/aes_ssse3.h index 0cdb5f4de..59bb85f12 100644 --- a/src/block/aes_ssse3/aes_ssse3.h +++ b/src/block/aes_ssse3/aes_ssse3.h @@ -15,7 +15,7 @@ namespace Botan { /** * AES-128 using SSSE3 */ -class BOTAN_DLL AES_128_SSSE3 : public BlockCipher +class BOTAN_DLL AES_128_SSSE3 : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,8 @@ class BOTAN_DLL AES_128_SSSE3 : public BlockCipher std::string name() const { return "AES-128"; } BlockCipher* clone() const { return new AES_128_SSSE3; } - AES_128_SSSE3() : BlockCipher(16, 16), EK(44), DK(44) {} + AES_128_SSSE3() : BlockCipher_Fixed_Block_Size(16), + EK(44), DK(44) {} private: void key_schedule(const byte[], size_t); @@ -35,7 +36,7 @@ class BOTAN_DLL AES_128_SSSE3 : public BlockCipher /** * AES-192 using SSSE3 */ -class BOTAN_DLL AES_192_SSSE3 : public BlockCipher +class BOTAN_DLL AES_192_SSSE3 : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -45,7 +46,8 @@ class BOTAN_DLL AES_192_SSSE3 : public BlockCipher std::string name() const { return "AES-192"; } BlockCipher* clone() const { return new AES_192_SSSE3; } - AES_192_SSSE3() : BlockCipher(16, 24), EK(52), DK(52) {} + AES_192_SSSE3() : BlockCipher_Fixed_Block_Size(24), + EK(52), DK(52) {} private: void key_schedule(const byte[], size_t); @@ -55,7 +57,7 @@ class BOTAN_DLL AES_192_SSSE3 : public BlockCipher /** * AES-256 using SSSE3 */ -class BOTAN_DLL AES_256_SSSE3 : public BlockCipher +class BOTAN_DLL AES_256_SSSE3 : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -65,7 +67,8 @@ class BOTAN_DLL AES_256_SSSE3 : public BlockCipher std::string name() const { return "AES-256"; } BlockCipher* clone() const { return new AES_256_SSSE3; } - AES_256_SSSE3() : BlockCipher(16, 32), EK(60), DK(60) {} + AES_256_SSSE3() : BlockCipher_Fixed_Block_Size(32), + EK(60), DK(60) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/block_cipher.h b/src/block/block_cipher.h index 5f5e5e530..e522005b9 100644 --- a/src/block/block_cipher.h +++ b/src/block/block_cipher.h @@ -25,24 +25,17 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm * @param key_max the maximum key size * @param key_mod the modulo restriction on the key size */ - BlockCipher(u32bit block_size, - u32bit key_min, + BlockCipher(u32bit key_min, u32bit key_max = 0, u32bit key_mod = 1) : - SymmetricAlgorithm(key_min, key_max, key_mod), - BLOCK_SIZE(block_size) {} + SymmetricAlgorithm(key_min, key_max, key_mod) {} virtual ~BlockCipher() {} /** - * The block size of this algorithm. - */ - const u32bit BLOCK_SIZE; - - /** * @return block size of this algorithm */ - size_t block_size() const { return BLOCK_SIZE; } + virtual size_t block_size() const = 0; /** * @return native parallelism of this cipher in blocks @@ -122,6 +115,19 @@ class BOTAN_DLL BlockCipher : public SymmetricAlgorithm virtual void clear() = 0; }; +template<size_t N> +class BlockCipher_Fixed_Block_Size : public BlockCipher + { + public: + BlockCipher_Fixed_Block_Size(u32bit kmin, + u32bit kmax = 0, + u32bit kmod = 1) : + BlockCipher(kmin, kmax, kmod) {} + + enum { BLOCK_SIZE = N }; + size_t block_size() const { return N; } + }; + } #endif diff --git a/src/block/blowfish/blowfish.cpp b/src/block/blowfish/blowfish.cpp index f77c65d4d..ea227e93e 100644 --- a/src/block/blowfish/blowfish.cpp +++ b/src/block/blowfish/blowfish.cpp @@ -40,8 +40,8 @@ void Blowfish::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, R, L); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -75,8 +75,8 @@ void Blowfish::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, R, L); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/blowfish/blowfish.h b/src/block/blowfish/blowfish.h index 4d39e9e58..c9bf8b2e0 100644 --- a/src/block/blowfish/blowfish.h +++ b/src/block/blowfish/blowfish.h @@ -15,7 +15,7 @@ namespace Botan { /** * Blowfish */ -class BOTAN_DLL Blowfish : public BlockCipher +class BOTAN_DLL Blowfish : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL Blowfish : public BlockCipher std::string name() const { return "Blowfish"; } BlockCipher* clone() const { return new Blowfish; } - Blowfish() : BlockCipher(8, 1, 56), S(1024), P(18) {} + Blowfish() : BlockCipher_Fixed_Block_Size(1, 56), S(1024), P(18) {} private: void key_schedule(const byte[], size_t); void generate_sbox(MemoryRegion<u32bit>& box, diff --git a/src/block/cascade/cascade.cpp b/src/block/cascade/cascade.cpp index 225b7fd6e..2701c20e7 100644 --- a/src/block/cascade/cascade.cpp +++ b/src/block/cascade/cascade.cpp @@ -81,10 +81,11 @@ size_t block_size_for_cascade(size_t bs, size_t bs2) } Cascade_Cipher::Cascade_Cipher(BlockCipher* c1, BlockCipher* c2) : - BlockCipher(block_size_for_cascade(c1->block_size(), c2->block_size()), - c1->MAXIMUM_KEYLENGTH + c2->MAXIMUM_KEYLENGTH), + BlockCipher(c1->MAXIMUM_KEYLENGTH + c2->MAXIMUM_KEYLENGTH), cipher1(c1), cipher2(c2) { + block = block_size_for_cascade(c1->block_size(), c2->block_size()); + if(block_size() % c1->block_size() || block_size() % c2->block_size()) throw Internal_Error("Failure in " + name() + " constructor"); } diff --git a/src/block/cascade/cascade.h b/src/block/cascade/cascade.h index 5e1989cb6..31ee3b336 100644 --- a/src/block/cascade/cascade.h +++ b/src/block/cascade/cascade.h @@ -21,6 +21,8 @@ class BOTAN_DLL Cascade_Cipher : public BlockCipher void encrypt_n(const byte in[], byte out[], size_t blocks) const; void decrypt_n(const byte in[], byte out[], size_t blocks) const; + size_t block_size() const { return block; } + void clear(); std::string name() const; BlockCipher* clone() const; @@ -36,6 +38,7 @@ class BOTAN_DLL Cascade_Cipher : public BlockCipher private: void key_schedule(const byte[], size_t); + size_t block; BlockCipher* cipher1; BlockCipher* cipher2; }; diff --git a/src/block/cast/cast128.cpp b/src/block/cast/cast128.cpp index 092fc201e..24469e025 100644 --- a/src/block/cast/cast128.cpp +++ b/src/block/cast/cast128.cpp @@ -74,8 +74,8 @@ void CAST_128::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, R, L); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -108,8 +108,8 @@ void CAST_128::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, R, L); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/cast/cast128.h b/src/block/cast/cast128.h index edccf04b3..3ecbcaa5a 100644 --- a/src/block/cast/cast128.h +++ b/src/block/cast/cast128.h @@ -15,7 +15,7 @@ namespace Botan { /** * CAST-128 */ -class BOTAN_DLL CAST_128 : public BlockCipher +class BOTAN_DLL CAST_128 : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL CAST_128 : public BlockCipher std::string name() const { return "CAST-128"; } BlockCipher* clone() const { return new CAST_128; } - CAST_128() : BlockCipher(8, 11, 16), MK(16), RK(16) {} + CAST_128() : BlockCipher_Fixed_Block_Size(11, 16), MK(16), RK(16) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/cast/cast256.cpp b/src/block/cast/cast256.cpp index 1b41cd2af..8be0a8dd6 100644 --- a/src/block/cast/cast256.cpp +++ b/src/block/cast/cast256.cpp @@ -84,8 +84,8 @@ void CAST_256::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, A, B, C, D); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -128,8 +128,8 @@ void CAST_256::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, A, B, C, D); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/cast/cast256.h b/src/block/cast/cast256.h index 74e38face..0dda7f0d7 100644 --- a/src/block/cast/cast256.h +++ b/src/block/cast/cast256.h @@ -15,7 +15,7 @@ namespace Botan { /** * CAST-256 */ -class BOTAN_DLL CAST_256 : public BlockCipher +class BOTAN_DLL CAST_256 : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL CAST_256 : public BlockCipher std::string name() const { return "CAST-256"; } BlockCipher* clone() const { return new CAST_256; } - CAST_256() : BlockCipher(16, 4, 32, 4), MK(48), RK(48) {} + CAST_256() : BlockCipher_Fixed_Block_Size(4, 32, 4), MK(48), RK(48) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/des/des.cpp b/src/block/des/des.cpp index 7c61df3db..15c771bda 100644 --- a/src/block/des/des.cpp +++ b/src/block/des/des.cpp @@ -162,8 +162,8 @@ void DES::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(T, out); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -193,8 +193,8 @@ void DES::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(T, out); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -234,8 +234,8 @@ void TripleDES::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(T, out); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -267,8 +267,8 @@ void TripleDES::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(T, out); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/des/des.h b/src/block/des/des.h index 03641ba40..d758cc4c1 100644 --- a/src/block/des/des.h +++ b/src/block/des/des.h @@ -15,7 +15,7 @@ namespace Botan { /** * DES */ -class BOTAN_DLL DES : public BlockCipher +class BOTAN_DLL DES : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL DES : public BlockCipher std::string name() const { return "DES"; } BlockCipher* clone() const { return new DES; } - DES() : BlockCipher(8, 8), round_key(32) {} + DES() : BlockCipher_Fixed_Block_Size(8), round_key(32) {} private: void key_schedule(const byte[], size_t); @@ -35,7 +35,7 @@ class BOTAN_DLL DES : public BlockCipher /** * Triple DES */ -class BOTAN_DLL TripleDES : public BlockCipher +class BOTAN_DLL TripleDES : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -45,7 +45,7 @@ class BOTAN_DLL TripleDES : public BlockCipher std::string name() const { return "TripleDES"; } BlockCipher* clone() const { return new TripleDES; } - TripleDES() : BlockCipher(8, 16, 24, 8), round_key(96) {} + TripleDES() : BlockCipher_Fixed_Block_Size(16, 24, 8), round_key(96) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/des/desx.cpp b/src/block/des/desx.cpp index c4dacdfdd..b92011e56 100644 --- a/src/block/des/desx.cpp +++ b/src/block/des/desx.cpp @@ -17,12 +17,12 @@ void DESX::encrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { - xor_buf(out, in, &K1[0], block_size()); + xor_buf(out, in, &K1[0], BLOCK_SIZE); des.encrypt(out); - xor_buf(out, &K2[0], block_size()); + xor_buf(out, &K2[0], BLOCK_SIZE); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -33,12 +33,12 @@ void DESX::decrypt_n(const byte in[], byte out[], size_t blocks) const { for(size_t i = 0; i != blocks; ++i) { - xor_buf(out, in, &K2[0], block_size()); + xor_buf(out, in, &K2[0], BLOCK_SIZE); des.decrypt(out); - xor_buf(out, &K1[0], block_size()); + xor_buf(out, &K1[0], BLOCK_SIZE); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/des/desx.h b/src/block/des/desx.h index b61ea3cf9..962575529 100644 --- a/src/block/des/desx.h +++ b/src/block/des/desx.h @@ -15,7 +15,7 @@ namespace Botan { /** * DESX */ -class BOTAN_DLL DESX : public BlockCipher +class BOTAN_DLL DESX : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL DESX : public BlockCipher std::string name() const { return "DESX"; } BlockCipher* clone() const { return new DESX; } - DESX() : BlockCipher(8, 24), K1(8), K2(8) {} + DESX() : BlockCipher_Fixed_Block_Size(24), K1(8), K2(8) {} private: void key_schedule(const byte[], size_t); SecureVector<byte> K1, K2; diff --git a/src/block/gost_28147/gost_28147.cpp b/src/block/gost_28147/gost_28147.cpp index ddf26b3d0..9adc0d568 100644 --- a/src/block/gost_28147/gost_28147.cpp +++ b/src/block/gost_28147/gost_28147.cpp @@ -52,7 +52,7 @@ GOST_28147_89_Params::GOST_28147_89_Params(const std::string& n) : name(n) * GOST Constructor */ GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : - BlockCipher(8, 32), SBOX(1024), EK(8) + BlockCipher_Fixed_Block_Size(32), SBOX(1024), EK(8) { // Convert the parallel 4x4 sboxes into larger word-based sboxes for(size_t i = 0; i != 4; ++i) @@ -107,8 +107,8 @@ void GOST_28147_89::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, N2, N1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -136,8 +136,8 @@ void GOST_28147_89::decrypt_n(const byte in[], byte out[], size_t blocks) const } store_le(out, N2, N1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/gost_28147/gost_28147.h b/src/block/gost_28147/gost_28147.h index d06b63228..adf542bbe 100644 --- a/src/block/gost_28147/gost_28147.h +++ b/src/block/gost_28147/gost_28147.h @@ -49,7 +49,7 @@ class BOTAN_DLL GOST_28147_89_Params /** * GOST 28147-89 */ -class BOTAN_DLL GOST_28147_89 : public BlockCipher +class BOTAN_DLL GOST_28147_89 : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -66,7 +66,7 @@ class BOTAN_DLL GOST_28147_89 : public BlockCipher GOST_28147_89(const GOST_28147_89_Params& params); private: GOST_28147_89(const SecureVector<u32bit>& other_SBOX) : - BlockCipher(8, 32), SBOX(other_SBOX), EK(8) {} + BlockCipher_Fixed_Block_Size(32), SBOX(other_SBOX), EK(8) {} void key_schedule(const byte[], size_t); diff --git a/src/block/idea/idea.h b/src/block/idea/idea.h index c0af38ad6..3552d282f 100644 --- a/src/block/idea/idea.h +++ b/src/block/idea/idea.h @@ -15,7 +15,7 @@ namespace Botan { /** * IDEA */ -class BOTAN_DLL IDEA : public BlockCipher +class BOTAN_DLL IDEA : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL IDEA : public BlockCipher std::string name() const { return "IDEA"; } BlockCipher* clone() const { return new IDEA; } - IDEA() : BlockCipher(8, 16), EK(52), DK(52) {} + IDEA() : BlockCipher_Fixed_Block_Size(16), EK(52), DK(52) {} protected: /** * @return const reference to encryption subkeys diff --git a/src/block/idea_sse2/idea_sse2.cpp b/src/block/idea_sse2/idea_sse2.cpp index 8c7bd2a2c..469a33943 100644 --- a/src/block/idea_sse2/idea_sse2.cpp +++ b/src/block/idea_sse2/idea_sse2.cpp @@ -201,8 +201,8 @@ void IDEA_SSE2::encrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 8) { idea_op_8(in, out, KS); - in += 8 * block_size(); - out += 8 * block_size(); + in += 8 * BLOCK_SIZE; + out += 8 * BLOCK_SIZE; blocks -= 8; } @@ -220,8 +220,8 @@ void IDEA_SSE2::decrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 8) { idea_op_8(in, out, KS); - in += 8 * block_size(); - out += 8 * block_size(); + in += 8 * BLOCK_SIZE; + out += 8 * BLOCK_SIZE; blocks -= 8; } diff --git a/src/block/kasumi/kasumi.cpp b/src/block/kasumi/kasumi.cpp index 1a217a9c7..a57c0396a 100644 --- a/src/block/kasumi/kasumi.cpp +++ b/src/block/kasumi/kasumi.cpp @@ -145,8 +145,8 @@ void KASUMI::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B0, B1, B2, B3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -191,8 +191,8 @@ void KASUMI::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B0, B1, B2, B3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/kasumi/kasumi.h b/src/block/kasumi/kasumi.h index c6b3c4351..7b416f193 100644 --- a/src/block/kasumi/kasumi.h +++ b/src/block/kasumi/kasumi.h @@ -15,7 +15,7 @@ namespace Botan { /** * KASUMI, the block cipher used in 3G telephony */ -class BOTAN_DLL KASUMI : public BlockCipher +class BOTAN_DLL KASUMI : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL KASUMI : public BlockCipher std::string name() const { return "KASUMI"; } BlockCipher* clone() const { return new KASUMI; } - KASUMI() : BlockCipher(8, 16), EK(64) {} + KASUMI() : BlockCipher_Fixed_Block_Size(16), EK(64) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/lion/lion.cpp b/src/block/lion/lion.cpp index 9214044f6..103759e39 100644 --- a/src/block/lion/lion.cpp +++ b/src/block/lion/lion.cpp @@ -33,8 +33,8 @@ void Lion::encrypt_n(const byte in[], byte out[], size_t blocks) const cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -60,8 +60,8 @@ void Lion::decrypt_n(const byte in[], byte out[], size_t blocks) const cipher->set_key(buffer, LEFT_SIZE); cipher->cipher1(out + LEFT_SIZE, RIGHT_SIZE); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -83,7 +83,7 @@ std::string Lion::name() const { return "Lion(" + hash->name() + "," + cipher->name() + "," + - std::to_string(block_size()) + ")"; + std::to_string(BLOCK_SIZE) + ")"; } /* @@ -91,7 +91,7 @@ std::string Lion::name() const */ BlockCipher* Lion::clone() const { - return new Lion(hash->clone(), cipher->clone(), block_size()); + return new Lion(hash->clone(), cipher->clone(), BLOCK_SIZE); } /* @@ -109,14 +109,14 @@ void Lion::clear() * Lion Constructor */ Lion::Lion(HashFunction* hash_in, StreamCipher* sc_in, size_t block_len) : - BlockCipher(std::max<size_t>(2*hash_in->output_length() + 1, block_len), - 2, 2*hash_in->output_length(), 2), + BlockCipher(2, 2*hash_in->output_length(), 2), + BLOCK_SIZE(std::max<size_t>(2*hash_in->output_length() + 1, block_len)), LEFT_SIZE(hash_in->output_length()), - RIGHT_SIZE(block_size() - LEFT_SIZE), + RIGHT_SIZE(BLOCK_SIZE - LEFT_SIZE), hash(hash_in), cipher(sc_in) { - if(2*LEFT_SIZE + 1 > block_size()) + if(2*LEFT_SIZE + 1 > BLOCK_SIZE) throw Invalid_Argument(name() + ": Chosen block size is too small"); if(!cipher->valid_keylength(LEFT_SIZE)) diff --git a/src/block/lion/lion.h b/src/block/lion/lion.h index 9beb68ca6..d4eb9c327 100644 --- a/src/block/lion/lion.h +++ b/src/block/lion/lion.h @@ -28,6 +28,8 @@ class BOTAN_DLL Lion : public BlockCipher void encrypt_n(const byte in[], byte out[], size_t blocks) const; void decrypt_n(const byte in[], byte out[], size_t blocks) const; + size_t block_size() const { return BLOCK_SIZE; } + void clear(); std::string name() const; BlockCipher* clone() const; @@ -45,7 +47,7 @@ class BOTAN_DLL Lion : public BlockCipher private: void key_schedule(const byte[], size_t); - const size_t LEFT_SIZE, RIGHT_SIZE; + const size_t BLOCK_SIZE, LEFT_SIZE, RIGHT_SIZE; HashFunction* hash; StreamCipher* cipher; diff --git a/src/block/lubyrack/lubyrack.cpp b/src/block/lubyrack/lubyrack.cpp index aa33c6bc4..335570973 100644 --- a/src/block/lubyrack/lubyrack.cpp +++ b/src/block/lubyrack/lubyrack.cpp @@ -42,8 +42,8 @@ void LubyRackoff::encrypt_n(const byte in[], byte out[], size_t blocks) const hash->final(buffer); xor_buf(out, buffer, len); - in += block_size(); - out += block_size(); + in += 2 * len; + out += 2 * len; } } @@ -79,8 +79,8 @@ void LubyRackoff::decrypt_n(const byte in[], byte out[], size_t blocks) const hash->final(buffer); xor_buf(out + len, buffer, len); - in += block_size(); - out += block_size(); + in += 2 * len; + out += 2 * len; } } @@ -123,8 +123,7 @@ std::string LubyRackoff::name() const * Luby-Rackoff Constructor */ LubyRackoff::LubyRackoff(HashFunction* h) : - BlockCipher(2 * (h ? h->output_length(): 0), - 2, 32, 2), + BlockCipher(2, 32, 2), hash(h) { } diff --git a/src/block/lubyrack/lubyrack.h b/src/block/lubyrack/lubyrack.h index 4567215e1..0c267683a 100644 --- a/src/block/lubyrack/lubyrack.h +++ b/src/block/lubyrack/lubyrack.h @@ -22,6 +22,8 @@ class BOTAN_DLL LubyRackoff : public BlockCipher void encrypt_n(const byte in[], byte out[], size_t blocks) const; void decrypt_n(const byte in[], byte out[], size_t blocks) const; + size_t block_size() const { return 2 * hash->output_length(); } + void clear(); std::string name() const; BlockCipher* clone() const; diff --git a/src/block/mars/mars.cpp b/src/block/mars/mars.cpp index 5864ac49b..fa73e564f 100644 --- a/src/block/mars/mars.cpp +++ b/src/block/mars/mars.cpp @@ -267,8 +267,8 @@ void MARS::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, A, B, C, D); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -310,8 +310,8 @@ void MARS::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, D, C, B, A); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/mars/mars.h b/src/block/mars/mars.h index a61f475f2..7a53d116b 100644 --- a/src/block/mars/mars.h +++ b/src/block/mars/mars.h @@ -15,7 +15,7 @@ namespace Botan { /** * MARS, IBM's candidate for AES */ -class BOTAN_DLL MARS : public BlockCipher +class BOTAN_DLL MARS : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL MARS : public BlockCipher std::string name() const { return "MARS"; } BlockCipher* clone() const { return new MARS; } - MARS() : BlockCipher(16, 16, 32, 4), EK(40) {} + MARS() : BlockCipher_Fixed_Block_Size(16, 32, 4), EK(40) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/misty1/misty1.cpp b/src/block/misty1/misty1.cpp index b87f513a0..9ad30502c 100644 --- a/src/block/misty1/misty1.cpp +++ b/src/block/misty1/misty1.cpp @@ -144,8 +144,8 @@ void MISTY1::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B2, B3, B0, B1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -194,8 +194,8 @@ void MISTY1::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B0, B1, B2, B3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -251,7 +251,9 @@ void MISTY1::key_schedule(const byte key[], size_t length) /* * MISTY1 Constructor */ -MISTY1::MISTY1(size_t rounds) : BlockCipher(8, 16), EK(100), DK(100) +MISTY1::MISTY1(size_t rounds) : + BlockCipher_Fixed_Block_Size(16), + EK(100), DK(100) { if(rounds != 8) throw Invalid_Argument("MISTY1: Invalid number of rounds: " diff --git a/src/block/misty1/misty1.h b/src/block/misty1/misty1.h index 318e63b7d..3bd05b4c6 100644 --- a/src/block/misty1/misty1.h +++ b/src/block/misty1/misty1.h @@ -15,7 +15,7 @@ namespace Botan { /** * MISTY1 */ -class BOTAN_DLL MISTY1 : public BlockCipher +class BOTAN_DLL MISTY1 : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; diff --git a/src/block/noekeon/noekeon.cpp b/src/block/noekeon/noekeon.cpp index c29fed93e..06c415be9 100644 --- a/src/block/noekeon/noekeon.cpp +++ b/src/block/noekeon/noekeon.cpp @@ -114,8 +114,8 @@ void Noekeon::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, A0, A1, A2, A3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -152,8 +152,8 @@ void Noekeon::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, A0, A1, A2, A3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/noekeon/noekeon.h b/src/block/noekeon/noekeon.h index 593afa634..79c627579 100644 --- a/src/block/noekeon/noekeon.h +++ b/src/block/noekeon/noekeon.h @@ -15,7 +15,7 @@ namespace Botan { /** * Noekeon */ -class BOTAN_DLL Noekeon : public BlockCipher +class BOTAN_DLL Noekeon : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL Noekeon : public BlockCipher std::string name() const { return "Noekeon"; } BlockCipher* clone() const { return new Noekeon; } - Noekeon() : BlockCipher(16, 16), EK(4), DK(4) {} + Noekeon() : BlockCipher_Fixed_Block_Size(16), EK(4), DK(4) {} protected: /** * The Noekeon round constants diff --git a/src/block/rc2/rc2.cpp b/src/block/rc2/rc2.cpp index 5c7cb1ead..97ca5d577 100644 --- a/src/block/rc2/rc2.cpp +++ b/src/block/rc2/rc2.cpp @@ -48,8 +48,8 @@ void RC2::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, R0, R1, R2, R3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -90,8 +90,8 @@ void RC2::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, R0, R1, R2, R3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/rc2/rc2.h b/src/block/rc2/rc2.h index 4addf22ed..ad4b1a308 100644 --- a/src/block/rc2/rc2.h +++ b/src/block/rc2/rc2.h @@ -15,7 +15,7 @@ namespace Botan { /** * RC2 */ -class BOTAN_DLL RC2 : public BlockCipher +class BOTAN_DLL RC2 : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -32,7 +32,7 @@ class BOTAN_DLL RC2 : public BlockCipher std::string name() const { return "RC2"; } BlockCipher* clone() const { return new RC2; } - RC2() : BlockCipher(8, 1, 32), K(64) {} + RC2() : BlockCipher_Fixed_Block_Size(1, 32), K(64) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/rc5/rc5.cpp b/src/block/rc5/rc5.cpp index 36d4cdd3a..ebcbaf69f 100644 --- a/src/block/rc5/rc5.cpp +++ b/src/block/rc5/rc5.cpp @@ -38,8 +38,8 @@ void RC5::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, A, B); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -68,8 +68,8 @@ void RC5::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, A, B); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -112,7 +112,9 @@ std::string RC5::name() const /* * RC5 Constructor */ -RC5::RC5(size_t r) : BlockCipher(8, 1, 32), ROUNDS(r) +RC5::RC5(size_t r) : + BlockCipher_Fixed_Block_Size(1, 32), + ROUNDS(r) { if(ROUNDS < 8 || ROUNDS > 32 || (ROUNDS % 4 != 0)) throw Invalid_Argument(name() + ": Invalid number of rounds"); diff --git a/src/block/rc5/rc5.h b/src/block/rc5/rc5.h index 11a62badb..a9f3b5b0e 100644 --- a/src/block/rc5/rc5.h +++ b/src/block/rc5/rc5.h @@ -15,7 +15,7 @@ namespace Botan { /** * RC5 */ -class BOTAN_DLL RC5 : public BlockCipher +class BOTAN_DLL RC5 : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; diff --git a/src/block/rc6/rc6.cpp b/src/block/rc6/rc6.cpp index df87acbb1..53ca5a7a2 100644 --- a/src/block/rc6/rc6.cpp +++ b/src/block/rc6/rc6.cpp @@ -55,8 +55,8 @@ void RC6::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, A, B, C, D); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -103,8 +103,8 @@ void RC6::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, A, B, C, D); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/rc6/rc6.h b/src/block/rc6/rc6.h index 307834a8c..8446138e0 100644 --- a/src/block/rc6/rc6.h +++ b/src/block/rc6/rc6.h @@ -15,7 +15,7 @@ namespace Botan { /** * RC6, Ron Rivest's AES candidate */ -class BOTAN_DLL RC6 : public BlockCipher +class BOTAN_DLL RC6 : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL RC6 : public BlockCipher std::string name() const { return "RC6"; } BlockCipher* clone() const { return new RC6; } - RC6() : BlockCipher(16, 1, 32), S(44) {} + RC6() : BlockCipher_Fixed_Block_Size(1, 32), S(44) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/safer/safer_sk.cpp b/src/block/safer/safer_sk.cpp index 5edd9245b..2e6c3a1d6 100644 --- a/src/block/safer/safer_sk.cpp +++ b/src/block/safer/safer_sk.cpp @@ -43,8 +43,8 @@ void SAFER_SK::encrypt_n(const byte in[], byte out[], size_t blocks) const out[4] = E ^ EK[16*ROUNDS+4]; out[5] = F + EK[16*ROUNDS+5]; out[6] = G + EK[16*ROUNDS+6]; out[7] = H ^ EK[16*ROUNDS+7]; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -81,8 +81,8 @@ void SAFER_SK::decrypt_n(const byte in[], byte out[], size_t blocks) const out[0] = A; out[1] = B; out[2] = C; out[3] = D; out[4] = E; out[5] = F; out[6] = G; out[7] = H; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -127,8 +127,9 @@ BlockCipher* SAFER_SK::clone() const /* * SAFER-SK Constructor */ -SAFER_SK::SAFER_SK(size_t rounds) : BlockCipher(8, 16), - EK(16 * rounds + 8), ROUNDS(rounds) +SAFER_SK::SAFER_SK(size_t rounds) : + BlockCipher_Fixed_Block_Size(16), + EK(16 * rounds + 8), ROUNDS(rounds) { if(ROUNDS > 13 || ROUNDS == 0) throw Invalid_Argument(name() + ": Invalid number of rounds"); diff --git a/src/block/safer/safer_sk.h b/src/block/safer/safer_sk.h index a64d09fb7..5e8d32b0a 100644 --- a/src/block/safer/safer_sk.h +++ b/src/block/safer/safer_sk.h @@ -15,7 +15,7 @@ namespace Botan { /** * SAFER-SK */ -class BOTAN_DLL SAFER_SK : public BlockCipher +class BOTAN_DLL SAFER_SK : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; diff --git a/src/block/seed/seed.cpp b/src/block/seed/seed.cpp index 015d2d48d..408220013 100644 --- a/src/block/seed/seed.cpp +++ b/src/block/seed/seed.cpp @@ -54,8 +54,8 @@ void SEED::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B2, B3, B0, B1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -94,8 +94,8 @@ void SEED::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, B2, B3, B0, B1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/seed/seed.h b/src/block/seed/seed.h index 48fefc9b0..649e28a68 100644 --- a/src/block/seed/seed.h +++ b/src/block/seed/seed.h @@ -15,7 +15,7 @@ namespace Botan { /** * SEED, a Korean block cipher */ -class BOTAN_DLL SEED : public BlockCipher +class BOTAN_DLL SEED : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL SEED : public BlockCipher std::string name() const { return "SEED"; } BlockCipher* clone() const { return new SEED; } - SEED() : BlockCipher(16, 16), K(32) {} + SEED() : BlockCipher_Fixed_Block_Size(16), K(32) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/serpent/serpent.cpp b/src/block/serpent/serpent.cpp index ec37a9e97..1d940cf39 100644 --- a/src/block/serpent/serpent.cpp +++ b/src/block/serpent/serpent.cpp @@ -287,8 +287,8 @@ void Serpent::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, B0, B1, B2, B3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -339,8 +339,8 @@ void Serpent::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, B0, B1, B2, B3); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/serpent/serpent.h b/src/block/serpent/serpent.h index 515a90407..fccdcf214 100644 --- a/src/block/serpent/serpent.h +++ b/src/block/serpent/serpent.h @@ -15,7 +15,7 @@ namespace Botan { /** * Serpent, an AES finalist */ -class BOTAN_DLL Serpent : public BlockCipher +class BOTAN_DLL Serpent : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -24,7 +24,9 @@ class BOTAN_DLL Serpent : public BlockCipher void clear() { zeroise(round_key); } std::string name() const { return "Serpent"; } BlockCipher* clone() const { return new Serpent; } - Serpent() : BlockCipher(16, 16, 32, 8), round_key(132) {} + + Serpent() : BlockCipher_Fixed_Block_Size(16, 32, 8), + round_key(132) {} protected: /** * For use by subclasses using SIMD, asm, etc diff --git a/src/block/serpent_ia32/serp_ia32.cpp b/src/block/serpent_ia32/serp_ia32.cpp index 76814647c..d2f8adb62 100644 --- a/src/block/serpent_ia32/serp_ia32.cpp +++ b/src/block/serpent_ia32/serp_ia32.cpp @@ -49,8 +49,8 @@ void Serpent_IA32::encrypt_n(const byte in[], byte out[], size_t blocks) const for(size_t i = 0; i != blocks; ++i) { botan_serpent_ia32_encrypt(in, out, this->get_round_keys()); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -62,8 +62,8 @@ void Serpent_IA32::decrypt_n(const byte in[], byte out[], size_t blocks) const for(size_t i = 0; i != blocks; ++i) { botan_serpent_ia32_decrypt(in, out, this->get_round_keys()); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/serpent_simd/serp_simd.cpp b/src/block/serpent_simd/serp_simd.cpp index aef37cb99..babe68d40 100644 --- a/src/block/serpent_simd/serp_simd.cpp +++ b/src/block/serpent_simd/serp_simd.cpp @@ -185,8 +185,8 @@ void Serpent_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 4) { serpent_encrypt_4(in, out, KS); - in += 4 * block_size(); - out += 4 * block_size(); + in += 4 * BLOCK_SIZE; + out += 4 * BLOCK_SIZE; blocks -= 4; } @@ -204,8 +204,8 @@ void Serpent_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 4) { serpent_decrypt_4(in, out, KS); - in += 4 * block_size(); - out += 4 * block_size(); + in += 4 * BLOCK_SIZE; + out += 4 * BLOCK_SIZE; blocks -= 4; } diff --git a/src/block/skipjack/skipjack.cpp b/src/block/skipjack/skipjack.cpp index 7f25cc90a..b73972b59 100644 --- a/src/block/skipjack/skipjack.cpp +++ b/src/block/skipjack/skipjack.cpp @@ -108,8 +108,8 @@ void Skipjack::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, W4, W3, W2, W1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -149,8 +149,8 @@ void Skipjack::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, W4, W3, W2, W1); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/skipjack/skipjack.h b/src/block/skipjack/skipjack.h index dff85df6c..73ae28de2 100644 --- a/src/block/skipjack/skipjack.h +++ b/src/block/skipjack/skipjack.h @@ -15,7 +15,7 @@ namespace Botan { /** * Skipjack, a NSA designed cipher used in Fortezza */ -class BOTAN_DLL Skipjack : public BlockCipher +class BOTAN_DLL Skipjack : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL Skipjack : public BlockCipher std::string name() const { return "Skipjack"; } BlockCipher* clone() const { return new Skipjack; } - Skipjack() : BlockCipher(8, 10), FTAB(2560) {} + Skipjack() : BlockCipher_Fixed_Block_Size(10), FTAB(2560) {} private: void key_schedule(const byte[], size_t); diff --git a/src/block/square/square.cpp b/src/block/square/square.cpp index ba86dd931..b1517b990 100644 --- a/src/block/square/square.cpp +++ b/src/block/square/square.cpp @@ -68,8 +68,8 @@ void Square::encrypt_n(const byte in[], byte out[], size_t blocks) const out[14] = SE[get_byte(3, B2)] ^ ME[30]; out[15] = SE[get_byte(3, B3)] ^ ME[31]; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -130,8 +130,8 @@ void Square::decrypt_n(const byte in[], byte out[], size_t blocks) const out[14] = SD[get_byte(3, B2)] ^ MD[30]; out[15] = SD[get_byte(3, B3)] ^ MD[31]; - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/square/square.h b/src/block/square/square.h index 0a134bcb5..d6df63131 100644 --- a/src/block/square/square.h +++ b/src/block/square/square.h @@ -15,7 +15,7 @@ namespace Botan { /** * Square */ -class BOTAN_DLL Square : public BlockCipher +class BOTAN_DLL Square : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,9 @@ class BOTAN_DLL Square : public BlockCipher std::string name() const { return "Square"; } BlockCipher* clone() const { return new Square; } - Square() : BlockCipher(16, 16), EK(28), DK(28), ME(32), MD(32) {} + Square() : BlockCipher_Fixed_Block_Size(16), + EK(28), DK(28), ME(32), MD(32) {} + private: void key_schedule(const byte[], size_t); diff --git a/src/block/tea/tea.cpp b/src/block/tea/tea.cpp index 328786a14..4ef995a7c 100644 --- a/src/block/tea/tea.cpp +++ b/src/block/tea/tea.cpp @@ -30,8 +30,8 @@ void TEA::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, L, R); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -55,8 +55,8 @@ void TEA::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, L, R); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/tea/tea.h b/src/block/tea/tea.h index eeab13cbc..a7318ba5c 100644 --- a/src/block/tea/tea.h +++ b/src/block/tea/tea.h @@ -15,7 +15,7 @@ namespace Botan { /** * TEA */ -class BOTAN_DLL TEA : public BlockCipher +class BOTAN_DLL TEA : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL TEA : public BlockCipher std::string name() const { return "TEA"; } BlockCipher* clone() const { return new TEA; } - TEA() : BlockCipher(8, 16), K(4) {} + TEA() : BlockCipher_Fixed_Block_Size(16), K(4) {} private: void key_schedule(const byte[], size_t); SecureVector<u32bit> K; diff --git a/src/block/twofish/twofish.cpp b/src/block/twofish/twofish.cpp index a573c2ec8..41bc7ca1c 100644 --- a/src/block/twofish/twofish.cpp +++ b/src/block/twofish/twofish.cpp @@ -57,8 +57,8 @@ void Twofish::encrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, C, D, A, B); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -108,8 +108,8 @@ void Twofish::decrypt_n(const byte in[], byte out[], size_t blocks) const store_le(out, C, D, A, B); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/twofish/twofish.h b/src/block/twofish/twofish.h index 38263af98..a212bd285 100644 --- a/src/block/twofish/twofish.h +++ b/src/block/twofish/twofish.h @@ -15,7 +15,7 @@ namespace Botan { /** * Twofish, an AES finalist */ -class BOTAN_DLL Twofish : public BlockCipher +class BOTAN_DLL Twofish : public BlockCipher_Fixed_Block_Size<16> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,9 @@ class BOTAN_DLL Twofish : public BlockCipher std::string name() const { return "Twofish"; } BlockCipher* clone() const { return new Twofish; } - Twofish() : BlockCipher(16, 16, 32, 8), SB(1024), RK(40) {} + Twofish() : BlockCipher_Fixed_Block_Size(16, 32, 8), + SB(1024), RK(40) {} + private: void key_schedule(const byte[], size_t); diff --git a/src/block/xtea/xtea.cpp b/src/block/xtea/xtea.cpp index ba07ba57c..597eedd07 100644 --- a/src/block/xtea/xtea.cpp +++ b/src/block/xtea/xtea.cpp @@ -64,8 +64,8 @@ void XTEA::encrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 4) { xtea_encrypt_4(in, out, &(this->EK[0])); - in += 4 * block_size(); - out += 4 * block_size(); + in += 4 * BLOCK_SIZE; + out += 4 * BLOCK_SIZE; blocks -= 4; } @@ -82,8 +82,8 @@ void XTEA::encrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, L, R); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } @@ -95,8 +95,8 @@ void XTEA::decrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 4) { xtea_decrypt_4(in, out, &(this->EK[0])); - in += 4 * block_size(); - out += 4 * block_size(); + in += 4 * BLOCK_SIZE; + out += 4 * BLOCK_SIZE; blocks -= 4; } @@ -113,8 +113,8 @@ void XTEA::decrypt_n(const byte in[], byte out[], size_t blocks) const store_be(out, L, R); - in += block_size(); - out += block_size(); + in += BLOCK_SIZE; + out += BLOCK_SIZE; } } diff --git a/src/block/xtea/xtea.h b/src/block/xtea/xtea.h index c870f588a..539725be8 100644 --- a/src/block/xtea/xtea.h +++ b/src/block/xtea/xtea.h @@ -15,7 +15,7 @@ namespace Botan { /** * XTEA */ -class BOTAN_DLL XTEA : public BlockCipher +class BOTAN_DLL XTEA : public BlockCipher_Fixed_Block_Size<8> { public: void encrypt_n(const byte in[], byte out[], size_t blocks) const; @@ -25,7 +25,7 @@ class BOTAN_DLL XTEA : public BlockCipher std::string name() const { return "XTEA"; } BlockCipher* clone() const { return new XTEA; } - XTEA() : BlockCipher(8, 16), EK(64) {} + XTEA() : BlockCipher_Fixed_Block_Size(16), EK(64) {} protected: /** * @return const reference to the key schedule diff --git a/src/block/xtea_simd/xtea_simd.cpp b/src/block/xtea_simd/xtea_simd.cpp index 5b73c7bb9..831cc0359 100644 --- a/src/block/xtea_simd/xtea_simd.cpp +++ b/src/block/xtea_simd/xtea_simd.cpp @@ -99,8 +99,8 @@ void XTEA_SIMD::encrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 8) { xtea_encrypt_8(in, out, KS); - in += 8 * block_size(); - out += 8 * block_size(); + in += 8 * BLOCK_SIZE; + out += 8 * BLOCK_SIZE; blocks -= 8; } @@ -118,8 +118,8 @@ void XTEA_SIMD::decrypt_n(const byte in[], byte out[], size_t blocks) const while(blocks >= 8) { xtea_decrypt_8(in, out, KS); - in += 8 * block_size(); - out += 8 * block_size(); + in += 8 * BLOCK_SIZE; + out += 8 * BLOCK_SIZE; blocks -= 8; } diff --git a/src/cert/pkcs10/pkcs10.h b/src/cert/pkcs10/pkcs10.h index d1be9e0d3..bd01fb6b5 100644 --- a/src/cert/pkcs10/pkcs10.h +++ b/src/cert/pkcs10/pkcs10.h @@ -9,6 +9,7 @@ #define BOTAN_PKCS10_H__ #include <botan/x509_obj.h> +#include <botan/x509_dn.h> #include <botan/pkcs8.h> #include <botan/datastor.h> #include <vector> diff --git a/src/cert/x509cert/x509cert.h b/src/cert/x509cert/x509cert.h index dc7ef4dbb..754553f3d 100644 --- a/src/cert/x509cert/x509cert.h +++ b/src/cert/x509cert/x509cert.h @@ -9,6 +9,7 @@ #define BOTAN_X509_CERTS_H__ #include <botan/x509_obj.h> +#include <botan/x509_dn.h> #include <botan/x509_key.h> #include <botan/datastor.h> #include <botan/pubkey_enums.h> diff --git a/src/constructs/fpe/fpe.cpp b/src/constructs/fpe/fpe.cpp index d7101c544..3747171c2 100644 --- a/src/constructs/fpe/fpe.cpp +++ b/src/constructs/fpe/fpe.cpp @@ -105,10 +105,10 @@ FPE_Encryptor::FPE_Encryptor(const SymmetricKey& key, if(n_bin.size() > MAX_N_BYTES) throw std::runtime_error("N is too large for FPE encryption"); - mac->update_be(n_bin.size(), 4); + mac->update_be((u32bit)n_bin.size()); mac->update(&n_bin[0], n_bin.size()); - mac->update_be(tweak.size(), 4); + mac->update_be((u32bit)tweak.size()); mac->update(&tweak[0], tweak.size()); mac_n_t = mac->final(); @@ -119,9 +119,9 @@ BigInt FPE_Encryptor::operator()(u32bit round_no, const BigInt& R) SecureVector<byte> r_bin = BigInt::encode(R); mac->update(mac_n_t); - mac->update_be(round_no, 4); + mac->update_be(round_no); - mac->update_be(r_bin.size(), 4); + mac->update_be((u32bit)r_bin.size()); mac->update(&r_bin[0], r_bin.size()); SecureVector<byte> X = mac->final(); diff --git a/src/hash/mdx_hash/mdx_hash.cpp b/src/hash/mdx_hash/mdx_hash.cpp index 84b97fd7f..f82c971f8 100644 --- a/src/hash/mdx_hash/mdx_hash.cpp +++ b/src/hash/mdx_hash/mdx_hash.cpp @@ -50,22 +50,22 @@ void MDx_HashFunction::add_data(const byte input[], size_t length) { buffer.copy(position, input, length); - if(position + length >= hash_block_size()) + if(position + length >= buffer.size()) { compress_n(&buffer[0], 1); - input += (hash_block_size() - position); - length -= (hash_block_size() - position); + input += (buffer.size() - position); + length -= (buffer.size() - position); position = 0; } } - const size_t full_blocks = length / hash_block_size(); - const size_t remaining = length % hash_block_size(); + const size_t full_blocks = length / buffer.size(); + const size_t remaining = length % buffer.size(); if(full_blocks) compress_n(input, full_blocks); - buffer.copy(position, input + full_blocks * hash_block_size(), remaining); + buffer.copy(position, input + full_blocks * buffer.size(), remaining); position += remaining; } @@ -75,16 +75,16 @@ void MDx_HashFunction::add_data(const byte input[], size_t length) void MDx_HashFunction::final_result(byte output[]) { buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01); - for(size_t i = position+1; i != hash_block_size(); ++i) + for(size_t i = position+1; i != buffer.size(); ++i) buffer[i] = 0; - if(position >= hash_block_size() - COUNT_SIZE) + if(position >= buffer.size() - COUNT_SIZE) { compress_n(&buffer[0], 1); zeroise(buffer); } - write_count(&buffer[hash_block_size() - COUNT_SIZE]); + write_count(&buffer[buffer.size() - COUNT_SIZE]); compress_n(&buffer[0], 1); copy_out(output); diff --git a/src/utils/buf_comp/buf_comp.h b/src/utils/buf_comp/buf_comp.h index a64d807dd..0b0ef6e16 100644 --- a/src/utils/buf_comp/buf_comp.h +++ b/src/utils/buf_comp/buf_comp.h @@ -14,9 +14,8 @@ namespace Botan { /** -* This class represents any kind of computation which -* uses an internal state, -* such as hash functions. +* This class represents any kind of computation which uses an internal +* state, such as hash functions or MACs */ class BOTAN_DLL BufferedComputation { @@ -48,9 +47,13 @@ class BOTAN_DLL BufferedComputation add_data(&in[0], in.size()); } - template<typename T> void update_be(const T in, size_t upto = sizeof(T)) + /** + * Add an integer in big-endian order + * @param in the value + */ + template<typename T> void update_be(const T in) { - for(size_t i = 0; i != std::min(upto, sizeof(T)); ++i) + for(size_t i = 0; i != sizeof(T); ++i) { byte b = get_byte(i, in); add_data(&b, 1); |