chacha_drbg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This file is part of the Trezor project, https://trezor.io/
  3. *
  4. * Copyright (c) SatoshiLabs
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "chacha_drbg.h"
  20. #include <assert.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include "chacha20poly1305/ecrypt-portable.h"
  24. #include "memzero.h"
  25. #include "sha2.h"
  26. #define CHACHA_DRBG_KEY_LENGTH 32
  27. #define CHACHA_DRBG_COUNTER_LENGTH 8
  28. #define CHACHA_DRBG_IV_LENGTH 8
  29. #define CHACHA_DRBG_SEED_LENGTH \
  30. (CHACHA_DRBG_KEY_LENGTH + CHACHA_DRBG_COUNTER_LENGTH + CHACHA_DRBG_IV_LENGTH)
  31. #define MAX(a, b) (a) > (b) ? (a) : (b)
  32. static void derivation_function(const uint8_t *input1, size_t input1_length,
  33. const uint8_t *input2, size_t input2_length,
  34. uint8_t *output, size_t output_length) {
  35. // Implementation of Hash_df from NIST SP 800-90A
  36. uint32_t block_count = (output_length - 1) / SHA256_DIGEST_LENGTH + 1;
  37. size_t partial_block_length = output_length % SHA256_DIGEST_LENGTH;
  38. assert(block_count <= 255);
  39. uint32_t output_length_bits = output_length * 8;
  40. #if BYTE_ORDER == LITTLE_ENDIAN
  41. REVERSE32(output_length_bits, output_length_bits);
  42. #endif
  43. SHA256_CTX ctx = {0};
  44. for (uint8_t counter = 1; counter <= block_count; counter++) {
  45. sha256_Init(&ctx);
  46. sha256_Update(&ctx, &counter, sizeof(counter));
  47. sha256_Update(&ctx, (uint8_t *)&output_length_bits,
  48. sizeof(output_length_bits));
  49. sha256_Update(&ctx, input1, input1_length);
  50. sha256_Update(&ctx, input2, input2_length);
  51. if (counter != block_count || partial_block_length == 0) {
  52. sha256_Final(&ctx, output);
  53. output += SHA256_DIGEST_LENGTH;
  54. } else { // last block is partial
  55. uint8_t digest[SHA256_DIGEST_LENGTH] = {0};
  56. sha256_Final(&ctx, digest);
  57. memcpy(output, digest, partial_block_length);
  58. memzero(digest, sizeof(digest));
  59. }
  60. }
  61. memzero(&ctx, sizeof(ctx));
  62. }
  63. void chacha_drbg_init(CHACHA_DRBG_CTX *ctx, const uint8_t *entropy,
  64. size_t entropy_length, const uint8_t *nonce,
  65. size_t nonce_length) {
  66. uint8_t buffer[MAX(CHACHA_DRBG_KEY_LENGTH, CHACHA_DRBG_IV_LENGTH)] = {0};
  67. ECRYPT_keysetup(&ctx->chacha_ctx, buffer, CHACHA_DRBG_KEY_LENGTH * 8,
  68. CHACHA_DRBG_IV_LENGTH * 8);
  69. ECRYPT_ivsetup(&ctx->chacha_ctx, buffer);
  70. chacha_drbg_reseed(ctx, entropy, entropy_length, nonce, nonce_length);
  71. }
  72. static void chacha_drbg_update(CHACHA_DRBG_CTX *ctx,
  73. const uint8_t data[CHACHA_DRBG_SEED_LENGTH]) {
  74. uint8_t seed[CHACHA_DRBG_SEED_LENGTH] = {0};
  75. if (data)
  76. ECRYPT_encrypt_bytes(&ctx->chacha_ctx, data, seed, CHACHA_DRBG_SEED_LENGTH);
  77. else
  78. ECRYPT_keystream_bytes(&ctx->chacha_ctx, seed, CHACHA_DRBG_SEED_LENGTH);
  79. ECRYPT_keysetup(&ctx->chacha_ctx, seed, CHACHA_DRBG_KEY_LENGTH * 8,
  80. CHACHA_DRBG_IV_LENGTH * 8);
  81. ECRYPT_ivsetup(&ctx->chacha_ctx,
  82. seed + CHACHA_DRBG_KEY_LENGTH + CHACHA_DRBG_COUNTER_LENGTH);
  83. ECRYPT_ctrsetup(&ctx->chacha_ctx, seed + CHACHA_DRBG_KEY_LENGTH);
  84. memzero(seed, sizeof(seed));
  85. }
  86. void chacha_drbg_generate(CHACHA_DRBG_CTX *ctx, uint8_t *output,
  87. size_t output_length) {
  88. assert(output_length < 65536);
  89. assert(ctx->reseed_counter + 1 != 0);
  90. ECRYPT_keystream_bytes(&ctx->chacha_ctx, output, output_length);
  91. chacha_drbg_update(ctx, NULL);
  92. ctx->reseed_counter++;
  93. }
  94. void chacha_drbg_reseed(CHACHA_DRBG_CTX *ctx, const uint8_t *entropy,
  95. size_t entropy_length, const uint8_t *additional_input,
  96. size_t additional_input_length) {
  97. uint8_t seed[CHACHA_DRBG_SEED_LENGTH] = {0};
  98. derivation_function(entropy, entropy_length, additional_input,
  99. additional_input_length, seed, sizeof(seed));
  100. chacha_drbg_update(ctx, seed);
  101. memzero(seed, sizeof(seed));
  102. ctx->reseed_counter = 1;
  103. }