ecx_exch.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2020-2021 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. #ifdef S390X_EC_ASM
  20. # include "s390x_arch.h"
  21. #endif
  22. static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
  23. static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
  24. static OSSL_FUNC_keyexch_init_fn ecx_init;
  25. static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
  26. static OSSL_FUNC_keyexch_derive_fn ecx_derive;
  27. static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
  28. static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
  29. /*
  30. * What's passed as an actual key is defined by the KEYMGMT interface.
  31. * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
  32. * we use that here too.
  33. */
  34. typedef struct {
  35. size_t keylen;
  36. ECX_KEY *key;
  37. ECX_KEY *peerkey;
  38. } PROV_ECX_CTX;
  39. static void *ecx_newctx(void *provctx, size_t keylen)
  40. {
  41. PROV_ECX_CTX *ctx;
  42. if (!ossl_prov_is_running())
  43. return NULL;
  44. ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
  45. if (ctx == NULL) {
  46. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  47. return NULL;
  48. }
  49. ctx->keylen = keylen;
  50. return ctx;
  51. }
  52. static void *x25519_newctx(void *provctx)
  53. {
  54. return ecx_newctx(provctx, X25519_KEYLEN);
  55. }
  56. static void *x448_newctx(void *provctx)
  57. {
  58. return ecx_newctx(provctx, X448_KEYLEN);
  59. }
  60. static int ecx_init(void *vecxctx, void *vkey,
  61. ossl_unused const OSSL_PARAM params[])
  62. {
  63. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  64. ECX_KEY *key = vkey;
  65. if (!ossl_prov_is_running())
  66. return 0;
  67. if (ecxctx == NULL
  68. || key == NULL
  69. || key->keylen != ecxctx->keylen
  70. || !ossl_ecx_key_up_ref(key)) {
  71. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  72. return 0;
  73. }
  74. ossl_ecx_key_free(ecxctx->key);
  75. ecxctx->key = key;
  76. return 1;
  77. }
  78. static int ecx_set_peer(void *vecxctx, void *vkey)
  79. {
  80. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  81. ECX_KEY *key = vkey;
  82. if (!ossl_prov_is_running())
  83. return 0;
  84. if (ecxctx == NULL
  85. || key == NULL
  86. || key->keylen != ecxctx->keylen
  87. || !ossl_ecx_key_up_ref(key)) {
  88. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  89. return 0;
  90. }
  91. ossl_ecx_key_free(ecxctx->peerkey);
  92. ecxctx->peerkey = key;
  93. return 1;
  94. }
  95. static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
  96. size_t outlen)
  97. {
  98. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  99. if (!ossl_prov_is_running())
  100. return 0;
  101. if (ecxctx->key == NULL
  102. || ecxctx->key->privkey == NULL
  103. || ecxctx->peerkey == NULL) {
  104. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  105. return 0;
  106. }
  107. if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
  108. || ecxctx->keylen == X448_KEYLEN)) {
  109. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  110. return 0;
  111. }
  112. if (secret == NULL) {
  113. *secretlen = ecxctx->keylen;
  114. return 1;
  115. }
  116. if (outlen < ecxctx->keylen) {
  117. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  118. return 0;
  119. }
  120. if (ecxctx->keylen == X25519_KEYLEN) {
  121. #ifdef S390X_EC_ASM
  122. if (OPENSSL_s390xcap_P.pcc[1]
  123. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)) {
  124. if (s390x_x25519_mul(secret, ecxctx->peerkey->pubkey,
  125. ecxctx->key->privkey) == 0) {
  126. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  127. return 0;
  128. }
  129. } else
  130. #endif
  131. if (ossl_x25519(secret, ecxctx->key->privkey,
  132. ecxctx->peerkey->pubkey) == 0) {
  133. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  134. return 0;
  135. }
  136. } else {
  137. #ifdef S390X_EC_ASM
  138. if (OPENSSL_s390xcap_P.pcc[1]
  139. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)) {
  140. if (s390x_x448_mul(secret, ecxctx->peerkey->pubkey,
  141. ecxctx->key->privkey) == 0) {
  142. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  143. return 0;
  144. }
  145. } else
  146. #endif
  147. if (ossl_x448(secret, ecxctx->key->privkey,
  148. ecxctx->peerkey->pubkey) == 0) {
  149. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  150. return 0;
  151. }
  152. }
  153. *secretlen = ecxctx->keylen;
  154. return 1;
  155. }
  156. static void ecx_freectx(void *vecxctx)
  157. {
  158. PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
  159. ossl_ecx_key_free(ecxctx->key);
  160. ossl_ecx_key_free(ecxctx->peerkey);
  161. OPENSSL_free(ecxctx);
  162. }
  163. static void *ecx_dupctx(void *vecxctx)
  164. {
  165. PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
  166. PROV_ECX_CTX *dstctx;
  167. if (!ossl_prov_is_running())
  168. return NULL;
  169. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  170. if (dstctx == NULL) {
  171. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  172. return NULL;
  173. }
  174. *dstctx = *srcctx;
  175. if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
  176. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  177. OPENSSL_free(dstctx);
  178. return NULL;
  179. }
  180. if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
  181. ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
  182. ossl_ecx_key_free(dstctx->key);
  183. OPENSSL_free(dstctx);
  184. return NULL;
  185. }
  186. return dstctx;
  187. }
  188. const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
  189. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
  190. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
  191. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  192. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  193. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  194. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  195. { 0, NULL }
  196. };
  197. const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
  198. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
  199. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
  200. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
  201. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
  202. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
  203. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
  204. { 0, NULL }
  205. };