diff options
author | lloyd <[email protected]> | 2008-09-28 17:41:22 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 17:41:22 +0000 |
commit | d84014e4b717da0618fc2f4c85dd1ef41ac6d0fa (patch) | |
tree | 405638113ed2a72764d71d16bbebbf7597302cfc /modules/pk_pad | |
parent | 8c085b3c40c30607ca5cac10d04062355fcad187 (diff) |
Modularize EME1 and PKCS #1 v1.5 EME
Diffstat (limited to 'modules/pk_pad')
-rw-r--r-- | modules/pk_pad/eme1/eme1.cpp | 95 | ||||
-rw-r--r-- | modules/pk_pad/eme1/eme1.h | 35 | ||||
-rw-r--r-- | modules/pk_pad/eme_pkcs/eme_pkcs.cpp | 68 | ||||
-rw-r--r-- | modules/pk_pad/emsa1/emsa1.cpp | 2 | ||||
-rw-r--r-- | modules/pk_pad/emsa2/emsa2.cpp | 2 | ||||
-rw-r--r-- | modules/pk_pad/emsa3/emsa3.cpp | 2 | ||||
-rw-r--r-- | modules/pk_pad/emsa4/emsa4.cpp | 2 | ||||
-rw-r--r-- | modules/pk_pad/emsa_raw/emsa_raw.cpp | 2 |
8 files changed, 203 insertions, 5 deletions
diff --git a/modules/pk_pad/eme1/eme1.cpp b/modules/pk_pad/eme1/eme1.cpp new file mode 100644 index 000000000..2ca10c166 --- /dev/null +++ b/modules/pk_pad/eme1/eme1.cpp @@ -0,0 +1,95 @@ +/************************************************* +* EME1 Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/eme1.h> +#include <botan/lookup.h> +#include <memory> + +namespace Botan { + +/************************************************* +* EME1 Pad Operation * +*************************************************/ +SecureVector<byte> EME1::pad(const byte in[], u32bit in_length, + u32bit key_length, + RandomNumberGenerator& rng) const + { + key_length /= 8; + + if(in_length > key_length - 2*HASH_LENGTH - 1) + throw Exception("EME1: Input is too large"); + + SecureVector<byte> out(key_length); + + out.clear(); + + rng.randomize(out, HASH_LENGTH); + + out.copy(HASH_LENGTH, Phash, Phash.size()); + out[out.size() - in_length - 1] = 0x01; + out.copy(out.size() - in_length, in, in_length); + mgf->mask(out, HASH_LENGTH, out + HASH_LENGTH, out.size() - HASH_LENGTH); + mgf->mask(out + HASH_LENGTH, out.size() - HASH_LENGTH, out, HASH_LENGTH); + + return out; + } + +/************************************************* +* EME1 Unpad Operation * +*************************************************/ +SecureVector<byte> EME1::unpad(const byte in[], u32bit in_length, + u32bit key_length) const + { + key_length /= 8; + if(in_length > key_length) + throw Decoding_Error("Invalid EME1 encoding"); + + SecureVector<byte> tmp(key_length); + tmp.copy(key_length - in_length, in, in_length); + + mgf->mask(tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH, tmp, HASH_LENGTH); + mgf->mask(tmp, HASH_LENGTH, tmp + HASH_LENGTH, tmp.size() - HASH_LENGTH); + + for(u32bit j = 0; j != Phash.size(); ++j) + if(tmp[j+HASH_LENGTH] != Phash[j]) + throw Decoding_Error("Invalid EME1 encoding"); + + for(u32bit j = HASH_LENGTH + Phash.size(); j != tmp.size(); ++j) + { + if(tmp[j] && tmp[j] != 0x01) + throw Decoding_Error("Invalid EME1 encoding"); + if(tmp[j] && tmp[j] == 0x01) + { + SecureVector<byte> retval(tmp + j + 1, tmp.size() - j - 1); + return retval; + } + } + throw Decoding_Error("Invalid EME1 encoding"); + } + +/************************************************* +* Return the max input size for a given key size * +*************************************************/ +u32bit EME1::maximum_input_size(u32bit keybits) const + { + if(keybits / 8 > 2*HASH_LENGTH + 1) + return ((keybits / 8) - 2*HASH_LENGTH - 1); + else + return 0; + } + +/************************************************* +* EME1 Constructor * +*************************************************/ +EME1::EME1(const std::string& hash_name, const std::string& mgf_name, + const std::string& P) : + HASH_LENGTH(output_length_of(hash_name)) + { + mgf = get_mgf(mgf_name + "(" + hash_name + ")"); + std::auto_ptr<HashFunction> hash(get_hash(hash_name)); + Phash = hash->process(P); + } + +} diff --git a/modules/pk_pad/eme1/eme1.h b/modules/pk_pad/eme1/eme1.h new file mode 100644 index 000000000..2a0ac92c4 --- /dev/null +++ b/modules/pk_pad/eme1/eme1.h @@ -0,0 +1,35 @@ +/************************************************* +* EME1 Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_EME1_H__ +#define BOTAN_EME1_H__ + +#include <botan/pk_util.h> + +namespace Botan { + +/************************************************* +* EME1 * +*************************************************/ +class BOTAN_DLL EME1 : public EME + { + public: + u32bit maximum_input_size(u32bit) const; + + EME1(const std::string&, const std::string&, const std::string& = ""); + ~EME1() { delete mgf; } + private: + SecureVector<byte> pad(const byte[], u32bit, u32bit, + RandomNumberGenerator&) const; + SecureVector<byte> unpad(const byte[], u32bit, u32bit) const; + + const u32bit HASH_LENGTH; + SecureVector<byte> Phash; + MGF* mgf; + }; + +} + +#endif diff --git a/modules/pk_pad/eme_pkcs/eme_pkcs.cpp b/modules/pk_pad/eme_pkcs/eme_pkcs.cpp new file mode 100644 index 000000000..494f238b4 --- /dev/null +++ b/modules/pk_pad/eme_pkcs/eme_pkcs.cpp @@ -0,0 +1,68 @@ +/************************************************* +* PKCS1 EME Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/eme_pkcs.h> + +namespace Botan { + +/************************************************* +* PKCS1 Pad Operation * +*************************************************/ +SecureVector<byte> EME_PKCS1v15::pad(const byte in[], u32bit inlen, + u32bit olen, + RandomNumberGenerator& rng) const + { + olen /= 8; + + if(olen < 10) + throw Encoding_Error("PKCS1: Output space too small"); + if(inlen > olen - 10) + throw Encoding_Error("PKCS1: Input is too large"); + + SecureVector<byte> out(olen); + + out[0] = 0x02; + for(u32bit j = 1; j != olen - inlen - 1; ++j) + while(out[j] == 0) + out[j] = rng.next_byte(); + out.copy(olen - inlen, in, inlen); + + return out; + } + +/************************************************* +* PKCS1 Unpad Operation * +*************************************************/ +SecureVector<byte> EME_PKCS1v15::unpad(const byte in[], u32bit inlen, + u32bit key_len) const + { + if(inlen != key_len / 8 || inlen < 10 || in[0] != 0x02) + throw Decoding_Error("PKCS1::unpad"); + + u32bit seperator = 0; + for(u32bit j = 0; j != inlen; ++j) + if(in[j] == 0) + { + seperator = j; + break; + } + if(seperator < 9) + throw Decoding_Error("PKCS1::unpad"); + + return SecureVector<byte>(in + seperator + 1, inlen - seperator - 1); + } + +/************************************************* +* Return the max input size for a given key size * +*************************************************/ +u32bit EME_PKCS1v15::maximum_input_size(u32bit keybits) const + { + if(keybits / 8 > 10) + return ((keybits / 8) - 10); + else + return 0; + } + +} diff --git a/modules/pk_pad/emsa1/emsa1.cpp b/modules/pk_pad/emsa1/emsa1.cpp index 8977356e8..15d53f6e5 100644 --- a/modules/pk_pad/emsa1/emsa1.cpp +++ b/modules/pk_pad/emsa1/emsa1.cpp @@ -3,7 +3,7 @@ * (C) 1999-2007 Jack Lloyd * *************************************************/ -#include <botan/emsa.h> +#include <botan/emsa1.h> #include <botan/lookup.h> namespace Botan { diff --git a/modules/pk_pad/emsa2/emsa2.cpp b/modules/pk_pad/emsa2/emsa2.cpp index 825329e7e..24292d390 100644 --- a/modules/pk_pad/emsa2/emsa2.cpp +++ b/modules/pk_pad/emsa2/emsa2.cpp @@ -3,7 +3,7 @@ * (C) 1999-2007 Jack Lloyd * *************************************************/ -#include <botan/emsa.h> +#include <botan/emsa2.h> #include <botan/hash_id.h> #include <botan/lookup.h> diff --git a/modules/pk_pad/emsa3/emsa3.cpp b/modules/pk_pad/emsa3/emsa3.cpp index 35a9f6fe3..60880d1aa 100644 --- a/modules/pk_pad/emsa3/emsa3.cpp +++ b/modules/pk_pad/emsa3/emsa3.cpp @@ -3,7 +3,7 @@ * (C) 1999-2007 Jack Lloyd * *************************************************/ -#include <botan/emsa.h> +#include <botan/emsa3.h> #include <botan/hash_id.h> #include <botan/lookup.h> diff --git a/modules/pk_pad/emsa4/emsa4.cpp b/modules/pk_pad/emsa4/emsa4.cpp index 6fb63fe0a..9821c1f13 100644 --- a/modules/pk_pad/emsa4/emsa4.cpp +++ b/modules/pk_pad/emsa4/emsa4.cpp @@ -3,7 +3,7 @@ * (C) 1999-2007 Jack Lloyd * *************************************************/ -#include <botan/emsa.h> +#include <botan/emsa4.h> #include <botan/lookup.h> #include <botan/look_pk.h> #include <botan/bit_ops.h> diff --git a/modules/pk_pad/emsa_raw/emsa_raw.cpp b/modules/pk_pad/emsa_raw/emsa_raw.cpp index 037e22375..c10bb4890 100644 --- a/modules/pk_pad/emsa_raw/emsa_raw.cpp +++ b/modules/pk_pad/emsa_raw/emsa_raw.cpp @@ -3,7 +3,7 @@ * (C) 1999-2007 Jack Lloyd * *************************************************/ -#include <botan/emsa.h> +#include <botan/emsa_raw.h> namespace Botan { |