hmac_prng.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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
  68. */
  69. static void update(TCHmacPrng_t prng, const uint8_t *data, unsigned int datalen, const uint8_t *additional_data, unsigned int additional_datalen)
  70. {
  71. const uint8_t separator0 = 0x00;
  72. const uint8_t separator1 = 0x01;
  73. /* configure the new prng key into the prng's instance of hmac */
  74. tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  75. /* use current state, e and separator 0 to compute a new prng key: */
  76. (void)tc_hmac_init(&prng->h);
  77. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  78. (void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0));
  79. if (data && datalen)
  80. (void)tc_hmac_update(&prng->h, data, datalen);
  81. if (additional_data && additional_datalen)
  82. (void)tc_hmac_update(&prng->h, additional_data, additional_datalen);
  83. (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
  84. /* configure the new prng key into the prng's instance of hmac */
  85. (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  86. /* use the new key to compute a new state variable v */
  87. (void)tc_hmac_init(&prng->h);
  88. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  89. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  90. if (data == 0 || datalen == 0)
  91. return;
  92. /* configure the new prng key into the prng's instance of hmac */
  93. tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  94. /* use current state, e and separator 1 to compute a new prng key: */
  95. (void)tc_hmac_init(&prng->h);
  96. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  97. (void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1));
  98. (void)tc_hmac_update(&prng->h, data, datalen);
  99. if (additional_data && additional_datalen)
  100. (void)tc_hmac_update(&prng->h, additional_data, additional_datalen);
  101. (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
  102. /* configure the new prng key into the prng's instance of hmac */
  103. (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  104. /* use the new key to compute a new state variable v */
  105. (void)tc_hmac_init(&prng->h);
  106. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  107. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  108. }
  109. int tc_hmac_prng_init(TCHmacPrng_t prng,
  110. const uint8_t *personalization,
  111. unsigned int plen)
  112. {
  113. /* input sanity check: */
  114. if (prng == (TCHmacPrng_t) 0 ||
  115. personalization == (uint8_t *) 0 ||
  116. plen > MAX_PLEN) {
  117. return TC_CRYPTO_FAIL;
  118. }
  119. /* put the generator into a known state: */
  120. _set(prng->key, 0x00, sizeof(prng->key));
  121. _set(prng->v, 0x01, sizeof(prng->v));
  122. update(prng, personalization, plen, 0, 0);
  123. /* force a reseed before allowing tc_hmac_prng_generate to succeed: */
  124. prng->countdown = 0;
  125. return TC_CRYPTO_SUCCESS;
  126. }
  127. int tc_hmac_prng_reseed(TCHmacPrng_t prng,
  128. const uint8_t *seed,
  129. unsigned int seedlen,
  130. const uint8_t *additional_input,
  131. unsigned int additionallen)
  132. {
  133. /* input sanity check: */
  134. if (prng == (TCHmacPrng_t) 0 ||
  135. seed == (const uint8_t *) 0 ||
  136. seedlen < MIN_SLEN ||
  137. seedlen > MAX_SLEN) {
  138. return TC_CRYPTO_FAIL;
  139. }
  140. if (additional_input != (const uint8_t *) 0) {
  141. /*
  142. * Abort if additional_input is provided but has inappropriate
  143. * length
  144. */
  145. if (additionallen == 0 ||
  146. additionallen > MAX_ALEN) {
  147. return TC_CRYPTO_FAIL;
  148. } else {
  149. /* call update for the seed and additional_input */
  150. update(prng, seed, seedlen, additional_input, additionallen);
  151. }
  152. } else {
  153. /* call update only for the seed */
  154. update(prng, seed, seedlen, 0, 0);
  155. }
  156. /* ... and enable hmac_prng_generate */
  157. prng->countdown = MAX_GENS;
  158. return TC_CRYPTO_SUCCESS;
  159. }
  160. int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng)
  161. {
  162. unsigned int bufferlen;
  163. /* input sanity check: */
  164. if (out == (uint8_t *) 0 ||
  165. prng == (TCHmacPrng_t) 0 ||
  166. outlen == 0 ||
  167. outlen > MAX_OUT) {
  168. return TC_CRYPTO_FAIL;
  169. } else if (prng->countdown == 0) {
  170. return TC_HMAC_PRNG_RESEED_REQ;
  171. }
  172. prng->countdown--;
  173. while (outlen != 0) {
  174. /* configure the new prng key into the prng's instance of hmac */
  175. tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
  176. /* operate HMAC in OFB mode to create "random" outputs */
  177. (void)tc_hmac_init(&prng->h);
  178. (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
  179. (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
  180. bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ?
  181. outlen : TC_SHA256_DIGEST_SIZE;
  182. (void)_copy(out, bufferlen, prng->v, bufferlen);
  183. out += bufferlen;
  184. outlen = (outlen > TC_SHA256_DIGEST_SIZE) ?
  185. (outlen - TC_SHA256_DIGEST_SIZE) : 0;
  186. }
  187. /* block future PRNG compromises from revealing past state */
  188. update(prng, 0, 0, 0, 0);
  189. return TC_CRYPTO_SUCCESS;
  190. }