Botan 3.6.1
Crypto and TLS for C&
emsa_pkcs1.cpp
Go to the documentation of this file.
1/*
2* PKCS #1 v1.5 signature padding
3* (C) 1999-2008 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/emsa_pkcs1.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/hash_id.h>
12#include <botan/internal/stl_util.h>
13
14namespace Botan {
15
16namespace {
17
18std::vector<uint8_t> pkcs1v15_sig_encoding(const std::vector<uint8_t>& msg,
19 size_t output_bits,
20 std::span<const uint8_t> hash_id) {
21 const size_t output_length = output_bits / 8;
22
23 if(output_length < hash_id.size() + msg.size() + 2 + 8) {
24 throw Encoding_Error("pkcs1v15_sig_encoding: Output length is too small");
25 }
26
27 std::vector<uint8_t> padded(output_length);
28 BufferStuffer stuffer(padded);
29
30 stuffer.append(0x01);
31 stuffer.append(0xFF, stuffer.remaining_capacity() - (1 + hash_id.size() + msg.size()));
32 stuffer.append(0x00);
33 stuffer.append(hash_id);
34 stuffer.append(msg);
35 BOTAN_ASSERT_NOMSG(stuffer.full());
36
37 return padded;
38}
39
40} // namespace
41
42void EMSA_PKCS1v15::update(const uint8_t input[], size_t length) {
43 m_hash->update(input, length);
44}
45
46std::vector<uint8_t> EMSA_PKCS1v15::raw_data() {
47 return m_hash->final_stdvec();
48}
49
50std::vector<uint8_t> EMSA_PKCS1v15::encoding_of(const std::vector<uint8_t>& msg,
51 size_t output_bits,
52 RandomNumberGenerator& /*rng*/) {
53 if(msg.size() != m_hash->output_length()) {
54 throw Encoding_Error("EMSA_PKCS1v15::encoding_of: Bad input length");
55 }
56
57 return pkcs1v15_sig_encoding(msg, output_bits, m_hash_id);
58}
59
60bool EMSA_PKCS1v15::verify(const std::vector<uint8_t>& coded, const std::vector<uint8_t>& raw, size_t key_bits) {
61 if(raw.size() != m_hash->output_length()) {
62 return false;
63 }
64
65 try {
66 return coded == pkcs1v15_sig_encoding(raw, key_bits, m_hash_id);
67 } catch(...) {
68 return false;
69 }
70}
71
72EMSA_PKCS1v15::EMSA_PKCS1v15(std::unique_ptr<HashFunction> hash) : m_hash(std::move(hash)) {
73 m_hash_id = pkcs_hash_id(m_hash->name());
74}
75
77 m_hash_output_len = 0;
78 // m_hash_id, m_hash_name left empty
79}
80
81EMSA_PKCS1v15_Raw::EMSA_PKCS1v15_Raw(std::string_view hash_algo) {
82 std::unique_ptr<HashFunction> hash(HashFunction::create_or_throw(hash_algo));
83 m_hash_id = pkcs_hash_id(hash_algo);
84 m_hash_name = hash->name();
85 m_hash_output_len = hash->output_length();
86}
87
88void EMSA_PKCS1v15_Raw::update(const uint8_t input[], size_t length) {
89 m_message += std::make_pair(input, length);
90}
91
92std::vector<uint8_t> EMSA_PKCS1v15_Raw::raw_data() {
93 std::vector<uint8_t> ret;
94 std::swap(ret, m_message);
95
96 if(m_hash_output_len > 0 && ret.size() != m_hash_output_len) {
97 throw Encoding_Error("EMSA_PKCS1v15_Raw::encoding_of: Bad input length");
98 }
99
100 return ret;
101}
102
103std::vector<uint8_t> EMSA_PKCS1v15_Raw::encoding_of(const std::vector<uint8_t>& msg,
104 size_t output_bits,
105 RandomNumberGenerator& /*rng*/) {
106 return pkcs1v15_sig_encoding(msg, output_bits, m_hash_id);
107}
108
109bool EMSA_PKCS1v15_Raw::verify(const std::vector<uint8_t>& coded, const std::vector<uint8_t>& raw, size_t key_bits) {
110 if(m_hash_output_len > 0 && raw.size() != m_hash_output_len) {
111 return false;
112 }
113
114 try {
115 return coded == pkcs1v15_sig_encoding(raw, key_bits, m_hash_id);
116 } catch(...) {
117 return false;
118 }
119}
120
121} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
std::vector< uint8_t > encoding_of(const std::vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
void update(const uint8_t[], size_t) override
std::vector< uint8_t > raw_data() override
bool verify(const std::vector< uint8_t > &, const std::vector< uint8_t > &, size_t) override
std::vector< uint8_t > raw_data() override
EMSA_PKCS1v15(std::unique_ptr< HashFunction > hash)
bool verify(const std::vector< uint8_t > &, const std::vector< uint8_t > &, size_t) override
void update(const uint8_t[], size_t) override
std::vector< uint8_t > encoding_of(const std::vector< uint8_t > &, size_t, RandomNumberGenerator &rng) override
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
Definition hash_id.cpp:78