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