2
0

fips_rand_lcl.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* fips/rand/fips_rand_lcl.h */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. typedef struct drbg_hash_ctx_st DRBG_HASH_CTX;
  54. typedef struct drbg_ctr_ctx_st DRBG_CTR_CTX;
  55. /* 888 bits from 10.1 table 2 */
  56. #define HASH_PRNG_MAX_SEEDLEN 111
  57. struct drbg_hash_ctx_st
  58. {
  59. const EVP_MD *md;
  60. EVP_MD_CTX mctx;
  61. unsigned char V[HASH_PRNG_MAX_SEEDLEN];
  62. unsigned char C[HASH_PRNG_MAX_SEEDLEN];
  63. /* Temporary value storage: should always exceed max digest length */
  64. unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
  65. };
  66. struct drbg_ctr_ctx_st
  67. {
  68. AES_KEY ks;
  69. size_t keylen;
  70. unsigned char K[32];
  71. unsigned char V[16];
  72. /* Temp variables used by derivation function */
  73. AES_KEY df_ks;
  74. AES_KEY df_kxks;
  75. /* Temporary block storage used by ctr_df */
  76. unsigned char bltmp[16];
  77. size_t bltmp_pos;
  78. unsigned char KX[48];
  79. };
  80. /* DRBG flags */
  81. /* Functions shouldn't call err library */
  82. #define DRBG_FLAG_NOERR 0x4
  83. /* DRBG status values */
  84. /* not initialised */
  85. #define DRBG_STATUS_UNINITIALISED 0
  86. /* ok and ready to generate random bits */
  87. #define DRBG_STATUS_READY 1
  88. /* reseed required */
  89. #define DRBG_STATUS_RESEED 2
  90. /* fatal error condition */
  91. #define DRBG_STATUS_ERROR 3
  92. /* A default maximum length: larger than any reasonable value used in pratice */
  93. #define DRBG_MAX_LENGTH 0x7ffffff0
  94. /* Maximum DRBG block length: all md sizes are bigger than cipher blocks sizes
  95. * so use max digest length.
  96. */
  97. #define DRBG_MAX_BLOCK EVP_MAX_MD_SIZE
  98. #define DRBG_HEALTH_INTERVAL (1 << 24)
  99. /* DRBG context structure */
  100. struct drbg_ctx_st
  101. {
  102. /* First types common to all implementations */
  103. /* DRBG type: a NID for the underlying algorithm */
  104. int type;
  105. /* Various flags */
  106. unsigned int flags;
  107. /* Used for periodic health checks */
  108. int health_check_cnt, health_check_interval;
  109. /* The following parameters are setup by mechanism drbg_init() call */
  110. int strength;
  111. size_t blocklength;
  112. size_t max_request;
  113. size_t min_entropy, max_entropy;
  114. size_t min_nonce, max_nonce;
  115. size_t max_pers, max_adin;
  116. unsigned int reseed_counter;
  117. unsigned int reseed_interval;
  118. size_t seedlen;
  119. int status;
  120. /* Application data: typically used by test get_entropy */
  121. void *app_data;
  122. /* Implementation specific structures */
  123. union
  124. {
  125. DRBG_HASH_CTX hash;
  126. DRBG_CTR_CTX ctr;
  127. } d;
  128. /* Initialiase PRNG and setup callbacks below */
  129. int (*init)(DRBG_CTX *ctx, int nid, int security, unsigned int flags);
  130. /* Intantiate PRNG */
  131. int (*instantiate)(DRBG_CTX *ctx,
  132. const unsigned char *ent, size_t entlen,
  133. const unsigned char *nonce, size_t noncelen,
  134. const unsigned char *pers, size_t perslen);
  135. /* reseed */
  136. int (*reseed)(DRBG_CTX *ctx,
  137. const unsigned char *ent, size_t entlen,
  138. const unsigned char *adin, size_t adinlen);
  139. /* generat output */
  140. int (*generate)(DRBG_CTX *ctx,
  141. unsigned char *out, size_t outlen,
  142. const unsigned char *adin, size_t adinlen);
  143. /* uninstantiate */
  144. int (*uninstantiate)(DRBG_CTX *ctx);
  145. /* Entropy source block length */
  146. size_t entropy_blocklen;
  147. /* entropy gathering function */
  148. size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
  149. int entropy, size_t min_len, size_t max_len);
  150. /* Indicates we have finished with entropy buffer */
  151. void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
  152. /* nonce gathering function */
  153. size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
  154. int entropy, size_t min_len, size_t max_len);
  155. /* Indicates we have finished with nonce buffer */
  156. void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
  157. /* Continuous random number test temporary area */
  158. /* Last block */
  159. unsigned char lb[EVP_MAX_MD_SIZE];
  160. /* set if lb is valid */
  161. int lb_valid;
  162. /* Callbacks used when called through RAND interface */
  163. /* Get any additional input for generate */
  164. size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout);
  165. void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen);
  166. /* Callback for RAND_seed(), RAND_add() */
  167. int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num);
  168. int (*rand_add_cb)(DRBG_CTX *ctx,
  169. const void *buf, int num, double entropy);
  170. };
  171. int fips_drbg_ctr_init(DRBG_CTX *dctx);
  172. int fips_drbg_hash_init(DRBG_CTX *dctx);
  173. int fips_drbg_kat(DRBG_CTX *dctx, int nid, unsigned int flags);
  174. int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out);