25 #ifndef CRYPTO3_EMSA_PKCS1_HPP
26 #define CRYPTO3_EMSA_PKCS1_HPP
35 secure_vector<uint8_t>
emsa3_encoding(
const secure_vector<uint8_t> &msg,
size_t output_bits,
36 const uint8_t hash_id[],
size_t hash_id_length) {
37 size_t output_length = output_bits / 8;
38 if (output_length < hash_id_length + msg.size() + 10) {
39 throw encoding_error(
"emsa3_encoding: Output length is too small");
42 secure_vector<uint8_t> T(output_length);
43 const size_t P_LENGTH = output_length - msg.size() - hash_id_length - 2;
47 T[P_LENGTH + 1] = 0x00;
49 if (hash_id_length > 0) {
50 BOOST_ASSERT(hash_id !=
nullptr)
51 buffer_insert(T, P_LENGTH + 2, hash_id, hash_id_length);
54 buffer_insert(T, output_length - msg.size(), msg.data(), msg.size());
59 template<
typename Scheme,
typename Hash>
61 template<
typename InputMessageIterator,
typename OutputIterator>
62 secure_vector<uint8_t>
emsa3_encoding(InputMessageIterator first1, InputMessageIterator last1,
64 size_t output_length = output_bits / 8;
65 std::ptrdiff_t message_length = std::distance(first1, last1);
67 if (output_length < Hash::policy_type::pkcs_id.size() + message_length + 10) {
68 throw encoding_error(
"emsa3_encoding: Output length is too small");
71 secure_vector<uint8_t> T(output_length);
72 const size_t P_LENGTH = output_length - message_length - Hash::policy_type::pkcs_id.size() - 2;
76 T[P_LENGTH + 1] = 0x00;
78 if (Hash::policy_type::pkcs_id.size() > 0) {
79 BOOST_ASSERT(Hash::policy_type::pkcs_id !=
nullptr);
80 buffer_insert(T, P_LENGTH + 2, Hash::policy_type::pkcs_id,
81 Hash::policy_type::pkcs_id.size());
84 buffer_insert(T, output_length - message_length, msg.data(), message_length);
93 template<
typename Scheme,
typename Hash>
95 template<
typename InputIterator1,
typename InputIterator2>
96 bool verify(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
97 InputIterator2 last2, std::size_t key_bits)
const {
98 std::ptrdiff_t raw_length = std::distance(first2, last2);
99 if (raw_length != Hash::policy_type::digest_bits) {
104 return std::equal(first1, last1,
emsa3_encoding(first2, last2, key_bits));
105 }
catch (
const std::exception &) {
110 template<
typename SinglePassRange1,
typename SinglePassRange2>
111 bool verify(
const SinglePassRange1 &range1,
const SinglePassRange2 &range2,
112 std::size_t key_bits)
const {
113 return verify(boost::begin(range1), boost::end(range1), boost::begin(range2),
114 boost::end(range2), key_bits);
127 template<
typename Scheme,
typename Hash>
129 template<
typename InputIterator1,
typename InputIterator2>
130 bool verify(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
131 InputIterator2 last2, std::size_t key_bits)
const {
132 if (Hash::policy_type::digest_bits > 0 &&
133 std::distance(first2, last2) != Hash::policy_type::digest_bits) {
138 return std::equal(first1, last1,
emsa3_encoding(first2, last2, key_bits));
139 }
catch (
const std::exception &) {
144 template<
typename SinglePassRange1,
typename SinglePassRange2>
145 bool verify(
const SinglePassRange1 &range1,
const SinglePassRange2 &range2,
146 std::size_t key_bits)
const {
147 return verify(boost::begin(range1), boost::end(range1), boost::begin(range2),
148 boost::end(range2), 0);
secure_vector< uint8_t > emsa3_encoding(const secure_vector< uint8_t > &msg, size_t output_bits, const uint8_t hash_id[], size_t hash_id_length)
Definition: emsa_pkcs1.hpp:35
void set_mem(T *ptr, size_t n, uint8_t val)
Definition: memory_operations.hpp:199
EMSA, from IEEE 1363s Encoding Method for Signatures, Appendix.
Definition: emsa.hpp:48
Definition: emsa_pkcs1.hpp:60
secure_vector< uint8_t > emsa3_encoding(InputMessageIterator first1, InputMessageIterator last1, size_t output_bits)
Definition: emsa_pkcs1.hpp:62
EMSA_PKCS1v15_Raw which is EMSA_PKCS1v15 without a hash or digest id (which according to QCA docs is ...
Definition: emsa_pkcs1.hpp:128
bool verify(const SinglePassRange1 &range1, const SinglePassRange2 &range2, std::size_t key_bits) const
Definition: emsa_pkcs1.hpp:145
bool verify(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, std::size_t key_bits) const
Definition: emsa_pkcs1.hpp:130
PKCS #1 v1.5 signature padding aka PKCS #1 block type 1 aka EMSA3 from IEEE 1363
Definition: emsa_pkcs1.hpp:94
bool verify(const SinglePassRange1 &range1, const SinglePassRange2 &range2, std::size_t key_bits) const
Definition: emsa_pkcs1.hpp:111
bool verify(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, std::size_t key_bits) const
Definition: emsa_pkcs1.hpp:96