Botan 3.14.1
Crypto and TLS for C&
pssr.cpp
Go to the documentation of this file.
1/*
2* PSSR
3* (C) 1999-2007,2017,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/pssr.h>
9
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/hash.h>
13#include <botan/pk_options_readers.h>
14#include <botan/rng.h>
15#include <botan/internal/buffer_stuffer.h>
16#include <botan/internal/ct_utils.h>
17#include <botan/internal/fmt.h>
18#include <botan/internal/mgf1.h>
19#include <array>
20
21namespace Botan {
22
23namespace {
24
25/*
26* PSSR Encode Operation
27*/
28std::vector<uint8_t> pss_encode(HashFunction& hash,
29 std::span<const uint8_t> msg,
30 std::span<const uint8_t> salt,
31 size_t output_bits) {
32 const size_t HASH_SIZE = hash.output_length();
33
34 if(msg.size() != HASH_SIZE) {
35 throw Encoding_Error("Cannot encode PSS string, input length invalid for hash");
36 }
37 if(output_bits < 8 * HASH_SIZE + 8 * salt.size() + 9) {
38 throw Encoding_Error("Cannot encode PSS string, output length too small");
39 }
40
41 const size_t output_length = ceil_tobytes(output_bits);
42 const uint8_t db0_mask = 0xFF >> (8 * output_length - output_bits);
43
44 std::array<uint8_t, 8> padding = {0};
45 hash.update(padding);
46 hash.update(msg);
47 hash.update(salt);
48 std::vector<uint8_t> H = hash.final_stdvec();
49
50 const size_t db_len = output_length - HASH_SIZE - 1;
51 std::vector<uint8_t> EM(output_length);
52
53 BufferStuffer stuffer(EM);
54 stuffer.append(0x00, stuffer.remaining_capacity() - (1 + salt.size() + H.size() + 1));
55 stuffer.append(0x01);
56 stuffer.append(salt);
57
58 mgf1_mask(hash, H, std::span{EM}.first(db_len));
59 EM[0] &= db0_mask;
60
61 stuffer.append(H);
62 stuffer.append(0xBC);
63 BOTAN_ASSERT_NOMSG(stuffer.full());
64
65 return EM;
66}
67
68bool pss_verify(HashFunction& hash,
69 std::span<const uint8_t> pss_repr,
70 std::span<const uint8_t> message_hash,
71 size_t key_bits,
72 size_t* out_salt_size) {
73 const size_t HASH_SIZE = hash.output_length();
74 const size_t key_bytes = ceil_tobytes(key_bits);
75
76 if(key_bits < 8 * HASH_SIZE + 9) {
77 return false;
78 }
79
80 if(message_hash.size() != HASH_SIZE) {
81 return false;
82 }
83
84 if(pss_repr.size() > key_bytes || pss_repr.size() <= 1) {
85 return false;
86 }
87
88 if(pss_repr[pss_repr.size() - 1] != 0xBC) {
89 return false;
90 }
91
92 std::vector<uint8_t> coded;
93 if(pss_repr.size() < key_bytes) {
94 coded.resize(key_bytes);
95 BufferStuffer stuffer(coded);
96 stuffer.append(0x00, key_bytes - pss_repr.size());
97 stuffer.append(pss_repr);
98 } else {
99 coded.assign(pss_repr.begin(), pss_repr.end());
100 }
101
102 // We have to check this after potential zero padding above
103 const size_t top_bits = 8 * ((key_bits + 7) / 8) - key_bits;
104 if(top_bits > 8 - high_bit(coded[0])) {
105 return false;
106 }
107
108 uint8_t* DB = coded.data();
109 const size_t DB_size = coded.size() - HASH_SIZE - 1;
110
111 const uint8_t* H = &coded[DB_size];
112 const size_t H_size = HASH_SIZE;
113
114 mgf1_mask(hash, {H, H_size}, {DB, DB_size});
115 DB[0] &= 0xFF >> top_bits;
116
117 size_t salt_offset = 0;
118 for(size_t j = 0; j != DB_size; ++j) {
119 if(DB[j] == 0x01) {
120 salt_offset = j + 1;
121 break;
122 }
123 if(DB[j] != 0x00) {
124 return false;
125 }
126 }
127 if(salt_offset == 0) {
128 return false;
129 }
130
131 const size_t salt_size = DB_size - salt_offset;
132
133 std::array<uint8_t, 8> padding = {0};
134 hash.update(padding);
135 hash.update(message_hash);
136 hash.update(&DB[salt_offset], salt_size);
137
138 const std::vector<uint8_t> H2 = hash.final_stdvec();
139
140 const bool ok = CT::is_equal(H, H2.data(), HASH_SIZE).as_bool();
141
142 if(ok && out_salt_size != nullptr) {
143 *out_salt_size = salt_size;
144 }
145
146 return ok;
147}
148
149} // namespace
150
152 m_hash(HashFunction::create_or_throw(options.hash_function_name())),
153 m_salt_size(options.salt_size().value_or(m_hash->output_length())),
154 m_required_salt_len(options.using_salt_size()) {
155 if(options.using_deterministic_signature() && m_salt_size > 0) {
156 throw Invalid_Argument("PSS with a non-zero salt cannot produce deterministic signatures");
157 }
158}
159
160/*
161* PSSR Update Operation
162*/
163void PSSR::update(const uint8_t input[], size_t length) {
164 m_hash->update(input, length);
165}
166
167/*
168* Return the raw (unencoded) data
169*/
170std::vector<uint8_t> PSSR::raw_data() {
171 return m_hash->final_stdvec();
172}
173
174std::vector<uint8_t> PSSR::encoding_of(std::span<const uint8_t> msg, size_t output_bits, RandomNumberGenerator& rng) {
175 const auto salt = rng.random_vec<std::vector<uint8_t>>(m_salt_size);
176 return pss_encode(*m_hash, msg, salt, output_bits);
177}
178
179/*
180* PSSR Decode/Verify Operation
181*/
182bool PSSR::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t key_bits) {
183 size_t salt_size = 0;
184 const bool ok = pss_verify(*m_hash, coded, raw, key_bits, &salt_size);
185
186 if(m_required_salt_len && salt_size != m_salt_size) {
187 return false;
188 }
189
190 return ok;
191}
192
193std::string PSSR::hash_function() const {
194 return m_hash->name();
195}
196
197std::string PSSR::name() const {
198 return fmt("PSS({},MGF1,{})", m_hash->name(), m_salt_size);
199}
200
202 m_hash(HashFunction::create_or_throw(options.hash_function_name())),
203 m_salt_size(options.salt_size().value_or(m_hash->output_length())),
204 m_required_salt_len(options.using_salt_size()) {
205 if(options.using_deterministic_signature() && m_salt_size > 0) {
206 throw Invalid_Argument("PSS with a non-zero salt cannot produce deterministic signatures");
207 }
208}
209
210/*
211* PSS_Raw Update Operation
212*/
213void PSS_Raw::update(const uint8_t input[], size_t length) {
214 if(length > 0) {
215 if(m_msg.size() + length > m_hash->output_length()) {
216 throw Encoding_Error("PSS_Raw: Input length exceeded hash output");
217 }
218 m_msg.insert(m_msg.end(), input, input + length);
219 }
220}
221
222/*
223* Return the raw (unencoded) data
224*/
225std::vector<uint8_t> PSS_Raw::raw_data() {
226 std::vector<uint8_t> ret;
227 std::swap(ret, m_msg);
228
229 if(ret.size() != m_hash->output_length()) {
230 throw Encoding_Error("PSS_Raw Bad input length, did not match hash");
231 }
232
233 return ret;
234}
235
236std::vector<uint8_t> PSS_Raw::encoding_of(std::span<const uint8_t> msg,
237 size_t output_bits,
239 const auto salt = rng.random_vec<std::vector<uint8_t>>(m_salt_size);
240 return pss_encode(*m_hash, msg, salt, output_bits);
241}
242
243/*
244* PSS_Raw Decode/Verify Operation
245*/
246bool PSS_Raw::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t key_bits) {
247 size_t salt_size = 0;
248 const bool ok = pss_verify(*m_hash, coded, raw, key_bits, &salt_size);
249
250 if(m_required_salt_len && salt_size != m_salt_size) {
251 return false;
252 }
253
254 return ok;
255}
256
257std::string PSS_Raw::hash_function() const {
258 return m_hash->name();
259}
260
261std::string PSS_Raw::name() const {
262 return fmt("PSS_Raw({},MGF1,{})", m_hash->name(), m_salt_size);
263}
264
265} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
Helper class to ease in-place marshalling of concatenated fixed-length values.
std::string name() const override
Definition pssr.cpp:197
std::string hash_function() const override
Definition pssr.cpp:193
PSSR(const PK_Signature_Options_Reader &options)
Definition pssr.cpp:151
std::string name() const override
Definition pssr.cpp:261
std::string hash_function() const override
Definition pssr.cpp:257
PSS_Raw(const PK_Signature_Options_Reader &options)
Definition pssr.cpp:201
static std::unique_ptr< SignaturePaddingScheme > create_or_throw(const PK_Signature_Options_Reader &options)
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:803
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void mgf1_mask(HashFunction &hash, std::span< const uint8_t > input, std::span< uint8_t > output)
Definition mgf1.cpp:15
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
Definition bit_ops.h:176
BOTAN_FORCE_INLINE constexpr size_t high_bit(T n)
Definition bit_ops.h:73