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