ctrdrbg.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Copyright (c) 2017, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <openssl/rand.h>
  15. #include <openssl/type_check.h>
  16. #include <openssl/mem.h>
  17. #include "internal.h"
  18. #include "../cipher/internal.h"
  19. // Section references in this file refer to SP 800-90Ar1:
  20. // http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-90Ar1.pdf
  21. // See table 3.
  22. static const uint64_t kMaxReseedCount = UINT64_C(1) << 48;
  23. int CTR_DRBG_init(CTR_DRBG_STATE *drbg,
  24. const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
  25. const uint8_t *personalization, size_t personalization_len) {
  26. // Section 10.2.1.3.1
  27. if (personalization_len > CTR_DRBG_ENTROPY_LEN) {
  28. return 0;
  29. }
  30. uint8_t seed_material[CTR_DRBG_ENTROPY_LEN];
  31. OPENSSL_memcpy(seed_material, entropy, CTR_DRBG_ENTROPY_LEN);
  32. for (size_t i = 0; i < personalization_len; i++) {
  33. seed_material[i] ^= personalization[i];
  34. }
  35. // Section 10.2.1.2
  36. // kInitMask is the result of encrypting blocks with big-endian value 1, 2
  37. // and 3 with the all-zero AES-256 key.
  38. static const uint8_t kInitMask[CTR_DRBG_ENTROPY_LEN] = {
  39. 0x53, 0x0f, 0x8a, 0xfb, 0xc7, 0x45, 0x36, 0xb9, 0xa9, 0x63, 0xb4, 0xf1,
  40. 0xc4, 0xcb, 0x73, 0x8b, 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
  41. 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18, 0x72, 0x60, 0x03, 0xca,
  42. 0x37, 0xa6, 0x2a, 0x74, 0xd1, 0xa2, 0xf5, 0x8e, 0x75, 0x06, 0x35, 0x8e,
  43. };
  44. for (size_t i = 0; i < sizeof(kInitMask); i++) {
  45. seed_material[i] ^= kInitMask[i];
  46. }
  47. drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, seed_material, 32);
  48. OPENSSL_memcpy(drbg->counter.bytes, seed_material + 32, 16);
  49. drbg->reseed_counter = 1;
  50. return 1;
  51. }
  52. OPENSSL_COMPILE_ASSERT(CTR_DRBG_ENTROPY_LEN % AES_BLOCK_SIZE == 0,
  53. not_a_multiple_of_block_size);
  54. // ctr_inc adds |n| to the last four bytes of |drbg->counter|, treated as a
  55. // big-endian number.
  56. static void ctr32_add(CTR_DRBG_STATE *drbg, uint32_t n) {
  57. drbg->counter.words[3] =
  58. CRYPTO_bswap4(CRYPTO_bswap4(drbg->counter.words[3]) + n);
  59. }
  60. static int ctr_drbg_update(CTR_DRBG_STATE *drbg, const uint8_t *data,
  61. size_t data_len) {
  62. // Per section 10.2.1.2, |data_len| must be |CTR_DRBG_ENTROPY_LEN|. Here, we
  63. // allow shorter inputs and right-pad them with zeros. This is equivalent to
  64. // the specified algorithm but saves a copy in |CTR_DRBG_generate|.
  65. if (data_len > CTR_DRBG_ENTROPY_LEN) {
  66. return 0;
  67. }
  68. uint8_t temp[CTR_DRBG_ENTROPY_LEN];
  69. for (size_t i = 0; i < CTR_DRBG_ENTROPY_LEN; i += AES_BLOCK_SIZE) {
  70. ctr32_add(drbg, 1);
  71. drbg->block(drbg->counter.bytes, temp + i, &drbg->ks);
  72. }
  73. for (size_t i = 0; i < data_len; i++) {
  74. temp[i] ^= data[i];
  75. }
  76. drbg->ctr = aes_ctr_set_key(&drbg->ks, NULL, &drbg->block, temp, 32);
  77. OPENSSL_memcpy(drbg->counter.bytes, temp + 32, 16);
  78. return 1;
  79. }
  80. int CTR_DRBG_reseed(CTR_DRBG_STATE *drbg,
  81. const uint8_t entropy[CTR_DRBG_ENTROPY_LEN],
  82. const uint8_t *additional_data,
  83. size_t additional_data_len) {
  84. // Section 10.2.1.4
  85. uint8_t entropy_copy[CTR_DRBG_ENTROPY_LEN];
  86. if (additional_data_len > 0) {
  87. if (additional_data_len > CTR_DRBG_ENTROPY_LEN) {
  88. return 0;
  89. }
  90. OPENSSL_memcpy(entropy_copy, entropy, CTR_DRBG_ENTROPY_LEN);
  91. for (size_t i = 0; i < additional_data_len; i++) {
  92. entropy_copy[i] ^= additional_data[i];
  93. }
  94. entropy = entropy_copy;
  95. }
  96. if (!ctr_drbg_update(drbg, entropy, CTR_DRBG_ENTROPY_LEN)) {
  97. return 0;
  98. }
  99. drbg->reseed_counter = 1;
  100. return 1;
  101. }
  102. int CTR_DRBG_generate(CTR_DRBG_STATE *drbg, uint8_t *out, size_t out_len,
  103. const uint8_t *additional_data,
  104. size_t additional_data_len) {
  105. // See 9.3.1
  106. if (out_len > CTR_DRBG_MAX_GENERATE_LENGTH) {
  107. return 0;
  108. }
  109. // See 10.2.1.5.1
  110. if (drbg->reseed_counter > kMaxReseedCount) {
  111. return 0;
  112. }
  113. if (additional_data_len != 0 &&
  114. !ctr_drbg_update(drbg, additional_data, additional_data_len)) {
  115. return 0;
  116. }
  117. // kChunkSize is used to interact better with the cache. Since the AES-CTR
  118. // code assumes that it's encrypting rather than just writing keystream, the
  119. // buffer has to be zeroed first. Without chunking, large reads would zero
  120. // the whole buffer, flushing the L1 cache, and then do another pass (missing
  121. // the cache every time) to “encrypt” it. The code can avoid this by
  122. // chunking.
  123. static const size_t kChunkSize = 8 * 1024;
  124. while (out_len >= AES_BLOCK_SIZE) {
  125. size_t todo = kChunkSize;
  126. if (todo > out_len) {
  127. todo = out_len;
  128. }
  129. todo &= ~(AES_BLOCK_SIZE-1);
  130. const size_t num_blocks = todo / AES_BLOCK_SIZE;
  131. if (drbg->ctr) {
  132. OPENSSL_memset(out, 0, todo);
  133. ctr32_add(drbg, 1);
  134. drbg->ctr(out, out, num_blocks, &drbg->ks, drbg->counter.bytes);
  135. ctr32_add(drbg, num_blocks - 1);
  136. } else {
  137. for (size_t i = 0; i < todo; i += AES_BLOCK_SIZE) {
  138. ctr32_add(drbg, 1);
  139. drbg->block(drbg->counter.bytes, out + i, &drbg->ks);
  140. }
  141. }
  142. out += todo;
  143. out_len -= todo;
  144. }
  145. if (out_len > 0) {
  146. uint8_t block[AES_BLOCK_SIZE];
  147. ctr32_add(drbg, 1);
  148. drbg->block(drbg->counter.bytes, block, &drbg->ks);
  149. OPENSSL_memcpy(out, block, out_len);
  150. }
  151. // Right-padding |additional_data| in step 2.2 is handled implicitly by
  152. // |ctr_drbg_update|, to save a copy.
  153. if (!ctr_drbg_update(drbg, additional_data, additional_data_len)) {
  154. return 0;
  155. }
  156. drbg->reseed_counter++;
  157. return 1;
  158. }
  159. void CTR_DRBG_clear(CTR_DRBG_STATE *drbg) {
  160. OPENSSL_cleanse(drbg, sizeof(CTR_DRBG_STATE));
  161. }