diff options
author | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
commit | a2c99d3270eb73ef2db5704fc54356c6b75096f8 (patch) | |
tree | ad3d6c4fcc8dd0f403f8105598943616246fe172 /include |
Initial checkin1.5.6
Diffstat (limited to 'include')
160 files changed, 9025 insertions, 0 deletions
diff --git a/include/adler32.h b/include/adler32.h new file mode 100644 index 000000000..f6b5f172d --- /dev/null +++ b/include/adler32.h @@ -0,0 +1,33 @@ +/************************************************* +* Adler32 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ADLER32_H__ +#define BOTAN_ADLER32_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Adler32 * +*************************************************/ +class Adler32 : public HashFunction + { + public: + void clear() throw() { S1 = 1; S2 = 0; } + std::string name() const { return "Adler32"; } + HashFunction* clone() const { return new Adler32; } + Adler32() : HashFunction(4) { clear(); } + ~Adler32() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void hash(const byte[], u32bit); + u32bit S1, S2; + }; + +} + +#endif diff --git a/include/aes.h b/include/aes.h new file mode 100644 index 000000000..377d7479a --- /dev/null +++ b/include/aes.h @@ -0,0 +1,72 @@ +/************************************************* +* AES Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_AES_H__ +#define BOTAN_AES_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* AES * +*************************************************/ +class AES : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "AES"; } + BlockCipher* clone() const { return new AES; } + AES() : BlockCipher(16, 16, 32, 8) { ROUNDS = 14; } + AES(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + static u32bit S(u32bit); + static const byte SE[256], SD[256]; + static const u32bit TE0[256], TE1[256], TE2[256], TE3[256], + TD0[256], TD1[256], TD2[256], TD3[256]; + SecureBuffer<u32bit, 52> EK, DK; + SecureBuffer<byte, 32> ME, MD; + u32bit ROUNDS; + }; + +/************************************************* +* AES-128 * +*************************************************/ +class AES_128 : public AES + { + public: + std::string name() const { return "AES-128"; } + BlockCipher* clone() const { return new AES_128; } + AES_128() : AES(16) {} + }; + +/************************************************* +* AES-192 * +*************************************************/ +class AES_192 : public AES + { + public: + std::string name() const { return "AES-192"; } + BlockCipher* clone() const { return new AES_192; } + AES_192() : AES(24) {} + }; + +/************************************************* +* AES-256 * +*************************************************/ +class AES_256 : public AES + { + public: + std::string name() const { return "AES-256"; } + BlockCipher* clone() const { return new AES_256; } + AES_256() : AES(32) {} + }; + +} + +#endif diff --git a/include/allocate.h b/include/allocate.h new file mode 100644 index 000000000..a3f59cf1b --- /dev/null +++ b/include/allocate.h @@ -0,0 +1,36 @@ +/************************************************* +* Allocator Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ALLOCATOR_H__ +#define BOTAN_ALLOCATOR_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/************************************************* +* Allocator * +*************************************************/ +class Allocator + { + public: + virtual void* allocate(u32bit) = 0; + virtual void deallocate(void*, u32bit) = 0; + + virtual void init() {} + virtual void destroy() {} + + virtual ~Allocator() {} + }; + +/************************************************* +* Get an allocator * +*************************************************/ +Allocator* get_allocator(const std::string& = ""); + +} + +#endif diff --git a/include/arc4.h b/include/arc4.h new file mode 100644 index 000000000..7cba9a620 --- /dev/null +++ b/include/arc4.h @@ -0,0 +1,36 @@ +/************************************************* +* ARC4 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ARC4_H__ +#define BOTAN_ARC4_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* ARC4 * +*************************************************/ +class ARC4 : public StreamCipher + { + public: + void clear() throw(); + std::string name() const; + StreamCipher* clone() const { return new ARC4(SKIP); } + ARC4(u32bit = 0); + ~ARC4() { clear(); } + private: + void cipher(const byte[], byte[], u32bit); + void key(const byte[], u32bit); + void generate(); + const u32bit SKIP; + SecureBuffer<byte, 1024> buffer; + SecureBuffer<u32bit, 256> state; + u32bit X, Y, position; + }; + +} + +#endif diff --git a/include/asn1_int.h b/include/asn1_int.h new file mode 100644 index 000000000..40ddbb444 --- /dev/null +++ b/include/asn1_int.h @@ -0,0 +1,55 @@ +/************************************************* +* ASN.1 Internals Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ASN1_H__ +#define BOTAN_ASN1_H__ + +#include <botan/bigint.h> +#include <botan/enums.h> + +namespace Botan { + +/************************************************* +* Basic ASN.1 Object Interface * +*************************************************/ +class ASN1_Object + { + public: + virtual void encode_into(class DER_Encoder&) const = 0; + virtual ~ASN1_Object() {} + }; + +/************************************************* +* ASN.1 Utility Functions * +*************************************************/ +class DataSource; + +namespace ASN1 { + +SecureVector<byte> put_in_sequence(const MemoryRegion<byte>&); +bool maybe_BER(DataSource&); + +} + +/************************************************* +* General BER Decoding Error Exception * +*************************************************/ +struct BER_Decoding_Error : public Decoding_Error + { + BER_Decoding_Error(const std::string&); + }; + +/************************************************* +* Exception For Incorrect BER Taggings * +*************************************************/ +struct BER_Bad_Tag : public BER_Decoding_Error + { + BER_Bad_Tag(const std::string&, ASN1_Tag); + BER_Bad_Tag(const std::string&, ASN1_Tag, ASN1_Tag); + }; + +} + +#endif diff --git a/include/asn1_obj.h b/include/asn1_obj.h new file mode 100644 index 000000000..c36c2ac14 --- /dev/null +++ b/include/asn1_obj.h @@ -0,0 +1,209 @@ +/************************************************* +* Common ASN.1 Objects Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ASN1_OBJ_H__ +#define BOTAN_ASN1_OBJ_H__ + +#include <botan/asn1_int.h> +#include <botan/der_enc.h> +#include <botan/ber_dec.h> +#include <map> +#include <vector> + +namespace Botan { + +/************************************************* +* Algorithm Identifier * +*************************************************/ +class AlgorithmIdentifier : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + AlgorithmIdentifier() {} + AlgorithmIdentifier(const OID&, const MemoryRegion<byte>&); + AlgorithmIdentifier(const std::string&, const MemoryRegion<byte>&); + public: + OID oid; + SecureVector<byte> parameters; + }; + +/************************************************* +* Extension * +*************************************************/ +class Extension : public ASN1_Object + { + public: + bool critical; + OID oid; + SecureVector<byte> value; + + void encode_into(DER_Encoder&) const; + + Extension() { critical = false; } + Extension(const OID&, const MemoryRegion<byte>&); + Extension(const std::string&, const MemoryRegion<byte>&); + }; + +/************************************************* +* Attribute * +*************************************************/ +class Attribute : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + OID oid; + SecureVector<byte> parameters; + + Attribute() {} + Attribute(const OID&, const MemoryRegion<byte>&); + Attribute(const std::string&, const MemoryRegion<byte>&); + }; + +/************************************************* +* X.509 Time * +*************************************************/ +class X509_Time : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + std::string as_string() const; + std::string readable_string() const; + bool time_is_set() const; + + s32bit cmp(const X509_Time&) const; + + X509_Time(u64bit); + X509_Time(const std::string& = ""); + X509_Time(const std::string&, ASN1_Tag); + private: + bool passes_sanity_check() const; + u32bit year, month, day, hour, minute, second; + ASN1_Tag tag; + }; + +/************************************************* +* Simple String * +*************************************************/ +class ASN1_String : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + 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 X509_DN : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + std::multimap<OID, std::string> get_attributes() const; + std::vector<std::string> get_attribute(const std::string&) 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>&); + SecureVector<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; + SecureVector<byte> dn_bits; + }; + +/************************************************* +* Alternative Name * +*************************************************/ +class AlternativeName : public ASN1_Object + { + public: + void encode_into(DER_Encoder&) const; + + void add_attribute(const std::string&, const std::string&); + std::multimap<std::string, std::string> get_attributes() const; + + void add_othername(const OID&, const std::string&, ASN1_Tag); + std::multimap<OID, ASN1_String> get_othernames() const; + + bool has_items() const; + + AlternativeName(const std::string& = "", const std::string& = "", + const std::string& = ""); + private: + std::multimap<std::string, std::string> alt_info; + std::multimap<OID, ASN1_String> othernames; + }; + +/************************************************* +* Comparison Operations * +*************************************************/ +bool operator==(const AlgorithmIdentifier&, const AlgorithmIdentifier&); +bool operator!=(const AlgorithmIdentifier&, const AlgorithmIdentifier&); + +bool operator==(const X509_Time&, const X509_Time&); +bool operator!=(const X509_Time&, const X509_Time&); +bool operator<=(const X509_Time&, const X509_Time&); +bool operator>=(const X509_Time&, const X509_Time&); + +bool operator==(const X509_DN&, const X509_DN&); +bool operator!=(const X509_DN&, const X509_DN&); +bool operator<(const X509_DN&, const X509_DN&); + +s32bit validity_check(const X509_Time&, const X509_Time&, u64bit); + +bool is_string_type(ASN1_Tag); + +/************************************************* +* DER Encoding Functions * +*************************************************/ +namespace DER { + +//void encode(DER_Encoder&, const Attribute&); +//void encode(DER_Encoder&, const ASN1_String&); +void encode(DER_Encoder&, const ASN1_String&, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + +} + +/************************************************* +* BER Decoding Functions * +*************************************************/ +namespace BER { + +void decode(BER_Decoder&, AlgorithmIdentifier&); +void decode(BER_Decoder&, Extension&); +void decode(BER_Decoder&, Attribute&); +void decode(BER_Decoder&, X509_Time&); +void decode(BER_Decoder&, ASN1_String&); +void decode(BER_Decoder&, ASN1_String&, ASN1_Tag, ASN1_Tag); +void decode(BER_Decoder&, X509_DN&); +void decode(BER_Decoder&, AlternativeName&); +void decode(BER_Decoder&, Key_Constraints&); + +} + +} + +#endif diff --git a/include/asn1_oid.h b/include/asn1_oid.h new file mode 100644 index 000000000..b2bdd4867 --- /dev/null +++ b/include/asn1_oid.h @@ -0,0 +1,49 @@ +/************************************************* +* ASN.1 OID Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ASN1_OID_H__ +#define BOTAN_ASN1_OID_H__ + +#include <botan/asn1_int.h> +#include <string> +#include <vector> + +namespace Botan { + +/************************************************* +* ASN.1 Object Identifier * +*************************************************/ +class OID : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + + bool is_empty() const { return id.size() == 0; } + std::vector<u32bit> get_id() const { return id; } + std::string as_string() const; + + bool operator==(const OID&) const; + void clear(); + + OID& operator+=(u32bit); + OID(const std::string& = ""); + private: + std::vector<u32bit> id; + }; + +/************************************************* +* Append another component onto the OID * +*************************************************/ +OID operator+(const OID&, u32bit); + +/************************************************* +* Compare two OIDs * +*************************************************/ +bool operator!=(const OID&, const OID&); +bool operator<(const OID&, const OID&); + +} + +#endif diff --git a/include/base.h b/include/base.h new file mode 100644 index 000000000..bccf92e25 --- /dev/null +++ b/include/base.h @@ -0,0 +1,168 @@ +/************************************************* +* Base Classes Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BASE_H__ +#define BOTAN_BASE_H__ + +#include <botan/exceptn.h> +#include <botan/symkey.h> + +namespace Botan { + +/************************************************* +* Constants * +*************************************************/ +static const u32bit DEFAULT_BUFFERSIZE = BOTAN_DEFAULT_BUFFER_SIZE; + +/************************************************* +* Algorithm * +*************************************************/ +class Algorithm + { + public: + virtual void clear() throw() {}; + virtual std::string name() const = 0; + virtual ~Algorithm() {} + Algorithm() {} + private: + Algorithm(const Algorithm&) {} + Algorithm& operator=(const Algorithm&) { return (*this); } + }; + +/************************************************* +* Symmetric Algorithm * +*************************************************/ +class SymmetricAlgorithm : public virtual Algorithm + { + public: + const u32bit MAXIMUM_KEYLENGTH, MINIMUM_KEYLENGTH, KEYLENGTH_MULTIPLE; + void set_key(const SymmetricKey&) throw(Invalid_Key_Length); + void set_key(const byte[], u32bit) throw(Invalid_Key_Length); + bool valid_keylength(u32bit) const; + SymmetricAlgorithm(u32bit, u32bit, u32bit); + virtual ~SymmetricAlgorithm() {} + private: + virtual void key(const byte[], u32bit) = 0; + }; + +/************************************************* +* Block Cipher * +*************************************************/ +class BlockCipher : public SymmetricAlgorithm + { + public: + const u32bit BLOCK_SIZE; + void encrypt(const byte in[], byte out[]) const { enc(in, out); } + void decrypt(const byte in[], byte out[]) const { dec(in, out); } + void encrypt(byte block[]) const { enc(block, block); } + void decrypt(byte block[]) const { dec(block, block); } + virtual BlockCipher* clone() const = 0; + BlockCipher(u32bit, u32bit, u32bit = 0, u32bit = 1); + virtual ~BlockCipher() {} + private: + virtual void enc(const byte[], byte[]) const = 0; + virtual void dec(const byte[], byte[]) const = 0; + }; + +/************************************************* +* Stream Cipher * +*************************************************/ +class StreamCipher : public SymmetricAlgorithm + { + public: + const u32bit IV_LENGTH; + void encrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); } + void decrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); } + void encrypt(byte in[], u32bit len) { cipher(in, in, len); } + void decrypt(byte in[], u32bit len) { cipher(in, in, len); } + + virtual void resync(const byte[], u32bit); + virtual void seek(u32bit); + + virtual StreamCipher* clone() const = 0; + StreamCipher(u32bit, u32bit = 0, u32bit = 1, u32bit = 0); + virtual ~StreamCipher() {} + private: + virtual void cipher(const byte[], byte[], u32bit) = 0; + }; + +/************************************************* +* Buffered Computation * +*************************************************/ +class BufferedComputation : public virtual Algorithm + { + public: + const u32bit OUTPUT_LENGTH; + void update(const byte[], u32bit); + void update(const MemoryRegion<byte>&); + void update(const std::string&); + void update(byte); + void final(byte out[]) { final_result(out); } + SecureVector<byte> final(); + SecureVector<byte> process(const byte[], u32bit); + SecureVector<byte> process(const MemoryRegion<byte>&); + SecureVector<byte> process(const std::string&); + BufferedComputation(u32bit); + virtual ~BufferedComputation() {} + private: + virtual void add_data(const byte[], u32bit) = 0; + virtual void final_result(byte[]) = 0; + }; + +/************************************************* +* Hash Function * +*************************************************/ +class HashFunction : public BufferedComputation + { + public: + const u32bit HASH_BLOCK_SIZE; + virtual HashFunction* clone() const = 0; + HashFunction(u32bit, u32bit = 0); + virtual ~HashFunction() {} + }; + +/************************************************* +* Message Authentication Code * +*************************************************/ +class MessageAuthenticationCode : public BufferedComputation, + public SymmetricAlgorithm + { + public: + virtual std::string name() const = 0; + virtual MessageAuthenticationCode* clone() const = 0; + virtual bool verify_mac(const byte[], u32bit); + MessageAuthenticationCode(u32bit, u32bit, u32bit = 0, u32bit = 1); + virtual ~MessageAuthenticationCode() {} + }; + +/************************************************* +* Entropy Source * +*************************************************/ +class EntropySource + { + public: + virtual u32bit slow_poll(byte[], u32bit) = 0; + virtual u32bit fast_poll(byte[], u32bit); + virtual ~EntropySource() {} + }; + +/************************************************* +* Random Number Generator * +*************************************************/ +class RandomNumberGenerator : public Algorithm + { + public: + virtual void randomize(byte[], u32bit) throw(PRNG_Unseeded) = 0; + virtual bool is_seeded() const { return true; } + void add_entropy(const byte[], u32bit); + u32bit add_entropy(EntropySource&, bool = true); + virtual ~RandomNumberGenerator() {} + private: + virtual void add_randomness(const byte[], u32bit) = 0; + }; + +} + +#endif diff --git a/include/base64.h b/include/base64.h new file mode 100644 index 000000000..98b195e58 --- /dev/null +++ b/include/base64.h @@ -0,0 +1,58 @@ +/************************************************* +* Base64 Encoder/Decoder Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BASE64_H__ +#define BOTAN_BASE64_H__ + +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* Base64 Encoder * +*************************************************/ +class Base64_Encoder : public Filter + { + public: + static void encode(const byte[3], byte[4]); + + void write(const byte[], u32bit); + void end_msg(); + Base64_Encoder(bool = false, u32bit = 72); + private: + void encode_and_send(const byte[], u32bit); + void do_output(const byte[], u32bit); + static const byte BIN_TO_BASE64[64]; + + const u32bit line_length; + SecureVector<byte> in, out; + u32bit position, counter; + }; + +/************************************************* +* Base64 Decoder * +*************************************************/ +class Base64_Decoder : public Filter + { + public: + static void decode(const byte[4], byte[3]); + static bool is_valid(byte); + + void write(const byte[], u32bit); + void end_msg(); + Base64_Decoder(Decoder_Checking = NONE); + private: + void decode_and_send(const byte[], u32bit); + void handle_bad_char(byte); + static const byte BASE64_TO_BIN[256]; + + const Decoder_Checking checking; + SecureVector<byte> in, out; + u32bit position; + }; + +} + +#endif diff --git a/include/basefilt.h b/include/basefilt.h new file mode 100644 index 000000000..0febb3989 --- /dev/null +++ b/include/basefilt.h @@ -0,0 +1,55 @@ +/************************************************* +* Basic Filters Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BASEFILT_H__ +#define BOTAN_BASEFILT_H__ + +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* Chain * +*************************************************/ +class Chain : public Fanout_Filter + { + public: + void write(const byte input[], u32bit length) { send(input, length); } + + Chain(Filter* = 0, Filter* = 0, Filter* = 0, Filter* = 0); + Chain(Filter*[], u32bit); + }; + +/************************************************* +* Fork * +*************************************************/ +class Fork : public Fanout_Filter + { + public: + void write(const byte input[], u32bit length) { send(input, length); } + void set_port(u32bit n) { Fanout_Filter::set_port(n); } + + Fork(Filter*, Filter*, Filter* = 0, Filter* = 0); + Fork(Filter*[], u32bit); + }; + +/************************************************* +* Keyed Filter * +*************************************************/ +class Keyed_Filter : public Filter + { + public: + virtual void set_key(const SymmetricKey&); + virtual void set_iv(const InitializationVector&) {} + virtual bool valid_keylength(u32bit) const; + + Keyed_Filter() { base_ptr = 0; } + protected: + SymmetricAlgorithm* base_ptr; + }; + +} + +#endif diff --git a/include/ber_dec.h b/include/ber_dec.h new file mode 100644 index 000000000..32feb43e1 --- /dev/null +++ b/include/ber_dec.h @@ -0,0 +1,119 @@ +/************************************************* +* BER Decoder Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BER_DECODER_H__ +#define BOTAN_BER_DECODER_H__ + +#include <botan/asn1_oid.h> +#include <botan/bigint.h> +#include <botan/data_src.h> + +namespace Botan { + +/************************************************* +* BER Encoded Object * +*************************************************/ +struct BER_Object + { + ASN1_Tag type_tag, class_tag; + SecureVector<byte> value; + }; + +/************************************************* +* BER Decoding Object * +*************************************************/ +class BER_Decoder + { + public: + bool more_items() const; + void verify_end() const; + SecureVector<byte> get_remaining(); + void discard_remaining(); + BER_Object get_next_object(); + void push_back(const BER_Object&); + + BER_Decoder& decode_null(); + BER_Decoder& decode(bool&); + BER_Decoder& decode(u32bit&); + BER_Decoder& decode(class BigInt&); + BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag); + + BER_Decoder& decode(bool&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(u32bit&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(class BigInt&, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + BER_Decoder& decode(MemoryRegion<byte>&, ASN1_Tag, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + BER_Decoder(DataSource&); + BER_Decoder(const byte[], u32bit); + BER_Decoder(const MemoryRegion<byte>&); + BER_Decoder(const BER_Decoder&); + ~BER_Decoder(); + private: + BER_Decoder& operator=(const BER_Decoder&) { return (*this); } + DataSource* source; + BER_Object pushed; + mutable bool owns; + }; + +/************************************************* +* BER Decoding Functions * +*************************************************/ +namespace BER { + +void decode(BER_Decoder&, OID&); + +BER_Decoder get_subsequence(BER_Decoder&); +BER_Decoder get_subset(BER_Decoder&); + +BER_Decoder get_subsequence(BER_Decoder&, ASN1_Tag, + ASN1_Tag = CONTEXT_SPECIFIC); +BER_Decoder get_subset(BER_Decoder&, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + +std::string to_string(const BER_Object&); +bool decode_optional_string(BER_Decoder&, MemoryRegion<byte>&, + ASN1_Tag, ASN1_Tag, ASN1_Tag); + +/************************************************* +* Decode an OPTIONAL or DEFAULT element * +*************************************************/ +template<class T> +bool decode_optional(BER_Decoder& in, T& out, + ASN1_Tag type_tag, ASN1_Tag class_tag, + const T& default_value = T()) + { + BER_Object obj = in.get_next_object(); + + if(obj.type_tag == type_tag && obj.class_tag == class_tag) + { + if(class_tag & CONSTRUCTED) + { + BER_Decoder stored_value(obj.value); + //BER::decode(stored_value, out); + stored_value.decode(out); + stored_value.verify_end(); + } + else + { + in.push_back(obj); + //BER::decode(in, out, type_tag, class_tag); + in.decode(out, type_tag, class_tag); + } + return true; + } + else + { + out = default_value; + in.push_back(obj); + return false; + } + } + +} + +} + +#endif diff --git a/include/bigint.h b/include/bigint.h new file mode 100644 index 000000000..60989de67 --- /dev/null +++ b/include/bigint.h @@ -0,0 +1,155 @@ +/************************************************* +* BigInt Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BIGINT_H__ +#define BOTAN_BIGINT_H__ + +#include <botan/base.h> +#include <botan/mp_types.h> +#include <iosfwd> + +namespace Botan { + +/************************************************* +* BigInt * +*************************************************/ +class BigInt + { + public: + enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 }; + enum Sign { Negative = 0, Positive = 1 }; + enum NumberType { Random, Power2 }; + + struct DivideByZero : public Exception + { DivideByZero() : Exception("BigInt divide by zero") {} }; + + BigInt& operator+=(const BigInt&); + BigInt& operator-=(const BigInt&); + + BigInt& operator*=(const BigInt&); + BigInt& operator/=(const BigInt&); + BigInt& operator%=(const BigInt&); + word operator%=(word); + BigInt& operator<<=(u32bit); + BigInt& operator>>=(u32bit); + + BigInt& operator++() { return (*this += 1); } + BigInt& operator--() { return (*this -= 1); } + BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; } + BigInt operator--(int) { BigInt x = (*this); --(*this); return x; } + + BigInt operator-() const; + bool operator !() const { return (!is_nonzero()); } + + s32bit cmp(const BigInt&, bool = true) const; + bool is_even() const { return (get_bit(0) == 0); } + bool is_odd() const { return (get_bit(0) == 1); } + bool is_nonzero() const { return (!is_zero()); } + bool is_zero() const; + + void set_bit(u32bit); + void clear_bit(u32bit); + void mask_bits(u32bit); + + bool get_bit(u32bit) const; + u32bit get_substring(u32bit, u32bit) const; + byte byte_at(u32bit) const; + word word_at(u32bit n) const + { return ((n < size()) ? reg[n] : 0); } + + u32bit to_u32bit() const; + + bool is_negative() const { return (sign() == Negative); } + bool is_positive() const { return (sign() == Positive); } + Sign sign() const { return (signedness); } + Sign reverse_sign() const; + void flip_sign(); + void set_sign(Sign); + BigInt abs() const; + + u32bit size() const { return reg.size(); } + u32bit sig_words() const; + u32bit bytes() const; + u32bit bits() const; + + const word* data() const { return reg.begin(); } + SecureVector<word>& get_reg() { return reg; } + void grow_reg(u32bit) const; + + word& operator[](u32bit index) { return reg[index]; } + word operator[](u32bit index) const { return reg[index]; } + void clear() { reg.clear(); } + + void randomize(u32bit = 0); + + void binary_encode(byte[]) const; + void binary_decode(const byte[], u32bit); + u32bit encoded_size(Base = Binary) const; + + static SecureVector<byte> encode(const BigInt&, Base = Binary); + static void encode(byte[], const BigInt&, Base = Binary); + static BigInt decode(const byte[], u32bit, Base = Binary); + static BigInt decode(const MemoryRegion<byte>&, Base = Binary); + static SecureVector<byte> encode_1363(const BigInt&, u32bit); + + void swap(BigInt&); + + BigInt() { signedness = Positive; } + BigInt(u64bit); + BigInt(const BigInt&); + BigInt(const std::string&); + BigInt(const byte[], u32bit, Base = Binary); + BigInt(Sign, u32bit); + BigInt(NumberType, u32bit); + private: + friend void divide(const BigInt&, const BigInt&, BigInt&, BigInt&); + void grow_to(u32bit) const; + SecureVector<word> reg; + Sign signedness; + }; + +/************************************************* +* Arithmetic Operators * +*************************************************/ +BigInt operator+(const BigInt&, const BigInt&); +BigInt operator-(const BigInt&, const BigInt&); +BigInt operator*(const BigInt&, const BigInt&); +BigInt operator/(const BigInt&, const BigInt&); +BigInt operator%(const BigInt&, const BigInt&); +word operator%(const BigInt&, word); +BigInt operator<<(const BigInt&, u32bit); +BigInt operator>>(const BigInt&, u32bit); + +/************************************************* +* Comparison Operators * +*************************************************/ +inline bool operator==(const BigInt& a, const BigInt& b) + { return (a.cmp(b) == 0); } +inline bool operator!=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) != 0); } +inline bool operator<=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) <= 0); } +inline bool operator>=(const BigInt& a, const BigInt& b) + { return (a.cmp(b) >= 0); } +inline bool operator<(const BigInt& a, const BigInt& b) + { return (a.cmp(b) < 0); } +inline bool operator>(const BigInt& a, const BigInt& b) + { return (a.cmp(b) > 0); } + +/************************************************* +* I/O Operators * +*************************************************/ +std::ostream& operator<<(std::ostream&, const BigInt&); +std::istream& operator>>(std::istream&, BigInt&); + +} + +namespace std { + +inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); } + +} + +#endif diff --git a/include/bit_ops.h b/include/bit_ops.h new file mode 100644 index 000000000..e10b0e810 --- /dev/null +++ b/include/bit_ops.h @@ -0,0 +1,64 @@ +/************************************************* +* Bit/Word Operations Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BIT_OPS_H__ +#define BOTAN_BIT_OPS_H__ + +#include <botan/types.h> + +namespace Botan { + +/************************************************* +* Rotation Functions * +*************************************************/ +template<typename T> inline T rotate_left(T input, u32bit rot) + { return (T)((input << rot) | (input >> (8*sizeof(T)-rot))); } + +template<typename T> inline T rotate_right(T input, u32bit rot) + { return (T)((input >> rot) | (input << (8*sizeof(T)-rot))); } + +/************************************************* +* Byte Extraction Function * +*************************************************/ +template<typename T> inline byte get_byte(u32bit byte_num, T input) + { return (byte)(input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3)); } + +/************************************************* +* Byte to Word Conversions * +*************************************************/ +inline u16bit make_u16bit(byte input0, byte input1) + { return (u16bit)(((u16bit)input0 << 8) | input1); } + +inline u32bit make_u32bit(byte input0, byte input1, byte input2, byte input3) + { return (u32bit)(((u32bit)input0 << 24) | ((u32bit)input1 << 16) | + ((u32bit)input2 << 8) | input3); } + +inline u64bit make_u64bit(byte input0, byte input1, byte input2, byte input3, + byte input4, byte input5, byte input6, byte input7) + { + return (u64bit)(((u64bit)input0 << 56) | ((u64bit)input1 << 48) | + ((u64bit)input2 << 40) | ((u64bit)input3 << 32) | + ((u64bit)input4 << 24) | ((u64bit)input5 << 16) | + ((u64bit)input6 << 8) | input7); + } + +/************************************************* +* XOR Functions * +*************************************************/ +void xor_buf(byte[], const byte[], u32bit); +void xor_buf(byte[], const byte[], const byte[], u32bit); + +/************************************************* +* Misc Utility Functions * +*************************************************/ +bool power_of_2(u64bit); +u32bit high_bit(u64bit); +u32bit low_bit(u64bit); +u32bit significant_bytes(u64bit); +u32bit hamming_weight(u64bit); + +} + +#endif diff --git a/include/blinding.h b/include/blinding.h new file mode 100644 index 000000000..6e463df2e --- /dev/null +++ b/include/blinding.h @@ -0,0 +1,32 @@ +/************************************************* +* Blinder Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BLINDER_H__ +#define BOTAN_BLINDER_H__ + +#include <botan/bigint.h> +#include <botan/reducer.h> + +namespace Botan { + +/************************************************* +* Blinding Function Object * +*************************************************/ +class Blinder + { + public: + BigInt blind(const BigInt&) const; + BigInt unblind(const BigInt&) const; + + Blinder() {} + Blinder(const BigInt&, const BigInt&, const BigInt&); + private: + Modular_Reducer reducer; + mutable BigInt e, d; + }; + +} + +#endif diff --git a/include/blowfish.h b/include/blowfish.h new file mode 100644 index 000000000..2359528d6 --- /dev/null +++ b/include/blowfish.h @@ -0,0 +1,38 @@ +/************************************************* +* Blowfish Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BLOWFISH_H__ +#define BOTAN_BLOWFISH_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Blowfish * +*************************************************/ +class Blowfish : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Blowfish"; } + BlockCipher* clone() const { return new Blowfish; } + Blowfish() : BlockCipher(8, 1, 56) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + void generate_sbox(u32bit[], u32bit, u32bit&, u32bit&) const; + + static const u32bit PBOX[18], SBOX1[256], SBOX2[256], + SBOX3[256], SBOX4[256]; + + SecureBuffer<u32bit, 256> S1, S2, S3, S4; + SecureBuffer<u32bit, 18> P; + }; + +} + +#endif diff --git a/include/botan.h b/include/botan.h new file mode 100644 index 000000000..312ee7847 --- /dev/null +++ b/include/botan.h @@ -0,0 +1,13 @@ +/************************************************* +* Botan Core Interface Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#include <botan/base.h> +#include <botan/conf.h> +#include <botan/init.h> +#include <botan/lookup.h> +#include <botan/rng.h> +#include <botan/version.h> +#include <botan/bit_ops.h> +#include <botan/parsing.h> diff --git a/include/buf_es.h b/include/buf_es.h new file mode 100644 index 000000000..cb8057314 --- /dev/null +++ b/include/buf_es.h @@ -0,0 +1,39 @@ +/************************************************* +* Buffered EntropySource Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BUFFERED_ES_H__ +#define BOTAN_BUFFERED_ES_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Buffered EntropySource * +*************************************************/ +class Buffered_EntropySource : public EntropySource + { + public: + u32bit slow_poll(byte[], u32bit); + u32bit fast_poll(byte[], u32bit); + protected: + Buffered_EntropySource(); + u32bit copy_out(byte[], u32bit, u32bit); + + void add_bytes(const void*, u32bit); + void add_bytes(u64bit); + void add_timestamp(); + + virtual void do_slow_poll() = 0; + virtual void do_fast_poll(); + private: + SecureVector<byte> buffer; + u32bit write_pos, read_pos; + bool done_slow_poll; + }; + +} + +#endif diff --git a/include/buf_filt.h b/include/buf_filt.h new file mode 100644 index 000000000..4cea9108f --- /dev/null +++ b/include/buf_filt.h @@ -0,0 +1,35 @@ +/************************************************* +* Buffering Filter Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BUFFERING_FILTER_H__ +#define BOTAN_BUFFERING_FILTER_H__ + +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* Buffering Filter * +*************************************************/ +class Buffering_Filter : public Filter + { + public: + void write(const byte[], u32bit); + virtual void end_msg(); + Buffering_Filter(u32bit, u32bit = 0); + virtual ~Buffering_Filter() {} + protected: + virtual void initial_block(const byte[]) {} + virtual void main_block(const byte[]) = 0; + virtual void final_block(const byte[], u32bit) = 0; + private: + const u32bit INITIAL_BLOCK_SIZE, BLOCK_SIZE; + SecureVector<byte> initial, block; + u32bit initial_block_pos, block_pos; + }; + +} + +#endif diff --git a/include/cast128.h b/include/cast128.h new file mode 100644 index 000000000..191ada337 --- /dev/null +++ b/include/cast128.h @@ -0,0 +1,42 @@ +/************************************************* +* CAST-128 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CAST128_H__ +#define BOTAN_CAST128_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CAST-128 * +*************************************************/ +class CAST_128 : public BlockCipher + { + public: + void clear() throw() { MK.clear(); RK.clear(); } + std::string name() const { return "CAST-128"; } + BlockCipher* clone() const { return new CAST_128; } + CAST_128() : BlockCipher(8, 11, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + static void key_schedule(u32bit[16], u32bit[4]); + + static const u32bit S5[256], S6[256], S7[256], S8[256]; + + SecureBuffer<u32bit, 16> MK, RK; + }; + +extern const u32bit CAST_SBOX1[256]; +extern const u32bit CAST_SBOX2[256]; +extern const u32bit CAST_SBOX3[256]; +extern const u32bit CAST_SBOX4[256]; + +} + +#endif diff --git a/include/cast256.h b/include/cast256.h new file mode 100644 index 000000000..068da9ce4 --- /dev/null +++ b/include/cast256.h @@ -0,0 +1,43 @@ +/************************************************* +* CAST-256 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CAST256_H__ +#define BOTAN_CAST256_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CAST-256 * +*************************************************/ +class CAST_256 : public BlockCipher + { + public: + void clear() throw() { MK.clear(); RK.clear(); } + std::string name() const { return "CAST-256"; } + BlockCipher* clone() const { return new CAST_256; } + CAST_256() : BlockCipher(16, 4, 32, 4) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + void round1(u32bit&, u32bit, u32bit, u32bit) const; + void round2(u32bit&, u32bit, u32bit, u32bit) const; + void round3(u32bit&, u32bit, u32bit, u32bit) const; + static const u32bit KEY_MASK[192]; + static const byte KEY_ROT[32]; + SecureBuffer<u32bit, 48> MK; + SecureBuffer<byte, 48> RK; + }; + +extern const u32bit CAST_SBOX1[256]; +extern const u32bit CAST_SBOX2[256]; +extern const u32bit CAST_SBOX3[256]; +extern const u32bit CAST_SBOX4[256]; + +} + +#endif diff --git a/include/cbc.h b/include/cbc.h new file mode 100644 index 000000000..cedfffc88 --- /dev/null +++ b/include/cbc.h @@ -0,0 +1,49 @@ +/************************************************* +* CBC Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CBC_H__ +#define BOTAN_CBC_H__ + +#include <botan/modebase.h> +#include <botan/mode_pad.h> + +namespace Botan { + +/************************************************* +* CBC Encryption * +*************************************************/ +class CBC_Encryption : public BlockCipherMode + { + public: + CBC_Encryption(const std::string&, const std::string&); + CBC_Encryption(const std::string&, const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + std::string name() const; + void write(const byte[], u32bit); + void end_msg(); + const BlockCipherModePaddingMethod* padder; + }; + +/************************************************* +* CBC Decryption * +*************************************************/ +class CBC_Decryption : public BlockCipherMode + { + public: + CBC_Decryption(const std::string&, const std::string&); + CBC_Decryption(const std::string&, const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + std::string name() const; + void write(const byte[], u32bit); + void end_msg(); + const BlockCipherModePaddingMethod* padder; + SecureVector<byte> temp; + }; + +} + +#endif diff --git a/include/certstor.h b/include/certstor.h new file mode 100644 index 000000000..002f6b4e7 --- /dev/null +++ b/include/certstor.h @@ -0,0 +1,37 @@ +/************************************************* +* Certificate Store Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CERT_STORE_H__ +#define BOTAN_CERT_STORE_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> + +namespace Botan { + +/************************************************* +* Certificate Store Interface * +*************************************************/ +class Certificate_Store + { + public: + virtual std::vector<X509_Certificate> + by_SKID(const MemoryRegion<byte>&) const = 0; + + virtual std::vector<X509_Certificate> by_name(const std::string&) const; + virtual std::vector<X509_Certificate> by_email(const std::string&) const; + virtual std::vector<X509_Certificate> by_dn(const X509_DN&) const; + + virtual std::vector<X509_CRL> + get_crls_for(const X509_Certificate&) const; + + virtual Certificate_Store* clone() const = 0; + + virtual ~Certificate_Store() {} + }; + +} + +#endif diff --git a/include/cfb.h b/include/cfb.h new file mode 100644 index 000000000..bbecd63ec --- /dev/null +++ b/include/cfb.h @@ -0,0 +1,45 @@ +/************************************************* +* CFB Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CFB_H__ +#define BOTAN_CFB_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/************************************************* +* CFB Encryption * +*************************************************/ +class CFB_Encryption : public BlockCipherMode + { + public: + CFB_Encryption(const std::string&, u32bit = 0); + CFB_Encryption(const std::string&, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void feedback(); + const u32bit FEEDBACK_SIZE; + }; + +/************************************************* +* CFB Decryption * +*************************************************/ +class CFB_Decryption : public BlockCipherMode + { + public: + CFB_Decryption(const std::string&, u32bit = 0); + CFB_Decryption(const std::string&, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void feedback(); + const u32bit FEEDBACK_SIZE; + }; + +} + +#endif diff --git a/include/charset.h b/include/charset.h new file mode 100644 index 000000000..f2fb33e76 --- /dev/null +++ b/include/charset.h @@ -0,0 +1,32 @@ +/************************************************* +* Character Set Handling Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CHARSET_H__ +#define BOTAN_CHARSET_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/************************************************* +* Character Set Handling * +*************************************************/ +bool is_digit(char); +bool is_space(char); +char to_lower(char); + +byte char2digit(char); +char digit2char(byte); + +std::string local2iso(const std::string&); +std::string iso2local(const std::string&); + +std::string utf2iso(const std::string&); +std::string iso2utf(const std::string&); + +} + +#endif diff --git a/include/cmac.h b/include/cmac.h new file mode 100644 index 000000000..43c325058 --- /dev/null +++ b/include/cmac.h @@ -0,0 +1,37 @@ +/************************************************* +* CMAC Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CMAC_H__ +#define BOTAN_CMAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CMAC * +*************************************************/ +class CMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + CMAC(const std::string&); + ~CMAC() { delete e; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + + BlockCipher* e; + SecureVector<byte> buffer, state, B, P; + u32bit position; + byte polynomial; + }; + +} + +#endif diff --git a/include/conf.h b/include/conf.h new file mode 100644 index 000000000..6bf0122f6 --- /dev/null +++ b/include/conf.h @@ -0,0 +1,51 @@ +/************************************************* +* Configuration Handling Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_POLICY_CONF_H__ +#define BOTAN_POLICY_CONF_H__ + +#include <botan/types.h> +#include <botan/enums.h> +#include <string> +#include <vector> + +namespace Botan { + +class Library_State; + +namespace Config { + +/************************************************* +* Load a configuration file * +*************************************************/ +void load(const std::string&); +void load(const std::string&, Library_State&); + +/************************************************* +* Set an option * +*************************************************/ +void set(const std::string&, const std::string&, bool = true); + +/************************************************* +* Get the value of some option * +*************************************************/ +std::vector<std::string> get_list(const std::string&); +std::string get_string(const std::string&); +u32bit get_u32bit(const std::string&); +u32bit get_time(const std::string&); +bool get_bool(const std::string&); + +/************************************************* +* Choose the signature format for a PK algorithm * +*************************************************/ +void choose_sig_format(const std::string&, std::string&, Signature_Format&); +void choose_sig_format(const std::string&, std::string&, std::string&, + Signature_Format&); + +} + +} + +#endif diff --git a/include/crc24.h b/include/crc24.h new file mode 100644 index 000000000..383de7d7c --- /dev/null +++ b/include/crc24.h @@ -0,0 +1,32 @@ +/************************************************* +* CRC24 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CRC24_H__ +#define BOTAN_CRC24_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CRC24 * +*************************************************/ +class CRC24 : public HashFunction + { + public: + void clear() throw() { crc = 0xB704CE; } + std::string name() const { return "CRC24"; } + HashFunction* clone() const { return new CRC24; } + CRC24() : HashFunction(3) { clear(); } + ~CRC24() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + u32bit crc; + }; + +} + +#endif diff --git a/include/crc32.h b/include/crc32.h new file mode 100644 index 000000000..d3169d103 --- /dev/null +++ b/include/crc32.h @@ -0,0 +1,32 @@ +/************************************************* +* CRC32 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CRC32_H__ +#define BOTAN_CRC32_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* CRC32 * +*************************************************/ +class CRC32 : public HashFunction + { + public: + void clear() throw() { crc = 0xFFFFFFFF; } + std::string name() const { return "CRC32"; } + HashFunction* clone() const { return new CRC32; } + CRC32() : HashFunction(4) { clear(); } + ~CRC32() { clear(); } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + u32bit crc; + }; + +} + +#endif diff --git a/include/crl_ent.h b/include/crl_ent.h new file mode 100644 index 000000000..660148211 --- /dev/null +++ b/include/crl_ent.h @@ -0,0 +1,53 @@ +/************************************************* +* CRL Entry Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CRL_ENTRY_H__ +#define BOTAN_CRL_ENTRY_H__ + +#include <botan/x509cert.h> + +namespace Botan { + +/************************************************* +* CRL Entry * +*************************************************/ +class CRL_Entry + { + public: + MemoryVector<byte> serial; + X509_Time time; + CRL_Code reason; + CRL_Entry(); + CRL_Entry(const X509_Certificate&, CRL_Code = UNSPECIFIED); + }; + +/************************************************* +* Comparison Operations * +*************************************************/ +bool operator==(const CRL_Entry&, const CRL_Entry&); +bool operator!=(const CRL_Entry&, const CRL_Entry&); +bool operator<(const CRL_Entry&, const CRL_Entry&); + +/************************************************* +* DER Encoding Functions * +*************************************************/ +namespace DER { + +void encode(DER_Encoder&, const CRL_Entry&); + +} + +/************************************************* +* BER Decoding Functions * +*************************************************/ +namespace BER { + +void decode(BER_Decoder&, CRL_Entry&); + +} + +} + +#endif diff --git a/include/ctr.h b/include/ctr.h new file mode 100644 index 000000000..833fe62c2 --- /dev/null +++ b/include/ctr.h @@ -0,0 +1,29 @@ +/************************************************* +* CTR Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CTR_H__ +#define BOTAN_CTR_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/************************************************* +* CTR-BE Mode * +*************************************************/ +class CTR_BE : public BlockCipherMode + { + public: + CTR_BE(const std::string&); + CTR_BE(const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + void increment_counter(); + }; + +} + +#endif diff --git a/include/cts.h b/include/cts.h new file mode 100644 index 000000000..09f1a3037 --- /dev/null +++ b/include/cts.h @@ -0,0 +1,46 @@ +/************************************************* +* CTS Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CTS_H__ +#define BOTAN_CTS_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/************************************************* +* CTS Encryption * +*************************************************/ +class CTS_Encryption : public BlockCipherMode + { + public: + CTS_Encryption(const std::string&); + CTS_Encryption(const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + void end_msg(); + void encrypt(const byte[]); + }; + +/************************************************* +* CTS Decryption * +*************************************************/ +class CTS_Decryption : public BlockCipherMode + { + public: + CTS_Decryption(const std::string&); + CTS_Decryption(const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + void end_msg(); + void decrypt(const byte[]); + SecureVector<byte> temp; + }; + +} + +#endif diff --git a/include/data_snk.h b/include/data_snk.h new file mode 100644 index 000000000..d37a8fa29 --- /dev/null +++ b/include/data_snk.h @@ -0,0 +1,46 @@ +/************************************************* +* DataSink Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DATA_SINK_H__ +#define BOTAN_DATA_SINK_H__ + +#include <botan/filter.h> +#include <iosfwd> + +namespace Botan { + +/************************************************* +* Generic DataSink Interface * +*************************************************/ +class DataSink : public Filter + { + public: + bool attachable() { return false; } + DataSink() {} + virtual ~DataSink() {} + private: + DataSink& operator=(const DataSink&) { return (*this); } + DataSink(const DataSink&); + }; + +/************************************************* +* Stream-Based DataSink * +*************************************************/ +class DataSink_Stream : public DataSink + { + public: + void write(const byte[], u32bit); + DataSink_Stream(std::ostream&); + DataSink_Stream(const std::string&, bool = false); + ~DataSink_Stream(); + private: + const std::string fsname; + std::ostream* sink; + bool owns; + }; + +} + +#endif diff --git a/include/data_src.h b/include/data_src.h new file mode 100644 index 000000000..274ee7d5b --- /dev/null +++ b/include/data_src.h @@ -0,0 +1,75 @@ +/************************************************* +* DataSource Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DATA_SRC_H__ +#define BOTAN_DATA_SRC_H__ + +#include <botan/base.h> +#include <iosfwd> + +namespace Botan { + +/************************************************* +* Generic DataSource Interface * +*************************************************/ +class DataSource + { + public: + virtual u32bit read(byte[], u32bit) = 0; + virtual u32bit peek(byte[], u32bit, u32bit) const = 0; + virtual bool end_of_data() const = 0; + virtual std::string id() const { return ""; } + + u32bit read_byte(byte&); + u32bit peek_byte(byte&) const; + u32bit discard_next(u32bit); + + DataSource() {} + virtual ~DataSource() {} + private: + DataSource& operator=(const DataSource&) { return (*this); } + DataSource(const DataSource&); + }; + +/************************************************* +* Memory-Based DataSource * +*************************************************/ +class DataSource_Memory : public DataSource + { + public: + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit) const; + bool end_of_data() const; + + DataSource_Memory(const std::string&); + DataSource_Memory(const byte[], u32bit); + DataSource_Memory(const MemoryRegion<byte>&); + private: + SecureVector<byte> source; + u32bit offset; + }; + +/************************************************* +* Stream-Based DataSource * +*************************************************/ +class DataSource_Stream : public DataSource + { + public: + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit) const; + bool end_of_data() const; + std::string id() const; + + DataSource_Stream(const std::string&, bool = false); + ~DataSource_Stream(); + private: + const std::string fsname; + std::istream* source; + u32bit total_read; + }; + +} + +#endif diff --git a/include/datastor.h b/include/datastor.h new file mode 100644 index 000000000..86a607feb --- /dev/null +++ b/include/datastor.h @@ -0,0 +1,55 @@ +/************************************************* +* Data Store Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DATA_STORE_H__ +#define BOTAN_DATA_STORE_H__ + +#include <botan/secmem.h> +#include <utility> +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/************************************************* +* Data Store * +*************************************************/ +class Data_Store + { + public: + class Matcher + { + public: + virtual bool operator()(const std::string&, + const std::string&) const = 0; + + virtual std::pair<std::string, std::string> + transform(const std::string&, const std::string&) const; + + virtual ~Matcher() {} + }; + + bool operator==(const Data_Store&) const; + + std::multimap<std::string, std::string> get(const Matcher&) const; + std::vector<std::string> get(const std::string&) const; + + std::string get1(const std::string&) const; + + MemoryVector<byte> get1_memvec(const std::string&) const; + u32bit get1_u32bit(const std::string&, u32bit = 0) const; + + void add(const std::multimap<std::string, std::string>&); + void add(const std::string&, const std::string&); + void add(const std::string&, u32bit); + void add(const std::string&, const MemoryRegion<byte>&); + private: + std::multimap<std::string, std::string> contents; + }; + +} + +#endif diff --git a/include/def_powm.h b/include/def_powm.h new file mode 100644 index 000000000..5f686162d --- /dev/null +++ b/include/def_powm.h @@ -0,0 +1,62 @@ +/************************************************* +* Modular Exponentiation Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DEFAULT_MODEXP_H__ +#define BOTAN_DEFAULT_MODEXP_H__ + +#include <botan/pow_mod.h> +#include <botan/reducer.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Fixed Window Exponentiator * +*************************************************/ +class Fixed_Window_Exponentiator : public Modular_Exponentiator + { + public: + void set_exponent(const BigInt&); + void set_base(const BigInt&); + BigInt execute() const; + + Modular_Exponentiator* copy() const + { return new Fixed_Window_Exponentiator(*this); } + + Fixed_Window_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); + private: + Modular_Reducer reducer; + BigInt exp; + u32bit window_bits; + std::vector<BigInt> g; + Power_Mod::Usage_Hints hints; + }; + +/************************************************* +* Montgomery Exponentiator * +*************************************************/ +class Montgomery_Exponentiator : public Modular_Exponentiator + { + public: + void set_exponent(const BigInt&); + void set_base(const BigInt&); + BigInt execute() const; + + Modular_Exponentiator* copy() const + { return new Montgomery_Exponentiator(*this); } + + Montgomery_Exponentiator(const BigInt&, Power_Mod::Usage_Hints); + private: + BigInt exp, modulus; + BigInt R2, R_mod; + std::vector<BigInt> g; + word mod_prime; + u32bit mod_words, exp_bits, window_bits; + Power_Mod::Usage_Hints hints; + }; + +} + +#endif diff --git a/include/defalloc.h b/include/defalloc.h new file mode 100644 index 000000000..ef675d3a8 --- /dev/null +++ b/include/defalloc.h @@ -0,0 +1,39 @@ +/************************************************* +* Basic Allocators Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_BASIC_ALLOC_H__ +#define BOTAN_BASIC_ALLOC_H__ + +#include <botan/mem_pool.h> + +namespace Botan { + +/************************************************* +* Malloc Allocator * +*************************************************/ +class Malloc_Allocator : public Pooling_Allocator + { + public: + Malloc_Allocator() : Pooling_Allocator(64*1024, false) {} + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +/************************************************* +* Locking Allocator * +*************************************************/ +class Locking_Allocator : public Pooling_Allocator + { + public: + Locking_Allocator() : Pooling_Allocator(64*1024, true) {} + private: + void* alloc_block(u32bit); + void dealloc_block(void*, u32bit); + }; + +} + +#endif diff --git a/include/der_enc.h b/include/der_enc.h new file mode 100644 index 000000000..b07c76172 --- /dev/null +++ b/include/der_enc.h @@ -0,0 +1,85 @@ +/************************************************* +* DER Encoder Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DER_ENCODER_H__ +#define BOTAN_DER_ENCODER_H__ + +#include <botan/asn1_oid.h> +#include <vector> + +namespace Botan { + +/************************************************* +* General DER Encoding Object * +*************************************************/ +class DER_Encoder + { + public: + SecureVector<byte> get_contents(); + + DER_Encoder& start_sequence(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& end_sequence(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& start_set(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& end_set(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + DER_Encoder& start_sequence(); + DER_Encoder& end_sequence(); + DER_Encoder& start_set(); + DER_Encoder& end_set(); + + DER_Encoder& start_explicit(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& end_explicit(ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + DER_Encoder& add_raw_octets(const byte[], u32bit); + DER_Encoder& add_raw_octets(const MemoryRegion<byte>&); + + DER_Encoder& encode_null(); + DER_Encoder& encode(bool); + DER_Encoder& encode(u32bit); + DER_Encoder& encode(const class BigInt&); + DER_Encoder& encode(const MemoryRegion<byte>&, ASN1_Tag); + DER_Encoder& encode(const byte[], u32bit, ASN1_Tag); + + DER_Encoder& encode(bool, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(u32bit, ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + DER_Encoder& encode(const class BigInt&, ASN1_Tag, + 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, + ASN1_Tag, ASN1_Tag = CONTEXT_SPECIFIC); + + DER_Encoder& encode(const class ASN1_Object&); + DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const byte[], u32bit); + 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); + + DER_Encoder(); + private: + DER_Encoder& start_cons(ASN1_Tag, ASN1_Tag, bool); + DER_Encoder& end_cons(ASN1_Tag, ASN1_Tag); + + class DER_Sequence + { + public: + ASN1_Tag tag_of() const; + SecureVector<byte> get_contents(); + void add_bytes(const byte[], u32bit); + DER_Sequence(ASN1_Tag, ASN1_Tag, bool = false); + private: + ASN1_Tag type_tag, class_tag; + bool is_a_set; + SecureVector<byte> contents; + std::vector< SecureVector<byte> > set_contents; + }; + SecureVector<byte> contents; + std::vector<DER_Sequence> subsequences; + u32bit sequence_level; + }; + +} + +#endif diff --git a/include/des.h b/include/des.h new file mode 100644 index 000000000..6b4240be5 --- /dev/null +++ b/include/des.h @@ -0,0 +1,79 @@ +/************************************************* +* DES Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DES_H__ +#define BOTAN_DES_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* DES * +*************************************************/ +class DES : public BlockCipher + { + public: + void clear() throw() { round_key.clear(); } + std::string name() const { return "DES"; } + BlockCipher* clone() const { return new DES; } + DES() : BlockCipher(8, 8) {} + private: + friend class TripleDES; + + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + void raw_encrypt(u32bit&, u32bit&) const; + void raw_decrypt(u32bit&, u32bit&) const; + void round(u32bit&, u32bit, u32bit) const; + static void IP(u32bit&, u32bit&); + static void FP(u32bit&, u32bit&); + + static const u32bit SPBOX1[256], SPBOX2[256], SPBOX3[256], SPBOX4[256], + SPBOX5[256], SPBOX6[256], SPBOX7[256], SPBOX8[256]; + static const u64bit IPTAB1[256], IPTAB2[256], FPTAB1[256], FPTAB2[256]; + + SecureBuffer<u32bit, 32> round_key; + }; + +/************************************************* +* Triple DES * +*************************************************/ +class TripleDES : public BlockCipher + { + public: + void clear() throw() { des1.clear(); des2.clear(); des3.clear(); } + std::string name() const { return "TripleDES"; } + BlockCipher* clone() const { return new TripleDES; } + TripleDES() : BlockCipher(8, 16, 24, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + DES des1, des2, des3; + }; + +/************************************************* +* DESX * +*************************************************/ +class DESX : public BlockCipher + { + public: + void clear() throw() { des.clear(); K1.clear(); K2.clear(); } + std::string name() const { return "DESX"; } + BlockCipher* clone() const { return new DESX; } + DESX() : BlockCipher(8, 24) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + SecureBuffer<byte, 8> K1, K2; + DES des; + }; + +} + +#endif diff --git a/include/dh.h b/include/dh.h new file mode 100644 index 000000000..f30033d9d --- /dev/null +++ b/include/dh.h @@ -0,0 +1,57 @@ +/************************************************* +* Diffie-Hellman Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DIFFIE_HELLMAN_H__ +#define BOTAN_DIFFIE_HELLMAN_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* Diffie-Hellman Public Key * +*************************************************/ +class DH_PublicKey : public virtual DL_Scheme_PublicKey + { + public: + MemoryVector<byte> public_value() const; + DH_PublicKey(const DL_Group&, const BigInt&); + protected: + std::string algo_name() const { return "DH"; } + DH_PublicKey() {} + private: + friend X509_PublicKey* get_public_key(const std::string&); + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + void X509_load_hook(); + }; + +/************************************************* +* Diffie-Hellman Private Key * +*************************************************/ +class DH_PrivateKey : public DH_PublicKey, + public PK_Key_Agreement_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> derive_key(const byte[], u32bit) const; + SecureVector<byte> derive_key(const DH_PublicKey&) const; + SecureVector<byte> derive_key(const BigInt&) const; + + MemoryVector<byte> public_value() const; + + DH_PrivateKey(const DL_Group&); + DH_PrivateKey(const DL_Group&, const BigInt&, const BigInt& = 0); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + void PKCS8_load_hook(); + DH_PrivateKey() {} + + DH_Core core; + }; + +} + +#endif diff --git a/include/dl_algo.h b/include/dl_algo.h new file mode 100644 index 000000000..ccb8a05a0 --- /dev/null +++ b/include/dl_algo.h @@ -0,0 +1,67 @@ +/************************************************* +* DL Scheme Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DL_ALGO_H__ +#define BOTAN_DL_ALGO_H__ + +#include <botan/dl_group.h> +#include <botan/x509_key.h> +#include <botan/pkcs8.h> + +namespace Botan { + +/************************************************* +* DL Public Key * +*************************************************/ +class DL_Scheme_PublicKey : public virtual X509_PublicKey + { + public: + bool check_key(bool) const; + + const DL_Group& get_domain() const { return group; } + const BigInt& get_y() const { return y; } + + virtual ~DL_Scheme_PublicKey() {} + protected: + const BigInt& group_p() const { return group.get_p(); } + const BigInt& group_q() const { return group.get_q(); } + const BigInt& group_g() const { return group.get_g(); } + + BigInt y; + DL_Group group; + private: + MemoryVector<byte> DER_encode_pub() const; + MemoryVector<byte> DER_encode_params() const; + void BER_decode_pub(DataSource&); + void BER_decode_params(DataSource&); + + virtual DL_Group::Format group_format() const = 0; + virtual void X509_load_hook() {} + }; + +/************************************************* +* DL Private Key * +*************************************************/ +class DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey, + public virtual PKCS8_PrivateKey + { + public: + bool check_key(bool) const; + + const BigInt& get_x() const { return x; } + + virtual ~DL_Scheme_PrivateKey() {} + protected: + BigInt x; + private: + SecureVector<byte> DER_encode_priv() const; + void BER_decode_priv(DataSource&); + + virtual void PKCS8_load_hook() {} + }; + +} + +#endif diff --git a/include/dl_group.h b/include/dl_group.h new file mode 100644 index 000000000..28161023e --- /dev/null +++ b/include/dl_group.h @@ -0,0 +1,51 @@ +/************************************************* +* Discrete Logarithm Group Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DL_PARAM_H__ +#define BOTAN_DL_PARAM_H__ + +#include <botan/bigint.h> +#include <botan/data_src.h> + +namespace Botan { + +/************************************************* +* Discrete Logarithm Group * +*************************************************/ +class DL_Group + { + public: + const BigInt& get_p() const; + const BigInt& get_q() const; + const BigInt& get_g() const; + + enum Format { ANSI_X9_42, ANSI_X9_57, PKCS_3 }; + enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer }; + + bool verify_group(bool) const; + + std::string PEM_encode(Format) const; + SecureVector<byte> DER_encode(Format) const; + void BER_decode(DataSource&, Format); + void PEM_decode(DataSource&); + + static BigInt make_dsa_generator(const BigInt&, const BigInt&); + + DL_Group(); + DL_Group(const std::string&); + DL_Group(u32bit, PrimeType = Strong); + DL_Group(const MemoryRegion<byte>&, u32bit = 1024, u32bit = 0); + DL_Group(const BigInt&, const BigInt&); + DL_Group(const BigInt&, const BigInt&, const BigInt&); + private: + void init_check() const; + void initialize(const BigInt&, const BigInt&, const BigInt&); + bool initialized; + BigInt p, q, g; + }; + +} + +#endif diff --git a/include/dlies.h b/include/dlies.h new file mode 100644 index 000000000..87dc44c04 --- /dev/null +++ b/include/dlies.h @@ -0,0 +1,52 @@ +/************************************************* +* DLIES Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DLIES_H__ +#define BOTAN_DLIES_H__ + +#include <botan/pubkey.h> + +namespace Botan { + +/************************************************* +* DLIES Encryption * +*************************************************/ +class DLIES_Encryptor : public PK_Encryptor + { + public: + DLIES_Encryptor(const PK_Key_Agreement_Key&, + const std::string& = "KDF2(SHA-160)", + const std::string& = "HMAC(SHA-160)", u32bit = 20); + void set_other_key(const MemoryRegion<byte>&); + private: + SecureVector<byte> enc(const byte[], u32bit) const; + u32bit maximum_input_size() const; + const PK_Key_Agreement_Key& key; + SecureVector<byte> other_key; + const std::string kdf_algo; + const std::string mac_algo; + const u32bit MAC_KEYLEN; + }; + +/************************************************* +* DLIES Decryption * +*************************************************/ +class DLIES_Decryptor : public PK_Decryptor + { + public: + DLIES_Decryptor(const PK_Key_Agreement_Key&, + const std::string& = "KDF2(SHA-160)", + const std::string& = "HMAC(SHA-160)", u32bit = 20); + private: + SecureVector<byte> dec(const byte[], u32bit) const; + const PK_Key_Agreement_Key& key; + const std::string kdf_algo; + const std::string mac_algo; + const u32bit MAC_KEYLEN, PUBLIC_LEN; + }; + +} + +#endif diff --git a/include/dsa.h b/include/dsa.h new file mode 100644 index 000000000..f2f630e7c --- /dev/null +++ b/include/dsa.h @@ -0,0 +1,60 @@ +/************************************************* +* DSA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DSA_H__ +#define BOTAN_DSA_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* DSA Public Key * +*************************************************/ +class DSA_PublicKey : public PK_Verifying_wo_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + bool verify(const byte[], u32bit, const byte[], u32bit) const; + u32bit max_input_bits() const; + + DSA_PublicKey(const DL_Group&, const BigInt&); + protected: + std::string algo_name() const { return "DSA"; } + DSA_PublicKey() {} + + DSA_Core core; + private: + friend X509_PublicKey* get_public_key(const std::string&); + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + void X509_load_hook(); + }; + +/************************************************* +* DSA Private Key * +*************************************************/ +class DSA_PrivateKey : public DSA_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit) const; + + bool check_key(bool) const; + + DSA_PrivateKey(const DL_Group&); + DSA_PrivateKey(const DL_Group&, const BigInt&, const BigInt& = 0); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + void PKCS8_load_hook(); + DSA_PrivateKey() {} + }; + +} + +#endif diff --git a/include/eax.h b/include/eax.h new file mode 100644 index 000000000..e33619a0e --- /dev/null +++ b/include/eax.h @@ -0,0 +1,72 @@ +/************************************************* +* EAX Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_EAX_H__ +#define BOTAN_EAX_H__ + +#include <botan/basefilt.h> + +namespace Botan { + +/************************************************* +* EAX Base Class * +*************************************************/ +class EAX_Base : public Keyed_Filter + { + public: + void set_key(const SymmetricKey&); + void set_iv(const InitializationVector&); + void set_header(const byte[], u32bit); + std::string name() const; + + bool valid_keylength(u32bit) const; + + ~EAX_Base() { delete cipher; delete mac; } + protected: + EAX_Base(const std::string&, u32bit); + void start_msg(); + void increment_counter(); + + const u32bit TAG_SIZE, BLOCK_SIZE; + BlockCipher* cipher; + MessageAuthenticationCode* mac; + SecureVector<byte> nonce_mac, header_mac, state, buffer; + u32bit position; + }; + +/************************************************* +* EAX Encryption * +*************************************************/ +class EAX_Encryption : public EAX_Base + { + public: + EAX_Encryption(const std::string&, u32bit = 0); + EAX_Encryption(const std::string&, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +/************************************************* +* EAX Decryption * +*************************************************/ +class EAX_Decryption : public EAX_Base + { + public: + EAX_Decryption(const std::string&, u32bit = 0); + EAX_Decryption(const std::string&, const SymmetricKey&, + const InitializationVector&, u32bit = 0); + private: + void write(const byte[], u32bit); + void do_write(const byte[], u32bit); + void end_msg(); + SecureVector<byte> queue; + u32bit queue_start, queue_end; + }; + +} + +#endif diff --git a/include/ecb.h b/include/ecb.h new file mode 100644 index 000000000..10483b3a8 --- /dev/null +++ b/include/ecb.h @@ -0,0 +1,57 @@ +/************************************************* +* ECB Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ECB_H__ +#define BOTAN_ECB_H__ + +#include <botan/modebase.h> +#include <botan/mode_pad.h> + +namespace Botan { + +/************************************************* +* ECB * +*************************************************/ +class ECB : public BlockCipherMode + { + protected: + ECB(const std::string&, const std::string&); + std::string name() const; + const BlockCipherModePaddingMethod* padder; + private: + bool valid_iv_size(u32bit) const; + }; + +/************************************************* +* ECB Encryption * +*************************************************/ +class ECB_Encryption : public ECB + { + public: + ECB_Encryption(const std::string&, const std::string&); + ECB_Encryption(const std::string&, const std::string&, + const SymmetricKey&); + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +/************************************************* +* ECB Decryption * +*************************************************/ +class ECB_Decryption : public ECB + { + public: + ECB_Decryption(const std::string&, const std::string&); + ECB_Decryption(const std::string&, const std::string&, + const SymmetricKey&); + private: + void write(const byte[], u32bit); + void end_msg(); + }; + +} + +#endif diff --git a/include/elgamal.h b/include/elgamal.h new file mode 100644 index 000000000..3731e8f1b --- /dev/null +++ b/include/elgamal.h @@ -0,0 +1,59 @@ +/************************************************* +* ElGamal Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ELGAMAL_H__ +#define BOTAN_ELGAMAL_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> +#include <botan/blinding.h> + +namespace Botan { + +/************************************************* +* ElGamal Public Key * +*************************************************/ +class ElGamal_PublicKey : public PK_Encrypting_Key, + public virtual DL_Scheme_PublicKey + { + public: + SecureVector<byte> encrypt(const byte[], u32bit) const; + u32bit max_input_bits() const; + + ElGamal_PublicKey(const DL_Group&, const BigInt&); + protected: + std::string algo_name() const { return "ElGamal"; } + ElGamal_PublicKey() {} + + ELG_Core core; + private: + friend X509_PublicKey* get_public_key(const std::string&); + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; } + void X509_load_hook(); + }; + +/************************************************* +* ElGamal Private Key * +*************************************************/ +class ElGamal_PrivateKey : public ElGamal_PublicKey, + public PK_Decrypting_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> decrypt(const byte[], u32bit) const; + + bool check_key(bool) const; + + ElGamal_PrivateKey(const DL_Group&); + ElGamal_PrivateKey(const DL_Group&, const BigInt&, const BigInt& = 0); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + void PKCS8_load_hook(); + ElGamal_PrivateKey() {} + }; + +} + +#endif diff --git a/include/eme.h b/include/eme.h new file mode 100644 index 000000000..a82f49d79 --- /dev/null +++ b/include/eme.h @@ -0,0 +1,45 @@ +/************************************************* +* EME Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_EME_H__ +#define BOTAN_EME_H__ + +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* EME1 * +*************************************************/ +class EME1 : public EME + { + public: + u32bit maximum_input_size(u32bit) const; + + EME1(const std::string&, const std::string&, const std::string& = ""); + ~EME1() { delete mgf; } + private: + SecureVector<byte> pad(const byte[], u32bit, u32bit) const; + SecureVector<byte> unpad(const byte[], u32bit, u32bit) const; + const u32bit HASH_LENGTH; + SecureVector<byte> Phash; + MGF* mgf; + }; + +/************************************************* +* EME_PKCS1v15 * +*************************************************/ +class EME_PKCS1v15 : public EME + { + public: + u32bit maximum_input_size(u32bit) const; + private: + SecureVector<byte> pad(const byte[], u32bit, u32bit) const; + SecureVector<byte> unpad(const byte[], u32bit, u32bit) const; + }; + +} + +#endif diff --git a/include/emsa.h b/include/emsa.h new file mode 100644 index 000000000..2abd72c28 --- /dev/null +++ b/include/emsa.h @@ -0,0 +1,97 @@ +/************************************************* +* EMSA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_EMSA_H__ +#define BOTAN_EMSA_H__ + +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* EMSA1 * +*************************************************/ +class EMSA1 : public EMSA + { + public: + EMSA1(const std::string&); + ~EMSA1() { delete hash; } + private: + void update(const byte[], u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + HashFunction* hash; + }; + +/************************************************* +* EMSA2 * +*************************************************/ +class EMSA2 : public EMSA + { + public: + EMSA2(const std::string&); + ~EMSA2() { delete hash; } + private: + void update(const byte[], u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + SecureVector<byte> empty_hash; + HashFunction* hash; + byte hash_id; + }; + +/************************************************* +* EMSA3 * +*************************************************/ +class EMSA3 : public EMSA + { + public: + EMSA3(const std::string&); + ~EMSA3() { delete hash; } + private: + void update(const byte[], u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + HashFunction* hash; + SecureVector<byte> hash_id; + }; + +/************************************************* +* EMSA4 * +*************************************************/ +class EMSA4 : public EMSA + { + public: + EMSA4(const std::string&, const std::string&); + EMSA4(const std::string&, const std::string&, u32bit); + ~EMSA4() { delete hash; delete mgf; } + private: + void update(const byte[], u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + const u32bit SALT_SIZE; + HashFunction* hash; + const MGF* mgf; + }; + +/************************************************* +* EMSA-Raw * +*************************************************/ +class EMSA_Raw : public EMSA + { + private: + void update(const byte[], u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + SecureVector<byte> message; + }; + +} + +#endif diff --git a/include/eng_def.h b/include/eng_def.h new file mode 100644 index 000000000..5fbd07a08 --- /dev/null +++ b/include/eng_def.h @@ -0,0 +1,46 @@ +/************************************************* +* Default Engine Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_DEFAULT_ENGINE_H__ +#define BOTAN_DEFAULT_ENGINE_H__ + +#include <botan/engine.h> + +namespace Botan { + +/************************************************* +* Default Engine * +*************************************************/ +class Default_Engine : public Engine + { + public: + IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const; + DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const; + NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&) const; + ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const; + DH_Operation* dh_op(const DL_Group&, const BigInt&) const; + + Modular_Exponentiator* mod_exp(const BigInt&, + Power_Mod::Usage_Hints) const; + + Keyed_Filter* get_cipher(const std::string&, Cipher_Dir); + private: + BlockCipher* find_block_cipher(const std::string&) const; + StreamCipher* find_stream_cipher(const std::string&) const; + HashFunction* find_hash(const std::string&) const; + MessageAuthenticationCode* find_mac(const std::string&) const; + + class S2K* find_s2k(const std::string&) const; + class BlockCipherModePaddingMethod* + find_bc_pad(const std::string&) const; + }; + +} + +#endif diff --git a/include/engine.h b/include/engine.h new file mode 100644 index 000000000..d28095b71 --- /dev/null +++ b/include/engine.h @@ -0,0 +1,97 @@ +/************************************************* +* Engine Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ENGINE_H__ +#define BOTAN_ENGINE_H__ + +#include <botan/base.h> +#include <botan/mutex.h> +#include <botan/pk_ops.h> +#include <botan/pow_mod.h> +#include <botan/basefilt.h> +#include <utility> +#include <map> + +namespace Botan { + +/************************************************* +* Engine Base Class * +*************************************************/ +class Engine + { + public: + virtual IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&) const; + virtual DSA_Operation* dsa_op(const DL_Group&, const BigInt&, + const BigInt&) const; + virtual NR_Operation* nr_op(const DL_Group&, const BigInt&, + const BigInt&) const; + virtual ELG_Operation* elg_op(const DL_Group&, const BigInt&, + const BigInt&) const; + virtual DH_Operation* dh_op(const DL_Group&, const BigInt&) const; + + virtual Modular_Exponentiator* mod_exp(const BigInt&, + Power_Mod::Usage_Hints) const; + + virtual Keyed_Filter* get_cipher(const std::string&, Cipher_Dir); + + const BlockCipher* block_cipher(const std::string&) const; + const StreamCipher* stream_cipher(const std::string&) const; + const HashFunction* hash(const std::string&) const; + const MessageAuthenticationCode* mac(const std::string&) const; + const class S2K* s2k(const std::string&) const; + const class BlockCipherModePaddingMethod* + bc_pad(const std::string&) const; + + void add_algorithm(BlockCipher*) const; + void add_algorithm(StreamCipher*) const; + void add_algorithm(HashFunction*) const; + void add_algorithm(MessageAuthenticationCode*) const; + void add_algorithm(class S2K*) const; + void add_algorithm(class BlockCipherModePaddingMethod*) const; + + Engine(); + virtual ~Engine(); + private: + virtual BlockCipher* find_block_cipher(const std::string&) const; + virtual StreamCipher* find_stream_cipher(const std::string&) const; + virtual HashFunction* find_hash(const std::string&) const; + virtual MessageAuthenticationCode* find_mac(const std::string&) const; + virtual class S2K* find_s2k(const std::string&) const; + virtual class BlockCipherModePaddingMethod* + find_bc_pad(const std::string&) const; + + Algorithm* get_algo(const std::string&, const std::string&) const; + void add_algo(const std::string&, Algorithm*) const; + + typedef std::pair<Mutex*, std::map<std::string, Algorithm*> > + mutex_map_pair; + mutable std::map<std::string, mutex_map_pair> mappings; + }; + +namespace Engine_Core { + +/************************************************* +* Get an operation from an Engine * +*************************************************/ +Modular_Exponentiator* mod_exp(const BigInt&, Power_Mod::Usage_Hints); + +IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&, const BigInt&, + const BigInt&, const BigInt&); + +DSA_Operation* dsa_op(const DL_Group&, const BigInt&, const BigInt&); +NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&); + +ELG_Operation* elg_op(const DL_Group&, const BigInt&, const BigInt&); + +DH_Operation* dh_op(const DL_Group&, const BigInt&); + +} + +} + +#endif diff --git a/include/enums.h b/include/enums.h new file mode 100644 index 000000000..d09e8129d --- /dev/null +++ b/include/enums.h @@ -0,0 +1,125 @@ +/************************************************* +* Enumerations Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ENUMS_H__ +#define BOTAN_ENUMS_H__ + +namespace Botan { + +/************************************************* +* ASN.1 Type and Class Tags * +*************************************************/ +enum ASN1_Tag { + EOC = 0x00, + BOOLEAN = 0x01, + INTEGER = 0x02, + BIT_STRING = 0x03, + OCTET_STRING = 0x04, + NULL_TAG = 0x05, + OBJECT_ID = 0x06, + ENUMERATED = 0x0A, + SEQUENCE = 0x10, + SET = 0x11, + + UTF8_STRING = 0x0C, + NUMERIC_STRING = 0x12, + PRINTABLE_STRING = 0x13, + T61_STRING = 0x14, + IA5_STRING = 0x16, + VISIBLE_STRING = 0x1A, + BMP_STRING = 0x1E, + + UTC_TIME = 0x17, + GENERALIZED_TIME = 0x18, + + CONSTRUCTED = 0x20, + + UNIVERSAL = 0x00, + APPLICATION = 0x40, + CONTEXT_SPECIFIC = 0x80, + PRIVATE = 0xC0, + + NO_OBJECT = 0xFF00, + DIRECTORY_STRING = 0xFF01 +}; + +/************************************************* +* X.509v3 Key Constraints * +*************************************************/ +enum Key_Constraints { + NO_CONSTRAINTS = 0, + DIGITAL_SIGNATURE = 32768, + NON_REPUDIATION = 16384, + KEY_ENCIPHERMENT = 8192, + DATA_ENCIPHERMENT = 4096, + KEY_AGREEMENT = 2048, + KEY_CERT_SIGN = 1024, + CRL_SIGN = 512, + ENCIPHER_ONLY = 256, + DECIPHER_ONLY = 128 +}; + +/************************************************* +* X.509v2 CRL Reason Code * +*************************************************/ +enum CRL_Code { + UNSPECIFIED = 0, + KEY_COMPROMISE = 1, + CA_COMPROMISE = 2, + AFFILIATION_CHANGED = 3, + SUPERSEDED = 4, + CESSATION_OF_OPERATION = 5, + CERTIFICATE_HOLD = 6, + REMOVE_FROM_CRL = 8, + PRIVLEDGE_WITHDRAWN = 9, + AA_COMPROMISE = 10, + + DELETE_CRL_ENTRY = 0xFF00, + OCSP_GOOD = 0xFF01, + OCSP_UNKNOWN = 0xFF02 +}; + +/************************************************* +* X.509 Certificate Validation Result * +*************************************************/ +enum X509_Code { + VERIFIED, + UNKNOWN_X509_ERROR, + CANNOT_ESTABLISH_TRUST, + CERT_CHAIN_TOO_LONG, + SIGNATURE_ERROR, + POLICY_ERROR, + INVALID_USAGE, + + CERT_FORMAT_ERROR, + CERT_ISSUER_NOT_FOUND, + CERT_NOT_YET_VALID, + CERT_HAS_EXPIRED, + CERT_IS_REVOKED, + + CRL_FORMAT_ERROR, + CRL_ISSUER_NOT_FOUND, + CRL_NOT_YET_VALID, + CRL_HAS_EXPIRED, + + CA_CERT_CANNOT_SIGN, + CA_CERT_NOT_FOR_CERT_ISSUER, + CA_CERT_NOT_FOR_CRL_ISSUER +}; + +/************************************************* +* Various Other Enumerations * +*************************************************/ +enum Decoder_Checking { NONE, IGNORE_WS, FULL_CHECK }; + +enum X509_Encoding { RAW_BER, PEM }; + +enum Cipher_Dir { ENCRYPTION, DECRYPTION }; + +enum Signature_Format { IEEE_1363, DER_SEQUENCE }; + +} + +#endif diff --git a/include/es_file.h b/include/es_file.h new file mode 100644 index 000000000..b27c44ed1 --- /dev/null +++ b/include/es_file.h @@ -0,0 +1,24 @@ +/************************************************* +* File EntropySource Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ENTROPY_SRC_FILE_H__ +#define BOTAN_ENTROPY_SRC_FILE_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* File Based Entropy Source * +*************************************************/ +class File_EntropySource : public EntropySource + { + public: + u32bit slow_poll(byte[], u32bit); + }; + +} + +#endif diff --git a/include/exceptn.h b/include/exceptn.h new file mode 100644 index 000000000..7f7407893 --- /dev/null +++ b/include/exceptn.h @@ -0,0 +1,212 @@ +/************************************************* +* Exceptions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_EXCEPTION_H__ +#define BOTAN_EXCEPTION_H__ + +#include <botan/types.h> +#include <exception> +#include <string> + +namespace Botan { + +/************************************************* +* Exception Base Class * +*************************************************/ +class Exception : public std::exception + { + public: + const char* what() const throw() { return msg.c_str(); } + Exception(const std::string& m = "Unknown error") { set_msg(m); } + virtual ~Exception() throw() {} + protected: + void set_msg(const std::string& m) { msg = "Botan: " + m; } + private: + std::string msg; + }; + +/************************************************* +* Invalid_Argument Exception * +*************************************************/ +struct Invalid_Argument : public Exception + { + Invalid_Argument(const std::string& err = "") : Exception(err) {} + }; + +/************************************************* +* Invalid_Key_Length Exception * +*************************************************/ +struct Invalid_Key_Length : public Invalid_Argument + { + Invalid_Key_Length(const std::string&, u32bit); + }; + +/************************************************* +* Invalid_Block_Size Exception * +*************************************************/ +struct Invalid_Block_Size : public Invalid_Argument + { + Invalid_Block_Size(const std::string&, const std::string&); + }; + +/************************************************* +* Invalid_IV_Length Exception * +*************************************************/ +struct Invalid_IV_Length : public Invalid_Argument + { + Invalid_IV_Length(const std::string&, u32bit); + }; + +/************************************************* +* Invalid_Message_Number Exception * +*************************************************/ +struct Invalid_Message_Number : public Invalid_Argument + { + Invalid_Message_Number(const std::string&, u32bit); + }; + +/************************************************* +* Invalid_State Exception * +*************************************************/ +struct Invalid_State : public Exception + { + Invalid_State(const std::string& err) : Exception(err) {} + }; + +/************************************************* +* PRNG_Unseeded Exception * +*************************************************/ +struct PRNG_Unseeded : public Invalid_State + { + PRNG_Unseeded(const std::string& algo) : + Invalid_State("PRNG not seeded: " + algo) {} + }; + +/************************************************* +* Policy_Violation Exception * +*************************************************/ +struct Policy_Violation : public Invalid_State + { + Policy_Violation(const std::string& err) : + Invalid_State("Policy violation: " + err) {} + }; + +/************************************************* +* Lookup_Error Exception * +*************************************************/ +struct Lookup_Error : public Exception + { + Lookup_Error(const std::string& err) : Exception(err) {} + }; + +/************************************************* +* Algorithm_Not_Found Exception * +*************************************************/ +struct Algorithm_Not_Found : public Exception + { + Algorithm_Not_Found(const std::string&); + }; + +/************************************************* +* Format_Error Exception * +*************************************************/ +struct Format_Error : public Exception + { + Format_Error(const std::string& err = "") : Exception(err) {} + }; + +/************************************************* +* Invalid_Algorithm_Name Exception * +*************************************************/ +struct Invalid_Algorithm_Name : public Format_Error + { + Invalid_Algorithm_Name(const std::string&); + }; + +/************************************************* +* Encoding_Error Exception * +*************************************************/ +struct Encoding_Error : public Format_Error + { + Encoding_Error(const std::string& name) : + Format_Error("Encoding error: " + name) {} + }; + +/************************************************* +* Decoding_Error Exception * +*************************************************/ +struct Decoding_Error : public Format_Error + { + Decoding_Error(const std::string& name) : + Format_Error("Decoding error: " + name) {} + }; + +/************************************************* +* Invalid_OID Exception * +*************************************************/ +struct Invalid_OID : public Decoding_Error + { + Invalid_OID(const std::string& oid) : + Decoding_Error("Invalid ASN.1 OID: " + oid) {} + }; + +/************************************************* +* Stream_IO_Error Exception * +*************************************************/ +struct Stream_IO_Error : public Exception + { + Stream_IO_Error(const std::string& err) : + Exception("I/O error: " + err) {} + }; + +/************************************************* +* Configuration Error Exception * +*************************************************/ +struct Config_Error : public Format_Error + { + Config_Error(const std::string& err) : + Format_Error("Config error: " + err) {} + Config_Error(const std::string&, u32bit); + }; + +/************************************************* +* Integrity Failure Exception * +*************************************************/ +struct Integrity_Failure : public Exception + { + Integrity_Failure(const std::string& err) : + Exception("Integrity failure: " + err) {} + }; + +/************************************************* +* Internal_Error Exception * +*************************************************/ +struct Internal_Error : public Exception + { + Internal_Error(const std::string& err) : + Exception("Internal error: " + err) {} + }; + +/************************************************* +* Self Test Failure Exception * +*************************************************/ +struct Self_Test_Failure : public Internal_Error + { + Self_Test_Failure(const std::string& err) : + Internal_Error("Self test failed: " + err) {} + }; + +/************************************************* +* Memory Allocation Exception * +*************************************************/ +struct Memory_Exhaustion : public Exception + { + Memory_Exhaustion() : + Exception("Ran out of memory, allocation failed") {} + }; + +} + +#endif diff --git a/include/filter.h b/include/filter.h new file mode 100644 index 000000000..b8b051aa9 --- /dev/null +++ b/include/filter.h @@ -0,0 +1,70 @@ +/************************************************* +* Filter Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_FILTER_H__ +#define BOTAN_FILTER_H__ + +#include <botan/base.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Filter Base Class * +*************************************************/ +class Filter + { + public: + virtual void write(const byte[], u32bit) = 0; + virtual void start_msg() {} + virtual void end_msg() {} + virtual bool attachable() { return true; } + void new_msg(); + void finish_msg(); + virtual ~Filter() {} + protected: + virtual void send(const byte[], u32bit); + void send(byte input) { send(&input, 1); } + void send(const MemoryRegion<byte>& in) { send(in.begin(), in.size()); } + Filter(); + private: + Filter(const Filter&) {} + Filter& operator=(const Filter&) { return (*this); } + + friend class Pipe; + friend class Fanout_Filter; + + u32bit total_ports() const; + u32bit current_port() const { return port_num; } + void set_port(u32bit); + + u32bit owns() const { return filter_owns; } + + void attach(Filter*); + void set_next(Filter*[], u32bit); + Filter* get_next() const; + + SecureVector<byte> write_queue; + std::vector<Filter*> next; + u32bit port_num, filter_owns; + bool owned; + }; + +/************************************************* +* Fanout Filter Base Class * +*************************************************/ +class Fanout_Filter : public Filter + { + protected: + void incr_owns() { ++filter_owns; } + + void set_port(u32bit n) { Filter::set_port(n); } + void set_next(Filter* f[], u32bit n) { Filter::set_next(f, n); } + void attach(Filter* f) { Filter::attach(f); } + }; + +} + +#endif diff --git a/include/filters.h b/include/filters.h new file mode 100644 index 000000000..377181d63 --- /dev/null +++ b/include/filters.h @@ -0,0 +1,72 @@ +/************************************************* +* Filters Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_FILTERS_H__ +#define BOTAN_FILTERS_H__ + +#include <botan/pipe.h> +#include <botan/basefilt.h> +#include <botan/data_snk.h> +#include <botan/pk_filts.h> +#include <botan/base64.h> +#include <botan/hex.h> + +namespace Botan { + +/************************************************* +* Stream Cipher Filter * +*************************************************/ +class StreamCipher_Filter : public Keyed_Filter + { + public: + void seek(u32bit position) { cipher->seek(position); } + bool supports_resync() const { return (cipher->IV_LENGTH != 0); } + + void set_iv(const InitializationVector&); + void write(const byte[], u32bit); + + StreamCipher_Filter(const std::string&); + ~StreamCipher_Filter() { delete cipher; } + private: + SecureVector<byte> buffer; + StreamCipher* cipher; + }; + +/************************************************* +* Hash Filter * +*************************************************/ +class Hash_Filter : public Filter + { + public: + void write(const byte input[], u32bit len) { hash->update(input, len); } + void end_msg(); + + Hash_Filter(const std::string&, u32bit = 0); + ~Hash_Filter() { delete hash; } + private: + const u32bit OUTPUT_LENGTH; + HashFunction* hash; + }; + +/************************************************* +* MessageAuthenticationCode Filter * +*************************************************/ +class MAC_Filter : public Keyed_Filter + { + public: + void write(const byte input[], u32bit len) { mac->update(input, len); } + void end_msg(); + + MAC_Filter(const std::string&, u32bit = 0); + MAC_Filter(const std::string&, const SymmetricKey&, u32bit = 0); + ~MAC_Filter() { delete mac; } + private: + const u32bit OUTPUT_LENGTH; + MessageAuthenticationCode* mac; + }; + +} + +#endif diff --git a/include/fips140.h b/include/fips140.h new file mode 100644 index 000000000..400b5c117 --- /dev/null +++ b/include/fips140.h @@ -0,0 +1,25 @@ +/************************************************* +* FIPS 140 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_FIPS140_H__ +#define BOTAN_FIPS140_H__ + +#include <botan/base.h> + +namespace Botan { + +namespace FIPS140 { + +/************************************************* +* FIPS 140-2 Self Tests * +*************************************************/ +bool passes_self_tests(); +bool good_edc(const std::string&, const std::string&); + +} + +} + +#endif diff --git a/include/fork256.h b/include/fork256.h new file mode 100644 index 000000000..274bf4845 --- /dev/null +++ b/include/fork256.h @@ -0,0 +1,33 @@ +/************************************************* +* FORK-256 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_FORK_256_H__ +#define BOTAN_FORK_256_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* FORK-256 * +*************************************************/ +class FORK_256 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "FORK-256"; } + HashFunction* clone() const { return new FORK_256; } + FORK_256() : MDx_HashFunction(32, 64, true, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 8> digest; + SecureBuffer<u32bit, 16> M; + }; + +} + +#endif diff --git a/include/gost.h b/include/gost.h new file mode 100644 index 000000000..71a472b50 --- /dev/null +++ b/include/gost.h @@ -0,0 +1,35 @@ +/************************************************* +* GOST Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_GOST_H__ +#define BOTAN_GOST_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* GOST * +*************************************************/ +class GOST : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "GOST"; } + BlockCipher* clone() const { return new GOST; } + GOST() : BlockCipher(8, 32) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + static const u32bit SBOX1[256], SBOX2[256], SBOX3[256], SBOX4[256]; + + SecureBuffer<u32bit, 32> EK; + }; + +} + +#endif diff --git a/include/has160.h b/include/has160.h new file mode 100644 index 000000000..4ed18c87c --- /dev/null +++ b/include/has160.h @@ -0,0 +1,33 @@ +/************************************************* +* HAS-160 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_HAS_160_H__ +#define BOTAN_HAS_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* HAS-160 * +*************************************************/ +class HAS_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "HAS-160"; } + HashFunction* clone() const { return new HAS_160; } + HAS_160() : MDx_HashFunction(20, 64, false, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 20> X; + SecureBuffer<u32bit, 5> digest; + }; + +} + +#endif diff --git a/include/hash_id.h b/include/hash_id.h new file mode 100644 index 000000000..798e9579f --- /dev/null +++ b/include/hash_id.h @@ -0,0 +1,22 @@ +/************************************************* +* Hash Function Identification Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_HASHID_H__ +#define BOTAN_HASHID_H__ + +#include <botan/secmem.h> +#include <string> + +namespace Botan { + +/************************************************* +* Return the values of various defined HashIDs * +*************************************************/ +MemoryVector<byte> pkcs_hash_id(const std::string&); +byte ieee1363_hash_id(const std::string&); + +} + +#endif diff --git a/include/hex.h b/include/hex.h new file mode 100644 index 000000000..e3bfffbac --- /dev/null +++ b/include/hex.h @@ -0,0 +1,63 @@ +/************************************************* +* Hex Encoder/Decoder Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_HEX_H__ +#define BOTAN_HEX_H__ + +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* Hex Encoder * +*************************************************/ +class Hex_Encoder : public Filter + { + public: + enum Case { Uppercase, Lowercase }; + static void encode(byte, byte[2], Case = Uppercase); + + void write(const byte[], u32bit); + void end_msg(); + + Hex_Encoder(Case); + Hex_Encoder(bool = false, u32bit = 72, Case = Uppercase); + private: + void encode_and_send(const byte[], u32bit); + static const byte BIN_TO_HEX_UPPER[16]; + static const byte BIN_TO_HEX_LOWER[16]; + + const Case casing; + const u32bit line_length; + SecureVector<byte> in, out; + u32bit position, counter; + }; + +/************************************************* +* Hex Decoder * +*************************************************/ +class Hex_Decoder : public Filter + { + public: + static byte decode(const byte[2]); + static bool is_valid(byte); + + void write(const byte[], u32bit); + void end_msg(); + + Hex_Decoder(Decoder_Checking = NONE); + private: + void decode_and_send(const byte[], u32bit); + void handle_bad_char(byte); + static const byte HEX_TO_BIN[256]; + + const Decoder_Checking checking; + SecureVector<byte> in, out; + u32bit position; + }; + +} + +#endif diff --git a/include/hmac.h b/include/hmac.h new file mode 100644 index 000000000..6eba90187 --- /dev/null +++ b/include/hmac.h @@ -0,0 +1,34 @@ +/************************************************* +* HMAC Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_HMAC_H__ +#define BOTAN_HMAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* HMAC * +*************************************************/ +class HMAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const; + MessageAuthenticationCode* clone() const; + HMAC(const std::string&); + ~HMAC() { delete hash; } + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> i_key, o_key; + }; + +} + +#endif diff --git a/include/idea.h b/include/idea.h new file mode 100644 index 000000000..136708bfe --- /dev/null +++ b/include/idea.h @@ -0,0 +1,33 @@ +/************************************************* +* IDEA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_IDEA_H__ +#define BOTAN_IDEA_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* IDEA * +*************************************************/ +class IDEA : public BlockCipher + { + public: + void clear() throw() { EK.clear(); DK.clear(); } + std::string name() const { return "IDEA"; } + BlockCipher* clone() const { return new IDEA; } + IDEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + static u16bit mul_inv(u16bit); + SecureBuffer<u16bit, 52> EK, DK; + }; + +} + +#endif diff --git a/include/if_algo.h b/include/if_algo.h new file mode 100644 index 000000000..323d58256 --- /dev/null +++ b/include/if_algo.h @@ -0,0 +1,64 @@ +/************************************************* +* IF Scheme Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_IF_ALGO_H__ +#define BOTAN_IF_ALGO_H__ + +#include <botan/x509_key.h> +#include <botan/pkcs8.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* IF Public Key * +*************************************************/ +class IF_Scheme_PublicKey : public virtual X509_PublicKey + { + public: + bool check_key(bool) const; + + const BigInt& get_n() const { return n; } + const BigInt& get_e() const { return e; } + + u32bit max_input_bits() const { return (n.bits() - 1); } + + virtual ~IF_Scheme_PublicKey() {} + protected: + virtual void X509_load_hook(); + BigInt n, e; + IF_Core core; + private: + MemoryVector<byte> DER_encode_pub() const; + MemoryVector<byte> DER_encode_params() const; + void BER_decode_params(DataSource&); + void BER_decode_pub(DataSource&); + }; + +/************************************************* +* IF Private Key * +*************************************************/ +class IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey, + public virtual PKCS8_PrivateKey + { + public: + bool check_key(bool) const; + + const BigInt& get_p() const { return p; } + const BigInt& get_q() const { return q; } + const BigInt& get_d() const { return d; } + + virtual ~IF_Scheme_PrivateKey() {} + protected: + virtual void PKCS8_load_hook(); + BigInt d, p, q, d1, d2, c; + private: + SecureVector<byte> DER_encode_priv() const; + void BER_decode_priv(DataSource&); + }; + +} + +#endif diff --git a/include/init.h b/include/init.h new file mode 100644 index 000000000..6a3fcdbbd --- /dev/null +++ b/include/init.h @@ -0,0 +1,35 @@ +/************************************************* +* Library Initialization Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_INIT_H__ +#define BOTAN_INIT_H__ + +#include <string> + +namespace Botan { + +namespace Init { + +/************************************************* +* Main Library Initialization/Shutdown Functions * +*************************************************/ +void initialize(const std::string& = ""); +void deinitialize(); + +} + +/************************************************* +* Library Initialization/Shutdown Object * +*************************************************/ +class LibraryInitializer + { + public: + LibraryInitializer(const std::string& = ""); + ~LibraryInitializer(); + }; + +} + +#endif diff --git a/include/kasumi.h b/include/kasumi.h new file mode 100644 index 000000000..0d070380e --- /dev/null +++ b/include/kasumi.h @@ -0,0 +1,38 @@ +/************************************************* +* KASUMI Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_KASUMI_H__ +#define BOTAN_KASUMI_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* KASUMI * +*************************************************/ +class KASUMI : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "KASUMI"; } + BlockCipher* clone() const { return new KASUMI; } + + KASUMI() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + SecureBuffer<u16bit, 64> EK; + }; + +extern const byte KASUMI_SBOX_S7[128]; +extern const u16bit KASUMI_SBOX_S9[512]; + + +} + +#endif diff --git a/include/kdf.h b/include/kdf.h new file mode 100644 index 000000000..ee020dcfa --- /dev/null +++ b/include/kdf.h @@ -0,0 +1,57 @@ +/************************************************* +* KDF Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_KDF_H__ +#define BOTAN_KDF_H__ + +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* KDF1 * +*************************************************/ +class KDF1 : public KDF + { + public: + KDF1(const std::string&); + private: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + const std::string hash_name; + }; + +/************************************************* +* KDF2 * +*************************************************/ +class KDF2 : public KDF + { + public: + + KDF2(const std::string&); + private: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + const std::string hash_name; + }; + +/************************************************* +* X9.42 PRF * +*************************************************/ +class X942_PRF : public KDF + { + public: + X942_PRF(const std::string&); + private: + SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + std::string key_wrap_oid; + }; + +} + +#endif diff --git a/include/keypair.h b/include/keypair.h new file mode 100644 index 000000000..44d5a000f --- /dev/null +++ b/include/keypair.h @@ -0,0 +1,25 @@ +/************************************************* +* Keypair Checks Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_KEYPAIR_H__ +#define BOTAN_KEYPAIR_H__ + +#include <botan/look_pk.h> + +namespace Botan { + +namespace KeyPair { + +/************************************************* +* Check key pair consistency * +*************************************************/ +void check_key(PK_Encryptor*, PK_Decryptor*); +void check_key(PK_Signer*, PK_Verifier*); + +} + +} + +#endif diff --git a/include/libstate.h b/include/libstate.h new file mode 100644 index 000000000..340644502 --- /dev/null +++ b/include/libstate.h @@ -0,0 +1,84 @@ +/************************************************* +* Library Internal/Global State Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_LIB_STATE_H__ +#define BOTAN_LIB_STATE_H__ + +#include <botan/base.h> +#include <string> +#include <vector> +#include <map> + +namespace Botan { + +/************************************************* +* Global State Container Base * +*************************************************/ +class Library_State + { + public: + class Engine_Iterator + { + public: + class Engine* next(); + Engine_Iterator(const Library_State& l) : lib(l) { n = 0; } + private: + const Library_State& lib; + u32bit n; + }; + friend class Engine_Iterator; + + Allocator* get_allocator(const std::string& = "") const; + void add_allocator(const std::string&, Allocator*); + + void set_prng(RandomNumberGenerator*); + void randomize(byte[], u32bit); + void add_entropy_source(EntropySource*, bool = false); + void add_entropy(const byte[], u32bit); + void add_entropy(EntropySource&, bool); + u32bit seed_prng(bool, u32bit); + + u64bit system_clock() const; + + void set_option(const std::string&, const std::string&, + const std::string&, bool = true); + std::string get_option(const std::string&, const std::string&) const; + bool option_set(const std::string&, const std::string&) const; + + void add_engine(class Engine*); + + class Mutex* get_mutex(); + + Library_State(class Mutex_Factory*, class Timer*); + ~Library_State(); + private: + Library_State(const Library_State&) {} + Library_State& operator=(const Library_State&) { return (*this); } + + class Engine* get_engine_n(u32bit) const; + void set_default_policy(); + + class Mutex_Factory* mutex_factory; + class Timer* timer; + + std::map<std::string, class Mutex*> locks; + std::map<std::string, std::string> settings; + std::map<std::string, Allocator*> alloc_factory; + mutable Allocator* cached_default_allocator; + + RandomNumberGenerator* rng; + std::vector<EntropySource*> entropy_sources; + std::vector<class Engine*> engines; + }; + +/************************************************* +* Global State * +*************************************************/ +Library_State& global_state(); +void set_global_state(Library_State*); + +} + +#endif diff --git a/include/lion.h b/include/lion.h new file mode 100644 index 000000000..516b40de9 --- /dev/null +++ b/include/lion.h @@ -0,0 +1,36 @@ +/************************************************* +* Lion Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_LION_H__ +#define BOTAN_LION_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Lion * +*************************************************/ +class Lion : public BlockCipher + { + public: + void clear() throw(); + std::string name() const; + BlockCipher* clone() const; + Lion(const std::string&, const std::string&, u32bit); + ~Lion() { delete hash; delete cipher; } + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + const u32bit LEFT_SIZE, RIGHT_SIZE; + HashFunction* hash; + StreamCipher* cipher; + SecureVector<byte> key1, key2; + }; + +} + +#endif diff --git a/include/look_add.h b/include/look_add.h new file mode 100644 index 000000000..e6cc265b0 --- /dev/null +++ b/include/look_add.h @@ -0,0 +1,32 @@ +/************************************************* +* Lookup Table Management Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_LOOKUP_MANGEMENT_H__ +#define BOTAN_LOOKUP_MANGEMENT_H__ + +#include <botan/base.h> +#include <botan/mode_pad.h> +#include <botan/s2k.h> + +namespace Botan { + +/************************************************* +* Add an algorithm to the lookup table * +*************************************************/ +void add_algorithm(BlockCipher*); +void add_algorithm(StreamCipher*); +void add_algorithm(HashFunction*); +void add_algorithm(MessageAuthenticationCode*); +void add_algorithm(S2K*); +void add_algorithm(BlockCipherModePaddingMethod*); + +/************************************************* +* Add an alias for an algorithm * +*************************************************/ +void add_alias(const std::string&, const std::string&); + +} + +#endif diff --git a/include/look_pk.h b/include/look_pk.h new file mode 100644 index 000000000..2a6901804 --- /dev/null +++ b/include/look_pk.h @@ -0,0 +1,33 @@ +/************************************************* +* PK Algorithm Lookup Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_LOOKUP_H__ +#define BOTAN_PK_LOOKUP_H__ + +#include <botan/pubkey.h> + +namespace Botan { + +/************************************************* +* Get an PK algorithm object * +*************************************************/ +PK_Encryptor* get_pk_encryptor(const PK_Encrypting_Key&, const std::string&); +PK_Decryptor* get_pk_decryptor(const PK_Decrypting_Key&, const std::string&); + +PK_Signer* get_pk_signer(const PK_Signing_Key&, const std::string&, + Signature_Format = IEEE_1363); + +PK_Verifier* get_pk_verifier(const PK_Verifying_with_MR_Key&, + const std::string&, + Signature_Format = IEEE_1363); +PK_Verifier* get_pk_verifier(const PK_Verifying_wo_MR_Key&, + const std::string&, + Signature_Format = IEEE_1363); + +PK_Key_Agreement* get_pk_kas(const PK_Key_Agreement_Key&, const std::string&); + +} + +#endif diff --git a/include/lookup.h b/include/lookup.h new file mode 100644 index 000000000..7760eea35 --- /dev/null +++ b/include/lookup.h @@ -0,0 +1,81 @@ +/************************************************* +* Algorithm Lookup Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_LOOKUP_H__ +#define BOTAN_LOOKUP_H__ + +#include <botan/base.h> +#include <botan/filters.h> +#include <botan/mode_pad.h> +#include <botan/pk_util.h> +#include <botan/s2k.h> + +namespace Botan { + +/************************************************* +* Retrieve an object from the lookup table * +*************************************************/ +const BlockCipher* retrieve_block_cipher(const std::string&); +const StreamCipher* retrieve_stream_cipher(const std::string&); +const HashFunction* retrieve_hash(const std::string&); +const MessageAuthenticationCode* retrieve_mac(const std::string&); +const S2K* retrieve_s2k(const std::string&); +const BlockCipherModePaddingMethod* retrieve_bc_pad(const std::string&); + +/************************************************* +* Get an algorithm object * +*************************************************/ +BlockCipher* get_block_cipher(const std::string&); +StreamCipher* get_stream_cipher(const std::string&); +HashFunction* get_hash(const std::string&); +MessageAuthenticationCode* get_mac(const std::string&); +S2K* get_s2k(const std::string&); +const BlockCipherModePaddingMethod* get_bc_pad(const std::string&); + +/************************************************* +* Get an EMSA/EME/KDF/MGF function * +*************************************************/ +EME* get_eme(const std::string&); +EMSA* get_emsa(const std::string&); +MGF* get_mgf(const std::string&); +KDF* get_kdf(const std::string&); + +/************************************************* +* Get a cipher object * +*************************************************/ +Keyed_Filter* get_cipher(const std::string&, const SymmetricKey&, + const InitializationVector&, Cipher_Dir); +Keyed_Filter* get_cipher(const std::string&, const SymmetricKey&, Cipher_Dir); +Keyed_Filter* get_cipher(const std::string&, Cipher_Dir); + +/************************************************* +* Check to see if an algorithm exists * +*************************************************/ +bool have_algorithm(const std::string&); + +bool have_block_cipher(const std::string&); +bool have_stream_cipher(const std::string&); +bool have_hash(const std::string&); +bool have_mac(const std::string&); + +/************************************************* +* Dereference an alias * +*************************************************/ +std::string deref_alias(const std::string&); + +/************************************************* +* Query information about an algorithm * +*************************************************/ +u32bit block_size_of(const std::string&); +u32bit output_length_of(const std::string&); + +bool valid_keylength_for(u32bit, const std::string&); +u32bit min_keylength_of(const std::string&); +u32bit max_keylength_of(const std::string&); +u32bit keylength_multiple_of(const std::string&); + +} + +#endif diff --git a/include/lubyrack.h b/include/lubyrack.h new file mode 100644 index 000000000..d237bab8d --- /dev/null +++ b/include/lubyrack.h @@ -0,0 +1,34 @@ +/************************************************* +* Luby-Rackoff Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_LUBY_RACKOFF_H__ +#define BOTAN_LUBY_RACKOFF_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Luby-Rackoff * +*************************************************/ +class LubyRackoff : public BlockCipher + { + public: + void clear() throw(); + std::string name() const; + BlockCipher* clone() const; + LubyRackoff(const std::string&); + ~LubyRackoff() { delete hash; } + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + HashFunction* hash; + SecureVector<byte> K1, K2; + }; + +} + +#endif diff --git a/include/mars.h b/include/mars.h new file mode 100644 index 000000000..6ca718778 --- /dev/null +++ b/include/mars.h @@ -0,0 +1,36 @@ +/************************************************* +* MARS Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MARS_H__ +#define BOTAN_MARS_H__ + +#include <botan/base.h> + +namespace Botan { + +class MARS : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "MARS"; } + BlockCipher* clone() const { return new MARS; } + MARS() : BlockCipher(16, 16, 32, 4) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + void encrypt_round(u32bit&, u32bit&, u32bit&, u32bit&, u32bit) const; + void decrypt_round(u32bit&, u32bit&, u32bit&, u32bit&, u32bit) const; + static void forward_mix(u32bit&, u32bit&, u32bit&, u32bit&); + static void reverse_mix(u32bit&, u32bit&, u32bit&, u32bit&); + + static const u32bit SBOX[512]; + SecureBuffer<u32bit, 40> EK; + }; + +} + +#endif diff --git a/include/md2.h b/include/md2.h new file mode 100644 index 000000000..80a2b34fa --- /dev/null +++ b/include/md2.h @@ -0,0 +1,35 @@ +/************************************************* +* MD2 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MD2_H__ +#define BOTAN_MD2_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* MD2 * +*************************************************/ +class MD2 : public HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD2"; } + HashFunction* clone() const { return new MD2; } + MD2() : HashFunction(16, 16) { clear(); } + private: + void add_data(const byte[], u32bit); + void hash(const byte[]); + void final_result(byte[]); + + SecureBuffer<byte, 48> X; + SecureBuffer<byte, 16> checksum, buffer; + u32bit position; + }; + +} + +#endif diff --git a/include/md4.h b/include/md4.h new file mode 100644 index 000000000..31a934927 --- /dev/null +++ b/include/md4.h @@ -0,0 +1,33 @@ +/************************************************* +* MD4 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MD4_H__ +#define BOTAN_MD4_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* MD4 * +*************************************************/ +class MD4 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD4"; } + HashFunction* clone() const { return new MD4; } + MD4() : MDx_HashFunction(16, 64, false, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 48> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/include/md5.h b/include/md5.h new file mode 100644 index 000000000..bec889627 --- /dev/null +++ b/include/md5.h @@ -0,0 +1,33 @@ +/************************************************* +* MD5 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MD5_H__ +#define BOTAN_MD5_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* MD5 * +*************************************************/ +class MD5 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "MD5"; } + HashFunction* clone() const { return new MD5; } + MD5() : MDx_HashFunction(16, 64, false, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/include/mdx_hash.h b/include/mdx_hash.h new file mode 100644 index 000000000..b7ad8d73a --- /dev/null +++ b/include/mdx_hash.h @@ -0,0 +1,40 @@ +/************************************************* +* MDx Hash Function Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MDX_BASE_H__ +#define BOTAN_MDX_BASE_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* MDx Hash Function Base Class * +*************************************************/ +class MDx_HashFunction : public HashFunction + { + public: + MDx_HashFunction(u32bit, u32bit, bool, bool, u32bit = 8); + virtual ~MDx_HashFunction() {} + protected: + void clear() throw(); + SecureVector<byte> buffer; + u64bit count; + u32bit position; + private: + void add_data(const byte[], u32bit); + void final_result(byte output[]); + + virtual void hash(const byte[]) = 0; + virtual void copy_out(byte[]) = 0; + virtual void write_count(byte[]); + + const bool BIG_BYTE_ENDIAN, BIG_BIT_ENDIAN; + const u32bit COUNT_SIZE; + }; + +} + +#endif diff --git a/include/mem_ops.h b/include/mem_ops.h new file mode 100644 index 000000000..712284cd9 --- /dev/null +++ b/include/mem_ops.h @@ -0,0 +1,31 @@ +/************************************************* +* Memory Operations Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MEMORY_OPS_H__ +#define BOTAN_MEMORY_OPS_H__ + +#include <botan/types.h> +#include <cstring> + +namespace Botan { + +/************************************************* +* Memory Manipulation Functions * +*************************************************/ +template<typename T> inline void copy_mem(T* out, const T* in, u32bit n) + { std::memmove(out, in, sizeof(T)*n); } + +template<typename T> inline void clear_mem(T* ptr, u32bit n) + { std::memset(ptr, 0, sizeof(T)*n); } + +template<typename T> inline void set_mem(T* ptr, u32bit n, byte val) + { std::memset(ptr, val, sizeof(T)*n); } + +template<typename T> inline bool same_mem(const T* p1, const T* p2, u32bit n) + { return (std::memcmp(p1, p2, sizeof(T)*n) == 0); } + +} + +#endif diff --git a/include/mem_pool.h b/include/mem_pool.h new file mode 100644 index 000000000..59599d65c --- /dev/null +++ b/include/mem_pool.h @@ -0,0 +1,70 @@ +/************************************************* +* Pooling Allocator Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_POOLING_ALLOCATOR_H__ +#define BOTAN_POOLING_ALLOCATOR_H__ + +#include <botan/allocate.h> +#include <botan/exceptn.h> +#include <botan/mutex.h> +#include <utility> +#include <vector> + +namespace Botan { + +/************************************************* +* Pooling Allocator * +*************************************************/ +class Pooling_Allocator : public Allocator + { + public: + void* allocate(u32bit); + void deallocate(void*, u32bit); + + void init(); + void destroy(); + + Pooling_Allocator(u32bit, bool); + ~Pooling_Allocator(); + private: + void get_more_core(u32bit); + byte* allocate_blocks(u32bit); + + virtual void* alloc_block(u32bit) = 0; + virtual void dealloc_block(void*, u32bit) = 0; + + class Memory_Block + { + public: + Memory_Block(void*, u32bit, u32bit); + + static u32bit bitmap_size() { return BITMAP_SIZE; } + + bool contains(void*, u32bit) const throw(); + byte* alloc(u32bit) throw(); + void free(void*, u32bit) throw(); + + bool cmp_mem(const void* x) const { return (*this) < x; } + bool operator<(const void*) const; + bool operator<(const Memory_Block&) const; + private: + typedef u64bit bitmap_type; + static const u32bit BITMAP_SIZE = 8 * sizeof(bitmap_type); + bitmap_type bitmap; + byte* buffer, *buffer_end; + u32bit block_size; + }; + + const u32bit PREF_SIZE, BLOCK_SIZE; + + std::vector<Memory_Block> blocks; + std::vector<Memory_Block>::iterator last_used; + std::vector<std::pair<void*, u32bit> > allocated; + Mutex* mutex; + }; + +} + +#endif diff --git a/include/mgf1.h b/include/mgf1.h new file mode 100644 index 000000000..4f54bcf32 --- /dev/null +++ b/include/mgf1.h @@ -0,0 +1,27 @@ +/************************************************* +* MGF1 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MGF1_H__ +#define BOTAN_MGF1_H__ + +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* MGF1 * +*************************************************/ +class MGF1 : public MGF + { + public: + void mask(const byte[], u32bit, byte[], u32bit) const; + MGF1(const std::string&); + private: + const std::string hash_name; + }; + +} + +#endif diff --git a/include/misty1.h b/include/misty1.h new file mode 100644 index 000000000..fe3ed42a5 --- /dev/null +++ b/include/misty1.h @@ -0,0 +1,38 @@ +/************************************************* +* MISTY1 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MISTY1_H__ +#define BOTAN_MISTY1_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* MISTY1 * +*************************************************/ +class MISTY1 : public BlockCipher + { + public: + void clear() throw() { EK.clear(); DK.clear(); } + std::string name() const { return "MISTY1"; } + BlockCipher* clone() const { return new MISTY1; } + MISTY1(u32bit = 8); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + static const byte EK_ORDER[100], DK_ORDER[100]; + SecureBuffer<u16bit, 100> EK, DK; + }; + +extern const byte MISTY1_SBOX_S7[128]; +extern const u16bit MISTY1_SBOX_S9[512]; + + +} + +#endif diff --git a/include/mode_pad.h b/include/mode_pad.h new file mode 100644 index 000000000..38cb55487 --- /dev/null +++ b/include/mode_pad.h @@ -0,0 +1,77 @@ +/************************************************* +* CBC Padding Methods Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_CBC_PADDING_H__ +#define BOTAN_CBC_PADDING_H__ + +#include <botan/base.h> +#include <string> + +namespace Botan { + +/************************************************* +* Block Cipher Mode Padding Method * +*************************************************/ +class BlockCipherModePaddingMethod : public Algorithm + { + public: + virtual void pad(byte[], u32bit, u32bit) const = 0; + virtual u32bit unpad(const byte[], u32bit) const = 0; + virtual u32bit pad_bytes(u32bit, u32bit) const; + virtual bool valid_blocksize(u32bit) const = 0; + }; + +/************************************************* +* PKCS#7 Padding * +*************************************************/ +class PKCS7_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "PKCS7"; } + }; + +/************************************************* +* ANSI X9.23 Padding * +*************************************************/ +class ANSI_X923_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "X9.23"; } + }; + +/************************************************* +* One And Zeros Padding * +*************************************************/ +class OneAndZeros_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "OneAndZeros"; } + }; + +/************************************************* +* Null Padding * +*************************************************/ +class Null_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const { return; } + u32bit unpad(const byte[], u32bit size) const { return size; } + u32bit pad_bytes(u32bit, u32bit) const { return 0; } + bool valid_blocksize(u32bit) const { return true; } + std::string name() const { return "NoPadding"; } + }; + +} + +#endif diff --git a/include/modebase.h b/include/modebase.h new file mode 100644 index 000000000..5dce85bca --- /dev/null +++ b/include/modebase.h @@ -0,0 +1,35 @@ +/************************************************* +* Block Cipher Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MODEBASE_H__ +#define BOTAN_MODEBASE_H__ + +#include <botan/basefilt.h> + +namespace Botan { + +/************************************************* +* Block Cipher Mode * +*************************************************/ +class BlockCipherMode : public Keyed_Filter + { + public: + std::string name() const; + + BlockCipherMode(const std::string&, const std::string&, + u32bit, u32bit = 0, u32bit = 1); + virtual ~BlockCipherMode() { delete cipher; } + protected: + void set_iv(const InitializationVector&); + const u32bit BLOCK_SIZE, BUFFER_SIZE, IV_METHOD; + const std::string mode_name; + BlockCipher* cipher; + SecureVector<byte> buffer, state; + u32bit position; + }; + +} + +#endif diff --git a/include/modules.h b/include/modules.h new file mode 100644 index 000000000..499782c69 --- /dev/null +++ b/include/modules.h @@ -0,0 +1,39 @@ +/************************************************* +* Module Factory Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MODULE_FACTORIES_H__ +#define BOTAN_MODULE_FACTORIES_H__ + +#include <vector> +#include <string> +#include <map> + +namespace Botan { + +/************************************************* +* Forward Declarations * +*************************************************/ +class Mutex_Factory; +class Timer; +class EntropySource; +class Engine; +class Allocator; + +namespace Modules { + +/************************************************* +* Get the module objects * +*************************************************/ +class Mutex_Factory* get_mutex_factory(); +class Timer* get_timer(); +std::vector<EntropySource*> get_entropy_sources(); +std::vector<Engine*> get_engines(); +std::map<std::string, Allocator*> get_allocators(); + +} + +} + +#endif diff --git a/include/mp_asm.h b/include/mp_asm.h new file mode 100644 index 000000000..1066ae43b --- /dev/null +++ b/include/mp_asm.h @@ -0,0 +1,77 @@ +/************************************************* +* Lowest Level MPI Algorithms Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MP_ASM_H__ +#define BOTAN_MP_ASM_H__ + +#include <botan/mp_types.h> + +#if (BOTAN_MP_WORD_BITS == 8) + typedef Botan::u16bit dword; +#elif (BOTAN_MP_WORD_BITS == 16) + typedef Botan::u32bit dword; +#elif (BOTAN_MP_WORD_BITS == 32) + typedef Botan::u64bit dword; +#elif (BOTAN_MP_WORD_BITS == 64) + #error BOTAN_MP_WORD_BITS can be 64 only with assembly support +#else + #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 +#endif + +namespace Botan { + +extern "C" { + +/************************************************* +* Word Multiply/Add * +*************************************************/ +inline word word_madd2(word a, word b, word c, word* carry) + { + dword z = (dword)a * b + c; + *carry = (word)(z >> BOTAN_MP_WORD_BITS); + return (word)z; + } + +/************************************************* +* Word Multiply/Add * +*************************************************/ +inline word word_madd3(word a, word b, word c, word d, word* carry) + { + dword z = (dword)a * b + c + d; + *carry = (word)(z >> BOTAN_MP_WORD_BITS); + return (word)z; + } + +/************************************************* +* Multiply-Add Accumulator * +*************************************************/ +inline void word3_muladd(word* w2, word* w1, word* w0, word a, word b) + { + *w0 = word_madd2(a, b, *w0, &a); + *w1 += a; + *w2 += (*w1 < a) ? 1 : 0; + } + +/************************************************* +* Multiply-Add Accumulator * +*************************************************/ +inline void word3_muladd_2(word* w2, word* w1, word* w0, word a, word b) + { + a = word_madd2(a, b, 0, &b); + + *w0 += a; + *w1 += b + ((*w0 < a) ? 1 : 0); + *w2 += (*w1 < b) ? 1 : 0; + + *w0 += a; + *w1 += b + ((*w0 < a) ? 1 : 0); + *w2 += (*w1 < b) ? 1 : 0; + } + +} + +} + +#endif diff --git a/include/mp_asmi.h b/include/mp_asmi.h new file mode 100644 index 000000000..b05199d0a --- /dev/null +++ b/include/mp_asmi.h @@ -0,0 +1,157 @@ +/************************************************* +* Lowest Level MPI Algorithms Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MP_ASM_INTERNAL_H__ +#define BOTAN_MP_ASM_INTERNAL_H__ + +#include <botan/mp_asm.h> + +namespace Botan { + +extern "C" { + +/************************************************* +* Word Addition * +*************************************************/ +inline word word_add(word x, word y, word* carry) + { + word z = x + y; + word c1 = (z < x); + z += *carry; + *carry = c1 | (z < *carry); + return z; + } + +/************************************************* +* Eight Word Block Addition, Two Argument * +*************************************************/ +inline word word8_add2(word x[8], const word y[8], word carry) + { + x[0] = word_add(x[0], y[0], &carry); + x[1] = word_add(x[1], y[1], &carry); + x[2] = word_add(x[2], y[2], &carry); + x[3] = word_add(x[3], y[3], &carry); + x[4] = word_add(x[4], y[4], &carry); + x[5] = word_add(x[5], y[5], &carry); + x[6] = word_add(x[6], y[6], &carry); + x[7] = word_add(x[7], y[7], &carry); + return carry; + } + +/************************************************* +* Eight Word Block Addition, Three Argument * +*************************************************/ +inline word word8_add3(word z[8], const word x[8], + const word y[8], word carry) + { + z[0] = word_add(x[0], y[0], &carry); + z[1] = word_add(x[1], y[1], &carry); + z[2] = word_add(x[2], y[2], &carry); + z[3] = word_add(x[3], y[3], &carry); + z[4] = word_add(x[4], y[4], &carry); + z[5] = word_add(x[5], y[5], &carry); + z[6] = word_add(x[6], y[6], &carry); + z[7] = word_add(x[7], y[7], &carry); + return carry; + } + +/************************************************* +* Word Subtraction * +*************************************************/ +inline word word_sub(word x, word y, word* carry) + { + word t0 = x - y; + word c1 = (t0 > x); + word z = t0 - *carry; + *carry = c1 | (z > t0); + return z; + } + +/************************************************* +* Eight Word Block Subtraction, Two Argument * +*************************************************/ +inline word word8_sub2(word x[4], const word y[4], word carry) + { + x[0] = word_sub(x[0], y[0], &carry); + x[1] = word_sub(x[1], y[1], &carry); + x[2] = word_sub(x[2], y[2], &carry); + x[3] = word_sub(x[3], y[3], &carry); + x[4] = word_sub(x[4], y[4], &carry); + x[5] = word_sub(x[5], y[5], &carry); + x[6] = word_sub(x[6], y[6], &carry); + x[7] = word_sub(x[7], y[7], &carry); + return carry; + } + +/************************************************* +* Eight Word Block Subtraction, Three Argument * +*************************************************/ +inline word word8_sub3(word z[8], const word x[8], + const word y[8], word carry) + { + z[0] = word_sub(x[0], y[0], &carry); + z[1] = word_sub(x[1], y[1], &carry); + z[2] = word_sub(x[2], y[2], &carry); + z[3] = word_sub(x[3], y[3], &carry); + z[4] = word_sub(x[4], y[4], &carry); + z[5] = word_sub(x[5], y[5], &carry); + z[6] = word_sub(x[6], y[6], &carry); + z[7] = word_sub(x[7], y[7], &carry); + return carry; + } + +/************************************************* +* Eight Word Block Linear Multiplication * +*************************************************/ +inline word word8_linmul2(word x[4], word y, word carry) + { + x[0] = word_madd2(x[0], y, carry, &carry); + x[1] = word_madd2(x[1], y, carry, &carry); + x[2] = word_madd2(x[2], y, carry, &carry); + x[3] = word_madd2(x[3], y, carry, &carry); + x[4] = word_madd2(x[4], y, carry, &carry); + x[5] = word_madd2(x[5], y, carry, &carry); + x[6] = word_madd2(x[6], y, carry, &carry); + x[7] = word_madd2(x[7], y, carry, &carry); + return carry; + } + +/************************************************* +* Eight Word Block Linear Multiplication * +*************************************************/ +inline word word8_linmul3(word z[8], const word x[8], word y, word carry) + { + z[0] = word_madd2(x[0], y, carry, &carry); + z[1] = word_madd2(x[1], y, carry, &carry); + z[2] = word_madd2(x[2], y, carry, &carry); + z[3] = word_madd2(x[3], y, carry, &carry); + z[4] = word_madd2(x[4], y, carry, &carry); + z[5] = word_madd2(x[5], y, carry, &carry); + z[6] = word_madd2(x[6], y, carry, &carry); + z[7] = word_madd2(x[7], y, carry, &carry); + return carry; + } + +/************************************************* +* Eight Word Block Multiply/Add * +*************************************************/ +inline word word8_madd3(word z[8], const word x[8], word y, word carry) + { + z[0] = word_madd3(x[0], y, z[0], carry, &carry); + z[1] = word_madd3(x[1], y, z[1], carry, &carry); + z[2] = word_madd3(x[2], y, z[2], carry, &carry); + z[3] = word_madd3(x[3], y, z[3], carry, &carry); + z[4] = word_madd3(x[4], y, z[4], carry, &carry); + z[5] = word_madd3(x[5], y, z[5], carry, &carry); + z[6] = word_madd3(x[6], y, z[6], carry, &carry); + z[7] = word_madd3(x[7], y, z[7], carry, &carry); + return carry; + } + +} + +} + +#endif diff --git a/include/mp_core.h b/include/mp_core.h new file mode 100644 index 000000000..fd489a561 --- /dev/null +++ b/include/mp_core.h @@ -0,0 +1,88 @@ +/************************************************* +* MPI Algorithms Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MP_CORE_H__ +#define BOTAN_MP_CORE_H__ + +#include <botan/mp_types.h> + +namespace Botan { + +/************************************************* +* The size of the word type, in bits * +*************************************************/ +const u32bit MP_WORD_BITS = BOTAN_MP_WORD_BITS; + +extern "C" { + +/************************************************* +* Addition/Subtraction Operations * +*************************************************/ +void bigint_add2(word[], u32bit, const word[], u32bit); +void bigint_add3(word[], const word[], u32bit, const word[], u32bit); + +word bigint_add2_nc(word[], u32bit, const word[], u32bit); +word bigint_add3_nc(word[], const word[], u32bit, const word[], u32bit); + +void bigint_sub2(word[], u32bit, const word[], u32bit); +void bigint_sub3(word[], const word[], u32bit, const word[], u32bit); + +/************************************************* +* Shift Operations * +*************************************************/ +void bigint_shl1(word[], u32bit, u32bit, u32bit); +void bigint_shl2(word[], const word[], u32bit, u32bit, u32bit); +void bigint_shr1(word[], u32bit, u32bit, u32bit); +void bigint_shr2(word[], const word[], u32bit, u32bit, u32bit); + +/************************************************* +* Multiplication and Squaring Operations * +*************************************************/ +void bigint_linmul2(word[], u32bit, word); +void bigint_linmul3(word[], const word[], u32bit, word); + +void bigint_simple_mul(word[], const word[], u32bit, const word[], u32bit); +void bigint_linmul_add(word[], u32bit, const word[], u32bit, word); + +/************************************************* +* Modular Reduction * +*************************************************/ +void montgomery_reduce(word[], u32bit, const word[], u32bit, word); + +/************************************************* +* Misc Utility Operations * +*************************************************/ +u32bit bigint_divcore(word, word, word, word, word, word); +s32bit bigint_cmp(const word[], u32bit, const word[], u32bit); +word bigint_divop(word, word, word); +word bigint_modop(word, word, word); +void bigint_wordmul(word, word, word*, word*); + +/************************************************* +* Comba Multiplication / Squaring * +*************************************************/ +void bigint_comba_mul4(word[8], const word[4], const word[4]); +void bigint_comba_mul6(word[12], const word[6], const word[6]); +void bigint_comba_mul8(word[16], const word[8], const word[8]); + +void bigint_comba_sqr4(word[8], const word[4]); +void bigint_comba_sqr6(word[12], const word[6]); +void bigint_comba_sqr8(word[16], const word[8]); + +} + +/************************************************* +* High Level Multiplication/Squaring Interfaces * +*************************************************/ +void bigint_mul(word[], u32bit, word[], + const word[], u32bit, u32bit, + const word[], u32bit, u32bit); + +void bigint_sqr(word[], u32bit, word[], + const word[], u32bit, u32bit); + +} + +#endif diff --git a/include/mp_types.h b/include/mp_types.h new file mode 100644 index 000000000..0585c6229 --- /dev/null +++ b/include/mp_types.h @@ -0,0 +1,31 @@ +/************************************************* +* Low Level MPI Types Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MPI_TYPES_H__ +#define BOTAN_MPI_TYPES_H__ + +#include <botan/types.h> + +namespace Botan { + +#if (BOTAN_MP_WORD_BITS == 8) + typedef byte word; +#elif (BOTAN_MP_WORD_BITS == 16) + typedef u16bit word; +#elif (BOTAN_MP_WORD_BITS == 32) + typedef u32bit word; +#elif (BOTAN_MP_WORD_BITS == 64) + typedef u64bit word; +#else + #error BOTAN_MP_WORD_BITS must be 8, 16, 32, or 64 +#endif + +const word MP_WORD_MASK = ~((word)0); +const word MP_WORD_TOP_BIT = (word)1 << (8*sizeof(word) - 1); +const word MP_WORD_MAX = MP_WORD_MASK; + +} + +#endif diff --git a/include/mutex.h b/include/mutex.h new file mode 100644 index 000000000..30e7b231f --- /dev/null +++ b/include/mutex.h @@ -0,0 +1,48 @@ +/************************************************* +* Mutex Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MUTEX_H__ +#define BOTAN_MUTEX_H__ + +#include <botan/exceptn.h> + +namespace Botan { + +/************************************************* +* Mutex Base Class * +*************************************************/ +class Mutex + { + public: + virtual void lock() = 0; + virtual void unlock() = 0; + virtual ~Mutex() {} + }; + +/************************************************* +* Mutex Holding Class * +*************************************************/ +class Mutex_Holder + { + public: + Mutex_Holder(Mutex*); + ~Mutex_Holder(); + private: + Mutex* mux; + }; + +/************************************************* +* Mutex Factory * +*************************************************/ +class Mutex_Factory + { + public: + virtual Mutex* make(); + virtual ~Mutex_Factory() {} + }; + +} + +#endif diff --git a/include/nr.h b/include/nr.h new file mode 100644 index 000000000..14749a6b5 --- /dev/null +++ b/include/nr.h @@ -0,0 +1,60 @@ +/************************************************* +* Nyberg-Rueppel Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_NYBERG_RUEPPEL_H__ +#define BOTAN_NYBERG_RUEPPEL_H__ + +#include <botan/dl_algo.h> +#include <botan/pk_core.h> + +namespace Botan { + +/************************************************* +* Nyberg-Rueppel Public Key * +*************************************************/ +class NR_PublicKey : public PK_Verifying_with_MR_Key, + public virtual DL_Scheme_PublicKey + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + u32bit max_input_bits() const; + + NR_PublicKey(const DL_Group&, const BigInt&); + protected: + std::string algo_name() const { return "NR"; } + NR_PublicKey() {} + + NR_Core core; + private: + friend X509_PublicKey* get_public_key(const std::string&); + DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; } + u32bit message_parts() const { return 2; } + u32bit message_part_size() const; + void X509_load_hook(); + }; + +/************************************************* +* Nyberg-Rueppel Private Key * +*************************************************/ +class NR_PrivateKey : public NR_PublicKey, + public PK_Signing_Key, + public virtual DL_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit) const; + + bool check_key(bool) const; + + NR_PrivateKey(const DL_Group&); + NR_PrivateKey(const DL_Group&, const BigInt&, const BigInt& = 0); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + void PKCS8_load_hook(); + NR_PrivateKey() {} + }; + +} + +#endif diff --git a/include/numthry.h b/include/numthry.h new file mode 100644 index 000000000..d21472c77 --- /dev/null +++ b/include/numthry.h @@ -0,0 +1,91 @@ +/************************************************* +* Number Theory Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_NUMBTHRY_H__ +#define BOTAN_NUMBTHRY_H__ + +#include <botan/bigint.h> +#include <botan/reducer.h> +#include <botan/pow_mod.h> + +namespace Botan { + +/************************************************* +* Fused Arithmetic Operations * +*************************************************/ +BigInt mul_add(const BigInt&, const BigInt&, const BigInt&); +BigInt sub_mul(const BigInt&, const BigInt&, const BigInt&); + +/************************************************* +* Number Theory Functions * +*************************************************/ +inline BigInt abs(const BigInt& n) { return n.abs(); } + +void divide(const BigInt&, const BigInt&, BigInt&, BigInt&); + +BigInt gcd(const BigInt&, const BigInt&); +BigInt lcm(const BigInt&, const BigInt&); + +BigInt square(const BigInt&); +BigInt inverse_mod(const BigInt&, const BigInt&); +s32bit jacobi(const BigInt&, const BigInt&); + +BigInt power_mod(const BigInt&, const BigInt&, const BigInt&); + +/************************************************* +* Utility Functions * +*************************************************/ +u32bit low_zero_bits(const BigInt&); + +/************************************************* +* Primality Testing * +*************************************************/ +bool check_prime(const BigInt&); +bool is_prime(const BigInt&); +bool verify_prime(const BigInt&); + +s32bit simple_primality_tests(const BigInt&); +bool passes_mr_tests(const BigInt&, u32bit = 1); +bool run_primality_tests(const BigInt&, u32bit = 1); + +/************************************************* +* Random Number Generation * +*************************************************/ +BigInt random_integer(u32bit); +BigInt random_integer(const BigInt&, const BigInt&); +BigInt random_prime(u32bit, const BigInt& = 1, u32bit = 1, u32bit = 2); +BigInt random_safe_prime(u32bit); + +SecureVector<byte> generate_dsa_primes(BigInt&, BigInt&, u32bit); +bool generate_dsa_primes(BigInt&, BigInt&, const byte[], u32bit, u32bit, + u32bit = 0); + +/************************************************* +* Prime Numbers * +*************************************************/ +const u32bit PRIME_TABLE_SIZE = 6541; +const u32bit PRIME_PRODUCTS_TABLE_SIZE = 256; + +extern const u16bit PRIMES[]; +extern const u64bit PRIME_PRODUCTS[]; + +/************************************************* +* Miller-Rabin Primality Tester * +*************************************************/ +class MillerRabin_Test + { + public: + bool passes_test(const BigInt&); + MillerRabin_Test(const BigInt&); + private: + BigInt n, r, n_minus_1; + u32bit s; + Fixed_Exponent_Power_Mod pow_mod; + Modular_Reducer reducer; + }; + +} + +#endif diff --git a/include/ofb.h b/include/ofb.h new file mode 100644 index 000000000..fa09443c3 --- /dev/null +++ b/include/ofb.h @@ -0,0 +1,28 @@ +/************************************************* +* OFB Mode Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_OFB_H__ +#define BOTAN_OFB_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/************************************************* +* OFB Mode * +*************************************************/ +class OFB : public BlockCipherMode + { + public: + OFB(const std::string&); + OFB(const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + }; + +} + +#endif diff --git a/include/oids.h b/include/oids.h new file mode 100644 index 000000000..edb3b2bd4 --- /dev/null +++ b/include/oids.h @@ -0,0 +1,39 @@ +/************************************************* +* OID Registry Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_OIDS_H__ +#define BOTAN_OIDS_H__ + +#include <botan/asn1_oid.h> + +namespace Botan { + +namespace OIDS { + +/************************************************* +* Register an OID to string mapping * +*************************************************/ +void add_oid(const OID&, const std::string&); + +/************************************************* +* Do an OID to string lookup * +*************************************************/ +std::string lookup(const OID&); + +/************************************************* +* Do a string to OID lookup * +*************************************************/ +OID lookup(const std::string&); + +/************************************************* +* See if an OID exists in the internal table * +*************************************************/ +bool have_oid(const std::string&); + +} + +} + +#endif diff --git a/include/openpgp.h b/include/openpgp.h new file mode 100644 index 000000000..43adc69b0 --- /dev/null +++ b/include/openpgp.h @@ -0,0 +1,32 @@ +/************************************************* +* OpenPGP Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_OPENPGP_H__ +#define BOTAN_OPENPGP_H__ + +#include <botan/data_src.h> +#include <string> +#include <map> + +namespace Botan { + +namespace OpenPGP { + +/************************************************* +* OpenPGP Base64 encoding/decoding * +*************************************************/ +std::string encode(const byte[], u32bit, const std::string&, + const std::map<std::string, std::string>&); +SecureVector<byte> decode(DataSource&, std::string&, + std::map<std::string, std::string>&); + +std::string encode(const byte[], u32bit, const std::string&); +SecureVector<byte> decode(DataSource&, std::string&); + +} + +} + +#endif diff --git a/include/out_buf.h b/include/out_buf.h new file mode 100644 index 000000000..bac376763 --- /dev/null +++ b/include/out_buf.h @@ -0,0 +1,40 @@ +/************************************************* +* Output Buffer Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_OUTPUT_BUFFER_H__ +#define BOTAN_OUTPUT_BUFFER_H__ + +#include <botan/types.h> +#include <deque> + +namespace Botan { + +/************************************************* +* Container of output buffers for Pipe * +*************************************************/ +class Output_Buffers + { + public: + u32bit read(byte[], u32bit, u32bit); + u32bit peek(byte[], u32bit, u32bit, u32bit) const; + u32bit remaining(u32bit) const; + + void add(class SecureQueue*); + void retire(); + + u32bit message_count() const; + + Output_Buffers(); + ~Output_Buffers(); + private: + class SecureQueue* get(u32bit) const; + + std::deque<SecureQueue*> buffers; + u32bit offset; + }; + +} + +#endif diff --git a/include/par_hash.h b/include/par_hash.h new file mode 100644 index 000000000..702b5595b --- /dev/null +++ b/include/par_hash.h @@ -0,0 +1,33 @@ +/************************************************* +* Parallel Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PAR_HASH_H__ +#define BOTAN_PAR_HASH_H__ + +#include <botan/base.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Parallel * +*************************************************/ +class Parallel : public HashFunction + { + public: + void clear() throw(); + std::string name() const; + HashFunction* clone() const; + Parallel(const std::vector<std::string>&); + ~Parallel(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + std::vector<HashFunction*> hashes; + }; + +} + +#endif diff --git a/include/parsing.h b/include/parsing.h new file mode 100644 index 000000000..87558e0b3 --- /dev/null +++ b/include/parsing.h @@ -0,0 +1,32 @@ +/************************************************* +* Parser Functions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PARSER_H__ +#define BOTAN_PARSER_H__ + +#include <botan/types.h> +#include <string> +#include <vector> + +namespace Botan { + +/************************************************* +* String Parsing Functions * +*************************************************/ +std::vector<std::string> parse_algorithm_name(const std::string&); +std::vector<std::string> split_on(const std::string&, char); +std::vector<u32bit> parse_asn1_oid(const std::string&); +bool x500_name_cmp(const std::string&, const std::string&); +u32bit parse_expr(const std::string&); + +/************************************************* +* String/Integer Conversions * +*************************************************/ +std::string to_string(u64bit, u32bit = 0); +u32bit to_u32bit(const std::string&); + +} + +#endif diff --git a/include/pbe.h b/include/pbe.h new file mode 100644 index 000000000..7a23282cd --- /dev/null +++ b/include/pbe.h @@ -0,0 +1,36 @@ +/************************************************* +* PBE Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PBE_H__ +#define BOTAN_PBE_H__ + +#include <botan/asn1_oid.h> +#include <botan/data_src.h> +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* Password Based Encryption * +*************************************************/ +class PBE : public Filter + { + public: + virtual void set_key(const std::string&) = 0; + virtual void new_params() = 0; + virtual MemoryVector<byte> encode_params() const = 0; + virtual void decode_params(DataSource&) = 0; + virtual OID get_oid() const = 0; + }; + +/************************************************* +* Get a PBE object * +*************************************************/ +PBE* get_pbe(const std::string&); +PBE* get_pbe(const OID&, DataSource&); + +} + +#endif diff --git a/include/pbe_pkcs.h b/include/pbe_pkcs.h new file mode 100644 index 000000000..a39b8edce --- /dev/null +++ b/include/pbe_pkcs.h @@ -0,0 +1,67 @@ +/************************************************* +* PKCS PBE Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PBE_PKCS_H__ +#define BOTAN_PBE_PKCS_H__ + +#include <botan/pbe.h> +#include <botan/pipe.h> + +namespace Botan { + +/************************************************* +* PKCS#5 v1.5 PBE * +*************************************************/ +class PBE_PKCS5v15 : public PBE + { + public: + void write(const byte[], u32bit); + void start_msg(); + void end_msg(); + PBE_PKCS5v15(const std::string&, const std::string&, Cipher_Dir); + private: + void set_key(const std::string&); + void new_params(); + MemoryVector<byte> encode_params() const; + void decode_params(DataSource&); + OID get_oid() const; + void flush_pipe(bool); + const Cipher_Dir direction; + const std::string digest, cipher; + SecureVector<byte> salt, key, iv; + u32bit iterations; + Pipe pipe; + }; + +/************************************************* +* PKCS#5 v2.0 PBE * +*************************************************/ +class PBE_PKCS5v20 : public PBE + { + public: + void write(const byte[], u32bit); + void start_msg(); + void end_msg(); + PBE_PKCS5v20(DataSource&); + PBE_PKCS5v20(const std::string&, const std::string&); + private: + void set_key(const std::string&); + void new_params(); + MemoryVector<byte> encode_params() const; + void decode_params(DataSource&); + OID get_oid() const; + void flush_pipe(bool); + bool known_cipher(const std::string&) const; + + const Cipher_Dir direction; + std::string digest, cipher, cipher_algo; + SecureVector<byte> salt, key, iv; + u32bit iterations, key_length; + Pipe pipe; + }; + +} + +#endif diff --git a/include/pem.h b/include/pem.h new file mode 100644 index 000000000..08aa1c0cf --- /dev/null +++ b/include/pem.h @@ -0,0 +1,29 @@ +/************************************************* +* PEM Encoding/Decoding Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PEM_H__ +#define BOTAN_PEM_H__ + +#include <botan/data_src.h> + +namespace Botan { + +namespace PEM_Code { + +/************************************************* +* PEM Encoding/Decoding * +*************************************************/ +std::string encode(const byte[], u32bit, const std::string&); +std::string encode(const MemoryRegion<byte>&, const std::string&); + +SecureVector<byte> decode(DataSource&, std::string&); +SecureVector<byte> decode_check_label(DataSource&, const std::string&); +bool matches(DataSource&, const std::string& = ""); + +} + +} + +#endif diff --git a/include/pgp_s2k.h b/include/pgp_s2k.h new file mode 100644 index 000000000..bb90e8b06 --- /dev/null +++ b/include/pgp_s2k.h @@ -0,0 +1,30 @@ +/************************************************* +* OpenPGP S2K Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_OPENPGP_S2K_H__ +#define BOTAN_OPENPGP_S2K_H__ + +#include <botan/s2k.h> + +namespace Botan { + +/************************************************* +* OpenPGP S2K * +*************************************************/ +class OpenPGP_S2K : public S2K + { + public: + std::string name() const; + S2K* clone() const; + OpenPGP_S2K(const std::string&); + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + const std::string hash_name; + }; + +} + +#endif diff --git a/include/pipe.h b/include/pipe.h new file mode 100644 index 000000000..0139e44f9 --- /dev/null +++ b/include/pipe.h @@ -0,0 +1,92 @@ +/************************************************* +* Pipe Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PIPE_H__ +#define BOTAN_PIPE_H__ + +#include <botan/data_src.h> +#include <botan/filter.h> +#include <iosfwd> + +namespace Botan { + +/************************************************* +* Pipe * +*************************************************/ +class Pipe : public DataSource + { + public: + static const u32bit LAST_MESSAGE = 0xFFFFFFFE; + static const u32bit DEFAULT_MESSAGE = 0xFFFFFFFF; + + void write(const byte[], u32bit); + void write(const MemoryRegion<byte>&); + void write(const std::string&); + void write(DataSource&); + void write(byte); + + void process_msg(const byte[], u32bit); + void process_msg(const MemoryRegion<byte>&); + void process_msg(const std::string&); + void process_msg(DataSource&); + + u32bit remaining(u32bit = DEFAULT_MESSAGE) const; + + u32bit read(byte[], u32bit); + u32bit read(byte[], u32bit, u32bit); + u32bit read(byte&, u32bit = DEFAULT_MESSAGE); + + SecureVector<byte> read_all(u32bit = DEFAULT_MESSAGE); + std::string read_all_as_string(u32bit = DEFAULT_MESSAGE); + + u32bit peek(byte[], u32bit, u32bit) const; + u32bit peek(byte[], u32bit, u32bit, u32bit) const; + u32bit peek(byte&, u32bit, u32bit = DEFAULT_MESSAGE) const; + + u32bit default_msg() const { return default_read; } + void set_default_msg(u32bit); + u32bit message_count() const; + bool end_of_data() const; + + void start_msg(); + void end_msg(); + + void prepend(Filter*); + void append(Filter*); + void pop(); + void reset(); + + Pipe(Filter* = 0, Filter* = 0, Filter* = 0, Filter* = 0); + Pipe(Filter*[], u32bit); + ~Pipe(); + private: + Pipe(const Pipe&) : DataSource() {} + Pipe& operator=(const Pipe&) { return (*this); } + void init(); + void destruct(Filter*); + void find_endpoints(Filter*); + void clear_endpoints(Filter*); + + u32bit get_message_no(const std::string&, u32bit) const; + + Filter* pipe; + class Output_Buffers* outputs; + u32bit default_read; + bool inside_msg; + }; + +/************************************************* +* I/O Operators for Pipe * +*************************************************/ +std::ostream& operator<<(std::ostream&, Pipe&); +std::istream& operator>>(std::istream&, Pipe&); + +} + +#endif + +#if defined(BOTAN_EXT_PIPE_UNIXFD_IO) + #include <botan/fd_unix.h> +#endif diff --git a/include/pk_algs.h b/include/pk_algs.h new file mode 100644 index 000000000..f4ac9ae79 --- /dev/null +++ b/include/pk_algs.h @@ -0,0 +1,22 @@ +/************************************************* +* PK Key Factory Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_KEY_FACTORY_H__ +#define BOTAN_PK_KEY_FACTORY_H__ + +#include <botan/x509_key.h> +#include <botan/pkcs8.h> + +namespace Botan { + +/************************************************* +* Get an PK key object * +*************************************************/ +X509_PublicKey* get_public_key(const std::string&); +PKCS8_PrivateKey* get_private_key(const std::string&); + +} + +#endif diff --git a/include/pk_core.h b/include/pk_core.h new file mode 100644 index 000000000..fc1fcbdf2 --- /dev/null +++ b/include/pk_core.h @@ -0,0 +1,118 @@ +/************************************************* +* PK Algorithm Core Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_CORE_H__ +#define BOTAN_PK_CORE_H__ + +#include <botan/bigint.h> +#include <botan/dl_group.h> +#include <botan/blinding.h> +#include <botan/pk_ops.h> + +namespace Botan { + +/************************************************* +* IF Core * +*************************************************/ +class IF_Core + { + public: + BigInt public_op(const BigInt&) const; + BigInt private_op(const BigInt&) const; + + IF_Core& operator=(const IF_Core&); + + IF_Core() { op = 0; } + IF_Core(const IF_Core&); + IF_Core(const BigInt&, const BigInt&, + const BigInt& = 0, const BigInt& = 0, const BigInt& = 0, + const BigInt& = 0, const BigInt& = 0, const BigInt& = 0); + ~IF_Core() { delete op; } + private: + IF_Operation* op; + Blinder blinder; + }; + +/************************************************* +* DSA Core * +*************************************************/ +class DSA_Core + { + public: + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + bool verify(const byte[], u32bit, const byte[], u32bit) const; + + DSA_Core& operator=(const DSA_Core&); + + DSA_Core() { op = 0; } + DSA_Core(const DSA_Core&); + DSA_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~DSA_Core() { delete op; } + private: + DSA_Operation* op; + }; + +/************************************************* +* NR Core * +*************************************************/ +class NR_Core + { + public: + SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const; + SecureVector<byte> verify(const byte[], u32bit) const; + + NR_Core& operator=(const NR_Core&); + + NR_Core() { op = 0; } + NR_Core(const NR_Core&); + NR_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~NR_Core() { delete op; } + private: + NR_Operation* op; + }; + +/************************************************* +* ElGamal Core * +*************************************************/ +class ELG_Core + { + public: + SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const; + SecureVector<byte> decrypt(const byte[], u32bit) const; + + ELG_Core& operator=(const ELG_Core&); + + ELG_Core() { op = 0; } + ELG_Core(const ELG_Core&); + ELG_Core(const DL_Group&, const BigInt&, const BigInt& = 0); + ~ELG_Core() { delete op; } + private: + ELG_Operation* op; + Blinder blinder; + u32bit p_bytes; + }; + +/************************************************* +* DH Core * +*************************************************/ +class DH_Core + { + public: + BigInt agree(const BigInt&) const; + + DH_Core& operator=(const DH_Core&); + + DH_Core() { op = 0; } + DH_Core(const DH_Core&); + DH_Core(const DL_Group&, const BigInt&); + ~DH_Core() { delete op; } + private: + DH_Operation* op; + Blinder blinder; + }; + +} + +#endif diff --git a/include/pk_filts.h b/include/pk_filts.h new file mode 100644 index 000000000..5e532e3b5 --- /dev/null +++ b/include/pk_filts.h @@ -0,0 +1,81 @@ +/************************************************* +* PK Filters Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_FILTERS_H__ +#define BOTAN_PK_FILTERS_H__ + +#include <botan/filter.h> +#include <botan/pubkey.h> + +namespace Botan { + +/************************************************* +* PK_Encryptor Filter * +*************************************************/ +class PK_Encryptor_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + PK_Encryptor_Filter(PK_Encryptor* c) : cipher(c) {} + ~PK_Encryptor_Filter() { delete cipher; } + private: + PK_Encryptor* cipher; + SecureVector<byte> buffer; + }; + +/************************************************* +* PK_Decryptor Filter * +*************************************************/ +class PK_Decryptor_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + PK_Decryptor_Filter(PK_Decryptor* c) : cipher(c) {} + ~PK_Decryptor_Filter() { delete cipher; } + private: + PK_Decryptor* cipher; + SecureVector<byte> buffer; + }; + +/************************************************* +* PK_Signer Filter * +*************************************************/ +class PK_Signer_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + PK_Signer_Filter(PK_Signer* s) : signer(s) {} + ~PK_Signer_Filter() { delete signer; } + private: + PK_Signer* signer; + }; + +/************************************************* +* PK_Verifier Filter * +*************************************************/ +class PK_Verifier_Filter : public Filter + { + public: + void write(const byte[], u32bit); + void end_msg(); + + void set_signature(const byte[], u32bit); + void set_signature(const MemoryRegion<byte>&); + + PK_Verifier_Filter(PK_Verifier* v) : verifier(v) {} + PK_Verifier_Filter(PK_Verifier*, const byte[], u32bit); + PK_Verifier_Filter(PK_Verifier*, const MemoryRegion<byte>&); + ~PK_Verifier_Filter() { delete verifier; } + private: + PK_Verifier* verifier; + SecureVector<byte> signature; + }; + +} + +#endif diff --git a/include/pk_keys.h b/include/pk_keys.h new file mode 100644 index 000000000..96fb2051e --- /dev/null +++ b/include/pk_keys.h @@ -0,0 +1,100 @@ +/************************************************* +* PK Key Types Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_KEYS_H__ +#define BOTAN_PK_KEYS_H__ + +#include <botan/base.h> +#include <botan/asn1_oid.h> + +namespace Botan { + +/************************************************* +* Generic PK Key * +*************************************************/ +class PK_Key + { + public: + virtual std::string algo_name() const = 0; + + virtual OID get_oid() const; + virtual u32bit max_input_bits() const { return 0; } + virtual bool check_key(bool) const { return true; } + virtual u32bit message_parts() const { return 1; } + virtual u32bit message_part_size() const { return 0; } + virtual ~PK_Key() {} + protected: + void check_loaded_public() const; + void check_loaded_private() const; + void check_generated_private() const; + }; + +/************************************************* +* PK Encrypting Key * +*************************************************/ +class PK_Encrypting_Key : public virtual PK_Key + { + public: + virtual SecureVector<byte> encrypt(const byte[], u32bit) const = 0; + virtual ~PK_Encrypting_Key() {} + }; + +/************************************************* +* PK Decrypting Key * +*************************************************/ +class PK_Decrypting_Key : public virtual PK_Key + { + public: + virtual SecureVector<byte> decrypt(const byte[], u32bit) const = 0; + virtual ~PK_Decrypting_Key() {} + }; + +/************************************************* +* PK Signing Key * +*************************************************/ +class PK_Signing_Key : public virtual PK_Key + { + public: + virtual SecureVector<byte> sign(const byte[], u32bit) const = 0; + virtual ~PK_Signing_Key() {} + }; + +/************************************************* +* PK Verifying Key, Message Recovery Version * +*************************************************/ +class PK_Verifying_with_MR_Key : public virtual PK_Key + { + public: + virtual SecureVector<byte> verify(const byte[], u32bit) const = 0; + virtual ~PK_Verifying_with_MR_Key() {} + }; + +/************************************************* +* PK Verifying Key, No Message Recovery Version * +*************************************************/ +class PK_Verifying_wo_MR_Key : public virtual PK_Key + { + public: + virtual bool verify(const byte[], u32bit, + const byte[], u32bit) const = 0; + virtual ~PK_Verifying_wo_MR_Key() {} + }; + +/************************************************* +* PK Secret Value Derivation Key * +*************************************************/ +class PK_Key_Agreement_Key : public virtual PK_Key + { + public: + virtual SecureVector<byte> derive_key(const byte[], u32bit) const = 0; + virtual MemoryVector<byte> public_value() const = 0; + virtual ~PK_Key_Agreement_Key() {} + }; + +typedef PK_Key_Agreement_Key PK_KA_Key; + +} + +#endif diff --git a/include/pk_ops.h b/include/pk_ops.h new file mode 100644 index 000000000..d00c06d32 --- /dev/null +++ b/include/pk_ops.h @@ -0,0 +1,79 @@ +/************************************************* +* Public Key Operations Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PK_OPS_H__ +#define BOTAN_PK_OPS_H__ + +#include <botan/bigint.h> +#include <botan/dl_group.h> + +namespace Botan { + +/************************************************* +* IF Operation * +*************************************************/ +class IF_Operation + { + public: + virtual BigInt public_op(const BigInt&) const = 0; + virtual BigInt private_op(const BigInt&) const = 0; + virtual IF_Operation* clone() const = 0; + virtual ~IF_Operation() {} + }; + +/************************************************* +* DSA Operation * +*************************************************/ +class DSA_Operation + { + public: + virtual bool verify(const byte[], u32bit, + const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual DSA_Operation* clone() const = 0; + virtual ~DSA_Operation() {} + }; + +/************************************************* +* NR Operation * +*************************************************/ +class NR_Operation + { + public: + virtual SecureVector<byte> verify(const byte[], u32bit) const = 0; + virtual SecureVector<byte> sign(const byte[], u32bit, + const BigInt&) const = 0; + virtual NR_Operation* clone() const = 0; + virtual ~NR_Operation() {} + }; + +/************************************************* +* ElGamal Operation * +*************************************************/ +class ELG_Operation + { + public: + virtual SecureVector<byte> encrypt(const byte[], u32bit, + const BigInt&) const = 0; + virtual BigInt decrypt(const BigInt&, const BigInt&) const = 0; + virtual ELG_Operation* clone() const = 0; + virtual ~ELG_Operation() {} + }; + +/************************************************* +* DH Operation * +*************************************************/ +class DH_Operation + { + public: + virtual BigInt agree(const BigInt&) const = 0; + virtual DH_Operation* clone() const = 0; + virtual ~DH_Operation() {} + }; + +} + +#endif diff --git a/include/pk_util.h b/include/pk_util.h new file mode 100644 index 000000000..db18d3cc5 --- /dev/null +++ b/include/pk_util.h @@ -0,0 +1,82 @@ +/************************************************* +* PK Utility Classes Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PUBKEY_UTIL_H__ +#define BOTAN_PUBKEY_UTIL_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Encoding Method for Encryption * +*************************************************/ +class EME + { + public: + virtual u32bit maximum_input_size(u32bit) const = 0; + SecureVector<byte> encode(const byte[], u32bit, u32bit) const; + SecureVector<byte> encode(const MemoryRegion<byte>&, u32bit) const; + SecureVector<byte> decode(const byte[], u32bit, u32bit) const; + SecureVector<byte> decode(const MemoryRegion<byte>&, u32bit) const; + + virtual ~EME() {} + private: + virtual SecureVector<byte> pad(const byte[], u32bit, u32bit) const = 0; + virtual SecureVector<byte> unpad(const byte[], u32bit, u32bit) const = 0; + }; + +/************************************************* +* Encoding Method for Signatures, Appendix * +*************************************************/ +class EMSA + { + public: + virtual void update(const byte[], u32bit) = 0; + virtual SecureVector<byte> raw_data() = 0; + virtual SecureVector<byte> encoding_of(const MemoryRegion<byte>&, + u32bit) = 0; + virtual bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, + u32bit) throw(); + virtual ~EMSA() {} + }; + +/************************************************* +* Key Derivation Function * +*************************************************/ +class KDF + { + public: + SecureVector<byte> derive_key(u32bit, const MemoryRegion<byte>&, + const std::string& = "") const; + SecureVector<byte> derive_key(u32bit, const MemoryRegion<byte>&, + const MemoryRegion<byte>&) const; + SecureVector<byte> derive_key(u32bit, const MemoryRegion<byte>&, + const byte[], u32bit) const; + + SecureVector<byte> derive_key(u32bit, const byte[], u32bit, + const std::string& = "") const; + SecureVector<byte> derive_key(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + virtual ~KDF() {} + private: + virtual SecureVector<byte> derive(u32bit, const byte[], u32bit, + const byte[], u32bit) const = 0; + }; + +/************************************************* +* Mask Generation Function * +*************************************************/ +class MGF + { + public: + virtual void mask(const byte[], u32bit, byte[], u32bit) const = 0; + virtual ~MGF() {} + }; + +} + +#endif diff --git a/include/pkcs10.h b/include/pkcs10.h new file mode 100644 index 000000000..212bf7749 --- /dev/null +++ b/include/pkcs10.h @@ -0,0 +1,53 @@ +/************************************************* +* PKCS #10 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PKCS10_H__ +#define BOTAN_PKCS10_H__ + +#include <botan/x509_obj.h> +#include <botan/pkcs8.h> +#include <vector> + +namespace Botan { + +/************************************************* +* PKCS #10 Certificate Request * +*************************************************/ +class PKCS10_Request : public X509_Object + { + public: + X509_PublicKey* subject_public_key() const; + + MemoryVector<byte> raw_public_key() const; + X509_DN subject_dn() const; + AlternativeName subject_alt_name() const; + Key_Constraints constraints() const; + std::vector<OID> ex_constraints() const; + + bool is_CA() const; + u32bit path_limit() const; + + std::string challenge_password() const; + + PKCS10_Request(DataSource&); + PKCS10_Request(const std::string&); + private: + void force_decode(); + void handle_attribute(const Attribute&); + void handle_v3_extension(const Extension&); + + MemoryVector<byte> pub_key; + X509_DN dn; + AlternativeName subject_alt; + std::string challenge; + Key_Constraints constraints_value; + std::vector<OID> ex_constraints_list; + bool is_ca; + u32bit max_path_len; + }; + +} + +#endif diff --git a/include/pkcs5.h b/include/pkcs5.h new file mode 100644 index 000000000..510f2af87 --- /dev/null +++ b/include/pkcs5.h @@ -0,0 +1,45 @@ +/************************************************* +* PKCS #5 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PKCS5_H__ +#define BOTAN_PKCS5_H__ + +#include <botan/s2k.h> + +namespace Botan { + +/************************************************* +* PKCS #5 PBKDF1 * +*************************************************/ +class PKCS5_PBKDF1 : public S2K + { + public: + std::string name() const; + S2K* clone() const { return new PKCS5_PBKDF1(hash_name); } + PKCS5_PBKDF1(const std::string&); + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + const std::string hash_name; + }; + +/************************************************* +* PKCS #5 PBKDF2 * +*************************************************/ +class PKCS5_PBKDF2 : public S2K + { + public: + std::string name() const; + S2K* clone() const { return new PKCS5_PBKDF2(hash_name); } + PKCS5_PBKDF2(const std::string&); + private: + OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const; + const std::string hash_name; + }; + +} + +#endif diff --git a/include/pkcs8.h b/include/pkcs8.h new file mode 100644 index 000000000..dc70acbe4 --- /dev/null +++ b/include/pkcs8.h @@ -0,0 +1,59 @@ +/************************************************* +* PKCS #8 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PKCS8_H__ +#define BOTAN_PKCS8_H__ + +#include <botan/x509_key.h> +#include <botan/ui.h> + +namespace Botan { + +/************************************************* +* PKCS #8 Private Key * +*************************************************/ +class PKCS8_PrivateKey : public virtual X509_PublicKey + { + public: + virtual SecureVector<byte> DER_encode_priv() const = 0; + virtual void BER_decode_priv(DataSource&) = 0; + virtual ~PKCS8_PrivateKey() {} + }; + +/************************************************* +* PKCS #8 General Exception * +*************************************************/ +struct PKCS8_Exception : public Decoding_Error + { + PKCS8_Exception(const std::string& error) : + Decoding_Error("PKCS #8: " + error) {} + }; + +namespace PKCS8 { + +/************************************************* +* PKCS #8 Private Key Encoding/Decoding * +*************************************************/ +void encode(const PKCS8_PrivateKey&, Pipe&, X509_Encoding = PEM); +void encrypt_key(const PKCS8_PrivateKey&, Pipe&, const std::string&, + const std::string& = "", X509_Encoding = PEM); + +std::string PEM_encode(const PKCS8_PrivateKey&); +std::string PEM_encode(const PKCS8_PrivateKey&, const std::string&, + const std::string& = ""); + +PKCS8_PrivateKey* load_key(DataSource&, const User_Interface&); +PKCS8_PrivateKey* load_key(DataSource&, const std::string& = ""); + +PKCS8_PrivateKey* load_key(const std::string&, const User_Interface&); +PKCS8_PrivateKey* load_key(const std::string&, const std::string& = ""); + +PKCS8_PrivateKey* copy_key(const PKCS8_PrivateKey&); + +} + +} + +#endif diff --git a/include/pow_mod.h b/include/pow_mod.h new file mode 100644 index 000000000..c0fa49f9c --- /dev/null +++ b/include/pow_mod.h @@ -0,0 +1,91 @@ +/************************************************* +* Modular Exponentiator Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_POWER_MOD_H__ +#define BOTAN_POWER_MOD_H__ + +#include <botan/bigint.h> + +namespace Botan { + +/************************************************* +* Modular Exponentiator Interface * +*************************************************/ +class Modular_Exponentiator + { + public: + virtual void set_base(const BigInt&) = 0; + virtual void set_exponent(const BigInt&) = 0; + virtual BigInt execute() const = 0; + virtual Modular_Exponentiator* copy() const = 0; + virtual ~Modular_Exponentiator() {} + }; + +/************************************************* +* Modular Exponentiator Proxy * +*************************************************/ +class Power_Mod + { + public: + enum Usage_Hints { + NO_HINTS = 0x0000, + + BASE_IS_FIXED = 0x0001, + BASE_IS_SMALL = 0x0002, + BASE_IS_LARGE = 0x0004, + BASE_IS_2 = 0x0008, + + EXP_IS_FIXED = 0x0100, + EXP_IS_SMALL = 0x0200, + EXP_IS_LARGE = 0x0400 + }; + + void set_modulus(const BigInt&, Usage_Hints = NO_HINTS) const; + void set_base(const BigInt&) const; + void set_exponent(const BigInt&) const; + + BigInt execute() const; + + Power_Mod& operator=(const Power_Mod&); + + Power_Mod(const BigInt& = 0, Usage_Hints = NO_HINTS); + Power_Mod(const Power_Mod&); + ~Power_Mod(); + private: + mutable Modular_Exponentiator* core; + Usage_Hints hints; + }; + +/************************************************* +* Fixed Exponent Modular Exponentiator Proxy * +*************************************************/ +class Fixed_Exponent_Power_Mod : public Power_Mod + { + public: + BigInt operator()(const BigInt& b) const + { set_base(b); return execute(); } + + Fixed_Exponent_Power_Mod() {} + Fixed_Exponent_Power_Mod(const BigInt&, const BigInt&, + Usage_Hints = NO_HINTS); + }; + +/************************************************* +* Fixed Base Modular Exponentiator Proxy * +*************************************************/ +class Fixed_Base_Power_Mod : public Power_Mod + { + public: + BigInt operator()(const BigInt& e) const + { set_exponent(e); return execute(); } + + Fixed_Base_Power_Mod() {} + Fixed_Base_Power_Mod(const BigInt&, const BigInt&, + Usage_Hints = NO_HINTS); + }; + +} + +#endif diff --git a/include/pubkey.h b/include/pubkey.h new file mode 100644 index 000000000..68abe290f --- /dev/null +++ b/include/pubkey.h @@ -0,0 +1,169 @@ +/************************************************* +* Public Key Interface Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_PUBKEY_H__ +#define BOTAN_PUBKEY_H__ + +#include <botan/base.h> +#include <botan/pk_keys.h> +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* Public Key Encryptor * +*************************************************/ +class PK_Encryptor + { + public: + SecureVector<byte> encrypt(const byte[], u32bit) const; + SecureVector<byte> encrypt(const MemoryRegion<byte>&) const; + virtual u32bit maximum_input_size() const = 0; + virtual ~PK_Encryptor() {} + private: + virtual SecureVector<byte> enc(const byte[], u32bit) const = 0; + }; + +/************************************************* +* Public Key Decryptor * +*************************************************/ +class PK_Decryptor + { + public: + SecureVector<byte> decrypt(const byte[], u32bit) const; + SecureVector<byte> decrypt(const MemoryRegion<byte>&) const; + virtual ~PK_Decryptor() {} + private: + virtual SecureVector<byte> dec(const byte[], u32bit) const = 0; + }; + +/************************************************* +* Public Key Signer * +*************************************************/ +class PK_Signer + { + public: + SecureVector<byte> sign_message(const byte[], u32bit); + SecureVector<byte> sign_message(const MemoryRegion<byte>&); + + void update(byte); + void update(const byte[], u32bit); + void update(const MemoryRegion<byte>&); + + SecureVector<byte> signature(); + + void set_output_format(Signature_Format); + + PK_Signer(const PK_Signing_Key&, const std::string&); + ~PK_Signer() { delete emsa; } + private: + const PK_Signing_Key& key; + Signature_Format sig_format; + EMSA* emsa; + }; + +/************************************************* +* Public Key Verifier * +*************************************************/ +class PK_Verifier + { + public: + bool verify_message(const byte[], u32bit, const byte[], u32bit); + bool verify_message(const MemoryRegion<byte>&, + const MemoryRegion<byte>&); + + void update(byte); + void update(const byte[], u32bit); + void update(const MemoryRegion<byte>&); + + bool check_signature(const byte[], u32bit); + bool check_signature(const MemoryRegion<byte>&); + + void set_input_format(Signature_Format); + + PK_Verifier(const PK_Key&, const std::string&); + virtual ~PK_Verifier() { delete emsa; } + protected: + virtual bool validate_signature(const MemoryRegion<byte>&, + const byte[], u32bit) = 0; + Signature_Format sig_format; + EMSA* emsa; + private: + const PK_Key& key; + }; + +/************************************************* +* Key Agreement * +*************************************************/ +class PK_Key_Agreement + { + public: + SymmetricKey derive_key(u32bit, const byte[], u32bit, + const std::string& = "") const; + SymmetricKey derive_key(u32bit, const byte[], u32bit, + const byte[], u32bit) const; + + PK_Key_Agreement(const PK_Key_Agreement_Key&, const std::string&); + private: + const PK_Key_Agreement_Key& key; + const std::string kdf_name; + }; + +/************************************************* +* Encryption with an MR algorithm and an EME * +*************************************************/ +class PK_Encryptor_MR_with_EME : public PK_Encryptor + { + public: + u32bit maximum_input_size() const; + PK_Encryptor_MR_with_EME(const PK_Encrypting_Key&, const std::string&); + ~PK_Encryptor_MR_with_EME() { delete encoder; } + private: + SecureVector<byte> enc(const byte[], u32bit) const; + const PK_Encrypting_Key& key; + const EME* encoder; + }; + +/************************************************* +* Decryption with an MR algorithm and an EME * +*************************************************/ +class PK_Decryptor_MR_with_EME : public PK_Decryptor + { + public: + PK_Decryptor_MR_with_EME(const PK_Decrypting_Key&, const std::string&); + ~PK_Decryptor_MR_with_EME() { delete encoder; } + private: + SecureVector<byte> dec(const byte[], u32bit) const; + const PK_Decrypting_Key& key; + const EME* encoder; + }; + +/************************************************* +* Public Key Verifier with Message Recovery * +*************************************************/ +class PK_Verifier_with_MR : public PK_Verifier + { + public: + PK_Verifier_with_MR(const PK_Verifying_with_MR_Key&, const std::string&); + private: + bool validate_signature(const MemoryRegion<byte>&, const byte[], u32bit); + const PK_Verifying_with_MR_Key& key; + }; + +/************************************************* +* Public Key Verifier without Message Recovery * +*************************************************/ +class PK_Verifier_wo_MR : public PK_Verifier + { + public: + PK_Verifier_wo_MR(const PK_Verifying_wo_MR_Key&, const std::string&); + private: + bool validate_signature(const MemoryRegion<byte>&, const byte[], u32bit); + const PK_Verifying_wo_MR_Key& key; + }; + +} + +#endif diff --git a/include/randpool.h b/include/randpool.h new file mode 100644 index 000000000..bcde066a9 --- /dev/null +++ b/include/randpool.h @@ -0,0 +1,41 @@ +/************************************************* +* Randpool Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RANDPOOL_H__ +#define BOTAN_RANDPOOL_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Randpool * +*************************************************/ +class Randpool : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit) throw(PRNG_Unseeded); + bool is_seeded() const; + void clear() throw(); + std::string name() const; + + Randpool(); + ~Randpool(); + private: + void add_randomness(const byte[], u32bit); + void update_buffer(); + void mix_pool(); + + const u32bit ITERATIONS_BEFORE_RESEED, POOL_BLOCKS; + BlockCipher* cipher; + MessageAuthenticationCode* mac; + + SecureVector<byte> pool, buffer, counter; + u32bit entropy; + }; + +} + +#endif diff --git a/include/rc2.h b/include/rc2.h new file mode 100644 index 000000000..eea13a9a2 --- /dev/null +++ b/include/rc2.h @@ -0,0 +1,38 @@ +/************************************************* +* RC2 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RC2_H__ +#define BOTAN_RC2_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* RC2 * +*************************************************/ +class RC2 : public BlockCipher + { + public: + static byte EKB_code(u32bit); + + void clear() throw() { K.clear(); } + std::string name() const { return "RC2"; } + BlockCipher* clone() const { return new RC2; } + RC2() : BlockCipher(8, 1, 32) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + void mash(u16bit&, u16bit&, u16bit&, u16bit&) const; + void rmash(u16bit&, u16bit&, u16bit&, u16bit&) const; + void mix(u16bit&, u16bit&, u16bit&, u16bit&, u32bit) const; + void rmix(u16bit&, u16bit&, u16bit&, u16bit&, u32bit) const; + SecureBuffer<u16bit, 64> K; + }; + +} + +#endif diff --git a/include/rc5.h b/include/rc5.h new file mode 100644 index 000000000..9c2479edf --- /dev/null +++ b/include/rc5.h @@ -0,0 +1,33 @@ +/************************************************* +* RC5 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RC5_H__ +#define BOTAN_RC5_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* RC5 * +*************************************************/ +class RC5 : public BlockCipher + { + public: + void clear() throw() { S.clear(); } + std::string name() const; + BlockCipher* clone() const { return new RC5(ROUNDS); } + RC5(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + SecureVector<u32bit> S; + const u32bit ROUNDS; + }; + +} + +#endif diff --git a/include/rc6.h b/include/rc6.h new file mode 100644 index 000000000..fd21adc94 --- /dev/null +++ b/include/rc6.h @@ -0,0 +1,33 @@ +/************************************************* +* RC6 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RC6_H__ +#define BOTAN_RC6_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* RC6 * +*************************************************/ +class RC6 : public BlockCipher + { + public: + void clear() throw() { S.clear(); } + std::string name() const { return "RC6"; } + BlockCipher* clone() const { return new RC6; } + RC6() : BlockCipher(16, 1, 32) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + SecureBuffer<u32bit, 44> S; + }; + +} + +#endif diff --git a/include/reducer.h b/include/reducer.h new file mode 100644 index 000000000..537254bb7 --- /dev/null +++ b/include/reducer.h @@ -0,0 +1,34 @@ +/************************************************* +* Modular Reducer Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_MODARITH_H__ +#define BOTAN_MODARITH_H__ + +#include <botan/bigint.h> + +namespace Botan { + +/************************************************* +* Modular Reducer * +*************************************************/ +class Modular_Reducer + { + public: + BigInt multiply(const BigInt&, const BigInt&) const; + BigInt square(const BigInt&) const; + BigInt reduce(const BigInt&) const; + + bool initialized() const { return (mod_words != 0); } + + Modular_Reducer() { mod_words = 0; } + Modular_Reducer(const BigInt&); + private: + BigInt modulus, modulus_2, mu; + u32bit mod_words, mod2_words, mu_words; + }; + +} + +#endif diff --git a/include/rmd128.h b/include/rmd128.h new file mode 100644 index 000000000..7dbf2ea8a --- /dev/null +++ b/include/rmd128.h @@ -0,0 +1,33 @@ +/************************************************* +* RIPEMD-128 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RIPEMD_128_H__ +#define BOTAN_RIPEMD_128_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* RIPEMD-128 * +*************************************************/ +class RIPEMD_128 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "RIPEMD-128"; } + HashFunction* clone() const { return new RIPEMD_128; } + RIPEMD_128() : MDx_HashFunction(16, 64, false, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 4> digest; + }; + +} + +#endif diff --git a/include/rmd160.h b/include/rmd160.h new file mode 100644 index 000000000..a099f7376 --- /dev/null +++ b/include/rmd160.h @@ -0,0 +1,33 @@ +/************************************************* +* RIPEMD-160 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RIPEMD_160_H__ +#define BOTAN_RIPEMD_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* RIPEMD-160 * +*************************************************/ +class RIPEMD_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "RIPEMD-160"; } + HashFunction* clone() const { return new RIPEMD_160; } + RIPEMD_160() : MDx_HashFunction(20, 64, false, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 16> M; + SecureBuffer<u32bit, 5> digest; + }; + +} + +#endif diff --git a/include/rng.h b/include/rng.h new file mode 100644 index 000000000..5c341dabd --- /dev/null +++ b/include/rng.h @@ -0,0 +1,32 @@ +/************************************************* +* Global RNG Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_GLOBAL_RNG_H__ +#define BOTAN_GLOBAL_RNG_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* RNG Access and Seeding Functions * +*************************************************/ +namespace Global_RNG { + +void randomize(byte[], u32bit); +byte random(); + +void add_entropy(const byte[], u32bit); +void add_entropy(EntropySource&, bool = true); + +u32bit seed(bool = true, u32bit = 256); + +void add_es(EntropySource*, bool = true); + +} + +} + +#endif diff --git a/include/rsa.h b/include/rsa.h new file mode 100644 index 000000000..aff873c54 --- /dev/null +++ b/include/rsa.h @@ -0,0 +1,58 @@ +/************************************************* +* RSA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RSA_H__ +#define BOTAN_RSA_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/************************************************* +* RSA Public Key * +*************************************************/ +class RSA_PublicKey : public PK_Encrypting_Key, + public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + SecureVector<byte> encrypt(const byte[], u32bit) const; + SecureVector<byte> verify(const byte[], u32bit) const; + + RSA_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + std::string algo_name() const { return "RSA"; } + RSA_PublicKey() {} + private: + friend X509_PublicKey* get_public_key(const std::string&); + }; + +/************************************************* +* RSA Private Key * +*************************************************/ +class RSA_PrivateKey : public RSA_PublicKey, + public PK_Decrypting_Key, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> decrypt(const byte[], u32bit) const; + SecureVector<byte> sign(const byte[], u32bit) const; + + bool check_key(bool) const; + + RSA_PrivateKey(const BigInt&, const BigInt&, const BigInt&, + const BigInt& = 0, const BigInt& = 0); + RSA_PrivateKey(u32bit, u32bit = 65537); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + BigInt private_op(const byte[], u32bit) const; + RSA_PrivateKey() {} + }; + +} + +#endif diff --git a/include/rw.h b/include/rw.h new file mode 100644 index 000000000..f2c8729bc --- /dev/null +++ b/include/rw.h @@ -0,0 +1,53 @@ +/************************************************* +* Rabin-Williams Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_RW_H__ +#define BOTAN_RW_H__ + +#include <botan/if_algo.h> + +namespace Botan { + +/************************************************* +* Rabin-Williams Public Key * +*************************************************/ +class RW_PublicKey : public PK_Verifying_with_MR_Key, + public virtual IF_Scheme_PublicKey + { + public: + SecureVector<byte> verify(const byte[], u32bit) const; + + RW_PublicKey(const BigInt&, const BigInt&); + protected: + BigInt public_op(const BigInt&) const; + std::string algo_name() const { return "RW"; } + RW_PublicKey() {} + private: + friend X509_PublicKey* get_public_key(const std::string&); + }; + +/************************************************* +* Rabin-Williams Private Key * +*************************************************/ +class RW_PrivateKey : public RW_PublicKey, + public PK_Signing_Key, + public IF_Scheme_PrivateKey + { + public: + SecureVector<byte> sign(const byte[], u32bit) const; + + bool check_key(bool) const; + + RW_PrivateKey(const BigInt&, const BigInt&, const BigInt&, + const BigInt& = 0, const BigInt& = 0); + RW_PrivateKey(u32bit, u32bit = 2); + private: + friend PKCS8_PrivateKey* get_private_key(const std::string&); + RW_PrivateKey() {} + }; + +} + +#endif diff --git a/include/s2k.h b/include/s2k.h new file mode 100644 index 000000000..0b1eb0b50 --- /dev/null +++ b/include/s2k.h @@ -0,0 +1,42 @@ +/************************************************* +* S2K Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_S2K_H__ +#define BOTAN_S2K_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* S2K Interface * +*************************************************/ +class S2K : public Algorithm + { + public: + virtual S2K* clone() const = 0; + + OctetString derive_key(u32bit, const std::string&) const; + + void set_iterations(u32bit); + void change_salt(const byte[], u32bit); + void change_salt(const MemoryRegion<byte>&); + void new_random_salt(u32bit); + + u32bit iterations() const { return iter; } + SecureVector<byte> current_salt() const { return salt; } + + S2K() { iter = 0; } + virtual ~S2K() {} + private: + virtual OctetString derive(u32bit, const std::string&, + const byte[], u32bit, u32bit) const = 0; + SecureVector<byte> salt; + u32bit iter; + }; + +} + +#endif diff --git a/include/safer_sk.h b/include/safer_sk.h new file mode 100644 index 000000000..3b73eeebb --- /dev/null +++ b/include/safer_sk.h @@ -0,0 +1,34 @@ +/************************************************* +* SAFER-SK Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SAFER_SK_H__ +#define BOTAN_SAFER_SK_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* SAFER-SK * +*************************************************/ +class SAFER_SK : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const; + BlockCipher* clone() const; + SAFER_SK(u32bit); + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + static const byte EXP[256], LOG[512], BIAS[208], KEY_INDEX[208]; + SecureVector<byte> EK; + const u32bit ROUNDS; + }; + +} + +#endif diff --git a/include/secmem.h b/include/secmem.h new file mode 100644 index 000000000..84c821ec7 --- /dev/null +++ b/include/secmem.h @@ -0,0 +1,213 @@ +/************************************************* +* Secure Memory Buffers Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__ +#define BOTAN_SECURE_MEMORY_BUFFERS_H__ + +#include <botan/allocate.h> +#include <botan/mem_ops.h> + +namespace Botan { + +/************************************************* +* Variable Length Memory Buffer * +*************************************************/ +template<typename T> +class MemoryRegion + { + public: + u32bit size() const { return used; } + u32bit is_empty() const { return (used == 0); } + u32bit has_items() const { return (used != 0); } + + operator T* () { return buf; } + operator const T* () const { return buf; } + + T* begin() { return buf; } + const T* begin() const { return buf; } + + T* end() { return (buf + size()); } + const T* end() const { return (buf + size()); } + + bool operator==(const MemoryRegion<T>& other) const + { + return (size() == other.size() && + same_mem(buf, other.buf, size())); + } + + bool operator<(const MemoryRegion<T>&) const; + + bool operator!=(const MemoryRegion<T>& in) const + { return (!(*this == in)); } + MemoryRegion<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + void copy(const T in[], u32bit n) + { copy(0, in, n); } + void copy(u32bit off, const T in[], u32bit n) + { copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); } + + void set(const T in[], u32bit n) { create(n); copy(in, n); } + void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); } + + void append(const T data[], u32bit n) + { grow_to(size()+n); copy(size() - n, data, n); } + void append(T x) { append(&x, 1); } + void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); } + + void clear() { clear_mem(buf, allocated); } + void destroy() { create(0); } + + void create(u32bit); + void grow_to(u32bit) const; + void swap(MemoryRegion<T>&); + + ~MemoryRegion() { deallocate(buf, allocated); } + protected: + MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; } + MemoryRegion(const MemoryRegion<T>& copy) + { + buf = 0; + used = allocated = 0; + alloc = copy.alloc; + set(copy.buf, copy.used); + } + + void init(bool lock, u32bit size = 0) + { alloc = get_allocator(lock ? "" : "malloc"); create(size); } + private: + T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); } + void deallocate(T* p, u32bit n) const + { alloc->deallocate(p, sizeof(T)*n); } + + mutable T* buf; + mutable u32bit used; + mutable u32bit allocated; + mutable Allocator* alloc; + }; + +/************************************************* +* Create a new buffer * +*************************************************/ +template<typename T> +void MemoryRegion<T>::create(u32bit n) + { + if(n <= allocated) { clear(); used = n; return; } + deallocate(buf, allocated); + buf = allocate(n); + allocated = used = n; + } + +/************************************************* +* Increase the size of the buffer * +*************************************************/ +template<typename T> +void MemoryRegion<T>::grow_to(u32bit n) const + { + if(n > used && n <= allocated) + { + clear_mem(buf + used, n - used); + used = n; + return; + } + else if(n > allocated) + { + T* new_buf = allocate(n); + copy_mem(new_buf, buf, used); + deallocate(buf, allocated); + buf = new_buf; + allocated = used = n; + } + } + +/************************************************* +* Compare this buffer with another one * +*************************************************/ +template<typename T> +bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const + { + if(size() < in.size()) return true; + if(size() > in.size()) return false; + + for(u32bit j = 0; j != size(); j++) + { + if(buf[j] < in[j]) return true; + if(buf[j] > in[j]) return false; + } + + return false; + } + +/************************************************* +* Swap this buffer with another one * +*************************************************/ +template<typename T> +void MemoryRegion<T>::swap(MemoryRegion<T>& x) + { + std::swap(buf, x.buf); + std::swap(used, x.used); + std::swap(allocated, x.allocated); + std::swap(alloc, x.alloc); + } + +/************************************************* +* Unlocked Variable Length Buffer * +*************************************************/ +template<typename T> +class MemoryVector : public MemoryRegion<T> + { + public: + MemoryVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); } + MemoryVector(const T in[], u32bit n) + { MemoryRegion<T>::init(false); set(in, n); } + MemoryVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(false); set(in); } + MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(false); set(in1); append(in2); } + }; + +/************************************************* +* Locked Variable Length Buffer * +*************************************************/ +template<typename T> +class SecureVector : public MemoryRegion<T> + { + public: + SecureVector<T>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + + SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); } + SecureVector(const T in[], u32bit n) + { MemoryRegion<T>::init(true); set(in, n); } + SecureVector(const MemoryRegion<T>& in) + { MemoryRegion<T>::init(true); set(in); } + SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2) + { MemoryRegion<T>::init(true); set(in1); append(in2); } + }; + +/************************************************* +* Locked Fixed Length Buffer * +*************************************************/ +template<typename T, u32bit L> +class SecureBuffer : public MemoryRegion<T> + { + public: + SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in) + { if(this != &in) set(in); return (*this); } + + SecureBuffer() { MemoryRegion<T>::init(true, L); } + SecureBuffer(const T in[], u32bit n) + { MemoryRegion<T>::init(true, L); copy(in, n); } + private: + SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in) + { if(this != &in) set(in); return (*this); } + }; + +} + +#endif diff --git a/include/secqueue.h b/include/secqueue.h new file mode 100644 index 000000000..91395d1c1 --- /dev/null +++ b/include/secqueue.h @@ -0,0 +1,41 @@ +/************************************************* +* SecureQueue Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SECURE_QUEUE_H__ +#define BOTAN_SECURE_QUEUE_H__ + +#include <botan/data_src.h> +#include <botan/filter.h> + +namespace Botan { + +/************************************************* +* SecureQueue * +*************************************************/ +class SecureQueue : public Fanout_Filter, public DataSource + { + public: + void write(const byte[], u32bit); + + u32bit read(byte[], u32bit); + u32bit peek(byte[], u32bit, u32bit = 0) const; + + bool end_of_data() const; + u32bit size() const; + bool attachable() { return false; } + + SecureQueue& operator=(const SecureQueue&); + SecureQueue(); + SecureQueue(const SecureQueue&); + ~SecureQueue() { destroy(); } + private: + void destroy(); + class SecureQueueNode* head; + class SecureQueueNode* tail; + }; + +} + +#endif diff --git a/include/seed.h b/include/seed.h new file mode 100644 index 000000000..0e730864e --- /dev/null +++ b/include/seed.h @@ -0,0 +1,41 @@ +/************************************************* +* SEED Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SEED_H__ +#define BOTAN_SEED_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* SEED * +*************************************************/ +class SEED : public BlockCipher + { + public: + void clear() throw() { K.clear(); } + std::string name() const { return "SEED"; } + BlockCipher* clone() const { return new SEED; } + SEED() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + class G_FUNC + { + public: + u32bit operator()(u32bit) const; + private: + static const u32bit S0[256], S1[256], S2[256], S3[256]; + }; + + SecureBuffer<u32bit, 32> K; + }; + +} + +#endif diff --git a/include/serpent.h b/include/serpent.h new file mode 100644 index 000000000..c1722464e --- /dev/null +++ b/include/serpent.h @@ -0,0 +1,34 @@ +/************************************************* +* Serpent Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SERPENT_H__ +#define BOTAN_SERPENT_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Serpent * +*************************************************/ +class Serpent : public BlockCipher + { + public: + void clear() throw() { round_key.clear(); } + std::string name() const { return "Serpent"; } + BlockCipher* clone() const { return new Serpent; } + Serpent() : BlockCipher(16, 16, 32, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + void key_xor(u32bit, u32bit&, u32bit&, u32bit&, u32bit&) const; + SecureBuffer<u32bit, 132> round_key; + }; + +} + +#endif diff --git a/include/sha160.h b/include/sha160.h new file mode 100644 index 000000000..dfe9f370d --- /dev/null +++ b/include/sha160.h @@ -0,0 +1,36 @@ +/************************************************* +* SHA-160 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SHA_160_H__ +#define BOTAN_SHA_160_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* SHA-160 * +*************************************************/ +class SHA_160 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "SHA-160"; } + HashFunction* clone() const { return new SHA_160; } + SHA_160() : MDx_HashFunction(20, 64, true, true) { clear(); } + private: + friend class Gamma; + friend class FIPS_186_RNG; + + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 5> digest; + SecureBuffer<u32bit, 80> W; + }; + +} + +#endif diff --git a/include/sha256.h b/include/sha256.h new file mode 100644 index 000000000..d0ddeff42 --- /dev/null +++ b/include/sha256.h @@ -0,0 +1,33 @@ +/************************************************* +* SHA-256 Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SHA_256_H__ +#define BOTAN_SHA_256_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* SHA-256 * +*************************************************/ +class SHA_256 : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "SHA-256"; } + HashFunction* clone() const { return new SHA_256; } + SHA_256() : MDx_HashFunction(32, 64, true, true) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u32bit, 64> W; + SecureBuffer<u32bit, 8> digest; + }; + +} + +#endif diff --git a/include/sha_64.h b/include/sha_64.h new file mode 100644 index 000000000..3ef999220 --- /dev/null +++ b/include/sha_64.h @@ -0,0 +1,55 @@ +/************************************************* +* SHA-{384,512} Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SHA_64BIT_H__ +#define BOTAN_SHA_64BIT_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* SHA-{384,512} Base * +*************************************************/ +class SHA_64_BASE : public MDx_HashFunction + { + protected: + void clear() throw(); + SHA_64_BASE(u32bit out) : MDx_HashFunction(out, 128, true, true, 16) {} + SecureBuffer<u64bit, 8> digest; + private: + void hash(const byte[]); + void copy_out(byte[]); + + SecureBuffer<u64bit, 80> W; + }; + +/************************************************* +* SHA-384 * +*************************************************/ +class SHA_384 : public SHA_64_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-384"; } + HashFunction* clone() const { return new SHA_384; } + SHA_384() : SHA_64_BASE(48) { clear(); } + }; + +/************************************************* +* SHA-512 * +*************************************************/ +class SHA_512 : public SHA_64_BASE + { + public: + void clear() throw(); + std::string name() const { return "SHA-512"; } + HashFunction* clone() const { return new SHA_512; } + SHA_512() : SHA_64_BASE(64) { clear(); } + }; + +} + +#endif diff --git a/include/skipjack.h b/include/skipjack.h new file mode 100644 index 000000000..533b71f7a --- /dev/null +++ b/include/skipjack.h @@ -0,0 +1,36 @@ +/************************************************* +* Skipjack Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SKIPJACK_H__ +#define BOTAN_SKIPJACK_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Skipjack * +*************************************************/ +class Skipjack : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Skipjack"; } + BlockCipher* clone() const { return new Skipjack; } + Skipjack() : BlockCipher(8, 10) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + void step_A(u16bit&, u16bit&, u32bit) const; + void step_B(u16bit&, u16bit&, u32bit) const; + void step_Ai(u16bit&, u16bit&, u32bit) const; + void step_Bi(u16bit&, u16bit&, u32bit) const; + SecureBuffer<byte, 256> FTABLE[10]; + }; + +} + +#endif diff --git a/include/square.h b/include/square.h new file mode 100644 index 000000000..8f029f691 --- /dev/null +++ b/include/square.h @@ -0,0 +1,38 @@ +/************************************************* +* Square Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SQUARE_H__ +#define BOTAN_SQUARE_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Square * +*************************************************/ +class Square : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Square"; } + BlockCipher* clone() const { return new Square; } + Square() : BlockCipher(16, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + static void transform(u32bit[4]); + static byte mul(byte, byte); + static const byte SE[256], SD[256], Log[256], ALog[255]; + static const u32bit TE0[256], TE1[256], TE2[256], TE3[256], + TD0[256], TD1[256], TD2[256], TD3[256]; + SecureBuffer<u32bit, 28> EK, DK; + SecureBuffer<byte, 32> ME, MD; + }; + +} + +#endif diff --git a/include/stl_util.h b/include/stl_util.h new file mode 100644 index 000000000..5ecbe0d9c --- /dev/null +++ b/include/stl_util.h @@ -0,0 +1,65 @@ +/************************************************* +* STL Utility Functions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_STL_UTIL_H__ +#define BOTAN_STL_UTIL_H__ + +#include <map> + +namespace Botan { + +/************************************************* +* Copy-on-Predicate Algorithm * +*************************************************/ +template <typename InputIterator, typename OutputIterator, typename Predicate> +OutputIterator copy_if(InputIterator current, InputIterator end, + OutputIterator dest, Predicate copy_p) + { + while(current != end) + { + if(copy_p(*current)) + *dest++ = *current; + ++current; + } + return dest; + } + +/************************************************* +* Searching through a std::map * +*************************************************/ +template<typename K, typename V> +inline V search_map(const std::map<K, V>& mapping, + const K& key, + const V& null_result = V()) + { + typename std::map<K, V>::const_iterator i = mapping.find(key); + if(i == mapping.end()) + return null_result; + return i->second; + } + +template<typename K, typename V, typename R> +inline R search_map(const std::map<K, V>& mapping, const K& key, + const R& null_result, const R& found_result) + { + typename std::map<K, V>::const_iterator i = mapping.find(key); + if(i == mapping.end()) + return null_result; + return found_result; + } + +/************************************************* +* Insert a key/value pair into a multimap * +*************************************************/ +template<typename K, typename V> +void multimap_insert(std::multimap<K, V>& multimap, + const K& key, const V& value) + { + multimap.insert(std::make_pair(key, value)); + } + +} + +#endif diff --git a/include/symkey.h b/include/symkey.h new file mode 100644 index 000000000..c3b5789d5 --- /dev/null +++ b/include/symkey.h @@ -0,0 +1,81 @@ +/************************************************* +* OctetString Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_SYMKEY_H__ +#define BOTAN_SYMKEY_H__ + +#include <botan/secmem.h> +#include <botan/enums.h> +#include <string> + +namespace Botan { + +/************************************************* +* Octet String * +*************************************************/ +class 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(u32bit); + void change(const std::string&); + void change(const byte[], u32bit); + void change(const MemoryRegion<byte>& in) { bits = in; } + + 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 * +*************************************************/ +bool operator==(const OctetString&, const OctetString&); +bool operator!=(const OctetString&, const OctetString&); +OctetString operator+(const OctetString&, const OctetString&); +OctetString operator^(const OctetString&, const OctetString&); + +/************************************************* +* Symmetric Key * +*************************************************/ +class SymmetricKey : public OctetString + { + public: + SymmetricKey(u32bit len) { change(len); } + SymmetricKey(const std::string& str = "") : OctetString(str) {} + SymmetricKey(const byte in[], u32bit l) : OctetString(in, l) {} + SymmetricKey(const MemoryRegion<byte>& in) : OctetString(in) {} + SymmetricKey(const OctetString& os) : OctetString(os) {} + }; + +/************************************************* +* Initialization Vector * +*************************************************/ +class InitializationVector : public OctetString + { + public: + InitializationVector(u32bit len) { change(len); } + InitializationVector(const std::string& str = "") : OctetString(str) {} + InitializationVector(const byte in[], u32bit l) : OctetString(in, l) {} + InitializationVector(const MemoryRegion<byte>& in) : OctetString(in) {} + InitializationVector(const OctetString& os) : OctetString(os) {} + }; + +} + +#endif diff --git a/include/tea.h b/include/tea.h new file mode 100644 index 000000000..5d517bbdf --- /dev/null +++ b/include/tea.h @@ -0,0 +1,32 @@ +/************************************************* +* TEA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TEA_H__ +#define BOTAN_TEA_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* TEA * +*************************************************/ +class TEA : public BlockCipher + { + public: + void clear() throw() { K.clear(); } + std::string name() const { return "TEA"; } + BlockCipher* clone() const { return new TEA; } + TEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + SecureBuffer<u32bit, 4> K; + }; + +} + +#endif diff --git a/include/tiger.h b/include/tiger.h new file mode 100644 index 000000000..26837e20e --- /dev/null +++ b/include/tiger.h @@ -0,0 +1,38 @@ +/************************************************* +* Tiger Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TIGER_H__ +#define BOTAN_TIGER_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* Tiger * +*************************************************/ +class Tiger : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const; + HashFunction* clone() const { return new Tiger(OUTPUT_LENGTH); } + Tiger(u32bit = 24, u32bit = 3); + private: + void hash(const byte[]); + void copy_out(byte[]); + + static void pass(u64bit&, u64bit&, u64bit&, u64bit[8], byte); + static void mix(u64bit[8]); + static void round(u64bit&, u64bit&, u64bit&, u64bit, byte); + static const u64bit SBOX1[256], SBOX2[256], SBOX3[256], SBOX4[256]; + SecureBuffer<u64bit, 8> X; + SecureBuffer<u64bit, 3> digest; + const u32bit PASS; + }; + +} + +#endif diff --git a/include/timers.h b/include/timers.h new file mode 100644 index 000000000..d639d63f5 --- /dev/null +++ b/include/timers.h @@ -0,0 +1,25 @@ +/************************************************* +* Timestamp Functions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TIMERS_H__ +#define BOTAN_TIMERS_H__ + +#include <botan/types.h> + +namespace Botan { + +/************************************************* +* Timer Interface * +*************************************************/ +class Timer + { + public: + virtual u64bit clock() const; + virtual ~Timer() {} + }; + +} + +#endif diff --git a/include/turing.h b/include/turing.h new file mode 100644 index 000000000..68bdbd847 --- /dev/null +++ b/include/turing.h @@ -0,0 +1,45 @@ +/************************************************* +* Turing Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TURING_H__ +#define BOTAN_TURING_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Turing * +*************************************************/ +class Turing : public StreamCipher + { + public: + void clear() throw(); + std::string name() const { return "Turing"; } + StreamCipher* clone() const { return new Turing; } + Turing() : StreamCipher(4, 32, 4) { position = 0; } + private: + void cipher(const byte[], byte[], u32bit); + void key(const byte[], u32bit); + void resync(const byte[], u32bit); + void generate(); + + static u32bit fixedS(u32bit); + static void gen_sbox(MemoryRegion<u32bit>&, u32bit, + const MemoryRegion<u32bit>&); + + static const u32bit Q_BOX[256]; + static const byte SBOX[256], OFFSETS[272]; + + SecureBuffer<u32bit, 256> S0, S1, S2, S3; + SecureBuffer<u32bit, 17> R; + SecureVector<u32bit> K; + SecureBuffer<byte, 340> buffer; + u32bit position; + }; + +} + +#endif diff --git a/include/twofish.h b/include/twofish.h new file mode 100644 index 000000000..3f1602ef5 --- /dev/null +++ b/include/twofish.h @@ -0,0 +1,40 @@ +/************************************************* +* Twofish Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TWOFISH_H__ +#define BOTAN_TWOFISH_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* Twofish * +*************************************************/ +class Twofish : public BlockCipher + { + public: + void clear() throw(); + std::string name() const { return "Twofish"; } + BlockCipher* clone() const { return new Twofish; } + Twofish() : BlockCipher(16, 16, 32, 8) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + + static void rs_mul(byte[4], byte, u32bit); + + static const u32bit MDS0[256], MDS1[256], MDS2[256], MDS3[256]; + static const byte Q0[256], Q1[256], RS[32]; + static const byte EXP_TO_POLY[255], POLY_TO_EXP[255]; + + SecureBuffer<u32bit, 256> SBox0, SBox1, SBox2, SBox3; + SecureBuffer<u32bit, 40> round_key; + }; + +} + +#endif diff --git a/include/types.h b/include/types.h new file mode 100644 index 000000000..395727b39 --- /dev/null +++ b/include/types.h @@ -0,0 +1,38 @@ +/************************************************* +* Low Level Types Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_TYPES_H__ +#define BOTAN_TYPES_H__ + +#include <botan/config.h> + +namespace Botan { + +typedef unsigned char byte; +typedef unsigned short u16bit; +typedef unsigned int u32bit; + +typedef signed int s32bit; + +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef unsigned __int64 u64bit; +#elif defined(__KCC) + typedef unsigned __long_long u64bit; +#elif defined(__GNUG__) + __extension__ typedef unsigned long long u64bit; +#else + typedef unsigned long long u64bit; +#endif + +} + +namespace Botan_types { + +typedef Botan::byte byte; +typedef Botan::u32bit u32bit; + +} + +#endif diff --git a/include/ui.h b/include/ui.h new file mode 100644 index 000000000..a956518d6 --- /dev/null +++ b/include/ui.h @@ -0,0 +1,63 @@ +/************************************************* +* User Interface Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_UI_H__ +#define BOTAN_UI_H__ + +#include <string> + +namespace Botan { + +/************************************************* +* User Interface * +*************************************************/ +class User_Interface + { + public: + enum UI_Result { OK, CANCEL_ACTION }; + + virtual std::string get_passphrase(const std::string&, + const std::string&, + UI_Result&) const; + User_Interface(const std::string& = ""); + virtual ~User_Interface() {} + protected: + const std::string preset_passphrase; + mutable bool first_try; + }; + +namespace UI { + +/************************************************* +* Pulse Function * +*************************************************/ +enum Pulse_Type { + GENERAL_PULSE, + + PIPE_WRITE, + + PRIME_SEARCHING, + PRIME_SIEVING, + PRIME_PASSED_SIEVE, + PRIME_TESTING, + PRIME_FOUND +}; +typedef void (*pulse_func)(Pulse_Type, void*); + +/************************************************* +* Set the UI pulse function * +*************************************************/ +void set_pulse(pulse_func, void* = 0); + +/************************************************* +* Call the UI pulse function * +*************************************************/ +void pulse(Pulse_Type = GENERAL_PULSE); + +} + +} + +#endif diff --git a/include/util.h b/include/util.h new file mode 100644 index 000000000..8e25db50c --- /dev/null +++ b/include/util.h @@ -0,0 +1,40 @@ +/************************************************* +* Utility Functions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_UTIL_H__ +#define BOTAN_UTIL_H__ + +#include <botan/types.h> + +namespace Botan { + +/************************************************* +* Timer Access Functions * +*************************************************/ +u64bit system_time(); +u64bit system_clock(); + +/************************************************* +* Memory Locking Functions * +*************************************************/ +void lock_mem(void*, u32bit); +void unlock_mem(void*, u32bit); + +/************************************************* +* Misc Utility Functions * +*************************************************/ +u32bit round_up(u32bit, u32bit); +u32bit round_down(u32bit, u32bit); +u64bit combine_timers(u32bit, u32bit, u32bit); + +/************************************************* +* Work Factor Estimates * +*************************************************/ +u32bit entropy_estimate(const byte[], u32bit); +u32bit dl_work_factor(u32bit); + +} + +#endif diff --git a/include/version.h b/include/version.h new file mode 100644 index 000000000..3fe1323bb --- /dev/null +++ b/include/version.h @@ -0,0 +1,33 @@ +/************************************************* +* Version Information Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_VERSION_H__ +#define BOTAN_VERSION_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/************************************************* +* Get information describing the version * +*************************************************/ +std::string version_string(); +u32bit version_major(); +u32bit version_minor(); +u32bit version_patch(); + +/************************************************* +* Macros for compile-time version checks * +*************************************************/ +#define BOTAN_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c)) + +#define BOTAN_VERSION_CODE BOTAN_VERSION_CODE_FOR(BOTAN_VERSION_MAJOR, \ + BOTAN_VERSION_MINOR, \ + BOTAN_VERSION_PATCH) + +} + +#endif diff --git a/include/whrlpool.h b/include/whrlpool.h new file mode 100644 index 000000000..0bcbef4ac --- /dev/null +++ b/include/whrlpool.h @@ -0,0 +1,34 @@ +/************************************************* +* Whirlpool Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_WHIRLPOOL_H__ +#define BOTAN_WHIRLPOOL_H__ + +#include <botan/mdx_hash.h> + +namespace Botan { + +/************************************************* +* Whirlpool * +*************************************************/ +class Whirlpool : public MDx_HashFunction + { + public: + void clear() throw(); + std::string name() const { return "Whirlpool"; } + HashFunction* clone() const { return new Whirlpool; } + Whirlpool() : MDx_HashFunction(64, 64, true, true, 32) { clear(); } + private: + void hash(const byte[]); + void copy_out(byte[]); + + static const u64bit C0[256], C1[256], C2[256], C3[256], + C4[256], C5[256], C6[256], C7[256]; + SecureBuffer<u64bit, 8> M, digest; + }; + +} + +#endif diff --git a/include/wid_wake.h b/include/wid_wake.h new file mode 100644 index 000000000..533404837 --- /dev/null +++ b/include/wid_wake.h @@ -0,0 +1,37 @@ +/************************************************* +* WiderWake Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_WIDER_WAKE_H__ +#define BOTAN_WIDER_WAKE_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* WiderWake4+1-BE * +*************************************************/ +class WiderWake_41_BE : public StreamCipher + { + public: + void clear() throw(); + std::string name() const { return "WiderWake4+1-BE"; } + StreamCipher* clone() const { return new WiderWake_41_BE; } + WiderWake_41_BE() : StreamCipher(16, 16, 1, 8) {} + private: + void cipher(const byte[], byte[], u32bit); + void key(const byte[], u32bit); + void resync(const byte[], u32bit); + void generate(u32bit); + SecureBuffer<byte, 1024> buffer; + SecureBuffer<u32bit, 256> T; + SecureBuffer<u32bit, 5> state; + SecureBuffer<u32bit, 4> t_key; + u32bit position; + }; + +} + +#endif diff --git a/include/x509_ca.h b/include/x509_ca.h new file mode 100644 index 000000000..75e8ac04b --- /dev/null +++ b/include/x509_ca.h @@ -0,0 +1,56 @@ +/************************************************* +* X.509 Certificate Authority Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_CA_H__ +#define BOTAN_X509_CA_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> +#include <botan/pubkey.h> + +namespace Botan { + +/************************************************* +* X.509 Certificate Authority * +*************************************************/ +class X509_CA + { + public: + X509_Certificate sign_request(const PKCS10_Request&, u32bit = 0) const; + + X509_Certificate ca_certificate() const; + + X509_CRL new_crl(u32bit = 0) const; + X509_CRL update_crl(const X509_CRL&, const std::vector<CRL_Entry>&, + u32bit = 0) const; + + static X509_Certificate make_cert(PK_Signer*, const AlgorithmIdentifier&, + const MemoryRegion<byte>&, + const MemoryRegion<byte>&, + const X509_Time&, const X509_Time&, + const X509_DN&, const X509_DN&, + bool, u32bit, const AlternativeName&, + const AlternativeName&, + Key_Constraints, + const std::vector<OID>&); + + X509_CA(const X509_Certificate&, const PKCS8_PrivateKey&); + ~X509_CA(); + private: + X509_CA(const X509_CA&) {} + X509_CA& operator=(const X509_CA&) { return (*this); } + + X509_CRL make_crl(const std::vector<CRL_Entry>&, u32bit, u32bit) const; + + AlgorithmIdentifier ca_sig_algo; + X509_Certificate cert; + PK_Signer* signer; + }; + +} + +#endif diff --git a/include/x509_crl.h b/include/x509_crl.h new file mode 100644 index 000000000..7d8a3e3cb --- /dev/null +++ b/include/x509_crl.h @@ -0,0 +1,51 @@ +/************************************************* +* X.509 CRL Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_CRL_H__ +#define BOTAN_X509_CRL_H__ + +#include <botan/x509_obj.h> +#include <botan/crl_ent.h> +#include <vector> + +namespace Botan { + +/************************************************* +* X.509 CRL * +*************************************************/ +class X509_CRL : public X509_Object + { + public: + struct X509_CRL_Error : public Exception + { + X509_CRL_Error(const std::string& error) : + Exception("X509_CRL: " + error) {} + }; + + std::vector<CRL_Entry> get_revoked() const; + + X509_DN issuer_dn() const; + MemoryVector<byte> authority_key_id() const; + + u32bit crl_number() const; + X509_Time this_update() const; + X509_Time next_update() const; + + void force_decode(); + + X509_CRL(DataSource&); + X509_CRL(const std::string&); + private: + void handle_crl_extension(const Extension&); + std::vector<CRL_Entry> revoked; + MemoryVector<byte> issuer_key_id; + X509_Time start, end; + X509_DN issuer; + u32bit version, crl_count; + }; + +} + +#endif diff --git a/include/x509_ext.h b/include/x509_ext.h new file mode 100644 index 000000000..7e3b68288 --- /dev/null +++ b/include/x509_ext.h @@ -0,0 +1,194 @@ +/************************************************* +* X.509 Certificate Extensions Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_EXTENSIONS__ +#define BOTAN_X509_EXTENSIONS__ + +#include <botan/asn1_int.h> +#include <botan/asn1_oid.h> +#include <botan/asn1_obj.h> + +namespace Botan { + +/************************************************* +* X.509 Certificate Extension * +*************************************************/ +class Certificate_Extension : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + void decode_from(class BER_Decoder&); + void maybe_add(class DER_Encoder&) const; + + OID oid_of() const; + void make_critical() { critical = true; } + bool is_critical() const { return critical; } + + //virtual std::multimap<std::string, std::string> contents() const = 0; + virtual std::string config_id() const = 0; + virtual std::string oid_name() const = 0; + + Certificate_Extension() { critical = false; } + virtual ~Certificate_Extension() {} + protected: + virtual bool should_encode() const { return true; } + virtual MemoryVector<byte> encode_inner() const = 0; + virtual void decode_inner(const MemoryRegion<byte>&) = 0; + private: + void encode_into(class DER_Encoder&, bool) const; + bool critical; + }; + +/************************************************* +* X.509 Certificate Extensions * +*************************************************/ +class Extensions : public ASN1_Object + { + public: + void encode_into(class DER_Encoder&) const; + + void add(Certificate_Extension* extn) + { extensions.push_back(extn); } + + ~Extensions(); + private: + std::vector<Certificate_Extension*> extensions; + }; + +namespace Cert_Extension { + +/************************************************* +* Basic Constraints Extension * +*************************************************/ +class Basic_Constraints : public Certificate_Extension + { + public: + Basic_Constraints(bool = false, u32bit = 0); + private: + std::string config_id() const { return "basic_constraints"; } + std::string oid_name() const { return "X509v3.BasicConstraints"; } + + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + bool is_ca; + u32bit path_limit; + }; + +/************************************************* +* Key Usage Constraints Extension * +*************************************************/ +class Key_Usage : public Certificate_Extension + { + public: + Key_Usage(Key_Constraints); + private: + std::string config_id() const { return "key_usage"; } + std::string oid_name() const { return "X509v3.KeyUsage"; } + + bool should_encode() const { return (constraints != NO_CONSTRAINTS); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + Key_Constraints constraints; + }; + +/************************************************* +* Subject Key Identifier Extension * +*************************************************/ +class Subject_Key_ID : public Certificate_Extension + { + public: + Subject_Key_ID(const MemoryRegion<byte>&); + private: + std::string config_id() const { return "subject_key_id"; } + std::string oid_name() const { return "X509v3.SubjectKeyIdentifier"; } + + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + MemoryVector<byte> key_id; + }; + +/************************************************* +* Authority Key Identifier Extension * +*************************************************/ +class Authority_Key_ID : public Certificate_Extension + { + public: + Authority_Key_ID(const MemoryRegion<byte>&); + private: + std::string config_id() const { return "authority_key_id"; } + std::string oid_name() const { return "X509v3.AuthorityKeyIdentifier"; } + + bool should_encode() const { return (key_id.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + MemoryVector<byte> key_id; + }; + +/************************************************* +* Alternative Name Extension * +*************************************************/ +class Alternative_Name : public Certificate_Extension + { + public: + Alternative_Name(const AlternativeName&, + const std::string&, const std::string&); + private: + std::string config_id() const { return config_name_str; } + std::string oid_name() const { return oid_name_str; } + + bool should_encode() const { return alt_name.has_items(); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + std::string config_name_str, oid_name_str; + AlternativeName alt_name; + }; + +/************************************************* +* Extended Key Usage Extension * +*************************************************/ +class Extended_Key_Usage : public Certificate_Extension + { + public: + Extended_Key_Usage(const std::vector<OID>&); + private: + std::string config_id() const { return "extended_key_usage"; } + std::string oid_name() const { return "X509v3.ExtendedKeyUsage"; } + + bool should_encode() const { return (oids.size() > 0); } + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + std::vector<OID> oids; + }; + +/************************************************* +* CRL Number Extension * +*************************************************/ +class CRL_Number : public Certificate_Extension + { + public: + CRL_Number(u32bit = 0); + private: + std::string config_id() const { return "crl_number"; } + std::string oid_name() const { return "X509v3.CRLNumber"; } + + bool should_encode() const { return (crl_number != 0); } + + MemoryVector<byte> encode_inner() const; + void decode_inner(const MemoryRegion<byte>&); + + u32bit crl_number; + }; + +} + +} + +#endif diff --git a/include/x509_key.h b/include/x509_key.h new file mode 100644 index 000000000..b55ba69cc --- /dev/null +++ b/include/x509_key.h @@ -0,0 +1,48 @@ +/************************************************* +* X.509 Public Key Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_PUBLIC_KEY_H__ +#define BOTAN_X509_PUBLIC_KEY_H__ + +#include <botan/pipe.h> +#include <botan/pk_keys.h> + +namespace Botan { + +/************************************************* +* X.509 Public Key * +*************************************************/ +class X509_PublicKey : public virtual PK_Key + { + public: + u64bit key_id() const; + virtual MemoryVector<byte> DER_encode_pub() const = 0; + virtual MemoryVector<byte> DER_encode_params() const = 0; + virtual void BER_decode_pub(DataSource&) = 0; + virtual void BER_decode_params(DataSource&) = 0; + virtual ~X509_PublicKey() {} + }; + +namespace X509 { + +/************************************************* +* X.509 Public Key Encoding/Decoding * +*************************************************/ +void encode(const X509_PublicKey&, Pipe&, X509_Encoding = PEM); +std::string PEM_encode(const X509_PublicKey&); + +X509_PublicKey* load_key(DataSource&); +X509_PublicKey* load_key(const std::string&); +X509_PublicKey* load_key(const MemoryRegion<byte>&); + +X509_PublicKey* copy_key(const X509_PublicKey&); + +Key_Constraints find_constraints(const X509_PublicKey&, Key_Constraints); + +} + +} + +#endif diff --git a/include/x509_obj.h b/include/x509_obj.h new file mode 100644 index 000000000..18c1e574b --- /dev/null +++ b/include/x509_obj.h @@ -0,0 +1,47 @@ +/************************************************* +* X.509 SIGNED Object Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_OBJECT_H__ +#define BOTAN_X509_OBJECT_H__ + +#include <botan/asn1_obj.h> +#include <botan/pipe.h> +#include <vector> + +namespace Botan { + +/************************************************* +* Generic X.509 SIGNED Object * +*************************************************/ +class X509_Object + { + public: + SecureVector<byte> tbs_data() const; + SecureVector<byte> signature() const; + AlgorithmIdentifier signature_algorithm() const; + + void encode(Pipe&, X509_Encoding = PEM) const; + SecureVector<byte> BER_encode() const; + std::string PEM_encode() const; + + X509_Object(DataSource&, const std::string&); + X509_Object(const std::string&, const std::string&); + virtual ~X509_Object() {} + protected: + void do_decode(); + X509_Object() {} + AlgorithmIdentifier sig_algo; + SecureVector<byte> tbs_bits, sig; + private: + virtual void force_decode() = 0; + void init(DataSource&, const std::string&); + void decode_info(DataSource&); + std::vector<std::string> PEM_labels_allowed; + std::string PEM_label_pref; + }; + +} + +#endif diff --git a/include/x509cert.h b/include/x509cert.h new file mode 100644 index 000000000..60534c769 --- /dev/null +++ b/include/x509cert.h @@ -0,0 +1,70 @@ +/************************************************* +* X.509 Certificates Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_CERTS_H__ +#define BOTAN_X509_CERTS_H__ + +#include <botan/x509_obj.h> +#include <botan/x509_key.h> +#include <botan/datastor.h> +#include <map> + +namespace Botan { + +static const u32bit NO_CERT_PATH_LIMIT = 0xFFFFFFFF; + +/************************************************* +* X.509 Certificate * +*************************************************/ +class X509_Certificate : public X509_Object + { + public: + X509_PublicKey* subject_public_key() const; + + u32bit x509_version() const; + MemoryVector<byte> serial_number() const; + MemoryVector<byte> authority_key_id() const; + MemoryVector<byte> subject_key_id() const; + Key_Constraints constraints() const; + + std::string start_time() const; + std::string end_time() const; + + std::string subject_info(const std::string&) const; + std::string issuer_info(const std::string&) const; + X509_DN issuer_dn() const; + X509_DN subject_dn() const; + + bool self_signed() const; + bool is_CA_cert() const; + + u32bit path_limit() const; + std::vector<std::string> ex_constraints() const; + std::vector<std::string> policies() const; + + bool operator==(const X509_Certificate&) const; + + void force_decode(); + + X509_Certificate(DataSource&); + X509_Certificate(const std::string&); + private: + friend class X509_CA; + X509_Certificate() {} + void handle_v3_extension(const Extension&); + + Data_Store info; + std::multimap<std::string, std::string> subject, issuer; + bool is_ca; + }; + +/************************************************* +* X.509 Certificate Comparison * +*************************************************/ +bool operator!=(const X509_Certificate&, const X509_Certificate&); + +} + +#endif diff --git a/include/x509self.h b/include/x509self.h new file mode 100644 index 000000000..439535fce --- /dev/null +++ b/include/x509self.h @@ -0,0 +1,71 @@ +/************************************************* +* X.509 Self-Signed Certificate Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_SELF_H__ +#define BOTAN_X509_SELF_H__ + +#include <botan/x509cert.h> +#include <botan/pkcs8.h> +#include <botan/pkcs10.h> + +namespace Botan { + +/************************************************* +* Options for X.509 Certificates * +*************************************************/ +class X509_Cert_Options + { + public: + std::string common_name; + std::string country; + std::string organization; + std::string org_unit; + std::string locality; + std::string state; + std::string serial_number; + + std::string email, uri, dns, xmpp; + + std::string challenge; + + X509_Time start, end; + + bool is_CA; + u32bit path_limit; + Key_Constraints constraints; + std::vector<OID> ex_constraints; + + void sanity_check() const; + + void CA_key(u32bit = 8); + void not_before(const std::string&); + void not_after(const std::string&); + + void add_constraints(Key_Constraints); + void add_ex_constraint(const OID&); + void add_ex_constraint(const std::string&); + + X509_Cert_Options(const std::string& = ""); + }; + +namespace X509 { + +/************************************************* +* Create a self-signed X.509 certificate * +*************************************************/ +X509_Certificate create_self_signed_cert(const X509_Cert_Options&, + const PKCS8_PrivateKey&); + +/************************************************* +* Create a PKCS #10 certificate request * +*************************************************/ +PKCS10_Request create_cert_req(const X509_Cert_Options&, + const PKCS8_PrivateKey&); + +} + +} + +#endif diff --git a/include/x509stor.h b/include/x509stor.h new file mode 100644 index 000000000..74f3ca1c4 --- /dev/null +++ b/include/x509stor.h @@ -0,0 +1,122 @@ +/************************************************* +* X.509 Certificate Store Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_X509_CERT_STORE_H__ +#define BOTAN_X509_CERT_STORE_H__ + +#include <botan/x509cert.h> +#include <botan/x509_crl.h> +#include <botan/certstor.h> + +namespace Botan { + +/************************************************* +* X.509 Certificate Store * +*************************************************/ +class X509_Store + { + public: + class Search_Func + { + public: + virtual bool match(const X509_Certificate&) const = 0; + virtual ~Search_Func() {} + }; + + enum Cert_Usage { + ANY = 0x00, + TLS_SERVER = 0x01, + TLS_CLIENT = 0x02, + CODE_SIGNING = 0x04, + EMAIL_PROTECTION = 0x08, + TIME_STAMPING = 0x10, + CRL_SIGNING = 0x20 + }; + + X509_Code validate_cert(const X509_Certificate&, Cert_Usage = ANY); + + std::vector<X509_Certificate> get_certs(const Search_Func&) const; + std::vector<X509_Certificate> get_cert_chain(const X509_Certificate&); + std::string PEM_encode() const; + + X509_Code add_crl(const X509_CRL&); + void add_cert(const X509_Certificate&, bool = false); + void add_certs(DataSource&); + void add_trusted_certs(DataSource&); + + void add_new_certstore(Certificate_Store*); + + static X509_Code check_sig(const X509_Object&, X509_PublicKey*); + + X509_Store(); + X509_Store(const X509_Store&); + ~X509_Store(); + private: + X509_Store& operator=(const X509_Store&) { return (*this); } + + class Cert_Info + { + public: + bool is_verified() const; + bool is_trusted() const; + X509_Code verify_result() const; + void set_result(X509_Code) const; + Cert_Info(const X509_Certificate&, bool = false); + + X509_Certificate cert; + bool trusted; + private: + mutable bool checked; + mutable X509_Code result; + mutable u64bit last_checked; + }; + + class CRL_Data + { + public: + X509_DN issuer; + MemoryVector<byte> serial, auth_key_id; + bool operator==(const CRL_Data&) const; + bool operator!=(const CRL_Data&) const; + bool operator<(const CRL_Data&) const; + }; + + u32bit find_cert(const X509_DN&, const MemoryRegion<byte>&) const; + X509_Code check_sig(const Cert_Info&, const Cert_Info&) const; + void recompute_revoked_info() const; + + void do_add_certs(DataSource&, bool); + X509_Code construct_cert_chain(const X509_Certificate&, + std::vector<u32bit>&, bool = false); + + u32bit find_parent_of(const X509_Certificate&); + bool is_revoked(const X509_Certificate&) const; + + static const u32bit NO_CERT_FOUND = 0xFFFFFFFF; + std::vector<Cert_Info> certs; + std::vector<CRL_Data> revoked; + std::vector<Certificate_Store*> stores; + mutable bool revoked_info_valid; + }; + +namespace X509_Store_Search { + +/************************************************* +* Methods to search through a X509_Store * +*************************************************/ +std::vector<X509_Certificate> by_email(const X509_Store&, const std::string&); +std::vector<X509_Certificate> by_name(const X509_Store&, const std::string&); +std::vector<X509_Certificate> by_dns(const X509_Store&, const std::string&); +std::vector<X509_Certificate> by_keyid(const X509_Store&, u64bit); +std::vector<X509_Certificate> by_iands(const X509_Store&, const X509_DN&, + const MemoryRegion<byte>&); +std::vector<X509_Certificate> by_SKID(const X509_Store&, + const MemoryRegion<byte>&); + +} + +} + +#endif diff --git a/include/x919_mac.h b/include/x919_mac.h new file mode 100644 index 000000000..e5f1529ac --- /dev/null +++ b/include/x919_mac.h @@ -0,0 +1,36 @@ +/************************************************* +* ANSI X9.19 MAC Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ANSI_X919_MAC_H__ +#define BOTAN_ANSI_X919_MAC_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* ANSI X9.19 MAC * +*************************************************/ +class ANSI_X919_MAC : public MessageAuthenticationCode + { + public: + void clear() throw(); + std::string name() const { return "X9.19-MAC"; } + MessageAuthenticationCode* clone() const { return new ANSI_X919_MAC; } + ANSI_X919_MAC(); + ~ANSI_X919_MAC(); + private: + void add_data(const byte[], u32bit); + void final_result(byte[]); + void key(const byte[], u32bit); + BlockCipher* e; + BlockCipher* d; + SecureBuffer<byte, 8> state; + u32bit position; + }; + +} + +#endif diff --git a/include/x931_rng.h b/include/x931_rng.h new file mode 100644 index 000000000..fda64ba86 --- /dev/null +++ b/include/x931_rng.h @@ -0,0 +1,38 @@ +/************************************************* +* ANSI X9.31 RNG Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_ANSI_X931_RNG_H__ +#define BOTAN_ANSI_X931_RNG_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* ANSI X9.31 RNG * +*************************************************/ +class ANSI_X931_RNG : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit) throw(PRNG_Unseeded); + bool is_seeded() const; + void clear() throw(); + std::string name() const; + + ANSI_X931_RNG(const std::string& = "", RandomNumberGenerator* = 0); + ~ANSI_X931_RNG(); + private: + void add_randomness(const byte[], u32bit); + void update_buffer(); + + BlockCipher* cipher; + RandomNumberGenerator* prng; + SecureVector<byte> V, R; + u32bit position; + }; + +} + +#endif diff --git a/include/xtea.h b/include/xtea.h new file mode 100644 index 000000000..0041891c4 --- /dev/null +++ b/include/xtea.h @@ -0,0 +1,32 @@ +/************************************************* +* XTEA Header File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#ifndef BOTAN_XTEA_H__ +#define BOTAN_XTEA_H__ + +#include <botan/base.h> + +namespace Botan { + +/************************************************* +* XTEA * +*************************************************/ +class XTEA : public BlockCipher + { + public: + void clear() throw() { EK.clear(); } + std::string name() const { return "XTEA"; } + BlockCipher* clone() const { return new XTEA; } + XTEA() : BlockCipher(8, 16) {} + private: + void enc(const byte[], byte[]) const; + void dec(const byte[], byte[]) const; + void key(const byte[], u32bit); + SecureBuffer<u32bit, 64> EK; + }; + +} + +#endif |