Botan 3.14.1
Crypto and TLS for C&
raw_sig_padding.cpp
Go to the documentation of this file.
1/*
2* (C) 1999-2007,2025 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/raw_sig_padding.h>
8
9#include <botan/exceptn.h>
10#include <botan/hash.h>
11#include <botan/mem_ops.h>
12#include <botan/pk_options_readers.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/pk_options_impl.h>
16
17namespace Botan {
18
20 m_expected_size([&]() -> size_t {
22
23 // If the caller named the hash they used, the input length is checked against it
25 if(auto prehash = externally_computed_prehash_name(options)) {
26 return HashFunction::create_or_throw(*prehash)->output_length();
27 }
28 }
29 return 0;
30 }()) {}
31
32std::string SignRawBytes::name() const {
33 if(m_expected_size > 0) {
34 return fmt("Raw({})", m_expected_size);
35 }
36 return "Raw";
37}
38
39void SignRawBytes::update(const uint8_t input[], size_t length) {
40 // The input is just accumulated into the buffer
41 m_message += std::make_pair(input, length);
42}
43
44std::vector<uint8_t> SignRawBytes::raw_data() {
45 /*
46 * Return the provided data. If a specific length was indicated (eg for a prehash),
47 * check that.
48 */
49
50 if(m_expected_size > 0 && m_message.size() != m_expected_size) {
51 throw Invalid_Argument(
52 fmt("SignRawBytes was configured to use a {} byte hash but instead was used for a {} byte hash",
53 m_expected_size,
54 m_message.size()));
55 }
56
57 std::vector<uint8_t> output;
58 std::swap(m_message, output);
59 return output;
60}
61
62std::vector<uint8_t> SignRawBytes::encoding_of(std::span<const uint8_t> msg,
63 size_t /*output_bits*/,
64 RandomNumberGenerator& /*rng*/) {
65 if(m_expected_size > 0 && msg.size() != m_expected_size) {
66 throw Invalid_Argument(
67 fmt("SignRawBytes was configured to use a {} byte hash but instead was used for a {} byte hash",
68 m_expected_size,
69 msg.size()));
70 }
71
72 return std::vector<uint8_t>(msg.begin(), msg.end());
73}
74
75bool SignRawBytes::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t /*key_bits*/) {
76 if(m_expected_size > 0 && raw.size() != m_expected_size) {
77 return false;
78 }
79
80 if(raw.size() > coded.size()) {
81 // handle zero padding differences
82 const size_t expected_lz = raw.size() - coded.size();
83 const auto zeros_ok = CT::all_zeros(raw.data(), expected_lz);
84 const auto contents_ok = CT::is_equal(coded.data(), raw.data() + expected_lz, coded.size());
85 return (zeros_ok & contents_ok).as_bool();
86 }
87
88 return constant_time_compare(coded, raw);
89}
90
91} // namespace Botan
SignRawBytes(const PK_Signature_Options_Reader &options)
std::string name() const override
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17