8#include <botan/internal/oaep.h>
10#include <botan/exceptn.h>
11#include <botan/pk_options_readers.h>
13#include <botan/internal/buffer_stuffer.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/mgf1.h>
22size_t OAEP::pad(std::span<uint8_t> output,
23 std::span<const uint8_t> input,
29 throw Invalid_Argument(
"OAEP: Input is too large");
32 const size_t output_size = key_length;
34 output = output.first(output_size);
36 BufferStuffer stuffer(output);
39 rng.randomize(stuffer.next(m_Phash.size()));
40 stuffer.append(m_Phash);
41 stuffer.append(0x00, stuffer.remaining_capacity() - (1 + input.size()));
43 stuffer.append(input);
46 const size_t hlen = m_Phash.size();
48 mgf1_mask(*m_mgf1_hash, output.first(hlen), output.subspan(hlen));
50 mgf1_mask(*m_mgf1_hash, output.subspan(hlen), output.first(hlen));
58CT::Option<size_t> OAEP::unpad(std::span<uint8_t> output, std::span<const uint8_t> input)
const {
81 const size_t hlen = m_Phash.size();
83 if(input.size() < 1 + 2 * hlen + 1) {
92 const auto buf = std::span{decoded};
94 mgf1_mask(*m_mgf1_hash, buf.subspan(hlen), buf.first(hlen));
96 mgf1_mask(*m_mgf1_hash, buf.first(hlen), buf.subspan(hlen));
100 return CT::copy_output(delim.has_value() && has_leading_0, output, buf, delim.value_or(0));
105 if(input.size() < 1 + 2 * phash.size()) {
109 size_t delim_idx = 2 * phash.size();
113 for(
const uint8_t ib : input.subspan(2 * phash.size())) {
117 const auto add_m = waiting_for_delim & zero_m;
119 bad_input_m |= waiting_for_delim & ~(zero_m | one_m);
123 waiting_for_delim &= zero_m;
127 bad_input_m |= waiting_for_delim;
130 bad_input_m |=
CT::is_not_equal(&input[phash.size()], phash.data(), phash.size());
134 const auto accept = !(bad_input_m.
as_choice());
143 if(keybits / 8 > 2 * m_Phash.size() + 1) {
144 return ((keybits / 8) - 2 * m_Phash.size() - 1);
152 throw Lookup_Error(
"OAEP requires specifying a hash function");
160 m_mgf1_hash = hash->new_object();
164 if(
const auto& label = options.
context()) {
165 hash->update(*label);
167 m_Phash = hash->final();
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);
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);
#define BOTAN_ASSERT_NOMSG(expr)
static constexpr Mask< T > set()
constexpr T if_set_return(T x) const
constexpr CT::Choice as_choice() const
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_zero(T x)
static constexpr Mask< T > cleared()
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
OAEP(const PK_Encryption_Options_Reader &options)
size_t maximum_input_size(size_t keybits) const override
bool using_mgf1_hash() const
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)
constexpr auto scoped_poison(const Ts &... xs)
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)
void mgf1_mask(HashFunction &hash, std::span< const uint8_t > input, std::span< uint8_t > output)
CT::Option< size_t > oaep_find_delim(std::span< const uint8_t > input, std::span< const uint8_t > phash)
std::vector< T, secure_allocator< T > > secure_vector