hmac_prng.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* hmac_prng.h - TinyCrypt interface to an HMAC-PRNG implementation */
  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. /**
  32. * @file
  33. * @brief Interface to an HMAC-PRNG implementation.
  34. *
  35. * Overview: A pseudo-random number generator (PRNG) generates a sequence
  36. * of numbers that have a distribution close to the one expected
  37. * for a sequence of truly random numbers. The NIST Special
  38. * Publication 800-90A specifies several mechanisms to generate
  39. * sequences of pseudo random numbers, including the HMAC-PRNG one
  40. * which is based on HMAC. TinyCrypt implements HMAC-PRNG with
  41. * certain modifications from the NIST SP 800-90A spec.
  42. *
  43. * Security: A cryptographically secure PRNG depends on the existence of an
  44. * entropy source to provide a truly random seed as well as the
  45. * security of the primitives used as the building blocks (HMAC and
  46. * SHA256, for TinyCrypt).
  47. *
  48. * The NIST SP 800-90A standard tolerates a null personalization,
  49. * while TinyCrypt requires a non-null personalization. This is
  50. * because a personalization string (the host name concatenated
  51. * with a time stamp, for example) is easily computed and might be
  52. * the last line of defense against failure of the entropy source.
  53. *
  54. * Requires: - SHA-256
  55. * - HMAC
  56. *
  57. * Usage: 1) call tc_hmac_prng_init to set the HMAC key and process the
  58. * personalization data.
  59. *
  60. * 2) call tc_hmac_prng_reseed to process the seed and additional
  61. * input.
  62. *
  63. * 3) call tc_hmac_prng_generate to out put the pseudo-random data.
  64. */
  65. #ifndef __TC_HMAC_PRNG_H__
  66. #define __TC_HMAC_PRNG_H__
  67. #include <tinycrypt/sha256.h>
  68. #include <tinycrypt/hmac.h>
  69. #ifdef __cplusplus
  70. extern "C" {
  71. #endif
  72. #define TC_HMAC_PRNG_RESEED_REQ -1
  73. struct tc_hmac_prng_struct {
  74. /* the HMAC instance for this PRNG */
  75. struct tc_hmac_state_struct h;
  76. /* the PRNG key */
  77. uint8_t key[TC_SHA256_DIGEST_SIZE];
  78. /* PRNG state */
  79. uint8_t v[TC_SHA256_DIGEST_SIZE];
  80. /* calls to tc_hmac_prng_generate left before re-seed */
  81. unsigned int countdown;
  82. };
  83. typedef struct tc_hmac_prng_struct *TCHmacPrng_t;
  84. /**
  85. * @brief HMAC-PRNG initialization procedure
  86. * Initializes prng with personalization, disables tc_hmac_prng_generate
  87. * @return returns TC_CRYPTO_SUCCESS (1)
  88. * returns TC_CRYPTO_FAIL (0) if:
  89. * prng == NULL,
  90. * personalization == NULL,
  91. * plen > MAX_PLEN
  92. * @note Assumes: - personalization != NULL.
  93. * The personalization is a platform unique string (e.g., the host
  94. * name) and is the last line of defense against failure of the
  95. * entropy source
  96. * @warning NIST SP 800-90A specifies 3 items as seed material during
  97. * initialization: entropy seed, personalization, and an optional
  98. * nonce. TinyCrypts requires instead a non-null personalization
  99. * (which is easily computed) and indirectly requires an entropy
  100. * seed (since the reseed function is mandatorily called after
  101. * init)
  102. * @param prng IN/OUT -- the PRNG state to initialize
  103. * @param personalization IN -- personalization string
  104. * @param plen IN -- personalization length in bytes
  105. */
  106. int tc_hmac_prng_init(TCHmacPrng_t prng,
  107. const uint8_t *personalization,
  108. unsigned int plen);
  109. /**
  110. * @brief HMAC-PRNG reseed procedure
  111. * Mixes seed into prng, enables tc_hmac_prng_generate
  112. * @return returns TC_CRYPTO_SUCCESS (1)
  113. * returns TC_CRYPTO_FAIL (0) if:
  114. * prng == NULL,
  115. * seed == NULL,
  116. * seedlen < MIN_SLEN,
  117. * seendlen > MAX_SLEN,
  118. * additional_input != (const uint8_t *) 0 && additionallen == 0,
  119. * additional_input != (const uint8_t *) 0 && additionallen > MAX_ALEN
  120. * @note Assumes:- tc_hmac_prng_init has been called for prng
  121. * - seed has sufficient entropy.
  122. *
  123. * @param prng IN/OUT -- the PRNG state
  124. * @param seed IN -- entropy to mix into the prng
  125. * @param seedlen IN -- length of seed in bytes
  126. * @param additional_input IN -- additional input to the prng
  127. * @param additionallen IN -- additional input length in bytes
  128. */
  129. int tc_hmac_prng_reseed(TCHmacPrng_t prng, const uint8_t *seed,
  130. unsigned int seedlen, const uint8_t *additional_input,
  131. unsigned int additionallen);
  132. /**
  133. * @brief HMAC-PRNG generate procedure
  134. * Generates outlen pseudo-random bytes into out buffer, updates prng
  135. * @return returns TC_CRYPTO_SUCCESS (1)
  136. * returns TC_HMAC_PRNG_RESEED_REQ (-1) if a reseed is needed
  137. * returns TC_CRYPTO_FAIL (0) if:
  138. * out == NULL,
  139. * prng == NULL,
  140. * outlen == 0,
  141. * outlen >= MAX_OUT
  142. * @note Assumes tc_hmac_prng_init has been called for prng
  143. * @param out IN/OUT -- buffer to receive output
  144. * @param outlen IN -- size of out buffer in bytes
  145. * @param prng IN/OUT -- the PRNG state
  146. */
  147. int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng);
  148. #ifdef __cplusplus
  149. }
  150. #endif
  151. #endif /* __TC_HMAC_PRNG_H__ */