Botan 3.6.1
Crypto and TLS for C&
sm3.cpp
Go to the documentation of this file.
1/*
2* SM3
3* (C) 2017 Ribose Inc.
4* (C) 2021 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/sm3.h>
10
11#include <botan/internal/bit_ops.h>
12#include <botan/internal/loadstor.h>
13#include <botan/internal/rotate.h>
14#include <botan/internal/stl_util.h>
15
16namespace Botan {
17
18namespace {
19
20inline uint32_t P0(uint32_t X) {
21 return X ^ rotl<9>(X) ^ rotl<17>(X);
22}
23
24inline void R1(uint32_t A,
25 uint32_t& B,
26 uint32_t C,
27 uint32_t& D,
28 uint32_t E,
29 uint32_t& F,
30 uint32_t G,
31 uint32_t& H,
32 uint32_t TJ,
33 uint32_t Wi,
34 uint32_t Wj) {
35 const uint32_t A12 = rotl<12>(A);
36 const uint32_t SS1 = rotl<7>(A12 + E + TJ);
37 const uint32_t TT1 = (A ^ B ^ C) + D + (SS1 ^ A12) + Wj;
38 const uint32_t TT2 = (E ^ F ^ G) + H + SS1 + Wi;
39
40 B = rotl<9>(B);
41 D = TT1;
42 F = rotl<19>(F);
43 H = P0(TT2);
44}
45
46inline void R2(uint32_t A,
47 uint32_t& B,
48 uint32_t C,
49 uint32_t& D,
50 uint32_t E,
51 uint32_t& F,
52 uint32_t G,
53 uint32_t& H,
54 uint32_t TJ,
55 uint32_t Wi,
56 uint32_t Wj) {
57 const uint32_t A12 = rotl<12>(A);
58 const uint32_t SS1 = rotl<7>(A12 + E + TJ);
59 const uint32_t TT1 = majority(A, B, C) + D + (SS1 ^ A12) + Wj;
60 const uint32_t TT2 = choose(E, F, G) + H + SS1 + Wi;
61
62 B = rotl<9>(B);
63 D = TT1;
64 F = rotl<19>(F);
65 H = P0(TT2);
66}
67
68inline uint32_t P1(uint32_t X) {
69 return X ^ rotl<15>(X) ^ rotl<23>(X);
70}
71
72inline uint32_t SM3_E(uint32_t W0, uint32_t W7, uint32_t W13, uint32_t W3, uint32_t W10) {
73 return P1(W0 ^ W7 ^ rotl<15>(W13)) ^ rotl<7>(W3) ^ W10;
74}
75
76} // namespace
77
78/*
79* SM3 Compression Function
80*/
81void SM3::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
82 uint32_t A = digest[0], B = digest[1], C = digest[2], D = digest[3], E = digest[4], F = digest[5], G = digest[6],
83 H = digest[7];
84 std::array<uint32_t, 16> W;
85
86 BufferSlicer in(input);
87
88 for(size_t i = 0; i != blocks; ++i) {
89 load_be(W, in.take<block_bytes>());
90
91 // clang-format off
92
93 R1(A, B, C, D, E, F, G, H, 0x79CC4519, W[ 0], W[ 0] ^ W[ 4]);
94 W[ 0] = SM3_E(W[ 0], W[ 7], W[13], W[ 3], W[10]);
95 R1(D, A, B, C, H, E, F, G, 0xF3988A32, W[ 1], W[ 1] ^ W[ 5]);
96 W[ 1] = SM3_E(W[ 1], W[ 8], W[14], W[ 4], W[11]);
97 R1(C, D, A, B, G, H, E, F, 0xE7311465, W[ 2], W[ 2] ^ W[ 6]);
98 W[ 2] = SM3_E(W[ 2], W[ 9], W[15], W[ 5], W[12]);
99 R1(B, C, D, A, F, G, H, E, 0xCE6228CB, W[ 3], W[ 3] ^ W[ 7]);
100 W[ 3] = SM3_E(W[ 3], W[10], W[ 0], W[ 6], W[13]);
101 R1(A, B, C, D, E, F, G, H, 0x9CC45197, W[ 4], W[ 4] ^ W[ 8]);
102 W[ 4] = SM3_E(W[ 4], W[11], W[ 1], W[ 7], W[14]);
103 R1(D, A, B, C, H, E, F, G, 0x3988A32F, W[ 5], W[ 5] ^ W[ 9]);
104 W[ 5] = SM3_E(W[ 5], W[12], W[ 2], W[ 8], W[15]);
105 R1(C, D, A, B, G, H, E, F, 0x7311465E, W[ 6], W[ 6] ^ W[10]);
106 W[ 6] = SM3_E(W[ 6], W[13], W[ 3], W[ 9], W[ 0]);
107 R1(B, C, D, A, F, G, H, E, 0xE6228CBC, W[ 7], W[ 7] ^ W[11]);
108 W[ 7] = SM3_E(W[ 7], W[14], W[ 4], W[10], W[ 1]);
109 R1(A, B, C, D, E, F, G, H, 0xCC451979, W[ 8], W[ 8] ^ W[12]);
110 W[ 8] = SM3_E(W[ 8], W[15], W[ 5], W[11], W[ 2]);
111 R1(D, A, B, C, H, E, F, G, 0x988A32F3, W[ 9], W[ 9] ^ W[13]);
112 W[ 9] = SM3_E(W[ 9], W[ 0], W[ 6], W[12], W[ 3]);
113 R1(C, D, A, B, G, H, E, F, 0x311465E7, W[10], W[10] ^ W[14]);
114 W[10] = SM3_E(W[10], W[ 1], W[ 7], W[13], W[ 4]);
115 R1(B, C, D, A, F, G, H, E, 0x6228CBCE, W[11], W[11] ^ W[15]);
116 W[11] = SM3_E(W[11], W[ 2], W[ 8], W[14], W[ 5]);
117 R1(A, B, C, D, E, F, G, H, 0xC451979C, W[12], W[12] ^ W[ 0]);
118 W[12] = SM3_E(W[12], W[ 3], W[ 9], W[15], W[ 6]);
119 R1(D, A, B, C, H, E, F, G, 0x88A32F39, W[13], W[13] ^ W[ 1]);
120 W[13] = SM3_E(W[13], W[ 4], W[10], W[ 0], W[ 7]);
121 R1(C, D, A, B, G, H, E, F, 0x11465E73, W[14], W[14] ^ W[ 2]);
122 W[14] = SM3_E(W[14], W[ 5], W[11], W[ 1], W[ 8]);
123 R1(B, C, D, A, F, G, H, E, 0x228CBCE6, W[15], W[15] ^ W[ 3]);
124 W[15] = SM3_E(W[15], W[ 6], W[12], W[ 2], W[ 9]);
125 R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W[ 0], W[ 0] ^ W[ 4]);
126 W[ 0] = SM3_E(W[ 0], W[ 7], W[13], W[ 3], W[10]);
127 R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W[ 1], W[ 1] ^ W[ 5]);
128 W[ 1] = SM3_E(W[ 1], W[ 8], W[14], W[ 4], W[11]);
129 R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W[ 2], W[ 2] ^ W[ 6]);
130 W[ 2] = SM3_E(W[ 2], W[ 9], W[15], W[ 5], W[12]);
131 R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W[ 3], W[ 3] ^ W[ 7]);
132 W[ 3] = SM3_E(W[ 3], W[10], W[ 0], W[ 6], W[13]);
133 R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W[ 4], W[ 4] ^ W[ 8]);
134 W[ 4] = SM3_E(W[ 4], W[11], W[ 1], W[ 7], W[14]);
135 R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W[ 5], W[ 5] ^ W[ 9]);
136 W[ 5] = SM3_E(W[ 5], W[12], W[ 2], W[ 8], W[15]);
137 R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W[ 6], W[ 6] ^ W[10]);
138 W[ 6] = SM3_E(W[ 6], W[13], W[ 3], W[ 9], W[ 0]);
139 R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W[ 7], W[ 7] ^ W[11]);
140 W[ 7] = SM3_E(W[ 7], W[14], W[ 4], W[10], W[ 1]);
141 R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W[ 8], W[ 8] ^ W[12]);
142 W[ 8] = SM3_E(W[ 8], W[15], W[ 5], W[11], W[ 2]);
143 R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W[ 9], W[ 9] ^ W[13]);
144 W[ 9] = SM3_E(W[ 9], W[ 0], W[ 6], W[12], W[ 3]);
145 R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W[10], W[10] ^ W[14]);
146 W[10] = SM3_E(W[10], W[ 1], W[ 7], W[13], W[ 4]);
147 R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W[11], W[11] ^ W[15]);
148 W[11] = SM3_E(W[11], W[ 2], W[ 8], W[14], W[ 5]);
149 R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W[12], W[12] ^ W[ 0]);
150 W[12] = SM3_E(W[12], W[ 3], W[ 9], W[15], W[ 6]);
151 R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W[13], W[13] ^ W[ 1]);
152 W[13] = SM3_E(W[13], W[ 4], W[10], W[ 0], W[ 7]);
153 R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W[14], W[14] ^ W[ 2]);
154 W[14] = SM3_E(W[14], W[ 5], W[11], W[ 1], W[ 8]);
155 R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W[15], W[15] ^ W[ 3]);
156 W[15] = SM3_E(W[15], W[ 6], W[12], W[ 2], W[ 9]);
157 R2(A, B, C, D, E, F, G, H, 0x7A879D8A, W[ 0], W[ 0] ^ W[ 4]);
158 W[ 0] = SM3_E(W[ 0], W[ 7], W[13], W[ 3], W[10]);
159 R2(D, A, B, C, H, E, F, G, 0xF50F3B14, W[ 1], W[ 1] ^ W[ 5]);
160 W[ 1] = SM3_E(W[ 1], W[ 8], W[14], W[ 4], W[11]);
161 R2(C, D, A, B, G, H, E, F, 0xEA1E7629, W[ 2], W[ 2] ^ W[ 6]);
162 W[ 2] = SM3_E(W[ 2], W[ 9], W[15], W[ 5], W[12]);
163 R2(B, C, D, A, F, G, H, E, 0xD43CEC53, W[ 3], W[ 3] ^ W[ 7]);
164 W[ 3] = SM3_E(W[ 3], W[10], W[ 0], W[ 6], W[13]);
165 R2(A, B, C, D, E, F, G, H, 0xA879D8A7, W[ 4], W[ 4] ^ W[ 8]);
166 W[ 4] = SM3_E(W[ 4], W[11], W[ 1], W[ 7], W[14]);
167 R2(D, A, B, C, H, E, F, G, 0x50F3B14F, W[ 5], W[ 5] ^ W[ 9]);
168 W[ 5] = SM3_E(W[ 5], W[12], W[ 2], W[ 8], W[15]);
169 R2(C, D, A, B, G, H, E, F, 0xA1E7629E, W[ 6], W[ 6] ^ W[10]);
170 W[ 6] = SM3_E(W[ 6], W[13], W[ 3], W[ 9], W[ 0]);
171 R2(B, C, D, A, F, G, H, E, 0x43CEC53D, W[ 7], W[ 7] ^ W[11]);
172 W[ 7] = SM3_E(W[ 7], W[14], W[ 4], W[10], W[ 1]);
173 R2(A, B, C, D, E, F, G, H, 0x879D8A7A, W[ 8], W[ 8] ^ W[12]);
174 W[ 8] = SM3_E(W[ 8], W[15], W[ 5], W[11], W[ 2]);
175 R2(D, A, B, C, H, E, F, G, 0x0F3B14F5, W[ 9], W[ 9] ^ W[13]);
176 W[ 9] = SM3_E(W[ 9], W[ 0], W[ 6], W[12], W[ 3]);
177 R2(C, D, A, B, G, H, E, F, 0x1E7629EA, W[10], W[10] ^ W[14]);
178 W[10] = SM3_E(W[10], W[ 1], W[ 7], W[13], W[ 4]);
179 R2(B, C, D, A, F, G, H, E, 0x3CEC53D4, W[11], W[11] ^ W[15]);
180 W[11] = SM3_E(W[11], W[ 2], W[ 8], W[14], W[ 5]);
181 R2(A, B, C, D, E, F, G, H, 0x79D8A7A8, W[12], W[12] ^ W[ 0]);
182 W[12] = SM3_E(W[12], W[ 3], W[ 9], W[15], W[ 6]);
183 R2(D, A, B, C, H, E, F, G, 0xF3B14F50, W[13], W[13] ^ W[ 1]);
184 W[13] = SM3_E(W[13], W[ 4], W[10], W[ 0], W[ 7]);
185 R2(C, D, A, B, G, H, E, F, 0xE7629EA1, W[14], W[14] ^ W[ 2]);
186 W[14] = SM3_E(W[14], W[ 5], W[11], W[ 1], W[ 8]);
187 R2(B, C, D, A, F, G, H, E, 0xCEC53D43, W[15], W[15] ^ W[ 3]);
188 W[15] = SM3_E(W[15], W[ 6], W[12], W[ 2], W[ 9]);
189 R2(A, B, C, D, E, F, G, H, 0x9D8A7A87, W[ 0], W[ 0] ^ W[ 4]);
190 W[ 0] = SM3_E(W[ 0], W[ 7], W[13], W[ 3], W[10]);
191 R2(D, A, B, C, H, E, F, G, 0x3B14F50F, W[ 1], W[ 1] ^ W[ 5]);
192 W[ 1] = SM3_E(W[ 1], W[ 8], W[14], W[ 4], W[11]);
193 R2(C, D, A, B, G, H, E, F, 0x7629EA1E, W[ 2], W[ 2] ^ W[ 6]);
194 W[ 2] = SM3_E(W[ 2], W[ 9], W[15], W[ 5], W[12]);
195 R2(B, C, D, A, F, G, H, E, 0xEC53D43C, W[ 3], W[ 3] ^ W[ 7]);
196 W[ 3] = SM3_E(W[ 3], W[10], W[ 0], W[ 6], W[13]);
197 R2(A, B, C, D, E, F, G, H, 0xD8A7A879, W[ 4], W[ 4] ^ W[ 8]);
198 R2(D, A, B, C, H, E, F, G, 0xB14F50F3, W[ 5], W[ 5] ^ W[ 9]);
199 R2(C, D, A, B, G, H, E, F, 0x629EA1E7, W[ 6], W[ 6] ^ W[10]);
200 R2(B, C, D, A, F, G, H, E, 0xC53D43CE, W[ 7], W[ 7] ^ W[11]);
201 R2(A, B, C, D, E, F, G, H, 0x8A7A879D, W[ 8], W[ 8] ^ W[12]);
202 R2(D, A, B, C, H, E, F, G, 0x14F50F3B, W[ 9], W[ 9] ^ W[13]);
203 R2(C, D, A, B, G, H, E, F, 0x29EA1E76, W[10], W[10] ^ W[14]);
204 R2(B, C, D, A, F, G, H, E, 0x53D43CEC, W[11], W[11] ^ W[15]);
205 R2(A, B, C, D, E, F, G, H, 0xA7A879D8, W[12], W[12] ^ W[ 0]);
206 R2(D, A, B, C, H, E, F, G, 0x4F50F3B1, W[13], W[13] ^ W[ 1]);
207 R2(C, D, A, B, G, H, E, F, 0x9EA1E762, W[14], W[14] ^ W[ 2]);
208 R2(B, C, D, A, F, G, H, E, 0x3D43CEC5, W[15], W[15] ^ W[ 3]);
209
210 // clang-format on
211
212 A = (digest[0] ^= A);
213 B = (digest[1] ^= B);
214 C = (digest[2] ^= C);
215 D = (digest[3] ^= D);
216 E = (digest[4] ^= E);
217 F = (digest[5] ^= F);
218 G = (digest[6] ^= G);
219 H = (digest[7] ^= H);
220 }
221}
222
223void SM3::init(digest_type& digest) {
224 digest.assign(
225 {0x7380166fUL, 0x4914b2b9UL, 0x172442d7UL, 0xda8a0600UL, 0xa96f30bcUL, 0x163138aaUL, 0xe38dee4dUL, 0xb0fb0e4eUL});
226}
227
228std::unique_ptr<HashFunction> SM3::new_object() const {
229 return std::make_unique<SM3>();
230}
231
232std::unique_ptr<HashFunction> SM3::copy_state() const {
233 return std::make_unique<SM3>(*this);
234}
235
236void SM3::add_data(std::span<const uint8_t> input) {
237 m_md.update(input);
238}
239
240void SM3::final_result(std::span<uint8_t> output) {
241 m_md.final(output);
242}
243
244} // namespace Botan
std::span< const uint8_t > take(const size_t count)
Definition stl_util.h:98
std::unique_ptr< HashFunction > copy_state() const override
Definition sm3.cpp:232
std::unique_ptr< HashFunction > new_object() const override
Definition sm3.cpp:228
secure_vector< uint32_t > digest_type
Definition sm3.h:20
static constexpr size_t block_bytes
Definition sm3.h:24
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sm3.cpp:81
static void init(digest_type &digest)
Definition sm3.cpp:223
FE_25519 X
Definition ge.cpp:25
constexpr T rotl(T input)
Definition rotate.h:21
constexpr T choose(T mask, T a, T b)
Definition bit_ops.h:193
constexpr T majority(T a, T b, T c)
Definition bit_ops.h:199
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:530