9#include <botan/internal/iso9796.h>
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <botan/hash.h>
14#include <botan/pk_options_readers.h>
16#include <botan/internal/buffer_stuffer.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/fmt.h>
19#include <botan/internal/hash_id.h>
20#include <botan/internal/mgf1.h>
21#include <botan/internal/pk_options_impl.h>
28 std::span<const uint8_t> msg1,
29 std::span<const uint8_t> hmsg2,
30 std::span<const uint8_t> salt) {
32 hash.update_be(
static_cast<uint64_t
>(msg1.size()) * 8);
36 return hash.final_stdvec();
39std::vector<uint8_t> iso9796_encoding(std::span<const uint8_t> msg,
41 std::unique_ptr<HashFunction>& hash,
45 const size_t output_length = (output_bits + 7) / 8;
48 const size_t trailer_len = (implicit) ? 1 : 2;
50 const size_t hash_len = hash->output_length();
52 if(output_length <= hash_len + salt_len + trailer_len) {
53 throw Encoding_Error(
"ISO9796-2::encoding_of: Output length is too small");
57 const size_t capacity = output_length - hash_len - salt_len - trailer_len - 1;
60 const size_t msg1_len = std::min(capacity, msg.size());
61 const auto msg1 = msg.first(msg1_len);
62 const auto msg2 = msg.subspan(msg1_len);
64 const auto hmsg2 = hash->process<std::vector<uint8_t>>(msg2);
65 const auto salt = rng.random_vec<std::vector<uint8_t>>(salt_len);
67 const auto H = iso9796_hash(*hash, msg1, hmsg2, salt);
69 std::vector<uint8_t> EM(output_length);
72 stuffer.append(0x00, stuffer.remaining_capacity() - (hash_len + salt_len + trailer_len + msg1_len + 1));
78 const size_t mgf1_bytes = EM.size() - hash_len - trailer_len;
79 mgf1_mask(*hash, H, std::span{EM}.first(mgf1_bytes));
82 EM[0] &= 0xFF >> (8 * output_length - output_bits);
93 throw Encoding_Error(
"ISO-9796: no hash identifier for " + hash->name());
95 stuffer.append(hash_id);
104bool iso9796_verification(std::span<const uint8_t> repr,
105 std::span<const uint8_t> raw,
107 std::unique_ptr<HashFunction>& hash,
112 if(repr.size() > key_bytes) {
118 std::vector<uint8_t> coded(key_bytes);
121 stuffer.append(0x00, key_bytes - repr.size());
122 stuffer.append(repr);
132 const size_t trailer_len = implicit ? 1 : 2;
134 const size_t hash_len = hash->output_length();
139 if(coded.size() <= hash_len + trailer_len + salt_len) {
144 if(coded[coded.size() - 1] != 0xBC) {
150 throw Decoding_Error(
"ISO-9796: no hash identifier for " + hash->name());
153 const uint8_t trailer_0 = coded[coded.size() - 2];
154 const uint8_t trailer_1 = coded[coded.size() - 1];
156 if(trailer_0 != hash_id || trailer_1 != 0xCC) {
163 const size_t top_bits = 8 * key_bytes - key_bits;
164 if(top_bits > 8 -
high_bit(coded[0])) {
170 uint8_t* DB = coded.data();
171 const size_t DB_size = coded.size() - hash_len - trailer_len;
173 const uint8_t* H = &coded[DB_size];
175 mgf1_mask(*hash, {H, hash_len}, {DB, DB_size});
177 DB[0] &= 0xFF >> top_bits;
180 size_t msg1_offset = 1;
185 for(
size_t j = 0; j < DB_size; ++j) {
189 const auto add_m = waiting_for_delim & is_zero;
191 bad_input |= waiting_for_delim & ~(is_zero | is_one);
192 msg1_offset += add_m.if_set_return(1);
194 waiting_for_delim &= is_zero;
198 bad_input |= waiting_for_delim;
200 const auto bad_offset =
CT::Mask<size_t>::is_lt(coded.size(), trailer_len + hash_len + msg1_offset + salt_len);
209 const size_t msg1_len = coded.size() - (trailer_len + hash_len + msg1_offset + salt_len);
211 const auto msg1 = std::span(coded).subspan(msg1_offset, msg1_len);
212 const auto salt = std::span(coded).subspan(msg1_offset + msg1.size(), salt_len);
217 const size_t capacity = key_bytes - hash_len - salt_len - trailer_len - 1;
219 std::span<const uint8_t> msg1raw = raw;
220 if(msg1raw.size() > capacity) {
221 hash->update(msg1raw.subspan(capacity));
222 msg1raw = msg1raw.first(capacity);
225 const auto hmsg2 = hash->final_stdvec();
228 const auto H2 = iso9796_hash(*hash, msg1, hmsg2, salt);
235 bad_input |=
~CT::is_equal(msg1.data(), msg1raw.data(), std::min(msg1.size(), msg1raw.size()));
238 return (bad_input.as_bool() ==
false);
245 m_implicit(!options.using_explicit_trailer_field()),
246 m_salt_len(options.salt_size().value_or(m_hash->output_length())) {
248 throw Invalid_Argument(
"ISO_9796_DS2 with a non-zero salt cannot produce deterministic signatures");
256void ISO_9796_DS2::update(
const uint8_t input[],
size_t length) {
259 m_msg_buffer.insert(m_msg_buffer.end(), input, input + length);
267 std::vector<uint8_t> retbuffer = m_msg_buffer;
268 m_msg_buffer.clear();
275std::vector<uint8_t> ISO_9796_DS2::encoding_of(std::span<const uint8_t> msg,
278 return iso9796_encoding(msg, output_bits, m_hash, m_salt_len, m_implicit, rng);
284bool ISO_9796_DS2::verify(std::span<const uint8_t> repr, std::span<const uint8_t> raw,
size_t key_bits) {
285 return iso9796_verification(repr, raw, key_bits, m_hash, m_salt_len, m_implicit);
289 return m_hash->name();
296 return fmt(
"ISO_9796_DS2({},{},{})", m_hash->name(), (m_implicit ?
"imp" :
"exp"), m_salt_len);
301 m_implicit(!options.using_explicit_trailer_field()) {
309void ISO_9796_DS3::update(
const uint8_t input[],
size_t length) {
312 m_msg_buffer.insert(m_msg_buffer.end(), input, input + length);
320 std::vector<uint8_t> retbuffer = m_msg_buffer;
321 m_msg_buffer.clear();
328std::vector<uint8_t> ISO_9796_DS3::encoding_of(std::span<const uint8_t> msg,
331 return iso9796_encoding(msg, output_bits, m_hash, 0, m_implicit, rng);
337bool ISO_9796_DS3::verify(std::span<const uint8_t> repr, std::span<const uint8_t> raw,
size_t key_bits) {
338 return iso9796_verification(repr, raw, key_bits, m_hash, 0, m_implicit);
342 return m_hash->name();
349 return fmt(
"ISO_9796_DS3({},{})", m_hash->name(), (m_implicit ?
"imp" :
"exp"));
#define BOTAN_ASSERT_NOMSG(expr)
Helper class to ease in-place marshalling of concatenated fixed-length values.
static constexpr Mask< T > set()
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_lt(T x, T y)
static constexpr Mask< T > is_zero(T x)
static constexpr Mask< T > cleared()
std::string name() const override
ISO_9796_DS2(const PK_Signature_Options_Reader &options)
std::string hash_function() const override
std::string hash_function() const override
ISO_9796_DS3(const PK_Signature_Options_Reader &options)
std::string name() const override
bool using_deterministic_signature() const
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_not_equal(const T x[], const T y[], size_t len)
constexpr void unpoison(const T *p, size_t n)
constexpr void poison(const T *p, size_t n)
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)
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
BOTAN_FORCE_INLINE constexpr T ceil_tobytes(T bits)
uint8_t ieee1363_hash_id(std::string_view name)
BOTAN_FORCE_INLINE constexpr size_t high_bit(T n)