Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

emsa_pkcs1.cpp
Go to the documentation of this file.
1 /*
2 * PKCS #1 v1.5 signature padding
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/emsa_pkcs1.h>
9 #include <botan/hash_id.h>
10
11 namespace Botan {
12
13 namespace {
14
15 secure_vector<uint8_t> emsa3_encoding(const secure_vector<uint8_t>& msg,
16 size_t output_bits,
17 const uint8_t hash_id[],
18 size_t hash_id_length)
19 {
20 size_t output_length = output_bits / 8;
21 if(output_length < hash_id_length + msg.size() + 10)
22 throw Encoding_Error("emsa3_encoding: Output length is too small");
23
24 secure_vector<uint8_t> T(output_length);
25 const size_t P_LENGTH = output_length - msg.size() - hash_id_length - 2;
26
27 T[0] = 0x01;
28 set_mem(&T[1], P_LENGTH, 0xFF);
29 T[P_LENGTH+1] = 0x00;
30
31 if(hash_id_length > 0)
32 {
33 BOTAN_ASSERT_NONNULL(hash_id);
34 buffer_insert(T, P_LENGTH+2, hash_id, hash_id_length);
35 }
36
37 buffer_insert(T, output_length-msg.size(), msg.data(), msg.size());
38 return T;
39 }
40
41 }
42
43 void EMSA_PKCS1v15::update(const uint8_t input[], size_t length)
44 {
45 m_hash->update(input, length);
46 }
47
48 secure_vector<uint8_t> EMSA_PKCS1v15::raw_data()
49 {
50 return m_hash->final();
51 }
52
53 secure_vector<uint8_t>
54 EMSA_PKCS1v15::encoding_of(const secure_vector<uint8_t>& msg,
55 size_t output_bits,
56 RandomNumberGenerator&)
57 {
58 if(msg.size() != m_hash->output_length())
59 throw Encoding_Error("EMSA_PKCS1v15::encoding_of: Bad input length");
60
61 return emsa3_encoding(msg, output_bits,
62 m_hash_id.data(), m_hash_id.size());
63 }
64
65 bool EMSA_PKCS1v15::verify(const secure_vector<uint8_t>& coded,
66 const secure_vector<uint8_t>& raw,
67 size_t key_bits)
68 {
69 if(raw.size() != m_hash->output_length())
70 return false;
71
72 try
73 {
74 return (coded == emsa3_encoding(raw, key_bits,
75 m_hash_id.data(), m_hash_id.size()));
76 }
77 catch(...)
78 {
79 return false;
80 }
81 }
82
83 EMSA_PKCS1v15::EMSA_PKCS1v15(HashFunction* hash) : m_hash(hash)
84 {
85 m_hash_id = pkcs_hash_id(m_hash->name());
86 }
87
88 void EMSA_PKCS1v15_Raw::update(const uint8_t input[], size_t length)
89 {
90 m_message += std::make_pair(input, length);
91 }
92
93 secure_vector<uint8_t> EMSA_PKCS1v15_Raw::raw_data()
94 {
95 secure_vector<uint8_t> ret;
96 std::swap(ret, m_message);
97 return ret;
98 }
99
100 secure_vector<uint8_t>
101 EMSA_PKCS1v15_Raw::encoding_of(const secure_vector<uint8_t>& msg,
102 size_t output_bits,
103 RandomNumberGenerator&)
104 {
105 return emsa3_encoding(msg, output_bits, nullptr, 0);
106 }
107
108 bool EMSA_PKCS1v15_Raw::verify(const secure_vector<uint8_t>& coded,
109 const secure_vector<uint8_t>& raw,
110 size_t key_bits)
111 {
112 try
113 {
114 return (coded == emsa3_encoding(raw, key_bits, nullptr, 0));
115 }
116 catch(...)
117 {
118 return false;
119 }
120 }
121
122 }
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::EMSA_PKCS1v15::verify
bool verify(const secure_vector< uint8_t > &, const secure_vector< uint8_t > &, size_t) override
Definition: emsa_pkcs1.cpp:65
Botan::EMSA_PKCS1v15::update
void update(const uint8_t[], size_t) override
Definition: emsa_pkcs1.cpp:43
Botan::EMSA_PKCS1v15_Raw::verify
bool verify(const secure_vector< uint8_t > &, const secure_vector< uint8_t > &, size_t) override
Definition: emsa_pkcs1.cpp:108
Botan::EMSA_PKCS1v15::EMSA_PKCS1v15
EMSA_PKCS1v15(HashFunction *hash)
Definition: emsa_pkcs1.cpp:83
Botan::set_mem
void set_mem(T *ptr, size_t n, uint8_t val)
Definition: mem_ops.h:83
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::EMSA_PKCS1v15_Raw::update
void update(const uint8_t[], size_t) override
Definition: emsa_pkcs1.cpp:88
BOTAN_ASSERT_NONNULL
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:79
Botan::EMSA_PKCS1v15::raw_data
secure_vector< uint8_t > raw_data() override
Definition: emsa_pkcs1.cpp:48
Botan::EMSA_PKCS1v15_Raw::raw_data
secure_vector< uint8_t > raw_data() override
Definition: emsa_pkcs1.cpp:93
Botan
Definition: alg_id.cpp:13
Botan::HashFunction
Definition: hash.h:19
Botan::pkcs_hash_id
std::vector< uint8_t > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:56
Botan::EMSA_PKCS1v15_Raw::encoding_of
secure_vector< uint8_t > encoding_of(const secure_vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
Definition: emsa_pkcs1.cpp:101
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
Botan::EMSA_PKCS1v15::encoding_of
secure_vector< uint8_t > encoding_of(const secure_vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
Definition: emsa_pkcs1.cpp:54