drbg_hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/err.h>
  14. #include <openssl/rand.h>
  15. #include "internal/thread_once.h"
  16. #include "prov/providercommon.h"
  17. #include "rand_local.h"
  18. /* 440 bits from SP800-90Ar1 10.1 table 2 */
  19. #define HASH_PRNG_SMALL_SEEDLEN (440/8)
  20. /* Determine what seedlen to use based on the block length */
  21. #define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8)
  22. #define INBYTE_IGNORE ((unsigned char)0xFF)
  23. /*
  24. * SP800-90Ar1 10.3.1 Derivation function using a Hash Function (Hash_df).
  25. * The input string used is composed of:
  26. * inbyte - An optional leading byte (ignore if equal to INBYTE_IGNORE)
  27. * in - input string 1 (A Non NULL value).
  28. * in2 - optional input string (Can be NULL).
  29. * in3 - optional input string (Can be NULL).
  30. * These are concatenated as part of the DigestUpdate process.
  31. */
  32. static int hash_df(RAND_DRBG *drbg, unsigned char *out,
  33. const unsigned char inbyte,
  34. const unsigned char *in, size_t inlen,
  35. const unsigned char *in2, size_t in2len,
  36. const unsigned char *in3, size_t in3len)
  37. {
  38. RAND_DRBG_HASH *hash = &drbg->data.hash;
  39. EVP_MD_CTX *ctx = hash->ctx;
  40. unsigned char *vtmp = hash->vtmp;
  41. /* tmp = counter || num_bits_returned || [inbyte] */
  42. unsigned char tmp[1 + 4 + 1];
  43. int tmp_sz = 0;
  44. size_t outlen = drbg->seedlen;
  45. size_t num_bits_returned = outlen * 8;
  46. /*
  47. * No need to check outlen size here, as the standard only ever needs
  48. * seedlen bytes which is always less than the maximum permitted.
  49. */
  50. /* (Step 3) counter = 1 (tmp[0] is the 8 bit counter) */
  51. tmp[tmp_sz++] = 1;
  52. /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */
  53. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff);
  54. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff);
  55. tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff);
  56. tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff);
  57. /* Tack the additional input byte onto the end of tmp if it exists */
  58. if (inbyte != INBYTE_IGNORE)
  59. tmp[tmp_sz++] = inbyte;
  60. /* (Step 4) */
  61. for (;;) {
  62. /*
  63. * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3])
  64. * (where tmp = counter || num_bits_returned || [inbyte])
  65. */
  66. if (!(EVP_DigestInit_ex(ctx, hash->md, NULL)
  67. && EVP_DigestUpdate(ctx, tmp, tmp_sz)
  68. && EVP_DigestUpdate(ctx, in, inlen)
  69. && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len))
  70. && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len))))
  71. return 0;
  72. if (outlen < hash->blocklen) {
  73. if (!EVP_DigestFinal(ctx, vtmp, NULL))
  74. return 0;
  75. memcpy(out, vtmp, outlen);
  76. OPENSSL_cleanse(vtmp, hash->blocklen);
  77. break;
  78. } else if(!EVP_DigestFinal(ctx, out, NULL)) {
  79. return 0;
  80. }
  81. outlen -= hash->blocklen;
  82. if (outlen == 0)
  83. break;
  84. /* (Step 4.2) counter++ */
  85. tmp[0]++;
  86. out += hash->blocklen;
  87. }
  88. return 1;
  89. }
  90. /* Helper function that just passes 2 input parameters to hash_df() */
  91. static int hash_df1(RAND_DRBG *drbg, unsigned char *out,
  92. const unsigned char in_byte,
  93. const unsigned char *in1, size_t in1len)
  94. {
  95. return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0);
  96. }
  97. /*
  98. * Add 2 byte buffers together. The first elements in each buffer are the top
  99. * most bytes. The result is stored in the dst buffer.
  100. * The final carry is ignored i.e: dst = (dst + in) mod (2^seedlen_bits).
  101. * where dst size is drbg->seedlen, and inlen <= drbg->seedlen.
  102. */
  103. static int add_bytes(RAND_DRBG *drbg, unsigned char *dst,
  104. unsigned char *in, size_t inlen)
  105. {
  106. size_t i;
  107. int result;
  108. const unsigned char *add;
  109. unsigned char carry = 0, *d;
  110. assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen);
  111. d = &dst[drbg->seedlen - 1];
  112. add = &in[inlen - 1];
  113. for (i = inlen; i > 0; i--, d--, add--) {
  114. result = *d + *add + carry;
  115. carry = (unsigned char)(result >> 8);
  116. *d = (unsigned char)(result & 0xff);
  117. }
  118. if (carry != 0) {
  119. /* Add the carry to the top of the dst if inlen is not the same size */
  120. for (i = drbg->seedlen - inlen; i > 0; --i, d--) {
  121. *d += 1; /* Carry can only be 1 */
  122. if (*d != 0) /* exit if carry doesnt propagate to the next byte */
  123. break;
  124. }
  125. }
  126. return 1;
  127. }
  128. /* V = (V + Hash(inbyte || V || [additional_input]) mod (2^seedlen) */
  129. static int add_hash_to_v(RAND_DRBG *drbg, unsigned char inbyte,
  130. const unsigned char *adin, size_t adinlen)
  131. {
  132. RAND_DRBG_HASH *hash = &drbg->data.hash;
  133. EVP_MD_CTX *ctx = hash->ctx;
  134. return EVP_DigestInit_ex(ctx, hash->md, NULL)
  135. && EVP_DigestUpdate(ctx, &inbyte, 1)
  136. && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen)
  137. && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen))
  138. && EVP_DigestFinal(ctx, hash->vtmp, NULL)
  139. && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen);
  140. }
  141. /*
  142. * The Hashgen() as listed in SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process.
  143. *
  144. * drbg contains the current value of V.
  145. * outlen is the requested number of bytes.
  146. * out is a buffer to return the generated bits.
  147. *
  148. * The algorithm to generate the bits is:
  149. * data = V
  150. * w = NULL
  151. * for (i = 1 to m) {
  152. * W = W || Hash(data)
  153. * data = (data + 1) mod (2^seedlen)
  154. * }
  155. * out = Leftmost(W, outlen)
  156. *
  157. * Returns zero if an error occurs otherwise it returns 1.
  158. */
  159. static int hash_gen(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
  160. {
  161. RAND_DRBG_HASH *hash = &drbg->data.hash;
  162. unsigned char one = 1;
  163. if (outlen == 0)
  164. return 1;
  165. memcpy(hash->vtmp, hash->V, drbg->seedlen);
  166. for(;;) {
  167. if (!EVP_DigestInit_ex(hash->ctx, hash->md, NULL)
  168. || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen))
  169. return 0;
  170. if (outlen < hash->blocklen) {
  171. if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL))
  172. return 0;
  173. memcpy(out, hash->vtmp, outlen);
  174. return 1;
  175. } else {
  176. if (!EVP_DigestFinal(hash->ctx, out, NULL))
  177. return 0;
  178. outlen -= hash->blocklen;
  179. if (outlen == 0)
  180. break;
  181. out += hash->blocklen;
  182. }
  183. add_bytes(drbg, hash->vtmp, &one, 1);
  184. }
  185. return 1;
  186. }
  187. /*
  188. * SP800-90Ar1 10.1.1.2 Hash_DRBG_Instantiate_Process:
  189. *
  190. * ent is entropy input obtained from a randomness source of length ent_len.
  191. * nonce is a string of bytes of length nonce_len.
  192. * pstr is a personalization string received from an application. May be NULL.
  193. *
  194. * Returns zero if an error occurs otherwise it returns 1.
  195. */
  196. static int drbg_hash_instantiate(RAND_DRBG *drbg,
  197. const unsigned char *ent, size_t ent_len,
  198. const unsigned char *nonce, size_t nonce_len,
  199. const unsigned char *pstr, size_t pstr_len)
  200. {
  201. RAND_DRBG_HASH *hash = &drbg->data.hash;
  202. /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */
  203. return hash_df(drbg, hash->V, INBYTE_IGNORE,
  204. ent, ent_len, nonce, nonce_len, pstr, pstr_len)
  205. /* (Step 4) C = Hash_df(0x00||V, seedlen) */
  206. && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
  207. }
  208. /*
  209. * SP800-90Ar1 10.1.1.3 Hash_DRBG_Reseed_Process:
  210. *
  211. * ent is entropy input bytes obtained from a randomness source.
  212. * addin is additional input received from an application. May be NULL.
  213. *
  214. * Returns zero if an error occurs otherwise it returns 1.
  215. */
  216. static int drbg_hash_reseed(RAND_DRBG *drbg,
  217. const unsigned char *ent, size_t ent_len,
  218. const unsigned char *adin, size_t adin_len)
  219. {
  220. RAND_DRBG_HASH *hash = &drbg->data.hash;
  221. /* (Step 1-2) V = Hash_df(0x01 || V || entropy_input || additional_input)*/
  222. /* V about to be updated so use C as output instead */
  223. if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len,
  224. adin, adin_len))
  225. return 0;
  226. memcpy(hash->V, hash->C, drbg->seedlen);
  227. /* (Step 4) C = Hash_df(0x00||V, seedlen) */
  228. return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
  229. }
  230. /*
  231. * SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process:
  232. *
  233. * Generates pseudo random bytes using the drbg.
  234. * out is a buffer to fill with outlen bytes of pseudo random data.
  235. * addin is additional input received from an application. May be NULL.
  236. *
  237. * Returns zero if an error occurs otherwise it returns 1.
  238. */
  239. static int drbg_hash_generate(RAND_DRBG *drbg,
  240. unsigned char *out, size_t outlen,
  241. const unsigned char *adin, size_t adin_len)
  242. {
  243. RAND_DRBG_HASH *hash = &drbg->data.hash;
  244. unsigned char counter[4];
  245. int reseed_counter = drbg->reseed_gen_counter;
  246. counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff);
  247. counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff);
  248. counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff);
  249. counter[3] = (unsigned char)(reseed_counter & 0xff);
  250. return (adin == NULL
  251. /* (Step 2) if adin != NULL then V = V + Hash(0x02||V||adin) */
  252. || adin_len == 0
  253. || add_hash_to_v(drbg, 0x02, adin, adin_len))
  254. /* (Step 3) Hashgen(outlen, V) */
  255. && hash_gen(drbg, out, outlen)
  256. /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */
  257. && add_hash_to_v(drbg, 0x03, NULL, 0)
  258. /* (Step 5) V = (V + H + C + reseed_counter) mod (2^seedlen_bits) */
  259. /* V = (V + C) mod (2^seedlen_bits) */
  260. && add_bytes(drbg, hash->V, hash->C, drbg->seedlen)
  261. /* V = (V + reseed_counter) mod (2^seedlen_bits) */
  262. && add_bytes(drbg, hash->V, counter, 4);
  263. }
  264. static int drbg_hash_uninstantiate(RAND_DRBG *drbg)
  265. {
  266. EVP_MD_free(drbg->data.hash.md);
  267. EVP_MD_CTX_free(drbg->data.hash.ctx);
  268. OPENSSL_cleanse(&drbg->data.hash, sizeof(drbg->data.hash));
  269. return 1;
  270. }
  271. static RAND_DRBG_METHOD drbg_hash_meth = {
  272. drbg_hash_instantiate,
  273. drbg_hash_reseed,
  274. drbg_hash_generate,
  275. drbg_hash_uninstantiate
  276. };
  277. int drbg_hash_init(RAND_DRBG *drbg)
  278. {
  279. EVP_MD *md;
  280. RAND_DRBG_HASH *hash = &drbg->data.hash;
  281. /*
  282. * Confirm digest is allowed. We allow all digests that are not XOF
  283. * (such as SHAKE). In FIPS mode, the fetch will fail for non-approved
  284. * digests.
  285. */
  286. md = EVP_MD_fetch(drbg->libctx, ossl_prov_util_nid_to_name(drbg->type), "");
  287. if (md == NULL)
  288. return 0;
  289. if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0)
  290. return 0;
  291. drbg->meth = &drbg_hash_meth;
  292. if (hash->ctx == NULL) {
  293. hash->ctx = EVP_MD_CTX_new();
  294. if (hash->ctx == NULL) {
  295. EVP_MD_free(md);
  296. return 0;
  297. }
  298. }
  299. EVP_MD_free(hash->md);
  300. hash->md = md;
  301. /* These are taken from SP 800-90 10.1 Table 2 */
  302. hash->blocklen = EVP_MD_size(md);
  303. /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
  304. drbg->strength = 64 * (hash->blocklen >> 3);
  305. if (drbg->strength > 256)
  306. drbg->strength = 256;
  307. if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN)
  308. drbg->seedlen = HASH_PRNG_MAX_SEEDLEN;
  309. else
  310. drbg->seedlen = HASH_PRNG_SMALL_SEEDLEN;
  311. drbg->min_entropylen = drbg->strength / 8;
  312. drbg->max_entropylen = DRBG_MAX_LENGTH;
  313. drbg->min_noncelen = drbg->min_entropylen / 2;
  314. drbg->max_noncelen = DRBG_MAX_LENGTH;
  315. drbg->max_perslen = DRBG_MAX_LENGTH;
  316. drbg->max_adinlen = DRBG_MAX_LENGTH;
  317. /* Maximum number of bits per request = 2^19 = 2^16 bytes */
  318. drbg->max_request = 1 << 16;
  319. return 1;
  320. }