39 inline void SM3::transformBlock(uint32_t* digest, uint8_t* input)
44 for (uint8_t j = 0, i = 0; j < 64; ++j, i += 4)
46 W[j] = (input[i] << 24) | (input[i + 1] << 16) | (input[i + 2] << 8) | input[i + 3];
49 for (uint8_t j = 16; j < 68; ++j)
51 uint32_t wj3 = W[j - 3];
52 uint32_t r15 = rotleft(wj3, 15);
53 uint32_t wj13 = W[j - 13];
54 uint32_t r7 = rotleft(wj13, 7);
55 W[j] = p1(W[j - 16] ^ W[j - 9] ^ r15) ^ r7 ^ W[j - 6];
58 for (uint8_t j = 0; j < 64; ++j)
60 W1[j] = W[j] ^ W[j + 4];
63 uint32_t A = digest[0];
64 uint32_t B = digest[1];
65 uint32_t C = digest[2];
66 uint32_t D = digest[3];
67 uint32_t E = digest[4];
68 uint32_t
F = digest[5];
69 uint32_t
G = digest[6];
70 uint32_t
H = digest[7];
72 for (uint8_t j = 0; j < 16; ++j)
74 uint32_t a12 = rotleft(A, 12);
75 uint32_t s1 = a12 + E + rotleft(T0, j);
76 uint32_t SS1 = rotleft(s1, 7);
77 uint32_t SS2 = SS1 ^ a12;
78 uint32_t TT1 = FF0(A, B, C) + D + SS2 + W1[j];
79 uint32_t TT2 = GG0(E,
F,
G) +
H + SS1 + W[j];
91 for (uint8_t j = 16; j < 64; ++j)
93 uint32_t a12 = rotleft(A, 12);
94 uint32_t s1 = a12 + E + rotleft(T1, (j % 32));
95 uint32_t SS1 = rotleft(s1, 7);
96 uint32_t SS2 = SS1 ^ a12;
97 uint32_t TT1 = FF1(A, B, C) + D + SS2 + W1[j];
98 uint32_t TT2 = GG1(E,
F,
G) +
H + SS1 + W[j];
122 void SM3::finalize(uint8_t* output)
124 uint8_t* local =
reinterpret_cast<uint8_t*
>(workspace);
125 addwc(&processed_high, &processed_low, workspace_used);
127 local[workspace_used++] = 0x80;
129 if (workspace_used > SM3_PAD_SIZE)
131 memset(&local[workspace_used], 0, SM3_BLOCK_SIZE - workspace_used);
132 workspace_used += SM3_BLOCK_SIZE - workspace_used;
134 transformBlock(digest, local);
138 memset(&local[workspace_used], 0, SM3_PAD_SIZE - workspace_used);
140 uint32_t processed_bits_high = swap(processed_high << 3 | ((processed_low >> 29) & 0x07));
141 memcpy(&local[SM3_PAD_SIZE], &processed_bits_high,
sizeof(uint32_t));
143 uint32_t processed_bits_low = swap(processed_low << 3);
144 memcpy(&local[SM3_PAD_SIZE +
sizeof(uint32_t)], &processed_bits_low,
sizeof(uint32_t));
146 transformBlock(digest, local);
148 uint8_t* temp = output;
149 for (uint8_t j = 0; j < (SM3_DIGEST_SIZE /
sizeof(uint32_t)); ++j)
151 *temp++ = (digest[j]) >> 24;
152 *temp++ = (digest[j]) >> 16;
153 *temp++ = (digest[j]) >> 8;
154 *temp++ = (digest[j]);
158 for (uint8_t j = 8; j < (SM3_BLOCK_SIZE /
sizeof(uint32_t)); ++j)
184 digest[0] = 0x7380166F;
185 digest[1] = 0x4914B2B9;
186 digest[2] = 0x172442D7;
187 digest[3] = 0xDA8A0600;
188 digest[4] = 0xA96F30BC;
189 digest[5] = 0x163138AA;
190 digest[6] = 0xE38DEE4D;
191 digest[7] = 0xB0FB0E4E;
193 for (
size_t j = 0; j < (SM3_BLOCK_SIZE /
sizeof(uint32_t)); j++)
207 void SM3::update(
const uint8_t* buf,
size_t len)
209 uint8_t* local =
reinterpret_cast<uint8_t*
>(workspace);
213 uint32_t add = min(len, SM3_BLOCK_SIZE - workspace_used);
215 memcpy(&local[workspace_used], buf, add);
217 workspace_used += add;
221 if(workspace_used == SM3_BLOCK_SIZE)
223 transformBlock(digest, local);
224 addwc(&processed_high, &processed_low, SM3_BLOCK_SIZE);
235 uint8_t* SM3::getValue(uint8_t* buffer)
const
238 sm3.finalize(
static_cast<uint8_t*
>(buffer));
#define F(x, y, z)
Helper function for MD4's computing.
#define G(x, y, z)
Helper function for MD4's computing.
#define H(x, y, z)
Helper function for MD4's computing.
@ SM3
SM3 cryptographic hash function.