Botan 3.6.1
Crypto and TLS for C&
seed.cpp
Go to the documentation of this file.
1/*
2* SEED
3* (C) 1999-2007,2020 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/seed.h>
9
10#include <botan/internal/loadstor.h>
11#include <botan/internal/prefetch.h>
12
13namespace Botan {
14
15namespace {
16
17alignas(256) const uint8_t SEED_S0[256] = {
18 0xA9, 0x85, 0xD6, 0xD3, 0x54, 0x1D, 0xAC, 0x25, 0x5D, 0x43, 0x18, 0x1E, 0x51, 0xFC, 0xCA, 0x63, 0x28, 0x44, 0x20,
19 0x9D, 0xE0, 0xE2, 0xC8, 0x17, 0xA5, 0x8F, 0x03, 0x7B, 0xBB, 0x13, 0xD2, 0xEE, 0x70, 0x8C, 0x3F, 0xA8, 0x32, 0xDD,
20 0xF6, 0x74, 0xEC, 0x95, 0x0B, 0x57, 0x5C, 0x5B, 0xBD, 0x01, 0x24, 0x1C, 0x73, 0x98, 0x10, 0xCC, 0xF2, 0xD9, 0x2C,
21 0xE7, 0x72, 0x83, 0x9B, 0xD1, 0x86, 0xC9, 0x60, 0x50, 0xA3, 0xEB, 0x0D, 0xB6, 0x9E, 0x4F, 0xB7, 0x5A, 0xC6, 0x78,
22 0xA6, 0x12, 0xAF, 0xD5, 0x61, 0xC3, 0xB4, 0x41, 0x52, 0x7D, 0x8D, 0x08, 0x1F, 0x99, 0x00, 0x19, 0x04, 0x53, 0xF7,
23 0xE1, 0xFD, 0x76, 0x2F, 0x27, 0xB0, 0x8B, 0x0E, 0xAB, 0xA2, 0x6E, 0x93, 0x4D, 0x69, 0x7C, 0x09, 0x0A, 0xBF, 0xEF,
24 0xF3, 0xC5, 0x87, 0x14, 0xFE, 0x64, 0xDE, 0x2E, 0x4B, 0x1A, 0x06, 0x21, 0x6B, 0x66, 0x02, 0xF5, 0x92, 0x8A, 0x0C,
25 0xB3, 0x7E, 0xD0, 0x7A, 0x47, 0x96, 0xE5, 0x26, 0x80, 0xA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0x37, 0xE7, 0x24, 0xA4, 0xCB, 0x53, 0x0A,
48 0x87, 0xD9, 0x4C, 0x83, 0x8F, 0xCE, 0x3B, 0x4A, 0xB7,
49};
50
51/*
52* SEED G Function
53*/
54inline uint32_t SEED_G(uint32_t X) {
55 const uint32_t M = 0x01010101;
56 const uint32_t s0 = M * SEED_S0[get_byte<3>(X)];
57 const uint32_t s1 = M * SEED_S1[get_byte<2>(X)];
58 const uint32_t s2 = M * SEED_S0[get_byte<1>(X)];
59 const uint32_t s3 = M * SEED_S1[get_byte<0>(X)];
60
61 const uint32_t M0 = 0x3FCFF3FC;
62 const uint32_t M1 = 0xFC3FCFF3;
63 const uint32_t M2 = 0xF3FC3FCF;
64 const uint32_t M3 = 0xCFF3FC3F;
65
66 return (s0 & M0) ^ (s1 & M1) ^ (s2 & M2) ^ (s3 & M3);
67}
68
69} // namespace
70
71/*
72* SEED Encryption
73*/
74void SEED::encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
76
77 prefetch_arrays(SEED_S0, SEED_S1);
78
79 for(size_t i = 0; i != blocks; ++i) {
80 uint32_t B0 = load_be<uint32_t>(in, 0);
81 uint32_t B1 = load_be<uint32_t>(in, 1);
82 uint32_t B2 = load_be<uint32_t>(in, 2);
83 uint32_t B3 = load_be<uint32_t>(in, 3);
84
85 for(size_t j = 0; j != 16; j += 2) {
86 uint32_t T0, T1;
87
88 T0 = B2 ^ m_K[2 * j];
89 T1 = SEED_G(B2 ^ B3 ^ m_K[2 * j + 1]);
90 T0 = SEED_G(T1 + T0);
91 T1 = SEED_G(T1 + T0);
92 B1 ^= T1;
93 B0 ^= T0 + T1;
94
95 T0 = B0 ^ m_K[2 * j + 2];
96 T1 = SEED_G(B0 ^ B1 ^ m_K[2 * j + 3]);
97 T0 = SEED_G(T1 + T0);
98 T1 = SEED_G(T1 + T0);
99 B3 ^= T1;
100 B2 ^= T0 + T1;
101 }
102
103 store_be(out, B2, B3, B0, B1);
104
105 in += BLOCK_SIZE;
106 out += BLOCK_SIZE;
107 }
108}
109
110/*
111* SEED Decryption
112*/
113void SEED::decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const {
115
116 prefetch_arrays(SEED_S0, SEED_S1);
117
118 for(size_t i = 0; i != blocks; ++i) {
119 uint32_t B0 = load_be<uint32_t>(in, 0);
120 uint32_t B1 = load_be<uint32_t>(in, 1);
121 uint32_t B2 = load_be<uint32_t>(in, 2);
122 uint32_t B3 = load_be<uint32_t>(in, 3);
123
124 for(size_t j = 0; j != 16; j += 2) {
125 uint32_t T0, T1;
126
127 T0 = B2 ^ m_K[30 - 2 * j];
128 T1 = SEED_G(B2 ^ B3 ^ m_K[31 - 2 * j]);
129 T0 = SEED_G(T1 + T0);
130 T1 = SEED_G(T1 + T0);
131 B1 ^= T1;
132 B0 ^= T0 + T1;
133
134 T0 = B0 ^ m_K[28 - 2 * j];
135 T1 = SEED_G(B0 ^ B1 ^ m_K[29 - 2 * j]);
136 T0 = SEED_G(T1 + T0);
137 T1 = SEED_G(T1 + T0);
138 B3 ^= T1;
139 B2 ^= T0 + T1;
140 }
141
142 store_be(out, B2, B3, B0, B1);
143
144 in += BLOCK_SIZE;
145 out += BLOCK_SIZE;
146 }
147}
148
150 return !m_K.empty();
151}
152
153/*
154* SEED Key Schedule
155*/
156void SEED::key_schedule(std::span<const uint8_t> key) {
157 const uint32_t RC[16] = {0x9E3779B9,
158 0x3C6EF373,
159 0x78DDE6E6,
160 0xF1BBCDCC,
161 0xE3779B99,
162 0xC6EF3733,
163 0x8DDE6E67,
164 0x1BBCDCCF,
165 0x3779B99E,
166 0x6EF3733C,
167 0xDDE6E678,
168 0xBBCDCCF1,
169 0x779B99E3,
170 0xEF3733C6,
171 0xDE6E678D,
172 0xBCDCCF1B};
173
175
176 for(size_t i = 0; i != 4; ++i) {
177 WK[i] = load_be<uint32_t>(key.data(), i);
178 }
179
180 m_K.resize(32);
181
182 for(size_t i = 0; i != 16; i += 2) {
183 m_K[2 * i] = SEED_G(WK[0] + WK[2] - RC[i]);
184 m_K[2 * i + 1] = SEED_G(WK[1] - WK[3] + RC[i]) ^ m_K[2 * i];
185
186 uint32_t T = (WK[0] & 0xFF) << 24;
187 WK[0] = (WK[0] >> 8) | (get_byte<3>(WK[1]) << 24);
188 WK[1] = (WK[1] >> 8) | T;
189
190 m_K[2 * i + 2] = SEED_G(WK[0] + WK[2] - RC[i + 1]);
191 m_K[2 * i + 3] = SEED_G(WK[1] - WK[3] + RC[i + 1]) ^ m_K[2 * i + 2];
192
193 T = get_byte<0>(WK[3]);
194 WK[3] = (WK[3] << 8) | get_byte<0>(WK[2]);
195 WK[2] = (WK[2] << 8) | T;
196 }
197}
198
200 zap(m_K);
201}
202
203} // namespace Botan
void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition seed.cpp:74
bool has_keying_material() const override
Definition seed.cpp:149
void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const override
Definition seed.cpp:113
void clear() override
Definition seed.cpp:199
void assert_key_material_set() const
Definition sym_algo.h:139
FE_25519 X
Definition ge.cpp:25
FE_25519 T
Definition ge.cpp:34
constexpr uint8_t get_byte(T input)
Definition loadstor.h:75
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:117
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
T prefetch_arrays(T(&... arr)[Ns]) noexcept
Definition prefetch.h:34
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:773
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:530