ecx_meth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
  304. EVP_PKEY_X25519,
  305. EVP_PKEY_X25519,
  306. 0,
  307. "X25519",
  308. "OpenSSL X25519 algorithm",
  309. ecx_pub_decode,
  310. ecx_pub_encode,
  311. ecx_pub_cmp,
  312. ecx_pub_print,
  313. ecx_priv_decode,
  314. ecx_priv_encode,
  315. ecx_priv_print,
  316. ecx_size,
  317. ecx_bits,
  318. ecx_security_bits,
  319. 0, 0, 0, 0,
  320. ecx_cmp_parameters,
  321. 0, 0,
  322. ecx_free,
  323. ecx_ctrl,
  324. NULL,
  325. NULL,
  326. NULL,
  327. NULL,
  328. NULL,
  329. NULL,
  330. NULL,
  331. NULL,
  332. ecx_set_priv_key,
  333. ecx_set_pub_key,
  334. };
  335. const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
  336. EVP_PKEY_X448,
  337. EVP_PKEY_X448,
  338. 0,
  339. "X448",
  340. "OpenSSL X448 algorithm",
  341. ecx_pub_decode,
  342. ecx_pub_encode,
  343. ecx_pub_cmp,
  344. ecx_pub_print,
  345. ecx_priv_decode,
  346. ecx_priv_encode,
  347. ecx_priv_print,
  348. ecx_size,
  349. ecx_bits,
  350. ecx_security_bits,
  351. 0, 0, 0, 0,
  352. ecx_cmp_parameters,
  353. 0, 0,
  354. ecx_free,
  355. ecx_ctrl,
  356. NULL,
  357. NULL,
  358. NULL,
  359. NULL,
  360. NULL,
  361. NULL,
  362. NULL,
  363. NULL,
  364. ecx_set_priv_key,
  365. ecx_set_pub_key,
  366. };
  367. static int ecd_size25519(const EVP_PKEY *pkey)
  368. {
  369. return ED25519_SIGSIZE;
  370. }
  371. static int ecd_size448(const EVP_PKEY *pkey)
  372. {
  373. return ED448_SIGSIZE;
  374. }
  375. static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  376. X509_ALGOR *sigalg, ASN1_BIT_STRING *str,
  377. EVP_PKEY *pkey)
  378. {
  379. const ASN1_OBJECT *obj;
  380. int ptype;
  381. int nid;
  382. /* Sanity check: make sure it is ED25519/ED448 with absent parameters */
  383. X509_ALGOR_get0(&obj, &ptype, NULL, sigalg);
  384. nid = OBJ_obj2nid(obj);
  385. if ((nid != NID_ED25519 && nid != NID_ED448) || ptype != V_ASN1_UNDEF) {
  386. ECerr(EC_F_ECD_ITEM_VERIFY, EC_R_INVALID_ENCODING);
  387. return 0;
  388. }
  389. if (!EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey))
  390. return 0;
  391. return 2;
  392. }
  393. static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  394. X509_ALGOR *alg1, X509_ALGOR *alg2,
  395. ASN1_BIT_STRING *str)
  396. {
  397. /* Set algorithms identifiers */
  398. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
  399. if (alg2)
  400. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
  401. /* Algorithm idetifiers set: carry on as normal */
  402. return 3;
  403. }
  404. static int ecd_sig_info_set25519(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  405. const ASN1_STRING *sig)
  406. {
  407. X509_SIG_INFO_set(siginf, NID_undef, NID_ED25519, X25519_SECURITY_BITS,
  408. X509_SIG_INFO_TLS);
  409. return 1;
  410. }
  411. static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
  412. X509_ALGOR *alg1, X509_ALGOR *alg2,
  413. ASN1_BIT_STRING *str)
  414. {
  415. /* Set algorithm identifier */
  416. X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
  417. if (alg2 != NULL)
  418. X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
  419. /* Algorithm identifier set: carry on as normal */
  420. return 3;
  421. }
  422. static int ecd_sig_info_set448(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
  423. const ASN1_STRING *sig)
  424. {
  425. X509_SIG_INFO_set(siginf, NID_undef, NID_ED448, X448_SECURITY_BITS,
  426. X509_SIG_INFO_TLS);
  427. return 1;
  428. }
  429. const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
  430. EVP_PKEY_ED25519,
  431. EVP_PKEY_ED25519,
  432. 0,
  433. "ED25519",
  434. "OpenSSL ED25519 algorithm",
  435. ecx_pub_decode,
  436. ecx_pub_encode,
  437. ecx_pub_cmp,
  438. ecx_pub_print,
  439. ecx_priv_decode,
  440. ecx_priv_encode,
  441. ecx_priv_print,
  442. ecd_size25519,
  443. ecx_bits,
  444. ecx_security_bits,
  445. 0, 0, 0, 0,
  446. ecx_cmp_parameters,
  447. 0, 0,
  448. ecx_free,
  449. 0,
  450. NULL,
  451. NULL,
  452. ecd_item_verify,
  453. ecd_item_sign25519,
  454. ecd_sig_info_set25519,
  455. NULL,
  456. NULL,
  457. NULL,
  458. ecx_set_priv_key,
  459. ecx_set_pub_key,
  460. };
  461. const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
  462. EVP_PKEY_ED448,
  463. EVP_PKEY_ED448,
  464. 0,
  465. "ED448",
  466. "OpenSSL ED448 algorithm",
  467. ecx_pub_decode,
  468. ecx_pub_encode,
  469. ecx_pub_cmp,
  470. ecx_pub_print,
  471. ecx_priv_decode,
  472. ecx_priv_encode,
  473. ecx_priv_print,
  474. ecd_size448,
  475. ecx_bits,
  476. ecx_security_bits,
  477. 0, 0, 0, 0,
  478. ecx_cmp_parameters,
  479. 0, 0,
  480. ecx_free,
  481. 0,
  482. NULL,
  483. NULL,
  484. ecd_item_verify,
  485. ecd_item_sign448,
  486. ecd_sig_info_set448,
  487. NULL,
  488. NULL,
  489. NULL,
  490. ecx_set_priv_key,
  491. ecx_set_pub_key,
  492. };
  493. static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  494. {
  495. return ecx_key_op(pkey, ctx->pmeth->pkey_id, NULL, NULL, 0, KEY_OP_KEYGEN);
  496. }
  497. static int validate_ecx_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  498. size_t *keylen,
  499. const unsigned char **privkey,
  500. const unsigned char **pubkey)
  501. {
  502. const ECX_KEY *ecxkey, *peerkey;
  503. if (ctx->pkey == NULL || ctx->peerkey == NULL) {
  504. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_KEYS_NOT_SET);
  505. return 0;
  506. }
  507. ecxkey = ctx->pkey->pkey.ecx;
  508. peerkey = ctx->peerkey->pkey.ecx;
  509. if (ecxkey == NULL || ecxkey->privkey == NULL) {
  510. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PRIVATE_KEY);
  511. return 0;
  512. }
  513. if (peerkey == NULL) {
  514. ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PEER_KEY);
  515. return 0;
  516. }
  517. *privkey = ecxkey->privkey;
  518. *pubkey = peerkey->pubkey;
  519. return 1;
  520. }
  521. static int pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
  522. size_t *keylen)
  523. {
  524. const unsigned char *privkey, *pubkey;
  525. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  526. || (key != NULL
  527. && X25519(key, privkey, pubkey) == 0))
  528. return 0;
  529. *keylen = X25519_KEYLEN;
  530. return 1;
  531. }
  532. static int pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
  533. size_t *keylen)
  534. {
  535. const unsigned char *privkey, *pubkey;
  536. if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
  537. || (key != NULL
  538. && X448(key, privkey, pubkey) == 0))
  539. return 0;
  540. *keylen = X448_KEYLEN;
  541. return 1;
  542. }
  543. static int pkey_ecx_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  544. {
  545. /* Only need to handle peer key for derivation */
  546. if (type == EVP_PKEY_CTRL_PEER_KEY)
  547. return 1;
  548. return -2;
  549. }
  550. const EVP_PKEY_METHOD ecx25519_pkey_meth = {
  551. EVP_PKEY_X25519,
  552. 0, 0, 0, 0, 0, 0, 0,
  553. pkey_ecx_keygen,
  554. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  555. pkey_ecx_derive25519,
  556. pkey_ecx_ctrl,
  557. 0
  558. };
  559. const EVP_PKEY_METHOD ecx448_pkey_meth = {
  560. EVP_PKEY_X448,
  561. 0, 0, 0, 0, 0, 0, 0,
  562. pkey_ecx_keygen,
  563. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  564. pkey_ecx_derive448,
  565. pkey_ecx_ctrl,
  566. 0
  567. };
  568. static int pkey_ecd_sign25519(EVP_PKEY_CTX *ctx, unsigned char *sig,
  569. size_t *siglen, const unsigned char *tbs,
  570. size_t tbslen)
  571. {
  572. const ECX_KEY *edkey = ctx->pkey->pkey.ecx;
  573. if (sig == NULL) {
  574. *siglen = ED25519_SIGSIZE;
  575. return 1;
  576. }
  577. if (*siglen < ED25519_SIGSIZE) {
  578. ECerr(EC_F_PKEY_ECD_SIGN25519, EC_R_BUFFER_TOO_SMALL);
  579. return 0;
  580. }
  581. if (ED25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey) == 0)
  582. return 0;
  583. *siglen = ED25519_SIGSIZE;
  584. return 1;
  585. }
  586. static int pkey_ecd_digestsign25519(EVP_MD_CTX *ctx, unsigned char *sig,
  587. size_t *siglen, const unsigned char *tbs,
  588. size_t tbslen)
  589. {
  590. return pkey_ecd_sign25519(EVP_MD_CTX_pkey_ctx(ctx), sig, siglen, tbs,
  591. tbslen);
  592. }
  593. static int pkey_ecd_sign448(EVP_PKEY_CTX *ctx, unsigned char *sig,
  594. size_t *siglen, const unsigned char *tbs,
  595. size_t tbslen)
  596. {
  597. const ECX_KEY *edkey = ctx->pkey->pkey.ecx;
  598. if (sig == NULL) {
  599. *siglen = ED448_SIGSIZE;
  600. return 1;
  601. }
  602. if (*siglen < ED448_SIGSIZE) {
  603. ECerr(EC_F_PKEY_ECD_SIGN448, EC_R_BUFFER_TOO_SMALL);
  604. return 0;
  605. }
  606. if (ED448_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
  607. 0) == 0)
  608. return 0;
  609. *siglen = ED448_SIGSIZE;
  610. return 1;
  611. }
  612. static int pkey_ecd_digestsign448(EVP_MD_CTX *ctx, unsigned char *sig,
  613. size_t *siglen, const unsigned char *tbs,
  614. size_t tbslen)
  615. {
  616. return pkey_ecd_sign448(EVP_MD_CTX_pkey_ctx(ctx), sig, siglen, tbs, tbslen);
  617. }
  618. static int pkey_ecd_verify25519(EVP_PKEY_CTX *ctx, const unsigned char *sig,
  619. size_t siglen, const unsigned char *tbs,
  620. size_t tbslen)
  621. {
  622. const ECX_KEY *edkey = ctx->pkey->pkey.ecx;
  623. if (siglen != ED25519_SIGSIZE)
  624. return 0;
  625. return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
  626. }
  627. static int pkey_ecd_digestverify25519(EVP_MD_CTX *ctx, const unsigned char *sig,
  628. size_t siglen, const unsigned char *tbs,
  629. size_t tbslen)
  630. {
  631. return pkey_ecd_verify25519(EVP_MD_CTX_pkey_ctx(ctx), sig, siglen, tbs,
  632. tbslen);
  633. }
  634. static int pkey_ecd_verify448(EVP_PKEY_CTX *ctx, const unsigned char *sig,
  635. size_t siglen, const unsigned char *tbs,
  636. size_t tbslen)
  637. {
  638. const ECX_KEY *edkey = ctx->pkey->pkey.ecx;
  639. if (siglen != ED448_SIGSIZE)
  640. return 0;
  641. return ED448_verify(tbs, tbslen, sig, edkey->pubkey, NULL, 0);
  642. }
  643. static int pkey_ecd_digestverify448(EVP_MD_CTX *ctx, const unsigned char *sig,
  644. size_t siglen, const unsigned char *tbs,
  645. size_t tbslen)
  646. {
  647. return pkey_ecd_verify448(EVP_MD_CTX_pkey_ctx(ctx), sig, siglen, tbs,
  648. tbslen);
  649. }
  650. static int pkey_ecd_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  651. {
  652. switch (type) {
  653. case EVP_PKEY_CTRL_MD:
  654. /* Only NULL allowed as digest */
  655. if (p2 == NULL)
  656. return 1;
  657. ECerr(EC_F_PKEY_ECD_CTRL, EC_R_INVALID_DIGEST_TYPE);
  658. return 0;
  659. case EVP_PKEY_CTRL_DIGESTINIT:
  660. return 1;
  661. }
  662. return -2;
  663. }
  664. const EVP_PKEY_METHOD ed25519_pkey_meth = {
  665. EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  666. 0, 0, 0, 0, 0, 0,
  667. pkey_ecx_keygen,
  668. 0,
  669. pkey_ecd_sign25519,
  670. 0,
  671. pkey_ecd_verify25519,
  672. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  673. pkey_ecd_ctrl,
  674. 0,
  675. pkey_ecd_digestsign25519,
  676. pkey_ecd_digestverify25519
  677. };
  678. const EVP_PKEY_METHOD ed448_pkey_meth = {
  679. EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
  680. 0, 0, 0, 0, 0, 0,
  681. pkey_ecx_keygen,
  682. 0,
  683. pkey_ecd_sign448,
  684. 0,
  685. pkey_ecd_verify448,
  686. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  687. pkey_ecd_ctrl,
  688. 0,
  689. pkey_ecd_digestsign448,
  690. pkey_ecd_digestverify448
  691. };