aboutsummaryrefslogtreecommitdiffstats
path: root/src/emsa2.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-06-27 15:37:29 +0000
committerlloyd <[email protected]>2008-06-27 15:37:29 +0000
commit9d77dd59ab4a7d122a20b0d89ae081a177f38287 (patch)
tree9a6c0bbda944a7494932cef883d348d1f16f804e /src/emsa2.cpp
parent59865d69871bdd936cf77a7fde38aa7657d04c40 (diff)
Reorganize the EMSA classes, and remove the last references to prng_reference
in the library ccode.
Diffstat (limited to 'src/emsa2.cpp')
-rw-r--r--src/emsa2.cpp77
1 files changed, 56 insertions, 21 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;
+ }
}
/*************************************************