diff options
Diffstat (limited to 'src/sym_algo')
-rw-r--r-- | src/sym_algo/sym_algo.h | 93 | ||||
-rw-r--r-- | src/sym_algo/symkey.cpp | 139 | ||||
-rw-r--r-- | src/sym_algo/symkey.h | 60 |
3 files changed, 292 insertions, 0 deletions
diff --git a/src/sym_algo/sym_algo.h b/src/sym_algo/sym_algo.h new file mode 100644 index 000000000..02343ed56 --- /dev/null +++ b/src/sym_algo/sym_algo.h @@ -0,0 +1,93 @@ +/** +* Symmetric Algorithm Base Class +* (C) 1999-2007 Jack Lloyd +*/ + +#ifndef BOTAN_SYMMETRIC_ALGORITHM_H__ +#define BOTAN_SYMMETRIC_ALGORITHM_H__ + +#include <botan/types.h> +#include <botan/exceptn.h> +#include <botan/symkey.h> + +namespace Botan { + +/** +* This class represents a symmetric algorithm object. +*/ +class BOTAN_DLL SymmetricAlgorithm + { + public: + + /** + * The maximum allowed key length. + */ + const u32bit MAXIMUM_KEYLENGTH; + + /** + * The minimal allowed key length. + */ + const u32bit MINIMUM_KEYLENGTH; + + /** + * A valid keylength is a multiple of this value. + */ + const u32bit KEYLENGTH_MULTIPLE; + + /** + * The name of the algorithm. + * @return the name of the algorithm + */ + virtual std::string name() const = 0; + + /** + * Set the symmetric key of this object. + * @param key the SymmetricKey to be set. + */ + void set_key(const SymmetricKey& skey) throw(Invalid_Key_Length) + { set_key(skey.begin(), skey.length()); } + + /** + * Set the symmetric key of this object. + * @param key the to be set as a byte array. + * @param the length of the byte array. + */ + void set_key(const byte skey[], u32bit length) throw(Invalid_Key_Length) + { + if(!valid_keylength(length)) + throw Invalid_Key_Length(name(), length); + key(skey, length); + } + + /** + * Check whether a given key length is valid for this algorithm. + * @param length the key length to be checked. + * @return true if the key length is valid. + */ + bool valid_keylength(u32bit length) const + { + return ((length >= MINIMUM_KEYLENGTH) && + (length <= MAXIMUM_KEYLENGTH) && + (length % KEYLENGTH_MULTIPLE == 0)); + } + + /** + * Construct a SymmetricAlgorithm. + * @param key_min the minimum allowed key length + * @param key_max the maximum allowed key length + * @param key_mod any valid key length must be a multiple of this value + */ + SymmetricAlgorithm(u32bit key_min, u32bit key_max, u32bit key_mod) : + MAXIMUM_KEYLENGTH(key_max ? key_max : key_min), + MINIMUM_KEYLENGTH(key_min), + KEYLENGTH_MULTIPLE(key_mod) + {} + + virtual ~SymmetricAlgorithm() {} + private: + virtual void key(const byte[], u32bit) = 0; + }; + +} + +#endif diff --git a/src/sym_algo/symkey.cpp b/src/sym_algo/symkey.cpp new file mode 100644 index 000000000..7cc1bce64 --- /dev/null +++ b/src/sym_algo/symkey.cpp @@ -0,0 +1,139 @@ +/************************************************* +* OctetString Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/symkey.h> +#include <botan/xor_buf.h> +#include <botan/rng.h> +#include <botan/pipe.h> +#include <botan/hex.h> +#include <algorithm> + +namespace Botan { + +/************************************************* +* Create an OctetString from RNG output * +*************************************************/ +OctetString::OctetString(RandomNumberGenerator& rng, + u32bit length) + { + bits.create(length); + rng.randomize(bits, length); + } + +/************************************************* +* Create an OctetString from a hex string * +*************************************************/ +void OctetString::change(const std::string& hex_string) + { + SecureVector<byte> hex; + for(u32bit j = 0; j != hex_string.length(); ++j) + if(Hex_Decoder::is_valid(hex_string[j])) + hex.append(hex_string[j]); + + if(hex.size() % 2 != 0) + throw Invalid_Argument("OctetString: hex string must encode full bytes"); + bits.create(hex.size() / 2); + for(u32bit j = 0; j != bits.size(); ++j) + bits[j] = Hex_Decoder::decode(hex.begin() + 2*j); + } + +/************************************************* +* Create an OctetString from a byte string * +*************************************************/ +void OctetString::change(const byte in[], u32bit n) + { + bits.create(n); + bits.copy(in, n); + } + +/************************************************* +* Set the parity of each key byte to odd * +*************************************************/ +void OctetString::set_odd_parity() + { + const byte ODD_PARITY[256] = { + 0x01, 0x01, 0x02, 0x02, 0x04, 0x04, 0x07, 0x07, 0x08, 0x08, 0x0B, 0x0B, + 0x0D, 0x0D, 0x0E, 0x0E, 0x10, 0x10, 0x13,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8, 0xCB, 0xCB, + 0xCD, 0xCD, 0xCE, 0xCE, 0xD0, 0xD0, 0xD3, 0xD3, 0xD5, 0xD5, 0xD6, 0xD6, + 0xD9, 0xD9, 0xDA, 0xDA, 0xDC, 0xDC, 0xDF, 0xDF, 0xE0, 0xE0, 0xE3, 0xE3, + 0xE5, 0xE5, 0xE6, 0xE6, 0xE9, 0xE9, 0xEA, 0xEA, 0xEC, 0xEC, 0xEF, 0xEF, + 0xF1, 0xF1, 0xF2, 0xF2, 0xF4, 0xF4, 0xF7, 0xF7, 0xF8, 0xF8, 0xFB, 0xFB, + 0xFD, 0xFD, 0xFE, 0xFE }; + + for(u32bit j = 0; j != bits.size(); ++j) + bits[j] = ODD_PARITY[bits[j]]; + } + +/************************************************* +* Hex encode an OctetString * +*************************************************/ +std::string OctetString::as_string() const + { + Pipe pipe(new Hex_Encoder); + pipe.process_msg(bits); + return pipe.read_all_as_string(); + } + +/************************************************* +* XOR Operation for OctetStrings * +*************************************************/ +OctetString& OctetString::operator^=(const OctetString& k) + { + if(&k == this) { bits.clear(); return (*this); } + xor_buf(bits.begin(), k.begin(), std::min(length(), k.length())); + return (*this); + } + +/************************************************* +* Equality Operation for OctetStrings * +*************************************************/ +bool operator==(const OctetString& s1, const OctetString& s2) + { + return (s1.bits_of() == s2.bits_of()); + } + +/************************************************* +* Unequality Operation for OctetStrings * +*************************************************/ +bool operator!=(const OctetString& s1, const OctetString& s2) + { + return !(s1 == s2); + } + +/************************************************* +* Append Operation for OctetStrings * +*************************************************/ +OctetString operator+(const OctetString& k1, const OctetString& k2) + { + return OctetString(SecureVector<byte>(k1.bits_of(), k2.bits_of())); + } + +/************************************************* +* XOR Operation for OctetStrings * +*************************************************/ +OctetString operator^(const OctetString& k1, const OctetString& k2) + { + SecureVector<byte> ret(std::max(k1.length(), k2.length())); + ret.copy(k1.begin(), k1.length()); + xor_buf(ret, k2.begin(), k2.length()); + return OctetString(ret); + } + +} diff --git a/src/sym_algo/symkey.h b/src/sym_algo/symkey.h new file mode 100644 index 000000000..f3211eb0f --- /dev/null +++ b/src/sym_algo/symkey.h @@ -0,0 +1,60 @@ +/************************************************* +* OctetString Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_SYMKEY_H__ +#define BOTAN_SYMKEY_H__ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/************************************************* +* Octet String * +*************************************************/ +class BOTAN_DLL OctetString + { + public: + u32bit length() const { return bits.size(); } + SecureVector<byte> bits_of() const { return bits; } + + const byte* begin() const { return bits.begin(); } + const byte* end() const { return bits.end(); } + + std::string as_string() const; + + OctetString& operator^=(const OctetString&); + + void set_odd_parity(); + + void change(const std::string&); + void change(const byte[], u32bit); + void change(const MemoryRegion<byte>& in) { bits = in; } + + OctetString(class RandomNumberGenerator&, u32bit len); + OctetString(const std::string& str = "") { change(str); } + OctetString(const byte in[], u32bit len) { change(in, len); } + OctetString(const MemoryRegion<byte>& in) { change(in); } + private: + SecureVector<byte> bits; + }; + +/************************************************* +* Operations on Octet Strings * +*************************************************/ +BOTAN_DLL bool operator==(const OctetString&, const OctetString&); +BOTAN_DLL bool operator!=(const OctetString&, const OctetString&); +BOTAN_DLL OctetString operator+(const OctetString&, const OctetString&); +BOTAN_DLL OctetString operator^(const OctetString&, const OctetString&); + +/************************************************* +* Alternate Names * +*************************************************/ +typedef OctetString SymmetricKey; +typedef OctetString InitializationVector; + +} + +#endif |