| 1 |
/*- |
| 2 |
* Copyright 2005-2016 Colin Percival |
| 3 |
* Copyright 2016-2018,2021 Alexander Peslyak |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in the |
| 13 |
* documentation and/or other materials provided with the distribution. |
| 14 |
* |
| 15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 25 |
* SUCH DAMAGE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "crypt-port.h" |
| 29 |
|
| 30 |
#if INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || \ |
| 31 |
INCLUDE_sha256crypt || INCLUDE_sm3_yescrypt |
| 32 |
|
| 33 |
#include "alg-sha256.h" |
| 34 |
#include "byteorder.h" |
| 35 |
|
| 36 |
#ifdef __ICC |
| 37 |
/* Miscompile with icc 14.0.0 (at least), so don't use restrict there */ |
| 38 |
#define restrict |
| 39 |
#elif __STDC_VERSION__ >= 199901L |
| 40 |
/* Have restrict */ |
| 41 |
#elif defined(__GNUC__) |
| 42 |
#define restrict __restrict |
| 43 |
#else |
| 44 |
#define restrict |
| 45 |
#endif |
| 46 |
|
| 47 |
/* SHA256 round constants. */ |
| 48 |
static const uint32_t Krnd[64] = { |
| 49 |
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, |
| 50 |
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, |
| 51 |
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, |
| 52 |
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, |
| 53 |
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, |
| 54 |
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, |
| 55 |
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, |
| 56 |
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, |
| 57 |
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, |
| 58 |
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, |
| 59 |
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, |
| 60 |
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, |
| 61 |
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, |
| 62 |
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, |
| 63 |
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, |
| 64 |
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 |
| 65 |
}; |
| 66 |
|
| 67 |
/* Elementary functions used by SHA256 */ |
| 68 |
#define Ch(x, y, z) ((x & (y ^ z)) ^ z) |
| 69 |
#if 1 /* Explicit caching/reuse of common subexpression between rounds */ |
| 70 |
#define Maj(x, y, z) (y ^ ((x_xor_y = x ^ y) & y_xor_z)) |
| 71 |
#else /* Let the compiler cache/reuse or not */ |
| 72 |
#define Maj(x, y, z) (y ^ ((x ^ y) & (y ^ z))) |
| 73 |
#endif |
| 74 |
#define SHR(x, n) (x >> n) |
| 75 |
#define ROTR(x, n) ((x >> n) | (x << (32 - n))) |
| 76 |
#define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22)) |
| 77 |
#define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25)) |
| 78 |
#define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3)) |
| 79 |
#define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10)) |
| 80 |
|
| 81 |
/* SHA256 round function */ |
| 82 |
#define RND(a, b, c, d, e, f, g, h, k) \ |
| 83 |
h += S1(e) + Ch(e, f, g) + k; \ |
| 84 |
d += h; \ |
| 85 |
h += S0(a) + Maj(a, b, c); \ |
| 86 |
y_xor_z = x_xor_y; |
| 87 |
|
| 88 |
/* Adjusted round function for rotating state */ |
| 89 |
#define RNDr(S, W, i, ii) \ |
| 90 |
RND(S[(64 - i) % 8], S[(65 - i) % 8], \ |
| 91 |
S[(66 - i) % 8], S[(67 - i) % 8], \ |
| 92 |
S[(68 - i) % 8], S[(69 - i) % 8], \ |
| 93 |
S[(70 - i) % 8], S[(71 - i) % 8], \ |
| 94 |
W[i + ii] + Krnd[i + ii]) |
| 95 |
|
| 96 |
/* Message schedule computation */ |
| 97 |
#define MSCH(W, ii, i) \ |
| 98 |
W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii] |
| 99 |
|
| 100 |
/* |
| 101 |
* SHA256 block compression function. The 256-bit state is transformed via |
| 102 |
* the 512-bit input block to produce a new state. |
| 103 |
*/ |
| 104 |
static void |
| 105 |
SHA256_Transform(uint32_t state[static restrict 8], |
| 106 |
const uint8_t block[static restrict 64], |
| 107 |
uint32_t W[static restrict 64], uint32_t S[static restrict 8]) |
| 108 |
{ |
| 109 |
int i; |
| 110 |
|
| 111 |
/* 1. Prepare the first part of the message schedule W. */ |
| 112 |
be32dec_vect(W, block, 16); |
| 113 |
|
| 114 |
/* 2. Initialize working variables. */ |
| 115 |
memcpy(S, state, 32); |
| 116 |
|
| 117 |
/* 3. Mix. */ |
| 118 |
for (i = 0; i <= 48; i += 16) { |
| 119 |
uint32_t x_xor_y, y_xor_z = S[(65 - i) % 8] ^ S[(66 - i) % 8]; |
| 120 |
RNDr(S, W, 0, i); |
| 121 |
RNDr(S, W, 1, i); |
| 122 |
RNDr(S, W, 2, i); |
| 123 |
RNDr(S, W, 3, i); |
| 124 |
RNDr(S, W, 4, i); |
| 125 |
RNDr(S, W, 5, i); |
| 126 |
RNDr(S, W, 6, i); |
| 127 |
RNDr(S, W, 7, i); |
| 128 |
RNDr(S, W, 8, i); |
| 129 |
RNDr(S, W, 9, i); |
| 130 |
RNDr(S, W, 10, i); |
| 131 |
RNDr(S, W, 11, i); |
| 132 |
RNDr(S, W, 12, i); |
| 133 |
RNDr(S, W, 13, i); |
| 134 |
RNDr(S, W, 14, i); |
| 135 |
RNDr(S, W, 15, i); |
| 136 |
|
| 137 |
if (i == 48) |
| 138 |
break; |
| 139 |
|
| 140 |
MSCH(W, 0, i); |
| 141 |
MSCH(W, 1, i); |
| 142 |
MSCH(W, 2, i); |
| 143 |
MSCH(W, 3, i); |
| 144 |
MSCH(W, 4, i); |
| 145 |
MSCH(W, 5, i); |
| 146 |
MSCH(W, 6, i); |
| 147 |
MSCH(W, 7, i); |
| 148 |
MSCH(W, 8, i); |
| 149 |
MSCH(W, 9, i); |
| 150 |
MSCH(W, 10, i); |
| 151 |
MSCH(W, 11, i); |
| 152 |
MSCH(W, 12, i); |
| 153 |
MSCH(W, 13, i); |
| 154 |
MSCH(W, 14, i); |
| 155 |
MSCH(W, 15, i); |
| 156 |
} |
| 157 |
|
| 158 |
/* 4. Mix local working variables into global state. */ |
| 159 |
state[0] += S[0]; |
| 160 |
state[1] += S[1]; |
| 161 |
state[2] += S[2]; |
| 162 |
state[3] += S[3]; |
| 163 |
state[4] += S[4]; |
| 164 |
state[5] += S[5]; |
| 165 |
state[6] += S[6]; |
| 166 |
state[7] += S[7]; |
| 167 |
} |
| 168 |
|
| 169 |
static const uint8_t PAD[64] = { |
| 170 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 171 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 172 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 173 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 174 |
}; |
| 175 |
|
| 176 |
/* Add padding and terminating bit-count. */ |
| 177 |
static void |
| 178 |
SHA256_Pad(SHA256_CTX * ctx, uint32_t tmp32[static restrict 72]) |
| 179 |
{ |
| 180 |
size_t r; |
| 181 |
|
| 182 |
/* Figure out how many bytes we have buffered. */ |
| 183 |
r = (ctx->count >> 3) & 0x3f; |
| 184 |
|
| 185 |
/* Pad to 56 mod 64, transforming if we finish a block en route. */ |
| 186 |
if (r < 56) { |
| 187 |
/* Pad to 56 mod 64. */ |
| 188 |
memcpy(&ctx->buf[r], PAD, 56 - r); |
| 189 |
} else { |
| 190 |
/* Finish the current block and mix. */ |
| 191 |
memcpy(&ctx->buf[r], PAD, 64 - r); |
| 192 |
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]); |
| 193 |
|
| 194 |
/* The start of the final block is all zeroes. */ |
| 195 |
memset(&ctx->buf[0], 0, 56); |
| 196 |
} |
| 197 |
|
| 198 |
/* Add the terminating bit-count. */ |
| 199 |
be64enc(&ctx->buf[56], ctx->count); |
| 200 |
|
| 201 |
/* Mix in the final block. */ |
| 202 |
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]); |
| 203 |
} |
| 204 |
|
| 205 |
/* Magic initialization constants. */ |
| 206 |
static const uint32_t initial_state[8] = { |
| 207 |
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, |
| 208 |
0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 |
| 209 |
}; |
| 210 |
|
| 211 |
/** |
| 212 |
* SHA256_Init(ctx): |
| 213 |
* Initialize the SHA256 context ${ctx}. |
| 214 |
*/ |
| 215 |
void |
| 216 |
SHA256_Init(SHA256_CTX * ctx) |
| 217 |
{ |
| 218 |
|
| 219 |
/* Zero bits processed so far. */ |
| 220 |
ctx->count = 0; |
| 221 |
|
| 222 |
/* Initialize state. */ |
| 223 |
memcpy(ctx->state, initial_state, sizeof(initial_state)); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* SHA256_Update(ctx, in, len): |
| 228 |
* Input ${len} bytes from ${in} into the SHA256 context ${ctx}. |
| 229 |
*/ |
| 230 |
static void |
| 231 |
_SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len, |
| 232 |
uint32_t tmp32[static restrict 72]) |
| 233 |
{ |
| 234 |
uint32_t r; |
| 235 |
const uint8_t * src = in; |
| 236 |
|
| 237 |
/* Return immediately if we have nothing to do. */ |
| 238 |
if (len == 0) |
| 239 |
return; |
| 240 |
|
| 241 |
/* Number of bytes left in the buffer from previous updates. */ |
| 242 |
r = (ctx->count >> 3) & 0x3f; |
| 243 |
|
| 244 |
/* Update number of bits. */ |
| 245 |
ctx->count += (uint64_t)(len) << 3; |
| 246 |
|
| 247 |
/* Handle the case where we don't need to perform any transforms. */ |
| 248 |
if (len < 64 - r) { |
| 249 |
memcpy(&ctx->buf[r], src, len); |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
/* Finish the current block. */ |
| 254 |
memcpy(&ctx->buf[r], src, 64 - r); |
| 255 |
SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]); |
| 256 |
src += 64 - r; |
| 257 |
len -= 64 - r; |
| 258 |
|
| 259 |
/* Perform complete blocks. */ |
| 260 |
while (len >= 64) { |
| 261 |
SHA256_Transform(ctx->state, src, &tmp32[0], &tmp32[64]); |
| 262 |
src += 64; |
| 263 |
len -= 64; |
| 264 |
} |
| 265 |
|
| 266 |
/* Copy left over data into buffer. */ |
| 267 |
memcpy(ctx->buf, src, len); |
| 268 |
} |
| 269 |
|
| 270 |
/* Wrapper function for intermediate-values sanitization. */ |
| 271 |
void |
| 272 |
SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len) |
| 273 |
{ |
| 274 |
uint32_t tmp32[72]; |
| 275 |
|
| 276 |
/* Call the real function. */ |
| 277 |
_SHA256_Update(ctx, in, len, tmp32); |
| 278 |
|
| 279 |
/* Clean the stack. */ |
| 280 |
explicit_bzero(tmp32, 288); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* SHA256_Final(digest, ctx): |
| 285 |
* Output the SHA256 hash of the data input to the context ${ctx} into the |
| 286 |
* buffer ${digest}. |
| 287 |
*/ |
| 288 |
static void |
| 289 |
_SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx, |
| 290 |
uint32_t tmp32[static restrict 72]) |
| 291 |
{ |
| 292 |
|
| 293 |
/* Add padding. */ |
| 294 |
SHA256_Pad(ctx, tmp32); |
| 295 |
|
| 296 |
/* Write the hash. */ |
| 297 |
be32enc_vect(digest, ctx->state, 8); |
| 298 |
} |
| 299 |
|
| 300 |
/* Wrapper function for intermediate-values sanitization. */ |
| 301 |
void |
| 302 |
SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx) |
| 303 |
{ |
| 304 |
uint32_t tmp32[72]; |
| 305 |
|
| 306 |
/* Call the real function. */ |
| 307 |
_SHA256_Final(digest, ctx, tmp32); |
| 308 |
|
| 309 |
/* Clear the context state. */ |
| 310 |
explicit_bzero(ctx, sizeof(SHA256_CTX)); |
| 311 |
|
| 312 |
/* Clean the stack. */ |
| 313 |
explicit_bzero(tmp32, 288); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* SHA256_Buf(in, len, digest): |
| 318 |
* Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}. |
| 319 |
*/ |
| 320 |
void |
| 321 |
SHA256_Buf(const void * in, size_t len, uint8_t digest[32]) |
| 322 |
{ |
| 323 |
SHA256_CTX ctx; |
| 324 |
uint32_t tmp32[72]; |
| 325 |
|
| 326 |
SHA256_Init(&ctx); |
| 327 |
_SHA256_Update(&ctx, in, len, tmp32); |
| 328 |
_SHA256_Final(digest, &ctx, tmp32); |
| 329 |
|
| 330 |
/* Clean the stack. */ |
| 331 |
explicit_bzero(&ctx, sizeof(SHA256_CTX)); |
| 332 |
explicit_bzero(tmp32, 288); |
| 333 |
} |
| 334 |
|
| 335 |
#endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || |
| 336 |
INCLUDE_sha256crypt || INCLUDE_sm3_yescrypt */ |
| 337 |
|
| 338 |
#if INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || \ |
| 339 |
INCLUDE_sm3_yescrypt |
| 340 |
|
| 341 |
/** |
| 342 |
* HMAC_SHA256_Init(ctx, K, Klen): |
| 343 |
* Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from |
| 344 |
* ${K}. |
| 345 |
*/ |
| 346 |
static void |
| 347 |
_HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen, |
| 348 |
uint32_t tmp32[static restrict 72], uint8_t pad[static restrict 64], |
| 349 |
uint8_t khash[static restrict 32]) |
| 350 |
{ |
| 351 |
const uint8_t * K = _K; |
| 352 |
size_t i; |
| 353 |
|
| 354 |
/* If Klen > 64, the key is really SHA256(K). */ |
| 355 |
if (Klen > 64) { |
| 356 |
SHA256_Init(&ctx->ictx); |
| 357 |
_SHA256_Update(&ctx->ictx, K, Klen, tmp32); |
| 358 |
_SHA256_Final(khash, &ctx->ictx, tmp32); |
| 359 |
K = khash; |
| 360 |
Klen = 32; |
| 361 |
} |
| 362 |
|
| 363 |
/* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */ |
| 364 |
SHA256_Init(&ctx->ictx); |
| 365 |
memset(pad, 0x36, 64); |
| 366 |
for (i = 0; i < Klen; i++) |
| 367 |
pad[i] ^= K[i]; |
| 368 |
_SHA256_Update(&ctx->ictx, pad, 64, tmp32); |
| 369 |
|
| 370 |
/* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */ |
| 371 |
SHA256_Init(&ctx->octx); |
| 372 |
memset(pad, 0x5c, 64); |
| 373 |
for (i = 0; i < Klen; i++) |
| 374 |
pad[i] ^= K[i]; |
| 375 |
_SHA256_Update(&ctx->octx, pad, 64, tmp32); |
| 376 |
} |
| 377 |
|
| 378 |
/* Wrapper function for intermediate-values sanitization. */ |
| 379 |
void |
| 380 |
HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * _K, size_t Klen) |
| 381 |
{ |
| 382 |
uint32_t tmp32[72]; |
| 383 |
uint8_t pad[64]; |
| 384 |
uint8_t khash[32]; |
| 385 |
|
| 386 |
/* Call the real function. */ |
| 387 |
_HMAC_SHA256_Init(ctx, _K, Klen, tmp32, pad, khash); |
| 388 |
|
| 389 |
/* Clean the stack. */ |
| 390 |
explicit_bzero(tmp32, 288); |
| 391 |
explicit_bzero(khash, 32); |
| 392 |
explicit_bzero(pad, 64); |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* HMAC_SHA256_Update(ctx, in, len): |
| 397 |
* Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}. |
| 398 |
*/ |
| 399 |
static void |
| 400 |
_HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len, |
| 401 |
uint32_t tmp32[static restrict 72]) |
| 402 |
{ |
| 403 |
|
| 404 |
/* Feed data to the inner SHA256 operation. */ |
| 405 |
_SHA256_Update(&ctx->ictx, in, len, tmp32); |
| 406 |
} |
| 407 |
|
| 408 |
/* Wrapper function for intermediate-values sanitization. */ |
| 409 |
void |
| 410 |
HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len) |
| 411 |
{ |
| 412 |
uint32_t tmp32[72]; |
| 413 |
|
| 414 |
/* Call the real function. */ |
| 415 |
_HMAC_SHA256_Update(ctx, in, len, tmp32); |
| 416 |
|
| 417 |
/* Clean the stack. */ |
| 418 |
explicit_bzero(tmp32, 288); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* HMAC_SHA256_Final(digest, ctx): |
| 423 |
* Output the HMAC-SHA256 of the data input to the context ${ctx} into the |
| 424 |
* buffer ${digest}. |
| 425 |
*/ |
| 426 |
static void |
| 427 |
_HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx, |
| 428 |
uint32_t tmp32[static restrict 72], uint8_t ihash[static restrict 32]) |
| 429 |
{ |
| 430 |
|
| 431 |
/* Finish the inner SHA256 operation. */ |
| 432 |
_SHA256_Final(ihash, &ctx->ictx, tmp32); |
| 433 |
|
| 434 |
/* Feed the inner hash to the outer SHA256 operation. */ |
| 435 |
_SHA256_Update(&ctx->octx, ihash, 32, tmp32); |
| 436 |
|
| 437 |
/* Finish the outer SHA256 operation. */ |
| 438 |
_SHA256_Final(digest, &ctx->octx, tmp32); |
| 439 |
} |
| 440 |
|
| 441 |
/* Wrapper function for intermediate-values sanitization. */ |
| 442 |
void |
| 443 |
HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx) |
| 444 |
{ |
| 445 |
uint32_t tmp32[72]; |
| 446 |
uint8_t ihash[32]; |
| 447 |
|
| 448 |
/* Call the real function. */ |
| 449 |
_HMAC_SHA256_Final(digest, ctx, tmp32, ihash); |
| 450 |
|
| 451 |
/* Clear the context state. */ |
| 452 |
explicit_bzero(ctx, sizeof(HMAC_SHA256_CTX)); |
| 453 |
|
| 454 |
/* Clean the stack. */ |
| 455 |
explicit_bzero(tmp32, 288); |
| 456 |
explicit_bzero(ihash, 32); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* HMAC_SHA256_Buf(K, Klen, in, len, digest): |
| 461 |
* Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of |
| 462 |
* length ${Klen}, and write the result to ${digest}. |
| 463 |
*/ |
| 464 |
void |
| 465 |
HMAC_SHA256_Buf(const void * K, size_t Klen, const void * in, size_t len, |
| 466 |
uint8_t digest[32]) |
| 467 |
{ |
| 468 |
HMAC_SHA256_CTX ctx; |
| 469 |
uint32_t tmp32[72]; |
| 470 |
uint8_t tmp8[96]; |
| 471 |
|
| 472 |
_HMAC_SHA256_Init(&ctx, K, Klen, tmp32, &tmp8[0], &tmp8[64]); |
| 473 |
_HMAC_SHA256_Update(&ctx, in, len, tmp32); |
| 474 |
_HMAC_SHA256_Final(digest, &ctx, tmp32, &tmp8[0]); |
| 475 |
|
| 476 |
/* Clean the stack. */ |
| 477 |
explicit_bzero(&ctx, sizeof(HMAC_SHA256_CTX)); |
| 478 |
explicit_bzero(tmp32, 288); |
| 479 |
explicit_bzero(tmp8, 96); |
| 480 |
} |
| 481 |
|
| 482 |
/* Add padding and terminating bit-count, but don't invoke Transform yet. */ |
| 483 |
static int |
| 484 |
SHA256_Pad_Almost(SHA256_CTX * ctx, uint8_t len[static restrict 8], |
| 485 |
uint32_t tmp32[static restrict 72]) |
| 486 |
{ |
| 487 |
uint32_t r; |
| 488 |
|
| 489 |
r = (ctx->count >> 3) & 0x3f; |
| 490 |
if (r >= 56) |
| 491 |
return -1; |
| 492 |
|
| 493 |
/* |
| 494 |
* Convert length to a vector of bytes -- we do this now rather |
| 495 |
* than later because the length will change after we pad. |
| 496 |
*/ |
| 497 |
be64enc(len, ctx->count); |
| 498 |
|
| 499 |
/* Add 1--56 bytes so that the resulting length is 56 mod 64. */ |
| 500 |
_SHA256_Update(ctx, PAD, 56 - r, tmp32); |
| 501 |
|
| 502 |
/* Add the terminating bit-count. */ |
| 503 |
ctx->buf[63] = len[7]; |
| 504 |
_SHA256_Update(ctx, len, 7, tmp32); |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): |
| 511 |
* Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and |
| 512 |
* write the output to buf. The value dkLen must be at most 32 * (2^32 - 1). |
| 513 |
*/ |
| 514 |
void |
| 515 |
PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, |
| 516 |
size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen) |
| 517 |
{ |
| 518 |
HMAC_SHA256_CTX Phctx, PShctx, hctx; |
| 519 |
uint32_t tmp32[72]; |
| 520 |
union { |
| 521 |
uint8_t tmp8[96]; |
| 522 |
uint32_t state[8]; |
| 523 |
} u; |
| 524 |
size_t i; |
| 525 |
uint8_t ivec[4]; |
| 526 |
uint8_t U[32]; |
| 527 |
uint8_t T[32]; |
| 528 |
uint64_t j; |
| 529 |
int k; |
| 530 |
size_t clen; |
| 531 |
|
| 532 |
/* Sanity-check. */ |
| 533 |
assert(dkLen <= 32 * (size_t)(UINT32_MAX)); |
| 534 |
|
| 535 |
if (c == 1 && (dkLen & 31) == 0 && (saltlen & 63) <= 51) { |
| 536 |
uint32_t oldcount; |
| 537 |
uint8_t * ivecp; |
| 538 |
|
| 539 |
/* Compute HMAC state after processing P and S. */ |
| 540 |
_HMAC_SHA256_Init(&hctx, passwd, passwdlen, |
| 541 |
tmp32, &u.tmp8[0], &u.tmp8[64]); |
| 542 |
_HMAC_SHA256_Update(&hctx, salt, saltlen, tmp32); |
| 543 |
|
| 544 |
/* Prepare ictx padding. */ |
| 545 |
oldcount = hctx.ictx.count & (0x3f << 3); |
| 546 |
_HMAC_SHA256_Update(&hctx, "\0\0\0", 4, tmp32); |
| 547 |
if ((hctx.ictx.count & (0x3f << 3)) < oldcount || |
| 548 |
SHA256_Pad_Almost(&hctx.ictx, u.tmp8, tmp32)) |
| 549 |
goto generic; /* Can't happen due to saltlen check */ |
| 550 |
ivecp = hctx.ictx.buf + (oldcount >> 3); |
| 551 |
|
| 552 |
/* Prepare octx padding. */ |
| 553 |
hctx.octx.count += 32 << 3; |
| 554 |
SHA256_Pad_Almost(&hctx.octx, u.tmp8, tmp32); |
| 555 |
|
| 556 |
/* Iterate through the blocks. */ |
| 557 |
for (i = 0; i * 32 < dkLen; i++) { |
| 558 |
/* Generate INT(i + 1). */ |
| 559 |
be32enc(ivecp, (uint32_t)(i + 1)); |
| 560 |
|
| 561 |
/* Compute U_1 = PRF(P, S || INT(i)). */ |
| 562 |
memcpy(u.state, hctx.ictx.state, sizeof(u.state)); |
| 563 |
SHA256_Transform(u.state, hctx.ictx.buf, |
| 564 |
&tmp32[0], &tmp32[64]); |
| 565 |
be32enc_vect(hctx.octx.buf, u.state, 8); |
| 566 |
memcpy(u.state, hctx.octx.state, sizeof(u.state)); |
| 567 |
SHA256_Transform(u.state, hctx.octx.buf, |
| 568 |
&tmp32[0], &tmp32[64]); |
| 569 |
be32enc_vect(&buf[i * 32], u.state, 8); |
| 570 |
} |
| 571 |
|
| 572 |
goto cleanup; |
| 573 |
} |
| 574 |
|
| 575 |
generic: |
| 576 |
/* Compute HMAC state after processing P. */ |
| 577 |
_HMAC_SHA256_Init(&Phctx, passwd, passwdlen, |
| 578 |
tmp32, &u.tmp8[0], &u.tmp8[64]); |
| 579 |
|
| 580 |
/* Compute HMAC state after processing P and S. */ |
| 581 |
memcpy(&PShctx, &Phctx, sizeof(HMAC_SHA256_CTX)); |
| 582 |
_HMAC_SHA256_Update(&PShctx, salt, saltlen, tmp32); |
| 583 |
|
| 584 |
/* Iterate through the blocks. */ |
| 585 |
for (i = 0; i * 32 < dkLen; i++) { |
| 586 |
/* Generate INT(i + 1). */ |
| 587 |
be32enc(ivec, (uint32_t)(i + 1)); |
| 588 |
|
| 589 |
/* Compute U_1 = PRF(P, S || INT(i)). */ |
| 590 |
memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX)); |
| 591 |
_HMAC_SHA256_Update(&hctx, ivec, 4, tmp32); |
| 592 |
_HMAC_SHA256_Final(T, &hctx, tmp32, u.tmp8); |
| 593 |
|
| 594 |
if (c > 1) { |
| 595 |
/* T_i = U_1 ... */ |
| 596 |
memcpy(U, T, 32); |
| 597 |
|
| 598 |
for (j = 2; j <= c; j++) { |
| 599 |
/* Compute U_j. */ |
| 600 |
memcpy(&hctx, &Phctx, sizeof(HMAC_SHA256_CTX)); |
| 601 |
_HMAC_SHA256_Update(&hctx, U, 32, tmp32); |
| 602 |
_HMAC_SHA256_Final(U, &hctx, tmp32, u.tmp8); |
| 603 |
|
| 604 |
/* ... xor U_j ... */ |
| 605 |
for (k = 0; k < 32; k++) |
| 606 |
T[k] ^= U[k]; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
/* Copy as many bytes as necessary into buf. */ |
| 611 |
clen = dkLen - i * 32; |
| 612 |
if (clen > 32) |
| 613 |
clen = 32; |
| 614 |
memcpy(&buf[i * 32], T, clen); |
| 615 |
} |
| 616 |
|
| 617 |
/* Clean the stack. */ |
| 618 |
explicit_bzero(&Phctx, sizeof(HMAC_SHA256_CTX)); |
| 619 |
explicit_bzero(&PShctx, sizeof(HMAC_SHA256_CTX)); |
| 620 |
explicit_bzero(U, 32); |
| 621 |
explicit_bzero(T, 32); |
| 622 |
|
| 623 |
cleanup: |
| 624 |
explicit_bzero(&hctx, sizeof(HMAC_SHA256_CTX)); |
| 625 |
explicit_bzero(tmp32, 288); |
| 626 |
explicit_bzero(&u, sizeof(u)); |
| 627 |
} |
| 628 |
|
| 629 |
#endif /* INCLUDE_gost_yescrypt || INCLUDE_yescrypt || INCLUDE_scrypt || |
| 630 |
INCLUDE_sm3_yescrypt */ |