/src/botan/src/lib/block/gost_28147/gost_28147.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * GOST 28147-89 |
3 | | * (C) 1999-2009,2011 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/internal/gost_28147.h> |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/internal/fmt.h> |
12 | | #include <botan/internal/loadstor.h> |
13 | | #include <botan/internal/rotate.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | 0 | uint8_t GOST_28147_89_Params::sbox_entry(size_t row, size_t col) const { |
18 | 0 | const uint8_t x = m_sboxes[4 * col + (row / 2)]; |
19 | 0 | return (row % 2 == 0) ? (x >> 4) : (x & 0x0F); |
20 | 0 | } |
21 | | |
22 | 0 | uint8_t GOST_28147_89_Params::sbox_pair(size_t row, size_t col) const { |
23 | 0 | const uint8_t x = m_sboxes[4 * (col % 16) + row]; |
24 | 0 | const uint8_t y = m_sboxes[4 * (col / 16) + row]; |
25 | 0 | return (x >> 4) | (y << 4); |
26 | 0 | } |
27 | | |
28 | 0 | GOST_28147_89_Params::GOST_28147_89_Params(std::string_view n) : m_name(n) { |
29 | | // Encoded in the packed fromat from RFC 4357 |
30 | | |
31 | | // GostR3411_94_TestParamSet (OID 1.2.643.2.2.31.0) |
32 | 0 | static const uint8_t GOST_R_3411_TEST_PARAMS[64] = { |
33 | 0 | 0x4E, 0x57, 0x64, 0xD1, 0xAB, 0x8D, 0xCB, 0xBF, 0x94, 0x1A, 0x7A, 0x4D, 0x2C, 0xD1, 0x10, 0x10, |
34 | 0 | 0xD6, 0xA0, 0x57, 0x35, 0x8D, 0x38, 0xF2, 0xF7, 0x0F, 0x49, 0xD1, 0x5A, 0xEA, 0x2F, 0x8D, 0x94, |
35 | 0 | 0x62, 0xEE, 0x43, 0x09, 0xB3, 0xF4, 0xA6, 0xA2, 0x18, 0xC6, 0x98, 0xE3, 0xC1, 0x7C, 0xE5, 0x7E, |
36 | 0 | 0x70, 0x6B, 0x09, 0x66, 0xF7, 0x02, 0x3C, 0x8B, 0x55, 0x95, 0xBF, 0x28, 0x39, 0xB3, 0x2E, 0xCC}; |
37 | | |
38 | | // GostR3411-94-CryptoProParamSet (OID 1.2.643.2.2.31.1) |
39 | 0 | static const uint8_t GOST_R_3411_CRYPTOPRO_PARAMS[64] = { |
40 | 0 | 0xA5, 0x74, 0x77, 0xD1, 0x4F, 0xFA, 0x66, 0xE3, 0x54, 0xC7, 0x42, 0x4A, 0x60, 0xEC, 0xB4, 0x19, |
41 | 0 | 0x82, 0x90, 0x9D, 0x75, 0x1D, 0x4F, 0xC9, 0x0B, 0x3B, 0x12, 0x2F, 0x54, 0x79, 0x08, 0xA0, 0xAF, |
42 | 0 | 0xD1, 0x3E, 0x1A, 0x38, 0xC7, 0xB1, 0x81, 0xC6, 0xE6, 0x56, 0x05, 0x87, 0x03, 0x25, 0xEB, 0xFE, |
43 | 0 | 0x9C, 0x6D, 0xF8, 0x6D, 0x2E, 0xAB, 0xDE, 0x20, 0xBA, 0x89, 0x3C, 0x92, 0xF8, 0xD3, 0x53, 0xBC}; |
44 | |
|
45 | 0 | if(m_name == "R3411_94_TestParam") { |
46 | 0 | m_sboxes = GOST_R_3411_TEST_PARAMS; |
47 | 0 | } else if(m_name == "R3411_CryptoPro") { |
48 | 0 | m_sboxes = GOST_R_3411_CRYPTOPRO_PARAMS; |
49 | 0 | } else { |
50 | 0 | throw Invalid_Argument(fmt("GOST_28147_89_Params: Unknown sbox params '{}'", m_name)); |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | /* |
55 | | * GOST Constructor |
56 | | */ |
57 | | GOST_28147_89::GOST_28147_89(const GOST_28147_89_Params& param) : |
58 | 0 | m_SBOX(1024), m_name(fmt("GOST-28147-89({})", param.param_name())) { |
59 | | // Convert the parallel 4x4 sboxes into larger word-based sboxes |
60 | |
|
61 | 0 | for(size_t i = 0; i != 256; ++i) { |
62 | 0 | m_SBOX[i] = rotl<11, uint32_t>(param.sbox_pair(0, i)); |
63 | 0 | m_SBOX[i + 256] = rotl<19, uint32_t>(param.sbox_pair(1, i)); |
64 | 0 | m_SBOX[i + 512] = rotl<27, uint32_t>(param.sbox_pair(2, i)); |
65 | 0 | m_SBOX[i + 768] = rotl<3, uint32_t>(param.sbox_pair(3, i)); |
66 | 0 | } |
67 | 0 | } |
68 | | |
69 | 0 | std::string GOST_28147_89::name() const { return m_name; } |
70 | | |
71 | | namespace { |
72 | | |
73 | | /* |
74 | | * Two rounds of GOST |
75 | | */ |
76 | | template <size_t R1, size_t R2> |
77 | 0 | void GOST_ROUND2(uint32_t& N1, uint32_t& N2, const std::vector<uint32_t>& S, const secure_vector<uint32_t>& EK) { |
78 | 0 | const uint32_t T0 = N1 + EK[R1]; |
79 | 0 | N2 ^= S[get_byte<3>(T0)] | S[get_byte<2>(T0) + 256] | S[get_byte<1>(T0) + 512] | S[get_byte<0>(T0) + 768]; |
80 | |
|
81 | 0 | const uint32_t T1 = N2 + EK[R2]; |
82 | 0 | N1 ^= S[get_byte<3>(T1)] | S[get_byte<2>(T1) + 256] | S[get_byte<1>(T1) + 512] | S[get_byte<0>(T1) + 768]; |
83 | 0 | } Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<0ul, 1ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<2ul, 3ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<4ul, 5ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<6ul, 7ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<7ul, 6ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<5ul, 4ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<3ul, 2ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) Unexecuted instantiation: gost_28147.cpp:void Botan::(anonymous namespace)::GOST_ROUND2<1ul, 0ul>(unsigned int&, unsigned int&, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> > const&, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&) |
84 | | |
85 | | } // namespace |
86 | | |
87 | | /* |
88 | | * GOST Encryption |
89 | | */ |
90 | 0 | void GOST_28147_89::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const { |
91 | 0 | assert_key_material_set(); |
92 | |
|
93 | 0 | for(size_t i = 0; i != blocks; ++i) { |
94 | 0 | uint32_t N1 = load_le<uint32_t>(in, 0); |
95 | 0 | uint32_t N2 = load_le<uint32_t>(in, 1); |
96 | |
|
97 | 0 | for(size_t j = 0; j != 3; ++j) { |
98 | 0 | GOST_ROUND2<0, 1>(N1, N2, m_SBOX, m_EK); |
99 | 0 | GOST_ROUND2<2, 3>(N1, N2, m_SBOX, m_EK); |
100 | 0 | GOST_ROUND2<4, 5>(N1, N2, m_SBOX, m_EK); |
101 | 0 | GOST_ROUND2<6, 7>(N1, N2, m_SBOX, m_EK); |
102 | 0 | } |
103 | |
|
104 | 0 | GOST_ROUND2<7, 6>(N1, N2, m_SBOX, m_EK); |
105 | 0 | GOST_ROUND2<5, 4>(N1, N2, m_SBOX, m_EK); |
106 | 0 | GOST_ROUND2<3, 2>(N1, N2, m_SBOX, m_EK); |
107 | 0 | GOST_ROUND2<1, 0>(N1, N2, m_SBOX, m_EK); |
108 | |
|
109 | 0 | store_le(out, N2, N1); |
110 | |
|
111 | 0 | in += BLOCK_SIZE; |
112 | 0 | out += BLOCK_SIZE; |
113 | 0 | } |
114 | 0 | } |
115 | | |
116 | | /* |
117 | | * GOST Decryption |
118 | | */ |
119 | 0 | void GOST_28147_89::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const { |
120 | 0 | assert_key_material_set(); |
121 | |
|
122 | 0 | for(size_t i = 0; i != blocks; ++i) { |
123 | 0 | uint32_t N1 = load_le<uint32_t>(in, 0); |
124 | 0 | uint32_t N2 = load_le<uint32_t>(in, 1); |
125 | |
|
126 | 0 | GOST_ROUND2<0, 1>(N1, N2, m_SBOX, m_EK); |
127 | 0 | GOST_ROUND2<2, 3>(N1, N2, m_SBOX, m_EK); |
128 | 0 | GOST_ROUND2<4, 5>(N1, N2, m_SBOX, m_EK); |
129 | 0 | GOST_ROUND2<6, 7>(N1, N2, m_SBOX, m_EK); |
130 | |
|
131 | 0 | for(size_t j = 0; j != 3; ++j) { |
132 | 0 | GOST_ROUND2<7, 6>(N1, N2, m_SBOX, m_EK); |
133 | 0 | GOST_ROUND2<5, 4>(N1, N2, m_SBOX, m_EK); |
134 | 0 | GOST_ROUND2<3, 2>(N1, N2, m_SBOX, m_EK); |
135 | 0 | GOST_ROUND2<1, 0>(N1, N2, m_SBOX, m_EK); |
136 | 0 | } |
137 | |
|
138 | 0 | store_le(out, N2, N1); |
139 | 0 | in += BLOCK_SIZE; |
140 | 0 | out += BLOCK_SIZE; |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | 0 | bool GOST_28147_89::has_keying_material() const { return !m_EK.empty(); } |
145 | | |
146 | | /* |
147 | | * GOST Key Schedule |
148 | | */ |
149 | 0 | void GOST_28147_89::key_schedule(const uint8_t key[], size_t /*length*/) { |
150 | 0 | m_EK.resize(8); |
151 | 0 | for(size_t i = 0; i != 8; ++i) { |
152 | 0 | m_EK[i] = load_le<uint32_t>(key, i); |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | void GOST_28147_89::clear() { zap(m_EK); } |
157 | | |
158 | | } // namespace Botan |