fips_rand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* ====================================================================
  2. * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. /*
  50. * This is a FIPS approved AES PRNG based on ANSI X9.31 A.2.4.
  51. */
  52. #include "e_os.h"
  53. /*
  54. * If we don't define _XOPEN_SOURCE_EXTENDED, struct timeval won't be defined
  55. * and gettimeofday() won't be declared with strict compilers like DEC C in
  56. * ANSI C mode.
  57. */
  58. #ifndef _XOPEN_SOURCE_EXTENDED
  59. # define _XOPEN_SOURCE_EXTENDED 1
  60. #endif
  61. #include <openssl/rand.h>
  62. #include <openssl/aes.h>
  63. #include <openssl/err.h>
  64. #include <openssl/fips_rand.h>
  65. #ifndef OPENSSL_SYS_WIN32
  66. # include <sys/time.h>
  67. #endif
  68. #include <assert.h>
  69. #ifndef OPENSSL_SYS_WIN32
  70. # ifdef OPENSSL_UNISTD
  71. # include OPENSSL_UNISTD
  72. # else
  73. # include <unistd.h>
  74. # endif
  75. #endif
  76. #include <string.h>
  77. #include <openssl/fips.h>
  78. #include "fips_locl.h"
  79. #ifdef OPENSSL_FIPS
  80. void *OPENSSL_stderr(void);
  81. # define AES_BLOCK_LENGTH 16
  82. /* AES FIPS PRNG implementation */
  83. typedef struct {
  84. int seeded;
  85. int keyed;
  86. int test_mode;
  87. int second;
  88. int error;
  89. unsigned long counter;
  90. AES_KEY ks;
  91. int vpos;
  92. /* Temporary storage for key if it equals seed length */
  93. unsigned char tmp_key[AES_BLOCK_LENGTH];
  94. unsigned char V[AES_BLOCK_LENGTH];
  95. unsigned char DT[AES_BLOCK_LENGTH];
  96. unsigned char last[AES_BLOCK_LENGTH];
  97. } FIPS_PRNG_CTX;
  98. static FIPS_PRNG_CTX sctx;
  99. static int fips_prng_fail = 0;
  100. void FIPS_rng_stick(void)
  101. {
  102. fips_prng_fail = 1;
  103. }
  104. static void fips_rand_prng_reset(FIPS_PRNG_CTX * ctx)
  105. {
  106. ctx->seeded = 0;
  107. ctx->keyed = 0;
  108. ctx->test_mode = 0;
  109. ctx->counter = 0;
  110. ctx->second = 0;
  111. ctx->error = 0;
  112. ctx->vpos = 0;
  113. OPENSSL_cleanse(ctx->V, AES_BLOCK_LENGTH);
  114. OPENSSL_cleanse(&ctx->ks, sizeof(AES_KEY));
  115. }
  116. static int fips_set_prng_key(FIPS_PRNG_CTX * ctx,
  117. const unsigned char *key,
  118. FIPS_RAND_SIZE_T keylen)
  119. {
  120. FIPS_selftest_check();
  121. if (keylen != 16 && keylen != 24 && keylen != 32) {
  122. /* error: invalid key size */
  123. return 0;
  124. }
  125. AES_set_encrypt_key(key, keylen << 3, &ctx->ks);
  126. if (keylen == 16) {
  127. memcpy(ctx->tmp_key, key, 16);
  128. ctx->keyed = 2;
  129. } else
  130. ctx->keyed = 1;
  131. ctx->seeded = 0;
  132. ctx->second = 0;
  133. return 1;
  134. }
  135. static int fips_set_prng_seed(FIPS_PRNG_CTX * ctx,
  136. const unsigned char *seed,
  137. FIPS_RAND_SIZE_T seedlen)
  138. {
  139. int i;
  140. if (!ctx->keyed)
  141. return 0;
  142. /* In test mode seed is just supplied data */
  143. if (ctx->test_mode) {
  144. if (seedlen != AES_BLOCK_LENGTH)
  145. return 0;
  146. memcpy(ctx->V, seed, AES_BLOCK_LENGTH);
  147. ctx->seeded = 1;
  148. return 1;
  149. }
  150. /* Outside test mode XOR supplied data with existing seed */
  151. for (i = 0; i < seedlen; i++) {
  152. ctx->V[ctx->vpos++] ^= seed[i];
  153. if (ctx->vpos == AES_BLOCK_LENGTH) {
  154. ctx->vpos = 0;
  155. /*
  156. * Special case if first seed and key length equals block size
  157. * check key and seed do not match.
  158. */
  159. if (ctx->keyed == 2) {
  160. if (!memcmp(ctx->tmp_key, ctx->V, 16)) {
  161. RANDerr(RAND_F_FIPS_SET_PRNG_SEED,
  162. RAND_R_PRNG_SEED_MUST_NOT_MATCH_KEY);
  163. return 0;
  164. }
  165. OPENSSL_cleanse(ctx->tmp_key, 16);
  166. ctx->keyed = 1;
  167. }
  168. ctx->seeded = 1;
  169. }
  170. }
  171. return 1;
  172. }
  173. static int fips_set_test_mode(FIPS_PRNG_CTX * ctx)
  174. {
  175. if (ctx->keyed) {
  176. RANDerr(RAND_F_FIPS_SET_TEST_MODE, RAND_R_PRNG_KEYED);
  177. return 0;
  178. }
  179. ctx->test_mode = 1;
  180. return 1;
  181. }
  182. int FIPS_rand_test_mode(void)
  183. {
  184. return fips_set_test_mode(&sctx);
  185. }
  186. int FIPS_rand_set_dt(unsigned char *dt)
  187. {
  188. if (!sctx.test_mode) {
  189. RANDerr(RAND_F_FIPS_RAND_SET_DT, RAND_R_NOT_IN_TEST_MODE);
  190. return 0;
  191. }
  192. memcpy(sctx.DT, dt, AES_BLOCK_LENGTH);
  193. return 1;
  194. }
  195. static void fips_get_dt(FIPS_PRNG_CTX * ctx)
  196. {
  197. # ifdef OPENSSL_SYS_WIN32
  198. FILETIME ft;
  199. # else
  200. struct timeval tv;
  201. # endif
  202. unsigned char *buf = ctx->DT;
  203. # ifndef GETPID_IS_MEANINGLESS
  204. unsigned long pid;
  205. # endif
  206. # ifdef OPENSSL_SYS_WIN32
  207. GetSystemTimeAsFileTime(&ft);
  208. buf[0] = (unsigned char)(ft.dwHighDateTime & 0xff);
  209. buf[1] = (unsigned char)((ft.dwHighDateTime >> 8) & 0xff);
  210. buf[2] = (unsigned char)((ft.dwHighDateTime >> 16) & 0xff);
  211. buf[3] = (unsigned char)((ft.dwHighDateTime >> 24) & 0xff);
  212. buf[4] = (unsigned char)(ft.dwLowDateTime & 0xff);
  213. buf[5] = (unsigned char)((ft.dwLowDateTime >> 8) & 0xff);
  214. buf[6] = (unsigned char)((ft.dwLowDateTime >> 16) & 0xff);
  215. buf[7] = (unsigned char)((ft.dwLowDateTime >> 24) & 0xff);
  216. # else
  217. gettimeofday(&tv, NULL);
  218. buf[0] = (unsigned char)(tv.tv_sec & 0xff);
  219. buf[1] = (unsigned char)((tv.tv_sec >> 8) & 0xff);
  220. buf[2] = (unsigned char)((tv.tv_sec >> 16) & 0xff);
  221. buf[3] = (unsigned char)((tv.tv_sec >> 24) & 0xff);
  222. buf[4] = (unsigned char)(tv.tv_usec & 0xff);
  223. buf[5] = (unsigned char)((tv.tv_usec >> 8) & 0xff);
  224. buf[6] = (unsigned char)((tv.tv_usec >> 16) & 0xff);
  225. buf[7] = (unsigned char)((tv.tv_usec >> 24) & 0xff);
  226. # endif
  227. buf[8] = (unsigned char)(ctx->counter & 0xff);
  228. buf[9] = (unsigned char)((ctx->counter >> 8) & 0xff);
  229. buf[10] = (unsigned char)((ctx->counter >> 16) & 0xff);
  230. buf[11] = (unsigned char)((ctx->counter >> 24) & 0xff);
  231. ctx->counter++;
  232. # ifndef GETPID_IS_MEANINGLESS
  233. pid = (unsigned long)getpid();
  234. buf[12] = (unsigned char)(pid & 0xff);
  235. buf[13] = (unsigned char)((pid >> 8) & 0xff);
  236. buf[14] = (unsigned char)((pid >> 16) & 0xff);
  237. buf[15] = (unsigned char)((pid >> 24) & 0xff);
  238. # endif
  239. }
  240. static int fips_rand(FIPS_PRNG_CTX * ctx,
  241. unsigned char *out, FIPS_RAND_SIZE_T outlen)
  242. {
  243. unsigned char R[AES_BLOCK_LENGTH], I[AES_BLOCK_LENGTH];
  244. unsigned char tmp[AES_BLOCK_LENGTH];
  245. int i;
  246. if (ctx->error) {
  247. RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_ERROR);
  248. return 0;
  249. }
  250. if (!ctx->keyed) {
  251. RANDerr(RAND_F_FIPS_RAND, RAND_R_NO_KEY_SET);
  252. return 0;
  253. }
  254. if (!ctx->seeded) {
  255. RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_NOT_SEEDED);
  256. return 0;
  257. }
  258. for (;;) {
  259. if (!ctx->test_mode)
  260. fips_get_dt(ctx);
  261. AES_encrypt(ctx->DT, I, &ctx->ks);
  262. for (i = 0; i < AES_BLOCK_LENGTH; i++)
  263. tmp[i] = I[i] ^ ctx->V[i];
  264. AES_encrypt(tmp, R, &ctx->ks);
  265. for (i = 0; i < AES_BLOCK_LENGTH; i++)
  266. tmp[i] = R[i] ^ I[i];
  267. AES_encrypt(tmp, ctx->V, &ctx->ks);
  268. /* Continuous PRNG test */
  269. if (ctx->second) {
  270. if (fips_prng_fail)
  271. memcpy(ctx->last, R, AES_BLOCK_LENGTH);
  272. if (!memcmp(R, ctx->last, AES_BLOCK_LENGTH)) {
  273. RANDerr(RAND_F_FIPS_RAND, RAND_R_PRNG_STUCK);
  274. ctx->error = 1;
  275. fips_set_selftest_fail();
  276. return 0;
  277. }
  278. }
  279. memcpy(ctx->last, R, AES_BLOCK_LENGTH);
  280. if (!ctx->second) {
  281. ctx->second = 1;
  282. if (!ctx->test_mode)
  283. continue;
  284. }
  285. if (outlen <= AES_BLOCK_LENGTH) {
  286. memcpy(out, R, outlen);
  287. break;
  288. }
  289. memcpy(out, R, AES_BLOCK_LENGTH);
  290. out += AES_BLOCK_LENGTH;
  291. outlen -= AES_BLOCK_LENGTH;
  292. }
  293. return 1;
  294. }
  295. int FIPS_rand_set_key(const unsigned char *key, FIPS_RAND_SIZE_T keylen)
  296. {
  297. int ret;
  298. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  299. ret = fips_set_prng_key(&sctx, key, keylen);
  300. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  301. return ret;
  302. }
  303. int FIPS_rand_seed(const void *seed, FIPS_RAND_SIZE_T seedlen)
  304. {
  305. int ret;
  306. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  307. ret = fips_set_prng_seed(&sctx, seed, seedlen);
  308. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  309. return ret;
  310. }
  311. int FIPS_rand_bytes(unsigned char *out, FIPS_RAND_SIZE_T count)
  312. {
  313. int ret;
  314. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  315. ret = fips_rand(&sctx, out, count);
  316. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  317. return ret;
  318. }
  319. int FIPS_rand_status(void)
  320. {
  321. int ret;
  322. CRYPTO_r_lock(CRYPTO_LOCK_RAND);
  323. ret = sctx.seeded;
  324. CRYPTO_r_unlock(CRYPTO_LOCK_RAND);
  325. return ret;
  326. }
  327. void FIPS_rand_reset(void)
  328. {
  329. CRYPTO_w_lock(CRYPTO_LOCK_RAND);
  330. fips_rand_prng_reset(&sctx);
  331. CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
  332. }
  333. static void fips_do_rand_seed(const void *seed, FIPS_RAND_SIZE_T seedlen)
  334. {
  335. FIPS_rand_seed(seed, seedlen);
  336. }
  337. static void fips_do_rand_add(const void *seed, FIPS_RAND_SIZE_T seedlen,
  338. double add_entropy)
  339. {
  340. FIPS_rand_seed(seed, seedlen);
  341. }
  342. static const RAND_METHOD rand_fips_meth = {
  343. fips_do_rand_seed,
  344. FIPS_rand_bytes,
  345. FIPS_rand_reset,
  346. fips_do_rand_add,
  347. FIPS_rand_bytes,
  348. FIPS_rand_status
  349. };
  350. const RAND_METHOD *FIPS_rand_method(void)
  351. {
  352. return &rand_fips_meth;
  353. }
  354. #endif