Botan 3.4.0
Crypto and TLS for C&
emsa_x931.cpp
Go to the documentation of this file.
1/*
2* EMSA_X931
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/emsa_x931.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/hash_id.h>
12#include <botan/internal/stl_util.h>
13
14namespace Botan {
15
16namespace {
17
18std::vector<uint8_t> emsa2_encoding(const std::vector<uint8_t>& msg,
19 size_t output_bits,
20 const std::vector<uint8_t>& empty_hash,
21 uint8_t hash_id) {
22 const size_t HASH_SIZE = empty_hash.size();
23
24 const size_t output_length = (output_bits + 1) / 8;
25
26 if(msg.size() != HASH_SIZE) {
27 throw Encoding_Error("EMSA_X931::encoding_of: Bad input length");
28 }
29 if(output_length < HASH_SIZE + 4) {
30 throw Encoding_Error("EMSA_X931::encoding_of: Output length is too small");
31 }
32
33 const bool empty_input = (msg == empty_hash);
34
35 std::vector<uint8_t> output(output_length);
36 BufferStuffer stuffer(output);
37
38 stuffer.append(empty_input ? 0x4B : 0x6B);
39 stuffer.append(0xBB, stuffer.remaining_capacity() - (1 + msg.size() + 2));
40 stuffer.append(0xBA);
41 stuffer.append(msg);
42 stuffer.append(hash_id);
43 stuffer.append(0xCC);
44 BOTAN_ASSERT_NOMSG(stuffer.full());
45
46 return output;
47}
48
49} // namespace
50
51std::string EMSA_X931::name() const {
52 return "EMSA2(" + m_hash->name() + ")";
53}
54
55void EMSA_X931::update(const uint8_t input[], size_t length) {
56 m_hash->update(input, length);
57}
58
59std::vector<uint8_t> EMSA_X931::raw_data() {
60 return m_hash->final_stdvec();
61}
62
63/*
64* EMSA_X931 Encode Operation
65*/
66std::vector<uint8_t> EMSA_X931::encoding_of(const std::vector<uint8_t>& msg,
67 size_t output_bits,
68 RandomNumberGenerator& /*rng*/) {
69 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
70}
71
72/*
73* EMSA_X931 Verify Operation
74*/
75bool EMSA_X931::verify(const std::vector<uint8_t>& coded, const std::vector<uint8_t>& raw, size_t key_bits) {
76 try {
77 return (coded == emsa2_encoding(raw, key_bits, m_empty_hash, m_hash_id));
78 } catch(...) {
79 return false;
80 }
81}
82
83/*
84* EMSA_X931 Constructor
85*/
86EMSA_X931::EMSA_X931(std::unique_ptr<HashFunction> hash) : m_hash(std::move(hash)) {
87 m_empty_hash = m_hash->final_stdvec();
88
89 m_hash_id = ieee1363_hash_id(m_hash->name());
90
91 if(!m_hash_id) {
92 throw Encoding_Error("EMSA_X931 no hash identifier for " + m_hash->name());
93 }
94}
95
96} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
EMSA_X931(std::unique_ptr< HashFunction > hash)
Definition emsa_x931.cpp:86
std::string name() const override
Definition emsa_x931.cpp:51
uint8_t ieee1363_hash_id(std::string_view name)
Definition hash_id.cpp:144