ecx_key.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 <string.h>
  10. #include <openssl/err.h>
  11. #include <openssl/proverr.h>
  12. #include "crypto/ecx.h"
  13. #include "internal/common.h" /* for ossl_assert() */
  14. #ifdef S390X_EC_ASM
  15. # include "s390x_arch.h"
  16. #endif
  17. ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
  18. const char *propq)
  19. {
  20. ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
  21. if (ret == NULL)
  22. return NULL;
  23. ret->libctx = libctx;
  24. ret->haspubkey = haspubkey;
  25. switch (type) {
  26. case ECX_KEY_TYPE_X25519:
  27. ret->keylen = X25519_KEYLEN;
  28. break;
  29. case ECX_KEY_TYPE_X448:
  30. ret->keylen = X448_KEYLEN;
  31. break;
  32. case ECX_KEY_TYPE_ED25519:
  33. ret->keylen = ED25519_KEYLEN;
  34. break;
  35. case ECX_KEY_TYPE_ED448:
  36. ret->keylen = ED448_KEYLEN;
  37. break;
  38. }
  39. ret->type = type;
  40. if (!CRYPTO_NEW_REF(&ret->references, 1))
  41. goto err;
  42. if (propq != NULL) {
  43. ret->propq = OPENSSL_strdup(propq);
  44. if (ret->propq == NULL)
  45. goto err;
  46. }
  47. return ret;
  48. err:
  49. if (ret != NULL) {
  50. OPENSSL_free(ret->propq);
  51. CRYPTO_FREE_REF(&ret->references);
  52. }
  53. OPENSSL_free(ret);
  54. return NULL;
  55. }
  56. void ossl_ecx_key_free(ECX_KEY *key)
  57. {
  58. int i;
  59. if (key == NULL)
  60. return;
  61. CRYPTO_DOWN_REF(&key->references, &i);
  62. REF_PRINT_COUNT("ECX_KEY", key);
  63. if (i > 0)
  64. return;
  65. REF_ASSERT_ISNT(i < 0);
  66. OPENSSL_free(key->propq);
  67. OPENSSL_secure_clear_free(key->privkey, key->keylen);
  68. CRYPTO_FREE_REF(&key->references);
  69. OPENSSL_free(key);
  70. }
  71. void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx)
  72. {
  73. key->libctx = libctx;
  74. }
  75. int ossl_ecx_key_up_ref(ECX_KEY *key)
  76. {
  77. int i;
  78. if (CRYPTO_UP_REF(&key->references, &i) <= 0)
  79. return 0;
  80. REF_PRINT_COUNT("ECX_KEY", key);
  81. REF_ASSERT_ISNT(i < 2);
  82. return ((i > 1) ? 1 : 0);
  83. }
  84. unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key)
  85. {
  86. key->privkey = OPENSSL_secure_zalloc(key->keylen);
  87. return key->privkey;
  88. }
  89. int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
  90. unsigned char *secret, size_t *secretlen, size_t outlen)
  91. {
  92. if (priv == NULL
  93. || priv->privkey == NULL
  94. || peer == NULL) {
  95. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
  96. return 0;
  97. }
  98. if (!ossl_assert(keylen == X25519_KEYLEN
  99. || keylen == X448_KEYLEN)) {
  100. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  101. return 0;
  102. }
  103. if (secret == NULL) {
  104. *secretlen = keylen;
  105. return 1;
  106. }
  107. if (outlen < keylen) {
  108. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  109. return 0;
  110. }
  111. if (keylen == X25519_KEYLEN) {
  112. #ifdef S390X_EC_ASM
  113. if (OPENSSL_s390xcap_P.pcc[1]
  114. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)) {
  115. if (s390x_x25519_mul(secret, peer->pubkey, priv->privkey) == 0) {
  116. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  117. return 0;
  118. }
  119. } else
  120. #endif
  121. if (ossl_x25519(secret, priv->privkey, peer->pubkey) == 0) {
  122. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  123. return 0;
  124. }
  125. } else {
  126. #ifdef S390X_EC_ASM
  127. if (OPENSSL_s390xcap_P.pcc[1]
  128. & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)) {
  129. if (s390x_x448_mul(secret, peer->pubkey, priv->privkey) == 0) {
  130. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  131. return 0;
  132. }
  133. } else
  134. #endif
  135. if (ossl_x448(secret, priv->privkey, peer->pubkey) == 0) {
  136. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
  137. return 0;
  138. }
  139. }
  140. *secretlen = keylen;
  141. return 1;
  142. }