Botan 3.10.0
Crypto and TLS for C&
eme_pkcs.cpp
Go to the documentation of this file.
1/*
2* PKCS #1 v1.5 Type 2 (encryption) padding
3* (C) 1999-2007,2015,2016,2024 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/eme_pkcs.h>
9
10#include <botan/exceptn.h>
11#include <botan/rng.h>
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/int_utils.h>
14#include <botan/internal/stl_util.h>
15
16namespace Botan {
17
18/*
19* PKCS1 Pad Operation
20*/
21size_t EME_PKCS1v15::pad(std::span<uint8_t> output,
22 std::span<const uint8_t> input,
23 size_t key_length,
24 RandomNumberGenerator& rng) const {
25 key_length /= 8;
26
27 if(input.size() > maximum_input_size(key_length * 8)) {
28 throw Invalid_Argument("PKCS1: Input is too large");
29 }
30
31 BufferStuffer stuffer(output);
32
33 const size_t padding_bytes = [&]() {
34 auto d = checked_sub(key_length, input.size() + 2);
35 BOTAN_ASSERT_NOMSG(d.has_value());
36 return *d;
37 }();
38
39 stuffer.append(0x02);
40 for(size_t i = 0; i != padding_bytes; ++i) {
41 stuffer.append(rng.next_nonzero_byte());
42 }
43 stuffer.append(0x00);
44 stuffer.append(input);
45
46 return output.size() - stuffer.remaining_capacity();
47}
48
49/*
50* PKCS1 Unpad Operation
51*/
52CT::Option<size_t> EME_PKCS1v15::unpad(std::span<uint8_t> output, std::span<const uint8_t> input) const {
53 BOTAN_ASSERT_NOMSG(output.size() >= input.size());
54
55 /*
56 * RSA decryption pads the ciphertext up to the modulus size, so this only
57 * occurs with very (!) small keys, or when fuzzing.
58 *
59 * 11 bytes == 00,02 + 8 bytes mandatory padding + 00
60 */
61 if(input.size() < 11) {
62 return {};
63 }
64
65 auto scope = CT::scoped_poison(input);
66
67 CT::Mask<uint8_t> bad_input_m = CT::Mask<uint8_t>::cleared();
68 CT::Mask<uint8_t> seen_zero_m = CT::Mask<uint8_t>::cleared();
69 size_t delim_idx = 2; // initial 0002
70
71 bad_input_m |= ~CT::Mask<uint8_t>::is_equal(input[0], 0);
72 bad_input_m |= ~CT::Mask<uint8_t>::is_equal(input[1], 2);
73
74 for(size_t i = 2; i < input.size(); ++i) {
75 const auto is_zero_m = CT::Mask<uint8_t>::is_zero(input[i]);
76 delim_idx += seen_zero_m.if_not_set_return(1);
77 seen_zero_m |= is_zero_m;
78 }
79
80 // no zero delim -> bad padding
81 bad_input_m |= ~seen_zero_m;
82 /*
83 delim indicates < 8 bytes padding -> bad padding
84
85 We require 11 here because we are counting also the 00 delim byte
86 */
87 bad_input_m |= CT::Mask<uint8_t>(CT::Mask<size_t>::is_lt(delim_idx, 11));
88
89 const CT::Choice accept = !(bad_input_m.as_choice());
90
91 return CT::copy_output(accept, output, input, delim_idx);
92}
93
94/*
95* Return the max input size for a given key size
96*/
97size_t EME_PKCS1v15::maximum_input_size(size_t keybits) const {
98 if(keybits / 8 > 10) {
99 return ((keybits / 8) - 10);
100 } else {
101 return 0;
102 }
103}
104
105} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
static constexpr Mask< T > is_lt(T x, T y)
Definition ct_utils.h:478
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:465
static constexpr Mask< T > cleared()
Definition ct_utils.h:415
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:220
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
constexpr std::optional< T > checked_sub(T a, T b)
Definition int_utils.h:28