Botan 3.14.1
Crypto and TLS for C&
ct_utils.cpp
Go to the documentation of this file.
1/*
2* (C) 2018,2021 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/ct_utils.h>
8
9#include <botan/mem_ops.h>
10
11namespace Botan {
12
14 std::span<uint8_t> output,
15 std::span<const uint8_t> input,
16 size_t offset) {
17 // This leaks information about the input length, but this happens
18 // unavoidably since we are unable to ready any bytes besides those
19 // in input[0..n]
20 BOTAN_ARG_CHECK(output.size() >= input.size(), "Invalid span lengths");
21
22 /*
23 * We do not poison the input here because if we did we would have
24 * to unpoison it at exit. We assume instead that callers have
25 * already poisoned the input and will unpoison it, and the outputs,
26 * at their own time.
27 */
28
29 /**
30 * Zeroize the entire output buffer to get started
31 */
32 clear_mem(output);
33
34 /*
35 * If the offset is greater than input length, then the arguments are
36 * invalid. Ideally we would throw an exception, but that leaks
37 * information about the offset. Instead treat it as if the input
38 * was invalid.
39 */
40 accept = accept && CT::Mask<size_t>::is_lte(offset, input.size()).as_choice();
41
42 /*
43 * If the input is invalid, then set offset == input_length
44 */
45 offset = CT::Mask<size_t>::from_choice(accept).select(offset, input.size());
46
47 /*
48 * Move the desired output bytes to the front using a slow (O^n)
49 * but constant time loop that does not leak the value of the offset
50 */
51 for(size_t i = 0; i != input.size(); ++i) {
52 /*
53 * If bad_input was set then we modified offset to equal the input_length.
54 * In that case, this_loop will be greater than input_length, and so is_eq
55 * mask will always be false. As a result none of the input values will be
56 * written to output.
57 *
58 * This is ignoring the possibility of integer overflow of offset + i. But
59 * for this to happen the input would have to consume nearly the entire
60 * address space.
61 */
62 const size_t this_loop = offset + i;
63
64 /*
65 start index from i rather than 0 since we know j must be >= i + offset
66 to have any effect, and starting from i does not reveal information
67 */
68 for(size_t j = i; j != input.size(); ++j) {
69 const uint8_t b = input[j];
70 const auto is_eq = CT::Mask<size_t>::is_equal(j, this_loop);
71 output[i] |= is_eq.if_set_return(b);
72 }
73 }
74
75 // This will always be zero if the input was invalid
76 const size_t output_bytes = input.size() - offset;
77
78 return CT::Option<size_t>(output_bytes, accept);
79}
80
81size_t CT::count_leading_zero_bytes(std::span<const uint8_t> input) {
82 size_t leading_zeros = 0;
83 auto only_zeros = Mask<uint8_t>::set();
84 for(const uint8_t b : input) {
85 only_zeros &= CT::Mask<uint8_t>::is_zero(b);
86 leading_zeros += only_zeros.if_set_return(1);
87 }
88 return leading_zeros;
89}
90
91} // namespace Botan
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static constexpr Mask< T > is_lte(T x, T y)
Definition ct_utils.h:468
static constexpr Mask< T > set()
Definition ct_utils.h:387
static constexpr Mask< T > from_choice(Choice c)
Definition ct_utils.h:407
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
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
size_t count_leading_zero_bytes(std::span< const uint8_t > input)
Definition ct_utils.cpp:81
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118