hkdf.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "internal/evp_int.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(EVP_PKEY_CTX *ctx, unsigned char *key,
  138. size_t *keylen)
  139. {
  140. HKDF_PKEY_CTX *kctx = ctx->data;
  141. if (kctx->md == NULL) {
  142. KDFerr(KDF_F_PKEY_HKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
  143. return 0;
  144. }
  145. if (kctx->key == NULL) {
  146. KDFerr(KDF_F_PKEY_HKDF_DERIVE, KDF_R_MISSING_KEY);
  147. return 0;
  148. }
  149. switch (kctx->mode) {
  150. case EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND:
  151. return HKDF(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
  152. kctx->key_len, kctx->info, kctx->info_len, key,
  153. *keylen) != NULL;
  154. case EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY:
  155. if (key == NULL) {
  156. *keylen = EVP_MD_size(kctx->md);
  157. return 1;
  158. }
  159. return HKDF_Extract(kctx->md, kctx->salt, kctx->salt_len, kctx->key,
  160. kctx->key_len, key, keylen) != NULL;
  161. case EVP_PKEY_HKDEF_MODE_EXPAND_ONLY:
  162. return HKDF_Expand(kctx->md, kctx->key, kctx->key_len, kctx->info,
  163. kctx->info_len, key, *keylen) != NULL;
  164. default:
  165. return 0;
  166. }
  167. }
  168. const EVP_PKEY_METHOD hkdf_pkey_meth = {
  169. EVP_PKEY_HKDF,
  170. 0,
  171. pkey_hkdf_init,
  172. 0,
  173. pkey_hkdf_cleanup,
  174. 0, 0,
  175. 0, 0,
  176. 0,
  177. 0,
  178. 0,
  179. 0,
  180. 0, 0,
  181. 0, 0, 0, 0,
  182. 0, 0,
  183. 0, 0,
  184. 0,
  185. pkey_hkdf_derive,
  186. pkey_hkdf_ctrl,
  187. pkey_hkdf_ctrl_str
  188. };
  189. static unsigned char *HKDF(const EVP_MD *evp_md,
  190. const unsigned char *salt, size_t salt_len,
  191. const unsigned char *key, size_t key_len,
  192. const unsigned char *info, size_t info_len,
  193. unsigned char *okm, size_t okm_len)
  194. {
  195. unsigned char prk[EVP_MAX_MD_SIZE];
  196. unsigned char *ret;
  197. size_t prk_len;
  198. if (!HKDF_Extract(evp_md, salt, salt_len, key, key_len, prk, &prk_len))
  199. return NULL;
  200. ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
  201. OPENSSL_cleanse(prk, sizeof(prk));
  202. return ret;
  203. }
  204. static unsigned char *HKDF_Extract(const EVP_MD *evp_md,
  205. const unsigned char *salt, size_t salt_len,
  206. const unsigned char *key, size_t key_len,
  207. unsigned char *prk, size_t *prk_len)
  208. {
  209. unsigned int tmp_len;
  210. if (!HMAC(evp_md, salt, salt_len, key, key_len, prk, &tmp_len))
  211. return NULL;
  212. *prk_len = tmp_len;
  213. return prk;
  214. }
  215. static unsigned char *HKDF_Expand(const EVP_MD *evp_md,
  216. const unsigned char *prk, size_t prk_len,
  217. const unsigned char *info, size_t info_len,
  218. unsigned char *okm, size_t okm_len)
  219. {
  220. HMAC_CTX *hmac;
  221. unsigned int i;
  222. unsigned char prev[EVP_MAX_MD_SIZE];
  223. size_t done_len = 0, dig_len = EVP_MD_size(evp_md);
  224. size_t n = okm_len / dig_len;
  225. if (okm_len % dig_len)
  226. n++;
  227. if (n > 255 || okm == NULL)
  228. return NULL;
  229. if ((hmac = HMAC_CTX_new()) == NULL)
  230. return NULL;
  231. if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
  232. goto err;
  233. for (i = 1; i <= n; i++) {
  234. size_t copy_len;
  235. const unsigned char ctr = i;
  236. if (i > 1) {
  237. if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
  238. goto err;
  239. if (!HMAC_Update(hmac, prev, dig_len))
  240. goto err;
  241. }
  242. if (!HMAC_Update(hmac, info, info_len))
  243. goto err;
  244. if (!HMAC_Update(hmac, &ctr, 1))
  245. goto err;
  246. if (!HMAC_Final(hmac, prev, NULL))
  247. goto err;
  248. copy_len = (done_len + dig_len > okm_len) ?
  249. okm_len - done_len :
  250. dig_len;
  251. memcpy(okm + done_len, prev, copy_len);
  252. done_len += copy_len;
  253. }
  254. HMAC_CTX_free(hmac);
  255. return okm;
  256. err:
  257. HMAC_CTX_free(hmac);
  258. return NULL;
  259. }