ctr_prng.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* ctr_prng.h - TinyCrypt interface to a CTR-PRNG implementation */
  2. /*
  3. * Copyright (c) 2016, Chris Morrison
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /**
  29. * @file
  30. * @brief Interface to a CTR-PRNG implementation.
  31. *
  32. * Overview: A pseudo-random number generator (PRNG) generates a sequence
  33. * of numbers that have a distribution close to the one expected
  34. * for a sequence of truly random numbers. The NIST Special
  35. * Publication 800-90A specifies several mechanisms to generate
  36. * sequences of pseudo random numbers, including the CTR-PRNG one
  37. * which is based on AES. TinyCrypt implements CTR-PRNG with
  38. * AES-128.
  39. *
  40. * Security: A cryptographically secure PRNG depends on the existence of an
  41. * entropy source to provide a truly random seed as well as the
  42. * security of the primitives used as the building blocks (AES-128
  43. * in this instance).
  44. *
  45. * Requires: - AES-128
  46. *
  47. * Usage: 1) call tc_ctr_prng_init to seed the prng context
  48. *
  49. * 2) call tc_ctr_prng_reseed to mix in additional entropy into
  50. * the prng context
  51. *
  52. * 3) call tc_ctr_prng_generate to output the pseudo-random data
  53. *
  54. * 4) call tc_ctr_prng_uninstantiate to zero out the prng context
  55. */
  56. #ifndef __TC_CTR_PRNG_H__
  57. #define __TC_CTR_PRNG_H__
  58. #include <tinycrypt/aes.h>
  59. #define TC_CTR_PRNG_RESEED_REQ -1
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. typedef struct {
  64. /* updated each time another BLOCKLEN_BYTES bytes are produced */
  65. uint8_t V[TC_AES_BLOCK_SIZE];
  66. /* updated whenever the PRNG is reseeded */
  67. struct tc_aes_key_sched_struct key;
  68. /* number of requests since initialization/reseeding */
  69. uint64_t reseedCount;
  70. } TCCtrPrng_t;
  71. /**
  72. * @brief CTR-PRNG initialization procedure
  73. * Initializes prng context with entropy and personalization string (if any)
  74. * @return returns TC_CRYPTO_SUCCESS (1)
  75. * returns TC_CRYPTO_FAIL (0) if:
  76. * ctx == NULL,
  77. * entropy == NULL,
  78. * entropyLen < (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE)
  79. * @note Only the first (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE) bytes of
  80. * both the entropy and personalization inputs are used -
  81. * supplying additional bytes has no effect.
  82. * @param ctx IN/OUT -- the PRNG context to initialize
  83. * @param entropy IN -- entropy used to seed the PRNG
  84. * @param entropyLen IN -- entropy length in bytes
  85. * @param personalization IN -- personalization string used to seed the PRNG
  86. * (may be null)
  87. * @param plen IN -- personalization length in bytes
  88. *
  89. */
  90. int tc_ctr_prng_init(TCCtrPrng_t * const ctx,
  91. uint8_t const * const entropy,
  92. unsigned int entropyLen,
  93. uint8_t const * const personalization,
  94. unsigned int pLen);
  95. /**
  96. * @brief CTR-PRNG reseed procedure
  97. * Mixes entropy and additional_input into the prng context
  98. * @return returns TC_CRYPTO_SUCCESS (1)
  99. * returns TC_CRYPTO_FAIL (0) if:
  100. * ctx == NULL,
  101. * entropy == NULL,
  102. * entropylen < (TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE)
  103. * @note It is better to reseed an existing prng context rather than
  104. * re-initialise, so that any existing entropy in the context is
  105. * presereved. This offers some protection against undetected failures
  106. * of the entropy source.
  107. * @note Assumes tc_ctr_prng_init has been called for ctx
  108. * @param ctx IN/OUT -- the PRNG state
  109. * @param entropy IN -- entropy to mix into the prng
  110. * @param entropylen IN -- length of entropy in bytes
  111. * @param additional_input IN -- additional input to the prng (may be null)
  112. * @param additionallen IN -- additional input length in bytes
  113. */
  114. int tc_ctr_prng_reseed(TCCtrPrng_t * const ctx,
  115. uint8_t const * const entropy,
  116. unsigned int entropyLen,
  117. uint8_t const * const additional_input,
  118. unsigned int additionallen);
  119. /**
  120. * @brief CTR-PRNG generate procedure
  121. * Generates outlen pseudo-random bytes into out buffer, updates prng
  122. * @return returns TC_CRYPTO_SUCCESS (1)
  123. * returns TC_CTR_PRNG_RESEED_REQ (-1) if a reseed is needed
  124. * returns TC_CRYPTO_FAIL (0) if:
  125. * ctx == NULL,
  126. * out == NULL,
  127. * outlen >= 2^16
  128. * @note Assumes tc_ctr_prng_init has been called for ctx
  129. * @param ctx IN/OUT -- the PRNG context
  130. * @param additional_input IN -- additional input to the prng (may be null)
  131. * @param additionallen IN -- additional input length in bytes
  132. * @param out IN/OUT -- buffer to receive output
  133. * @param outlen IN -- size of out buffer in bytes
  134. */
  135. int tc_ctr_prng_generate(TCCtrPrng_t * const ctx,
  136. uint8_t const * const additional_input,
  137. unsigned int additionallen,
  138. uint8_t * const out,
  139. unsigned int outlen);
  140. /**
  141. * @brief CTR-PRNG uninstantiate procedure
  142. * Zeroes the internal state of the supplied prng context
  143. * @return none
  144. * @param ctx IN/OUT -- the PRNG context
  145. */
  146. void tc_ctr_prng_uninstantiate(TCCtrPrng_t * const ctx);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif /* __TC_CTR_PRNG_H__ */