hmac_drbg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright (c) 2019 Andrew R. Kozlik
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "hmac_drbg.h"
  23. #include <string.h>
  24. #include "memzero.h"
  25. #include "sha2.h"
  26. static void update_k(HMAC_DRBG_CTX *ctx, uint8_t domain, const uint8_t *data1,
  27. size_t len1, const uint8_t *data2, size_t len2) {
  28. // Computes K = HMAC(K, V || domain || data1 || data 2).
  29. // First hash operation of HMAC.
  30. uint32_t h[SHA256_BLOCK_LENGTH / sizeof(uint32_t)] = {0};
  31. if (len1 + len2 == 0) {
  32. ctx->v[8] = 0x00800000;
  33. ctx->v[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH + 1) * 8;
  34. sha256_Transform(ctx->idig, ctx->v, h);
  35. ctx->v[8] = 0x80000000;
  36. ctx->v[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
  37. } else {
  38. SHA256_CTX sha_ctx = {0};
  39. memcpy(sha_ctx.state, ctx->idig, SHA256_DIGEST_LENGTH);
  40. for (size_t i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++) {
  41. #if BYTE_ORDER == LITTLE_ENDIAN
  42. REVERSE32(ctx->v[i], sha_ctx.buffer[i]);
  43. #else
  44. sha_ctx.buffer[i] = ctx->v[i];
  45. #endif
  46. }
  47. ((uint8_t *)sha_ctx.buffer)[SHA256_DIGEST_LENGTH] = domain;
  48. sha_ctx.bitcount = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH + 1) * 8;
  49. sha256_Update(&sha_ctx, data1, len1);
  50. sha256_Update(&sha_ctx, data2, len2);
  51. sha256_Final(&sha_ctx, (uint8_t *)h);
  52. #if BYTE_ORDER == LITTLE_ENDIAN
  53. for (size_t i = 0; i < SHA256_DIGEST_LENGTH / sizeof(uint32_t); i++)
  54. REVERSE32(h[i], h[i]);
  55. #endif
  56. }
  57. // Second hash operation of HMAC.
  58. h[8] = 0x80000000;
  59. h[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
  60. sha256_Transform(ctx->odig, h, h);
  61. // Precompute the inner digest and outer digest of K.
  62. h[8] = 0;
  63. h[15] = 0;
  64. for (size_t i = 0; i < SHA256_BLOCK_LENGTH / sizeof(uint32_t); i++) {
  65. h[i] ^= 0x36363636;
  66. }
  67. sha256_Transform(sha256_initial_hash_value, h, ctx->idig);
  68. for (size_t i = 0; i < SHA256_BLOCK_LENGTH / sizeof(uint32_t); i++) {
  69. h[i] = h[i] ^ 0x36363636 ^ 0x5c5c5c5c;
  70. }
  71. sha256_Transform(sha256_initial_hash_value, h, ctx->odig);
  72. memzero(h, sizeof(h));
  73. }
  74. static void update_v(HMAC_DRBG_CTX *ctx) {
  75. sha256_Transform(ctx->idig, ctx->v, ctx->v);
  76. sha256_Transform(ctx->odig, ctx->v, ctx->v);
  77. }
  78. void hmac_drbg_init(HMAC_DRBG_CTX *ctx, const uint8_t *entropy,
  79. size_t entropy_len, const uint8_t *nonce,
  80. size_t nonce_len) {
  81. uint32_t h[SHA256_BLOCK_LENGTH / sizeof(uint32_t)] = {0};
  82. // Precompute the inner digest and outer digest of K = 0x00 ... 0x00.
  83. memset(h, 0x36, sizeof(h));
  84. sha256_Transform(sha256_initial_hash_value, h, ctx->idig);
  85. memset(h, 0x5c, sizeof(h));
  86. sha256_Transform(sha256_initial_hash_value, h, ctx->odig);
  87. // Let V = 0x01 ... 0x01.
  88. memset(ctx->v, 1, SHA256_DIGEST_LENGTH);
  89. for (size_t i = 9; i < 15; i++) ctx->v[i] = 0;
  90. ctx->v[8] = 0x80000000;
  91. ctx->v[15] = (SHA256_BLOCK_LENGTH + SHA256_DIGEST_LENGTH) * 8;
  92. hmac_drbg_reseed(ctx, entropy, entropy_len, nonce, nonce_len);
  93. memzero(h, sizeof(h));
  94. }
  95. void hmac_drbg_reseed(HMAC_DRBG_CTX *ctx, const uint8_t *entropy, size_t len,
  96. const uint8_t *addin, size_t addin_len) {
  97. update_k(ctx, 0, entropy, len, addin, addin_len);
  98. update_v(ctx);
  99. if (len == 0) return;
  100. update_k(ctx, 1, entropy, len, addin, addin_len);
  101. update_v(ctx);
  102. }
  103. void hmac_drbg_generate(HMAC_DRBG_CTX *ctx, uint8_t *buf, size_t len) {
  104. size_t i = 0;
  105. while (i < len) {
  106. update_v(ctx);
  107. for (size_t j = 0; j < 8 && i < len; j++) {
  108. uint32_t r = ctx->v[j];
  109. for (int k = 24; k >= 0 && i < len; k -= 8) {
  110. buf[i++] = (r >> k) & 0xFF;
  111. }
  112. }
  113. }
  114. update_k(ctx, 0, NULL, 0, NULL, 0);
  115. update_v(ctx);
  116. }