Botan 3.8.1
Crypto and TLS for C&
emsa_raw.cpp
Go to the documentation of this file.
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
15namespace Botan {
16
17std::string EMSA_Raw::name() const {
18 if(m_expected_size > 0) {
19 return fmt("Raw({})", m_expected_size);
20 }
21 return "Raw";
22}
23
24/*
25* EMSA-Raw Encode Operation
26*/
27void EMSA_Raw::update(const uint8_t input[], size_t length) {
28 m_message += std::make_pair(input, length);
29}
30
31/*
32* Return the raw (unencoded) data
33*/
34std::vector<uint8_t> EMSA_Raw::raw_data() {
35 if(m_expected_size && m_message.size() != m_expected_size) {
36 throw Invalid_Argument(
37 fmt("EMSA_Raw was configured to use a {} byte hash but instead was used for a {} byte hash",
38 m_expected_size,
39 m_message.size()));
40 }
41
42 std::vector<uint8_t> output;
43 std::swap(m_message, output);
44 return output;
45}
46
47/*
48* EMSA-Raw Encode Operation
49*/
50std::vector<uint8_t> EMSA_Raw::encoding_of(std::span<const uint8_t> msg,
51 size_t /*output_bits*/,
52 RandomNumberGenerator& /*rng*/) {
53 if(m_expected_size && msg.size() != m_expected_size) {
54 throw Invalid_Argument(
55 fmt("EMSA_Raw was configured to use a {} byte hash but instead was used for a {} byte hash",
56 m_expected_size,
57 msg.size()));
58 }
59
60 return std::vector<uint8_t>(msg.begin(), msg.end());
61}
62
63/*
64* EMSA-Raw Verify Operation
65*/
66bool EMSA_Raw::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t /*key_bits*/) {
67 if(m_expected_size && raw.size() != m_expected_size) {
68 return false;
69 }
70
71 if(raw.size() > coded.size()) {
72 // handle zero padding differences
73 const size_t expected_lz = raw.size() - coded.size();
74 auto zeros_ok = CT::all_zeros(raw.data(), expected_lz);
75 auto contents_ok = CT::is_equal(coded.data(), raw.data() + expected_lz, coded.size());
76 return (zeros_ok & contents_ok).as_bool();
77 }
78
79 return constant_time_compare(coded, raw);
80}
81
82} // namespace Botan
std::string name() const override
Definition emsa_raw.cpp:17
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:789
constexpr CT::Mask< T > all_zeros(const T elem[], size_t len)
Definition ct_utils.h:776
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