chacha_drbg.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(
  33. const uint8_t* input1,
  34. size_t input1_length,
  35. const uint8_t* input2,
  36. size_t input2_length,
  37. uint8_t* output,
  38. size_t output_length) {
  39. // Implementation of Hash_df from NIST SP 800-90A
  40. uint32_t block_count = (output_length - 1) / SHA256_DIGEST_LENGTH + 1;
  41. size_t partial_block_length = output_length % SHA256_DIGEST_LENGTH;
  42. assert(block_count <= 255);
  43. uint32_t output_length_bits = output_length * 8;
  44. #if BYTE_ORDER == LITTLE_ENDIAN
  45. REVERSE32(output_length_bits, output_length_bits);
  46. #endif
  47. SHA256_CTX ctx = {0};
  48. for(uint8_t counter = 1; counter <= block_count; counter++) {
  49. sha256_Init(&ctx);
  50. sha256_Update(&ctx, &counter, sizeof(counter));
  51. sha256_Update(&ctx, (uint8_t*)&output_length_bits, sizeof(output_length_bits));
  52. sha256_Update(&ctx, input1, input1_length);
  53. sha256_Update(&ctx, input2, input2_length);
  54. if(counter != block_count || partial_block_length == 0) {
  55. sha256_Final(&ctx, output);
  56. output += SHA256_DIGEST_LENGTH;
  57. } else { // last block is partial
  58. uint8_t digest[SHA256_DIGEST_LENGTH] = {0};
  59. sha256_Final(&ctx, digest);
  60. memcpy(output, digest, partial_block_length);
  61. memzero(digest, sizeof(digest));
  62. }
  63. }
  64. memzero(&ctx, sizeof(ctx));
  65. }
  66. void chacha_drbg_init(
  67. CHACHA_DRBG_CTX* ctx,
  68. const uint8_t* entropy,
  69. size_t entropy_length,
  70. const uint8_t* nonce,
  71. size_t nonce_length) {
  72. uint8_t buffer[MAX(CHACHA_DRBG_KEY_LENGTH, CHACHA_DRBG_IV_LENGTH)] = {0};
  73. ECRYPT_keysetup(
  74. &ctx->chacha_ctx, buffer, CHACHA_DRBG_KEY_LENGTH * 8, CHACHA_DRBG_IV_LENGTH * 8);
  75. ECRYPT_ivsetup(&ctx->chacha_ctx, buffer);
  76. chacha_drbg_reseed(ctx, entropy, entropy_length, nonce, nonce_length);
  77. }
  78. static void chacha_drbg_update(CHACHA_DRBG_CTX* ctx, const uint8_t data[CHACHA_DRBG_SEED_LENGTH]) {
  79. uint8_t seed[CHACHA_DRBG_SEED_LENGTH] = {0};
  80. if(data)
  81. ECRYPT_encrypt_bytes(&ctx->chacha_ctx, data, seed, CHACHA_DRBG_SEED_LENGTH);
  82. else
  83. ECRYPT_keystream_bytes(&ctx->chacha_ctx, seed, CHACHA_DRBG_SEED_LENGTH);
  84. ECRYPT_keysetup(&ctx->chacha_ctx, seed, CHACHA_DRBG_KEY_LENGTH * 8, CHACHA_DRBG_IV_LENGTH * 8);
  85. ECRYPT_ivsetup(&ctx->chacha_ctx, seed + CHACHA_DRBG_KEY_LENGTH + CHACHA_DRBG_COUNTER_LENGTH);
  86. ECRYPT_ctrsetup(&ctx->chacha_ctx, seed + CHACHA_DRBG_KEY_LENGTH);
  87. memzero(seed, sizeof(seed));
  88. }
  89. void chacha_drbg_generate(CHACHA_DRBG_CTX* ctx, uint8_t* output, size_t output_length) {
  90. assert(output_length < 65536);
  91. assert(ctx->reseed_counter + 1 != 0);
  92. ECRYPT_keystream_bytes(&ctx->chacha_ctx, output, output_length);
  93. chacha_drbg_update(ctx, NULL);
  94. ctx->reseed_counter++;
  95. }
  96. void chacha_drbg_reseed(
  97. CHACHA_DRBG_CTX* ctx,
  98. const uint8_t* entropy,
  99. size_t entropy_length,
  100. const uint8_t* additional_input,
  101. size_t additional_input_length) {
  102. uint8_t seed[CHACHA_DRBG_SEED_LENGTH] = {0};
  103. derivation_function(
  104. entropy, entropy_length, additional_input, additional_input_length, seed, sizeof(seed));
  105. chacha_drbg_update(ctx, seed);
  106. memzero(seed, sizeof(seed));
  107. ctx->reseed_counter = 1;
  108. }