2 * Copyright 2006-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/ec.h>
14 #include <openssl/rand.h>
15 #include "internal/asn1_int.h"
16 #include "internal/evp_int.h"
18 #include "curve448/curve448_lcl.h"
20 #define X25519_BITS 253
21 #define X25519_SECURITY_BITS 128
23 #define ED25519_SIGSIZE 64
26 #define ED448_BITS 456
27 #define X448_SECURITY_BITS 224
29 #define ED448_SIGSIZE 114
31 #define ISX448(id) ((id) == EVP_PKEY_X448)
32 #define IS25519(id) ((id) == EVP_PKEY_X25519 || (id) == EVP_PKEY_ED25519)
33 #define KEYLENID(id) (IS25519(id) ? X25519_KEYLEN \
34 : ((id) == EVP_PKEY_X448 ? X448_KEYLEN \
36 #define KEYLEN(p) KEYLENID((p)->ameth->pkey_id)
45 /* Setup EVP_PKEY using public, private or generation */
46 static int ecx_key_op(EVP_PKEY *pkey, int id, const X509_ALGOR *palg,
47 const unsigned char *p, int plen, ecx_key_op_t op)
50 unsigned char *privkey, *pubkey;
52 if (op != KEY_OP_KEYGEN) {
56 /* Algorithm parameters must be absent */
57 X509_ALGOR_get0(NULL, &ptype, NULL, palg);
58 if (ptype != V_ASN1_UNDEF) {
59 ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
64 if (p == NULL || plen != KEYLENID(id)) {
65 ECerr(EC_F_ECX_KEY_OP, EC_R_INVALID_ENCODING);
70 key = OPENSSL_zalloc(sizeof(*key));
72 ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
77 if (op == KEY_OP_PUBLIC) {
78 memcpy(pubkey, p, plen);
80 privkey = key->privkey = OPENSSL_secure_malloc(KEYLENID(id));
81 if (privkey == NULL) {
82 ECerr(EC_F_ECX_KEY_OP, ERR_R_MALLOC_FAILURE);
85 if (op == KEY_OP_KEYGEN) {
86 if (RAND_priv_bytes(privkey, KEYLENID(id)) <= 0) {
87 OPENSSL_secure_free(privkey);
91 if (id == EVP_PKEY_X25519) {
93 privkey[X25519_KEYLEN - 1] &= 127;
94 privkey[X25519_KEYLEN - 1] |= 64;
95 } else if (id == EVP_PKEY_X448) {
97 privkey[X448_KEYLEN - 1] |= 128;
100 memcpy(privkey, p, KEYLENID(id));
103 case EVP_PKEY_X25519:
104 X25519_public_from_private(pubkey, privkey);
106 case EVP_PKEY_ED25519:
107 ED25519_public_from_private(pubkey, privkey);
110 X448_public_from_private(pubkey, privkey);
113 ED448_public_from_private(pubkey, privkey);
118 EVP_PKEY_assign(pkey, id, key);
125 static int ecx_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
127 const ECX_KEY *ecxkey = pkey->pkey.ecx;
130 if (ecxkey == NULL) {
131 ECerr(EC_F_ECX_PUB_ENCODE, EC_R_INVALID_KEY);
135 penc = OPENSSL_memdup(ecxkey->pubkey, KEYLEN(pkey));
137 ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
141 if (!X509_PUBKEY_set0_param(pk, OBJ_nid2obj(pkey->ameth->pkey_id),
142 V_ASN1_UNDEF, NULL, penc, KEYLEN(pkey))) {
144 ECerr(EC_F_ECX_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
150 static int ecx_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
152 const unsigned char *p;
156 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
158 return ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, pklen,
162 static int ecx_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
164 const ECX_KEY *akey = a->pkey.ecx;
165 const ECX_KEY *bkey = b->pkey.ecx;
167 if (akey == NULL || bkey == NULL)
170 return CRYPTO_memcmp(akey->pubkey, bkey->pubkey, KEYLEN(a)) == 0;
173 static int ecx_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
175 const unsigned char *p;
177 ASN1_OCTET_STRING *oct = NULL;
178 const X509_ALGOR *palg;
181 if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8))
184 oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
189 p = ASN1_STRING_get0_data(oct);
190 plen = ASN1_STRING_length(oct);
193 rv = ecx_key_op(pkey, pkey->ameth->pkey_id, palg, p, plen, KEY_OP_PRIVATE);
194 ASN1_OCTET_STRING_free(oct);
198 static int ecx_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
200 const ECX_KEY *ecxkey = pkey->pkey.ecx;
201 ASN1_OCTET_STRING oct;
202 unsigned char *penc = NULL;
205 if (ecxkey == NULL || ecxkey->privkey == NULL) {
206 ECerr(EC_F_ECX_PRIV_ENCODE, EC_R_INVALID_PRIVATE_KEY);
210 oct.data = ecxkey->privkey;
211 oct.length = KEYLEN(pkey);
214 penclen = i2d_ASN1_OCTET_STRING(&oct, &penc);
216 ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
220 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(pkey->ameth->pkey_id), 0,
221 V_ASN1_UNDEF, NULL, penc, penclen)) {
222 OPENSSL_clear_free(penc, penclen);
223 ECerr(EC_F_ECX_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
230 static int ecx_size(const EVP_PKEY *pkey)
235 static int ecx_bits(const EVP_PKEY *pkey)
237 if (IS25519(pkey->ameth->pkey_id)) {
239 } else if(ISX448(pkey->ameth->pkey_id)) {
246 static int ecx_security_bits(const EVP_PKEY *pkey)
248 if (IS25519(pkey->ameth->pkey_id)) {
249 return X25519_SECURITY_BITS;
251 return X448_SECURITY_BITS;
255 static void ecx_free(EVP_PKEY *pkey)
257 if (pkey->pkey.ecx != NULL)
258 OPENSSL_secure_clear_free(pkey->pkey.ecx->privkey, KEYLEN(pkey));
259 OPENSSL_free(pkey->pkey.ecx);
262 /* "parameters" are always equal */
263 static int ecx_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
268 static int ecx_key_print(BIO *bp, const EVP_PKEY *pkey, int indent,
269 ASN1_PCTX *ctx, ecx_key_op_t op)
271 const ECX_KEY *ecxkey = pkey->pkey.ecx;
272 const char *nm = OBJ_nid2ln(pkey->ameth->pkey_id);
274 if (op == KEY_OP_PRIVATE) {
275 if (ecxkey == NULL || ecxkey->privkey == NULL) {
276 if (BIO_printf(bp, "%*s<INVALID PRIVATE KEY>\n", indent, "") <= 0)
280 if (BIO_printf(bp, "%*s%s Private-Key:\n", indent, "", nm) <= 0)
282 if (BIO_printf(bp, "%*spriv:\n", indent, "") <= 0)
284 if (ASN1_buf_print(bp, ecxkey->privkey, KEYLEN(pkey),
288 if (ecxkey == NULL) {
289 if (BIO_printf(bp, "%*s<INVALID PUBLIC KEY>\n", indent, "") <= 0)
293 if (BIO_printf(bp, "%*s%s Public-Key:\n", indent, "", nm) <= 0)
296 if (BIO_printf(bp, "%*spub:\n", indent, "") <= 0)
299 if (ASN1_buf_print(bp, ecxkey->pubkey, KEYLEN(pkey),
305 static int ecx_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
308 return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PRIVATE);
311 static int ecx_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
314 return ecx_key_print(bp, pkey, indent, ctx, KEY_OP_PUBLIC);
317 static int ecx_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
321 case ASN1_PKEY_CTRL_SET1_TLS_ENCPT:
322 return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, arg2, arg1,
325 case ASN1_PKEY_CTRL_GET1_TLS_ENCPT:
326 if (pkey->pkey.ecx != NULL) {
327 unsigned char **ppt = arg2;
329 *ppt = OPENSSL_memdup(pkey->pkey.ecx->pubkey, KEYLEN(pkey));
341 static int ecd_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
344 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
345 /* We currently only support Pure EdDSA which takes no digest */
346 *(int *)arg2 = NID_undef;
355 static int ecx_set_priv_key(EVP_PKEY *pkey, const unsigned char *priv,
358 return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, priv, len,
362 static int ecx_set_pub_key(EVP_PKEY *pkey, const unsigned char *pub, size_t len)
364 return ecx_key_op(pkey, pkey->ameth->pkey_id, NULL, pub, len,
368 static int ecx_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
371 const ECX_KEY *key = pkey->pkey.ecx;
374 *len = KEYLENID(pkey->ameth->pkey_id);
379 || key->privkey == NULL
380 || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
383 *len = KEYLENID(pkey->ameth->pkey_id);
384 memcpy(priv, key->privkey, *len);
389 static int ecx_get_pub_key(const EVP_PKEY *pkey, unsigned char *pub,
392 const ECX_KEY *key = pkey->pkey.ecx;
395 *len = KEYLENID(pkey->ameth->pkey_id);
400 || *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
403 *len = KEYLENID(pkey->ameth->pkey_id);
404 memcpy(pub, key->pubkey, *len);
409 const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
414 "OpenSSL X25519 algorithm",
452 const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
457 "OpenSSL X448 algorithm",
495 static int ecd_size25519(const EVP_PKEY *pkey)
497 return ED25519_SIGSIZE;
500 static int ecd_size448(const EVP_PKEY *pkey)
502 return ED448_SIGSIZE;
505 static int ecd_item_verify(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
506 X509_ALGOR *sigalg, ASN1_BIT_STRING *str,
509 const ASN1_OBJECT *obj;
513 /* Sanity check: make sure it is ED25519/ED448 with absent parameters */
514 X509_ALGOR_get0(&obj, &ptype, NULL, sigalg);
515 nid = OBJ_obj2nid(obj);
516 if ((nid != NID_ED25519 && nid != NID_ED448) || ptype != V_ASN1_UNDEF) {
517 ECerr(EC_F_ECD_ITEM_VERIFY, EC_R_INVALID_ENCODING);
521 if (!EVP_DigestVerifyInit(ctx, NULL, NULL, NULL, pkey))
527 static int ecd_item_sign25519(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
528 X509_ALGOR *alg1, X509_ALGOR *alg2,
529 ASN1_BIT_STRING *str)
531 /* Set algorithms identifiers */
532 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
534 X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED25519), V_ASN1_UNDEF, NULL);
535 /* Algorithm idetifiers set: carry on as normal */
539 static int ecd_sig_info_set25519(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
540 const ASN1_STRING *sig)
542 X509_SIG_INFO_set(siginf, NID_undef, NID_ED25519, X25519_SECURITY_BITS,
547 static int ecd_item_sign448(EVP_MD_CTX *ctx, const ASN1_ITEM *it, void *asn,
548 X509_ALGOR *alg1, X509_ALGOR *alg2,
549 ASN1_BIT_STRING *str)
551 /* Set algorithm identifier */
552 X509_ALGOR_set0(alg1, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
554 X509_ALGOR_set0(alg2, OBJ_nid2obj(NID_ED448), V_ASN1_UNDEF, NULL);
555 /* Algorithm identifier set: carry on as normal */
559 static int ecd_sig_info_set448(X509_SIG_INFO *siginf, const X509_ALGOR *alg,
560 const ASN1_STRING *sig)
562 X509_SIG_INFO_set(siginf, NID_undef, NID_ED448, X448_SECURITY_BITS,
568 const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
573 "OpenSSL ED25519 algorithm",
598 ecd_sig_info_set25519,
610 const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
615 "OpenSSL ED448 algorithm",
652 static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
654 return ecx_key_op(pkey, ctx->pmeth->pkey_id, NULL, NULL, 0, KEY_OP_KEYGEN);
657 static int validate_ecx_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
659 const unsigned char **privkey,
660 const unsigned char **pubkey)
662 const ECX_KEY *ecxkey, *peerkey;
664 if (ctx->pkey == NULL || ctx->peerkey == NULL) {
665 ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_KEYS_NOT_SET);
668 ecxkey = ctx->pkey->pkey.ecx;
669 peerkey = ctx->peerkey->pkey.ecx;
670 if (ecxkey == NULL || ecxkey->privkey == NULL) {
671 ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PRIVATE_KEY);
674 if (peerkey == NULL) {
675 ECerr(EC_F_VALIDATE_ECX_DERIVE, EC_R_INVALID_PEER_KEY);
678 *privkey = ecxkey->privkey;
679 *pubkey = peerkey->pubkey;
684 static int pkey_ecx_derive25519(EVP_PKEY_CTX *ctx, unsigned char *key,
687 const unsigned char *privkey, *pubkey;
689 if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
691 && X25519(key, privkey, pubkey) == 0))
693 *keylen = X25519_KEYLEN;
697 static int pkey_ecx_derive448(EVP_PKEY_CTX *ctx, unsigned char *key,
700 const unsigned char *privkey, *pubkey;
702 if (!validate_ecx_derive(ctx, key, keylen, &privkey, &pubkey)
704 && X448(key, privkey, pubkey) == 0))
706 *keylen = X448_KEYLEN;
710 static int pkey_ecx_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
712 /* Only need to handle peer key for derivation */
713 if (type == EVP_PKEY_CTRL_PEER_KEY)
718 const EVP_PKEY_METHOD ecx25519_pkey_meth = {
722 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
723 pkey_ecx_derive25519,
728 const EVP_PKEY_METHOD ecx448_pkey_meth = {
732 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
738 static int pkey_ecd_digestsign25519(EVP_MD_CTX *ctx, unsigned char *sig,
739 size_t *siglen, const unsigned char *tbs,
742 const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
745 *siglen = ED25519_SIGSIZE;
748 if (*siglen < ED25519_SIGSIZE) {
749 ECerr(EC_F_PKEY_ECD_DIGESTSIGN25519, EC_R_BUFFER_TOO_SMALL);
753 if (ED25519_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey) == 0)
755 *siglen = ED25519_SIGSIZE;
759 static int pkey_ecd_digestsign448(EVP_MD_CTX *ctx, unsigned char *sig,
760 size_t *siglen, const unsigned char *tbs,
763 const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
766 *siglen = ED448_SIGSIZE;
769 if (*siglen < ED448_SIGSIZE) {
770 ECerr(EC_F_PKEY_ECD_DIGESTSIGN448, EC_R_BUFFER_TOO_SMALL);
774 if (ED448_sign(sig, tbs, tbslen, edkey->pubkey, edkey->privkey, NULL,
777 *siglen = ED448_SIGSIZE;
781 static int pkey_ecd_digestverify25519(EVP_MD_CTX *ctx, const unsigned char *sig,
782 size_t siglen, const unsigned char *tbs,
785 const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
787 if (siglen != ED25519_SIGSIZE)
790 return ED25519_verify(tbs, tbslen, sig, edkey->pubkey);
793 static int pkey_ecd_digestverify448(EVP_MD_CTX *ctx, const unsigned char *sig,
794 size_t siglen, const unsigned char *tbs,
797 const ECX_KEY *edkey = EVP_MD_CTX_pkey_ctx(ctx)->pkey->pkey.ecx;
799 if (siglen != ED448_SIGSIZE)
802 return ED448_verify(tbs, tbslen, sig, edkey->pubkey, NULL, 0);
805 static int pkey_ecd_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
808 case EVP_PKEY_CTRL_MD:
809 /* Only NULL allowed as digest */
810 if (p2 == NULL || (const EVP_MD *)p2 == EVP_md_null())
812 ECerr(EC_F_PKEY_ECD_CTRL, EC_R_INVALID_DIGEST_TYPE);
815 case EVP_PKEY_CTRL_DIGESTINIT:
821 const EVP_PKEY_METHOD ed25519_pkey_meth = {
822 EVP_PKEY_ED25519, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
825 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
828 pkey_ecd_digestsign25519,
829 pkey_ecd_digestverify25519
832 const EVP_PKEY_METHOD ed448_pkey_meth = {
833 EVP_PKEY_ED448, EVP_PKEY_FLAG_SIGCTX_CUSTOM,
836 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
839 pkey_ecd_digestsign448,
840 pkey_ecd_digestverify448