8#include <botan/internal/pssr.h>
10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
13#include <botan/internal/bit_ops.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/mgf1.h>
16#include <botan/internal/stl_util.h>
26std::vector<uint8_t> pss_encode(HashFunction& hash,
27 const std::vector<uint8_t>& msg,
28 const std::vector<uint8_t>& salt,
30 const size_t HASH_SIZE = hash.output_length();
32 if(msg.size() != HASH_SIZE) {
33 throw Encoding_Error(
"Cannot encode PSS string, input length invalid for hash");
35 if(output_bits < 8 * HASH_SIZE + 8 * salt.size() + 9) {
36 throw Encoding_Error(
"Cannot encode PSS string, output length too small");
40 const uint8_t db0_mask = 0xFF >> (8 * output_length - output_bits);
42 std::array<uint8_t, 8> padding = {0};
46 std::vector<uint8_t> H = hash.final_stdvec();
48 const size_t db_len = output_length - HASH_SIZE - 1;
49 std::vector<uint8_t> EM(output_length);
51 BufferStuffer stuffer(EM);
52 stuffer.append(0x00, stuffer.remaining_capacity() - (1 + salt.size() + H.size() + 1));
56 mgf1_mask(hash, H.data(), H.size(), EM.data(), db_len);
66bool pss_verify(HashFunction& hash,
67 const std::vector<uint8_t>& pss_repr,
68 const std::vector<uint8_t>& message_hash,
70 size_t* out_salt_size) {
71 const size_t HASH_SIZE = hash.output_length();
74 if(key_bits < 8 * HASH_SIZE + 9) {
78 if(message_hash.size() != HASH_SIZE) {
82 if(pss_repr.size() > key_bytes || pss_repr.size() <= 1) {
86 if(pss_repr[pss_repr.size() - 1] != 0xBC) {
90 std::vector<uint8_t> coded = pss_repr;
91 if(coded.size() < key_bytes) {
92 std::vector<uint8_t> temp(key_bytes);
93 BufferStuffer stuffer(temp);
94 stuffer.append(0x00, stuffer.remaining_capacity() - coded.size());
95 stuffer.append(coded);
99 const size_t TOP_BITS = 8 * ((key_bits + 7) / 8) - key_bits;
100 if(TOP_BITS > 8 -
high_bit(coded[0])) {
104 uint8_t* DB = coded.data();
105 const size_t DB_size = coded.size() - HASH_SIZE - 1;
107 const uint8_t* H = &coded[DB_size];
108 const size_t H_size = HASH_SIZE;
111 DB[0] &= 0xFF >> TOP_BITS;
113 size_t salt_offset = 0;
114 for(
size_t j = 0; j != DB_size; ++j) {
123 if(salt_offset == 0) {
127 const size_t salt_size = DB_size - salt_offset;
129 std::array<uint8_t, 8> padding = {0};
130 hash.update(padding);
131 hash.update(message_hash);
132 hash.update(&DB[salt_offset], salt_size);
134 const std::vector<uint8_t> H2 = hash.final_stdvec();
136 const bool ok =
CT::is_equal(H, H2.data(), HASH_SIZE).as_bool();
138 if(out_salt_size && ok) {
139 *out_salt_size = salt_size;
148 m_hash(std::move(hash)), m_salt_size(m_hash->output_length()), m_required_salt_len(false) {}
150PSSR::PSSR(std::unique_ptr<HashFunction> hash,
size_t salt_size) :
151 m_hash(std::move(hash)), m_salt_size(salt_size), m_required_salt_len(true) {}
156void PSSR::update(
const uint8_t input[],
size_t length) {
157 m_hash->update(input, length);
163std::vector<uint8_t> PSSR::raw_data() {
164 return m_hash->final_stdvec();
167std::vector<uint8_t> PSSR::encoding_of(
const std::vector<uint8_t>& msg,
169 RandomNumberGenerator& rng) {
170 const auto salt = rng.random_vec<std::vector<uint8_t>>(m_salt_size);
171 return pss_encode(*m_hash, msg, salt, output_bits);
177bool PSSR::verify(
const std::vector<uint8_t>& coded,
const std::vector<uint8_t>& raw,
size_t key_bits) {
178 size_t salt_size = 0;
179 const bool ok = pss_verify(*m_hash, coded, raw, key_bits, &salt_size);
181 if(m_required_salt_len && salt_size != m_salt_size) {
189 return "EMSA4(" + m_hash->name() +
",MGF1," + std::to_string(m_salt_size) +
")";
193 m_hash(std::move(hash)), m_salt_size(m_hash->output_length()), m_required_salt_len(false) {}
196 m_hash(std::move(hash)), m_salt_size(salt_size), m_required_salt_len(true) {}
201void PSSR_Raw::update(
const uint8_t input[],
size_t length) {
202 m_msg.insert(m_msg.end(), input, input + length);
208std::vector<uint8_t> PSSR_Raw::raw_data() {
209 std::vector<uint8_t> ret;
210 std::swap(ret, m_msg);
212 if(ret.size() != m_hash->output_length()) {
213 throw Encoding_Error(
"PSSR_Raw Bad input length, did not match hash");
219std::vector<uint8_t> PSSR_Raw::encoding_of(
const std::vector<uint8_t>& msg,
221 RandomNumberGenerator& rng) {
222 const auto salt = rng.random_vec<std::vector<uint8_t>>(m_salt_size);
223 return pss_encode(*m_hash, msg, salt, output_bits);
229bool PSSR_Raw::verify(
const std::vector<uint8_t>& coded,
const std::vector<uint8_t>& raw,
size_t key_bits) {
230 size_t salt_size = 0;
231 const bool ok = pss_verify(*m_hash, coded, raw, key_bits, &salt_size);
233 if(m_required_salt_len && salt_size != m_salt_size) {
241 return "PSSR_Raw(" + m_hash->name() +
",MGF1," + std::to_string(m_salt_size) +
")";
#define BOTAN_ASSERT_NOMSG(expr)
PSSR_Raw(std::unique_ptr< HashFunction > hash)
std::string name() const override
std::string name() const override
PSSR(std::unique_ptr< HashFunction > hash)
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
void mgf1_mask(HashFunction &hash, const uint8_t in[], size_t in_len, uint8_t out[], size_t out_len)
constexpr size_t high_bit(T n)
constexpr T ceil_tobytes(T bits)