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 /src | |
parent | 8c085b3c40c30607ca5cac10d04062355fcad187 (diff) |
Modularize EME1 and PKCS #1 v1.5 EME
Diffstat (limited to 'src')
-rw-r--r-- | src/eme1.cpp | 95 | ||||
-rw-r--r-- | src/eme_pkcs.cpp | 68 | ||||
-rw-r--r-- | src/get_enc.cpp | 13 |
3 files changed, 12 insertions, 164 deletions
diff --git a/src/eme1.cpp b/src/eme1.cpp deleted file mode 100644 index ca96e29e0..000000000 --- a/src/eme1.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/************************************************* -* EME1 Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/eme.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/src/eme_pkcs.cpp b/src/eme_pkcs.cpp deleted file mode 100644 index 9a9f463ea..000000000 --- a/src/eme_pkcs.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/************************************************* -* PKCS1 EME Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/eme.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/src/get_enc.cpp b/src/get_enc.cpp index 98cba0ad2..77799d318 100644 --- a/src/get_enc.cpp +++ b/src/get_enc.cpp @@ -6,7 +6,6 @@ #include <botan/lookup.h> #include <botan/libstate.h> #include <botan/parsing.h> -#include <botan/eme.h> #include <botan/kdf.h> #include <botan/mgf1.h> #include <botan/util.h> @@ -31,6 +30,14 @@ #include <botan/emsa_raw.h> #endif +#ifdef BOTAN_HAS_EME1 + #include <botan/eme1.h> +#endif + +#ifdef BOTAN_HAS_EME_PKCS1v15 + #include <botan/eme_pkcs.h> +#endif + namespace Botan { /************************************************* @@ -96,12 +103,15 @@ EME* get_eme(const std::string& algo_spec) std::vector<std::string> name = parse_algorithm_name(algo_spec); const std::string eme_name = global_state().deref_alias(name[0]); +#ifdef BOTAN_HAS_EME_PKCS1v15 if(eme_name == "PKCS1v15") { if(name.size() == 1) return new EME_PKCS1v15; } +#endif +#ifdef BOTAN_HAS_EME1 if(eme_name == "EME1") { if(name.size() == 2) @@ -109,6 +119,7 @@ EME* get_eme(const std::string& algo_spec) if(name.size() == 3) return new EME1(name[1], name[2]); } +#endif throw Algorithm_Not_Found(algo_spec); } |