8#include <botan/internal/emsa_x931.h>
10#include <botan/exceptn.h>
11#include <botan/internal/fmt.h>
12#include <botan/internal/hash_id.h>
13#include <botan/internal/stl_util.h>
19std::vector<uint8_t> emsa2_encoding(
const std::vector<uint8_t>& msg,
21 const std::vector<uint8_t>& empty_hash,
23 const size_t HASH_SIZE = empty_hash.size();
25 const size_t output_length = (output_bits + 1) / 8;
27 if(msg.size() != HASH_SIZE) {
28 throw Encoding_Error(
"EMSA_X931::encoding_of: Bad input length");
30 if(output_length < HASH_SIZE + 4) {
31 throw Encoding_Error(
"EMSA_X931::encoding_of: Output length is too small");
34 const bool empty_input = (msg == empty_hash);
36 std::vector<uint8_t> output(output_length);
37 BufferStuffer stuffer(output);
39 stuffer.append(empty_input ? 0x4B : 0x6B);
40 stuffer.append(0xBB, stuffer.remaining_capacity() - (1 + msg.size() + 2));
43 stuffer.append(hash_id);
53 return fmt(
"X9.31({})", m_hash->name());
56void EMSA_X931::update(
const uint8_t input[],
size_t length) {
57 m_hash->update(input, length);
60std::vector<uint8_t> EMSA_X931::raw_data() {
61 return m_hash->final_stdvec();
67std::vector<uint8_t> EMSA_X931::encoding_of(
const std::vector<uint8_t>& msg,
69 RandomNumberGenerator& ) {
70 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
76bool EMSA_X931::verify(
const std::vector<uint8_t>& coded,
const std::vector<uint8_t>& raw,
size_t key_bits) {
78 return (coded == emsa2_encoding(raw, key_bits, m_empty_hash, m_hash_id));
88 m_empty_hash = m_hash->final_stdvec();
93 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
std::string fmt(std::string_view format, const T &... args)
uint8_t ieee1363_hash_id(std::string_view name)