Botan 3.14.1
Crypto and TLS for C&
x931_sig_padding.cpp
Go to the documentation of this file.
1/*
2* (C) 1999-2007 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/x931_sig_padding.h>
8
9#include <botan/assert.h>
10#include <botan/exceptn.h>
11#include <botan/hash.h>
12#include <botan/mem_ops.h>
13#include <botan/pk_options_readers.h>
14#include <botan/internal/buffer_stuffer.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/hash_id.h>
17#include <botan/internal/pk_options_impl.h>
18
19namespace Botan {
20
21namespace {
22
23std::vector<uint8_t> x931_encoding(std::span<const uint8_t> msg,
24 size_t output_bits,
25 std::span<const uint8_t> empty_hash,
26 uint8_t hash_id) {
27 const size_t HASH_SIZE = empty_hash.size();
28
29 const size_t output_length = (output_bits + 1) / 8;
30
31 if(msg.size() != HASH_SIZE) {
32 throw Encoding_Error("X931_SignaturePadding::encoding_of: Bad input length");
33 }
34 if(output_length < HASH_SIZE + 4) {
35 throw Encoding_Error("X931_SignaturePadding::encoding_of: Output length is too small");
36 }
37
38 const bool empty_input = constant_time_compare(msg, empty_hash);
39
40 std::vector<uint8_t> output(output_length);
41 BufferStuffer stuffer(output);
42
43 stuffer.append(empty_input ? 0x4B : 0x6B);
44 stuffer.append(0xBB, stuffer.remaining_capacity() - (1 + msg.size() + 2));
45 stuffer.append(0xBA);
46 stuffer.append(msg);
47 stuffer.append(hash_id);
48 stuffer.append(0xCC);
49 BOTAN_ASSERT_NOMSG(stuffer.full());
50
51 return output;
52}
53
54} // namespace
55
57 return m_hash->name();
58}
59
60std::string X931_SignaturePadding::name() const {
61 return fmt("X9.31({})", m_hash->name());
62}
63
64void X931_SignaturePadding::update(const uint8_t input[], size_t length) {
65 m_hash->update(input, length);
66}
67
68std::vector<uint8_t> X931_SignaturePadding::raw_data() {
69 return m_hash->final_stdvec();
70}
71
72/*
73* X931_SignaturePadding Encode Operation
74*/
75std::vector<uint8_t> X931_SignaturePadding::encoding_of(std::span<const uint8_t> msg,
76 size_t output_bits,
77 RandomNumberGenerator& /*rng*/) {
78 return x931_encoding(msg, output_bits, m_empty_hash, m_hash_id);
79}
80
81/*
82* X931_SignaturePadding Verify Operation
83*/
84bool X931_SignaturePadding::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t key_bits) {
85 try {
86 const auto x931 = x931_encoding(raw, key_bits, m_empty_hash, m_hash_id);
87 return constant_time_compare(coded, x931);
88 } catch(...) {
89 return false;
90 }
91}
92
93/*
94* X931_SignaturePadding Constructor
95*/
97 m_hash(HashFunction::create_or_throw(options.hash_function_name())) {
99
100 m_empty_hash = m_hash->final_stdvec();
101
102 m_hash_id = ieee1363_hash_id(m_hash->name());
103
104 if(m_hash_id == 0) {
105 throw Encoding_Error("X931_SignaturePadding no hash identifier for " + m_hash->name());
106 }
107}
108
109} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
Helper class to ease in-place marshalling of concatenated fixed-length values.
static std::unique_ptr< SignaturePaddingScheme > create_or_throw(const PK_Signature_Options_Reader &options)
virtual std::vector< uint8_t > raw_data()=0
X931_SignaturePadding(const PK_Signature_Options_Reader &options)
std::string name() const override
std::string hash_function() const override
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void acknowledge_always_deterministic(const PK_Signature_Options_Reader &options)
uint8_t ieee1363_hash_id(std::string_view name)
Definition hash_id.cpp:144
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17