Botan 2.19.1
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/emsa_x931.h>
9#include <botan/exceptn.h>
10#include <botan/hash_id.h>
11
12namespace Botan {
13
14namespace {
15
16secure_vector<uint8_t> emsa2_encoding(const secure_vector<uint8_t>& msg,
17 size_t output_bits,
18 const secure_vector<uint8_t>& empty_hash,
19 uint8_t hash_id)
20 {
21 const size_t HASH_SIZE = empty_hash.size();
22
23 size_t output_length = (output_bits + 1) / 8;
24
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");
29
30 const bool empty_input = (msg == empty_hash);
31
32 secure_vector<uint8_t> output(output_length);
33
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;
40
41 return output;
42 }
43
44}
45
46std::string EMSA_X931::name() const
47 {
48 return "EMSA2(" + m_hash->name() + ")";
49 }
50
51void EMSA_X931::update(const uint8_t input[], size_t length)
52 {
53 m_hash->update(input, length);
54 }
55
56secure_vector<uint8_t> EMSA_X931::raw_data()
57 {
58 return m_hash->final();
59 }
60
61/*
62* EMSA_X931 Encode Operation
63*/
64secure_vector<uint8_t> EMSA_X931::encoding_of(const secure_vector<uint8_t>& msg,
65 size_t output_bits,
66 RandomNumberGenerator&)
67 {
68 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
69 }
70
71/*
72* EMSA_X931 Verify Operation
73*/
74bool EMSA_X931::verify(const secure_vector<uint8_t>& coded,
75 const secure_vector<uint8_t>& raw,
76 size_t key_bits)
77 {
78 try
79 {
80 return (coded == emsa2_encoding(raw, key_bits,
81 m_empty_hash, m_hash_id));
82 }
83 catch(...)
84 {
85 return false;
86 }
87 }
88
89/*
90* EMSA_X931 Constructor
91*/
93 {
94 m_empty_hash = m_hash->final();
95
96 m_hash_id = ieee1363_hash_id(hash->name());
97
98 if(!m_hash_id)
99 throw Encoding_Error("EMSA_X931 no hash identifier for " + hash->name());
100 }
101
102}
std::string name() const override
Definition: emsa_x931.cpp:46
EMSA_X931(HashFunction *hash)
Definition: emsa_x931.cpp:92
Definition: alg_id.cpp:13
void set_mem(uint8_t *ptr, size_t n, uint8_t val)
Definition: mem_ops.h:182
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: mem_ops.h:228
uint8_t ieee1363_hash_id(const std::string &name)
Definition: hash_id.cpp:146
MechanismType hash