8#include <botan/internal/emsa_x931.h>
10#include <botan/exceptn.h>
11#include <botan/internal/hash_id.h>
12#include <botan/internal/stl_util.h>
18std::vector<uint8_t> emsa2_encoding(
const std::vector<uint8_t>& msg,
20 const std::vector<uint8_t>& empty_hash,
22 const size_t HASH_SIZE = empty_hash.size();
24 const size_t output_length = (output_bits + 1) / 8;
26 if(msg.size() != HASH_SIZE) {
27 throw Encoding_Error(
"EMSA_X931::encoding_of: Bad input length");
29 if(output_length < HASH_SIZE + 4) {
30 throw Encoding_Error(
"EMSA_X931::encoding_of: Output length is too small");
33 const bool empty_input = (msg == empty_hash);
35 std::vector<uint8_t> output(output_length);
36 BufferStuffer stuffer(output);
38 stuffer.append(empty_input ? 0x4B : 0x6B);
39 stuffer.append(0xBB, stuffer.remaining_capacity() - (1 + msg.size() + 2));
42 stuffer.append(hash_id);
52 return "EMSA2(" + m_hash->name() +
")";
55void EMSA_X931::update(
const uint8_t input[],
size_t length) {
56 m_hash->update(input, length);
59std::vector<uint8_t> EMSA_X931::raw_data() {
60 return m_hash->final_stdvec();
66std::vector<uint8_t> EMSA_X931::encoding_of(
const std::vector<uint8_t>& msg,
68 RandomNumberGenerator& ) {
69 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
75bool EMSA_X931::verify(
const std::vector<uint8_t>& coded,
const std::vector<uint8_t>& raw,
size_t key_bits) {
77 return (coded == emsa2_encoding(raw, key_bits, m_empty_hash, m_hash_id));
87 m_empty_hash = m_hash->final_stdvec();
92 throw Encoding_Error(
"EMSA_X931 no hash identifier for " + m_hash->name());
#define BOTAN_ASSERT_NOMSG(expr)
EMSA_X931(std::unique_ptr< HashFunction > hash)
std::string name() const override
uint8_t ieee1363_hash_id(std::string_view name)