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