sha256.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. #include <assert.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "cpusupport.h"
  5. #include "insecure_memzero.h"
  6. #include "sha256_arm.h"
  7. #include "sha256_shani.h"
  8. #include "sha256_sse2.h"
  9. #include "sysendian.h"
  10. #include "warnp.h"
  11. #include "sha256.h"
  12. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3) || \
  13. defined(CPUSUPPORT_X86_SSE2) || \
  14. defined(CPUSUPPORT_ARM_SHA256)
  15. #define HWACCEL
  16. static enum {
  17. HW_SOFTWARE = 0,
  18. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
  19. HW_X86_SHANI,
  20. #endif
  21. #if defined(CPUSUPPORT_X86_SSE2)
  22. HW_X86_SSE2,
  23. #endif
  24. #if defined(CPUSUPPORT_ARM_SHA256)
  25. HW_ARM_SHA256,
  26. #endif
  27. HW_UNSET
  28. } hwaccel = HW_UNSET;
  29. #endif
  30. #ifdef POSIXFAIL_ABSTRACT_DECLARATOR
  31. static void SHA256_Transform(uint32_t state[static restrict 8],
  32. const uint8_t block[static restrict 64], uint32_t W[static restrict 64],
  33. uint32_t S[static restrict 8]);
  34. #else
  35. static void SHA256_Transform(uint32_t[static restrict 8],
  36. const uint8_t[static restrict 64], uint32_t[static restrict 64],
  37. uint32_t[static restrict 8]);
  38. #endif
  39. /*
  40. * Encode a length len/4 vector of (uint32_t) into a length len vector of
  41. * (uint8_t) in big-endian form. Assumes len is a multiple of 4.
  42. */
  43. static void
  44. be32enc_vect(uint8_t * dst, const uint32_t * src, size_t len)
  45. {
  46. size_t i;
  47. /* Sanity-check. */
  48. assert(len % 4 == 0);
  49. /* Encode vector, one word at a time. */
  50. for (i = 0; i < len / 4; i++)
  51. be32enc(dst + i * 4, src[i]);
  52. }
  53. /*
  54. * Decode a big-endian length len vector of (uint8_t) into a length
  55. * len/4 vector of (uint32_t). Assumes len is a multiple of 4.
  56. */
  57. static void
  58. be32dec_vect(uint32_t * dst, const uint8_t * src, size_t len)
  59. {
  60. size_t i;
  61. /* Sanity-check. */
  62. assert(len % 4 == 0);
  63. /* Decode vector, one word at a time. */
  64. for (i = 0; i < len / 4; i++)
  65. dst[i] = be32dec(src + i * 4);
  66. }
  67. /* SHA256 round constants. */
  68. static const uint32_t Krnd[64] = {
  69. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  70. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  71. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  72. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  73. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  74. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  75. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  76. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  77. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  78. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  79. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  80. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  81. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  82. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  83. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  84. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  85. };
  86. /* Magic initialization constants. */
  87. static const uint32_t initial_state[8] = {
  88. 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
  89. 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
  90. };
  91. #ifdef HWACCEL
  92. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
  93. /* Shim so that we can test SHA256_Transform_shani() in the standard manner. */
  94. static void
  95. SHA256_Transform_shani_with_W_S(uint32_t state[static restrict 8],
  96. const uint8_t block[static restrict 64], uint32_t W[static restrict 64],
  97. uint32_t S[static restrict 8])
  98. {
  99. (void)W; /* UNUSED */
  100. (void)S; /* UNUSED */
  101. SHA256_Transform_shani(state, block);
  102. }
  103. #endif
  104. #if defined(CPUSUPPORT_ARM_SHA256)
  105. /* Shim so that we can test SHA256_Transform_arm() in the standard manner. */
  106. static void
  107. SHA256_Transform_arm_with_W_S(uint32_t state[static restrict 8],
  108. const uint8_t block[static restrict 64], uint32_t W[static restrict 64],
  109. uint32_t S[static restrict 8])
  110. {
  111. (void)W; /* UNUSED */
  112. (void)S; /* UNUSED */
  113. SHA256_Transform_arm(state, block);
  114. }
  115. #endif
  116. /*
  117. * Test whether software and hardware extensions transform code produce the
  118. * same results. Must be called with (hwaccel == HW_SOFTWARE).
  119. */
  120. static int
  121. hwtest(const uint32_t state[static restrict 8],
  122. const uint8_t block[static restrict 64],
  123. uint32_t W[static restrict 64], uint32_t S[static restrict 8],
  124. void (* func)(uint32_t state[static restrict 8],
  125. const uint8_t block[static restrict 64], uint32_t W[static restrict 64],
  126. uint32_t S[static restrict 8]))
  127. {
  128. uint32_t state_sw[8];
  129. uint32_t state_hw[8];
  130. /* Software transform. */
  131. memcpy(state_sw, state, sizeof(state_sw));
  132. SHA256_Transform(state_sw, block, W, S);
  133. /* Hardware transform. */
  134. memcpy(state_hw, state, sizeof(state_hw));
  135. func(state_hw, block, W, S);
  136. /* Do the results match? */
  137. return (memcmp(state_sw, state_hw, sizeof(state_sw)));
  138. }
  139. /* Which type of hardware acceleration should we use, if any? */
  140. static void
  141. hwaccel_init(void)
  142. {
  143. uint32_t W[64];
  144. uint32_t S[8];
  145. uint8_t block[64];
  146. uint8_t i;
  147. /* If we've already set hwaccel, we're finished. */
  148. if (hwaccel != HW_UNSET)
  149. return;
  150. /* Default to software. */
  151. hwaccel = HW_SOFTWARE;
  152. /* Test case: Hash 0x00 0x01 0x02 ... 0x3f. */
  153. for (i = 0; i < 64; i++)
  154. block[i] = i;
  155. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
  156. CPUSUPPORT_VALIDATE(hwaccel, HW_X86_SHANI,
  157. cpusupport_x86_shani() && cpusupport_x86_ssse3(),
  158. hwtest(initial_state, block, W, S,
  159. SHA256_Transform_shani_with_W_S));
  160. #endif
  161. #if defined(CPUSUPPORT_X86_SSE2)
  162. CPUSUPPORT_VALIDATE(hwaccel, HW_X86_SSE2, cpusupport_x86_sse2(),
  163. hwtest(initial_state, block, W, S, SHA256_Transform_sse2));
  164. #endif
  165. #if defined(CPUSUPPORT_ARM_SHA256)
  166. CPUSUPPORT_VALIDATE(hwaccel, HW_ARM_SHA256, cpusupport_arm_sha256(),
  167. hwtest(initial_state, block, W, S, SHA256_Transform_arm_with_W_S));
  168. #endif
  169. }
  170. #endif /* HWACCEL */
  171. /* Elementary functions used by SHA256 */
  172. #define Ch(x, y, z) ((x & (y ^ z)) ^ z)
  173. #define Maj(x, y, z) ((x & (y | z)) | (y & z))
  174. #define SHR(x, n) (x >> n)
  175. #define ROTR(x, n) ((x >> n) | (x << (32 - n)))
  176. #define S0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
  177. #define S1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
  178. #define s0(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
  179. #define s1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
  180. /* SHA256 round function */
  181. #define RND(a, b, c, d, e, f, g, h, k) \
  182. h += S1(e) + Ch(e, f, g) + k; \
  183. d += h; \
  184. h += S0(a) + Maj(a, b, c)
  185. /* Adjusted round function for rotating state */
  186. #define RNDr(S, W, i, ii) \
  187. RND(S[(64 - i) % 8], S[(65 - i) % 8], \
  188. S[(66 - i) % 8], S[(67 - i) % 8], \
  189. S[(68 - i) % 8], S[(69 - i) % 8], \
  190. S[(70 - i) % 8], S[(71 - i) % 8], \
  191. W[i + ii] + Krnd[i + ii])
  192. /* Message schedule computation */
  193. #define MSCH(W, ii, i) \
  194. W[i + ii + 16] = s1(W[i + ii + 14]) + W[i + ii + 9] + s0(W[i + ii + 1]) + W[i + ii]
  195. /*
  196. * SHA256 block compression function. The 256-bit state is transformed via
  197. * the 512-bit input block to produce a new state. The arrays W and S may be
  198. * filled with sensitive data, and should be sanitized by the callee.
  199. */
  200. static void
  201. SHA256_Transform(uint32_t state[static restrict 8],
  202. const uint8_t block[static restrict 64],
  203. uint32_t W[static restrict 64], uint32_t S[static restrict 8])
  204. {
  205. int i;
  206. #ifdef HWACCEL
  207. #if defined(__GNUC__) && defined(__aarch64__)
  208. /*
  209. * We require that SHA256_Init() is called before SHA256_Transform(),
  210. * but the compiler has no way of knowing that. This assert adds a
  211. * significant speed boost for gcc on 64-bit ARM, and a minor penalty
  212. * on other systems & compilers.
  213. */
  214. assert(hwaccel != HW_UNSET);
  215. #endif
  216. switch (hwaccel) {
  217. #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
  218. case HW_X86_SHANI:
  219. SHA256_Transform_shani(state, block);
  220. return;
  221. #endif
  222. #if defined(CPUSUPPORT_X86_SSE2)
  223. case HW_X86_SSE2:
  224. SHA256_Transform_sse2(state, block, W, S);
  225. return;
  226. #endif
  227. #if defined(CPUSUPPORT_ARM_SHA256)
  228. case HW_ARM_SHA256:
  229. SHA256_Transform_arm(state, block);
  230. return;
  231. #endif
  232. case HW_SOFTWARE:
  233. case HW_UNSET:
  234. break;
  235. }
  236. #endif /* HWACCEL */
  237. /* 1. Prepare the first part of the message schedule W. */
  238. be32dec_vect(W, block, 64);
  239. /* 2. Initialize working variables. */
  240. memcpy(S, state, 32);
  241. /* 3. Mix. */
  242. for (i = 0; i < 64; i += 16) {
  243. RNDr(S, W, 0, i);
  244. RNDr(S, W, 1, i);
  245. RNDr(S, W, 2, i);
  246. RNDr(S, W, 3, i);
  247. RNDr(S, W, 4, i);
  248. RNDr(S, W, 5, i);
  249. RNDr(S, W, 6, i);
  250. RNDr(S, W, 7, i);
  251. RNDr(S, W, 8, i);
  252. RNDr(S, W, 9, i);
  253. RNDr(S, W, 10, i);
  254. RNDr(S, W, 11, i);
  255. RNDr(S, W, 12, i);
  256. RNDr(S, W, 13, i);
  257. RNDr(S, W, 14, i);
  258. RNDr(S, W, 15, i);
  259. if (i == 48)
  260. break;
  261. MSCH(W, 0, i);
  262. MSCH(W, 1, i);
  263. MSCH(W, 2, i);
  264. MSCH(W, 3, i);
  265. MSCH(W, 4, i);
  266. MSCH(W, 5, i);
  267. MSCH(W, 6, i);
  268. MSCH(W, 7, i);
  269. MSCH(W, 8, i);
  270. MSCH(W, 9, i);
  271. MSCH(W, 10, i);
  272. MSCH(W, 11, i);
  273. MSCH(W, 12, i);
  274. MSCH(W, 13, i);
  275. MSCH(W, 14, i);
  276. MSCH(W, 15, i);
  277. }
  278. /* 4. Mix local working variables into global state. */
  279. for (i = 0; i < 8; i++)
  280. state[i] += S[i];
  281. }
  282. static const uint8_t PAD[64] = {
  283. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  284. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  285. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  286. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  287. };
  288. /* Add padding and terminating bit-count. */
  289. static void
  290. SHA256_Pad(SHA256_CTX * ctx, uint32_t tmp32[static restrict 72])
  291. {
  292. size_t r;
  293. /* Figure out how many bytes we have buffered. */
  294. r = (ctx->count >> 3) & 0x3f;
  295. /* Pad to 56 mod 64, transforming if we finish a block en route. */
  296. if (r < 56) {
  297. /* Pad to 56 mod 64. */
  298. memcpy(&ctx->buf[r], PAD, 56 - r);
  299. } else {
  300. /* Finish the current block and mix. */
  301. memcpy(&ctx->buf[r], PAD, 64 - r);
  302. SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
  303. /* The start of the final block is all zeroes. */
  304. memset(&ctx->buf[0], 0, 56);
  305. }
  306. /* Add the terminating bit-count. */
  307. be64enc(&ctx->buf[56], ctx->count);
  308. /* Mix in the final block. */
  309. SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
  310. }
  311. /**
  312. * SHA256_Init(ctx):
  313. * Initialize the SHA256 context ${ctx}.
  314. */
  315. void
  316. SHA256_Init(SHA256_CTX * ctx)
  317. {
  318. /* Zero bits processed so far. */
  319. ctx->count = 0;
  320. /* Initialize state. */
  321. memcpy(ctx->state, initial_state, sizeof(initial_state));
  322. #ifdef HWACCEL
  323. /* Ensure that we've chosen the type of hardware acceleration. */
  324. hwaccel_init();
  325. #endif
  326. }
  327. /**
  328. * SHA256_Update(ctx, in, len):
  329. * Input ${len} bytes from ${in} into the SHA256 context ${ctx}.
  330. */
  331. static void
  332. SHA256_Update_internal(SHA256_CTX * ctx, const void * in, size_t len,
  333. uint32_t tmp32[static restrict 72])
  334. {
  335. uint32_t r;
  336. const uint8_t * src = in;
  337. /* Return immediately if we have nothing to do. */
  338. if (len == 0)
  339. return;
  340. /* Number of bytes left in the buffer from previous updates. */
  341. r = (ctx->count >> 3) & 0x3f;
  342. /* Update number of bits. */
  343. ctx->count += (uint64_t)(len) << 3;
  344. /* Handle the case where we don't need to perform any transforms. */
  345. if (len < 64 - r) {
  346. memcpy(&ctx->buf[r], src, len);
  347. return;
  348. }
  349. /* Finish the current block. */
  350. memcpy(&ctx->buf[r], src, 64 - r);
  351. SHA256_Transform(ctx->state, ctx->buf, &tmp32[0], &tmp32[64]);
  352. src += 64 - r;
  353. len -= 64 - r;
  354. /* Perform complete blocks. */
  355. while (len >= 64) {
  356. SHA256_Transform(ctx->state, src, &tmp32[0], &tmp32[64]);
  357. src += 64;
  358. len -= 64;
  359. }
  360. /* Copy left over data into buffer. */
  361. memcpy(ctx->buf, src, len);
  362. }
  363. /* Wrapper function for intermediate-values sanitization. */
  364. void
  365. SHA256_Update(SHA256_CTX * ctx, const void * in, size_t len)
  366. {
  367. uint32_t tmp32[72];
  368. /* Call the real function. */
  369. SHA256_Update_internal(ctx, in, len, tmp32);
  370. /* Clean the stack. */
  371. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  372. }
  373. /**
  374. * SHA256_Final(digest, ctx):
  375. * Output the SHA256 hash of the data input to the context ${ctx} into the
  376. * buffer ${digest}, and clear the context state.
  377. */
  378. static void
  379. SHA256_Final_internal(uint8_t digest[32], SHA256_CTX * ctx,
  380. uint32_t tmp32[static restrict 72])
  381. {
  382. /* Add padding. */
  383. SHA256_Pad(ctx, tmp32);
  384. /* Write the hash. */
  385. be32enc_vect(digest, ctx->state, 32);
  386. }
  387. /* Wrapper function for intermediate-values sanitization. */
  388. void
  389. SHA256_Final(uint8_t digest[32], SHA256_CTX * ctx)
  390. {
  391. uint32_t tmp32[72];
  392. /* Call the real function. */
  393. SHA256_Final_internal(digest, ctx, tmp32);
  394. /* Clear the context state. */
  395. insecure_memzero(ctx, sizeof(SHA256_CTX));
  396. /* Clean the stack. */
  397. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  398. }
  399. /**
  400. * SHA256_Buf(in, len, digest):
  401. * Compute the SHA256 hash of ${len} bytes from ${in} and write it to ${digest}.
  402. */
  403. void
  404. SHA256_Buf(const void * in, size_t len, uint8_t digest[32])
  405. {
  406. SHA256_CTX ctx;
  407. uint32_t tmp32[72];
  408. SHA256_Init(&ctx);
  409. SHA256_Update_internal(&ctx, in, len, tmp32);
  410. SHA256_Final_internal(digest, &ctx, tmp32);
  411. /* Clean the stack. */
  412. insecure_memzero(&ctx, sizeof(SHA256_CTX));
  413. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  414. }
  415. /**
  416. * HMAC_SHA256_Init(ctx, K, Klen):
  417. * Initialize the HMAC-SHA256 context ${ctx} with ${Klen} bytes of key from
  418. * ${K}.
  419. */
  420. static void
  421. HMAC_SHA256_Init_internal(HMAC_SHA256_CTX * ctx, const void * _k, size_t Klen,
  422. uint32_t tmp32[static restrict 72], uint8_t pad[static restrict 64],
  423. uint8_t khash[static restrict 32])
  424. {
  425. const uint8_t * K = _k;
  426. size_t i;
  427. /* If Klen > 64, the key is really SHA256(K). */
  428. if (Klen > 64) {
  429. SHA256_Init(&ctx->ictx);
  430. SHA256_Update_internal(&ctx->ictx, K, Klen, tmp32);
  431. SHA256_Final_internal(khash, &ctx->ictx, tmp32);
  432. K = khash;
  433. Klen = 32;
  434. }
  435. /* Inner SHA256 operation is SHA256(K xor [block of 0x36] || data). */
  436. SHA256_Init(&ctx->ictx);
  437. memset(pad, 0x36, 64);
  438. for (i = 0; i < Klen; i++)
  439. pad[i] ^= K[i];
  440. SHA256_Update_internal(&ctx->ictx, pad, 64, tmp32);
  441. /* Outer SHA256 operation is SHA256(K xor [block of 0x5c] || hash). */
  442. SHA256_Init(&ctx->octx);
  443. memset(pad, 0x5c, 64);
  444. for (i = 0; i < Klen; i++)
  445. pad[i] ^= K[i];
  446. SHA256_Update_internal(&ctx->octx, pad, 64, tmp32);
  447. }
  448. /* Wrapper function for intermediate-values sanitization. */
  449. void
  450. HMAC_SHA256_Init(HMAC_SHA256_CTX * ctx, const void * K, size_t Klen)
  451. {
  452. uint32_t tmp32[72];
  453. uint8_t pad[64];
  454. uint8_t khash[32];
  455. /* Call the real function. */
  456. HMAC_SHA256_Init_internal(ctx, K, Klen, tmp32, pad, khash);
  457. /* Clean the stack. */
  458. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  459. insecure_memzero(khash, 32);
  460. insecure_memzero(pad, 64);
  461. }
  462. /**
  463. * HMAC_SHA256_Update(ctx, in, len):
  464. * Input ${len} bytes from ${in} into the HMAC-SHA256 context ${ctx}.
  465. */
  466. static void
  467. HMAC_SHA256_Update_internal(HMAC_SHA256_CTX * ctx, const void * in, size_t len,
  468. uint32_t tmp32[static restrict 72])
  469. {
  470. /* Feed data to the inner SHA256 operation. */
  471. SHA256_Update_internal(&ctx->ictx, in, len, tmp32);
  472. }
  473. /* Wrapper function for intermediate-values sanitization. */
  474. void
  475. HMAC_SHA256_Update(HMAC_SHA256_CTX * ctx, const void * in, size_t len)
  476. {
  477. uint32_t tmp32[72];
  478. /* Call the real function. */
  479. HMAC_SHA256_Update_internal(ctx, in, len, tmp32);
  480. /* Clean the stack. */
  481. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  482. }
  483. /**
  484. * HMAC_SHA256_Final(digest, ctx):
  485. * Output the HMAC-SHA256 of the data input to the context ${ctx} into the
  486. * buffer ${digest}, and clear the context state.
  487. */
  488. static void
  489. HMAC_SHA256_Final_internal(uint8_t digest[32], HMAC_SHA256_CTX * ctx,
  490. uint32_t tmp32[static restrict 72], uint8_t ihash[static restrict 32])
  491. {
  492. /* Finish the inner SHA256 operation. */
  493. SHA256_Final_internal(ihash, &ctx->ictx, tmp32);
  494. /* Feed the inner hash to the outer SHA256 operation. */
  495. SHA256_Update_internal(&ctx->octx, ihash, 32, tmp32);
  496. /* Finish the outer SHA256 operation. */
  497. SHA256_Final_internal(digest, &ctx->octx, tmp32);
  498. }
  499. /* Wrapper function for intermediate-values sanitization. */
  500. void
  501. HMAC_SHA256_Final(uint8_t digest[32], HMAC_SHA256_CTX * ctx)
  502. {
  503. uint32_t tmp32[72];
  504. uint8_t ihash[32];
  505. /* Call the real function. */
  506. HMAC_SHA256_Final_internal(digest, ctx, tmp32, ihash);
  507. /* Clear the context state. */
  508. insecure_memzero(ctx, sizeof(HMAC_SHA256_CTX));
  509. /* Clean the stack. */
  510. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  511. insecure_memzero(ihash, 32);
  512. }
  513. /**
  514. * HMAC_SHA256_Buf(K, Klen, in, len, digest):
  515. * Compute the HMAC-SHA256 of ${len} bytes from ${in} using the key ${K} of
  516. * length ${Klen}, and write the result to ${digest}.
  517. */
  518. void
  519. HMAC_SHA256_Buf(const void * K, size_t Klen, const void * in, size_t len,
  520. uint8_t digest[32])
  521. {
  522. HMAC_SHA256_CTX ctx;
  523. uint32_t tmp32[72];
  524. uint8_t tmp8[96];
  525. HMAC_SHA256_Init_internal(&ctx, K, Klen, tmp32, &tmp8[0], &tmp8[64]);
  526. HMAC_SHA256_Update_internal(&ctx, in, len, tmp32);
  527. HMAC_SHA256_Final_internal(digest, &ctx, tmp32, &tmp8[0]);
  528. /* Clean the stack. */
  529. insecure_memzero(&ctx, sizeof(HMAC_SHA256_CTX));
  530. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  531. insecure_memzero(tmp8, 96);
  532. }
  533. /**
  534. * PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen):
  535. * Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and
  536. * write the output to buf. The value dkLen must be at most 32 * (2^32 - 1).
  537. */
  538. void
  539. PBKDF2_SHA256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt,
  540. size_t saltlen, uint64_t c, uint8_t * buf, size_t dkLen)
  541. {
  542. HMAC_SHA256_CTX Phctx, PShctx, hctx;
  543. uint32_t tmp32[72];
  544. uint8_t tmp8[96];
  545. size_t i;
  546. uint8_t ivec[4];
  547. uint8_t U[32];
  548. uint8_t T[32];
  549. uint64_t j;
  550. int k;
  551. size_t clen;
  552. #if SIZE_MAX >= (32 * UINT32_MAX)
  553. /* Sanity-check. */
  554. assert(dkLen <= 32 * (size_t)(UINT32_MAX));
  555. #endif
  556. /* Compute HMAC state after processing P. */
  557. HMAC_SHA256_Init_internal(&Phctx, passwd, passwdlen,
  558. tmp32, &tmp8[0], &tmp8[64]);
  559. /* Compute HMAC state after processing P and S. */
  560. memcpy(&PShctx, &Phctx, sizeof(HMAC_SHA256_CTX));
  561. HMAC_SHA256_Update_internal(&PShctx, salt, saltlen, tmp32);
  562. /* Iterate through the blocks. */
  563. for (i = 0; i * 32 < dkLen; i++) {
  564. /* Generate INT(i + 1). */
  565. be32enc(ivec, (uint32_t)(i + 1));
  566. /* Compute U_1 = PRF(P, S || INT(i)). */
  567. memcpy(&hctx, &PShctx, sizeof(HMAC_SHA256_CTX));
  568. HMAC_SHA256_Update_internal(&hctx, ivec, 4, tmp32);
  569. HMAC_SHA256_Final_internal(U, &hctx, tmp32, tmp8);
  570. /* T_i = U_1 ... */
  571. memcpy(T, U, 32);
  572. for (j = 2; j <= c; j++) {
  573. /* Compute U_j. */
  574. memcpy(&hctx, &Phctx, sizeof(HMAC_SHA256_CTX));
  575. HMAC_SHA256_Update_internal(&hctx, U, 32, tmp32);
  576. HMAC_SHA256_Final_internal(U, &hctx, tmp32, tmp8);
  577. /* ... xor U_j ... */
  578. for (k = 0; k < 32; k++)
  579. T[k] ^= U[k];
  580. }
  581. /* Copy as many bytes as necessary into buf. */
  582. clen = dkLen - i * 32;
  583. if (clen > 32)
  584. clen = 32;
  585. memcpy(&buf[i * 32], T, clen);
  586. }
  587. /* Clean the stack. */
  588. insecure_memzero(&Phctx, sizeof(HMAC_SHA256_CTX));
  589. insecure_memzero(&PShctx, sizeof(HMAC_SHA256_CTX));
  590. insecure_memzero(&hctx, sizeof(HMAC_SHA256_CTX));
  591. insecure_memzero(tmp32, sizeof(uint32_t) * 72);
  592. insecure_memzero(tmp8, 96);
  593. insecure_memzero(U, 32);
  594. insecure_memzero(T, 32);
  595. }