2
0

ecx_backend.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/err.h>
  15. #include "crypto/ecx.h"
  16. #include "ecx_backend.h"
  17. /*
  18. * The intention with the "backend" source file is to offer backend support
  19. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  20. * implementations alike.
  21. */
  22. int ossl_ecx_public_from_private(ECX_KEY *key)
  23. {
  24. switch (key->type) {
  25. case ECX_KEY_TYPE_X25519:
  26. ossl_x25519_public_from_private(key->pubkey, key->privkey);
  27. break;
  28. case ECX_KEY_TYPE_ED25519:
  29. if (!ossl_ed25519_public_from_private(key->libctx, key->pubkey,
  30. key->privkey, key->propq)) {
  31. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  32. return 0;
  33. }
  34. break;
  35. case ECX_KEY_TYPE_X448:
  36. ossl_x448_public_from_private(key->pubkey, key->privkey);
  37. break;
  38. case ECX_KEY_TYPE_ED448:
  39. if (!ossl_ed448_public_from_private(key->libctx, key->pubkey,
  40. key->privkey, key->propq)) {
  41. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  42. return 0;
  43. }
  44. break;
  45. }
  46. return 1;
  47. }
  48. int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
  49. int include_private)
  50. {
  51. size_t privkeylen = 0, pubkeylen = 0;
  52. const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
  53. unsigned char *pubkey;
  54. if (ecx == NULL)
  55. return 0;
  56. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  57. if (include_private)
  58. param_priv_key =
  59. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  60. if (param_pub_key == NULL && param_priv_key == NULL)
  61. return 0;
  62. if (param_priv_key != NULL
  63. && !OSSL_PARAM_get_octet_string(param_priv_key,
  64. (void **)&ecx->privkey, ecx->keylen,
  65. &privkeylen))
  66. return 0;
  67. pubkey = ecx->pubkey;
  68. if (param_pub_key != NULL
  69. && !OSSL_PARAM_get_octet_string(param_pub_key,
  70. (void **)&pubkey,
  71. sizeof(ecx->pubkey), &pubkeylen))
  72. return 0;
  73. if ((param_pub_key != NULL && pubkeylen != ecx->keylen)
  74. || (param_priv_key != NULL && privkeylen != ecx->keylen))
  75. return 0;
  76. if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
  77. return 0;
  78. ecx->haspubkey = 1;
  79. return 1;
  80. }
  81. ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
  82. {
  83. ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
  84. if (ret == NULL) {
  85. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  86. return NULL;
  87. }
  88. ret->lock = CRYPTO_THREAD_lock_new();
  89. if (ret->lock == NULL) {
  90. OPENSSL_free(ret);
  91. return NULL;
  92. }
  93. ret->libctx = key->libctx;
  94. ret->haspubkey = key->haspubkey;
  95. ret->keylen = key->keylen;
  96. ret->type = key->type;
  97. ret->references = 1;
  98. if (key->propq != NULL) {
  99. ret->propq = OPENSSL_strdup(key->propq);
  100. if (ret->propq == NULL)
  101. goto err;
  102. }
  103. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  104. memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
  105. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  106. && key->privkey != NULL) {
  107. if (ossl_ecx_key_allocate_privkey(ret) == NULL)
  108. goto err;
  109. memcpy(ret->privkey, key->privkey, ret->keylen);
  110. }
  111. return ret;
  112. err:
  113. ossl_ecx_key_free(ret);
  114. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  115. return NULL;
  116. }
  117. #ifndef FIPS_MODULE
  118. ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
  119. const unsigned char *p, int plen,
  120. int id, ecx_key_op_t op,
  121. OSSL_LIB_CTX *libctx, const char *propq)
  122. {
  123. ECX_KEY *key = NULL;
  124. unsigned char *privkey, *pubkey;
  125. if (op != KEY_OP_KEYGEN) {
  126. if (palg != NULL) {
  127. int ptype;
  128. /* Algorithm parameters must be absent */
  129. X509_ALGOR_get0(NULL, &ptype, NULL, palg);
  130. if (ptype != V_ASN1_UNDEF) {
  131. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  132. return 0;
  133. }
  134. if (id == EVP_PKEY_NONE)
  135. id = OBJ_obj2nid(palg->algorithm);
  136. else if (id != OBJ_obj2nid(palg->algorithm)) {
  137. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  138. return 0;
  139. }
  140. }
  141. if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
  142. ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
  143. return 0;
  144. }
  145. }
  146. key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
  147. if (key == NULL) {
  148. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  149. return 0;
  150. }
  151. pubkey = key->pubkey;
  152. if (op == KEY_OP_PUBLIC) {
  153. memcpy(pubkey, p, plen);
  154. } else {
  155. privkey = ossl_ecx_key_allocate_privkey(key);
  156. if (privkey == NULL) {
  157. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  158. goto err;
  159. }
  160. if (op == KEY_OP_KEYGEN) {
  161. if (id != EVP_PKEY_NONE) {
  162. if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id), 0) <= 0)
  163. goto err;
  164. if (id == EVP_PKEY_X25519) {
  165. privkey[0] &= 248;
  166. privkey[X25519_KEYLEN - 1] &= 127;
  167. privkey[X25519_KEYLEN - 1] |= 64;
  168. } else if (id == EVP_PKEY_X448) {
  169. privkey[0] &= 252;
  170. privkey[X448_KEYLEN - 1] |= 128;
  171. }
  172. }
  173. } else {
  174. memcpy(privkey, p, KEYLENID(id));
  175. }
  176. if (!ossl_ecx_public_from_private(key)) {
  177. ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
  178. goto err;
  179. }
  180. }
  181. return key;
  182. err:
  183. ossl_ecx_key_free(key);
  184. return NULL;
  185. }
  186. ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  187. OSSL_LIB_CTX *libctx, const char *propq)
  188. {
  189. ECX_KEY *ecx = NULL;
  190. const unsigned char *p;
  191. int plen;
  192. ASN1_OCTET_STRING *oct = NULL;
  193. const X509_ALGOR *palg;
  194. if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
  195. return 0;
  196. oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
  197. if (oct == NULL) {
  198. p = NULL;
  199. plen = 0;
  200. } else {
  201. p = ASN1_STRING_get0_data(oct);
  202. plen = ASN1_STRING_length(oct);
  203. }
  204. /*
  205. * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
  206. * on its own.
  207. */
  208. ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
  209. libctx, propq);
  210. ASN1_OCTET_STRING_free(oct);
  211. return ecx;
  212. }
  213. #endif