8#include <botan/emsa_x931.h>
9#include <botan/exceptn.h>
10#include <botan/hash_id.h>
16secure_vector<uint8_t> emsa2_encoding(
const secure_vector<uint8_t>& msg,
18 const secure_vector<uint8_t>& empty_hash,
21 const size_t HASH_SIZE = empty_hash.size();
23 size_t output_length = (output_bits + 1) / 8;
25 if(msg.size() != HASH_SIZE)
26 throw Encoding_Error(
"EMSA_X931::encoding_of: Bad input length");
27 if(output_length < HASH_SIZE + 4)
28 throw Encoding_Error(
"EMSA_X931::encoding_of: Output length is too small");
30 const bool empty_input = (msg == empty_hash);
32 secure_vector<uint8_t> output(output_length);
34 output[0] = (empty_input ? 0x4B : 0x6B);
35 output[output_length - 3 - HASH_SIZE] = 0xBA;
36 set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB);
37 buffer_insert(output, output_length - (HASH_SIZE + 2), msg.data(), msg.size());
38 output[output_length-2] = hash_id;
39 output[output_length-1] = 0xCC;
48 return "EMSA2(" + m_hash->name() +
")";
51void EMSA_X931::update(
const uint8_t input[],
size_t length)
53 m_hash->update(input, length);
56secure_vector<uint8_t> EMSA_X931::raw_data()
58 return m_hash->final();
64secure_vector<uint8_t> EMSA_X931::encoding_of(
const secure_vector<uint8_t>& msg,
66 RandomNumberGenerator&)
68 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
74bool EMSA_X931::verify(
const secure_vector<uint8_t>& coded,
75 const secure_vector<uint8_t>& raw,
80 return (coded == emsa2_encoding(raw, key_bits,
81 m_empty_hash, m_hash_id));
94 m_empty_hash = m_hash->final();
std::string name() const override
EMSA_X931(HashFunction *hash)
void set_mem(uint8_t *ptr, size_t n, uint8_t val)
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
uint8_t ieee1363_hash_id(const std::string &name)