8#include <botan/internal/pssr.h>
10#include <botan/assert.h>
11#include <botan/exceptn.h>
12#include <botan/hash.h>
13#include <botan/pk_options_readers.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>
29 std::span<const uint8_t> msg,
30 std::span<const uint8_t> salt,
32 const size_t HASH_SIZE = hash.output_length();
34 if(msg.size() != HASH_SIZE) {
35 throw Encoding_Error(
"Cannot encode PSS string, input length invalid for hash");
37 if(output_bits < 8 * HASH_SIZE + 8 * salt.size() + 9) {
38 throw Encoding_Error(
"Cannot encode PSS string, output length too small");
42 const uint8_t db0_mask = 0xFF >> (8 * output_length - output_bits);
44 std::array<uint8_t, 8> padding = {0};
48 std::vector<uint8_t> H = hash.final_stdvec();
50 const size_t db_len = output_length - HASH_SIZE - 1;
51 std::vector<uint8_t> EM(output_length);
54 stuffer.append(0x00, stuffer.remaining_capacity() - (1 + salt.size() + H.size() + 1));
58 mgf1_mask(hash, H, std::span{EM}.first(db_len));
69 std::span<const uint8_t> pss_repr,
70 std::span<const uint8_t> message_hash,
72 size_t* out_salt_size) {
73 const size_t HASH_SIZE = hash.output_length();
76 if(key_bits < 8 * HASH_SIZE + 9) {
80 if(message_hash.size() != HASH_SIZE) {
84 if(pss_repr.size() > key_bytes || pss_repr.size() <= 1) {
88 if(pss_repr[pss_repr.size() - 1] != 0xBC) {
92 std::vector<uint8_t> coded;
93 if(pss_repr.size() < key_bytes) {
94 coded.resize(key_bytes);
96 stuffer.append(0x00, key_bytes - pss_repr.size());
97 stuffer.append(pss_repr);
99 coded.assign(pss_repr.begin(), pss_repr.end());
103 const size_t top_bits = 8 * ((key_bits + 7) / 8) - key_bits;
104 if(top_bits > 8 -
high_bit(coded[0])) {
108 uint8_t* DB = coded.data();
109 const size_t DB_size = coded.size() - HASH_SIZE - 1;
111 const uint8_t* H = &coded[DB_size];
112 const size_t H_size = HASH_SIZE;
114 mgf1_mask(hash, {H, H_size}, {DB, DB_size});
115 DB[0] &= 0xFF >> top_bits;
117 size_t salt_offset = 0;
118 for(
size_t j = 0; j != DB_size; ++j) {
127 if(salt_offset == 0) {
131 const size_t salt_size = DB_size - salt_offset;
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);
138 const std::vector<uint8_t> H2 = hash.final_stdvec();
140 const bool ok =
CT::is_equal(H, H2.data(), HASH_SIZE).as_bool();
142 if(ok && out_salt_size !=
nullptr) {
143 *out_salt_size = salt_size;
153 m_salt_size(options.salt_size().value_or(m_hash->output_length())),
154 m_required_salt_len(options.using_salt_size()) {
156 throw Invalid_Argument(
"PSS with a non-zero salt cannot produce deterministic signatures");
163void PSSR::update(
const uint8_t input[],
size_t length) {
164 m_hash->update(input, length);
171 return m_hash->final_stdvec();
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);
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);
186 if(m_required_salt_len && salt_size != m_salt_size) {
194 return m_hash->name();
198 return fmt(
"PSS({},MGF1,{})", m_hash->name(), m_salt_size);
203 m_salt_size(options.salt_size().value_or(m_hash->output_length())),
204 m_required_salt_len(options.using_salt_size()) {
206 throw Invalid_Argument(
"PSS with a non-zero salt cannot produce deterministic signatures");
213void PSS_Raw::update(
const uint8_t input[],
size_t length) {
215 if(m_msg.size() + length > m_hash->output_length()) {
216 throw Encoding_Error(
"PSS_Raw: Input length exceeded hash output");
218 m_msg.insert(m_msg.end(), input, input + length);
226 std::vector<uint8_t> ret;
227 std::swap(ret, m_msg);
229 if(ret.size() != m_hash->output_length()) {
230 throw Encoding_Error(
"PSS_Raw Bad input length, did not match hash");
236std::vector<uint8_t> PSS_Raw::encoding_of(std::span<const uint8_t> msg,
239 const auto salt = rng.random_vec<std::vector<uint8_t>>(m_salt_size);
240 return pss_encode(*m_hash, msg, salt, output_bits);
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);
250 if(m_required_salt_len && salt_size != m_salt_size) {
258 return m_hash->name();
262 return fmt(
"PSS_Raw({},MGF1,{})", m_hash->name(), m_salt_size);
#define BOTAN_ASSERT_NOMSG(expr)
Helper class to ease in-place marshalling of concatenated fixed-length values.
bool using_deterministic_signature() const
std::string name() const override
std::string hash_function() const override
PSSR(const PK_Signature_Options_Reader &options)
std::string name() const override
std::string hash_function() const override
PSS_Raw(const PK_Signature_Options_Reader &options)
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)
std::string fmt(std::string_view format, const T &... args)
void mgf1_mask(HashFunction &hash, std::span< const uint8_t > input, std::span< uint8_t > output)
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
BOTAN_FORCE_INLINE constexpr size_t high_bit(T n)