Botan  2.1.0
Crypto and TLS for C++11
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 
49  {
50  return m_hash->final();
51  }
52 
55  size_t output_bits,
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 
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 
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 
94  {
96  std::swap(ret, m_message);
97  return ret;
98  }
99 
102  size_t output_bits,
104  {
105  return emsa3_encoding(msg, output_bits, nullptr, 0);
106  }
107 
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 }
bool verify(const secure_vector< uint8_t > &, const secure_vector< uint8_t > &, size_t) override
Definition: emsa_pkcs1.cpp:65
void update(const uint8_t[], size_t) override
Definition: emsa_pkcs1.cpp:43
bool verify(const secure_vector< uint8_t > &, const secure_vector< uint8_t > &, size_t) override
Definition: emsa_pkcs1.cpp:108
EMSA_PKCS1v15(HashFunction *hash)
Definition: emsa_pkcs1.cpp:83
void set_mem(T *ptr, size_t n, uint8_t val)
Definition: mem_ops.h:83
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
void update(const uint8_t[], size_t) override
Definition: emsa_pkcs1.cpp:88
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:79
secure_vector< uint8_t > raw_data() override
Definition: emsa_pkcs1.cpp:48
secure_vector< uint8_t > raw_data() override
Definition: emsa_pkcs1.cpp:93
Definition: alg_id.cpp:13
std::vector< uint8_t > pkcs_hash_id(const std::string &name)
Definition: hash_id.cpp:56
secure_vector< uint8_t > encoding_of(const secure_vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
Definition: emsa_pkcs1.cpp:101
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:133
std::unique_ptr< HashFunction > m_hash
Definition: tpm.cpp:439
MechanismType hash
secure_vector< uint8_t > encoding_of(const secure_vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
Definition: emsa_pkcs1.cpp:54