diff options
-rw-r--r-- | include/emsa.h | 25 | ||||
-rw-r--r-- | include/pk_util.h | 5 | ||||
-rw-r--r-- | src/emsa1.cpp | 60 | ||||
-rw-r--r-- | src/emsa2.cpp | 3 | ||||
-rw-r--r-- | src/emsa3.cpp | 3 | ||||
-rw-r--r-- | src/emsa4.cpp | 6 | ||||
-rw-r--r-- | src/emsa_raw.cpp | 3 | ||||
-rw-r--r-- | src/pk_util.cpp | 4 | ||||
-rw-r--r-- | src/pubkey.cpp | 15 |
9 files changed, 84 insertions, 40 deletions
diff --git a/include/emsa.h b/include/emsa.h index 7a9367dc9..21992ed94 100644 --- a/include/emsa.h +++ b/include/emsa.h @@ -20,10 +20,14 @@ class BOTAN_DLL EMSA1 : public EMSA ~EMSA1() { delete hash; } private: void update(const byte[], u32bit); - SecureVector<byte> encoding_of(const MemoryRegion<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; }; @@ -37,8 +41,11 @@ class BOTAN_DLL EMSA2 : public EMSA ~EMSA2() { delete hash; } private: void update(const byte[], u32bit); - SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + SecureVector<byte> empty_hash; HashFunction* hash; byte hash_id; @@ -54,8 +61,12 @@ class BOTAN_DLL EMSA3 : public EMSA ~EMSA3() { delete hash; } private: void update(const byte[], u32bit); - SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> raw_data(); + + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator& rng); + HashFunction* hash; SecureVector<byte> hash_id; }; @@ -71,10 +82,13 @@ class BOTAN_DLL EMSA4 : public EMSA ~EMSA4() { delete hash; delete mgf; } private: void update(const byte[], u32bit); - SecureVector<byte> encoding_of(const MemoryRegion<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(); + const u32bit SALT_SIZE; HashFunction* hash; const MGF* mgf; @@ -87,7 +101,8 @@ class BOTAN_DLL EMSA_Raw : public EMSA { private: void update(const byte[], u32bit); - SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit); + SecureVector<byte> encoding_of(const MemoryRegion<byte>&, u32bit, + RandomNumberGenerator&); SecureVector<byte> raw_data(); SecureVector<byte> message; }; diff --git a/include/pk_util.h b/include/pk_util.h index 1803a59a2..86b8859fd 100644 --- a/include/pk_util.h +++ b/include/pk_util.h @@ -42,8 +42,11 @@ 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) = 0; + u32bit, + RandomNumberGenerator& rng) = 0; + virtual bool verify(const MemoryRegion<byte>&, const MemoryRegion<byte>&, u32bit) throw(); virtual ~EMSA() {} diff --git a/src/emsa1.cpp b/src/emsa1.cpp index dd26342c2..8977356e8 100644 --- a/src/emsa1.cpp +++ b/src/emsa1.cpp @@ -8,30 +8,11 @@ namespace Botan { -/************************************************* -* EMSA1 Update Operation * -*************************************************/ -void EMSA1::update(const byte input[], u32bit length) - { - hash->update(input, length); - } +namespace { -/************************************************* -* Return the raw (unencoded) data * -*************************************************/ -SecureVector<byte> EMSA1::raw_data() +SecureVector<byte> emsa1_encoding(const MemoryRegion<byte>& msg, + u32bit output_bits) { - return hash->final(); - } - -/************************************************* -* EMSA1 Encode Operation * -*************************************************/ -SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg, - u32bit output_bits) - { - if(msg.size() != hash->OUTPUT_LENGTH) - throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); if(8*msg.size() <= output_bits) return msg; @@ -56,6 +37,36 @@ SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg, return digest; } +} + +/************************************************* +* EMSA1 Update Operation * +*************************************************/ +void EMSA1::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/************************************************* +* Return the raw (unencoded) data * +*************************************************/ +SecureVector<byte> EMSA1::raw_data() + { + return hash->final(); + } + +/************************************************* +* EMSA1 Encode Operation * +*************************************************/ +SecureVector<byte> EMSA1::encoding_of(const MemoryRegion<byte>& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + if(msg.size() != hash->OUTPUT_LENGTH) + throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); + return emsa1_encoding(msg, output_bits); + } + /************************************************* * EMSA1 Decode/Verify Operation * *************************************************/ @@ -63,7 +74,10 @@ bool EMSA1::verify(const MemoryRegion<byte>& coded, const MemoryRegion<byte>& raw, u32bit key_bits) throw() { try { - SecureVector<byte> our_coding = encoding_of(raw, key_bits); + if(raw.size() != hash->OUTPUT_LENGTH) + throw Encoding_Error("EMSA1::encoding_of: Invalid size for input"); + + SecureVector<byte> our_coding = emsa1_encoding(raw, key_bits); if(our_coding == coded) return true; if(our_coding[0] != 0) return false; diff --git a/src/emsa2.cpp b/src/emsa2.cpp index 052d9226a..f8d69737a 100644 --- a/src/emsa2.cpp +++ b/src/emsa2.cpp @@ -29,7 +29,8 @@ SecureVector<byte> EMSA2::raw_data() * EMSA2 Encode Operation * *************************************************/ SecureVector<byte> EMSA2::encoding_of(const MemoryRegion<byte>& msg, - u32bit output_bits) + u32bit output_bits, + RandomNumberGenerator&) { u32bit output_length = (output_bits + 1) / 8; diff --git a/src/emsa3.cpp b/src/emsa3.cpp index 3c0aec50d..cf1aae3fa 100644 --- a/src/emsa3.cpp +++ b/src/emsa3.cpp @@ -29,7 +29,8 @@ SecureVector<byte> EMSA3::raw_data() * EMSA3 Encode Operation * *************************************************/ SecureVector<byte> EMSA3::encoding_of(const MemoryRegion<byte>& msg, - u32bit output_bits) + u32bit output_bits, + RandomNumberGenerator&) { if(msg.size() != hash->OUTPUT_LENGTH) throw Encoding_Error("EMSA3::encoding_of: Bad input length"); diff --git a/src/emsa4.cpp b/src/emsa4.cpp index 831afd590..6fb63fe0a 100644 --- a/src/emsa4.cpp +++ b/src/emsa4.cpp @@ -7,7 +7,6 @@ #include <botan/lookup.h> #include <botan/look_pk.h> #include <botan/bit_ops.h> -#include <botan/libstate.h> namespace Botan { @@ -31,7 +30,8 @@ SecureVector<byte> EMSA4::raw_data() * EMSA4 Encode Operation * *************************************************/ SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, - u32bit output_bits) + u32bit output_bits, + RandomNumberGenerator& rng) { const u32bit HASH_SIZE = hash->OUTPUT_LENGTH; @@ -43,7 +43,7 @@ SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, const u32bit output_length = (output_bits + 7) / 8; SecureVector<byte> salt(SALT_SIZE); - global_state().randomize(salt, SALT_SIZE); + rng.randomize(salt, SALT_SIZE); for(u32bit j = 0; j != 8; ++j) hash->update(0); diff --git a/src/emsa_raw.cpp b/src/emsa_raw.cpp index 7442a4f4f..b7ca16195 100644 --- a/src/emsa_raw.cpp +++ b/src/emsa_raw.cpp @@ -29,7 +29,8 @@ SecureVector<byte> EMSA_Raw::raw_data() * EMSA-Raw Encode Operation * *************************************************/ SecureVector<byte> EMSA_Raw::encoding_of(const MemoryRegion<byte>& msg, - u32bit) + u32bit, + RandomNumberGenerator&) { return msg; } diff --git a/src/pk_util.cpp b/src/pk_util.cpp index c5f7f8d67..24499ad83 100644 --- a/src/pk_util.cpp +++ b/src/pk_util.cpp @@ -4,6 +4,7 @@ *************************************************/ #include <botan/pk_util.h> +#include <botan/libstate.h> namespace Botan { @@ -53,7 +54,8 @@ bool EMSA::verify(const MemoryRegion<byte>& coded, u32bit key_bits) throw() { try { - return (coded == encoding_of(raw, key_bits)); + return (coded == encoding_of(raw, key_bits, + global_state().prng_reference())); } catch(Invalid_Argument) { diff --git a/src/pubkey.cpp b/src/pubkey.cpp index d151878c4..dc14c66b7 100644 --- a/src/pubkey.cpp +++ b/src/pubkey.cpp @@ -185,10 +185,13 @@ void PK_Signer::update(const MemoryRegion<byte>& in) *************************************************/ SecureVector<byte> PK_Signer::signature() { + RandomNumberGenerator& rng = global_state().prng_reference(); + SecureVector<byte> encoded = emsa->encoding_of(emsa->raw_data(), - key.max_input_bits()); - SecureVector<byte> plain_sig = key.sign(encoded, encoded.size(), - global_state().prng_reference()); + key.max_input_bits(), + rng); + + SecureVector<byte> plain_sig = key.sign(encoded, encoded.size(), rng); if(key.message_parts() == 1 || sig_format == IEEE_1363) return plain_sig; @@ -363,7 +366,11 @@ 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) { - SecureVector<byte> encoded = emsa->encoding_of(msg, key.max_input_bits()); + RandomNumberGenerator& rng = global_state().prng_reference(); + + SecureVector<byte> encoded = + emsa->encoding_of(msg, key.max_input_bits(), rng); + return key.verify(encoded, encoded.size(), sig, sig_len); } |