ecx_exch.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2020-2023 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. static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
  20. static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
  21. static OSSL_FUNC_keyexch_init_fn ecx_init;
  22. static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
  23. static OSSL_FUNC_keyexch_derive_fn ecx_derive;
  24. static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
  25. static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
  26. /*
  27. * What's passed as an actual key is defined by the KEYMGMT interface.
  28. * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
  29. * we use that here too.
  30. */
  31. typedef struct {
  32. size_t keylen;
  33. ECX_KEY *key;
  34. ECX_KEY *peerkey;
  35. } PROV_ECX_CTX;
  36. static void *ecx_newctx(void *provctx, size_t keylen)
  37. {
  38. PROV_ECX_CTX *ctx;
  39. if (!ossl_prov_is_running())
  40. return NULL;
  41. ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
  42. if (ctx == NULL)
  43. return NULL;
  44. ctx->keylen = keylen;
  45. return ctx;
  46. }
  47. static void *x25519_newctx(void *provctx)
  48. {
  49. return ecx_newctx(provctx, X25519_KEYLEN);
  50. }
  51. static void *x448_newctx(void *provctx)
  52. {
  53. return ecx_newctx(provctx, X448_KEYLEN);
  54. }
  55. static int ecx_init(void *vecxctx, void *vkey,
  56. ossl_unused const OSSL_PARAM params[])
  57. {
  58. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  59. ECX_KEY *key = vkey;
  60. if (!ossl_prov_is_running())
  61. return 0;
  62. if (ecxctx == NULL
  63. || key == NULL
  64. || key->keylen != ecxctx->keylen
  65. || !ossl_ecx_key_up_ref(key)) {
  66. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  67. return 0;
  68. }
  69. ossl_ecx_key_free(ecxctx->key);
  70. ecxctx->key = key;
  71. return 1;
  72. }
  73. static int ecx_set_peer(void *vecxctx, void *vkey)
  74. {
  75. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  76. ECX_KEY *key = vkey;
  77. if (!ossl_prov_is_running())
  78. return 0;
  79. if (ecxctx == NULL
  80. || key == NULL
  81. || key->keylen != ecxctx->keylen
  82. || !ossl_ecx_key_up_ref(key)) {
  83. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  84. return 0;
  85. }
  86. ossl_ecx_key_free(ecxctx->peerkey);
  87. ecxctx->peerkey = key;
  88. return 1;
  89. }
  90. static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
  91. size_t outlen)
  92. {
  93. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  94. if (!ossl_prov_is_running())
  95. return 0;
  96. return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
  97. secret, secretlen, outlen);
  98. }
  99. static void ecx_freectx(void *vecxctx)
  100. {
  101. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  102. ossl_ecx_key_free(ecxctx->key);
  103. ossl_ecx_key_free(ecxctx->peerkey);
  104. OPENSSL_free(ecxctx);
  105. }
  106. static void *ecx_dupctx(void *vecxctx)
  107. {
  108. PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
  109. PROV_ECX_CTX *dstctx;
  110. if (!ossl_prov_is_running())
  111. return NULL;
  112. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  113. if (dstctx == NULL)
  114. return NULL;
  115. *dstctx = *srcctx;
  116. if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
  117. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  118. OPENSSL_free(dstctx);
  119. return NULL;
  120. }
  121. if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
  122. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  123. ossl_ecx_key_free(dstctx->key);
  124. OPENSSL_free(dstctx);
  125. return NULL;
  126. }
  127. return dstctx;
  128. }
  129. const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
  130. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
  131. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
  132. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  133. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  134. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  135. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  136. OSSL_DISPATCH_END
  137. };
  138. const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
  139. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
  140. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
  141. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  142. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  143. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  144. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  145. OSSL_DISPATCH_END
  146. };