diff options
author | lloyd <[email protected]> | 2008-06-27 15:37:29 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-06-27 15:37:29 +0000 |
commit | 9d77dd59ab4a7d122a20b0d89ae081a177f38287 (patch) | |
tree | 9a6c0bbda944a7494932cef883d348d1f16f804e /src | |
parent | 59865d69871bdd936cf77a7fde38aa7657d04c40 (diff) |
Reorganize the EMSA classes, and remove the last references to prng_reference
in the library ccode.
Diffstat (limited to 'src')
-rw-r--r-- | src/emsa2.cpp | 77 | ||||
-rw-r--r-- | src/emsa3.cpp | 66 | ||||
-rw-r--r-- | src/emsa_raw.cpp | 10 | ||||
-rw-r--r-- | src/pk_util.cpp | 20 | ||||
-rw-r--r-- | src/pubkey.cpp | 11 |
5 files changed, 126 insertions, 58 deletions
diff --git a/src/emsa2.cpp b/src/emsa2.cpp index f8d69737a..825329e7e 100644 --- a/src/emsa2.cpp +++ b/src/emsa2.cpp @@ -9,6 +9,44 @@ namespace Botan { +namespace { + +/************************************************* +* EMSA2 Encode Operation * +*************************************************/ +SecureVector<byte> emsa2_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits, + const MemoryRegion<byte>& empty_hash, + byte hash_id) + { + const u32bit HASH_SIZE = empty_hash.size(); + + u32bit output_length = (output_bits + 1) / 8; + + if(msg.size() != HASH_SIZE) + throw Encoding_Error("EMSA2::encoding_of: Bad input length"); + if(output_length < HASH_SIZE + 4) + throw Encoding_Error("EMSA2::encoding_of: Output length is too small"); + + bool empty = true; + for(u32bit j = 0; j != HASH_SIZE; ++j) + if(empty_hash[j] != msg[j]) + empty = false; + + SecureVector<byte> output(output_length); + + output[0] = (empty ? 0x4B : 0x6B); + output[output_length - 3 - HASH_SIZE] = 0xBA; + set_mem(output + 1, output_length - 4 - HASH_SIZE, 0xBB); + output.copy(output_length - (HASH_SIZE + 2), msg, msg.size()); + output[output_length-2] = hash_id; + output[output_length-1] = 0xCC; + + return output; + } + +} + /************************************************* * EMSA2 Update Operation * *************************************************/ @@ -32,28 +70,25 @@ SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg, u32bit output_bits, RandomNumberGenerator&) { - u32bit output_length = (output_bits + 1) / 8; - - if(msg.size() != hash->OUTPUT_LENGTH) - throw Encoding_Error("EMSA2::encoding_of: Bad input length"); - if(output_length < hash->OUTPUT_LENGTH + 4) - throw Encoding_Error("EMSA2::encoding_of: Output length is too small"); - - bool empty = true; - for(u32bit j = 0; j != hash->OUTPUT_LENGTH; ++j) - if(empty_hash[j] != msg[j]) - empty = false; - - SecureVector<byte> output(output_length); - - output[0] = (empty ? 0x4B : 0x6B); - output[output_length - 3 - hash->OUTPUT_LENGTH] = 0xBA; - set_mem(output + 1, output_length - 4 - hash->OUTPUT_LENGTH, 0xBB); - output.copy(output_length-2-hash->OUTPUT_LENGTH, msg, msg.size()); - output[output_length-2] = hash_id; - output[output_length-1] = 0xCC; + return emsa2_encoding(msg, output_bits, empty_hash, hash_id); + } - return output; +/************************************************* +* EMSA2 Verify Operation * +*************************************************/ +bool EMSA2::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + try + { + return (coded == emsa2_encoding(raw, key_bits, + empty_hash, hash_id)); + } + catch(...) + { + return false; + } } /************************************************* diff --git a/src/emsa3.cpp b/src/emsa3.cpp index cf1aae3fa..35a9f6fe3 100644 --- a/src/emsa3.cpp +++ b/src/emsa3.cpp @@ -9,6 +9,36 @@ namespace Botan { +namespace { + +/************************************************* +* EMSA3 Encode Operation * +*************************************************/ +SecureVector<byte> emsa3_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits, + const MemoryRegion<byte>& hash_id, + u32bit hash_size) + { + if(msg.size() != hash_size) + throw Encoding_Error("EMSA3::encoding_of: Bad input length"); + + u32bit output_length = output_bits / 8; + if(output_length < hash_id.size() + hash_size + 10) + throw Encoding_Error("EMSA3::pad: Output length is too small"); + + SecureVector<byte> T(output_length); + const u32bit P_LENGTH = output_length - hash_size - hash_id.size() - 2; + + T[0] = 0x01; + set_mem(T+1, P_LENGTH, 0xFF); + T[P_LENGTH+1] = 0x00; + T.copy(P_LENGTH+2, hash_id, hash_id.size()); + T.copy(output_length-hash_size, msg, msg.size()); + return T; + } + +} + /************************************************* * EMSA3 Update Operation * *************************************************/ @@ -32,23 +62,27 @@ SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg, u32bit output_bits, RandomNumberGenerator&) { - if(msg.size() != hash->OUTPUT_LENGTH) - throw Encoding_Error("EMSA3::encoding_of: Bad input length"); - - u32bit output_length = output_bits / 8; - if(output_length < hash_id.size() + hash->OUTPUT_LENGTH + 10) - throw Encoding_Error("EMSA3::pad: Output length is too small"); - - SecureVector<byte> T(output_length); - const u32bit P_LENGTH = output_length - hash->OUTPUT_LENGTH - - hash_id.size() - 2; + return emsa3_encoding(msg, output_bits, hash_id, + hash->OUTPUT_LENGTH); + } - T[0] = 0x01; - set_mem(T+1, P_LENGTH, 0xFF); - T[P_LENGTH+1] = 0x00; - T.copy(P_LENGTH+2, hash_id, hash_id.size()); - T.copy(output_length-hash->OUTPUT_LENGTH, msg, msg.size()); - return T; +/************************************************* +* Default signature decoding * +*************************************************/ +bool EMSA3::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + try + { + return (coded == emsa3_encoding(raw, key_bits, + hash_id, + hash->OUTPUT_LENGTH)); + } + catch(...) + { + return false; + } } /************************************************* diff --git a/src/emsa_raw.cpp b/src/emsa_raw.cpp index b7ca16195..4955456ff 100644 --- a/src/emsa_raw.cpp +++ b/src/emsa_raw.cpp @@ -35,4 +35,14 @@ SecureVector<byte> EMSA_Raw::encoding_of(const MemoryRegion<byte>& msg, return msg; } +/************************************************* +* EMSA-Raw Verify Operation * +*************************************************/ +bool EMSA_Raw::verify(const MemoryRegion<byte>& coded, + const MemoryRegion<byte>& raw, + u32bit key_bits) throw() + { + return (coded == raw); + } + } diff --git a/src/pk_util.cpp b/src/pk_util.cpp index 24499ad83..1976436ea 100644 --- a/src/pk_util.cpp +++ b/src/pk_util.cpp @@ -1,10 +1,9 @@ /************************************************* * PK Utility Classes Source File * -* (C) 1999-2007 Jack Lloyd * +* (C) 1999-2008 Jack Lloyd * *************************************************/ #include <botan/pk_util.h> -#include <botan/libstate.h> namespace Botan { @@ -46,21 +45,4 @@ SecureVector<byte> EME::decode(const MemoryRegion<byte>& msg, return unpad(msg, msg.size(), key_bits); } -/************************************************* -* Default signature decoding * -*************************************************/ -bool EMSA::verify(const MemoryRegion<byte>& coded, - const MemoryRegion<byte>& raw, - u32bit key_bits) throw() - { - try { - return (coded == encoding_of(raw, key_bits, - global_state().prng_reference())); - } - catch(Invalid_Argument) - { - return false; - } - } - } diff --git a/src/pubkey.cpp b/src/pubkey.cpp index d51bed70f..f52208b3d 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -10,7 +10,6 @@ #include <botan/bigint.h> #include <botan/parsing.h> #include <botan/bit_ops.h> -#include <botan/libstate.h> #include <memory> namespace Botan { @@ -368,7 +367,15 @@ PK_Verifier_wo_MR::PK_Verifier_wo_MR(const PK_Verifying_wo_MR_Key& k, bool PK_Verifier_wo_MR::validate_signature(const MemoryRegion<byte>& msg, const byte sig[], u32bit sig_len) { - RandomNumberGenerator& rng = global_state().prng_reference(); + class Null_RNG : public RandomNumberGenerator + { + public: + void randomize(byte[], u32bit) { throw PRNG_Unseeded("Null_RNG"); } + bool is_seeded() const { return false; } + void add_randomness(const byte[], u32bit) {} + }; + + Null_RNG rng; SecureVector<byte> encoded = emsa->encoding_of(msg, key.max_input_bits(), rng); |