/src/botan/src/lib/utils/poly_dbl/poly_dbl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 2017,2018 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include <botan/internal/poly_dbl.h> |
8 | | |
9 | | #include <botan/exceptn.h> |
10 | | #include <botan/internal/ct_utils.h> |
11 | | #include <botan/internal/loadstor.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | namespace { |
16 | | |
17 | | /* |
18 | | * The minimum weight irreducible binary polynomial of size n |
19 | | * |
20 | | * See "Table of Low-Weight Binary Irreducible Polynomials" |
21 | | * by Gadiel Seroussi, HP Labs Tech Report HPL-98-135 |
22 | | * http://www.hpl.hp.com/techreports/98/HPL-98-135.pdf |
23 | | */ |
24 | | enum class MinWeightPolynomial : uint32_t { |
25 | | P64 = 0x1B, |
26 | | P128 = 0x87, |
27 | | P192 = 0x87, |
28 | | P256 = 0x425, |
29 | | P512 = 0x125, |
30 | | P1024 = 0x80043, |
31 | | }; |
32 | | |
33 | | /** |
34 | | * If the top bit of c is set, returns the carry (the polynomial) |
35 | | * |
36 | | * Otherwise returns zero. |
37 | | */ |
38 | | template <MinWeightPolynomial P> |
39 | 641 | inline uint64_t return_carry(uint64_t c) { |
40 | 641 | return CT::Mask<uint64_t>::expand_top_bit(c).if_set_return(static_cast<uint64_t>(P)); |
41 | 641 | } Unexecuted instantiation: poly_dbl.cpp:unsigned long Botan::(anonymous namespace)::return_carry<(Botan::(anonymous namespace)::MinWeightPolynomial)27>(unsigned long) Unexecuted instantiation: poly_dbl.cpp:unsigned long Botan::(anonymous namespace)::return_carry<(Botan::(anonymous namespace)::MinWeightPolynomial)1061>(unsigned long) Unexecuted instantiation: poly_dbl.cpp:unsigned long Botan::(anonymous namespace)::return_carry<(Botan::(anonymous namespace)::MinWeightPolynomial)293>(unsigned long) Unexecuted instantiation: poly_dbl.cpp:unsigned long Botan::(anonymous namespace)::return_carry<(Botan::(anonymous namespace)::MinWeightPolynomial)524355>(unsigned long) poly_dbl.cpp:unsigned long Botan::(anonymous namespace)::return_carry<(Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned long) Line | Count | Source | 39 | 641 | inline uint64_t return_carry(uint64_t c) { | 40 | 641 | return CT::Mask<uint64_t>::expand_top_bit(c).if_set_return(static_cast<uint64_t>(P)); | 41 | 641 | } |
|
42 | | |
43 | | template <size_t LIMBS, MinWeightPolynomial P> |
44 | 641 | void poly_double(uint8_t out[], const uint8_t in[]) { |
45 | 641 | uint64_t W[LIMBS]; |
46 | 641 | load_be(W, in, LIMBS); |
47 | | |
48 | 641 | const uint64_t carry = return_carry<P>(W[0]); |
49 | | |
50 | 641 | if constexpr(LIMBS > 0) { |
51 | 1.28k | for(size_t i = 0; i != LIMBS - 1; ++i) { |
52 | 641 | W[i] = (W[i] << 1) ^ (W[i + 1] >> 63); |
53 | 641 | } |
54 | 641 | } |
55 | | |
56 | 641 | W[LIMBS - 1] = (W[LIMBS - 1] << 1) ^ carry; |
57 | | |
58 | 641 | copy_out_be(std::span(out, LIMBS * 8), W); |
59 | 641 | } Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<1ul, (Botan::(anonymous namespace)::MinWeightPolynomial)27>(unsigned char*, unsigned char const*) poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<2ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Line | Count | Source | 44 | 641 | void poly_double(uint8_t out[], const uint8_t in[]) { | 45 | 641 | uint64_t W[LIMBS]; | 46 | 641 | load_be(W, in, LIMBS); | 47 | | | 48 | 641 | const uint64_t carry = return_carry<P>(W[0]); | 49 | | | 50 | 641 | if constexpr(LIMBS > 0) { | 51 | 1.28k | for(size_t i = 0; i != LIMBS - 1; ++i) { | 52 | 641 | W[i] = (W[i] << 1) ^ (W[i + 1] >> 63); | 53 | 641 | } | 54 | 641 | } | 55 | | | 56 | 641 | W[LIMBS - 1] = (W[LIMBS - 1] << 1) ^ carry; | 57 | | | 58 | 641 | copy_out_be(std::span(out, LIMBS * 8), W); | 59 | 641 | } |
Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<3ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<4ul, (Botan::(anonymous namespace)::MinWeightPolynomial)1061>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<8ul, (Botan::(anonymous namespace)::MinWeightPolynomial)293>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double<16ul, (Botan::(anonymous namespace)::MinWeightPolynomial)524355>(unsigned char*, unsigned char const*) |
60 | | |
61 | | template <size_t LIMBS, MinWeightPolynomial P> |
62 | 0 | void poly_double_le(uint8_t out[], const uint8_t in[]) { |
63 | 0 | uint64_t W[LIMBS]; |
64 | 0 | load_le(W, in, LIMBS); |
65 | |
|
66 | 0 | const uint64_t carry = return_carry<P>(W[LIMBS - 1]); |
67 | |
|
68 | 0 | if constexpr(LIMBS > 0) { |
69 | 0 | for(size_t i = 0; i != LIMBS - 1; ++i) { |
70 | 0 | W[LIMBS - 1 - i] = (W[LIMBS - 1 - i] << 1) ^ (W[LIMBS - 2 - i] >> 63); |
71 | 0 | } |
72 | 0 | } |
73 | |
|
74 | 0 | W[0] = (W[0] << 1) ^ carry; |
75 | |
|
76 | 0 | copy_out_le(std::span(out, LIMBS * 8), W); |
77 | 0 | } Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<1ul, (Botan::(anonymous namespace)::MinWeightPolynomial)27>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<2ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<3ul, (Botan::(anonymous namespace)::MinWeightPolynomial)135>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<4ul, (Botan::(anonymous namespace)::MinWeightPolynomial)1061>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<8ul, (Botan::(anonymous namespace)::MinWeightPolynomial)293>(unsigned char*, unsigned char const*) Unexecuted instantiation: poly_dbl.cpp:void Botan::(anonymous namespace)::poly_double_le<16ul, (Botan::(anonymous namespace)::MinWeightPolynomial)524355>(unsigned char*, unsigned char const*) |
78 | | |
79 | | } // namespace |
80 | | |
81 | 641 | void poly_double_n(uint8_t out[], const uint8_t in[], size_t n) { |
82 | 641 | switch(n) { |
83 | 0 | case 8: |
84 | 0 | return poly_double<1, MinWeightPolynomial::P64>(out, in); |
85 | 641 | case 16: |
86 | 641 | return poly_double<2, MinWeightPolynomial::P128>(out, in); |
87 | 0 | case 24: |
88 | 0 | return poly_double<3, MinWeightPolynomial::P192>(out, in); |
89 | 0 | case 32: |
90 | 0 | return poly_double<4, MinWeightPolynomial::P256>(out, in); |
91 | 0 | case 64: |
92 | 0 | return poly_double<8, MinWeightPolynomial::P512>(out, in); |
93 | 0 | case 128: |
94 | 0 | return poly_double<16, MinWeightPolynomial::P1024>(out, in); |
95 | 0 | default: |
96 | 0 | throw Invalid_Argument("Unsupported size for poly_double_n"); |
97 | 641 | } |
98 | 641 | } |
99 | | |
100 | 0 | void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n) { |
101 | 0 | switch(n) { |
102 | 0 | case 8: |
103 | 0 | return poly_double_le<1, MinWeightPolynomial::P64>(out, in); |
104 | 0 | case 16: |
105 | 0 | return poly_double_le<2, MinWeightPolynomial::P128>(out, in); |
106 | 0 | case 24: |
107 | 0 | return poly_double_le<3, MinWeightPolynomial::P192>(out, in); |
108 | 0 | case 32: |
109 | 0 | return poly_double_le<4, MinWeightPolynomial::P256>(out, in); |
110 | 0 | case 64: |
111 | 0 | return poly_double_le<8, MinWeightPolynomial::P512>(out, in); |
112 | 0 | case 128: |
113 | 0 | return poly_double_le<16, MinWeightPolynomial::P1024>(out, in); |
114 | 0 | default: |
115 | 0 | throw Invalid_Argument("Unsupported size for poly_double_n_le"); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | 0 | void xts_update_tweak_block(uint8_t tweak[], size_t BS, size_t blocks_in_tweak) { |
120 | 0 | if(BS == 16) { |
121 | 0 | constexpr size_t LIMBS = 2; |
122 | |
|
123 | 0 | uint64_t W[LIMBS]; |
124 | 0 | load_le(W, &tweak[0], LIMBS); |
125 | |
|
126 | 0 | for(size_t i = 1; i < blocks_in_tweak; ++i) { |
127 | 0 | const uint64_t carry = return_carry<MinWeightPolynomial::P128>(W[1]); |
128 | 0 | W[1] = (W[1] << 1) ^ (W[0] >> 63); |
129 | 0 | W[0] = (W[0] << 1) ^ carry; |
130 | 0 | copy_out_le(std::span(&tweak[i * BS], 2 * 8), W); |
131 | 0 | } |
132 | 0 | } else { |
133 | 0 | for(size_t i = 1; i < blocks_in_tweak; ++i) { |
134 | 0 | const uint8_t* prev = &tweak[(i - 1) * BS]; |
135 | 0 | uint8_t* cur = &tweak[i * BS]; |
136 | 0 | poly_double_n_le(cur, prev, BS); |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | } // namespace Botan |