Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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/hash_id.h>
10
11 namespace Botan {
12
13 namespace {
14
15 secure_vector<uint8_t> emsa2_encoding(const secure_vector<uint8_t>& msg,
16 size_t output_bits,
17 const secure_vector<uint8_t>& empty_hash,
18 uint8_t hash_id)
19 {
20 const size_t HASH_SIZE = empty_hash.size();
21
22 size_t output_length = (output_bits + 1) / 8;
23
24 if(msg.size() != HASH_SIZE)
25 throw Encoding_Error("EMSA_X931::encoding_of: Bad input length");
26 if(output_length < HASH_SIZE + 4)
27 throw Encoding_Error("EMSA_X931::encoding_of: Output length is too small");
28
29 const bool empty_input = (msg == empty_hash);
30
31 secure_vector<uint8_t> output(output_length);
32
33 output[0] = (empty_input ? 0x4B : 0x6B);
34 output[output_length - 3 - HASH_SIZE] = 0xBA;
35 set_mem(&output[1], output_length - 4 - HASH_SIZE, 0xBB);
36 buffer_insert(output, output_length - (HASH_SIZE + 2), msg.data(), msg.size());
37 output[output_length-2] = hash_id;
38 output[output_length-1] = 0xCC;
39
40 return output;
41 }
42
43 }
44
45 void EMSA_X931::update(const uint8_t input[], size_t length)
46 {
47 m_hash->update(input, length);
48 }
49
50 secure_vector<uint8_t> EMSA_X931::raw_data()
51 {
52 return m_hash->final();
53 }
54
55 /*
56 * EMSA_X931 Encode Operation
57 */
58 secure_vector<uint8_t> EMSA_X931::encoding_of(const secure_vector<uint8_t>& msg,
59 size_t output_bits,
60 RandomNumberGenerator&)
61 {
62 return emsa2_encoding(msg, output_bits, m_empty_hash, m_hash_id);
63 }
64
65 /*
66 * EMSA_X931 Verify Operation
67 */
68 bool EMSA_X931::verify(const secure_vector<uint8_t>& coded,
69 const secure_vector<uint8_t>& raw,
70 size_t key_bits)
71 {
72 try
73 {
74 return (coded == emsa2_encoding(raw, key_bits,
75 m_empty_hash, m_hash_id));
76 }
77 catch(...)
78 {
79 return false;
80 }
81 }
82
83 /*
84 * EMSA_X931 Constructor
85 */
86 EMSA_X931::EMSA_X931(HashFunction* hash) : m_hash(hash)
87 {
88 m_empty_hash = m_hash->final();
89
90 m_hash_id = ieee1363_hash_id(hash->name());
91
92 if(!m_hash_id)
93 throw Encoding_Error("EMSA_X931 no hash identifier for " + hash->name());
94 }
95
96 }
Botan::HashFunction::name
virtual std::string name() const =0
Botan::ieee1363_hash_id
uint8_t ieee1363_hash_id(const std::string &name)
Definition: hash_id.cpp:104
Botan::set_mem
void set_mem(T *ptr, size_t n, uint8_t val)
Definition: mem_ops.h:83
Botan
Definition: alg_id.cpp:13
Botan::HashFunction
Definition: hash.h:19
Botan::EMSA_X931::EMSA_X931
EMSA_X931(HashFunction *hash)
Definition: emsa_x931.cpp:86
Botan::buffer_insert
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:133
m_hash
std::unique_ptr< HashFunction > m_hash
Definition: tpm.cpp:439
Botan::Encoding_Error
Definition: exceptn.h:180
hash
MechanismType hash
Definition: p11_mechanism.cpp:63