8 #include <botan/emsa2.h>
9 #include <botan/hash_id.h>
18 SecureVector<byte> emsa2_encoding(
const MemoryRegion<byte>& msg,
20 const MemoryRegion<byte>& empty_hash,
23 const size_t HASH_SIZE = empty_hash.size();
25 size_t output_length = (output_bits + 1) / 8;
27 if(msg.size() != HASH_SIZE)
28 throw Encoding_Error(
"EMSA2::encoding_of: Bad input length");
29 if(output_length < HASH_SIZE + 4)
30 throw Encoding_Error(
"EMSA2::encoding_of: Output length is too small");
33 for(
size_t j = 0; j != HASH_SIZE; ++j)
34 if(empty_hash[j] != msg[j])
37 SecureVector<byte> output(output_length);
39 output[0] = (empty ? 0x4B : 0x6B);
40 output[output_length - 3 - HASH_SIZE] = 0xBA;
41 set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB);
42 output.copy(output_length - (HASH_SIZE + 2), &msg[0], msg.size());
43 output[output_length-2] = hash_id;
44 output[output_length-1] = 0xCC;
54 void EMSA2::update(
const byte input[],
size_t length)
56 hash->
update(input, length);
62 SecureVector<byte> EMSA2::raw_data()
70 SecureVector<byte> EMSA2::encoding_of(
const MemoryRegion<byte>& msg,
72 RandomNumberGenerator&)
74 return emsa2_encoding(msg, output_bits, empty_hash, hash_id);
80 bool EMSA2::verify(
const MemoryRegion<byte>& coded,
81 const MemoryRegion<byte>& raw,
86 return (coded == emsa2_encoding(raw, key_bits,
87 empty_hash, hash_id));
100 empty_hash = hash->
final();
102 const std::string hash_name = hash->
name();
byte ieee1363_hash_id(const std::string &name)
void update(const byte in[], size_t length)
EMSA2(HashFunction *hash)
virtual std::string name() const =0
void set_mem(T *ptr, size_t n, byte val)