Compile Botan on windows.

This commit is contained in:
kh1
2010-01-11 16:16:42 +01:00
parent 6f22059c5d
commit 8d1c583554
218 changed files with 16342 additions and 0 deletions

4
src/libs/3rdparty/botan/botan.pro vendored Normal file
View File

@@ -0,0 +1,4 @@
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += src

View File

@@ -0,0 +1,35 @@
/*
* Adler32
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ADLER32_H__
#define BOTAN_ADLER32_H__
#include <botan/hash.h>
namespace Botan {
/*
* Adler32
*/
class BOTAN_DLL 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);
u16bit S1, S2;
};
}
#endif

View File

@@ -0,0 +1,81 @@
/**
* AES
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_AES_H__
#define BOTAN_AES_H__
#include <botan/block_cipher.h>
namespace Botan {
/**
* Rijndael aka AES
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
static u32bit S(u32bit);
static const byte SE[256];
static const byte SD[256];
static const u32bit TE[1024];
static const u32bit TD[1024];
u32bit ROUNDS;
SecureBuffer<u32bit, 56> EK;
SecureBuffer<byte, 16> ME;
SecureBuffer<u32bit, 56> DK;
SecureBuffer<byte, 16> MD;
};
/**
* AES-128
*/
class BOTAN_DLL 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 BOTAN_DLL 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 BOTAN_DLL AES_256 : public AES
{
public:
std::string name() const { return "AES-256"; }
BlockCipher* clone() const { return new AES_256; }
AES_256() : AES(32) {}
};
}
#endif

View File

@@ -0,0 +1,49 @@
/*
* Algorithm Identifier
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ALGORITHM_IDENTIFIER_H__
#define BOTAN_ALGORITHM_IDENTIFIER_H__
#include <botan/asn1_int.h>
#include <botan/asn1_oid.h>
#include <string>
namespace Botan {
/*
* Algorithm Identifier
*/
class BOTAN_DLL AlgorithmIdentifier : public ASN1_Object
{
public:
enum Encoding_Option { USE_NULL_PARAM };
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
AlgorithmIdentifier() {}
AlgorithmIdentifier(const OID&, Encoding_Option);
AlgorithmIdentifier(const std::string&, Encoding_Option);
AlgorithmIdentifier(const OID&, const MemoryRegion<byte>&);
AlgorithmIdentifier(const std::string&, const MemoryRegion<byte>&);
OID oid;
SecureVector<byte> parameters;
};
/*
* Comparison Operations
*/
bool BOTAN_DLL operator==(const AlgorithmIdentifier&,
const AlgorithmIdentifier&);
bool BOTAN_DLL operator!=(const AlgorithmIdentifier&,
const AlgorithmIdentifier&);
}
#endif

View File

@@ -0,0 +1,224 @@
/**
* An algorithm cache (used by Algorithm_Factory)
*/
#ifndef BOTAN_ALGORITHM_CACHE_TEMPLATE_H__
#define BOTAN_ALGORITHM_CACHE_TEMPLATE_H__
#include <botan/mutex.h>
#include <botan/stl_util.h>
#include <string>
#include <vector>
#include <map>
namespace Botan {
/**
* @param prov_name a provider name
* @return weight for this provider
*/
u32bit static_provider_weight(const std::string& prov_name);
/**
* Algorithm_Cache (used by Algorithm_Factory)
*/
template<typename T>
class Algorithm_Cache
{
public:
const T* get(const std::string& algo_spec,
const std::string& pref_provider);
/**
* Add a new algorithm implementation to the cache
*/
void add(T* algo,
const std::string& requested_name,
const std::string& provider_name);
/**
* Set the preferred provider
*/
void set_preferred_provider(const std::string& algo_spec,
const std::string& provider);
/**
* Return the list of providers of this algorithm
*/
std::vector<std::string> providers_of(const std::string& algo_name);
Algorithm_Cache(Mutex* m) : mutex(m) {}
~Algorithm_Cache();
private:
typedef typename std::map<std::string, std::map<std::string, T*> >::iterator
algorithms_iterator;
typedef typename std::map<std::string, T*>::iterator provider_iterator;
algorithms_iterator find_algorithm(const std::string& algo_spec);
Mutex* mutex;
std::map<std::string, std::string> aliases;
std::map<std::string, std::string> pref_providers;
std::map<std::string, std::map<std::string, T*> > algorithms;
};
/**
* Look for an algorithm implementation in the cache, also checking aliases
* Assumes object lock is held
*/
template<typename T>
typename Algorithm_Cache<T>::algorithms_iterator
Algorithm_Cache<T>::find_algorithm(const std::string& algo_spec)
{
algorithms_iterator algo = algorithms.find(algo_spec);
// Not found? Check if a known alias
if(algo == algorithms.end())
{
std::map<std::string, std::string>::const_iterator alias =
aliases.find(algo_spec);
if(alias != aliases.end())
algo = algorithms.find(alias->second);
}
return algo;
}
/**
* Look for an algorithm implementation by a particular provider
*/
template<typename T>
const T* Algorithm_Cache<T>::get(const std::string& algo_spec,
const std::string& requested_provider)
{
Mutex_Holder lock(mutex);
algorithms_iterator algo = find_algorithm(algo_spec);
if(algo == algorithms.end()) // algo not found at all (no providers)
return 0;
// If a provider is requested specifically, return it or fail entirely
if(requested_provider != "")
{
provider_iterator prov = algo->second.find(requested_provider);
if(prov != algo->second.end())
return prov->second;
return 0;
}
const T* prototype = 0;
std::string prototype_provider;
u32bit prototype_prov_weight = 0;
const std::string pref_provider = search_map(pref_providers, algo_spec);
for(provider_iterator i = algo->second.begin(); i != algo->second.end(); ++i)
{
const std::string prov_name = i->first;
const u32bit prov_weight = static_provider_weight(prov_name);
// preferred prov exists, return immediately
if(prov_name == pref_provider)
return i->second;
if(prototype == 0 || prov_weight > prototype_prov_weight)
{
prototype = i->second;
prototype_provider = i->first;
prototype_prov_weight = prov_weight;
}
}
return prototype;
}
/**
* Add an implementation to the cache
*/
template<typename T>
void Algorithm_Cache<T>::add(T* algo,
const std::string& requested_name,
const std::string& provider)
{
if(!algo)
return;
Mutex_Holder lock(mutex);
delete algorithms[algo->name()][provider];
algorithms[algo->name()][provider] = algo;
if(algo->name() != requested_name &&
aliases.find(requested_name) == aliases.end())
{
aliases[requested_name] = algo->name();
}
}
/**
* Find the providers of this algo (if any)
*/
template<typename T> std::vector<std::string>
Algorithm_Cache<T>::providers_of(const std::string& algo_name)
{
Mutex_Holder lock(mutex);
std::vector<std::string> providers;
algorithms_iterator algo = find_algorithm(algo_name);
if(algo != algorithms.end())
{
provider_iterator provider = algo->second.begin();
while(provider != algo->second.end())
{
providers.push_back(provider->first);
++provider;
}
}
return providers;
}
/**
* Set the preferred provider for an algorithm
*/
template<typename T>
void Algorithm_Cache<T>::set_preferred_provider(const std::string& algo_spec,
const std::string& provider)
{
Mutex_Holder lock(mutex);
pref_providers[algo_spec] = provider;
}
/**
* Algorithm_Cache<T> Destructor
*/
template<typename T>
Algorithm_Cache<T>::~Algorithm_Cache()
{
algorithms_iterator algo = algorithms.begin();
while(algo != algorithms.end())
{
provider_iterator provider = algo->second.begin();
while(provider != algo->second.end())
{
delete provider->second;
++provider;
}
++algo;
}
delete mutex;
}
}
#endif

View File

@@ -0,0 +1,132 @@
/**
* Algorithm Factory
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ALGORITHM_FACTORY_H__
#define BOTAN_ALGORITHM_FACTORY_H__
#include <botan/mutex.h>
#include <string>
#include <vector>
namespace Botan {
/**
* Forward declarations (don't need full definitions here)
*/
class BlockCipher;
class StreamCipher;
class HashFunction;
class MessageAuthenticationCode;
template<typename T> class Algorithm_Cache;
class Engine;
/**
* Algorithm Factory
*/
class BOTAN_DLL Algorithm_Factory
{
public:
/**
* Constructor
* @param engines_in the list of engines to use
* @param mf a mutex factory
*/
Algorithm_Factory(const std::vector<Engine*>& engines_in,
Mutex_Factory& mf);
/**
* Destructor
*/
~Algorithm_Factory();
/*
* Provider management
*/
std::vector<std::string> providers_of(const std::string& algo_spec);
void set_preferred_provider(const std::string& algo_spec,
const std::string& provider);
/*
* Block cipher operations
*/
const BlockCipher*
prototype_block_cipher(const std::string& algo_spec,
const std::string& provider = "");
BlockCipher* make_block_cipher(const std::string& algo_spec,
const std::string& provider = "");
void add_block_cipher(BlockCipher* hash, const std::string& provider);
/*
* Stream cipher operations
*/
const StreamCipher*
prototype_stream_cipher(const std::string& algo_spec,
const std::string& provider = "");
StreamCipher* make_stream_cipher(const std::string& algo_spec,
const std::string& provider = "");
void add_stream_cipher(StreamCipher* hash, const std::string& provider);
/*
* Hash function operations
*/
const HashFunction*
prototype_hash_function(const std::string& algo_spec,
const std::string& provider = "");
HashFunction* make_hash_function(const std::string& algo_spec,
const std::string& provider = "");
void add_hash_function(HashFunction* hash, const std::string& provider);
/*
* MAC operations
*/
const MessageAuthenticationCode*
prototype_mac(const std::string& algo_spec,
const std::string& provider = "");
MessageAuthenticationCode* make_mac(const std::string& algo_spec,
const std::string& provider = "");
void add_mac(MessageAuthenticationCode* mac,
const std::string& provider);
/*
* Deprecated
*/
class BOTAN_DLL Engine_Iterator
{
public:
class Engine* next() { return af.get_engine_n(n++); }
Engine_Iterator(const Algorithm_Factory& a) : af(a) { n = 0; }
private:
const Algorithm_Factory& af;
u32bit n;
};
friend class Engine_Iterator;
private:
class Engine* get_engine_n(u32bit) const;
std::vector<class Engine*> engines;
Algorithm_Cache<BlockCipher>* block_cipher_cache;
Algorithm_Cache<StreamCipher>* stream_cipher_cache;
Algorithm_Cache<HashFunction>* hash_cache;
Algorithm_Cache<MessageAuthenticationCode>* mac_cache;
};
}
#endif

View File

@@ -0,0 +1,37 @@
/*
* Allocator
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ALLOCATOR_H__
#define BOTAN_ALLOCATOR_H__
#include <botan/types.h>
#include <string>
namespace Botan {
/*
* Allocator Interface
*/
class BOTAN_DLL Allocator
{
public:
static Allocator* get(bool);
virtual void* allocate(u32bit) = 0;
virtual void deallocate(void*, u32bit) = 0;
virtual std::string type() const = 0;
virtual void init() {}
virtual void destroy() {}
virtual ~Allocator() {}
};
}
#endif

View File

@@ -0,0 +1,41 @@
/*
* ARC4
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ARC4_H__
#define BOTAN_ARC4_H__
#include <botan/stream_cipher.h>
#include <botan/types.h>
namespace Botan {
/*
* ARC4
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
void generate();
const u32bit SKIP;
SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer;
SecureBuffer<u32bit, 256> state;
u32bit X, Y, position;
};
}
#endif

View File

@@ -0,0 +1,108 @@
/*
* ASN.1 Internals
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ASN1_H__
#define BOTAN_ASN1_H__
#include <botan/secmem.h>
#include <botan/exceptn.h>
namespace Botan {
/*
* ASN.1 Type and Class Tags
*/
enum ASN1_Tag {
UNIVERSAL = 0x00,
APPLICATION = 0x40,
CONTEXT_SPECIFIC = 0x80,
PRIVATE = 0xC0,
CONSTRUCTED = 0x20,
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,
NO_OBJECT = 0xFF00,
DIRECTORY_STRING = 0xFF01
};
/*
* Basic ASN.1 Object Interface
*/
class BOTAN_DLL ASN1_Object
{
public:
virtual void encode_into(class DER_Encoder&) const = 0;
virtual void decode_from(class BER_Decoder&) = 0;
virtual ~ASN1_Object() {}
};
/*
* BER Encoded Object
*/
class BOTAN_DLL BER_Object
{
public:
void assert_is_a(ASN1_Tag, ASN1_Tag);
ASN1_Tag type_tag, class_tag;
SecureVector<byte> value;
};
/*
* ASN.1 Utility Functions
*/
class DataSource;
namespace ASN1 {
SecureVector<byte> put_in_sequence(const MemoryRegion<byte>&);
std::string to_string(const BER_Object&);
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

View File

@@ -0,0 +1,160 @@
/*
* Common ASN.1 Objects
* (C) 1999-2007 Jack Lloyd
* 2007 Yves Jerschow
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ASN1_OBJ_H__
#define BOTAN_ASN1_OBJ_H__
#include <botan/asn1_int.h>
#include <botan/asn1_oid.h>
#include <botan/alg_id.h>
#include <vector>
#include <map>
namespace Botan {
/*
* Attribute
*/
class BOTAN_DLL Attribute : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
OID oid;
MemoryVector<byte> parameters;
Attribute() {}
Attribute(const OID&, const MemoryRegion<byte>&);
Attribute(const std::string&, const MemoryRegion<byte>&);
};
/*
* X.509 Time
*/
class BOTAN_DLL X509_Time : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
std::string as_string() const;
std::string readable_string() const;
bool time_is_set() const;
s32bit cmp(const X509_Time&) const;
void set_to(const std::string&);
void set_to(const std::string&, ASN1_Tag);
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 BOTAN_DLL ASN1_String : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
std::string value() const;
std::string iso_8859() const;
ASN1_Tag tagging() const;
ASN1_String(const std::string& = "");
ASN1_String(const std::string&, ASN1_Tag);
private:
std::string iso_8859_str;
ASN1_Tag tag;
};
/*
* Distinguished Name
*/
class BOTAN_DLL X509_DN : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
std::multimap<OID, std::string> get_attributes() const;
std::vector<std::string> get_attribute(const std::string&) const;
std::multimap<std::string, std::string> contents() const;
void add_attribute(const std::string&, const std::string&);
void add_attribute(const OID&, const std::string&);
static std::string deref_info_field(const std::string&);
void do_decode(const MemoryRegion<byte>&);
MemoryVector<byte> get_bits() const;
X509_DN();
X509_DN(const std::multimap<OID, std::string>&);
X509_DN(const std::multimap<std::string, std::string>&);
private:
std::multimap<OID, ASN1_String> dn_info;
MemoryVector<byte> dn_bits;
};
/*
* Alternative Name
*/
class BOTAN_DLL AlternativeName : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
std::multimap<std::string, std::string> contents() 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& = "", const std::string& = "");
private:
std::multimap<std::string, std::string> alt_info;
std::multimap<OID, ASN1_String> othernames;
};
/*
* Comparison Operations
*/
bool BOTAN_DLL operator==(const X509_Time&, const X509_Time&);
bool BOTAN_DLL operator!=(const X509_Time&, const X509_Time&);
bool BOTAN_DLL operator<=(const X509_Time&, const X509_Time&);
bool BOTAN_DLL operator>=(const X509_Time&, const X509_Time&);
bool BOTAN_DLL operator==(const X509_DN&, const X509_DN&);
bool BOTAN_DLL operator!=(const X509_DN&, const X509_DN&);
bool BOTAN_DLL operator<(const X509_DN&, const X509_DN&);
/*
* Helper Functions
*/
bool BOTAN_DLL is_string_type(ASN1_Tag);
}
#endif

View File

@@ -0,0 +1,96 @@
/*
* ASN.1 OID
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ASN1_OID_H__
#define BOTAN_ASN1_OID_H__
#include <botan/asn1_int.h>
#include <string>
#include <vector>
namespace Botan {
/**
* This class represents ASN.1 object identifiers.
*/
class BOTAN_DLL OID : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
/**
* Find out whether this OID is empty
* @return true is no OID value is set
*/
bool is_empty() const { return id.size() == 0; }
/**
* Get this OID as list (vector) of its components.
* @return a vector representing this OID
*/
std::vector<u32bit> get_id() const { return id; }
/**
* Get this OID as a string
* @return a string representing this OID
*/
std::string as_string() const;
/**
* Compare two OIDs.
* @return true if they are equal, false otherwise
*/
bool operator==(const OID&) const;
/**
* Reset this instance to an empty OID.
*/
void clear();
/**
* Add a component to this OID.
* @param new_comp the new component to add to the end of this OID
* @return a reference to *this
*/
OID& operator+=(u32bit new_comp);
/**
* Construct an OID from a string.
* @param str a string in the form "a.b.c" etc., where a,b,c are numbers
*/
OID(const std::string& str = "");
private:
std::vector<u32bit> id;
};
/**
* Append another component onto the OID.
* @param oid the OID to add the new component to
* @param new_comp the new component to add
*/
OID operator+(const OID& oid, u32bit new_comp);
/**
* Compare two OIDs.
* @param a the first OID
* @param b the second OID
* @return true if a is not equal to b
*/
bool operator!=(const OID& a, const OID& b);
/**
* Compare two OIDs.
* @param a the first OID
* @param b the second OID
* @return true if a is lexicographically smaller than b
*/
bool operator<(const OID& a, const OID& b);
}
#endif

View File

@@ -0,0 +1,44 @@
/*
* Auto Seeded RNG
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_AUTO_SEEDING_RNG_H__
#define BOTAN_AUTO_SEEDING_RNG_H__
#include <botan/rng.h>
#include <string>
namespace Botan {
/**
* RNG that attempts to seed itself
*/
class BOTAN_DLL AutoSeeded_RNG : public RandomNumberGenerator
{
public:
void randomize(byte out[], u32bit len)
{ rng->randomize(out, len); }
bool is_seeded() const
{ return rng->is_seeded(); }
void clear() throw() { rng->clear(); }
std::string name() const
{ return "AutoSeeded(" + rng->name() + ")"; }
void reseed(u32bit poll_bits = 256) { rng->reseed(poll_bits); }
void add_entropy_source(EntropySource* es)
{ rng->add_entropy_source(es); }
void add_entropy(const byte in[], u32bit len)
{ rng->add_entropy(in, len); }
AutoSeeded_RNG(u32bit poll_bits = 256);
~AutoSeeded_RNG() { delete rng; }
private:
RandomNumberGenerator* rng;
};
}
#endif

View File

@@ -0,0 +1,94 @@
/*
* Base64 Encoder/Decoder
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BASE64_H__
#define BOTAN_BASE64_H__
#include <botan/filter.h>
namespace Botan {
/**
* This class represents a Base64 encoder.
*/
class BOTAN_DLL Base64_Encoder : public Filter
{
public:
static void encode(const byte in[3], byte out[4]);
/**
* Input a part of a message to the encoder.
* @param input the message to input as a byte array
* @param length the length of the byte array input
*/
void write(const byte input[], u32bit length);
/**
* Inform the Encoder that the current message shall be closed.
*/
void end_msg();
/**
* Create a base64 encoder.
* @param breaks whether to use line breaks in the Streamcipheroutput
* @param length the length of the lines of the output
* @param t_n whether to use a trailing newline
*/
Base64_Encoder(bool breaks = false, u32bit length = 72,
bool t_n = false);
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;
const bool trailing_newline;
SecureVector<byte> in, out;
u32bit position, counter;
};
/**
* This object represents a Base64 decoder.
*/
class BOTAN_DLL Base64_Decoder : public Filter
{
public:
static void decode(const byte input[4], byte output[3]);
static bool is_valid(byte);
/**
* Input a part of a message to the decoder.
* @param input the message to input as a byte array
* @param length the length of the byte array input
*/
void write(const byte input[], u32bit length);
/**
* Inform the Encoder that the current message shall be closed.
*/
void end_msg();
/**
* Create a base64 encoder.
* @param checking the type of checking that shall be performed by
* the decoder
*/
Base64_Decoder(Decoder_Checking 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

View File

@@ -0,0 +1,99 @@
/*
* Basic Filters
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BASEFILT_H__
#define BOTAN_BASEFILT_H__
#include <botan/filter.h>
#include <botan/sym_algo.h>
namespace Botan {
/**
* This class represents Filter chains. A Filter chain is an ordered
* concatenation of Filters, the input to a Chain sequentially passes
* through all the Filters contained in the Chain.
*/
class BOTAN_DLL Chain : public Fanout_Filter
{
public:
void write(const byte input[], u32bit length) { send(input, length); }
/**
* Construct a chain of up to four filters. The filters are set
* up in the same order as the arguments.
*/
Chain(Filter* = 0, Filter* = 0, Filter* = 0, Filter* = 0);
/**
* Construct a chain from range of filters
* @param filter_arr the list of filters
* @param length how many filters
*/
Chain(Filter* filter_arr[], u32bit length);
};
/**
* This class represents a fork filter, whose purpose is to fork the
* flow of data. It causes an input message to result in n messages at
* the end of the filter, where n is the number of forks.
*/
class BOTAN_DLL 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); }
/**
* Construct a Fork filter with up to four forks.
*/
Fork(Filter*, Filter*, Filter* = 0, Filter* = 0);
/**
* Construct a Fork from range of filters
* @param filter_arr the list of filters
* @param length how many filters
*/
Fork(Filter* filter_arr[], u32bit length);
};
/**
* This class represents keyed filters, i.e. filters that have to be
* fed with a key in order to function.
*/
class BOTAN_DLL Keyed_Filter : public Filter
{
public:
/**
* Set the key of this filter.
* @param key the key to set
*/
virtual void set_key(const SymmetricKey& key);
/**
* Set the initialization vector of this filter.
* @param iv the initialization vector to set
*/
virtual void set_iv(const InitializationVector&) {}
/**
* Check whether a key length is valid for this filter.
* @param length the key length to be checked for validity
* @return true if the key length is valid, false otherwise
*/
virtual bool valid_keylength(u32bit length) const;
Keyed_Filter() { base_ptr = 0; }
protected:
SymmetricAlgorithm* base_ptr;
};
}
#endif

View File

@@ -0,0 +1,59 @@
/**
* Runtime benchmarking
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_RUNTIME_BENCHMARK_H__
#define BOTAN_RUNTIME_BENCHMARK_H__
#include <botan/algo_factory.h>
#include <botan/timer.h>
#include <botan/rng.h>
#include <map>
#include <string>
/**
* Choose some sort of default timer implementation to use, since some
* (like hardware tick counters and current Win32 timer) are not
* reliable for benchmarking.
*/
#if defined(BOTAN_HAS_TIMER_POSIX)
#include <botan/tm_posix.h>
#elif defined(BOTAN_HAS_TIMER_UNIX)
#include <botan/tm_unix.h>
#endif
namespace Botan {
#if defined(BOTAN_HAS_TIMER_POSIX)
typedef POSIX_Timer Default_Benchmark_Timer;
#elif defined(BOTAN_HAS_TIMER_UNIX)
typedef Unix_Timer Default_Benchmark_Timer;
#else
/* I have not had good success using clock(), the results seem
* pretty bogus, but as a last resort it works.
*/
typedef ANSI_Clock_Timer Default_Benchmark_Timer;
#endif
/**
* Algorithm benchmark
* @param name the name of the algorithm to test (cipher, hash, or MAC)
* @param milliseconds total time for the benchmark to run
* @param timer the timer to use
* @param rng the rng to use to generate random inputs
* @param af the algorithm factory used to create objects
* @return results a map from provider to speed in mebibytes per second
*/
std::map<std::string, double>
algorithm_benchmark(const std::string& name,
u32bit milliseconds,
Timer& timer,
RandomNumberGenerator& rng,
Algorithm_Factory& af);
}
#endif

View File

@@ -0,0 +1,122 @@
/*
* BER Decoder
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BER_DECODER_H__
#define BOTAN_BER_DECODER_H__
#include <botan/asn1_oid.h>
#include <botan/data_src.h>
namespace Botan {
/*
* BER Decoding Object
*/
class BOTAN_DLL BER_Decoder
{
public:
BER_Object get_next_object();
void push_back(const BER_Object&);
bool more_items() const;
BER_Decoder& verify_end();
BER_Decoder& discard_remaining();
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL);
BER_Decoder& end_cons();
BER_Decoder& raw_bytes(MemoryRegion<byte>&);
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& decode(class ASN1_Object&);
template<typename T>
BER_Decoder& decode_optional(T&, ASN1_Tag, ASN1_Tag, const T& = T());
template<typename T>
BER_Decoder& decode_list(std::vector<T>&, bool = true);
BER_Decoder& decode_optional_string(MemoryRegion<byte>&,
ASN1_Tag, u16bit);
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); }
BER_Decoder* parent;
DataSource* source;
BER_Object pushed;
mutable bool owns;
};
/*
* Decode an OPTIONAL or DEFAULT element
*/
template<typename T>
BER_Decoder& BER_Decoder::decode_optional(T& out,
ASN1_Tag type_tag,
ASN1_Tag class_tag,
const T& default_value)
{
BER_Object obj = get_next_object();
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
{
if(class_tag & CONSTRUCTED)
BER_Decoder(obj.value).decode(out).verify_end();
else
{
push_back(obj);
decode(out, type_tag, class_tag);
}
}
else
{
out = default_value;
push_back(obj);
}
return (*this);
}
/*
* Decode a list of homogenously typed values
*/
template<typename T>
BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, bool clear_it)
{
if(clear_it)
vec.clear();
while(more_items())
{
T value;
decode(value);
vec.push_back(value);
}
return (*this);
}
}
#endif

View File

@@ -0,0 +1,534 @@
/*
* BigInt
* (C) 1999-2008 Jack Lloyd
* 2007 FlexSecure
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BIGINT_H__
#define BOTAN_BIGINT_H__
#include <botan/rng.h>
#include <botan/secmem.h>
#include <botan/mp_types.h>
#include <iosfwd>
namespace Botan {
/**
* Big Integer representation. This class defines an integer type,
* that can be very big. Additionally some helper functions are
* defined to work more comfortably.
*/
class BOTAN_DLL BigInt
{
public:
/**
* Base-Enumerator (currently 8,10,16 and 256 are defined)
*/
enum Base { Octal = 8, Decimal = 10, Hexadecimal = 16, Binary = 256 };
/**
* Sign symbol definitions for positive and negative numbers
*/
enum Sign { Negative = 0, Positive = 1 };
/**
* Number types (Powers of 2)
*/
enum NumberType { Power2 };
/**
* DivideByZero Exception
*/
struct DivideByZero : public Exception
{ DivideByZero() : Exception("BigInt divide by zero") {} };
/*************
* operators
*************/
/**
* += Operator
* @param y the BigInt to add to the local value
*/
BigInt& operator+=(const BigInt& y);
/**
* -= Operator
* @param y the BigInt to subtract from the local value
*/
BigInt& operator-=(const BigInt& y);
/**
* *= Operator
* @param y the BigInt to multiply with the local value
*/
BigInt& operator*=(const BigInt& y);
/**
* /= Operator
* @param y the BigInt to divide the local value by
*/
BigInt& operator/=(const BigInt& y);
/**
* %= Operator, modulo operator.
* @param y the modulus to reduce the local value by
*/
BigInt& operator%=(const BigInt& y);
/**
* %= Operator
* @param y the modulus (word) to reduce the local value by
*/
word operator%=(word y);
/**
* <<= Operator
* @param y the amount of bits to shift the local value left
*/
BigInt& operator<<=(u32bit y);
/**
* >>= Operator
* @param y the amount of bits to shift the local value right
*/
BigInt& operator>>=(u32bit y);
/**
* ++ Operator
*/
BigInt& operator++() { return (*this += 1); }
/**
* -- Operator
*/
BigInt& operator--() { return (*this -= 1); }
/**
* ++ Operator (postfix)
*/
BigInt operator++(int) { BigInt x = (*this); ++(*this); return x; }
/**
* -- Operator (postfix)
*/
BigInt operator--(int) { BigInt x = (*this); --(*this); return x; }
/**
* - Operator
*/
BigInt operator-() const;
/**
* ! Operator
*/
bool operator !() const { return (!is_nonzero()); }
/**
* [] Operator (array access)
*/
word& operator[](u32bit i) { return reg[i]; }
/**
* [] Operator (array access)
*/
word operator[](u32bit i) const { return reg[i]; }
/**
* Zeroize the BigInt
*/
void clear() { get_reg().clear(); }
/*************
* functions
************/
/**
* Compare *this to another BigInt.
* @param n the BigInt value to compare to the local value.
* @param check_signs Include sign in comparison?
* @result if (this<n) return -1, if (this>n) return 1, if both
* values are identical return 0.
*/
s32bit cmp(const BigInt& n, bool check_signs = true) const;
/**
* Test if the integer has an even value
* @result true, if the integer an even value, false otherwise
*/
bool is_even() const { return (get_bit(0) == 0); }
/**
* Test if the integer has an odd value
* @result true, if the integer an odd value, false otherwise
*/
bool is_odd() const { return (get_bit(0) == 1); }
/**
* Test if the integer is not zero.
* @result true, if the integer has a non-zero value, false otherwise
*/
bool is_nonzero() const { return (!is_zero()); }
/**
* Test if the integer is zero.
* @result true, if the integer has the value zero, false otherwise
*/
bool is_zero() const
{
const u32bit sw = sig_words();
for(u32bit i = 0; i != sw; ++i)
if(reg[i])
return false;
return true;
}
/**
* Set bit at specified position
* @param n bit position to set
*/
void set_bit(u32bit n);
/**
* Clear bit at specified position
* @param n bit position to clear
*/
void clear_bit(u32bit n);
/**
* Clear all but the lowest n bits
* @param n amount of bits to keep
*/
void mask_bits(u32bit n);
/**
* Return bit value at specified position
* @param n the bit offset to test
* @result true, if the bit at position n is set, false otherwise
*/
bool get_bit(u32bit n) const;
/**
* Return (a maximum of) 32 bits of the complete value
* @param offset the offset to start extracting
* @param length amount of bits to extract (starting at offset)
* @result the integer extracted from the register starting at
* offset with specified length
*/
u32bit get_substring(u32bit offset, u32bit length) const;
byte byte_at(u32bit) const;
/**
* Return the word at a specified position of the internal register
* @param n position in the register
* @return the value at position n
*/
word word_at(u32bit n) const
{ return ((n < size()) ? reg[n] : 0); }
/**
* Return the integer as an unsigned 32bit-integer-value. If the
* value is negative OR to big to be stored in 32bits, this
* function will throw an exception.
* @result a 32bit-integer
*/
u32bit to_u32bit() const;
/**
* Tests if the sign of the integer is negative.
* @result true, if the integer has a negative sign,
*/
bool is_negative() const { return (sign() == Negative); }
/**
* Tests if the sign of the integer is positive.
* @result true, if the integer has a positive sign,
*/
bool is_positive() const { return (sign() == Positive); }
/**
* Return the sign of the integer
* @result the sign of the integer
*/
Sign sign() const { return (signedness); }
/**
* Return the opposite sign of the represented integer value
* @result the opposite sign of the represented integer value
*/
Sign reverse_sign() const;
/**
* Flip (change!) the sign of the integer to its opposite value
*/
void flip_sign();
/**
* Set sign of the integer
* @param sign new Sign to set
*/
void set_sign(Sign sign);
/**
* Give absolute (positive) value of the integer
* @result absolute (positive) value of the integer
*/
BigInt abs() const;
/**
* Give size of internal register
* @result size of internal register in words
*/
u32bit size() const { return get_reg().size(); }
/**
* Give significant words of the represented integer value
* @result significant words of the represented integer value
*/
u32bit sig_words() const
{
const word* x = reg.begin();
u32bit sig = reg.size();
while(sig && (x[sig-1] == 0))
sig--;
return sig;
}
/**
* Give byte-length of the integer
* @result byte-length of the represented integer value
*/
u32bit bytes() const;
/**
* Get the bit-length of the integer.
* @result bit-length of the represented integer value
*/
u32bit bits() const;
/**
* Return a pointer to the big integer word register.
* @result a pointer to the start of the internal register of
* the integer value
*/
const word* data() const { return reg.begin(); }
/**
* return a reference to the internal register containing the value
* @result a reference to the word-array (SecureVector<word>)
* with the internal register value (containing the integer
* value)
*/
SecureVector<word>& get_reg() { return reg; }
/**
* return a const reference to the internal register containing the value
* @result a const reference to the word-array (SecureVector<word>)
* with the internal register value (containing the integer
* value)
*/
const SecureVector<word>& get_reg() const { return reg; }
/**
* Increase internal register buffer by n words
* @param n increase by n words
*/
void grow_reg(u32bit n);
void grow_to(u32bit n);
/**
* Fill BigInt with a random number with size of bitsize
* @param rng the random number generator to use
* @param bitsize number of bits the created random value should have
*/
void randomize(RandomNumberGenerator& rng, u32bit bitsize = 0);
/**
* Store BigInt-value in a given byte array
* @param buf destination byte array for the integer value
*/
void binary_encode(byte buf[]) const;
/**
* Read integer value from a byte array with given size
* @param buf byte array buffer containing the integer
* @param length size of buf
*/
void binary_decode(const byte buf[], u32bit length);
/**
* Read integer value from a byte array (MemoryRegion<byte>)
* @param buf the BigInt value to compare to the local value.
*/
void binary_decode(const MemoryRegion<byte>& buf);
u32bit encoded_size(Base = Binary) const;
/**
@param rng a random number generator
@result a random integer between min and max
*/
static BigInt random_integer(RandomNumberGenerator& rng,
const BigInt& min, const BigInt& max);
/**
* Encode the integer value from a BigInt to a SecureVector of bytes
* @param n the BigInt to use as integer source
* @param base number-base of resulting byte array representation
* @result SecureVector of bytes containing the integer with given base
*/
static SecureVector<byte> encode(const BigInt& n, Base base = Binary);
/**
* Encode the integer value from a BigInt to a byte array
* @param buf destination byte array for the encoded integer
* value with given base
* @param n the BigInt to use as integer source
* @param base number-base of resulting byte array representation
*/
static void encode(byte buf[], const BigInt& n, Base base = Binary);
/**
* Create a BigInt from an integer in a byte array
* @param buf the BigInt value to compare to the local value.
* @param length size of buf
* @param base number-base of the integer in buf
* @result BigInt-representing the given integer read from the byte array
*/
static BigInt decode(const byte buf[], u32bit length,
Base base = Binary);
static BigInt decode(const MemoryRegion<byte>&, Base = Binary);
/**
* Encode a Big Integer to a byte array according to IEEE1363.
* @param n the Big Integer to encode
* @param bytes the length of the resulting SecureVector<byte>
* @result a SecureVector<byte> containing the encoded Big Integer
*/
static SecureVector<byte> encode_1363(const BigInt& n, u32bit bytes);
/**
* Swap BigInt-value with given BigInt.
* @param bigint the BigInt to swap values with
*/
void swap(BigInt& bigint);
/**
* constructors
*/
/**
* Create empty BigInt
*/
BigInt() { signedness = Positive; }
/**
* Create BigInt from 64bit-Integer value
* @param n 64bit-integer
*/
BigInt(u64bit n);
/**
* Copy-Constructor: clone given BigInt
* @param bigint the BigInt to clone
*/
BigInt(const BigInt& bigint);
/**
* Create BigInt from a string.
* If the string starts with 0x the rest of the string will be
* interpreted as hexadecimal digits.
* If the string starts with 0 and the second character is NOT
* an 'x' the string will be interpreted as octal digits.
* If the string starts with non-zero digit, it will be
* interpreted as a decimal number.
* @param str the string to parse for an integer value
*/
BigInt(const std::string& str);
/**
* Create a BigInt from an integer in a byte array
* @param buf the BigInt value to compare to the local value.
* @param length size of buf
* @param base number-base of the integer in buf
*/
BigInt(const byte buf[], u32bit length, Base base = Binary);
/**
* Create a random BigInt of the specified size
* @param rng random number generator
* @param bits size in bits
*/
BigInt(RandomNumberGenerator& rng, u32bit bits);
/**
* Create BigInt from unsigned 32 bit integer value and an
* also specify the sign of the value
* @param n integer value
*/
BigInt(Sign, u32bit n);
/**
* Create a number of the specified type and size
* @param type the type of number to create
* @param n the size
*/
BigInt(NumberType type, u32bit n);
private:
SecureVector<word> reg;
Sign signedness;
};
/*
* Arithmetic Operators
*/
BigInt BOTAN_DLL operator+(const BigInt&, const BigInt&);
BigInt BOTAN_DLL operator-(const BigInt&, const BigInt&);
BigInt BOTAN_DLL operator*(const BigInt&, const BigInt&);
BigInt BOTAN_DLL operator/(const BigInt&, const BigInt&);
BigInt BOTAN_DLL operator%(const BigInt&, const BigInt&);
word BOTAN_DLL operator%(const BigInt&, word);
BigInt BOTAN_DLL operator<<(const BigInt&, u32bit);
BigInt BOTAN_DLL 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
*/
BOTAN_DLL std::ostream& operator<<(std::ostream&, const BigInt&);
BOTAN_DLL std::istream& operator>>(std::istream&, BigInt&);
}
namespace std {
inline void swap(Botan::BigInt& a, Botan::BigInt& b) { a.swap(b); }
}
#endif

View File

@@ -0,0 +1,91 @@
/*
* Bit/Word Operations
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BIT_OPS_H__
#define BOTAN_BIT_OPS_H__
#include <botan/types.h>
namespace Botan {
/*
* Return true iff arg is 2**n for some n > 0
* T should be an unsigned integer type
*/
template<typename T>
inline bool power_of_2(T arg)
{
return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0));
}
/*
* Return the index of the highest set bit
* T is an unsigned integer type
*/
template<typename T>
inline u32bit high_bit(T n)
{
for(u32bit i = 8*sizeof(T); i > 0; --i)
if((n >> (i - 1)) & 0x01)
return i;
return 0;
}
/*
* Return the index of the lowest set bit
*/
template<typename T>
inline u32bit low_bit(T n)
{
for(u32bit i = 0; i != 8*sizeof(T); ++i)
if((n >> i) & 0x01)
return (i + 1);
return 0;
}
/*
* Return the number of significant bytes in n
*/
template<typename T>
inline u32bit significant_bytes(T n)
{
for(u32bit j = 0; j != sizeof(T); ++j)
if(get_byte(j, n))
return sizeof(T)-j;
return 0;
}
/*
* Return the Hamming weight of n
*/
template<typename T>
inline u32bit hamming_weight(T n)
{
const byte NIBBLE_WEIGHTS[] = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
u32bit weight = 0;
for(u32bit i = 0; i != 2*sizeof(T); ++i)
weight += NIBBLE_WEIGHTS[(n >> (4*i)) & 0x0F];
return weight;
}
/*
* Count the trailing zero bits in n
*/
template<typename T>
inline u32bit ctz(T n)
{
for(u32bit i = 0; i != 8*sizeof(T); ++i)
if((n >> i) & 0x01)
return i;
return 8*sizeof(T);
}
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* Blinder
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BLINDER_H__
#define BOTAN_BLINDER_H__
#include <botan/bigint.h>
#include <botan/reducer.h>
namespace Botan {
/*
* Blinding Function Object
*/
class BOTAN_DLL 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

View File

@@ -0,0 +1,100 @@
/**
* Block Cipher Base Class
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BLOCK_CIPHER_H__
#define BOTAN_BLOCK_CIPHER_H__
#include <botan/sym_algo.h>
namespace Botan {
/**
* This class represents a block cipher object.
*
* It would be very useful to extend this interface to support the
* encryption of multiple blocks at a time. This could help
* performance, wrt cache effects in the software implementations, and
* could be a big deal when supporting block ciphers implemented as
* hardware devices. It could be used by implementations of ECB, and
* more importantly counter mode (which most designs are moving to, due
* to the parallelism possible in counter mode which is not the case
* with feedback-based modes like CBC).
*
* Probable future API here:
* virtual void encrypt_n(const byte in[], byte out[],
* u32bit blocks) const = 0;
* virtual void decrypt_n(const byte in[], byte out[],
* u32bit blocks) const = 0;
*/
class BOTAN_DLL BlockCipher : public SymmetricAlgorithm
{
public:
/**
* The block size of this algorithm.
*/
const u32bit BLOCK_SIZE;
/**
* Encrypt a block.
* @param in The plaintext block to be encrypted as a byte array.
* Must be of length BLOCK_SIZE.
* @param out The byte array designated to hold the encrypted block.
* Must be of length BLOCK_SIZE.
*/
void encrypt(const byte in[], byte out[]) const { enc(in, out); }
/**
* Decrypt a block.
* @param in The ciphertext block to be decypted as a byte array.
* Must be of length BLOCK_SIZE.
* @param out The byte array designated to hold the decrypted block.
* Must be of length BLOCK_SIZE.
*/
void decrypt(const byte in[], byte out[]) const { dec(in, out); }
/**
* Encrypt a block.
* @param in The plaintext block to be encrypted as a byte array.
* Must be of length BLOCK_SIZE. Will hold the result when the function
* has finished.
*/
void encrypt(byte block[]) const { enc(block, block); }
/**
* Decrypt a block.
* @param in The ciphertext block to be decrypted as a byte array.
* Must be of length BLOCK_SIZE. Will hold the result when the function
* has finished.
*/
void decrypt(byte block[]) const { dec(block, block); }
/**
* Get a new object representing the same algorithm as *this
*/
virtual BlockCipher* clone() const = 0;
/**
* Zeroize internal state
*/
virtual void clear() throw() = 0;
BlockCipher(u32bit block_size,
u32bit key_min,
u32bit key_max = 0,
u32bit key_mod = 1) :
SymmetricAlgorithm(key_min, key_max, key_mod),
BLOCK_SIZE(block_size) {}
virtual ~BlockCipher() {}
private:
virtual void enc(const byte[], byte[]) const = 0;
virtual void dec(const byte[], byte[]) const = 0;
};
}
#endif

View File

@@ -0,0 +1,40 @@
/*
* Blowfish
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BLOWFISH_H__
#define BOTAN_BLOWFISH_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* Blowfish
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
void generate_sbox(u32bit[], u32bit, u32bit&, u32bit&) const;
static const u32bit P_INIT[18];
static const u32bit S_INIT[1024];
SecureBuffer<u32bit, 1024> S;
SecureBuffer<u32bit, 18> P;
};
}
#endif

View File

@@ -0,0 +1,18 @@
/**
* A vague catch all include file for Botan
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/init.h>
#include <botan/lookup.h>
#include <botan/libstate.h>
#include <botan/version.h>
#include <botan/parsing.h>
#include <botan/rng.h>
#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
#include <botan/auto_rng.h>
#endif

View File

@@ -0,0 +1,62 @@
/*
* Byte Swapping Operations
* (C) 1999-2008 Jack Lloyd
* (C) 2007 Yves Jerschow
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BYTE_SWAP_H__
#define BOTAN_BYTE_SWAP_H__
#include <botan/types.h>
#include <botan/rotate.h>
namespace Botan {
/*
* Byte Swapping Functions
*/
inline u16bit reverse_bytes(u16bit input)
{
return rotate_left(input, 8);
}
inline u32bit reverse_bytes(u32bit input)
{
#if BOTAN_USE_GCC_INLINE_ASM && \
(defined(BOTAN_TARGET_ARCH_IS_IA32) || defined(BOTAN_TARGET_ARCH_IS_AMD64))
/* GCC-style inline assembly for x86 or x86-64 */
asm("bswapl %0" : "=r" (input) : "0" (input));
return input;
#elif defined(_MSC_VER) && defined(BOTAN_TARGET_ARCH_IS_IA32)
/* Visual C++ inline asm for 32-bit x86, by Yves Jerschow */
__asm mov eax, input;
__asm bswap eax;
#else
/* Generic implementation */
input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
return rotate_left(input, 16);
#endif
}
inline u64bit reverse_bytes(u64bit input)
{
#if BOTAN_USE_GCC_INLINE_ASM && defined(BOTAN_TARGET_ARCH_IS_AMD64)
asm("bswapq %0" : "=r" (input) : "0" (input));
return input;
#else
u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00);
u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
hi = (hi << 16) | (hi >> 16);
lo = (lo << 16) | (lo >> 16);
return (static_cast<u64bit>(lo) << 32) | hi;
#endif
}
}
#endif

View File

@@ -0,0 +1,126 @@
/**
* BufferedComputation
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BUFFERED_COMPUTATION_H__
#define BOTAN_BUFFERED_COMPUTATION_H__
#include <botan/secmem.h>
namespace Botan {
/**
* This class represents any kind of computation which
* uses an internal state,
* such as hash functions.
*/
class BOTAN_DLL BufferedComputation
{
public:
/**
* The length of the output of this function in bytes.
*/
const u32bit OUTPUT_LENGTH;
/**
* Add new input to process.
* @param in the input to process as a byte array
* @param the length of the byte array
*/
void update(const byte in[], u32bit length) { add_data(in, length); }
/**
* Add new input to process.
* @param in the input to process as a MemoryRegion
*/
void update(const MemoryRegion<byte>& in) { add_data(in, in.size()); }
/**
* Add new input to process.
* @param str the input to process as a std::string. Will be interpreted
* as a byte array based on
* the strings encoding.
*/
void update(const std::string& str)
{
add_data(reinterpret_cast<const byte*>(str.data()), str.size());
}
/**
* Process a single byte.
* @param in the byte to process
*/
void update(byte in) { add_data(&in, 1); }
/**
* Complete the computation and retrieve the
* final result.
* @param out The byte array to be filled with the result.
* Must be of length OUTPUT_LENGTH.
*/
void final(byte out[]) { final_result(out); }
/**
* Complete the computation and retrieve the
* final result.
* @return a SecureVector holding the result
*/
SecureVector<byte> final()
{
SecureVector<byte> output(OUTPUT_LENGTH);
final_result(output);
return output;
}
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process as a byte array
* @param length the length of the byte array
* @result the result of the call to final()
*/
SecureVector<byte> process(const byte in[], u32bit length)
{
add_data(in, length);
return final();
}
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process
* @result the result of the call to final()
*/
SecureVector<byte> process(const MemoryRegion<byte>& in)
{
add_data(in, in.size());
return final();
}
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process as a string
* @result the result of the call to final()
*/
SecureVector<byte> process(const std::string& in)
{
update(in);
return final();
}
BufferedComputation(u32bit out_len) : OUTPUT_LENGTH(out_len) {}
virtual ~BufferedComputation() {}
private:
BufferedComputation& operator=(const BufferedComputation&);
virtual void add_data(const byte[], u32bit) = 0;
virtual void final_result(byte[]) = 0;
};
}
#endif

View File

@@ -0,0 +1,39 @@
/*
* Buffering Filter
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BUFFERING_FILTER_H__
#define BOTAN_BUFFERING_FILTER_H__
#include <botan/filter.h>
namespace Botan {
/**
* Buffering_Filter: This class represents filters for operations that
* maintain an internal state.
*/
class BOTAN_DLL 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

View File

@@ -0,0 +1,39 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef BOTAN_BUILD_CONFIG_H_QT
#define BOTAN_BUILD_CONFIG_H_QT
#include <qglobal.h>
#if defined(Q_OS_WIN)
# include "build_windows.h"
#endif
#endif

View File

@@ -0,0 +1,335 @@
#ifndef BOTAN_BUILD_CONFIG_H__
#define BOTAN_BUILD_CONFIG_H__
/* This file was automatically generated Mon Jan 11 14:51:24 2010 UTC */
#define BOTAN_VERSION_MAJOR 1
#define BOTAN_VERSION_MINOR 8
#define BOTAN_VERSION_PATCH 8
#ifndef BOTAN_DLL
#define BOTAN_DLL __declspec(dllexport)
#endif
/* Chunk sizes */
#define BOTAN_DEFAULT_BUFFER_SIZE 4096
#define BOTAN_MEM_POOL_CHUNK_SIZE 64*1024
/* BigInt toggles */
#define BOTAN_MP_WORD_BITS 32
#define BOTAN_KARAT_MUL_THRESHOLD 32
#define BOTAN_KARAT_SQR_THRESHOLD 32
#define BOTAN_PRIVATE_KEY_OP_BLINDING_BITS 64
/* PK key consistency checking toggles */
#define BOTAN_PUBLIC_KEY_STRONG_CHECKS_ON_LOAD 1
#define BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_LOAD 1
#define BOTAN_PRIVATE_KEY_STRONG_CHECKS_ON_GENERATE 1
/* Should we use GCC-style inline assembler? */
#if !defined(BOTAN_USE_GCC_INLINE_ASM) && defined(__GNUG__)
#define BOTAN_USE_GCC_INLINE_ASM 1
#endif
#ifndef BOTAN_USE_GCC_INLINE_ASM
#define BOTAN_USE_GCC_INLINE_ASM 0
#endif
/* Target identification and feature test macros */
#define BOTAN_TARGET_OS_IS_WINDOWS
#define BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK
#define BOTAN_TARGET_ARCH_IS_IA64
#define BOTAN_TARGET_UNALIGNED_LOADSTOR_OK 0
/* Module definitions */
#define BOTAN_HAS_ADLER32
#define BOTAN_HAS_AES
#define BOTAN_HAS_ALGORITHM_FACTORY
#define BOTAN_HAS_ANSI_X919_MAC
#define BOTAN_HAS_ARC4
#define BOTAN_HAS_ASN1
#define BOTAN_HAS_AUTO_SEEDING_RNG
#define BOTAN_HAS_BASE64_CODEC
#define BOTAN_HAS_BIGINT
#define BOTAN_HAS_BIGINT_MATH
#define BOTAN_HAS_BLOCK_CIPHER
#define BOTAN_HAS_BLOWFISH
#define BOTAN_HAS_CAST
#define BOTAN_HAS_CBC
#define BOTAN_HAS_CBC_MAC
#define BOTAN_HAS_CFB
#define BOTAN_HAS_CIPHER_MODEBASE
#define BOTAN_HAS_CIPHER_MODE_PADDING
#define BOTAN_HAS_CMAC
#define BOTAN_HAS_CMS
#define BOTAN_HAS_CRC24
#define BOTAN_HAS_CRC32
#define BOTAN_HAS_CRYPTO_BOX
#define BOTAN_HAS_CTR
#define BOTAN_HAS_CTS
#define BOTAN_HAS_DEFAULT_ENGINE
#define BOTAN_HAS_DES
#define BOTAN_HAS_DIFFIE_HELLMAN
#define BOTAN_HAS_DLIES
#define BOTAN_HAS_DL_GROUP
#define BOTAN_HAS_DL_PUBLIC_KEY_FAMILY
#define BOTAN_HAS_DSA
#define BOTAN_HAS_EAX
#define BOTAN_HAS_ECB
#define BOTAN_HAS_ELGAMAL
#define BOTAN_HAS_EME1
#define BOTAN_HAS_EME_PKCS1v15
#define BOTAN_HAS_EMSA1
#define BOTAN_HAS_EMSA1_BSI
#define BOTAN_HAS_EMSA2
#define BOTAN_HAS_EMSA3
#define BOTAN_HAS_EMSA4
#define BOTAN_HAS_EMSA_RAW
#define BOTAN_HAS_ENGINES
#define BOTAN_HAS_ENTROPY_SRC_CAPI
#define BOTAN_HAS_ENTROPY_SRC_WIN32
#define BOTAN_HAS_FILTERS
#define BOTAN_HAS_FORK_256
#define BOTAN_HAS_GOST_28147_89
#define BOTAN_HAS_GOST_34_11
#define BOTAN_HAS_HASH_ID
#define BOTAN_HAS_HAS_160
#define BOTAN_HAS_HEX_CODEC
#define BOTAN_HAS_HMAC
#define BOTAN_HAS_HMAC_RNG
#define BOTAN_HAS_IDEA
#define BOTAN_HAS_IF_PUBLIC_KEY_FAMILY
#define BOTAN_HAS_KASUMI
#define BOTAN_HAS_KDF1
#define BOTAN_HAS_KDF2
#define BOTAN_HAS_KDF_BASE
#define BOTAN_HAS_KEYPAIR_TESTING
#define BOTAN_HAS_LIBSTATE_MODULE
#define BOTAN_HAS_LION
#define BOTAN_HAS_LUBY_RACKOFF
#define BOTAN_HAS_MARS
#define BOTAN_HAS_MD2
#define BOTAN_HAS_MD4
#define BOTAN_HAS_MD5
#define BOTAN_HAS_MDX_HASH_FUNCTION
#define BOTAN_HAS_MGF1
#define BOTAN_HAS_MISTY1
#define BOTAN_HAS_MUTEX_NOOP
#define BOTAN_HAS_MUTEX_WIN32
#define BOTAN_HAS_MUTEX_WRAPPERS
#define BOTAN_HAS_NOEKEON
#define BOTAN_HAS_NYBERG_RUEPPEL
#define BOTAN_HAS_OFB
#define BOTAN_HAS_OID_LOOKUP
#define BOTAN_HAS_OPENPGP_CODEC
#define BOTAN_HAS_PARALLEL_HASH
#define BOTAN_HAS_PASSWORD_BASED_ENCRYPTION
#define BOTAN_HAS_PBE_PKCS_V15
#define BOTAN_HAS_PBE_PKCS_V20
#define BOTAN_HAS_PBKDF1
#define BOTAN_HAS_PBKDF2
#define BOTAN_HAS_PEM_CODEC
#define BOTAN_HAS_PGPS2K
#define BOTAN_HAS_PK_PADDING
#define BOTAN_HAS_PUBLIC_KEY_CRYPTO
#define BOTAN_HAS_RANDPOOL
#define BOTAN_HAS_RC2
#define BOTAN_HAS_RC5
#define BOTAN_HAS_RC6
#define BOTAN_HAS_RIPEMD_128
#define BOTAN_HAS_RIPEMD_160
#define BOTAN_HAS_RSA
#define BOTAN_HAS_RUNTIME_BENCHMARKING
#define BOTAN_HAS_RW
#define BOTAN_HAS_SAFER
#define BOTAN_HAS_SALSA20
#define BOTAN_HAS_SEED
#define BOTAN_HAS_SELFTESTS
#define BOTAN_HAS_SERPENT
#define BOTAN_HAS_SHA1
#define BOTAN_HAS_SHA2
#define BOTAN_HAS_SKEIN_512
#define BOTAN_HAS_SKIPJACK
#define BOTAN_HAS_SQUARE
#define BOTAN_HAS_SSL3_MAC
#define BOTAN_HAS_SSL_V3_PRF
#define BOTAN_HAS_STREAM_CIPHER
#define BOTAN_HAS_TEA
#define BOTAN_HAS_TIGER
#define BOTAN_HAS_TIMER
#define BOTAN_HAS_TIMER_WIN32
#define BOTAN_HAS_TLS_V10_PRF
#define BOTAN_HAS_TURING
#define BOTAN_HAS_TWOFISH
#define BOTAN_HAS_UTIL_FUNCTIONS
#define BOTAN_HAS_WHIRLPOOL
#define BOTAN_HAS_WID_WAKE
#define BOTAN_HAS_X509
#define BOTAN_HAS_X931_RNG
#define BOTAN_HAS_X942_PRF
#define BOTAN_HAS_XTEA
#define BOTAN_HAS_XTS
/* Local configuration options */
/*
kheimric@deepburner ran 'E:\dev\creator\src\libs\3rdparty\botan\configure.py --cc=msvc --os=windows --cpu=ia64 --disable-asm'
Target
-------
Compiler: cl.exe /O2
Arch: ia64/ia64
OS: windows
Modules
-------
adler32 (Adler32)
aes (AES)
algo_factory (Algorithm Factory)
alloc (Allocator)
arc4 (ARC4)
asn1 (ASN.1/BER/DER module)
auto_rng (Auto-seeded Random Number Generator)
base64 (Base64 Codec)
benchmark (Benchmarking)
bigint (BigInt)
block (Block Ciphers)
blowfish (Blowfish)
buf_comp (Buffered Computation)
cast (CAST)
cbc (CBC block cipher mode)
cbc_mac (CBC-MAC)
cfb (CFB block cipher mode)
cmac (CMAC)
cms (CMS)
crc24 (CRC-24)
crc32 (CRC-32)
cryptoapi_rng (Win32 CryptoAPI Entropy Source)
cryptobox (Crypto Box)
ctr (CTR block cipher mode)
cts (CTS block cipher mode)
datastor (Datastore)
def_engine (Default Engine)
des (DES)
dh (Diffie-Hellman Key Agreement)
dl_algo (Discrete Logarithm PK Algorithms)
dl_group (DL Group)
dlies (DLIES)
dsa (DSA)
eax (EAX block cipher mode)
ecb (ECB block cipher mode)
elgamal (ElGamal)
eme1 (EME1)
eme_pkcs (PKCSv1 v1.5 EME)
emsa1 (EMSA1)
emsa1_bsi (EMSA1 (BSI variant))
emsa2 (EMSA2)
emsa3 (EMSA3)
emsa4 (EMSA4)
emsa_raw (EMSA-Raw)
engine (Engines)
entropy (Entropy Sources)
filters (Pipe/Filter)
fork256 (FORK-256)
gost_28147 (GOST 28147-89)
gost_3411 (GOST 34.11)
has160 (HAS-160)
hash (Hash Functions)
hash_id (Hash Function Identifiers)
hex (Hex Codec)
hmac (HMAC)
hmac_rng (HMAC RNG)
idea (IDEA)
if_algo (Integer Factorization Algorithms)
kasumi (Kasumi)
kdf (KDF Base Class)
kdf1 (KDF1)
kdf2 (KDF2)
keypair (Keypair Testing)
libstate (Botan Libstate Module)
lion (Lion)
lubyrack (Luby-Rackoff)
mac (Message Authentication Codes)
mars (MARS)
md2 (MD2)
md4 (MD4)
md5 (MD5)
mdx_hash (MDx Hash Base)
mem_pool (Memory Pool Allocator)
mgf1 (MGF1)
misty1 (MISTY-1)
mode_pad (Cipher Mode Padding Method)
modes (Cipher Mode Base Class)
monty_generic (Montgomery Reduction)
mp_generic (MPI Core (C++))
mulop_generic (BigInt Multiply-Add)
mutex (Mutex Wrappers)
noekeon (Noekeon)
noop_mutex (No-Op Mutex)
nr (Nyberg-Rueppel)
numbertheory (Math Functions)
ofb (OFB block cipher mode)
oid_lookup (OID Lookup)
openpgp (OpenPGP Codec)
par_hash (Parallel Hash)
pbe (PBE Base)
pbes1 (PKCS5 v1.5 PBE)
pbes2 (PKCS5 v2.0 PBE)
pbkdf1 (Pbkdf1)
pbkdf2 (Pbkdf2)
pem (PEM Codec)
pgps2k (Pgps2k)
pk_codecs (PK codecs (PKCS8, X.509))
pk_pad (Public Key EME/EMSA Padding Modes)
pubkey (Public Key Base)
randpool (Randpool RNG)
rc2 (RC2)
rc5 (RC5)
rc6 (RC6)
rmd128 (RIPEMD-128)
rmd160 (RIPEMD-160)
rng (Random Number Generators)
rsa (RSA)
rw (Rabin-Williams)
s2k (String to Key Functions)
safer (SAFER)
salsa20 (Salsa20)
seed (SEED)
selftest (Selftests)
serpent (Serpent)
sha1 (SHA-1)
sha2 (SHA-2 (224, 256, 384, 512))
skein (Skein)
skipjack (Skipjack)
square (Square)
ssl3mac (SSLv3 MAC)
ssl_prf (SSLv3 PRF)
stream (Stream Ciphers)
sym_algo (Symmetric Algorithms)
system_alloc (Default (Malloc) Allocators)
tea (TEA)
tiger (Tiger)
timer (Timer Base Class)
tls_prf (TLS v1.0 PRF)
turing (Turing)
twofish (Twofish)
utils (Utility Functions)
whirlpool (Whirlpool)
wid_wake (WiderWake)
win32_crit_section (Win32 Mutex)
win32_query_perf_ctr (Win32 Timer)
win32_stats (Win32 Entropy Source)
x509 (X.509)
x919_mac (ANSI X9.19 MAC)
x931_rng (ANSI X9.31 PRNG)
x942_prf (X942 PRF)
xtea (XTEA)
xts (XTS block cipher mode)
*/
#endif

View File

@@ -0,0 +1,47 @@
/*
* CAST-128
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CAST128_H__
#define BOTAN_CAST128_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* CAST-128
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
static void key_schedule(u32bit[16], u32bit[4]);
static const u32bit S5[256];
static const u32bit S6[256];
static const u32bit S7[256];
static const u32bit 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

View File

@@ -0,0 +1,44 @@
/*
* CAST-256
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CAST256_H__
#define BOTAN_CAST256_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* CAST-256
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
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

View File

@@ -0,0 +1,55 @@
/*
* CBC Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CBC_H__
#define BOTAN_CBC_H__
#include <botan/modebase.h>
#include <botan/mode_pad.h>
namespace Botan {
/*
* CBC Encryption
*/
class BOTAN_DLL CBC_Encryption : public BlockCipherMode
{
public:
CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*);
CBC_Encryption(BlockCipher*, BlockCipherModePaddingMethod*,
const SymmetricKey&, const InitializationVector&);
~CBC_Encryption() { delete padder; }
private:
std::string name() const;
void write(const byte[], u32bit);
void end_msg();
const BlockCipherModePaddingMethod* padder;
};
/*
* CBC Decryption
*/
class BOTAN_DLL CBC_Decryption : public BlockCipherMode
{
public:
CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*);
CBC_Decryption(BlockCipher*, BlockCipherModePaddingMethod*,
const SymmetricKey&, const InitializationVector&);
~CBC_Decryption() { delete padder; }
private:
std::string name() const;
void write(const byte[], u32bit);
void end_msg();
const BlockCipherModePaddingMethod* padder;
SecureVector<byte> temp;
};
}
#endif

View File

@@ -0,0 +1,40 @@
/*
* CBC-MAC
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CBC_MAC_H__
#define BOTAN_CBC_MAC_H__
#include <botan/mac.h>
#include <botan/block_cipher.h>
namespace Botan {
/*
* CBC-MAC
*/
class BOTAN_DLL CBC_MAC : public MessageAuthenticationCode
{
public:
void clear() throw();
std::string name() const;
MessageAuthenticationCode* clone() const;
CBC_MAC(BlockCipher* e);
~CBC_MAC();
private:
void add_data(const byte[], u32bit);
void final_result(byte[]);
void key_schedule(const byte[], u32bit);
BlockCipher* e;
SecureVector<byte> state;
u32bit position;
};
}
#endif

View File

@@ -0,0 +1,39 @@
/*
* Certificate Store
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#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 BOTAN_DLL 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

View File

@@ -0,0 +1,47 @@
/*
* CFB Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CFB_H__
#define BOTAN_CFB_H__
#include <botan/modebase.h>
namespace Botan {
/*
* CFB Encryption
*/
class BOTAN_DLL CFB_Encryption : public BlockCipherMode
{
public:
CFB_Encryption(BlockCipher*, u32bit = 0);
CFB_Encryption(BlockCipher*, const SymmetricKey&,
const InitializationVector&, u32bit = 0);
private:
void write(const byte[], u32bit);
void feedback();
const u32bit FEEDBACK_SIZE;
};
/*
* CFB Decryption
*/
class BOTAN_DLL CFB_Decryption : public BlockCipherMode
{
public:
CFB_Decryption(BlockCipher*, u32bit = 0);
CFB_Decryption(BlockCipher*, const SymmetricKey&,
const InitializationVector&, u32bit = 0);
private:
void write(const byte[], u32bit);
void feedback();
const u32bit FEEDBACK_SIZE;
};
}
#endif

View File

@@ -0,0 +1,44 @@
/*
* Character Set Handling
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CHARSET_H__
#define BOTAN_CHARSET_H__
#include <botan/types.h>
#include <string>
namespace Botan {
/**
* The different charsets (nominally) supported by Botan.
*/
enum Character_Set {
LOCAL_CHARSET,
UCS2_CHARSET,
UTF8_CHARSET,
LATIN1_CHARSET
};
namespace Charset {
/*
* Character Set Handling
*/
std::string transcode(const std::string&, Character_Set, Character_Set);
bool is_digit(char);
bool is_space(char);
bool caseless_cmp(char, char);
byte char2digit(char);
char digit2char(byte);
}
}
#endif

View File

@@ -0,0 +1,44 @@
/*
* CMAC
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CMAC_H__
#define BOTAN_CMAC_H__
#include <botan/mac.h>
#include <botan/block_cipher.h>
namespace Botan {
/*
* CMAC
*/
class BOTAN_DLL CMAC : public MessageAuthenticationCode
{
public:
void clear() throw();
std::string name() const;
MessageAuthenticationCode* clone() const;
static SecureVector<byte> poly_double(const MemoryRegion<byte>& in,
byte polynomial);
CMAC(BlockCipher* e);
~CMAC();
private:
void add_data(const byte[], u32bit);
void final_result(byte[]);
void key_schedule(const byte[], u32bit);
BlockCipher* e;
SecureVector<byte> buffer, state, B, P;
u32bit position;
byte polynomial;
};
}
#endif

View File

@@ -0,0 +1,65 @@
/*
* CMS Decoding
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CMS_DECODER_H__
#define BOTAN_CMS_DECODER_H__
#include <botan/x509cert.h>
#include <botan/x509stor.h>
#include <botan/pkcs8.h>
#include <botan/ber_dec.h>
#include <botan/ui.h>
namespace Botan {
/*
* CMS Decoding Operation
*/
class BOTAN_DLL CMS_Decoder
{
public:
enum Status { GOOD, BAD, NO_KEY, FAILURE };
enum Content_Type { DATA, UNKNOWN, COMPRESSED, ENVELOPED, SIGNED,
AUTHENTICATED, DIGESTED };
Status layer_status() const;
Content_Type layer_type() const;
std::string layer_info() const;
std::string layer_algo() const;
std::string get_data() const;
std::vector<X509_Certificate> get_certs() const;
std::vector<X509_CRL> get_crls() const;
void next_layer() { decode_layer(); }
void add_key(PKCS8_PrivateKey*);
CMS_Decoder(DataSource&, const X509_Store&, User_Interface&,
PKCS8_PrivateKey* = 0);
private:
std::string get_passphrase(const std::string&);
void read_econtent(BER_Decoder&);
void initial_read(DataSource&);
void decode_layer();
void decompress(BER_Decoder&);
User_Interface& ui;
X509_Store store;
std::vector<std::string> passphrases;
std::vector<PKCS8_PrivateKey*> keys;
OID type, next_type;
SecureVector<byte> data;
Status status;
std::string info;
};
}
#endif

View File

@@ -0,0 +1,92 @@
/*
* CMS Encoding
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CMS_ENCODER_H__
#define BOTAN_CMS_ENCODER_H__
#include <botan/x509cert.h>
#include <botan/x509stor.h>
#include <botan/pkcs8.h>
#include <botan/symkey.h>
namespace Botan {
/*
* CMS Encoding Operation
*/
class BOTAN_DLL CMS_Encoder
{
public:
void encrypt(RandomNumberGenerator&,
const X509_Certificate&, const std::string = "");
void encrypt(RandomNumberGenerator& rng,
const std::string&, const std::string& = "");
void encrypt(RandomNumberGenerator& rng,
const SymmetricKey&, const std::string& = "");
void authenticate(const X509_Certificate&, const std::string& = "");
void authenticate(const std::string&, const std::string& = "");
void authenticate(const SymmetricKey&, const std::string& = "");
void sign(const X509_Certificate& cert,
const PKCS8_PrivateKey& key,
RandomNumberGenerator& rng,
const std::vector<X509_Certificate>& cert_chain,
const std::string& hash,
const std::string& padding);
void digest(const std::string& = "");
void compress(const std::string&);
static bool can_compress_with(const std::string&);
SecureVector<byte> get_contents();
std::string PEM_contents();
void set_data(const std::string&);
void set_data(const byte[], u32bit);
CMS_Encoder(const std::string& str) { set_data(str); }
CMS_Encoder(const byte buf[], u32bit length) { set_data(buf, length); }
private:
void add_layer(const std::string&, DER_Encoder&);
void encrypt_ktri(RandomNumberGenerator&,
const X509_Certificate&, PK_Encrypting_Key*,
const std::string&);
void encrypt_kari(RandomNumberGenerator&,
const X509_Certificate&, X509_PublicKey*,
const std::string&);
SecureVector<byte> do_encrypt(RandomNumberGenerator& rng,
const SymmetricKey&, const std::string&);
static SecureVector<byte> make_econtent(const SecureVector<byte>&,
const std::string&);
static SymmetricKey setup_key(RandomNumberGenerator& rng,
const std::string&);
static SecureVector<byte> wrap_key(RandomNumberGenerator& rng,
const std::string&,
const SymmetricKey&,
const SymmetricKey&);
static SecureVector<byte> encode_params(const std::string&,
const SymmetricKey&,
const InitializationVector&);
SecureVector<byte> data;
std::string type;
};
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* CRC24
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CRC24_H__
#define BOTAN_CRC24_H__
#include <botan/hash.h>
namespace Botan {
/*
* CRC24
*/
class BOTAN_DLL 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

View File

@@ -0,0 +1,34 @@
/*
* CRC32
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CRC32_H__
#define BOTAN_CRC32_H__
#include <botan/hash.h>
namespace Botan {
/*
* CRC32
*/
class BOTAN_DLL 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

View File

@@ -0,0 +1,78 @@
/*
* CRL Entry
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CRL_ENTRY_H__
#define BOTAN_CRL_ENTRY_H__
#include <botan/x509cert.h>
namespace Botan {
/**
* This class represents CRL entries
*/
class BOTAN_DLL CRL_Entry : public ASN1_Object
{
public:
void encode_into(class DER_Encoder&) const;
void decode_from(class BER_Decoder&);
/**
* Get the serial number of the certificate associated with this entry.
* @return the certificate's serial number
*/
MemoryVector<byte> serial_number() const { return serial; }
/**
* Get the revocation date of the certificate associated with this entry
* @return the certificate's revocation date
*/
X509_Time expire_time() const { return time; }
/**
* Get the entries reason code
* @return the reason code
*/
CRL_Code reason_code() const { return reason; }
/**
* Construct an empty CRL entry.
*/
CRL_Entry(bool throw_on_unknown_critical_extension = false);
/**
* Construct an CRL entry.
* @param cert the certificate to revoke
* @param reason the reason code to set in the entry
*/
CRL_Entry(const X509_Certificate&, CRL_Code = UNSPECIFIED);
private:
bool throw_on_unknown_critical;
MemoryVector<byte> serial;
X509_Time time;
CRL_Code reason;
};
/**
* Test two CRL entries for equality in all fields.
*/
BOTAN_DLL bool operator==(const CRL_Entry&, const CRL_Entry&);
/**
* Test two CRL entries for inequality in at least one field.
*/
BOTAN_DLL bool operator!=(const CRL_Entry&, const CRL_Entry&);
/**
* Order two entries based on the revocation date.
*/
BOTAN_DLL bool operator<(const CRL_Entry&, const CRL_Entry&);
}
#endif

View File

@@ -0,0 +1,42 @@
/*
* Cryptobox Message Routines
* (C) 2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CRYPTOBOX_H__
#define BOTAN_CRYPTOBOX_H__
#include <string>
#include <botan/rng.h>
namespace Botan {
namespace CryptoBox {
/**
* Encrypt a message
* @param input the input data
* @param input_len the length of input in bytes
* @param passphrase the passphrase used to encrypt the message
* @param rng a ref to a random number generator, such as AutoSeeded_RNG
*/
BOTAN_DLL std::string encrypt(const byte input[], u32bit input_len,
const std::string& passphrase,
RandomNumberGenerator& rng);
/**
* Decrypt a message encrypted with CryptoBox::encrypt
* @param input the input data
* @param input_len the length of input in bytes
* @param passphrase the passphrase used to encrypt the message
*/
BOTAN_DLL std::string decrypt(const byte input[], u32bit input_len,
const std::string& passphrase);
}
}
#endif

View File

@@ -0,0 +1,31 @@
/*
* CTR Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_COUNTER_MODE_H__
#define BOTAN_COUNTER_MODE_H__
#include <botan/modebase.h>
#include <botan/modebase.h>
namespace Botan {
/*
* CTR-BE Mode
*/
class BOTAN_DLL CTR_BE : public BlockCipherMode
{
public:
CTR_BE(BlockCipher*);
CTR_BE(BlockCipher*, const SymmetricKey&, const InitializationVector&);
private:
void write(const byte[], u32bit);
void increment_counter();
};
}
#endif

View File

@@ -0,0 +1,60 @@
/*
* CTS Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_CTS_H__
#define BOTAN_CTS_H__
#include <botan/modebase.h>
#include <botan/block_cipher.h>
namespace Botan {
/*
* CTS Encryption
*/
class BOTAN_DLL CTS_Encryption : public BlockCipherMode
{
public:
CTS_Encryption(BlockCipher* ciph) :
BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2) {}
CTS_Encryption(BlockCipher* ciph,
const SymmetricKey& key,
const InitializationVector& iv) :
BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
{ set_key(key); set_iv(iv); }
private:
void write(const byte[], u32bit);
void end_msg();
void encrypt(const byte[]);
};
/*
* CTS Decryption
*/
class BOTAN_DLL CTS_Decryption : public BlockCipherMode
{
public:
CTS_Decryption(BlockCipher* ciph) :
BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
{ temp.create(BLOCK_SIZE); }
CTS_Decryption(BlockCipher* ciph,
const SymmetricKey& key,
const InitializationVector& iv) :
BlockCipherMode(ciph, "CTS", ciph->BLOCK_SIZE, 0, 2)
{ set_key(key); set_iv(iv); temp.create(BLOCK_SIZE); }
private:
void write(const byte[], u32bit);
void end_msg();
void decrypt(const byte[]);
SecureVector<byte> temp;
};
}
#endif

View File

@@ -0,0 +1,65 @@
/*
* DataSink
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DATA_SINK_H__
#define BOTAN_DATA_SINK_H__
#include <botan/filter.h>
#include <iosfwd>
namespace Botan {
/**
* This class represents abstract data sink objects.
*/
class BOTAN_DLL DataSink : public Filter
{
public:
bool attachable() { return false; }
DataSink() {}
virtual ~DataSink() {}
private:
DataSink& operator=(const DataSink&) { return (*this); }
DataSink(const DataSink&);
};
/**
* This class represents a data sink which writes its output to a stream.
*/
class BOTAN_DLL DataSink_Stream : public DataSink
{
public:
void write(const byte[], u32bit);
/**
* Construct a DataSink_Stream from a stream.
* @param stream the stream to write to
* @param name identifier
*/
DataSink_Stream(std::ostream& stream,
const std::string& name = "");
/**
* Construct a DataSink_Stream from a stream.
* @param file the name of the file to open a stream to
* @param use_binary indicates whether to treat the file
* as a binary file or not
*/
DataSink_Stream(const std::string& filename,
bool use_binary = false);
~DataSink_Stream();
private:
const std::string identifier;
const bool owner;
std::ostream* sink;
};
}
#endif

View File

@@ -0,0 +1,150 @@
/*
* DataSource
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DATA_SRC_H__
#define BOTAN_DATA_SRC_H__
#include <botan/secmem.h>
#include <string>
#include <iosfwd>
namespace Botan {
/**
* This class represents an abstract data source object.
*/
class BOTAN_DLL DataSource
{
public:
/**
* Read from the source. Moves the internal offset so that
* every call to read will return a new portion of the source.
* @param out the byte array to write the result to
* @param length the length of the byte array out
* @return the length in bytes that was actually read and put
* into out
*/
virtual u32bit read(byte out[], u32bit length) = 0;
/**
* Read from the source but do not modify the internal offset. Consecutive
* calls to peek() will return portions of the source starting at the same
* position.
* @param out the byte array to write the output to
* @param length the length of the byte array out
* @return the length in bytes that was actually read and put
* into out
*/
virtual u32bit peek(byte out[], u32bit length,
u32bit peek_offset) const = 0;
/**
* Test whether the source still has data that can be read.
* @return true if there is still data to read, false otherwise
*/
virtual bool end_of_data() const = 0;
/**
* return the id of this data source
* @return the std::string representing the id of this data source
*/
virtual std::string id() const { return ""; }
/**
* Read one byte.
* @param the byte to read to
* @return the length in bytes that was actually read and put
* into out
*/
u32bit read_byte(byte& out);
/**
* Peek at one byte.
* @param the byte to read to
* @return the length in bytes that was actually read and put
* into out
*/
u32bit peek_byte(byte& out) const;
/**
* Discard the next N bytes of the data
* @param N the number of bytes to discard
* @return the number of bytes actually discarded
*/
u32bit discard_next(u32bit N);
DataSource() {}
virtual ~DataSource() {}
private:
DataSource& operator=(const DataSource&) { return (*this); }
DataSource(const DataSource&);
};
/**
* This class represents a Memory-Based DataSource
*/
class BOTAN_DLL DataSource_Memory : public DataSource
{
public:
u32bit read(byte[], u32bit);
u32bit peek(byte[], u32bit, u32bit) const;
bool end_of_data() const;
/**
* Construct a memory source that reads from a string
* @param in the string to read from
*/
DataSource_Memory(const std::string& in);
/**
* Construct a memory source that reads from a byte array
* @param in the byte array to read from
* @param length the length of the byte array
*/
DataSource_Memory(const byte in[], u32bit length);
/**
* Construct a memory source that reads from a MemoryRegion
* @param in the MemoryRegion to read from
*/
DataSource_Memory(const MemoryRegion<byte>& in);
private:
SecureVector<byte> source;
u32bit offset;
};
/**
* This class represents a Stream-Based DataSource.
*/
class BOTAN_DLL 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(std::istream&, const std::string& id = "");
/**
* Construct a Stream-Based DataSource from file
* @param file the name of the file
* @param use_binary whether to treat the file as binary or not
*/
DataSource_Stream(const std::string& file, bool use_binary = false);
~DataSource_Stream();
private:
const std::string identifier;
const bool owner;
std::istream* source;
u32bit total_read;
};
}
#endif

View File

@@ -0,0 +1,61 @@
/*
* Data Store
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#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 BOTAN_DLL Data_Store
{
public:
class BOTAN_DLL 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>
search_with(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;
bool has_value(const std::string&) 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

View File

@@ -0,0 +1,83 @@
/*
* Default Engine
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DEFAULT_ENGINE_H__
#define BOTAN_DEFAULT_ENGINE_H__
#include <botan/engine.h>
namespace Botan {
/*
* Default Engine
*/
class BOTAN_DLL Default_Engine : public Engine
{
public:
std::string provider_name() const { return "core"; }
#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY)
IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&) const;
#endif
#if defined(BOTAN_HAS_DSA)
DSA_Operation* dsa_op(const DL_Group&, const BigInt&,
const BigInt&) const;
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
NR_Operation* nr_op(const DL_Group&, const BigInt&, const BigInt&) const;
#endif
#if defined(BOTAN_HAS_ELGAMAL)
ELG_Operation* elg_op(const DL_Group&, const BigInt&,
const BigInt&) const;
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
DH_Operation* dh_op(const DL_Group&, const BigInt&) const;
#endif
#if defined(BOTAN_HAS_ECDSA)
virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&,
const BigInt&,
const PointGFp&) const;
#endif
#if defined(BOTAN_HAS_ECKAEG)
virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&,
const BigInt&,
const PointGFp&) const;
#endif
Modular_Exponentiator* mod_exp(const BigInt&,
Power_Mod::Usage_Hints) const;
virtual bool can_add_algorithms() { return true; }
Keyed_Filter* get_cipher(const std::string&, Cipher_Dir,
Algorithm_Factory&);
private:
BlockCipher* find_block_cipher(const SCAN_Name&,
Algorithm_Factory&) const;
StreamCipher* find_stream_cipher(const SCAN_Name&,
Algorithm_Factory&) const;
HashFunction* find_hash(const SCAN_Name& reqeust,
Algorithm_Factory&) const;
MessageAuthenticationCode* find_mac(const SCAN_Name& reqeust,
Algorithm_Factory&) const;
};
}
#endif

View File

@@ -0,0 +1,64 @@
/*
* Modular Exponentiation
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#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 BOTAN_DLL 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 BOTAN_DLL 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

View File

@@ -0,0 +1,43 @@
/*
* Basic Allocators
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BASIC_ALLOC_H__
#define BOTAN_BASIC_ALLOC_H__
#include <botan/mem_pool.h>
namespace Botan {
/*
* Malloc Allocator
*/
class BOTAN_DLL Malloc_Allocator : public Allocator
{
public:
void* allocate(u32bit);
void deallocate(void*, u32bit);
std::string type() const { return "malloc"; }
};
/*
* Locking Allocator
*/
class BOTAN_DLL Locking_Allocator : public Pooling_Allocator
{
public:
Locking_Allocator(Mutex* m) : Pooling_Allocator(m) {}
std::string type() const { return "locking"; }
private:
void* alloc_block(u32bit);
void dealloc_block(void*, u32bit);
};
}
#endif

View File

@@ -0,0 +1,91 @@
/*
* DER Encoder
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DER_ENCODER_H__
#define BOTAN_DER_ENCODER_H__
#include <botan/asn1_int.h>
#include <vector>
namespace Botan {
/*
* General DER Encoding Object
*/
class BOTAN_DLL DER_Encoder
{
public:
SecureVector<byte> get_contents();
DER_Encoder& start_cons(ASN1_Tag, ASN1_Tag = UNIVERSAL);
DER_Encoder& end_cons();
DER_Encoder& start_explicit(u16bit);
DER_Encoder& end_explicit();
DER_Encoder& raw_bytes(const byte[], u32bit);
DER_Encoder& raw_bytes(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);
template<typename T>
DER_Encoder& encode_optional(const T& value, const T& default_value)
{
if(value != default_value)
encode(value);
return (*this);
}
template<typename T>
DER_Encoder& encode_list(const std::vector<T>& values)
{
for(u32bit j = 0; j != values.size(); ++j)
encode(values[j]);
return (*this);
}
DER_Encoder& encode(const class ASN1_Object&);
DER_Encoder& encode_if(bool, DER_Encoder&);
DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const byte[], u32bit);
DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const MemoryRegion<byte>&);
DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, const std::string&);
DER_Encoder& add_object(ASN1_Tag, ASN1_Tag, byte);
private:
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);
private:
ASN1_Tag type_tag, class_tag;
SecureVector<byte> contents;
std::vector< SecureVector<byte> > set_contents;
};
SecureVector<byte> contents;
std::vector<DER_Sequence> subsequences;
};
}
#endif

View File

@@ -0,0 +1,70 @@
/*
* DES
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DES_H__
#define BOTAN_DES_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* DES
*/
class BOTAN_DLL 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:
void enc(const byte[], byte[]) const;
void dec(const byte[], byte[]) const;
void key_schedule(const byte[], u32bit);
SecureBuffer<u32bit, 32> round_key;
};
/*
* Triple DES
*/
class BOTAN_DLL TripleDES : public BlockCipher
{
public:
void clear() throw() { round_key.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_schedule(const byte[], u32bit);
SecureBuffer<u32bit, 96> round_key;
};
/*
* DES Tables
*/
extern const u32bit DES_SPBOX1[256];
extern const u32bit DES_SPBOX2[256];
extern const u32bit DES_SPBOX3[256];
extern const u32bit DES_SPBOX4[256];
extern const u32bit DES_SPBOX5[256];
extern const u32bit DES_SPBOX6[256];
extern const u32bit DES_SPBOX7[256];
extern const u32bit DES_SPBOX8[256];
extern const u64bit DES_IPTAB1[256];
extern const u64bit DES_IPTAB2[256];
extern const u64bit DES_FPTAB1[256];
extern const u64bit DES_FPTAB2[256];
}
#endif

View File

@@ -0,0 +1,35 @@
/*
* DESX
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DESX_H__
#define BOTAN_DESX_H__
#include <botan/des.h>
namespace Botan {
/*
* DESX
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
SecureBuffer<byte, 8> K1, K2;
DES des;
};
}
#endif

View File

@@ -0,0 +1,80 @@
/*
* Diffie-Hellman
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DIFFIE_HELLMAN_H__
#define BOTAN_DIFFIE_HELLMAN_H__
#include <botan/dl_algo.h>
#include <botan/dh_core.h>
namespace Botan {
/**
* This class represents Diffie-Hellman public keys.
*/
class BOTAN_DLL DH_PublicKey : public virtual DL_Scheme_PublicKey
{
public:
std::string algo_name() const { return "DH"; }
MemoryVector<byte> public_value() const;
u32bit max_input_bits() const;
DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; }
/**
* Construct an uninitialized key. Use this constructor if you wish
* to decode an encoded key into the new instance.
*/
DH_PublicKey() {}
/**
* Construct a public key with the specified parameters.
* @param grp the DL group to use in the key
* @param y the public value y
*/
DH_PublicKey(const DL_Group& grp, const BigInt& y);
private:
void X509_load_hook();
};
/**
* This class represents Diffie-Hellman private keys.
*/
class BOTAN_DLL DH_PrivateKey : public DH_PublicKey,
public PK_Key_Agreement_Key,
public virtual DL_Scheme_PrivateKey
{
public:
SecureVector<byte> derive_key(const byte other[], u32bit length) const;
SecureVector<byte> derive_key(const DH_PublicKey& other) const;
SecureVector<byte> derive_key(const BigInt& other) const;
MemoryVector<byte> public_value() const;
/**
* Construct an uninitialized key. Use this constructor if you wish
* to decode an encoded key into the new instance.
*/
DH_PrivateKey() {}
/**
* Construct a private key with predetermined value.
* @param rng random number generator to use
* @param grp the group to be used in the key
* @param x the key's secret value (or if zero, generate a new key)
*/
DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp,
const BigInt& x = 0);
private:
void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false);
DH_Core core;
};
}
#endif

View File

@@ -0,0 +1,38 @@
/*
* DH Core
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DH_CORE_H__
#define BOTAN_DH_CORE_H__
#include <botan/dh_op.h>
#include <botan/blinding.h>
namespace Botan {
/*
* DH Core
*/
class BOTAN_DLL 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(RandomNumberGenerator& rng,
const DL_Group&, const BigInt&);
~DH_Core() { delete op; }
private:
DH_Operation* op;
Blinder blinder;
};
}
#endif

View File

@@ -0,0 +1,45 @@
/*
* DH Operations
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DH_OPS_H__
#define BOTAN_DH_OPS_H__
#include <botan/dl_group.h>
#include <botan/reducer.h>
#include <botan/pow_mod.h>
namespace Botan {
/*
* DH Operation Interface
*/
class BOTAN_DLL DH_Operation
{
public:
virtual BigInt agree(const BigInt&) const = 0;
virtual DH_Operation* clone() const = 0;
virtual ~DH_Operation() {}
};
/*
* Botan's Default DH Operation
*/
class BOTAN_DLL Default_DH_Op : public DH_Operation
{
public:
BigInt agree(const BigInt& i) const { return powermod_x_p(i); }
DH_Operation* clone() const { return new Default_DH_Op(*this); }
Default_DH_Op(const DL_Group& group, const BigInt& x) :
powermod_x_p(x, group.get_p()) {}
private:
Fixed_Exponent_Power_Mod powermod_x_p;
};
}
#endif

View File

@@ -0,0 +1,19 @@
/*
* Division
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DIVISON_ALGORITHM_H__
#define BOTAN_DIVISON_ALGORITHM_H__
#include <botan/bigint.h>
namespace Botan {
void BOTAN_DLL divide(const BigInt&, const BigInt&, BigInt&, BigInt&);
}
#endif

View File

@@ -0,0 +1,116 @@
/*
* DL Scheme
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DL_ALGO_H__
#define BOTAN_DL_ALGO_H__
#include <botan/dl_group.h>
#include <botan/x509_key.h>
#include <botan/pkcs8.h>
#include <botan/rng.h>
namespace Botan {
/**
* This class represents discrete logarithm (DL) public keys.
*/
class BOTAN_DLL DL_Scheme_PublicKey : public virtual Public_Key
{
public:
bool check_key(RandomNumberGenerator& rng, bool) const;
/**
* Get the DL domain parameters of this key.
* @return the DL domain parameters of this key
*/
const DL_Group& get_domain() const { return group; }
/**
* Get the public value y with y = g^x mod p where x is the secret key.
*/
const BigInt& get_y() const { return y; }
/**
* Get the prime p of the underlying DL group.
* @return the prime p
*/
const BigInt& group_p() const { return group.get_p(); }
/**
* Get the prime q of the underlying DL group.
* @return the prime q
*/
const BigInt& group_q() const { return group.get_q(); }
/**
* Get the generator g of the underlying DL group.
* @return the generator g
*/
const BigInt& group_g() const { return group.get_g(); }
/**
* Get the underlying groups encoding format.
* @return the encoding format
*/
virtual DL_Group::Format group_format() const = 0;
/**
* Get an X509 encoder for this key.
* @return an encoder usable to encode this key.
*/
X509_Encoder* x509_encoder() const;
/**
* Get an X509 decoder for this key.
* @return an decoder usable to decode a DL key and store the
* values in this instance.
*/
X509_Decoder* x509_decoder();
protected:
BigInt y;
DL_Group group;
private:
virtual void X509_load_hook() {}
};
/**
* This class represents discrete logarithm (DL) private keys.
*/
class BOTAN_DLL DL_Scheme_PrivateKey : public virtual DL_Scheme_PublicKey,
public virtual Private_Key
{
public:
bool check_key(RandomNumberGenerator& rng, bool) const;
/**
* Get the secret key x.
* @return the secret key
*/
const BigInt& get_x() const { return x; }
/**
* Get an PKCS#8 encoder for this key.
* @return an encoder usable to encode this key.
*/
PKCS8_Encoder* pkcs8_encoder() const;
/**
* Get an PKCS#8 decoder for this key.
* @param rng the rng to use
* @return an decoder usable to decode a DL key and store the
* values in this instance.
*/
PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator& rng);
protected:
BigInt x;
private:
virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false) {}
};
}
#endif

View File

@@ -0,0 +1,162 @@
/*
* Discrete Logarithm Group
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DL_PARAM_H__
#define BOTAN_DL_PARAM_H__
#include <botan/bigint.h>
#include <botan/data_src.h>
namespace Botan {
/**
* This class represents discrete logarithm groups. It holds a prime p,
* a prime q = (p-1)/2 and g = x^((p-1)/q) mod p.
*/
class BOTAN_DLL DL_Group
{
public:
/**
* Get the prime p.
* @return the prime p
*/
const BigInt& get_p() const;
/**
* Get the prime q.
* @return the prime q
*/
const BigInt& get_q() const;
/**
* Get the base g.
* @return the base g
*/
const BigInt& get_g() const;
/**
* The DL group encoding format variants.
*/
enum Format {
ANSI_X9_42,
ANSI_X9_57,
PKCS_3,
DSA_PARAMETERS = ANSI_X9_57,
DH_PARAMETERS = ANSI_X9_42,
X942_DH_PARAMETERS = ANSI_X9_42,
PKCS3_DH_PARAMETERS = PKCS_3
};
/**
* Determine the prime creation for DL groups.
*/
enum PrimeType { Strong, Prime_Subgroup, DSA_Kosherizer };
/**
* Perform validity checks on the group.
* @param rng the rng to use
* @param strong whether to perform stronger by lengthier tests
* @return true if the object is consistent, false otherwise
*/
bool verify_group(RandomNumberGenerator& rng, bool strong) const;
/**
* Encode this group into a string using PEM encoding.
* @param format the encoding format
* @return the string holding the PEM encoded group
*/
std::string PEM_encode(Format format) const;
/**
* Encode this group into a string using DER encoding.
* @param format the encoding format
* @return the string holding the DER encoded group
*/
SecureVector<byte> DER_encode(Format format) const;
/**
* Decode a DER/BER encoded group into this instance.
* @param src a DataSource providing the encoded group
* @param format the format of the encoded group
*/
void BER_decode(DataSource& src, Format format);
/**
* Decode a PEM encoded group into this instance.
* @param src a DataSource providing the encoded group
*/
void PEM_decode(DataSource& src);
/**
* Construct a DL group with uninitialized internal value.
* Use this constructor is you wish to set the groups values
* from a DER or PEM encoded group.
*/
DL_Group();
/**
* Construct a DL group that is registered in the configuration.
* @param name the name that is configured in the global configuration
* for the desired group. If no configuration file is specified,
* the default values from the file policy.cpp will be used. For instance,
* use "modp/ietf/768" as name.
*/
DL_Group(const std::string& name);
/**
* Create a new group randomly.
* @param rng the random number generator to use
* @param type specifies how the creation of primes p and q shall
* be performed. If type=Strong, then p will be determined as a
* safe prime, and q will be chosen as (p-1)/2. If
* type=Prime_Subgroup and qbits = 0, then the size of q will be
* determined according to the estimated difficulty of the DL
* problem. If type=DSA_Kosherizer, DSA primes will be created.
* @param pbits the number of bits of p
* @param qbits the number of bits of q. Leave it as 0 to have
* the value determined according to pbits.
*/
DL_Group(RandomNumberGenerator& rng, PrimeType type,
u32bit pbits, u32bit qbits = 0);
/**
* Create a DSA group with a given seed.
* @param rng the random number generator to use
* @param seed the seed to use to create the random primes
* @param pbits the desired bit size of the prime p
* @param qbits the desired bit size of the prime q.
*/
DL_Group(RandomNumberGenerator& rng, const MemoryRegion<byte>& seed,
u32bit pbits = 1024, u32bit qbits = 0);
/**
* Create a DL group. The prime q will be determined according to p.
* @param p the prime p
* @param g the base g
*/
DL_Group(const BigInt& p, const BigInt& g);
/**
* Create a DL group.
* @param p the prime p
* @param q the prime q
* @param g the base g
*/
DL_Group(const BigInt& p, const BigInt& q, const BigInt& g);
private:
static BigInt make_dsa_generator(const BigInt&, const BigInt&);
void init_check() const;
void initialize(const BigInt&, const BigInt&, const BigInt&);
bool initialized;
BigInt p, q, g;
};
}
#endif

View File

@@ -0,0 +1,69 @@
/*
* DLIES
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DLIES_H__
#define BOTAN_DLIES_H__
#include <botan/pubkey.h>
#include <botan/mac.h>
#include <botan/kdf.h>
namespace Botan {
/*
* DLIES Encryption
*/
class BOTAN_DLL DLIES_Encryptor : public PK_Encryptor
{
public:
DLIES_Encryptor(const PK_Key_Agreement_Key&,
KDF* kdf,
MessageAuthenticationCode* mac,
u32bit mac_key_len = 20);
~DLIES_Encryptor();
void set_other_key(const MemoryRegion<byte>&);
private:
SecureVector<byte> enc(const byte[], u32bit,
RandomNumberGenerator&) const;
u32bit maximum_input_size() const;
const PK_Key_Agreement_Key& key;
SecureVector<byte> other_key;
KDF* kdf;
MessageAuthenticationCode* mac;
u32bit mac_keylen;
};
/*
* DLIES Decryption
*/
class BOTAN_DLL DLIES_Decryptor : public PK_Decryptor
{
public:
DLIES_Decryptor(const PK_Key_Agreement_Key&,
KDF* kdf,
MessageAuthenticationCode* mac,
u32bit mac_key_len = 20);
~DLIES_Decryptor();
private:
SecureVector<byte> dec(const byte[], u32bit) const;
const PK_Key_Agreement_Key& key;
KDF* kdf;
MessageAuthenticationCode* mac;
u32bit mac_keylen;
};
}
#endif

View File

@@ -0,0 +1,62 @@
/*
* DSA
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DSA_H__
#define BOTAN_DSA_H__
#include <botan/dl_algo.h>
#include <botan/dsa_core.h>
namespace Botan {
/*
* DSA Public Key
*/
class BOTAN_DLL DSA_PublicKey : public PK_Verifying_wo_MR_Key,
public virtual DL_Scheme_PublicKey
{
public:
std::string algo_name() const { return "DSA"; }
DL_Group::Format group_format() const { return DL_Group::ANSI_X9_57; }
u32bit message_parts() const { return 2; }
u32bit message_part_size() const;
bool verify(const byte[], u32bit, const byte[], u32bit) const;
u32bit max_input_bits() const;
DSA_PublicKey() {}
DSA_PublicKey(const DL_Group&, const BigInt&);
protected:
DSA_Core core;
private:
void X509_load_hook();
};
/*
* DSA Private Key
*/
class BOTAN_DLL DSA_PrivateKey : public DSA_PublicKey,
public PK_Signing_Key,
public virtual DL_Scheme_PrivateKey
{
public:
SecureVector<byte> sign(const byte[], u32bit,
RandomNumberGenerator& rng) const;
bool check_key(RandomNumberGenerator& rng, bool) const;
DSA_PrivateKey() {}
DSA_PrivateKey(RandomNumberGenerator&, const DL_Group&,
const BigInt& = 0);
private:
void PKCS8_load_hook(RandomNumberGenerator& rng, bool = false);
};
}
#endif

View File

@@ -0,0 +1,37 @@
/*
* DSA Core
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DSA_CORE_H__
#define BOTAN_DSA_CORE_H__
#include <botan/dsa_op.h>
#include <botan/dl_group.h>
namespace Botan {
/*
* DSA Core
*/
class BOTAN_DLL 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;
};
}
#endif

View File

@@ -0,0 +1,53 @@
/*
* DSA Operations
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_DSA_OPS_H__
#define BOTAN_DSA_OPS_H__
#include <botan/numthry.h>
#include <botan/pow_mod.h>
#include <botan/reducer.h>
#include <botan/dl_group.h>
namespace Botan {
/*
* DSA Operation
*/
class BOTAN_DLL 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() {}
};
/*
* Botan's Default DSA Operation
*/
class BOTAN_DLL Default_DSA_Op : public DSA_Operation
{
public:
bool verify(const byte[], u32bit, const byte[], u32bit) const;
SecureVector<byte> sign(const byte[], u32bit, const BigInt&) const;
DSA_Operation* clone() const { return new Default_DSA_Op(*this); }
Default_DSA_Op(const DL_Group&, const BigInt&, const BigInt&);
private:
const BigInt x, y;
const DL_Group group;
Fixed_Base_Power_Mod powermod_g_p, powermod_y_p;
Modular_Reducer mod_p, mod_q;
};
}
#endif

View File

@@ -0,0 +1,85 @@
/*
* EAX Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EAX_H__
#define BOTAN_EAX_H__
#include <botan/basefilt.h>
#include <botan/block_cipher.h>
#include <botan/mac.h>
namespace Botan {
/*
* EAX Base Class
*/
class BOTAN_DLL 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(BlockCipher*, 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 BOTAN_DLL EAX_Encryption : public EAX_Base
{
public:
EAX_Encryption(BlockCipher* ciph, u32bit tag_size = 0) :
EAX_Base(ciph, tag_size) {}
EAX_Encryption(BlockCipher* ciph, const SymmetricKey& key,
const InitializationVector& iv,
u32bit tag_size) : EAX_Base(ciph, tag_size)
{
set_key(key);
set_iv(iv);
}
private:
void write(const byte[], u32bit);
void end_msg();
};
/*
* EAX Decryption
*/
class BOTAN_DLL EAX_Decryption : public EAX_Base
{
public:
EAX_Decryption(BlockCipher* ciph, u32bit tag_size = 0);
EAX_Decryption(BlockCipher* ciph, const SymmetricKey& key,
const InitializationVector& iv,
u32bit tag_size = 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

View File

@@ -0,0 +1,73 @@
/*
* ECB Mode
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ECB_H__
#define BOTAN_ECB_H__
#include <botan/modebase.h>
#include <botan/mode_pad.h>
#include <botan/block_cipher.h>
namespace Botan {
/*
* ECB
*/
class BOTAN_DLL ECB : public BlockCipherMode
{
protected:
ECB(BlockCipher* ciph, BlockCipherModePaddingMethod* pad) :
BlockCipherMode(ciph, "ECB", 0), padder(pad) {}
~ECB() { delete padder; }
std::string name() const;
BlockCipherModePaddingMethod* padder;
private:
bool valid_iv_size(u32bit) const;
};
/*
* ECB Encryption
*/
class BOTAN_DLL ECB_Encryption : public ECB
{
public:
ECB_Encryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad) :
ECB(ciph, pad) {}
ECB_Encryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad,
const SymmetricKey& key) :
ECB(ciph, pad) { set_key(key); }
private:
void write(const byte[], u32bit);
void end_msg();
};
/*
* ECB Decryption
*/
class BOTAN_DLL ECB_Decryption : public ECB
{
public:
ECB_Decryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad) :
ECB(ciph, pad) {}
ECB_Decryption(BlockCipher* ciph,
BlockCipherModePaddingMethod* pad,
const SymmetricKey& key) :
ECB(ciph, pad) { set_key(key); }
private:
void write(const byte[], u32bit);
void end_msg();
};
}
#endif

View File

@@ -0,0 +1,44 @@
/*
* ElGamal Core
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ELGAMAL_CORE_H__
#define BOTAN_ELGAMAL_CORE_H__
#include <botan/elg_op.h>
#include <botan/blinding.h>
#include <botan/dl_group.h>
namespace Botan {
/*
* ElGamal Core
*/
class BOTAN_DLL 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&);
ELG_Core(RandomNumberGenerator&, const DL_Group&,
const BigInt&, const BigInt&);
~ELG_Core() { delete op; }
private:
ELG_Operation* op;
Blinder blinder;
u32bit p_bytes;
};
}
#endif

View File

@@ -0,0 +1,52 @@
/*
* ElGamal Operations
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ELGAMAL_OPS_H__
#define BOTAN_ELGAMAL_OPS_H__
#include <botan/pow_mod.h>
#include <botan/numthry.h>
#include <botan/reducer.h>
#include <botan/dl_group.h>
namespace Botan {
/*
* ElGamal Operation
*/
class BOTAN_DLL 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() {}
};
/*
* Botan's Default ElGamal Operation
*/
class BOTAN_DLL Default_ELG_Op : public ELG_Operation
{
public:
SecureVector<byte> encrypt(const byte[], u32bit, const BigInt&) const;
BigInt decrypt(const BigInt&, const BigInt&) const;
ELG_Operation* clone() const { return new Default_ELG_Op(*this); }
Default_ELG_Op(const DL_Group&, const BigInt&, const BigInt&);
private:
const BigInt p;
Fixed_Base_Power_Mod powermod_g_p, powermod_y_p;
Fixed_Exponent_Power_Mod powermod_x_p;
Modular_Reducer mod_p;
};
}
#endif

View File

@@ -0,0 +1,59 @@
/*
* ElGamal
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ELGAMAL_H__
#define BOTAN_ELGAMAL_H__
#include <botan/dl_algo.h>
#include <botan/elg_core.h>
namespace Botan {
/*
* ElGamal Public Key
*/
class BOTAN_DLL ElGamal_PublicKey : public PK_Encrypting_Key,
public virtual DL_Scheme_PublicKey
{
public:
std::string algo_name() const { return "ElGamal"; }
DL_Group::Format group_format() const { return DL_Group::ANSI_X9_42; }
SecureVector<byte> encrypt(const byte[], u32bit,
RandomNumberGenerator& rng) const;
u32bit max_input_bits() const;
ElGamal_PublicKey() {}
ElGamal_PublicKey(const DL_Group&, const BigInt&);
protected:
ELG_Core core;
private:
void X509_load_hook();
};
/*
* ElGamal Private Key
*/
class BOTAN_DLL 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(RandomNumberGenerator& rng, bool) const;
ElGamal_PrivateKey() {}
ElGamal_PrivateKey(RandomNumberGenerator&, const DL_Group&,
const BigInt& = 0);
private:
void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
};
}
#endif

View File

@@ -0,0 +1,42 @@
/*
* EME Classes
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_PUBKEY_EME_ENCRYPTION_PAD_H__
#define BOTAN_PUBKEY_EME_ENCRYPTION_PAD_H__
#include <botan/secmem.h>
#include <botan/rng.h>
namespace Botan {
/*
* Encoding Method for Encryption
*/
class BOTAN_DLL EME
{
public:
virtual u32bit maximum_input_size(u32bit) const = 0;
SecureVector<byte> encode(const byte[], u32bit, u32bit,
RandomNumberGenerator&) const;
SecureVector<byte> encode(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator&) 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,
RandomNumberGenerator&) const = 0;
virtual SecureVector<byte> unpad(const byte[], u32bit, u32bit) const = 0;
};
}
#endif

View File

@@ -0,0 +1,45 @@
/*
* EME1
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EME1_H__
#define BOTAN_EME1_H__
#include <botan/eme.h>
#include <botan/kdf.h>
#include <botan/hash.h>
namespace Botan {
/*
* EME1
*/
class BOTAN_DLL EME1 : public EME
{
public:
u32bit maximum_input_size(u32bit) const;
/**
EME1 constructor. Hash will be deleted by ~EME1 (when mgf is deleted)
P is an optional label. Normally empty.
*/
EME1(HashFunction* hash, const std::string& P = "");
~EME1() { delete mgf; }
private:
SecureVector<byte> pad(const byte[], u32bit, u32bit,
RandomNumberGenerator&) const;
SecureVector<byte> unpad(const byte[], u32bit, u32bit) const;
const u32bit HASH_LENGTH;
SecureVector<byte> Phash;
MGF* mgf;
};
}
#endif

View File

@@ -0,0 +1,30 @@
/*
* EME PKCS#1 v1.5
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EME_PKCS1_H__
#define BOTAN_EME_PKCS1_H__
#include <botan/eme.h>
namespace Botan {
/*
* EME_PKCS1v15
*/
class BOTAN_DLL EME_PKCS1v15 : public EME
{
public:
u32bit maximum_input_size(u32bit) const;
private:
SecureVector<byte> pad(const byte[], u32bit, u32bit,
RandomNumberGenerator&) const;
SecureVector<byte> unpad(const byte[], u32bit, u32bit) const;
};
}
#endif

View File

@@ -0,0 +1,36 @@
/*
* EMSA Classes
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_PUBKEY_EMSA_H__
#define BOTAN_PUBKEY_EMSA_H__
#include <botan/secmem.h>
#include <botan/rng.h>
namespace Botan {
/*
* Encoding Method for Signatures, Appendix
*/
class BOTAN_DLL EMSA
{
public:
virtual void update(const byte[], u32bit) = 0;
virtual SecureVector<byte> raw_data() = 0;
virtual SecureVector<byte> encoding_of(const MemoryRegion<byte>&,
u32bit,
RandomNumberGenerator& rng) = 0;
virtual bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw() = 0;
virtual ~EMSA() {}
};
}
#endif

View File

@@ -0,0 +1,41 @@
/*
* EMSA1
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA1_H__
#define BOTAN_EMSA1_H__
#include <botan/emsa.h>
#include <botan/hash.h>
namespace Botan {
/*
* EMSA1
*/
class BOTAN_DLL EMSA1 : public EMSA
{
public:
EMSA1(HashFunction* h) : hash(h) {}
~EMSA1() { delete hash; }
protected:
const HashFunction* hash_ptr() const { return hash; }
private:
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
HashFunction* hash;
};
}
#endif

View File

@@ -0,0 +1,32 @@
/*
* EMSA1 BSI Variant
* (C) 1999-2008 Jack Lloyd
* 2007 FlexSecure GmbH
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA1_BSI_H__
#define BOTAN_EMSA1_BSI_H__
#include <botan/emsa1.h>
namespace Botan {
/**
EMSA1_BSI is a variant of EMSA1 specified by the BSI. It accepts only
hash values which are less or equal than the maximum key length. The
implementation comes from InSiTo
*/
class BOTAN_DLL EMSA1_BSI : public EMSA1
{
public:
EMSA1_BSI(HashFunction* hash) : EMSA1(hash) {}
private:
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
};
}
#endif

View File

@@ -0,0 +1,41 @@
/*
* EMSA2
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA2_H__
#define BOTAN_EMSA2_H__
#include <botan/emsa.h>
#include <botan/hash.h>
namespace Botan {
/*
* EMSA2
*/
class BOTAN_DLL EMSA2 : public EMSA
{
public:
EMSA2(HashFunction* hash);
~EMSA2() { delete hash; }
private:
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
SecureVector<byte> empty_hash;
HashFunction* hash;
byte hash_id;
};
}
#endif

View File

@@ -0,0 +1,65 @@
/*
* EMSA3 and EMSA3_Raw
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA3_H__
#define BOTAN_EMSA3_H__
#include <botan/emsa.h>
#include <botan/hash.h>
namespace Botan {
/**
* EMSA3
* aka PKCS #1 v1.5 signature padding
* aka PKCS #1 block type 1
*/
class BOTAN_DLL EMSA3 : public EMSA
{
public:
EMSA3(HashFunction*);
~EMSA3();
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
private:
HashFunction* hash;
SecureVector<byte> hash_id;
};
/**
* EMSA3_Raw which is EMSA3 without a hash or digest id (which
* according to QCA docs is "identical to PKCS#11's CKM_RSA_PKCS
* mechanism", something I have not confirmed)
*/
class BOTAN_DLL EMSA3_Raw : public EMSA
{
public:
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
private:
SecureVector<byte> message;
};
}
#endif

View File

@@ -0,0 +1,43 @@
/*
* EMSA4
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA4_H__
#define BOTAN_EMSA4_H__
#include <botan/emsa.h>
#include <botan/hash.h>
#include <botan/kdf.h>
namespace Botan {
/*
* EMSA4
*/
class BOTAN_DLL EMSA4 : public EMSA
{
public:
EMSA4(HashFunction*);
EMSA4(HashFunction*, u32bit);
~EMSA4() { delete hash; delete mgf; }
private:
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator& rng);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
u32bit SALT_SIZE;
HashFunction* hash;
const MGF* mgf;
};
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* EMSA-Raw
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EMSA_RAW_H__
#define BOTAN_EMSA_RAW_H__
#include <botan/emsa.h>
namespace Botan {
/*
* EMSA-Raw
*/
class BOTAN_DLL EMSA_Raw : public EMSA
{
private:
void update(const byte[], u32bit);
SecureVector<byte> raw_data();
SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit,
RandomNumberGenerator&);
bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&,
u32bit) throw();
SecureVector<byte> message;
};
}
#endif

View File

@@ -0,0 +1,140 @@
/*
* Engine
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ENGINE_H__
#define BOTAN_ENGINE_H__
#include <botan/scan_name.h>
#include <botan/block_cipher.h>
#include <botan/stream_cipher.h>
#include <botan/hash.h>
#include <botan/mac.h>
#include <botan/pow_mod.h>
#include <utility>
#include <map>
#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY)
#include <botan/if_op.h>
#endif
#if defined(BOTAN_HAS_DSA)
#include <botan/dsa_op.h>
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
#include <botan/dh_op.h>
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
#include <botan/nr_op.h>
#endif
#if defined(BOTAN_HAS_ELGAMAL)
#include <botan/elg_op.h>
#endif
#if defined(BOTAN_HAS_ECDSA)
#include <botan/ecdsa_op.h>
#include <botan/ec_dompar.h>
#endif
#if defined(BOTAN_HAS_ECKAEG)
#include <botan/eckaeg_op.h>
#include <botan/ec_dompar.h>
#endif
namespace Botan {
class Algorithm_Factory;
class Keyed_Filter;
/*
* Engine Base Class
*/
class BOTAN_DLL Engine
{
public:
virtual ~Engine() {}
virtual std::string provider_name() const = 0;
// Lookup functions
virtual BlockCipher*
find_block_cipher(const SCAN_Name&, Algorithm_Factory&) const
{ return 0; }
virtual StreamCipher*
find_stream_cipher(const SCAN_Name&, Algorithm_Factory&) const
{ return 0; }
virtual HashFunction*
find_hash(const SCAN_Name&, Algorithm_Factory&) const
{ return 0; }
virtual MessageAuthenticationCode*
find_mac(const SCAN_Name&, Algorithm_Factory&) const
{ return 0; }
virtual Modular_Exponentiator*
mod_exp(const BigInt&, Power_Mod::Usage_Hints) const
{ return 0; }
virtual Keyed_Filter* get_cipher(const std::string&,
Cipher_Dir,
Algorithm_Factory&)
{ return 0; }
#if defined(BOTAN_HAS_IF_PUBLIC_KEY_FAMILY)
virtual IF_Operation* if_op(const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_DSA)
virtual DSA_Operation* dsa_op(const DL_Group&, const BigInt&,
const BigInt&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_NYBERG_RUEPPEL)
virtual NR_Operation* nr_op(const DL_Group&, const BigInt&,
const BigInt&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_ELGAMAL)
virtual ELG_Operation* elg_op(const DL_Group&, const BigInt&,
const BigInt&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
virtual DH_Operation* dh_op(const DL_Group&, const BigInt&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_ECDSA)
virtual ECDSA_Operation* ecdsa_op(const EC_Domain_Params&,
const BigInt&,
const PointGFp&) const
{ return 0; }
#endif
#if defined(BOTAN_HAS_ECKAEG)
virtual ECKAEG_Operation* eckaeg_op(const EC_Domain_Params&,
const BigInt&,
const PointGFp&) const
{ return 0; }
#endif
};
}
#endif

View File

@@ -0,0 +1,95 @@
/**
* EntropySource
* (C) 2008-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ENTROPY_SOURCE_BASE_H__
#define BOTAN_ENTROPY_SOURCE_BASE_H__
#include <botan/buf_comp.h>
#include <string>
#include <utility>
namespace Botan {
/**
* Class used to accumulate the poll results of EntropySources
*/
class Entropy_Accumulator
{
public:
Entropy_Accumulator(u32bit goal) :
entropy_goal(goal), collected_bits(0) {}
virtual ~Entropy_Accumulator() {}
/**
@return cached I/O buffer for repeated polls
*/
MemoryRegion<byte>& get_io_buffer(u32bit size)
{ io_buffer.create(size); return io_buffer; }
u32bit bits_collected() const
{ return static_cast<u32bit>(collected_bits); }
bool polling_goal_achieved() const
{ return (collected_bits >= entropy_goal); }
u32bit desired_remaining_bits() const
{
if(collected_bits >= entropy_goal)
return 0;
return static_cast<u32bit>(entropy_goal - collected_bits);
}
void add(const void* bytes, u32bit length, double entropy_bits_per_byte)
{
add_bytes(reinterpret_cast<const byte*>(bytes), length);
collected_bits += entropy_bits_per_byte * length;
}
template<typename T>
void add(const T& v, double entropy_bits_per_byte)
{
add(&v, sizeof(T), entropy_bits_per_byte);
}
private:
virtual void add_bytes(const byte bytes[], u32bit length) = 0;
SecureVector<byte> io_buffer;
u32bit entropy_goal;
double collected_bits;
};
class Entropy_Accumulator_BufferedComputation : public Entropy_Accumulator
{
public:
Entropy_Accumulator_BufferedComputation(BufferedComputation& sink,
u32bit goal) :
Entropy_Accumulator(goal), entropy_sink(sink) {}
private:
virtual void add_bytes(const byte bytes[], u32bit length)
{
entropy_sink.update(bytes, length);
}
BufferedComputation& entropy_sink;
};
/**
* Abstract interface to a source of (hopefully unpredictable) system entropy
*/
class BOTAN_DLL EntropySource
{
public:
virtual std::string name() const = 0;
virtual void poll(Entropy_Accumulator& accum) = 0;
virtual ~EntropySource() {}
};
}
#endif

View File

@@ -0,0 +1,33 @@
/*
* Win32 CAPI EntropySource
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ENTROPY_SRC_WIN32_CAPI_H__
#define BOTAN_ENTROPY_SRC_WIN32_CAPI_H__
#include <botan/entropy_src.h>
#include <vector>
namespace Botan {
/**
* Win32 CAPI Entropy Source
*/
class BOTAN_DLL Win32_CAPI_EntropySource : public EntropySource
{
public:
std::string name() const { return "Win32 CryptoGenRandom"; }
void poll(Entropy_Accumulator& accum);
Win32_CAPI_EntropySource(const std::string& = "");
private:
std::vector<u64bit> prov_types;
};
}
#endif

View File

@@ -0,0 +1,27 @@
/**
* Win32 EntropySource
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_ENTROPY_SRC_WIN32_H__
#define BOTAN_ENTROPY_SRC_WIN32_H__
#include <botan/entropy_src.h>
namespace Botan {
/**
* Win32 Entropy Source
*/
class BOTAN_DLL Win32_EntropySource : public EntropySource
{
public:
std::string name() const { return "Win32 Statistics"; }
void poll(Entropy_Accumulator& accum);
};
}
#endif

View File

@@ -0,0 +1,197 @@
/*
* Exceptions
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_EXCEPTION_H__
#define BOTAN_EXCEPTION_H__
#include <botan/types.h>
#include <exception>
#include <string>
namespace Botan {
/*
* Exception Base Class
*/
class BOTAN_DLL 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 BOTAN_DLL Invalid_Argument : public Exception
{
Invalid_Argument(const std::string& err = "") : Exception(err) {}
};
/*
* Invalid_Key_Length Exception
*/
struct BOTAN_DLL Invalid_Key_Length : public Invalid_Argument
{
Invalid_Key_Length(const std::string&, u32bit);
};
/*
* Invalid_Block_Size Exception
*/
struct BOTAN_DLL Invalid_Block_Size : public Invalid_Argument
{
Invalid_Block_Size(const std::string&, const std::string&);
};
/*
* Invalid_IV_Length Exception
*/
struct BOTAN_DLL Invalid_IV_Length : public Invalid_Argument
{
Invalid_IV_Length(const std::string&, u32bit);
};
/*
* Invalid_State Exception
*/
struct BOTAN_DLL Invalid_State : public Exception
{
Invalid_State(const std::string& err) : Exception(err) {}
};
/*
* PRNG_Unseeded Exception
*/
struct BOTAN_DLL PRNG_Unseeded : public Invalid_State
{
PRNG_Unseeded(const std::string& algo) :
Invalid_State("PRNG not seeded: " + algo) {}
};
/*
* Policy_Violation Exception
*/
struct BOTAN_DLL Policy_Violation : public Invalid_State
{
Policy_Violation(const std::string& err) :
Invalid_State("Policy violation: " + err) {}
};
/*
* Lookup_Error Exception
*/
struct BOTAN_DLL Lookup_Error : public Exception
{
Lookup_Error(const std::string& err) : Exception(err) {}
};
/*
* Algorithm_Not_Found Exception
*/
struct BOTAN_DLL Algorithm_Not_Found : public Exception
{
Algorithm_Not_Found(const std::string&);
};
/*
* Format_Error Exception
*/
struct BOTAN_DLL Format_Error : public Exception
{
Format_Error(const std::string& err = "") : Exception(err) {}
};
/*
* Invalid_Algorithm_Name Exception
*/
struct BOTAN_DLL Invalid_Algorithm_Name : public Format_Error
{
Invalid_Algorithm_Name(const std::string&);
};
/*
* Encoding_Error Exception
*/
struct BOTAN_DLL Encoding_Error : public Format_Error
{
Encoding_Error(const std::string& name) :
Format_Error("Encoding error: " + name) {}
};
/*
* Decoding_Error Exception
*/
struct BOTAN_DLL Decoding_Error : public Format_Error
{
Decoding_Error(const std::string& name) :
Format_Error("Decoding error: " + name) {}
};
/*
* Invalid_OID Exception
*/
struct BOTAN_DLL Invalid_OID : public Decoding_Error
{
Invalid_OID(const std::string& oid) :
Decoding_Error("Invalid ASN.1 OID: " + oid) {}
};
/*
* Stream_IO_Error Exception
*/
struct BOTAN_DLL Stream_IO_Error : public Exception
{
Stream_IO_Error(const std::string& err) :
Exception("I/O error: " + err) {}
};
/*
* Configuration Error Exception
*/
struct BOTAN_DLL 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 BOTAN_DLL Integrity_Failure : public Exception
{
Integrity_Failure(const std::string& err) :
Exception("Integrity failure: " + err) {}
};
/*
* Internal_Error Exception
*/
struct BOTAN_DLL Internal_Error : public Exception
{
Internal_Error(const std::string& err) :
Exception("Internal error: " + err) {}
};
/*
* Self Test Failure Exception
*/
struct BOTAN_DLL Self_Test_Failure : public Internal_Error
{
Self_Test_Failure(const std::string& err) :
Internal_Error("Self test failed: " + err) {}
};
}
#endif

View File

@@ -0,0 +1,113 @@
/*
* Filter
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_FILTER_H__
#define BOTAN_FILTER_H__
#include <botan/secmem.h>
#include <vector>
namespace Botan {
/**
* This class represents general abstract filter objects.
*/
class BOTAN_DLL Filter
{
public:
/**
* Write a portion of a message to this filter.
* @param input the input as a byte array
* @param length the length of the byte array input
*/
virtual void write(const byte input[], u32bit length) = 0;
/**
* Start a new message. Must be closed by end_msg() before another
* message can be startet.
*/
virtual void start_msg() {}
/**
* Tell the Filter that the current message shall be ended.
*/
virtual void end_msg() {}
/**
* Check whether this filter is an attachable filter.
* @return true if this filter is attachable, false otherwise
*/
virtual bool attachable() { return true; }
/**
* Start a new message in *this and all following filters. Only for
* internal use, not intended for use in client applications.
*/
void new_msg();
/**
* End a new message in *this and all following filters. Only for
* internal use, not intended for use in client applications.
*/
void finish_msg();
virtual ~Filter() {}
protected:
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;
// true if filter belongs to a pipe --> prohibit filter sharing!
bool owned;
};
/**
* This is the abstract Fanout_Filter base class.
**/
class BOTAN_DLL 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); }
};
/**
* The type of checking to be performed by decoders:
* NONE - no checks, IGNORE_WS - perform checks, but ignore
* whitespaces, FULL_CHECK - perform checks, also complain
* about white spaces.
*/
enum Decoder_Checking { NONE, IGNORE_WS, FULL_CHECK };
}
#endif

View File

@@ -0,0 +1,189 @@
/*
* Filters
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_FILTERS_H__
#define BOTAN_FILTERS_H__
#include <botan/block_cipher.h>
#include <botan/stream_cipher.h>
#include <botan/hash.h>
#include <botan/mac.h>
#include <botan/pipe.h>
#include <botan/basefilt.h>
#include <botan/data_snk.h>
#include <botan/scan_name.h>
#if defined(BOTAN_HAS_BASE64_CODEC)
#include <botan/base64.h>
#endif
#if defined(BOTAN_HAS_HEX_CODEC)
#include <botan/hex.h>
#endif
namespace Botan {
/**
* Stream Cipher Filter.
*/
class BOTAN_DLL StreamCipher_Filter : public Keyed_Filter
{
public:
/**
* Seek in the stream.
* @param position the position to seek ahead
*/
void seek(u32bit position) { cipher->seek(position); }
/**
* Find out whether the cipher underlying this filter supports
* resyncing.
* @return true if the cipher supports resyncing
*/
bool supports_resync() const { return (cipher->IV_LENGTH != 0); }
/**
* Set the initialization vector for this filter.
* @param iv the initialization vector to set
*/
void set_iv(const InitializationVector& iv);
void write(const byte[], u32bit);
/**
* Construct a stream cipher filter.
* @param cipher_obj a cipher object to use
*/
StreamCipher_Filter(StreamCipher* cipher_obj);
/**
* Construct a stream cipher filter.
* @param cipher the name of the desired cipher
*/
StreamCipher_Filter(const std::string& cipher);
/**
* Construct a stream cipher filter.
* @param cipher the name of the desired cipher
* @param key the key to use inside this filter
*/
StreamCipher_Filter(const std::string& cipher, const SymmetricKey& key);
~StreamCipher_Filter() { delete cipher; }
private:
SecureVector<byte> buffer;
StreamCipher* cipher;
};
/**
* Hash Filter.
*/
class BOTAN_DLL Hash_Filter : public Filter
{
public:
void write(const byte input[], u32bit len) { hash->update(input, len); }
void end_msg();
/**
* Construct a hash filter.
* @param hash_fun the hash function to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the hashfunction
* hash. Otherwise, specify a smaller value here so that the
* output of the hash algorithm will be cut off.
*/
Hash_Filter(HashFunction* hash_fun, u32bit len = 0) :
OUTPUT_LENGTH(len), hash(hash_fun) {}
/**
* Construct a hash filter.
* @param request the name of the hash algorithm to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the hashfunction
* hash. Otherwise, specify a smaller value here so that the
* output of the hash algorithm will be cut off.
*/
Hash_Filter(const std::string& request, u32bit len = 0);
~Hash_Filter() { delete hash; }
private:
const u32bit OUTPUT_LENGTH;
HashFunction* hash;
};
/**
* MessageAuthenticationCode Filter.
*/
class BOTAN_DLL MAC_Filter : public Keyed_Filter
{
public:
void write(const byte input[], u32bit len) { mac->update(input, len); }
void end_msg();
/**
* Construct a MAC filter. The MAC key will be left empty.
* @param mac the MAC to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the
* MAC. Otherwise, specify a smaller value here so that the
* output of the MAC will be cut off.
*/
MAC_Filter(MessageAuthenticationCode* mac_obj,
u32bit out_len = 0) : OUTPUT_LENGTH(out_len)
{
base_ptr = mac = mac_obj;
}
/**
* Construct a MAC filter.
* @param mac the MAC to use
* @param key the MAC key to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the
* MAC. Otherwise, specify a smaller value here so that the
* output of the MAC will be cut off.
*/
MAC_Filter(MessageAuthenticationCode* mac_obj,
const SymmetricKey& key,
u32bit out_len = 0) : OUTPUT_LENGTH(out_len)
{
base_ptr = mac = mac_obj;
mac->set_key(key);
}
/**
* Construct a MAC filter. The MAC key will be left empty.
* @param mac the name of the MAC to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the
* MAC. Otherwise, specify a smaller value here so that the
* output of the MAC will be cut off.
*/
MAC_Filter(const std::string& mac, u32bit len = 0);
/**
* Construct a MAC filter.
* @param mac the name of the MAC to use
* @param key the MAC key to use
* @param len the output length of this filter. Leave the default
* value 0 if you want to use the full output of the
* MAC. Otherwise, specify a smaller value here so that the
* output of the MAC will be cut off.
*/
MAC_Filter(const std::string& mac, const SymmetricKey& key,
u32bit len = 0);
~MAC_Filter() { delete mac; }
private:
const u32bit OUTPUT_LENGTH;
MessageAuthenticationCode* mac;
};
}
#endif

View File

@@ -0,0 +1,35 @@
/*
* FORK-256
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_FORK_256_H__
#define BOTAN_FORK_256_H__
#include <botan/mdx_hash.h>
namespace Botan {
/*
* FORK-256
*/
class BOTAN_DLL 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 compress_n(const byte[], u32bit blocks);
void copy_out(byte[]);
SecureBuffer<u32bit, 8> digest;
SecureBuffer<u32bit, 16> M;
};
}
#endif

View File

@@ -0,0 +1,33 @@
/*
* PBE Lookup
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_LOOKUP_PBE_H__
#define BOTAN_LOOKUP_PBE_H__
#include <botan/pbe.h>
#include <string>
namespace Botan {
/**
* Factory function for PBEs.
* @param algo_spec the name of the PBE algorithm to retrieve
* @return a pointer to a PBE with randomly created parameters
*/
BOTAN_DLL PBE* get_pbe(const std::string&);
/**
* Factory function for PBEs.
* @param pbe_oid the oid of the desired PBE
* @param params a DataSource providing the DER encoded parameters to use
* @return a pointer to the PBE with the specified parameters
*/
BOTAN_DLL PBE* get_pbe(const OID&, DataSource&);
}
#endif

View File

@@ -0,0 +1,67 @@
/*
* GOST 28147-89
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_GOST_28147_89_H__
#define BOTAN_GOST_28147_89_H__
#include <botan/block_cipher.h>
namespace Botan {
class GOST_28147_89_Params;
/**
* The GOST 28147-89 block cipher uses a set of 4 bit Sboxes, however
* the standard does not actually define these Sboxes; they are
* considered a local configuration issue. Several different sets are
* used.
*/
class GOST_28147_89_Params
{
public:
byte sbox_entry(u32bit row, u32bit col) const;
std::string param_name() const { return name; }
/**
* Default GOST parameters are the ones given in GOST R 34.11 for
* testing purposes; these sboxes are also used by Crypto++, and,
* at least according to Wikipedia, the Central Bank of Russian Federation
*/
GOST_28147_89_Params(const std::string& name = "R3411_94_TestParam");
private:
const byte* sboxes;
std::string name;
};
/**
* GOST 28147-89
*/
class BOTAN_DLL GOST_28147_89 : public BlockCipher
{
public:
void clear() throw() { EK.clear(); }
std::string name() const { return "GOST-28147-89"; }
BlockCipher* clone() const { return new GOST_28147_89(SBOX); }
GOST_28147_89(const GOST_28147_89_Params& params);
private:
GOST_28147_89(const SecureBuffer<u32bit, 1024>& other_SBOX) :
BlockCipher(8, 32), SBOX(other_SBOX) {}
void enc(const byte[], byte[]) const;
void dec(const byte[], byte[]) const;
void key_schedule(const byte[], u32bit);
SecureBuffer<u32bit, 1024> SBOX;
SecureBuffer<u32bit, 8> EK;
};
}
#endif

View File

@@ -0,0 +1,41 @@
/**
* GOST 34.11
* (C) 2009 Jack Lloyd
*/
#ifndef BOTAN_GOST_3411_H__
#define BOTAN_GOST_3411_H__
#include <botan/hash.h>
#include <botan/gost_28147.h>
namespace Botan {
/**
* GOST 34.11
*/
class BOTAN_DLL GOST_34_11 : public HashFunction
{
public:
void clear() throw();
std::string name() const { return "GOST-R-34.11-94" ; }
HashFunction* clone() const { return new GOST_34_11; }
GOST_34_11();
protected:
void compress_n(const byte input[], u32bit blocks);
void add_data(const byte[], u32bit);
void final_result(byte[]);
GOST_28147_89 cipher;
SecureBuffer<byte, 32> buffer;
SecureBuffer<byte, 32> sum;
SecureBuffer<byte, 32> hash;
u64bit count;
u32bit position;
};
}
#endif

View File

@@ -0,0 +1,35 @@
/*
* HAS-160
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HAS_160_H__
#define BOTAN_HAS_160_H__
#include <botan/mdx_hash.h>
namespace Botan {
/*
* HAS-160
*/
class BOTAN_DLL 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 compress_n(const byte[], u32bit blocks);
void copy_out(byte[]);
SecureBuffer<u32bit, 20> X;
SecureBuffer<u32bit, 5> digest;
};
}
#endif

View File

@@ -0,0 +1,52 @@
/**
* Hash Function Base Class
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HASH_FUNCTION_BASE_CLASS_H__
#define BOTAN_HASH_FUNCTION_BASE_CLASS_H__
#include <botan/buf_comp.h>
#include <string>
namespace Botan {
/**
* This class represents hash function (message digest) objects.
*/
class BOTAN_DLL HashFunction : public BufferedComputation
{
public:
/**
* The hash block size as defined for this algorithm.
*/
const u32bit HASH_BLOCK_SIZE;
/**
* Get a new object representing the same algorithm as *this
*/
virtual HashFunction* clone() const = 0;
/**
* Get the name of this algorithm.
* @return the name of this algorithm
*/
virtual std::string name() const = 0;
/**
* Reset the internal state of this object.
*/
virtual void clear() throw() = 0;
HashFunction(u32bit hash_len, u32bit block_len = 0) :
BufferedComputation(hash_len), HASH_BLOCK_SIZE(block_len) {}
virtual ~HashFunction() {}
private:
HashFunction& operator=(const HashFunction&);
};
}
#endif

View File

@@ -0,0 +1,24 @@
/*
* Hash Function Identification
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HASHID_H__
#define BOTAN_HASHID_H__
#include <botan/secmem.h>
#include <string>
namespace Botan {
/*
* Return the values of various defined HashIDs
*/
BOTAN_DLL MemoryVector<byte> pkcs_hash_id(const std::string&);
BOTAN_DLL byte ieee1363_hash_id(const std::string&);
}
#endif

View File

@@ -0,0 +1,90 @@
/*
* Hex Encoder/Decoder
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HEX_H__
#define BOTAN_HEX_H__
#include <botan/filter.h>
namespace Botan {
/**
* This class represents a hex encoder. It encodes byte arrays to hex strings.
*/
class BOTAN_DLL Hex_Encoder : public Filter
{
public:
/**
* Whether to use uppercase or lowercase letters for the encoded string.
*/
enum Case { Uppercase, Lowercase };
/**
Encode a single byte into two hex characters
*/
static void encode(byte in, byte out[2], Case the_case = Uppercase);
void write(const byte in[], u32bit length);
void end_msg();
/**
* Create a hex encoder.
* @param the_case the case to use in the encoded strings.
*/
Hex_Encoder(Case the_case);
/**
* Create a hex encoder.
* @param newlines should newlines be used
* @param line_length if newlines are used, how long are lines
* @param the_case the case to use in the encoded strings
*/
Hex_Encoder(bool newlines = false,
u32bit line_length = 72,
Case the_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;
};
/**
* This class represents a hex decoder. It converts hex strings to byte arrays.
*/
class BOTAN_DLL Hex_Decoder : public Filter
{
public:
static byte decode(const byte[2]);
static bool is_valid(byte);
void write(const byte[], u32bit);
void end_msg();
/**
* Construct a Hex Decoder using the specified
* character checking.
* @param checking the checking to use during decoding.
*/
Hex_Decoder(Decoder_Checking 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

View File

@@ -0,0 +1,38 @@
/*
* HMAC
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HMAC_H__
#define BOTAN_HMAC_H__
#include <botan/mac.h>
#include <botan/hash.h>
namespace Botan {
/*
* HMAC
*/
class BOTAN_DLL HMAC : public MessageAuthenticationCode
{
public:
void clear() throw();
std::string name() const;
MessageAuthenticationCode* clone() const;
HMAC(HashFunction* hash);
~HMAC() { delete hash; }
private:
void add_data(const byte[], u32bit);
void final_result(byte[]);
void key_schedule(const byte[], u32bit);
HashFunction* hash;
SecureVector<byte> i_key, o_key;
};
}
#endif

View File

@@ -0,0 +1,59 @@
/*
* HMAC RNG
* (C) 2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_HMAC_RNG_H__
#define BOTAN_HMAC_RNG_H__
#include <botan/mac.h>
#include <botan/rng.h>
#include <vector>
namespace Botan {
/**
HMAC_RNG - based on the design described in "On Extract-then-Expand
Key Derivation Functions and an HMAC-based KDF" by Hugo Krawczyk
(henceforce, 'E-t-E')
However it actually can be parameterized with any two MAC functions,
not restricted to HMAC (this variation is also described in Krawczyk's
paper), for instance one could use HMAC(SHA-512) as the extractor
and CMAC(AES-256) as the PRF.
*/
class BOTAN_DLL HMAC_RNG : public RandomNumberGenerator
{
public:
void randomize(byte buf[], u32bit len);
bool is_seeded() const { return seeded; }
void clear() throw();
std::string name() const;
void reseed(u32bit poll_bits);
void add_entropy_source(EntropySource* es);
void add_entropy(const byte[], u32bit);
HMAC_RNG(MessageAuthenticationCode* extractor,
MessageAuthenticationCode* prf);
~HMAC_RNG();
private:
void reseed_with_input(u32bit poll_bits,
const byte input[], u32bit length);
MessageAuthenticationCode* extractor;
MessageAuthenticationCode* prf;
std::vector<EntropySource*> entropy_sources;
bool seeded;
SecureVector<byte> K, io_buffer;
u32bit counter, source_index;
};
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* IDEA
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_IDEA_H__
#define BOTAN_IDEA_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* IDEA
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
SecureBuffer<u16bit, 52> EK, DK;
};
}
#endif

View File

@@ -0,0 +1,85 @@
/*
* IF Scheme
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_IF_ALGO_H__
#define BOTAN_IF_ALGO_H__
#include <botan/if_core.h>
#include <botan/x509_key.h>
#include <botan/pkcs8.h>
namespace Botan {
/**
* This class represents public keys
* of integer factorization based (IF) public key schemes.
*/
class BOTAN_DLL IF_Scheme_PublicKey : public virtual Public_Key
{
public:
bool check_key(RandomNumberGenerator& rng, bool) const;
/**
* Get n = p * q.
* @return n
*/
const BigInt& get_n() const { return n; }
/**
* Get the public exponent used by the key.
* @return the public exponent
*/
const BigInt& get_e() const { return e; }
u32bit max_input_bits() const { return (n.bits() - 1); }
X509_Encoder* x509_encoder() const;
X509_Decoder* x509_decoder();
protected:
virtual void X509_load_hook();
BigInt n, e;
IF_Core core;
};
/**
* This class represents public keys
* of integer factorization based (IF) public key schemes.
*/
class BOTAN_DLL IF_Scheme_PrivateKey : public virtual IF_Scheme_PublicKey,
public virtual Private_Key
{
public:
bool check_key(RandomNumberGenerator& rng, bool) const;
/**
* Get the first prime p.
* @return the prime p
*/
const BigInt& get_p() const { return p; }
/**
* Get the second prime q.
* @return the prime q
*/
const BigInt& get_q() const { return q; }
/**
* Get d with exp * d = 1 mod (p - 1, q - 1).
* @return d
*/
const BigInt& get_d() const { return d; }
PKCS8_Encoder* pkcs8_encoder() const;
PKCS8_Decoder* pkcs8_decoder(RandomNumberGenerator&);
protected:
virtual void PKCS8_load_hook(RandomNumberGenerator&, bool = false);
BigInt d, p, q, d1, d2, c;
};
}
#endif

View File

@@ -0,0 +1,45 @@
/*
* IF Algorithm Core
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_IF_CORE_H__
#define BOTAN_IF_CORE_H__
#include <botan/if_op.h>
#include <botan/blinding.h>
namespace Botan {
/*
* IF Core
*/
class BOTAN_DLL 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&);
IF_Core(RandomNumberGenerator& rng,
const BigInt&, const BigInt&,
const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&, const BigInt&);
~IF_Core() { delete op; }
private:
IF_Operation* op;
Blinder blinder;
};
}
#endif

View File

@@ -0,0 +1,52 @@
/*
* IF Operations
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_IF_OP_H__
#define BOTAN_IF_OP_H__
#include <botan/bigint.h>
#include <botan/pow_mod.h>
#include <botan/reducer.h>
namespace Botan {
/*
* IF Operation
*/
class BOTAN_DLL 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() {}
};
/*
* Default IF Operation
*/
class BOTAN_DLL Default_IF_Op : public IF_Operation
{
public:
BigInt public_op(const BigInt& i) const
{ return powermod_e_n(i); }
BigInt private_op(const BigInt&) const;
IF_Operation* clone() const { return new Default_IF_Op(*this); }
Default_IF_Op(const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&, const BigInt&,
const BigInt&, const BigInt&);
private:
Fixed_Exponent_Power_Mod powermod_e_n, powermod_d1_p, powermod_d2_q;
Modular_Reducer reducer;
BigInt c, q;
};
}
#endif

View File

@@ -0,0 +1,41 @@
/**
* Library Initialization
* (C) 1999-2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_LIBRARY_INITIALIZER_H__
#define BOTAN_LIBRARY_INITIALIZER_H__
#include <botan/build.h>
#include <string>
namespace Botan {
/**
* This class represents the Library Initialization/Shutdown Object. It
* has to exceed the lifetime of any Botan object used in an
* application. You can call initialize/deinitialize or use
* LibraryInitializer in the RAII style.
*/
class BOTAN_DLL LibraryInitializer
{
public:
static void initialize(const std::string& options = "");
static void deinitialize();
/**
* Initialize the library
* @param thread_safe if the library should use a thread-safe mutex
*/
LibraryInitializer(const std::string& options = "")
{ LibraryInitializer::initialize(options); }
~LibraryInitializer() { LibraryInitializer::deinitialize(); }
};
}
#endif

View File

@@ -0,0 +1,36 @@
/*
* KASUMI
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_KASUMI_H__
#define BOTAN_KASUMI_H__
#include <botan/block_cipher.h>
namespace Botan {
/*
* KASUMI
*/
class BOTAN_DLL 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_schedule(const byte[], u32bit);
SecureBuffer<u16bit, 64> EK;
};
}
#endif

Some files were not shown because too many files have changed in this diff Show More