drbg_hash.c 12 KB

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