ecx_meth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/x509.h>
  12. #include <openssl/ec.h>
  13. #include <openssl/rand.h>
  14. #include "internal/asn1_int.h"
  15. #include "internal/evp_int.h"
  16. #include "ec_lcl.h"
  17. #define X25519_BITS 253
  18. #define X25519_SECURITY_BITS 128
  19. #define ED25519_SIGSIZE 64
  20. #define X448_BITS 448
  21. #define ED448_BITS 456
  22. #define X448_SECURITY_BITS 224
  23. #define ED448_SIGSIZE 114
  24. #define ISX448(id) ((id) == EVP_PKEY_X448)
  25. #define IS25519(id) ((id) == EVP_PKEY_X25519 || (id) == EVP_PKEY_ED25519)
  26. #define KEYLENID(id) (IS25519(id) ? X25519_KEYLEN \
  27. : ((id) == EVP_PKEY_X448 ? X448_KEYLEN \
  28. : ED448_KEYLEN))
  29. #define KEYLEN(p) KEYLENID((p)->ameth->pkey_id)
  30. typedef enum {
  31. KEY_OP_PUBLIC,
  32. KEY_OP_PRIVATE,
  33. KEY_OP_KEYGEN
  34. } ecx_key_op_t;
  35. /* Setup EVP_PKEY using public, private or generation */
  36. static int ecx_key_op(EVP_PKEY *pkey, int id, const X509_ALGOR *palg,
  37. const unsigned char *p, int plen, ecx_key_op_t op)
  38. {
  39. ECX_KEY *key = NULL;
  40. unsigned char *privkey, *pubkey;
  41. if (op != KEY_OP_KEYGEN) {
  42. if (palg != NULL) {
  43. int ptype;
  44. /* Algorithm parameters must be absent */
  45. X509_ALGOR_get0(NULL, &ptype, NULL, palg);
  46. if (ptype != V_ASN1_UNDEF) {
  47. ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
  48. return 0;
  49. }
  50. }
  51. if (p == NULL || plen != KEYLENID(id)) {
  52. ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
  53. return 0;
  54. }
  55. }
  56. key = OPENSSL_zalloc(sizeof(*key));
  57. if (key == NULL) {
  58. ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
  59. return 0;
  60. }
  61. pubkey = key->pubkey;
  62. if (op == KEY_OP_PUBLIC) {
  63. memcpy(pubkey, p, plen);
  64. } else {
  65. privkey = key->privkey = OPENSSL_secure_malloc(KEYLENID(id));
  66. if (privkey == NULL) {
  67. ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
  68. goto err;
  69. }
  70. if (op == KEY_OP_KEYGEN) {
  71. if (RAND_priv_bytes(privkey, KEYLENID(id)) <= 0) {
  72. OPENSSL_secure_free(privkey);
  73. key->privkey = NULL;
  74. goto err;
  75. }
  76. if (id == EVP_PKEY_X25519) {
  77. privkey[0] &= 248;
  78. privkey[X25519_KEYLEN - 1] &= 127;
  79. privkey[X25519_KEYLEN - 1] |= 64;
  80. } else if (id == EVP_PKEY_X448) {
  81. privkey[0] &= 252;
  82. privkey[X448_KEYLEN - 1] |= 128;
  83. }
  84. } else {
  85. memcpy(privkey, p, KEYLENID(id));
  86. }
  87. switch (id) {
  88. case EVP_PKEY_X25519:
  89. X25519_public_from_private(pubkey, privkey);
  90. break;
  91. case EVP_PKEY_ED25519:
  92. ED25519_public_from_private(pubkey, privkey);
  93. break;
  94. case EVP_PKEY_X448:
  95. X448_public_from_private(pubkey, privkey);
  96. break;
  97. case EVP_PKEY_ED448:
  98. ED448_public_from_private(pubkey, privkey);
  99. break;
  100. }
  101. }
  102. EVP_PKEY_assign(pkey, id, key);
  103. return 1;
  104. err:
  105. OPENSSL_free(key);
  106. return 0;
  107. }
  108. static int ecx_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  109. {
  110. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  111. unsigned char *penc;
  112. if (ecxkey == NULL) {
  113. ECerr(EC_F_ECX_PUB_ENCODE, EC_R_INVALID_KEY);
  114. return 0;
  115. }
  116. penc = OPENSSL_memdup(ecxkey->pubkey, KEYLEN(pkey));
  117. if (penc == NULL) {
  118. ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  119. return 0;
  120. }
  121. if (!X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
  122. V_ASN1_UNDEF, NULL, penc, KEYLEN(pkey))) {
  123. OPENSSL_free(penc);
  124. ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. static int ecx_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  130. {
  131. const unsigned char *p;
  132. int pklen;
  133. X509_ALGOR *palg;
  134. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  135. return 0;
  136. return ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, pklen,
  137. KEY_OP_PUBLIC);
  138. }
  139. static int ecx_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  140. {
  141. const ECX_KEY *akey = a->pkey.ecx;
  142. const ECX_KEY *bkey = b->pkey.ecx;
  143. if (akey == NULL || bkey == NULL)
  144. return -2;
  145. return CRYPTO_memcmp(akey->pubkey, bkey->pubkey, KEYLEN(a)) == 0;
  146. }
  147. static int ecx_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
  148. {
  149. const unsigned char *p;
  150. int plen;
  151. ASN1_OCTET_STRING *oct = NULL;
  152. const X509_ALGOR *palg;
  153. int rv;
  154. if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8))
  155. return 0;
  156. oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
  157. if (oct == NULL) {
  158. p = NULL;
  159. plen = 0;
  160. } else {
  161. p = ASN1_STRING_get0_data(oct);
  162. plen = ASN1_STRING_length(oct);
  163. }
  164. rv = ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, plen, KEY_OP_PRIVATE);
  165. ASN1_OCTET_STRING_free(oct);
  166. return rv;
  167. }
  168. static int ecx_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  169. {
  170. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  171. ASN1_OCTET_STRING oct;
  172. unsigned char *penc = NULL;
  173. int penclen;
  174. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  175. ECerr(EC_F_ECX_PRIV_ENCODE, EC_R_INVALID_PRIVATE_KEY);
  176. return 0;
  177. }
  178. oct.data = ecxkey->privkey;
  179. oct.length = KEYLEN(pkey);
  180. oct.flags = 0;
  181. penclen = i2d_ASN1_OCTET_STRING(&oct, &penc);
  182. if (penclen < 0) {
  183. ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  184. return 0;
  185. }
  186. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
  187. V_ASN1_UNDEF, NULL, penc, penclen)) {
  188. OPENSSL_clear_free(penc, penclen);
  189. ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
  190. return 0;
  191. }
  192. return 1;
  193. }
  194. static int ecx_size(const EVP_PKEY *pkey)
  195. {
  196. return KEYLEN(pkey);
  197. }
  198. static int ecx_bits(const EVP_PKEY *pkey)
  199. {
  200. if (IS25519(pkey->ameth->pkey_id)) {
  201. return X25519_BITS;
  202. } else if(ISX448(pkey->ameth->pkey_id)) {
  203. return X448_BITS;
  204. } else {
  205. return ED448_BITS;
  206. }
  207. }
  208. static int ecx_security_bits(const EVP_PKEY *pkey)
  209. {
  210. if (IS25519(pkey->ameth->pkey_id)) {
  211. return X25519_SECURITY_BITS;
  212. } else {
  213. return X448_SECURITY_BITS;
  214. }
  215. }
  216. static void ecx_free(EVP_PKEY *pkey)
  217. {
  218. if (pkey->pkey.ecx != NULL)
  219. OPENSSL_secure_clear_free(pkey->pkey.ecx->privkey, KEYLEN(pkey));
  220. OPENSSL_free(pkey->pkey.ecx);
  221. }
  222. /* "parameters" are always equal */
  223. static int ecx_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  224. {
  225. return 1;
  226. }
  227. static int ecx_key_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  228. ASN1_PCTX *ctx, ecx_key_op_t op)
  229. {
  230. const ECX_KEY *ecxkey = pkey->pkey.ecx;
  231. const char *nm = OBJ_nid2ln(pkey->ameth->pkey_id);
  232. if (op == KEY_OP_PRIVATE) {
  233. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  234. if (BIO_printf(bp, "%*s<INVALID PRIVATE KEY>\n", indent, "") <= 0)
  235. return 0;
  236. return 1;
  237. }
  238. if (BIO_printf(bp, "%*s%s Private-Key:\n", indent, "", nm) <= 0)
  239. return 0;
  240. if (BIO_printf(bp, "%*spriv:\n", indent, "") <= 0)
  241. return 0;
  242. if (ASN1_buf_print(bp, ecxkey->privkey, KEYLEN(pkey),
  243. indent + 4) == 0)
  244. return 0;
  245. } else {
  246. if (ecxkey == NULL) {
  247. if (BIO_printf(bp, "%*s<INVALID PUBLIC KEY>\n", indent, "") <= 0)
  248. return 0;
  249. return 1;
  250. }
  251. if (BIO_printf(bp, "%*s%s Public-Key:\n", indent, "", nm) <= 0)
  252. return 0;
  253. }
  254. if (BIO_printf(bp, "%*spub:\n", indent, "") <= 0)
  255. return 0;
  256. if (ASN1_buf_print(bp, ecxkey->pubkey, KEYLEN(pkey),
  257. indent + 4) == 0)
  258. return 0;
  259. return 1;
  260. }
  261. static int ecx_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  262. ASN1_PCTX *ctx)
  263. {
  264. return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PRIVATE);
  265. }
  266. static int ecx_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  267. ASN1_PCTX *ctx)
  268. {
  269. return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PUBLIC);
  270. }
  271. static int ecx_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
  272. {
  273. switch (op) {
  274. case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
  275. return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, arg2, arg1,
  276. KEY_OP_PUBLIC);
  277. case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
  278. if (pkey->pkey.ecx != NULL) {
  279. unsigned char **ppt = arg2;
  280. *ppt = OPENSSL_memdup(pkey->pkey.ecx->pubkey, KEYLEN(pkey));
  281. if (*ppt != NULL)
  282. return KEYLEN(pkey);
  283. }
  284. return 0;
  285. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  286. *(int *)arg2 = NID_sha256;
  287. return 2;
  288. default:
  289. return -2;
  290. }
  291. }
  292. static int ecx_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
  293. size_t len)
  294. {
  295. return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, priv, len,
  296. KEY_OP_PRIVATE);
  297. }
  298. static int ecx_set_pub_key(EVP_PKEY *pkey, const unsigned char *pub, size_t len)
  299. {
  300. return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, pub, len,
  301. KEY_OP_PUBLIC);
  302. }
  303. static int ecx_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
  304. size_t *len)
  305. {
  306. const ECX_KEY *key = pkey->pkey.ecx;
  307. if (priv == NULL) {
  308. *len = KEYLENID(pkey->ameth->pkey_id);
  309. return 1;
  310. }
  311. if (key == NULL
  312. || key->privkey == NULL
  313. || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
  314. return 0;
  315. *len = KEYLENID(pkey->ameth->pkey_id);
  316. memcpy(priv, key->privkey, *len);
  317. return 1;
  318. }
  319. static int ecx_get_pub_key(const EVP_PKEY *pkey, unsigned char *pub,
  320. size_t *len)
  321. {
  322. const ECX_KEY *key = pkey->pkey.ecx;
  323. if (pub == NULL) {
  324. *len = KEYLENID(pkey->ameth->pkey_id);
  325. return 1;
  326. }
  327. if (key == NULL
  328. || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
  329. return 0;
  330. *len = KEYLENID(pkey->ameth->pkey_id);
  331. memcpy(pub, key->pubkey, *len);
  332. return 1;
  333. }
  334. const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
  335. EVP_PKEY_X25519,
  336. EVP_PKEY_X25519,
  337. 0,
  338. "X25519",
  339. "OpenSSL X25519 algorithm",
  340. ecx_pub_decode,
  341. ecx_pub_encode,
  342. ecx_pub_cmp,
  343. ecx_pub_print,
  344. ecx_priv_decode,
  345. ecx_priv_encode,
  346. ecx_priv_print,
  347. ecx_size,
  348. ecx_bits,
  349. ecx_security_bits,
  350. 0, 0, 0, 0,
  351. ecx_cmp_parameters,
  352. 0, 0,
  353. ecx_free,
  354. ecx_ctrl,
  355. NULL,
  356. NULL,
  357. NULL,
  358. NULL,
  359. NULL,
  360. NULL,
  361. NULL,
  362. NULL,
  363. ecx_set_priv_key,
  364. ecx_set_pub_key,
  365. ecx_get_priv_key,
  366. ecx_get_pub_key,
  367. };
  368. const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
  369. EVP_PKEY_X448,
  370. EVP_PKEY_X448,
  371. 0,
  372. "X448",
  373. "OpenSSL X448 algorithm",
  374. ecx_pub_decode,
  375. ecx_pub_encode,
  376. ecx_pub_cmp,
  377. ecx_pub_print,
  378. ecx_priv_decode,
  379. ecx_priv_encode,
  380. ecx_priv_print,
  381. ecx_size,
  382. ecx_bits,
  383. ecx_security_bits,
  384. 0, 0, 0, 0,
  385. ecx_cmp_parameters,
  386. 0, 0,
  387. ecx_free,
  388. ecx_ctrl,
  389. NULL,
  390. NULL,
  391. NULL,
  392. NULL,
  393. NULL,
  394. NULL,
  395. NULL,
  396. NULL,
  397. ecx_set_priv_key,
  398. ecx_set_pub_key,
  399. ecx_get_priv_key,
  400. ecx_get_pub_key,
  401. };
  402. static int ecd_size25519(const EVP_PKEY *pkey)
  403. {
  404. return ED25519_SIGSIZE;
  405. }
  406. static int ecd_size448(const EVP_PKEY *pkey)
  407. {
  408. return ED448_SIGSIZE;
  409. }
  410. static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  411. X509_ALGOR *sigalg, ASN1_BIT_STRING *str,
  412. EVP_PKEY *pkey)
  413. {
  414. const ASN1_OBJECT *obj;
  415. int ptype;
  416. int nid;
  417. /* Sanity check: make sure it is ED25519/ED448 with absent parameters */
  418. X509_ALGOR_get0(&obj, &ptype, NULL, sigalg);
  419. nid = OBJ_obj2nid(obj);
  420. if ((nid != NID_ED25519 && nid != NID_ED448) || ptype != V_ASN1_UNDEF) {
  421. ECerr(EC_F_ECD_ITEM_VERIFY, EC_R_INVALID_ENCODING);
  422. return 0;
  423. }
  424. if (!EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey))
  425. return 0;
  426. return 2;
  427. }
  428. static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  429. X509_ALGOR *alg1, X509_ALGOR *alg2,
  430. ASN1_BIT_STRING *str)
  431. {
  432. /* Set algorithms identifiers */
  433. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
  434. if (alg2)
  435. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
  436. /* Algorithm idetifiers set: carry on as normal */
  437. return 3;
  438. }
  439. static int ecd_sig_info_set25519(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  440. const ASN1_STRING *sig)
  441. {
  442. X509_SIG_INFO_set(siginf, NID_undef, NID_ED25519, X25519_SECURITY_BITS,
  443. X509_SIG_INFO_TLS);
  444. return 1;
  445. }
  446. static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  447. X509_ALGOR *alg1, X509_ALGOR *alg2,
  448. ASN1_BIT_STRING *str)
  449. {
  450. /* Set algorithm identifier */
  451. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
  452. if (alg2 != NULL)
  453. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
  454. /* Algorithm identifier set: carry on as normal */
  455. return 3;
  456. }
  457. static int ecd_sig_info_set448(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  458. const ASN1_STRING *sig)
  459. {
  460. X509_SIG_INFO_set(siginf, NID_undef, NID_ED448, X448_SECURITY_BITS,
  461. X509_SIG_INFO_TLS);
  462. return 1;
  463. }
  464. const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
  465. EVP_PKEY_ED25519,
  466. EVP_PKEY_ED25519,
  467. 0,
  468. "ED25519",
  469. "OpenSSL ED25519 algorithm",
  470. ecx_pub_decode,
  471. ecx_pub_encode,
  472. ecx_pub_cmp,
  473. ecx_pub_print,
  474. ecx_priv_decode,
  475. ecx_priv_encode,
  476. ecx_priv_print,
  477. ecd_size25519,
  478. ecx_bits,
  479. ecx_security_bits,
  480. 0, 0, 0, 0,
  481. ecx_cmp_parameters,
  482. 0, 0,
  483. ecx_free,
  484. 0,
  485. NULL,
  486. NULL,
  487. ecd_item_verify,
  488. ecd_item_sign25519,
  489. ecd_sig_info_set25519,
  490. NULL,
  491. NULL,
  492. NULL,
  493. ecx_set_priv_key,
  494. ecx_set_pub_key,
  495. ecx_get_priv_key,
  496. ecx_get_pub_key,
  497. };
  498. const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
  499. EVP_PKEY_ED448,
  500. EVP_PKEY_ED448,
  501. 0,
  502. "ED448",
  503. "OpenSSL ED448 algorithm",
  504. ecx_pub_decode,
  505. ecx_pub_encode,
  506. ecx_pub_cmp,
  507. ecx_pub_print,
  508. ecx_priv_decode,
  509. ecx_priv_encode,
  510. ecx_priv_print,
  511. ecd_size448,
  512. ecx_bits,
  513. ecx_security_bits,
  514. 0, 0, 0, 0,
  515. ecx_cmp_parameters,
  516. 0, 0,
  517. ecx_free,
  518. 0,
  519. NULL,
  520. NULL,
  521. ecd_item_verify,
  522. ecd_item_sign448,
  523. ecd_sig_info_set448,
  524. NULL,
  525. NULL,
  526. NULL,
  527. ecx_set_priv_key,
  528. ecx_set_pub_key,
  529. ecx_get_priv_key,
  530. ecx_get_pub_key,
  531. };
  532. static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  533. {
  534. return ecx_key_op(pkey, ctx->pmeth->pkey_id, NULL, NULL, 0, KEY_OP_KEYGEN);
  535. }
  536. static int validate_ecx_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  537. size_t *keylen,
  538. const unsigned char **privkey,
  539. const unsigned char **pubkey)
  540. {
  541. const ECX_KEY *ecxkey, *peerkey;
  542. if (ctx->pkey == NULL || ctx->peerkey == NULL) {
  543. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_KEYS_NOT_SET);
  544. return 0;
  545. }
  546. ecxkey = ctx->pkey->pkey.ecx;
  547. peerkey = ctx->peerkey->pkey.ecx;
  548. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  549. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PRIVATE_KEY);
  550. return 0;
  551. }
  552. if (peerkey == NULL) {
  553. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PEER_KEY);
  554. return 0;
  555. }
  556. *privkey = ecxkey->privkey;
  557. *pubkey = peerkey->pubkey;
  558. return 1;
  559. }
  560. static int pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
  561. size_t *keylen)
  562. {
  563. const unsigned char *privkey, *pubkey;
  564. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  565. || (key != NULL
  566. && X25519(key, privkey, pubkey) == 0))
  567. return 0;
  568. *keylen = X25519_KEYLEN;
  569. return 1;
  570. }
  571. static int pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
  572. size_t *keylen)
  573. {
  574. const unsigned char *privkey, *pubkey;
  575. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  576. || (key != NULL
  577. && X448(key, privkey, pubkey) == 0))
  578. return 0;
  579. *keylen = X448_KEYLEN;
  580. return 1;
  581. }
  582. static int pkey_ecx_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  583. {
  584. /* Only need to handle peer key for derivation */
  585. if (type == EVP_PKEY_CTRL_PEER_KEY)
  586. return 1;
  587. return -2;
  588. }
  589. const EVP_PKEY_METHOD ecx25519_pkey_meth = {
  590. EVP_PKEY_X25519,
  591. 0, 0, 0, 0, 0, 0, 0,
  592. pkey_ecx_keygen,
  593. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  594. pkey_ecx_derive25519,
  595. pkey_ecx_ctrl,
  596. 0
  597. };
  598. const EVP_PKEY_METHOD ecx448_pkey_meth = {
  599. EVP_PKEY_X448,
  600. 0, 0, 0, 0, 0, 0, 0,
  601. pkey_ecx_keygen,
  602. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  603. pkey_ecx_derive448,
  604. pkey_ecx_ctrl,
  605. 0
  606. };
  607. static int pkey_ecd_digestsign25519(EVP_MD_CTX *ctx, unsigned char *sig,
  608. size_t *siglen, const unsigned char *tbs,
  609. size_t tbslen)
  610. {
  611. const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
  612. if (sig == NULL) {
  613. *siglen = ED25519_SIGSIZE;
  614. return 1;
  615. }
  616. if (*siglen < ED25519_SIGSIZE) {
  617. ECerr(EC_F_PKEY_ECD_DIGESTSIGN25519, EC_R_BUFFER_TOO_SMALL);
  618. return 0;
  619. }
  620. if (ED25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey) == 0)
  621. return 0;
  622. *siglen = ED25519_SIGSIZE;
  623. return 1;
  624. }
  625. static int pkey_ecd_digestsign448(EVP_MD_CTX *ctx, unsigned char *sig,
  626. size_t *siglen, const unsigned char *tbs,
  627. size_t tbslen)
  628. {
  629. const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
  630. if (sig == NULL) {
  631. *siglen = ED448_SIGSIZE;
  632. return 1;
  633. }
  634. if (*siglen < ED448_SIGSIZE) {
  635. ECerr(EC_F_PKEY_ECD_DIGESTSIGN448, EC_R_BUFFER_TOO_SMALL);
  636. return 0;
  637. }
  638. if (ED448_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
  639. 0) == 0)
  640. return 0;
  641. *siglen = ED448_SIGSIZE;
  642. return 1;
  643. }
  644. static int pkey_ecd_digestverify25519(EVP_MD_CTX *ctx, const unsigned char *sig,
  645. size_t siglen, const unsigned char *tbs,
  646. size_t tbslen)
  647. {
  648. const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
  649. if (siglen != ED25519_SIGSIZE)
  650. return 0;
  651. return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
  652. }
  653. static int pkey_ecd_digestverify448(EVP_MD_CTX *ctx, const unsigned char *sig,
  654. size_t siglen, const unsigned char *tbs,
  655. size_t tbslen)
  656. {
  657. const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
  658. if (siglen != ED448_SIGSIZE)
  659. return 0;
  660. return ED448_verify(tbs, tbslen, sig, edkey->pubkey, NULL, 0);
  661. }
  662. static int pkey_ecd_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  663. {
  664. switch (type) {
  665. case EVP_PKEY_CTRL_MD:
  666. /* Only NULL allowed as digest */
  667. if (p2 == NULL || (const EVP_MD *)p2 == EVP_md_null())
  668. return 1;
  669. ECerr(EC_F_PKEY_ECD_CTRL, EC_R_INVALID_DIGEST_TYPE);
  670. return 0;
  671. case EVP_PKEY_CTRL_DIGESTINIT:
  672. return 1;
  673. }
  674. return -2;
  675. }
  676. const EVP_PKEY_METHOD ed25519_pkey_meth = {
  677. EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  678. 0, 0, 0, 0, 0, 0,
  679. pkey_ecx_keygen,
  680. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  681. pkey_ecd_ctrl,
  682. 0,
  683. pkey_ecd_digestsign25519,
  684. pkey_ecd_digestverify25519
  685. };
  686. const EVP_PKEY_METHOD ed448_pkey_meth = {
  687. EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  688. 0, 0, 0, 0, 0, 0,
  689. pkey_ecx_keygen,
  690. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  691. pkey_ecd_ctrl,
  692. 0,
  693. pkey_ecd_digestsign448,
  694. pkey_ecd_digestverify448
  695. };