8 #include <botan/emsa_x931.h>
9 #include <botan/hash_id.h>
15 secure_vector<uint8_t> emsa2_encoding(
const secure_vector<uint8_t>& msg,
17 const secure_vector<uint8_t>& empty_hash,
20 const size_t HASH_SIZE = empty_hash.size();
22 size_t output_length = (output_bits + 1) / 8;
24 if(msg.size() != HASH_SIZE)
25 throw Encoding_Error(
"EMSA_X931::encoding_of: Bad input length");
26 if(output_length < HASH_SIZE + 4)
27 throw Encoding_Error(
"EMSA_X931::encoding_of: Output length is too small");
29 const bool empty_input = (msg == empty_hash);
31 secure_vector<uint8_t> output(output_length);
33 output[0] = (empty_input ? 0x4B : 0x6B);
34 output[output_length - 3 - HASH_SIZE] = 0xBA;
35 set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB);
36 buffer_insert(output, output_length - (HASH_SIZE + 2), msg.data(), msg.size());
37 output[output_length-2] = hash_id;
38 output[output_length-1] = 0xCC;
45 void EMSA_X931::update(
const uint8_t input[],
size_t length)
47 m_hash->update(input, length);
50 secure_vector<uint8_t> EMSA_X931::raw_data()
52 return m_hash->final();
58 secure_vector<uint8_t> EMSA_X931::encoding_of(
const secure_vector<uint8_t>& msg,
60 RandomNumberGenerator&)
62 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
68 bool EMSA_X931::verify(
const secure_vector<uint8_t>& coded,
69 const secure_vector<uint8_t>& raw,
74 return (coded == emsa2_encoding(raw, key_bits,
75 m_empty_hash, m_hash_id));
88 m_empty_hash = m_hash->final();
virtual std::string name() const =0
uint8_t ieee1363_hash_id(const std::string &name)
void set_mem(T *ptr, size_t n, uint8_t val)
EMSA_X931(HashFunction *hash)
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
std::unique_ptr< HashFunction > m_hash