/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,2025 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/emsa_raw.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/mem_ops.h> |
12 | | #include <botan/internal/ct_utils.h> |
13 | | #include <botan/internal/fmt.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | 0 | std::string EMSA_Raw::name() const { |
18 | 0 | if(m_expected_size > 0) { |
19 | 0 | return fmt("Raw({})", m_expected_size); |
20 | 0 | } |
21 | 0 | return "Raw"; |
22 | 0 | } |
23 | | |
24 | | /* |
25 | | * EMSA-Raw Encode Operation |
26 | | */ |
27 | 0 | void EMSA_Raw::update(const uint8_t input[], size_t length) { |
28 | 0 | m_message += std::make_pair(input, length); |
29 | 0 | } |
30 | | |
31 | | /* |
32 | | * Return the raw (unencoded) data |
33 | | */ |
34 | 0 | std::vector<uint8_t> EMSA_Raw::raw_data() { |
35 | 0 | if(m_expected_size && m_message.size() != m_expected_size) { |
36 | 0 | throw Invalid_Argument( |
37 | 0 | fmt("EMSA_Raw was configured to use a {} byte hash but instead was used for a {} byte hash", |
38 | 0 | m_expected_size, |
39 | 0 | m_message.size())); |
40 | 0 | } |
41 | | |
42 | 0 | std::vector<uint8_t> output; |
43 | 0 | std::swap(m_message, output); |
44 | 0 | return output; |
45 | 0 | } |
46 | | |
47 | | /* |
48 | | * EMSA-Raw Encode Operation |
49 | | */ |
50 | | std::vector<uint8_t> EMSA_Raw::encoding_of(std::span<const uint8_t> msg, |
51 | | size_t /*output_bits*/, |
52 | 0 | RandomNumberGenerator& /*rng*/) { |
53 | 0 | if(m_expected_size && msg.size() != m_expected_size) { |
54 | 0 | throw Invalid_Argument( |
55 | 0 | fmt("EMSA_Raw was configured to use a {} byte hash but instead was used for a {} byte hash", |
56 | 0 | m_expected_size, |
57 | 0 | msg.size())); |
58 | 0 | } |
59 | | |
60 | 0 | return std::vector<uint8_t>(msg.begin(), msg.end()); |
61 | 0 | } |
62 | | |
63 | | /* |
64 | | * EMSA-Raw Verify Operation |
65 | | */ |
66 | 0 | bool EMSA_Raw::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t /*key_bits*/) { |
67 | 0 | if(m_expected_size && raw.size() != m_expected_size) { |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | 0 | if(raw.size() > coded.size()) { |
72 | | // handle zero padding differences |
73 | 0 | const size_t expected_lz = raw.size() - coded.size(); |
74 | 0 | auto zeros_ok = CT::all_zeros(raw.data(), expected_lz); |
75 | 0 | auto contents_ok = CT::is_equal(coded.data(), raw.data() + expected_lz, coded.size()); |
76 | 0 | return (zeros_ok & contents_ok).as_bool(); |
77 | 0 | } |
78 | | |
79 | 0 | return constant_time_compare(coded, raw); |
80 | 0 | } |
81 | | |
82 | | } // namespace Botan |