hkdf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/hmac.h>
  12. #include <openssl/kdf.h>
  13. #include <openssl/evp.h>
  14. #include "internal/cryptlib.h"
  15. #include "crypto/evp.h"
  16. #define HKDF_MAXBUF 1024
  17. static unsigned char *HKDF(const EVP_MD *evp_md,
  18. const unsigned char *salt, size_t salt_len,
  19. const unsigned char *key, size_t key_len,
  20. const unsigned char *info, size_t info_len,
  21. unsigned char *okm, size_t okm_len);
  22. static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
  23. const unsigned char *salt, size_t salt_len,
  24. const unsigned char *key, size_t key_len,
  25. unsigned char *prk, size_t *prk_len);
  26. static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
  27. const unsigned char *prk, size_t prk_len,
  28. const unsigned char *info, size_t info_len,
  29. unsigned char *okm, size_t okm_len);
  30. typedef struct {
  31. int mode;
  32. const EVP_MD *md;
  33. unsigned char *salt;
  34. size_t salt_len;
  35. unsigned char *key;
  36. size_t key_len;
  37. unsigned char info[HKDF_MAXBUF];
  38. size_t info_len;
  39. } HKDF_PKEY_CTX;
  40. static int pkey_hkdf_init(EVP_PKEY_CTX *ctx)
  41. {
  42. HKDF_PKEY_CTX *kctx;
  43. if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL) {
  44. KDFerr(KDF_F_PKEY_HKDF_INIT, ERR_R_MALLOC_FAILURE);
  45. return 0;
  46. }
  47. ctx->data = kctx;
  48. return 1;
  49. }
  50. static void pkey_hkdf_cleanup(EVP_PKEY_CTX *ctx)
  51. {
  52. HKDF_PKEY_CTX *kctx = ctx->data;
  53. OPENSSL_clear_free(kctx->salt, kctx->salt_len);
  54. OPENSSL_clear_free(kctx->key, kctx->key_len);
  55. OPENSSL_cleanse(kctx->info, kctx->info_len);
  56. OPENSSL_free(kctx);
  57. }
  58. static int pkey_hkdf_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  59. {
  60. HKDF_PKEY_CTX *kctx = ctx->data;
  61. switch (type) {
  62. case EVP_PKEY_CTRL_HKDF_MD:
  63. if (p2 == NULL)
  64. return 0;
  65. kctx->md = p2;
  66. return 1;
  67. case EVP_PKEY_CTRL_HKDF_MODE:
  68. kctx->mode = p1;
  69. return 1;
  70. case EVP_PKEY_CTRL_HKDF_SALT:
  71. if (p1 == 0 || p2 == NULL)
  72. return 1;
  73. if (p1 < 0)
  74. return 0;
  75. if (kctx->salt != NULL)
  76. OPENSSL_clear_free(kctx->salt, kctx->salt_len);
  77. kctx->salt = OPENSSL_memdup(p2, p1);
  78. if (kctx->salt == NULL)
  79. return 0;
  80. kctx->salt_len = p1;
  81. return 1;
  82. case EVP_PKEY_CTRL_HKDF_KEY:
  83. if (p1 < 0)
  84. return 0;
  85. if (kctx->key != NULL)
  86. OPENSSL_clear_free(kctx->key, kctx->key_len);
  87. kctx->key = OPENSSL_memdup(p2, p1);
  88. if (kctx->key == NULL)
  89. return 0;
  90. kctx->key_len = p1;
  91. return 1;
  92. case EVP_PKEY_CTRL_HKDF_INFO:
  93. if (p1 == 0 || p2 == NULL)
  94. return 1;
  95. if (p1 < 0 || p1 > (int)(HKDF_MAXBUF - kctx->info_len))
  96. return 0;
  97. memcpy(kctx->info + kctx->info_len, p2, p1);
  98. kctx->info_len += p1;
  99. return 1;
  100. default:
  101. return -2;
  102. }
  103. }
  104. static int pkey_hkdf_ctrl_str(EVP_PKEY_CTX *ctx, const char *type,
  105. const char *value)
  106. {
  107. if (strcmp(type, "mode") == 0) {
  108. int mode;
  109. if (strcmp(value, "EXTRACT_AND_EXPAND") == 0)
  110. mode = EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND;
  111. else if (strcmp(value, "EXTRACT_ONLY") == 0)
  112. mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
  113. else if (strcmp(value, "EXPAND_ONLY") == 0)
  114. mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
  115. else
  116. return 0;
  117. return EVP_PKEY_CTX_hkdf_mode(ctx, mode);
  118. }
  119. if (strcmp(type, "md") == 0)
  120. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_DERIVE,
  121. EVP_PKEY_CTRL_HKDF_MD, value);
  122. if (strcmp(type, "salt") == 0)
  123. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
  124. if (strcmp(type, "hexsalt") == 0)
  125. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_SALT, value);
  126. if (strcmp(type, "key") == 0)
  127. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
  128. if (strcmp(type, "hexkey") == 0)
  129. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_KEY, value);
  130. if (strcmp(type, "info") == 0)
  131. return EVP_PKEY_CTX_str2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
  132. if (strcmp(type, "hexinfo") == 0)
  133. return EVP_PKEY_CTX_hex2ctrl(ctx, EVP_PKEY_CTRL_HKDF_INFO, value);
  134. KDFerr(KDF_F_PKEY_HKDF_CTRL_STR, KDF_R_UNKNOWN_PARAMETER_TYPE);
  135. return -2;
  136. }
  137. static int pkey_hkdf_derive_init(EVP_PKEY_CTX *ctx)
  138. {
  139. HKDF_PKEY_CTX *kctx = ctx->data;
  140. OPENSSL_clear_free(kctx->key, kctx->key_len);
  141. OPENSSL_clear_free(kctx->salt, kctx->salt_len);
  142. OPENSSL_cleanse(kctx->info, kctx->info_len);
  143. memset(kctx, 0, sizeof(*kctx));
  144. return 1;
  145. }
  146. static int pkey_hkdf_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  147. size_t *keylen)
  148. {
  149. HKDF_PKEY_CTX *kctx = ctx->data;
  150. if (kctx->md == NULL) {
  151. KDFerr(KDF_F_PKEY_HKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
  152. return 0;
  153. }
  154. if (kctx->key == NULL) {
  155. KDFerr(KDF_F_PKEY_HKDF_DERIVE, KDF_R_MISSING_KEY);
  156. return 0;
  157. }
  158. switch (kctx->mode) {
  159. case EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND:
  160. return HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
  161. kctx->key_len, kctx->info, kctx->info_len, key,
  162. *keylen) != NULL;
  163. case EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY:
  164. if (key == NULL) {
  165. *keylen = EVP_MD_size(kctx->md);
  166. return 1;
  167. }
  168. return HKDF_Extract(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
  169. kctx->key_len, key, keylen) != NULL;
  170. case EVP_PKEY_HKDEF_MODE_EXPAND_ONLY:
  171. return HKDF_Expand(kctx->md, kctx->key, kctx->key_len, kctx->info,
  172. kctx->info_len, key, *keylen) != NULL;
  173. default:
  174. return 0;
  175. }
  176. }
  177. const EVP_PKEY_METHOD hkdf_pkey_meth = {
  178. EVP_PKEY_HKDF,
  179. 0,
  180. pkey_hkdf_init,
  181. 0,
  182. pkey_hkdf_cleanup,
  183. 0, 0,
  184. 0, 0,
  185. 0,
  186. 0,
  187. 0,
  188. 0,
  189. 0, 0,
  190. 0, 0, 0, 0,
  191. 0, 0,
  192. 0, 0,
  193. pkey_hkdf_derive_init,
  194. pkey_hkdf_derive,
  195. pkey_hkdf_ctrl,
  196. pkey_hkdf_ctrl_str
  197. };
  198. static unsigned char *HKDF(const EVP_MD *evp_md,
  199. const unsigned char *salt, size_t salt_len,
  200. const unsigned char *key, size_t key_len,
  201. const unsigned char *info, size_t info_len,
  202. unsigned char *okm, size_t okm_len)
  203. {
  204. unsigned char prk[EVP_MAX_MD_SIZE];
  205. unsigned char *ret;
  206. size_t prk_len;
  207. if (!HKDF_Extract(evp_md, salt, salt_len, key, key_len, prk, &prk_len))
  208. return NULL;
  209. ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
  210. OPENSSL_cleanse(prk, sizeof(prk));
  211. return ret;
  212. }
  213. static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
  214. const unsigned char *salt, size_t salt_len,
  215. const unsigned char *key, size_t key_len,
  216. unsigned char *prk, size_t *prk_len)
  217. {
  218. unsigned int tmp_len;
  219. if (!HMAC(evp_md, salt, salt_len, key, key_len, prk, &tmp_len))
  220. return NULL;
  221. *prk_len = tmp_len;
  222. return prk;
  223. }
  224. static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
  225. const unsigned char *prk, size_t prk_len,
  226. const unsigned char *info, size_t info_len,
  227. unsigned char *okm, size_t okm_len)
  228. {
  229. HMAC_CTX *hmac;
  230. unsigned char *ret = NULL;
  231. unsigned int i;
  232. unsigned char prev[EVP_MAX_MD_SIZE];
  233. size_t done_len = 0, dig_len = EVP_MD_size(evp_md);
  234. size_t n = okm_len / dig_len;
  235. if (okm_len % dig_len)
  236. n++;
  237. if (n > 255 || okm == NULL)
  238. return NULL;
  239. if ((hmac = HMAC_CTX_new()) == NULL)
  240. return NULL;
  241. if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
  242. goto err;
  243. for (i = 1; i <= n; i++) {
  244. size_t copy_len;
  245. const unsigned char ctr = i;
  246. if (i > 1) {
  247. if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
  248. goto err;
  249. if (!HMAC_Update(hmac, prev, dig_len))
  250. goto err;
  251. }
  252. if (!HMAC_Update(hmac, info, info_len))
  253. goto err;
  254. if (!HMAC_Update(hmac, &ctr, 1))
  255. goto err;
  256. if (!HMAC_Final(hmac, prev, NULL))
  257. goto err;
  258. copy_len = (done_len + dig_len > okm_len) ?
  259. okm_len - done_len :
  260. dig_len;
  261. memcpy(okm + done_len, prev, copy_len);
  262. done_len += copy_len;
  263. }
  264. ret = okm;
  265. err:
  266. OPENSSL_cleanse(prev, sizeof(prev));
  267. HMAC_CTX_free(hmac);
  268. return ret;
  269. }