Botan 3.9.0
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/exceptn.h>
10#include <botan/hash.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/fmt.h>
13#include <botan/internal/hash_id.h>
14#include <botan/internal/stl_util.h>
15
16namespace Botan {
17
18namespace {
19
20std::vector<uint8_t> x931_encoding(std::span<const uint8_t> msg,
21 size_t output_bits,
22 std::span<const uint8_t> empty_hash,
23 uint8_t hash_id) {
24 const size_t HASH_SIZE = empty_hash.size();
25
26 const size_t output_length = (output_bits + 1) / 8;
27
28 if(msg.size() != HASH_SIZE) {
29 throw Encoding_Error("X931_SignaturePadding::encoding_of: Bad input length");
30 }
31 if(output_length < HASH_SIZE + 4) {
32 throw Encoding_Error("X931_SignaturePadding::encoding_of: Output length is too small");
33 }
34
35 const bool empty_input = constant_time_compare(msg, empty_hash);
36
37 std::vector<uint8_t> output(output_length);
38 BufferStuffer stuffer(output);
39
40 stuffer.append(empty_input ? 0x4B : 0x6B);
41 stuffer.append(0xBB, stuffer.remaining_capacity() - (1 + msg.size() + 2));
42 stuffer.append(0xBA);
43 stuffer.append(msg);
44 stuffer.append(hash_id);
45 stuffer.append(0xCC);
46 BOTAN_ASSERT_NOMSG(stuffer.full());
47
48 return output;
49}
50
51} // namespace
52
54 return m_hash->name();
55}
56
57std::string X931_SignaturePadding::name() const {
58 return fmt("X9.31({})", m_hash->name());
59}
60
61void X931_SignaturePadding::update(const uint8_t input[], size_t length) {
62 m_hash->update(input, length);
63}
64
65std::vector<uint8_t> X931_SignaturePadding::raw_data() {
66 return m_hash->final_stdvec();
67}
68
69/*
70* X931_SignaturePadding Encode Operation
71*/
72std::vector<uint8_t> X931_SignaturePadding::encoding_of(std::span<const uint8_t> msg,
73 size_t output_bits,
74 RandomNumberGenerator& /*rng*/) {
75 return x931_encoding(msg, output_bits, m_empty_hash, m_hash_id);
76}
77
78/*
79* X931_SignaturePadding Verify Operation
80*/
81bool X931_SignaturePadding::verify(std::span<const uint8_t> coded, std::span<const uint8_t> raw, size_t key_bits) {
82 try {
83 const auto x931 = x931_encoding(raw, key_bits, m_empty_hash, m_hash_id);
84 return constant_time_compare(coded, x931);
85 } catch(...) {
86 return false;
87 }
88}
89
90/*
91* X931_SignaturePadding Constructor
92*/
93X931_SignaturePadding::X931_SignaturePadding(std::unique_ptr<HashFunction> hash) : m_hash(std::move(hash)) {
94 m_empty_hash = m_hash->final_stdvec();
95
96 m_hash_id = ieee1363_hash_id(m_hash->name());
97
98 if(m_hash_id == 0) {
99 throw Encoding_Error("X931_SignaturePadding no hash identifier for " + m_hash->name());
100 }
101}
102
103} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
Helper class to ease in-place marshalling of concatenated fixed-length values.
Definition stl_util.h:134
virtual std::vector< uint8_t > raw_data()=0
std::string name() const override
std::string hash_function() const override
X931_SignaturePadding(std::unique_ptr< HashFunction > hash)
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
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