sshkdf.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2018-2019 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 <stdarg.h>
  11. #include <string.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/kdf.h>
  14. #include <openssl/core_names.h>
  15. #include "internal/cryptlib.h"
  16. #include "internal/numbers.h"
  17. #include "crypto/evp.h"
  18. #include "internal/provider_ctx.h"
  19. #include "internal/providercommonerr.h"
  20. #include "internal/provider_algs.h"
  21. # include "internal/provider_util.h"
  22. /* See RFC 4253, Section 7.2 */
  23. static OSSL_OP_kdf_newctx_fn kdf_sshkdf_new;
  24. static OSSL_OP_kdf_freectx_fn kdf_sshkdf_free;
  25. static OSSL_OP_kdf_reset_fn kdf_sshkdf_reset;
  26. static OSSL_OP_kdf_derive_fn kdf_sshkdf_derive;
  27. static OSSL_OP_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params;
  28. static OSSL_OP_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params;
  29. static OSSL_OP_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params;
  30. static OSSL_OP_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params;
  31. static int SSHKDF(const EVP_MD *evp_md,
  32. const unsigned char *key, size_t key_len,
  33. const unsigned char *xcghash, size_t xcghash_len,
  34. const unsigned char *session_id, size_t session_id_len,
  35. char type, unsigned char *okey, size_t okey_len);
  36. typedef struct {
  37. void *provctx;
  38. PROV_DIGEST digest;
  39. unsigned char *key; /* K */
  40. size_t key_len;
  41. unsigned char *xcghash; /* H */
  42. size_t xcghash_len;
  43. char type; /* X */
  44. unsigned char *session_id;
  45. size_t session_id_len;
  46. } KDF_SSHKDF;
  47. static void *kdf_sshkdf_new(void *provctx)
  48. {
  49. KDF_SSHKDF *ctx;
  50. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  51. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  52. ctx->provctx = provctx;
  53. return ctx;
  54. }
  55. static void kdf_sshkdf_free(void *vctx)
  56. {
  57. KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
  58. kdf_sshkdf_reset(ctx);
  59. OPENSSL_free(ctx);
  60. }
  61. static void kdf_sshkdf_reset(void *vctx)
  62. {
  63. KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
  64. ossl_prov_digest_reset(&ctx->digest);
  65. OPENSSL_clear_free(ctx->key, ctx->key_len);
  66. OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len);
  67. OPENSSL_clear_free(ctx->session_id, ctx->session_id_len);
  68. memset(ctx, 0, sizeof(*ctx));
  69. }
  70. static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
  71. const OSSL_PARAM *p)
  72. {
  73. OPENSSL_clear_free(*dst, *dst_len);
  74. *dst = NULL;
  75. return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
  76. }
  77. static int kdf_sshkdf_derive(void *vctx, unsigned char *key,
  78. size_t keylen)
  79. {
  80. KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
  81. const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
  82. if (md == NULL) {
  83. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
  84. return 0;
  85. }
  86. if (ctx->key == NULL) {
  87. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  88. return 0;
  89. }
  90. if (ctx->xcghash == NULL) {
  91. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH);
  92. return 0;
  93. }
  94. if (ctx->session_id == NULL) {
  95. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID);
  96. return 0;
  97. }
  98. if (ctx->type == 0) {
  99. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE);
  100. return 0;
  101. }
  102. return SSHKDF(md, ctx->key, ctx->key_len,
  103. ctx->xcghash, ctx->xcghash_len,
  104. ctx->session_id, ctx->session_id_len,
  105. ctx->type, key, keylen);
  106. }
  107. static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  108. {
  109. const OSSL_PARAM *p;
  110. KDF_SSHKDF *ctx = vctx;
  111. OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
  112. int t;
  113. if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
  114. return 0;
  115. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
  116. if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p))
  117. return 0;
  118. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH))
  119. != NULL)
  120. if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p))
  121. return 0;
  122. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID))
  123. != NULL)
  124. if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p))
  125. return 0;
  126. if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE))
  127. != NULL) {
  128. if (p->data == NULL || p->data_size == 0)
  129. return 0;
  130. t = *(unsigned char *)p->data;
  131. if (t < 65 || t > 70) {
  132. ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR);
  133. return 0;
  134. }
  135. ctx->type = (char)t;
  136. }
  137. return 1;
  138. }
  139. static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(void)
  140. {
  141. static const OSSL_PARAM known_settable_ctx_params[] = {
  142. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
  143. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
  144. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
  145. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0),
  146. OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0),
  147. OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0),
  148. OSSL_PARAM_END
  149. };
  150. return known_settable_ctx_params;
  151. }
  152. static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
  153. {
  154. OSSL_PARAM *p;
  155. if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
  156. return OSSL_PARAM_set_size_t(p, SIZE_MAX);
  157. return -2;
  158. }
  159. static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(void)
  160. {
  161. static const OSSL_PARAM known_gettable_ctx_params[] = {
  162. OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
  163. OSSL_PARAM_END
  164. };
  165. return known_gettable_ctx_params;
  166. }
  167. const OSSL_DISPATCH kdf_sshkdf_functions[] = {
  168. { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new },
  169. { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free },
  170. { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset },
  171. { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive },
  172. { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
  173. (void(*)(void))kdf_sshkdf_settable_ctx_params },
  174. { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params },
  175. { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
  176. (void(*)(void))kdf_sshkdf_gettable_ctx_params },
  177. { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params },
  178. { 0, NULL }
  179. };
  180. static int SSHKDF(const EVP_MD *evp_md,
  181. const unsigned char *key, size_t key_len,
  182. const unsigned char *xcghash, size_t xcghash_len,
  183. const unsigned char *session_id, size_t session_id_len,
  184. char type, unsigned char *okey, size_t okey_len)
  185. {
  186. EVP_MD_CTX *md = NULL;
  187. unsigned char digest[EVP_MAX_MD_SIZE];
  188. unsigned int dsize = 0;
  189. size_t cursize = 0;
  190. int ret = 0;
  191. md = EVP_MD_CTX_new();
  192. if (md == NULL)
  193. return 0;
  194. if (!EVP_DigestInit_ex(md, evp_md, NULL))
  195. goto out;
  196. if (!EVP_DigestUpdate(md, key, key_len))
  197. goto out;
  198. if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
  199. goto out;
  200. if (!EVP_DigestUpdate(md, &type, 1))
  201. goto out;
  202. if (!EVP_DigestUpdate(md, session_id, session_id_len))
  203. goto out;
  204. if (!EVP_DigestFinal_ex(md, digest, &dsize))
  205. goto out;
  206. if (okey_len < dsize) {
  207. memcpy(okey, digest, okey_len);
  208. ret = 1;
  209. goto out;
  210. }
  211. memcpy(okey, digest, dsize);
  212. for (cursize = dsize; cursize < okey_len; cursize += dsize) {
  213. if (!EVP_DigestInit_ex(md, evp_md, NULL))
  214. goto out;
  215. if (!EVP_DigestUpdate(md, key, key_len))
  216. goto out;
  217. if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
  218. goto out;
  219. if (!EVP_DigestUpdate(md, okey, cursize))
  220. goto out;
  221. if (!EVP_DigestFinal_ex(md, digest, &dsize))
  222. goto out;
  223. if (okey_len < cursize + dsize) {
  224. memcpy(okey + cursize, digest, okey_len - cursize);
  225. ret = 1;
  226. goto out;
  227. }
  228. memcpy(okey + cursize, digest, dsize);
  229. }
  230. ret = 1;
  231. out:
  232. EVP_MD_CTX_free(md);
  233. OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);
  234. return ret;
  235. }