Botan 3.14.1
Crypto and TLS for C&
xmd.cpp
Go to the documentation of this file.
1/*
2* (C) 2019,2020,2021,2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/xmd.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 <vector>
14
15namespace Botan {
16
18 std::span<uint8_t> output,
19 std::span<const uint8_t> input,
20 std::span<const uint8_t> domain_sep) {
21 const size_t block_size = hash.hash_block_size();
22 if(block_size == 0) {
23 throw Invalid_Argument(fmt("expand_message_xmd cannot be used with {}", hash.name()));
24 }
25
26 const size_t hash_output_size = hash.output_length();
27
28 // RFC 9380 Section 5.3.1: "For correctness, H requires b <= s."
29 if(hash_output_size > block_size || hash_output_size == 0 || hash_output_size >= 256) {
30 throw Invalid_Argument(fmt("expand_message_xmd cannot be used with {}", hash.name()));
31 }
32
33 if(output.size() > 255 * hash_output_size || output.size() > 0xFFFF) {
34 throw Invalid_Argument("expand_message_xmd requested output length too long");
35 }
36
37 std::vector<uint8_t> hashed_domain_sep;
38 if(domain_sep.size() > 0xFF) {
39 /* RFC 9380 section 5.3.3: "If applications require a domain
40 * separation tag longer than 255 bytes, e.g., because of
41 * requirements imposed by an invoking protocol, implementors MUST
42 * compute a short domain separation tag by hashing, as follows:
43 *
44 * For expand_message_xmd using hash function H, DST is computed as
45 *
46 * DST = H("H2C-OVERSIZE-DST-" || a_very_long_DST)"
47 */
48 hash.update("H2C-OVERSIZE-DST-");
49 hash.update(domain_sep);
50 hashed_domain_sep = hash.final_stdvec();
51 domain_sep = hashed_domain_sep;
52 }
53
54 if(domain_sep.empty()) {
55 // RFC 9380 Section 3.1: "Tags MUST have nonzero length."
56 throw Invalid_Argument("expand_message_xmd requires a non-empty domain separation tag");
57 }
58
59 const uint8_t domain_sep_len = static_cast<uint8_t>(domain_sep.size());
60
61 // Compute b_0 = H(msg_prime) = H(Z_pad || msg || l_i_b_str || 0x00 || DST_prime)
62
63 hash.update(std::vector<uint8_t>(block_size));
64 hash.update(input);
65 hash.update_be(static_cast<uint16_t>(output.size()));
66 hash.update(0x00);
67 hash.update(domain_sep);
68 hash.update(domain_sep_len);
69
70 const secure_vector<uint8_t> b_0 = hash.final();
71
72 // Compute b_1 = H(b_0 || 0x01 || DST_prime)
73
74 hash.update(b_0);
75 hash.update(0x01);
76 hash.update(domain_sep);
77 hash.update(domain_sep_len);
78
79 secure_vector<uint8_t> b_i = hash.final();
80
81 uint8_t cnt = 2;
82 for(;;) {
83 const size_t produced = std::min(output.size(), hash_output_size);
84
85 copy_mem(output.data(), b_i.data(), produced);
86 output = output.subspan(produced);
87
88 if(output.empty()) {
89 break;
90 }
91
92 // Now compute the next b_i if needed
93
94 b_i ^= b_0;
95 hash.update(b_i);
96 hash.update(cnt);
97 hash.update(domain_sep);
98 hash.update(domain_sep_len);
99 hash.final(b_i);
100 cnt += 1;
101 }
102}
103
104} // namespace Botan
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35
virtual size_t output_length() const =0
std::vector< uint8_t > final_stdvec()
Definition buf_comp.h:114
void update_be(uint16_t val)
Definition buf_comp.cpp:18
void final(uint8_t out[])
Definition buf_comp.h:97
virtual size_t hash_block_size() const
Definition hash.h:68
virtual std::string name() const =0
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
void expand_message_xmd(HashFunction &hash, std::span< uint8_t > output, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep)
Definition xmd.cpp:17
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128