Botan 3.9.0
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/mem_ops.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/fmt.h>
13
14namespace Botan {
15
16std::string SignRawBytes::name() const {
17 if(m_expected_size > 0) {
18 return fmt("Raw({})", m_expected_size);
19 }
20 return "Raw";
21}
22
23void SignRawBytes::update(const uint8_t input[], size_t length) {
24 // The input is just accumulated into the buffer
25 m_message += std::make_pair(input, length);
26}
27
28std::vector<uint8_t> SignRawBytes::raw_data() {
29 /*
30 * Return the provided data. If a specific length was indicated (eg for a prehash),
31 * check that.
32 */
33
34 if(m_expected_size > 0 && m_message.size() != m_expected_size) {
35 throw Invalid_Argument(
36 fmt("SignRawBytes was configured to use a {} byte hash but instead was used for a {} byte hash",
37 m_expected_size,
38 m_message.size()));
39 }
40
41 std::vector<uint8_t> output;
42 std::swap(m_message, output);
43 return output;
44}
45
46std::vector<uint8_t> SignRawBytes::encoding_of(std::span<const uint8_t> msg,
47 size_t /*output_bits*/,
48 RandomNumberGenerator& /*rng*/) {
49 if(m_expected_size > 0 && msg.size() != m_expected_size) {
50 throw Invalid_Argument(
51 fmt("SignRawBytes was configured to use a {} byte hash but instead was used for a {} byte hash",
52 m_expected_size,
53 msg.size()));
54 }
55
56 return std::vector<uint8_t>(msg.begin(), msg.end());
57}
58
59bool SignRawBytes::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t /*key_bits*/) {
60 if(m_expected_size > 0 && raw.size() != m_expected_size) {
61 return false;
62 }
63
64 if(raw.size() > coded.size()) {
65 // handle zero padding differences
66 const size_t expected_lz = raw.size() - coded.size();
67 auto zeros_ok = CT::all_zeros(raw.data(), expected_lz);
68 auto contents_ok = CT::is_equal(coded.data(), raw.data() + expected_lz, coded.size());
69 return (zeros_ok & contents_ok).as_bool();
70 }
71
72 return constant_time_compare(coded, raw);
73}
74
75} // namespace Botan
std::string name() const override
virtual std::vector< uint8_t > raw_data()=0
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:826
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:813
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17