Botan 3.14.1
Crypto and TLS for C&
oaep.cpp
Go to the documentation of this file.
1/*
2* OAEP
3* (C) 1999-2010,2015,2018,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/oaep.h>
9
10#include <botan/exceptn.h>
11#include <botan/pk_options_readers.h>
12#include <botan/rng.h>
13#include <botan/internal/buffer_stuffer.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/mgf1.h>
16
17namespace Botan {
18
19/*
20* OAEP Pad Operation
21*/
22size_t OAEP::pad(std::span<uint8_t> output,
23 std::span<const uint8_t> input,
24 size_t key_length,
25 RandomNumberGenerator& rng) const {
26 key_length /= 8;
27
28 if(input.size() > maximum_input_size(key_length * 8)) {
29 throw Invalid_Argument("OAEP: Input is too large");
30 }
31
32 const size_t output_size = key_length;
33
34 output = output.first(output_size); // remainder ignored
35
36 BufferStuffer stuffer(output);
37
38 // We always use a seed len equal to the underlying hash
39 rng.randomize(stuffer.next(m_Phash.size()));
40 stuffer.append(m_Phash);
41 stuffer.append(0x00, stuffer.remaining_capacity() - (1 + input.size()));
42 stuffer.append(0x01);
43 stuffer.append(input);
44 BOTAN_ASSERT_NOMSG(stuffer.full());
45
46 const size_t hlen = m_Phash.size();
47
48 mgf1_mask(*m_mgf1_hash, output.first(hlen), output.subspan(hlen));
49
50 mgf1_mask(*m_mgf1_hash, output.subspan(hlen), output.first(hlen));
51
52 return key_length;
53}
54
55/*
56* OAEP Unpad Operation
57*/
58CT::Option<size_t> OAEP::unpad(std::span<uint8_t> output, std::span<const uint8_t> input) const {
59 BOTAN_ASSERT_NOMSG(output.size() >= input.size());
60
61 /*
62 Must be careful about error messages here; if an attacker can
63 distinguish them, it is easy to use the differences as an oracle to
64 find the secret key, as described in "A Chosen Ciphertext Attack on
65 RSA Optimal Asymmetric Encryption Padding (OAEP) as Standardized in
66 PKCS #1 v2.0", James Manger, Crypto 2001
67
68 Also have to be careful about timing attacks! Pointed out by Falko
69 Strenzke.
70
71 According to the standard (RFC 3447 Section 7.1.1), the encryptor always
72 creates a message as follows:
73 i. Concatenate a single octet with hexadecimal value 0x00,
74 maskedSeed, and maskedDB to form an encoded message EM of
75 length k octets as
76 EM = 0x00 || maskedSeed || maskedDB.
77 where k is the length of the modulus N.
78 Therefore, the first byte should always be zero.
79 */
80
81 const size_t hlen = m_Phash.size();
82
83 if(input.size() < 1 + 2 * hlen + 1) {
84 return {};
85 }
86
87 const auto scope = CT::scoped_poison(input);
88
89 const auto has_leading_0 = CT::Mask<uint8_t>::is_zero(input[0]).as_choice();
90
91 secure_vector<uint8_t> decoded(input.begin() + 1, input.end());
92 const auto buf = std::span{decoded};
93
94 mgf1_mask(*m_mgf1_hash, buf.subspan(hlen), buf.first(hlen));
95
96 mgf1_mask(*m_mgf1_hash, buf.first(hlen), buf.subspan(hlen));
97
98 const auto delim = oaep_find_delim(buf, m_Phash);
99
100 return CT::copy_output(delim.has_value() && has_leading_0, output, buf, delim.value_or(0));
101}
102
103CT::Option<size_t> oaep_find_delim(std::span<const uint8_t> input, std::span<const uint8_t> phash) {
104 // Too short to be valid, reject immediately
105 if(input.size() < 1 + 2 * phash.size()) {
106 return {};
107 }
108
109 size_t delim_idx = 2 * phash.size();
110 CT::Mask<uint8_t> waiting_for_delim = CT::Mask<uint8_t>::set();
112
113 for(const uint8_t ib : input.subspan(2 * phash.size())) {
114 const auto zero_m = CT::Mask<uint8_t>::is_zero(ib);
115 const auto one_m = CT::Mask<uint8_t>::is_equal(ib, 1);
116
117 const auto add_m = waiting_for_delim & zero_m;
118
119 bad_input_m |= waiting_for_delim & ~(zero_m | one_m);
120
121 delim_idx += add_m.if_set_return(1);
122
123 waiting_for_delim &= zero_m;
124 }
125
126 // If we never saw any non-zero byte, then it's not valid input
127 bad_input_m |= waiting_for_delim;
128
129 // If the P hash is wrong, then it's not valid
130 bad_input_m |= CT::is_not_equal(&input[phash.size()], phash.data(), phash.size());
131
132 delim_idx += 1;
133
134 const auto accept = !(bad_input_m.as_choice());
135
136 return CT::Option(delim_idx, accept);
137}
138
139/*
140* Return the max input size for a given key size
141*/
142size_t OAEP::maximum_input_size(size_t keybits) const {
143 if(keybits / 8 > 2 * m_Phash.size() + 1) {
144 return ((keybits / 8) - 2 * m_Phash.size() - 1);
145 } else {
146 return 0;
147 }
148}
149
151 if(!options.using_hash()) {
152 throw Lookup_Error("OAEP requires specifying a hash function");
153 }
154
156
157 if(options.using_mgf1_hash()) {
158 m_mgf1_hash = HashFunction::create_or_throw(options.mgf1_hash_function().value());
159 } else {
160 m_mgf1_hash = hash->new_object();
161 }
162
163 // The label is optional and defaults to empty
164 if(const auto& label = options.context()) {
165 hash->update(*label);
166 }
167 m_Phash = hash->final();
168}
169
170OAEP::OAEP(std::unique_ptr<HashFunction> hash, std::string_view P) : m_mgf1_hash(std::move(hash)) {
171 m_Phash = m_mgf1_hash->process(P);
172}
173
174OAEP::OAEP(std::unique_ptr<HashFunction> hash, std::unique_ptr<HashFunction> mgf1_hash, std::string_view P) :
175 m_mgf1_hash(std::move(mgf1_hash)) {
176 auto phash = std::move(hash);
177 m_Phash = phash->process(P);
178}
179
180} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static constexpr Mask< T > set()
Definition ct_utils.h:387
constexpr T if_set_return(T x) const
Definition ct_utils.h:543
constexpr CT::Choice as_choice() const
Definition ct_utils.h:624
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:447
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:442
static constexpr Mask< T > cleared()
Definition ct_utils.h:392
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
OAEP(const PK_Encryption_Options_Reader &options)
Definition oaep.cpp:150
size_t maximum_input_size(size_t keybits) const override
Definition oaep.cpp:142
const std::optional< std::vector< uint8_t > > & context() const
const std::optional< std::string > & mgf1_hash_function() const
const std::string & hash_function_name() const
constexpr CT::Mask< T > is_not_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:843
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
BOTAN_TEST_API CT::Option< size_t > copy_output(CT::Choice accept, std::span< uint8_t > output, std::span< const uint8_t > input, size_t offset)
Definition ct_utils.cpp:13
void mgf1_mask(HashFunction &hash, std::span< const uint8_t > input, std::span< uint8_t > output)
Definition mgf1.cpp:15
CT::Option< size_t > oaep_find_delim(std::span< const uint8_t > input, std::span< const uint8_t > phash)
Definition oaep.cpp:103
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128