1 /*
2 * EMSA-Raw
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_raw.h>
9 #include <botan/exceptn.h>
10
11 namespace Botan {
12
name() const13 std::string EMSA_Raw::name() const
14 {
15 if(m_expected_size > 0)
16 return "Raw(" + std::to_string(m_expected_size) + ")";
17 return "Raw";
18 }
19
20 /*
21 * EMSA-Raw Encode Operation
22 */
update(const uint8_t input[],size_t length)23 void EMSA_Raw::update(const uint8_t input[], size_t length)
24 {
25 m_message += std::make_pair(input, length);
26 }
27
28 /*
29 * Return the raw (unencoded) data
30 */
raw_data()31 secure_vector<uint8_t> EMSA_Raw::raw_data()
32 {
33 if(m_expected_size && m_message.size() != m_expected_size)
34 throw Invalid_Argument("EMSA_Raw was configured to use a " +
35 std::to_string(m_expected_size) +
36 " byte hash but instead was used for a " +
37 std::to_string(m_message.size()) + " hash");
38
39 secure_vector<uint8_t> output;
40 std::swap(m_message, output);
41 return output;
42 }
43
44 /*
45 * EMSA-Raw Encode Operation
46 */
47 secure_vector<uint8_t>
encoding_of(const secure_vector<uint8_t> & msg,size_t,RandomNumberGenerator &)48 EMSA_Raw::encoding_of(const secure_vector<uint8_t>& msg,
49 size_t,
50 RandomNumberGenerator&)
51 {
52 if(m_expected_size && msg.size() != m_expected_size)
53 throw Invalid_Argument("EMSA_Raw was configured to use a " +
54 std::to_string(m_expected_size) +
55 " byte hash but instead was used for a " +
56 std::to_string(msg.size()) + " hash");
57
58 return msg;
59 }
60
61 /*
62 * EMSA-Raw Verify Operation
63 */
verify(const secure_vector<uint8_t> & coded,const secure_vector<uint8_t> & raw,size_t)64 bool EMSA_Raw::verify(const secure_vector<uint8_t>& coded,
65 const secure_vector<uint8_t>& raw,
66 size_t)
67 {
68 if(m_expected_size && raw.size() != m_expected_size)
69 return false;
70
71 if(coded.size() == raw.size())
72 return (coded == raw);
73
74 if(coded.size() > raw.size())
75 return false;
76
77 // handle zero padding differences
78 const size_t leading_zeros_expected = raw.size() - coded.size();
79
80 bool same_modulo_leading_zeros = true;
81
82 for(size_t i = 0; i != leading_zeros_expected; ++i)
83 if(raw[i])
84 same_modulo_leading_zeros = false;
85
86 if(!constant_time_compare(coded.data(), raw.data() + leading_zeros_expected, coded.size()))
87 same_modulo_leading_zeros = false;
88
89 return same_modulo_leading_zeros;
90 }
91
92 }
93