ecx_exch.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2020-2024 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 <openssl/crypto.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/params.h>
  13. #include <openssl/err.h>
  14. #include <openssl/proverr.h>
  15. #include "internal/cryptlib.h"
  16. #include "crypto/ecx.h"
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #include "prov/securitycheck.h"
  20. static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
  21. static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
  22. static OSSL_FUNC_keyexch_init_fn x25519_init;
  23. static OSSL_FUNC_keyexch_init_fn x448_init;
  24. static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
  25. static OSSL_FUNC_keyexch_derive_fn ecx_derive;
  26. static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
  27. static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
  28. static OSSL_FUNC_keyexch_gettable_ctx_params_fn ecx_gettable_ctx_params;
  29. static OSSL_FUNC_keyexch_get_ctx_params_fn ecx_get_ctx_params;
  30. /*
  31. * What's passed as an actual key is defined by the KEYMGMT interface.
  32. * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
  33. * we use that here too.
  34. */
  35. typedef struct {
  36. size_t keylen;
  37. ECX_KEY *key;
  38. ECX_KEY *peerkey;
  39. } PROV_ECX_CTX;
  40. static void *ecx_newctx(void *provctx, size_t keylen)
  41. {
  42. PROV_ECX_CTX *ctx;
  43. if (!ossl_prov_is_running())
  44. return NULL;
  45. ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
  46. if (ctx == NULL)
  47. return NULL;
  48. ctx->keylen = keylen;
  49. return ctx;
  50. }
  51. static void *x25519_newctx(void *provctx)
  52. {
  53. return ecx_newctx(provctx, X25519_KEYLEN);
  54. }
  55. static void *x448_newctx(void *provctx)
  56. {
  57. return ecx_newctx(provctx, X448_KEYLEN);
  58. }
  59. static int ecx_init(void *vecxctx, void *vkey, const char *algname)
  60. {
  61. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  62. ECX_KEY *key = vkey;
  63. if (!ossl_prov_is_running())
  64. return 0;
  65. if (ecxctx == NULL
  66. || key == NULL
  67. || key->keylen != ecxctx->keylen
  68. || !ossl_ecx_key_up_ref(key)) {
  69. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  70. return 0;
  71. }
  72. ossl_ecx_key_free(ecxctx->key);
  73. ecxctx->key = key;
  74. #ifdef FIPS_MODULE
  75. if (!ossl_FIPS_IND_callback(key->libctx, algname, "Init"))
  76. return 0;
  77. #endif
  78. return 1;
  79. }
  80. static int x25519_init(void *vecxctx, void *vkey,
  81. ossl_unused const OSSL_PARAM params[])
  82. {
  83. return ecx_init(vecxctx, vkey, "X25519");
  84. }
  85. static int x448_init(void *vecxctx, void *vkey,
  86. ossl_unused const OSSL_PARAM params[])
  87. {
  88. return ecx_init(vecxctx, vkey, "X448");
  89. }
  90. static int ecx_set_peer(void *vecxctx, void *vkey)
  91. {
  92. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  93. ECX_KEY *key = vkey;
  94. if (!ossl_prov_is_running())
  95. return 0;
  96. if (ecxctx == NULL
  97. || key == NULL
  98. || key->keylen != ecxctx->keylen
  99. || !ossl_ecx_key_up_ref(key)) {
  100. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  101. return 0;
  102. }
  103. ossl_ecx_key_free(ecxctx->peerkey);
  104. ecxctx->peerkey = key;
  105. return 1;
  106. }
  107. static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
  108. size_t outlen)
  109. {
  110. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  111. if (!ossl_prov_is_running())
  112. return 0;
  113. return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
  114. secret, secretlen, outlen);
  115. }
  116. static void ecx_freectx(void *vecxctx)
  117. {
  118. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  119. ossl_ecx_key_free(ecxctx->key);
  120. ossl_ecx_key_free(ecxctx->peerkey);
  121. OPENSSL_free(ecxctx);
  122. }
  123. static void *ecx_dupctx(void *vecxctx)
  124. {
  125. PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
  126. PROV_ECX_CTX *dstctx;
  127. if (!ossl_prov_is_running())
  128. return NULL;
  129. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  130. if (dstctx == NULL)
  131. return NULL;
  132. *dstctx = *srcctx;
  133. if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
  134. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  135. OPENSSL_free(dstctx);
  136. return NULL;
  137. }
  138. if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
  139. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  140. ossl_ecx_key_free(dstctx->key);
  141. OPENSSL_free(dstctx);
  142. return NULL;
  143. }
  144. return dstctx;
  145. }
  146. static const OSSL_PARAM *ecx_gettable_ctx_params(ossl_unused void *vctx,
  147. ossl_unused void *provctx)
  148. {
  149. static const OSSL_PARAM known_gettable_ctx_params[] = {
  150. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  151. OSSL_PARAM_END
  152. };
  153. return known_gettable_ctx_params;
  154. }
  155. static int ecx_get_ctx_params(ossl_unused void *vctx, OSSL_PARAM params[])
  156. {
  157. #ifdef FIPS_MODULE
  158. int approved = 0;
  159. OSSL_PARAM *p = OSSL_PARAM_locate(params,
  160. OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR);
  161. if (p != NULL && !OSSL_PARAM_set_int(p, approved))
  162. return 0;
  163. #endif
  164. return 1;
  165. }
  166. const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
  167. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
  168. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x25519_init },
  169. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  170. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  171. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  172. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  173. { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
  174. { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
  175. (void (*)(void))ecx_gettable_ctx_params },
  176. OSSL_DISPATCH_END
  177. };
  178. const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
  179. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
  180. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))x448_init },
  181. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  182. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  183. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  184. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  185. { OSSL_FUNC_KEYEXCH_GET_CTX_PARAMS, (void (*)(void))ecx_get_ctx_params },
  186. { OSSL_FUNC_KEYEXCH_GETTABLE_CTX_PARAMS,
  187. (void (*)(void))ecx_gettable_ctx_params },
  188. OSSL_DISPATCH_END
  189. };