drbg_hmac.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2011-2021 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 <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/proverr.h>
  15. #include "prov/provider_util.h"
  16. #include "internal/thread_once.h"
  17. #include "prov/providercommon.h"
  18. #include "prov/implementations.h"
  19. #include "prov/provider_ctx.h"
  20. #include "drbg_local.h"
  21. static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper;
  22. static OSSL_FUNC_rand_freectx_fn drbg_hmac_free;
  23. static OSSL_FUNC_rand_instantiate_fn drbg_hmac_instantiate_wrapper;
  24. static OSSL_FUNC_rand_uninstantiate_fn drbg_hmac_uninstantiate_wrapper;
  25. static OSSL_FUNC_rand_generate_fn drbg_hmac_generate_wrapper;
  26. static OSSL_FUNC_rand_reseed_fn drbg_hmac_reseed_wrapper;
  27. static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hmac_settable_ctx_params;
  28. static OSSL_FUNC_rand_set_ctx_params_fn drbg_hmac_set_ctx_params;
  29. static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params;
  30. static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params;
  31. static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization;
  32. typedef struct rand_drbg_hmac_st {
  33. EVP_MAC_CTX *ctx; /* H(x) = HMAC_hash OR H(x) = KMAC */
  34. PROV_DIGEST digest; /* H(x) = hash(x) */
  35. size_t blocklen;
  36. unsigned char K[EVP_MAX_MD_SIZE];
  37. unsigned char V[EVP_MAX_MD_SIZE];
  38. } PROV_DRBG_HMAC;
  39. /*
  40. * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process.
  41. *
  42. * hmac is an object that holds the input/output Key and Value (K and V).
  43. * inbyte is 0x00 on the first call and 0x01 on the second call.
  44. * in1, in2, in3 are optional inputs that can be NULL.
  45. * in1len, in2len, in3len are the lengths of the input buffers.
  46. *
  47. * The returned K,V is:
  48. * hmac->K = HMAC(hmac->K, hmac->V || inbyte || [in1] || [in2] || [in3])
  49. * hmac->V = HMAC(hmac->K, hmac->V)
  50. *
  51. * Returns zero if an error occurs otherwise it returns 1.
  52. */
  53. static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte,
  54. const unsigned char *in1, size_t in1len,
  55. const unsigned char *in2, size_t in2len,
  56. const unsigned char *in3, size_t in3len)
  57. {
  58. EVP_MAC_CTX *ctx = hmac->ctx;
  59. if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
  60. /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */
  61. || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
  62. || !EVP_MAC_update(ctx, &inbyte, 1)
  63. || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len))
  64. || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len))
  65. || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len))
  66. || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K)))
  67. return 0;
  68. /* V = HMAC(K, V) */
  69. return EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
  70. && EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
  71. && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V));
  72. }
  73. /*
  74. * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process
  75. *
  76. *
  77. * Updates the drbg objects Key(K) and Value(V) using the following algorithm:
  78. * K,V = do_hmac(hmac, 0, in1, in2, in3)
  79. * if (any input is not NULL)
  80. * K,V = do_hmac(hmac, 1, in1, in2, in3)
  81. *
  82. * where in1, in2, in3 are optional input buffers that can be NULL.
  83. * in1len, in2len, in3len are the lengths of the input buffers.
  84. *
  85. * Returns zero if an error occurs otherwise it returns 1.
  86. */
  87. static int drbg_hmac_update(PROV_DRBG *drbg,
  88. const unsigned char *in1, size_t in1len,
  89. const unsigned char *in2, size_t in2len,
  90. const unsigned char *in3, size_t in3len)
  91. {
  92. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  93. /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */
  94. if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len))
  95. return 0;
  96. /* (Step 3) If provided_data == NULL then return (K,V) */
  97. if (in1len == 0 && in2len == 0 && in3len == 0)
  98. return 1;
  99. /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */
  100. return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len);
  101. }
  102. /*
  103. * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process:
  104. *
  105. * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's.
  106. * and then calls (K,V) = drbg_hmac_update() with input parameters:
  107. * ent = entropy data (Can be NULL) of length ent_len.
  108. * nonce = nonce data (Can be NULL) of length nonce_len.
  109. * pstr = personalization data (Can be NULL) of length pstr_len.
  110. *
  111. * Returns zero if an error occurs otherwise it returns 1.
  112. */
  113. static int drbg_hmac_instantiate(PROV_DRBG *drbg,
  114. const unsigned char *ent, size_t ent_len,
  115. const unsigned char *nonce, size_t nonce_len,
  116. const unsigned char *pstr, size_t pstr_len)
  117. {
  118. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  119. if (hmac->ctx == NULL) {
  120. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
  121. return 0;
  122. }
  123. /* (Step 2) Key = 0x00 00...00 */
  124. memset(hmac->K, 0x00, hmac->blocklen);
  125. /* (Step 3) V = 0x01 01...01 */
  126. memset(hmac->V, 0x01, hmac->blocklen);
  127. /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */
  128. return drbg_hmac_update(drbg, ent, ent_len, nonce, nonce_len, pstr,
  129. pstr_len);
  130. }
  131. static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength,
  132. int prediction_resistance,
  133. const unsigned char *pstr,
  134. size_t pstr_len,
  135. const OSSL_PARAM params[])
  136. {
  137. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  138. if (!ossl_prov_is_running() || !drbg_hmac_set_ctx_params(drbg, params))
  139. return 0;
  140. return ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
  141. pstr, pstr_len);
  142. }
  143. /*
  144. * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process:
  145. *
  146. * Reseeds the drbg's Key (K) and Value (V) by calling
  147. * (K,V) = drbg_hmac_update() with the following input parameters:
  148. * ent = entropy input data (Can be NULL) of length ent_len.
  149. * adin = additional input data (Can be NULL) of length adin_len.
  150. *
  151. * Returns zero if an error occurs otherwise it returns 1.
  152. */
  153. static int drbg_hmac_reseed(PROV_DRBG *drbg,
  154. const unsigned char *ent, size_t ent_len,
  155. const unsigned char *adin, size_t adin_len)
  156. {
  157. /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */
  158. return drbg_hmac_update(drbg, ent, ent_len, adin, adin_len, NULL, 0);
  159. }
  160. static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance,
  161. const unsigned char *ent, size_t ent_len,
  162. const unsigned char *adin, size_t adin_len)
  163. {
  164. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  165. return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
  166. adin, adin_len);
  167. }
  168. /*
  169. * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process:
  170. *
  171. * Generates pseudo random bytes and updates the internal K,V for the drbg.
  172. * out is a buffer to fill with outlen bytes of pseudo random data.
  173. * adin is an additional_input string of size adin_len that may be NULL.
  174. *
  175. * Returns zero if an error occurs otherwise it returns 1.
  176. */
  177. static int drbg_hmac_generate(PROV_DRBG *drbg,
  178. unsigned char *out, size_t outlen,
  179. const unsigned char *adin, size_t adin_len)
  180. {
  181. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  182. EVP_MAC_CTX *ctx = hmac->ctx;
  183. const unsigned char *temp = hmac->V;
  184. /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */
  185. if (adin != NULL
  186. && adin_len > 0
  187. && !drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
  188. return 0;
  189. /*
  190. * (Steps 3-5) temp = NULL
  191. * while (len(temp) < outlen) {
  192. * V = HMAC(K, V)
  193. * temp = temp || V
  194. * }
  195. */
  196. for (;;) {
  197. if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)
  198. || !EVP_MAC_update(ctx, temp, hmac->blocklen))
  199. return 0;
  200. if (outlen > hmac->blocklen) {
  201. if (!EVP_MAC_final(ctx, out, NULL, outlen))
  202. return 0;
  203. temp = out;
  204. } else {
  205. if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)))
  206. return 0;
  207. memcpy(out, hmac->V, outlen);
  208. break;
  209. }
  210. out += hmac->blocklen;
  211. outlen -= hmac->blocklen;
  212. }
  213. /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */
  214. if (!drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
  215. return 0;
  216. return 1;
  217. }
  218. static int drbg_hmac_generate_wrapper
  219. (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
  220. int prediction_resistance, const unsigned char *adin, size_t adin_len)
  221. {
  222. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  223. return ossl_prov_drbg_generate(drbg, out, outlen, strength,
  224. prediction_resistance, adin, adin_len);
  225. }
  226. static int drbg_hmac_uninstantiate(PROV_DRBG *drbg)
  227. {
  228. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  229. OPENSSL_cleanse(hmac->K, sizeof(hmac->K));
  230. OPENSSL_cleanse(hmac->V, sizeof(hmac->V));
  231. return ossl_prov_drbg_uninstantiate(drbg);
  232. }
  233. static int drbg_hmac_uninstantiate_wrapper(void *vdrbg)
  234. {
  235. return drbg_hmac_uninstantiate((PROV_DRBG *)vdrbg);
  236. }
  237. static int drbg_hmac_verify_zeroization(void *vdrbg)
  238. {
  239. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  240. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  241. PROV_DRBG_VERYIFY_ZEROIZATION(hmac->K);
  242. PROV_DRBG_VERYIFY_ZEROIZATION(hmac->V);
  243. return 1;
  244. }
  245. static int drbg_hmac_new(PROV_DRBG *drbg)
  246. {
  247. PROV_DRBG_HMAC *hmac;
  248. hmac = OPENSSL_secure_zalloc(sizeof(*hmac));
  249. if (hmac == NULL) {
  250. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  251. return 0;
  252. }
  253. drbg->data = hmac;
  254. /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
  255. drbg->max_entropylen = DRBG_MAX_LENGTH;
  256. drbg->max_noncelen = DRBG_MAX_LENGTH;
  257. drbg->max_perslen = DRBG_MAX_LENGTH;
  258. drbg->max_adinlen = DRBG_MAX_LENGTH;
  259. /* Maximum number of bits per request = 2^19 = 2^16 bytes */
  260. drbg->max_request = 1 << 16;
  261. return 1;
  262. }
  263. static void *drbg_hmac_new_wrapper(void *provctx, void *parent,
  264. const OSSL_DISPATCH *parent_dispatch)
  265. {
  266. return ossl_rand_drbg_new(provctx, parent, parent_dispatch, &drbg_hmac_new,
  267. &drbg_hmac_instantiate, &drbg_hmac_uninstantiate,
  268. &drbg_hmac_reseed, &drbg_hmac_generate);
  269. }
  270. static void drbg_hmac_free(void *vdrbg)
  271. {
  272. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  273. PROV_DRBG_HMAC *hmac;
  274. if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) {
  275. EVP_MAC_CTX_free(hmac->ctx);
  276. ossl_prov_digest_reset(&hmac->digest);
  277. OPENSSL_secure_clear_free(hmac, sizeof(*hmac));
  278. }
  279. ossl_rand_drbg_free(drbg);
  280. }
  281. static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
  282. {
  283. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  284. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
  285. const char *name;
  286. const EVP_MD *md;
  287. OSSL_PARAM *p;
  288. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAC);
  289. if (p != NULL) {
  290. if (hmac->ctx == NULL)
  291. return 0;
  292. name = EVP_MAC_name(EVP_MAC_CTX_mac(hmac->ctx));
  293. if (!OSSL_PARAM_set_utf8_string(p, name))
  294. return 0;
  295. }
  296. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
  297. if (p != NULL) {
  298. md = ossl_prov_digest_md(&hmac->digest);
  299. if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
  300. return 0;
  301. }
  302. return ossl_drbg_get_ctx_params(drbg, params);
  303. }
  304. static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *vctx,
  305. ossl_unused void *p_ctx)
  306. {
  307. static const OSSL_PARAM known_gettable_ctx_params[] = {
  308. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
  309. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
  310. OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
  311. OSSL_PARAM_END
  312. };
  313. return known_gettable_ctx_params;
  314. }
  315. static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  316. {
  317. PROV_DRBG *ctx = (PROV_DRBG *)vctx;
  318. PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data;
  319. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  320. const EVP_MD *md;
  321. if (!ossl_prov_digest_load_from_params(&hmac->digest, params, libctx))
  322. return 0;
  323. /*
  324. * Confirm digest is allowed. We allow all digests that are not XOF
  325. * (such as SHAKE). In FIPS mode, the fetch will fail for non-approved
  326. * digests.
  327. */
  328. md = ossl_prov_digest_md(&hmac->digest);
  329. if (md != NULL && (EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0) {
  330. ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
  331. return 0;
  332. }
  333. if (!ossl_prov_macctx_load_from_params(&hmac->ctx, params,
  334. NULL, NULL, NULL, libctx))
  335. return 0;
  336. if (hmac->ctx != NULL) {
  337. /* These are taken from SP 800-90 10.1 Table 2 */
  338. hmac->blocklen = EVP_MD_size(md);
  339. /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
  340. ctx->strength = 64 * (int)(hmac->blocklen >> 3);
  341. if (ctx->strength > 256)
  342. ctx->strength = 256;
  343. ctx->seedlen = hmac->blocklen;
  344. ctx->min_entropylen = ctx->strength / 8;
  345. ctx->min_noncelen = ctx->min_entropylen / 2;
  346. }
  347. return ossl_drbg_set_ctx_params(ctx, params);
  348. }
  349. static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *vctx,
  350. ossl_unused void *p_ctx)
  351. {
  352. static const OSSL_PARAM known_settable_ctx_params[] = {
  353. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
  354. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
  355. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
  356. OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
  357. OSSL_PARAM_END
  358. };
  359. return known_settable_ctx_params;
  360. }
  361. const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = {
  362. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hmac_new_wrapper },
  363. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hmac_free },
  364. { OSSL_FUNC_RAND_INSTANTIATE,
  365. (void(*)(void))drbg_hmac_instantiate_wrapper },
  366. { OSSL_FUNC_RAND_UNINSTANTIATE,
  367. (void(*)(void))drbg_hmac_uninstantiate_wrapper },
  368. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hmac_generate_wrapper },
  369. { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hmac_reseed_wrapper },
  370. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
  371. { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
  372. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
  373. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  374. (void(*)(void))drbg_hmac_settable_ctx_params },
  375. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hmac_set_ctx_params },
  376. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  377. (void(*)(void))drbg_hmac_gettable_ctx_params },
  378. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hmac_get_ctx_params },
  379. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  380. (void(*)(void))drbg_hmac_verify_zeroization },
  381. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
  382. { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
  383. { 0, NULL }
  384. };