hmac_prng.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* hmac_prng.c - TinyCrypt implementation of HMAC-PRNG */
  2. /*
  3. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Intel Corporation nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <tinycrypt/hmac_prng.h>
  32. #include <tinycrypt/hmac.h>
  33. #include <tinycrypt/constants.h>
  34. #include <tinycrypt/utils.h>
  35. /*
  36. * min bytes in the seed string.
  37. * MIN_SLEN*8 must be at least the expected security level.
  38. */
  39. static const unsigned int MIN_SLEN = 32;
  40. /*
  41. * max bytes in the seed string;
  42. * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
  43. */
  44. static const unsigned int MAX_SLEN = UINT32_MAX;
  45. /*
  46. * max bytes in the personalization string;
  47. * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
  48. */
  49. static const unsigned int MAX_PLEN = UINT32_MAX;
  50. /*
  51. * max bytes in the additional_info string;
  52. * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
  53. */
  54. static const unsigned int MAX_ALEN = UINT32_MAX;
  55. /*
  56. * max number of generates between re-seeds;
  57. * TinyCrypt accepts up to (2^32 - 1) which is the maximal value of
  58. * a 32-bit unsigned int variable, while SP800-90A specifies a maximum of 2^48.
  59. */
  60. static const unsigned int MAX_GENS = UINT32_MAX;
  61. /*
  62. * maximum bytes per generate call;
  63. * SP800-90A specifies a maximum up to 2^19.
  64. */
  65. static const unsigned int MAX_OUT = (1 << 19);
  66. /*
  67. * Assumes: prng != NULL, e != NULL, len >= 0.
  68. */
  69. static void update(TCHmacPrng_t prng, const uint8_t *e, unsigned int len)
  70. {
  71. const uint8_t separator0 = 0x00;
  72. const uint8_t separator1 = 0x01;
  73. /* use current state, e and separator 0 to compute a new prng key: */
  74. (void)tc_hmac_init(&prng->h);
  75. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  76. (void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0));
  77. (void)tc_hmac_update(&prng->h, e, len);
  78. (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
  79. /* configure the new prng key into the prng's instance of hmac */
  80. (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  81. /* use the new key to compute a new state variable v */
  82. (void)tc_hmac_init(&prng->h);
  83. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  84. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  85. /* use current state, e and separator 1 to compute a new prng key: */
  86. (void)tc_hmac_init(&prng->h);
  87. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  88. (void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1));
  89. (void)tc_hmac_update(&prng->h, e, len);
  90. (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
  91. /* configure the new prng key into the prng's instance of hmac */
  92. (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  93. /* use the new key to compute a new state variable v */
  94. (void)tc_hmac_init(&prng->h);
  95. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  96. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  97. }
  98. int tc_hmac_prng_init(TCHmacPrng_t prng,
  99. const uint8_t *personalization,
  100. unsigned int plen)
  101. {
  102. /* input sanity check: */
  103. if (prng == (TCHmacPrng_t) 0 ||
  104. personalization == (uint8_t *) 0 ||
  105. plen > MAX_PLEN) {
  106. return TC_CRYPTO_FAIL;
  107. }
  108. /* put the generator into a known state: */
  109. _set(prng->key, 0x00, sizeof(prng->key));
  110. _set(prng->v, 0x01, sizeof(prng->v));
  111. tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  112. /* update assumes SOME key has been configured into HMAC */
  113. update(prng, personalization, plen);
  114. /* force a reseed before allowing tc_hmac_prng_generate to succeed: */
  115. prng->countdown = 0;
  116. return TC_CRYPTO_SUCCESS;
  117. }
  118. int tc_hmac_prng_reseed(TCHmacPrng_t prng,
  119. const uint8_t *seed,
  120. unsigned int seedlen,
  121. const uint8_t *additional_input,
  122. unsigned int additionallen)
  123. {
  124. /* input sanity check: */
  125. if (prng == (TCHmacPrng_t) 0 ||
  126. seed == (const uint8_t *) 0 ||
  127. seedlen < MIN_SLEN ||
  128. seedlen > MAX_SLEN) {
  129. return TC_CRYPTO_FAIL;
  130. }
  131. if (additional_input != (const uint8_t *) 0) {
  132. /*
  133. * Abort if additional_input is provided but has inappropriate
  134. * length
  135. */
  136. if (additionallen == 0 ||
  137. additionallen > MAX_ALEN) {
  138. return TC_CRYPTO_FAIL;
  139. } else {
  140. /* call update for the seed and additional_input */
  141. update(prng, seed, seedlen);
  142. update(prng, additional_input, additionallen);
  143. }
  144. } else {
  145. /* call update only for the seed */
  146. update(prng, seed, seedlen);
  147. }
  148. /* ... and enable hmac_prng_generate */
  149. prng->countdown = MAX_GENS;
  150. return TC_CRYPTO_SUCCESS;
  151. }
  152. int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
  153. {
  154. unsigned int bufferlen;
  155. /* input sanity check: */
  156. if (out == (uint8_t *) 0 ||
  157. prng == (TCHmacPrng_t) 0 ||
  158. outlen == 0 ||
  159. outlen > MAX_OUT) {
  160. return TC_CRYPTO_FAIL;
  161. } else if (prng->countdown == 0) {
  162. return TC_HMAC_PRNG_RESEED_REQ;
  163. }
  164. prng->countdown--;
  165. while (outlen != 0) {
  166. /* operate HMAC in OFB mode to create "random" outputs */
  167. (void)tc_hmac_init(&prng->h);
  168. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  169. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  170. bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ?
  171. outlen : TC_SHA256_DIGEST_SIZE;
  172. (void)_copy(out, bufferlen, prng->v, bufferlen);
  173. out += bufferlen;
  174. outlen = (outlen > TC_SHA256_DIGEST_SIZE) ?
  175. (outlen - TC_SHA256_DIGEST_SIZE) : 0;
  176. }
  177. /* block future PRNG compromises from revealing past state */
  178. update(prng, prng->v, TC_SHA256_DIGEST_SIZE);
  179. return TC_CRYPTO_SUCCESS;
  180. }