xref: /freebsd/contrib/libfido2/src/es256.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
10afa8e06SEd Maste /*
2*2ccfa855SEd Maste  * Copyright (c) 2018-2022 Yubico AB. All rights reserved.
30afa8e06SEd Maste  * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste  */
70afa8e06SEd Maste 
80afa8e06SEd Maste #include <openssl/bn.h>
9f540a430SEd Maste #include <openssl/ecdsa.h>
100afa8e06SEd Maste #include <openssl/obj_mac.h>
110afa8e06SEd Maste 
120afa8e06SEd Maste #include "fido.h"
130afa8e06SEd Maste #include "fido/es256.h"
140afa8e06SEd Maste 
15*2ccfa855SEd Maste #if OPENSSL_VERSION_NUMBER >= 0x30000000
16*2ccfa855SEd Maste #define get0_EC_KEY(x)	EVP_PKEY_get0_EC_KEY((x))
17*2ccfa855SEd Maste #else
18*2ccfa855SEd Maste #define get0_EC_KEY(x)	EVP_PKEY_get0((x))
19*2ccfa855SEd Maste #endif
20*2ccfa855SEd Maste 
21*2ccfa855SEd Maste static const int es256_nid = NID_X9_62_prime256v1;
22*2ccfa855SEd Maste 
230afa8e06SEd Maste static int
decode_coord(const cbor_item_t * item,void * xy,size_t xy_len)240afa8e06SEd Maste decode_coord(const cbor_item_t *item, void *xy, size_t xy_len)
250afa8e06SEd Maste {
260afa8e06SEd Maste 	if (cbor_isa_bytestring(item) == false ||
270afa8e06SEd Maste 	    cbor_bytestring_is_definite(item) == false ||
280afa8e06SEd Maste 	    cbor_bytestring_length(item) != xy_len) {
290afa8e06SEd Maste 		fido_log_debug("%s: cbor type", __func__);
300afa8e06SEd Maste 		return (-1);
310afa8e06SEd Maste 	}
320afa8e06SEd Maste 
330afa8e06SEd Maste 	memcpy(xy, cbor_bytestring_handle(item), xy_len);
340afa8e06SEd Maste 
350afa8e06SEd Maste 	return (0);
360afa8e06SEd Maste }
370afa8e06SEd Maste 
380afa8e06SEd Maste static int
decode_pubkey_point(const cbor_item_t * key,const cbor_item_t * val,void * arg)390afa8e06SEd Maste decode_pubkey_point(const cbor_item_t *key, const cbor_item_t *val, void *arg)
400afa8e06SEd Maste {
410afa8e06SEd Maste 	es256_pk_t *k = arg;
420afa8e06SEd Maste 
430afa8e06SEd Maste 	if (cbor_isa_negint(key) == false ||
440afa8e06SEd Maste 	    cbor_int_get_width(key) != CBOR_INT_8)
450afa8e06SEd Maste 		return (0); /* ignore */
460afa8e06SEd Maste 
470afa8e06SEd Maste 	switch (cbor_get_uint8(key)) {
480afa8e06SEd Maste 	case 1: /* x coordinate */
490afa8e06SEd Maste 		return (decode_coord(val, &k->x, sizeof(k->x)));
500afa8e06SEd Maste 	case 2: /* y coordinate */
510afa8e06SEd Maste 		return (decode_coord(val, &k->y, sizeof(k->y)));
520afa8e06SEd Maste 	}
530afa8e06SEd Maste 
540afa8e06SEd Maste 	return (0); /* ignore */
550afa8e06SEd Maste }
560afa8e06SEd Maste 
570afa8e06SEd Maste int
es256_pk_decode(const cbor_item_t * item,es256_pk_t * k)580afa8e06SEd Maste es256_pk_decode(const cbor_item_t *item, es256_pk_t *k)
590afa8e06SEd Maste {
600afa8e06SEd Maste 	if (cbor_isa_map(item) == false ||
610afa8e06SEd Maste 	    cbor_map_is_definite(item) == false ||
620afa8e06SEd Maste 	    cbor_map_iter(item, k, decode_pubkey_point) < 0) {
630afa8e06SEd Maste 		fido_log_debug("%s: cbor type", __func__);
640afa8e06SEd Maste 		return (-1);
650afa8e06SEd Maste 	}
660afa8e06SEd Maste 
670afa8e06SEd Maste 	return (0);
680afa8e06SEd Maste }
690afa8e06SEd Maste 
700afa8e06SEd Maste cbor_item_t *
es256_pk_encode(const es256_pk_t * pk,int ecdh)710afa8e06SEd Maste es256_pk_encode(const es256_pk_t *pk, int ecdh)
720afa8e06SEd Maste {
730afa8e06SEd Maste 	cbor_item_t		*item = NULL;
740afa8e06SEd Maste 	struct cbor_pair	 argv[5];
750afa8e06SEd Maste 	int			 alg;
760afa8e06SEd Maste 	int			 ok = -1;
770afa8e06SEd Maste 
780afa8e06SEd Maste 	memset(argv, 0, sizeof(argv));
790afa8e06SEd Maste 
800afa8e06SEd Maste 	if ((item = cbor_new_definite_map(5)) == NULL)
810afa8e06SEd Maste 		goto fail;
820afa8e06SEd Maste 
830afa8e06SEd Maste 	/* kty */
840afa8e06SEd Maste 	if ((argv[0].key = cbor_build_uint8(1)) == NULL ||
850afa8e06SEd Maste 	    (argv[0].value = cbor_build_uint8(2)) == NULL ||
860afa8e06SEd Maste 	    !cbor_map_add(item, argv[0]))
870afa8e06SEd Maste 		goto fail;
880afa8e06SEd Maste 
890afa8e06SEd Maste 	/*
900afa8e06SEd Maste 	 * "The COSEAlgorithmIdentifier used is -25 (ECDH-ES +
910afa8e06SEd Maste 	 * HKDF-256) although this is NOT the algorithm actually
920afa8e06SEd Maste 	 * used. Setting this to a different value may result in
930afa8e06SEd Maste 	 * compatibility issues."
940afa8e06SEd Maste 	 */
950afa8e06SEd Maste 	if (ecdh)
960afa8e06SEd Maste 		alg = COSE_ECDH_ES256;
970afa8e06SEd Maste 	else
980afa8e06SEd Maste 		alg = COSE_ES256;
990afa8e06SEd Maste 
1000afa8e06SEd Maste 	/* alg */
1010afa8e06SEd Maste 	if ((argv[1].key = cbor_build_uint8(3)) == NULL ||
1020afa8e06SEd Maste 	    (argv[1].value = cbor_build_negint8((uint8_t)(-alg - 1))) == NULL ||
1030afa8e06SEd Maste 	    !cbor_map_add(item, argv[1]))
1040afa8e06SEd Maste 		goto fail;
1050afa8e06SEd Maste 
1060afa8e06SEd Maste 	/* crv */
1070afa8e06SEd Maste 	if ((argv[2].key = cbor_build_negint8(0)) == NULL ||
1080afa8e06SEd Maste 	    (argv[2].value = cbor_build_uint8(1)) == NULL ||
1090afa8e06SEd Maste 	    !cbor_map_add(item, argv[2]))
1100afa8e06SEd Maste 		goto fail;
1110afa8e06SEd Maste 
1120afa8e06SEd Maste 	/* x */
1130afa8e06SEd Maste 	if ((argv[3].key = cbor_build_negint8(1)) == NULL ||
1140afa8e06SEd Maste 	    (argv[3].value = cbor_build_bytestring(pk->x,
1150afa8e06SEd Maste 	    sizeof(pk->x))) == NULL || !cbor_map_add(item, argv[3]))
1160afa8e06SEd Maste 		goto fail;
1170afa8e06SEd Maste 
1180afa8e06SEd Maste 	/* y */
1190afa8e06SEd Maste 	if ((argv[4].key = cbor_build_negint8(2)) == NULL ||
1200afa8e06SEd Maste 	    (argv[4].value = cbor_build_bytestring(pk->y,
1210afa8e06SEd Maste 	    sizeof(pk->y))) == NULL || !cbor_map_add(item, argv[4]))
1220afa8e06SEd Maste 		goto fail;
1230afa8e06SEd Maste 
1240afa8e06SEd Maste 	ok = 0;
1250afa8e06SEd Maste fail:
1260afa8e06SEd Maste 	if (ok < 0) {
1270afa8e06SEd Maste 		if (item != NULL) {
1280afa8e06SEd Maste 			cbor_decref(&item);
1290afa8e06SEd Maste 			item = NULL;
1300afa8e06SEd Maste 		}
1310afa8e06SEd Maste 	}
1320afa8e06SEd Maste 
1330afa8e06SEd Maste 	for (size_t i = 0; i < 5; i++) {
1340afa8e06SEd Maste 		if (argv[i].key)
1350afa8e06SEd Maste 			cbor_decref(&argv[i].key);
1360afa8e06SEd Maste 		if (argv[i].value)
1370afa8e06SEd Maste 			cbor_decref(&argv[i].value);
1380afa8e06SEd Maste 	}
1390afa8e06SEd Maste 
1400afa8e06SEd Maste 	return (item);
1410afa8e06SEd Maste }
1420afa8e06SEd Maste 
1430afa8e06SEd Maste es256_sk_t *
es256_sk_new(void)1440afa8e06SEd Maste es256_sk_new(void)
1450afa8e06SEd Maste {
1460afa8e06SEd Maste 	return (calloc(1, sizeof(es256_sk_t)));
1470afa8e06SEd Maste }
1480afa8e06SEd Maste 
1490afa8e06SEd Maste void
es256_sk_free(es256_sk_t ** skp)1500afa8e06SEd Maste es256_sk_free(es256_sk_t **skp)
1510afa8e06SEd Maste {
1520afa8e06SEd Maste 	es256_sk_t *sk;
1530afa8e06SEd Maste 
1540afa8e06SEd Maste 	if (skp == NULL || (sk = *skp) == NULL)
1550afa8e06SEd Maste 		return;
1560afa8e06SEd Maste 
1570afa8e06SEd Maste 	freezero(sk, sizeof(*sk));
1580afa8e06SEd Maste 	*skp = NULL;
1590afa8e06SEd Maste }
1600afa8e06SEd Maste 
1610afa8e06SEd Maste es256_pk_t *
es256_pk_new(void)1620afa8e06SEd Maste es256_pk_new(void)
1630afa8e06SEd Maste {
1640afa8e06SEd Maste 	return (calloc(1, sizeof(es256_pk_t)));
1650afa8e06SEd Maste }
1660afa8e06SEd Maste 
1670afa8e06SEd Maste void
es256_pk_free(es256_pk_t ** pkp)1680afa8e06SEd Maste es256_pk_free(es256_pk_t **pkp)
1690afa8e06SEd Maste {
1700afa8e06SEd Maste 	es256_pk_t *pk;
1710afa8e06SEd Maste 
1720afa8e06SEd Maste 	if (pkp == NULL || (pk = *pkp) == NULL)
1730afa8e06SEd Maste 		return;
1740afa8e06SEd Maste 
1750afa8e06SEd Maste 	freezero(pk, sizeof(*pk));
1760afa8e06SEd Maste 	*pkp = NULL;
1770afa8e06SEd Maste }
1780afa8e06SEd Maste 
1790afa8e06SEd Maste int
es256_pk_from_ptr(es256_pk_t * pk,const void * ptr,size_t len)1800afa8e06SEd Maste es256_pk_from_ptr(es256_pk_t *pk, const void *ptr, size_t len)
1810afa8e06SEd Maste {
1820afa8e06SEd Maste 	const uint8_t	*p = ptr;
183*2ccfa855SEd Maste 	EVP_PKEY	*pkey;
1840afa8e06SEd Maste 
1850afa8e06SEd Maste 	if (len < sizeof(*pk))
1860afa8e06SEd Maste 		return (FIDO_ERR_INVALID_ARGUMENT);
1870afa8e06SEd Maste 
1880afa8e06SEd Maste 	if (len == sizeof(*pk) + 1 && *p == 0x04)
1890afa8e06SEd Maste 		memcpy(pk, ++p, sizeof(*pk)); /* uncompressed format */
1900afa8e06SEd Maste 	else
1910afa8e06SEd Maste 		memcpy(pk, ptr, sizeof(*pk)); /* libfido2 x||y format */
1920afa8e06SEd Maste 
193*2ccfa855SEd Maste 	if ((pkey = es256_pk_to_EVP_PKEY(pk)) == NULL) {
194*2ccfa855SEd Maste 		fido_log_debug("%s: es256_pk_to_EVP_PKEY", __func__);
195*2ccfa855SEd Maste 		explicit_bzero(pk, sizeof(*pk));
196*2ccfa855SEd Maste 		return (FIDO_ERR_INVALID_ARGUMENT);
197*2ccfa855SEd Maste 	}
198*2ccfa855SEd Maste 
199*2ccfa855SEd Maste 	EVP_PKEY_free(pkey);
200*2ccfa855SEd Maste 
2010afa8e06SEd Maste 	return (FIDO_OK);
2020afa8e06SEd Maste }
2030afa8e06SEd Maste 
2040afa8e06SEd Maste int
es256_pk_set_x(es256_pk_t * pk,const unsigned char * x)2050afa8e06SEd Maste es256_pk_set_x(es256_pk_t *pk, const unsigned char *x)
2060afa8e06SEd Maste {
2070afa8e06SEd Maste 	memcpy(pk->x, x, sizeof(pk->x));
2080afa8e06SEd Maste 
2090afa8e06SEd Maste 	return (0);
2100afa8e06SEd Maste }
2110afa8e06SEd Maste 
2120afa8e06SEd Maste int
es256_pk_set_y(es256_pk_t * pk,const unsigned char * y)2130afa8e06SEd Maste es256_pk_set_y(es256_pk_t *pk, const unsigned char *y)
2140afa8e06SEd Maste {
2150afa8e06SEd Maste 	memcpy(pk->y, y, sizeof(pk->y));
2160afa8e06SEd Maste 
2170afa8e06SEd Maste 	return (0);
2180afa8e06SEd Maste }
2190afa8e06SEd Maste 
2200afa8e06SEd Maste int
es256_sk_create(es256_sk_t * key)2210afa8e06SEd Maste es256_sk_create(es256_sk_t *key)
2220afa8e06SEd Maste {
2230afa8e06SEd Maste 	EVP_PKEY_CTX	*pctx = NULL;
2240afa8e06SEd Maste 	EVP_PKEY_CTX	*kctx = NULL;
2250afa8e06SEd Maste 	EVP_PKEY	*p = NULL;
2260afa8e06SEd Maste 	EVP_PKEY	*k = NULL;
2270afa8e06SEd Maste 	const EC_KEY	*ec;
2280afa8e06SEd Maste 	const BIGNUM	*d;
2290afa8e06SEd Maste 	int		 n;
2300afa8e06SEd Maste 	int		 ok = -1;
2310afa8e06SEd Maste 
2320afa8e06SEd Maste 	if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
2330afa8e06SEd Maste 	    EVP_PKEY_paramgen_init(pctx) <= 0 ||
234*2ccfa855SEd Maste 	    EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, es256_nid) <= 0 ||
2350afa8e06SEd Maste 	    EVP_PKEY_paramgen(pctx, &p) <= 0) {
2360afa8e06SEd Maste 		fido_log_debug("%s: EVP_PKEY_paramgen", __func__);
2370afa8e06SEd Maste 		goto fail;
2380afa8e06SEd Maste 	}
2390afa8e06SEd Maste 
2400afa8e06SEd Maste 	if ((kctx = EVP_PKEY_CTX_new(p, NULL)) == NULL ||
2410afa8e06SEd Maste 	    EVP_PKEY_keygen_init(kctx) <= 0 || EVP_PKEY_keygen(kctx, &k) <= 0) {
2420afa8e06SEd Maste 		fido_log_debug("%s: EVP_PKEY_keygen", __func__);
2430afa8e06SEd Maste 		goto fail;
2440afa8e06SEd Maste 	}
2450afa8e06SEd Maste 
2460afa8e06SEd Maste 	if ((ec = EVP_PKEY_get0_EC_KEY(k)) == NULL ||
2470afa8e06SEd Maste 	    (d = EC_KEY_get0_private_key(ec)) == NULL ||
2480afa8e06SEd Maste 	    (n = BN_num_bytes(d)) < 0 || (size_t)n > sizeof(key->d) ||
2490afa8e06SEd Maste 	    (n = BN_bn2bin(d, key->d)) < 0 || (size_t)n > sizeof(key->d)) {
2500afa8e06SEd Maste 		fido_log_debug("%s: EC_KEY_get0_private_key", __func__);
2510afa8e06SEd Maste 		goto fail;
2520afa8e06SEd Maste 	}
2530afa8e06SEd Maste 
2540afa8e06SEd Maste 	ok = 0;
2550afa8e06SEd Maste fail:
2560afa8e06SEd Maste 	if (p != NULL)
2570afa8e06SEd Maste 		EVP_PKEY_free(p);
2580afa8e06SEd Maste 	if (k != NULL)
2590afa8e06SEd Maste 		EVP_PKEY_free(k);
2600afa8e06SEd Maste 	if (pctx != NULL)
2610afa8e06SEd Maste 		EVP_PKEY_CTX_free(pctx);
2620afa8e06SEd Maste 	if (kctx != NULL)
2630afa8e06SEd Maste 		EVP_PKEY_CTX_free(kctx);
2640afa8e06SEd Maste 
2650afa8e06SEd Maste 	return (ok);
2660afa8e06SEd Maste }
2670afa8e06SEd Maste 
2680afa8e06SEd Maste EVP_PKEY *
es256_pk_to_EVP_PKEY(const es256_pk_t * k)2690afa8e06SEd Maste es256_pk_to_EVP_PKEY(const es256_pk_t *k)
2700afa8e06SEd Maste {
2710afa8e06SEd Maste 	BN_CTX		*bnctx = NULL;
2720afa8e06SEd Maste 	EC_KEY		*ec = NULL;
2730afa8e06SEd Maste 	EC_POINT	*q = NULL;
2740afa8e06SEd Maste 	EVP_PKEY	*pkey = NULL;
2750afa8e06SEd Maste 	BIGNUM		*x = NULL;
2760afa8e06SEd Maste 	BIGNUM		*y = NULL;
2770afa8e06SEd Maste 	const EC_GROUP	*g = NULL;
2780afa8e06SEd Maste 	int		 ok = -1;
2790afa8e06SEd Maste 
2800afa8e06SEd Maste 	if ((bnctx = BN_CTX_new()) == NULL)
2810afa8e06SEd Maste 		goto fail;
2820afa8e06SEd Maste 
2830afa8e06SEd Maste 	BN_CTX_start(bnctx);
2840afa8e06SEd Maste 
2850afa8e06SEd Maste 	if ((x = BN_CTX_get(bnctx)) == NULL ||
2860afa8e06SEd Maste 	    (y = BN_CTX_get(bnctx)) == NULL)
2870afa8e06SEd Maste 		goto fail;
2880afa8e06SEd Maste 
2890afa8e06SEd Maste 	if (BN_bin2bn(k->x, sizeof(k->x), x) == NULL ||
2900afa8e06SEd Maste 	    BN_bin2bn(k->y, sizeof(k->y), y) == NULL) {
2910afa8e06SEd Maste 		fido_log_debug("%s: BN_bin2bn", __func__);
2920afa8e06SEd Maste 		goto fail;
2930afa8e06SEd Maste 	}
2940afa8e06SEd Maste 
295*2ccfa855SEd Maste 	if ((ec = EC_KEY_new_by_curve_name(es256_nid)) == NULL ||
2960afa8e06SEd Maste 	    (g = EC_KEY_get0_group(ec)) == NULL) {
2970afa8e06SEd Maste 		fido_log_debug("%s: EC_KEY init", __func__);
2980afa8e06SEd Maste 		goto fail;
2990afa8e06SEd Maste 	}
3000afa8e06SEd Maste 
3010afa8e06SEd Maste 	if ((q = EC_POINT_new(g)) == NULL ||
3020afa8e06SEd Maste 	    EC_POINT_set_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
3030afa8e06SEd Maste 	    EC_KEY_set_public_key(ec, q) == 0) {
3040afa8e06SEd Maste 		fido_log_debug("%s: EC_KEY_set_public_key", __func__);
3050afa8e06SEd Maste 		goto fail;
3060afa8e06SEd Maste 	}
3070afa8e06SEd Maste 
3080afa8e06SEd Maste 	if ((pkey = EVP_PKEY_new()) == NULL ||
3090afa8e06SEd Maste 	    EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
3100afa8e06SEd Maste 		fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
3110afa8e06SEd Maste 		goto fail;
3120afa8e06SEd Maste 	}
3130afa8e06SEd Maste 
3140afa8e06SEd Maste 	ec = NULL; /* at this point, ec belongs to evp */
3150afa8e06SEd Maste 
3160afa8e06SEd Maste 	ok = 0;
3170afa8e06SEd Maste fail:
3180afa8e06SEd Maste 	if (bnctx != NULL) {
3190afa8e06SEd Maste 		BN_CTX_end(bnctx);
3200afa8e06SEd Maste 		BN_CTX_free(bnctx);
3210afa8e06SEd Maste 	}
3220afa8e06SEd Maste 
3230afa8e06SEd Maste 	if (ec != NULL)
3240afa8e06SEd Maste 		EC_KEY_free(ec);
3250afa8e06SEd Maste 	if (q != NULL)
3260afa8e06SEd Maste 		EC_POINT_free(q);
3270afa8e06SEd Maste 
3280afa8e06SEd Maste 	if (ok < 0 && pkey != NULL) {
3290afa8e06SEd Maste 		EVP_PKEY_free(pkey);
3300afa8e06SEd Maste 		pkey = NULL;
3310afa8e06SEd Maste 	}
3320afa8e06SEd Maste 
3330afa8e06SEd Maste 	return (pkey);
3340afa8e06SEd Maste }
3350afa8e06SEd Maste 
3360afa8e06SEd Maste int
es256_pk_from_EC_KEY(es256_pk_t * pk,const EC_KEY * ec)3370afa8e06SEd Maste es256_pk_from_EC_KEY(es256_pk_t *pk, const EC_KEY *ec)
3380afa8e06SEd Maste {
3390afa8e06SEd Maste 	BN_CTX		*bnctx = NULL;
3400afa8e06SEd Maste 	BIGNUM		*x = NULL;
3410afa8e06SEd Maste 	BIGNUM		*y = NULL;
3420afa8e06SEd Maste 	const EC_POINT	*q = NULL;
343*2ccfa855SEd Maste 	EC_GROUP	*g = NULL;
344*2ccfa855SEd Maste 	size_t		 dx;
345*2ccfa855SEd Maste 	size_t		 dy;
3460afa8e06SEd Maste 	int		 ok = FIDO_ERR_INTERNAL;
347*2ccfa855SEd Maste 	int		 nx;
348*2ccfa855SEd Maste 	int		 ny;
3490afa8e06SEd Maste 
3500afa8e06SEd Maste 	if ((q = EC_KEY_get0_public_key(ec)) == NULL ||
351*2ccfa855SEd Maste 	    (g = EC_GROUP_new_by_curve_name(es256_nid)) == NULL ||
3520afa8e06SEd Maste 	    (bnctx = BN_CTX_new()) == NULL)
3530afa8e06SEd Maste 		goto fail;
3540afa8e06SEd Maste 
3550afa8e06SEd Maste 	BN_CTX_start(bnctx);
3560afa8e06SEd Maste 
3570afa8e06SEd Maste 	if ((x = BN_CTX_get(bnctx)) == NULL ||
3580afa8e06SEd Maste 	    (y = BN_CTX_get(bnctx)) == NULL)
3590afa8e06SEd Maste 		goto fail;
3600afa8e06SEd Maste 
361*2ccfa855SEd Maste 	if (EC_POINT_is_on_curve(g, q, bnctx) != 1) {
362*2ccfa855SEd Maste 		fido_log_debug("%s: EC_POINT_is_on_curve", __func__);
363*2ccfa855SEd Maste 		ok = FIDO_ERR_INVALID_ARGUMENT;
364*2ccfa855SEd Maste 		goto fail;
365*2ccfa855SEd Maste 	}
366*2ccfa855SEd Maste 
3670afa8e06SEd Maste 	if (EC_POINT_get_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
368*2ccfa855SEd Maste 	    (nx = BN_num_bytes(x)) < 0 || (size_t)nx > sizeof(pk->x) ||
369*2ccfa855SEd Maste 	    (ny = BN_num_bytes(y)) < 0 || (size_t)ny > sizeof(pk->y)) {
3700afa8e06SEd Maste 		fido_log_debug("%s: EC_POINT_get_affine_coordinates_GFp",
3710afa8e06SEd Maste 		    __func__);
3720afa8e06SEd Maste 		goto fail;
3730afa8e06SEd Maste 	}
3740afa8e06SEd Maste 
375*2ccfa855SEd Maste 	dx = sizeof(pk->x) - (size_t)nx;
376*2ccfa855SEd Maste 	dy = sizeof(pk->y) - (size_t)ny;
377*2ccfa855SEd Maste 
378*2ccfa855SEd Maste 	if ((nx = BN_bn2bin(x, pk->x + dx)) < 0 || (size_t)nx > sizeof(pk->x) ||
379*2ccfa855SEd Maste 	    (ny = BN_bn2bin(y, pk->y + dy)) < 0 || (size_t)ny > sizeof(pk->y)) {
3800afa8e06SEd Maste 		fido_log_debug("%s: BN_bn2bin", __func__);
3810afa8e06SEd Maste 		goto fail;
3820afa8e06SEd Maste 	}
3830afa8e06SEd Maste 
3840afa8e06SEd Maste 	ok = FIDO_OK;
3850afa8e06SEd Maste fail:
386*2ccfa855SEd Maste 	EC_GROUP_free(g);
387*2ccfa855SEd Maste 
3880afa8e06SEd Maste 	if (bnctx != NULL) {
3890afa8e06SEd Maste 		BN_CTX_end(bnctx);
3900afa8e06SEd Maste 		BN_CTX_free(bnctx);
3910afa8e06SEd Maste 	}
3920afa8e06SEd Maste 
3930afa8e06SEd Maste 	return (ok);
3940afa8e06SEd Maste }
3950afa8e06SEd Maste 
396f540a430SEd Maste int
es256_pk_from_EVP_PKEY(es256_pk_t * pk,const EVP_PKEY * pkey)397f540a430SEd Maste es256_pk_from_EVP_PKEY(es256_pk_t *pk, const EVP_PKEY *pkey)
398f540a430SEd Maste {
399*2ccfa855SEd Maste 	const EC_KEY *ec;
400f540a430SEd Maste 
401f540a430SEd Maste 	if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC ||
402*2ccfa855SEd Maste 	    (ec = get0_EC_KEY(pkey)) == NULL)
403f540a430SEd Maste 		return (FIDO_ERR_INVALID_ARGUMENT);
404f540a430SEd Maste 
405f540a430SEd Maste 	return (es256_pk_from_EC_KEY(pk, ec));
406f540a430SEd Maste }
407f540a430SEd Maste 
4080afa8e06SEd Maste EVP_PKEY *
es256_sk_to_EVP_PKEY(const es256_sk_t * k)4090afa8e06SEd Maste es256_sk_to_EVP_PKEY(const es256_sk_t *k)
4100afa8e06SEd Maste {
4110afa8e06SEd Maste 	BN_CTX		*bnctx = NULL;
4120afa8e06SEd Maste 	EC_KEY		*ec = NULL;
4130afa8e06SEd Maste 	EVP_PKEY	*pkey = NULL;
4140afa8e06SEd Maste 	BIGNUM		*d = NULL;
4150afa8e06SEd Maste 	int		 ok = -1;
4160afa8e06SEd Maste 
4170afa8e06SEd Maste 	if ((bnctx = BN_CTX_new()) == NULL)
4180afa8e06SEd Maste 		goto fail;
4190afa8e06SEd Maste 
4200afa8e06SEd Maste 	BN_CTX_start(bnctx);
4210afa8e06SEd Maste 
4220afa8e06SEd Maste 	if ((d = BN_CTX_get(bnctx)) == NULL ||
4230afa8e06SEd Maste 	    BN_bin2bn(k->d, sizeof(k->d), d) == NULL) {
4240afa8e06SEd Maste 		fido_log_debug("%s: BN_bin2bn", __func__);
4250afa8e06SEd Maste 		goto fail;
4260afa8e06SEd Maste 	}
4270afa8e06SEd Maste 
428*2ccfa855SEd Maste 	if ((ec = EC_KEY_new_by_curve_name(es256_nid)) == NULL ||
4290afa8e06SEd Maste 	    EC_KEY_set_private_key(ec, d) == 0) {
4300afa8e06SEd Maste 		fido_log_debug("%s: EC_KEY_set_private_key", __func__);
4310afa8e06SEd Maste 		goto fail;
4320afa8e06SEd Maste 	}
4330afa8e06SEd Maste 
4340afa8e06SEd Maste 	if ((pkey = EVP_PKEY_new()) == NULL ||
4350afa8e06SEd Maste 	    EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
4360afa8e06SEd Maste 		fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
4370afa8e06SEd Maste 		goto fail;
4380afa8e06SEd Maste 	}
4390afa8e06SEd Maste 
4400afa8e06SEd Maste 	ec = NULL; /* at this point, ec belongs to evp */
4410afa8e06SEd Maste 
4420afa8e06SEd Maste 	ok = 0;
4430afa8e06SEd Maste fail:
4440afa8e06SEd Maste 	if (bnctx != NULL) {
4450afa8e06SEd Maste 		BN_CTX_end(bnctx);
4460afa8e06SEd Maste 		BN_CTX_free(bnctx);
4470afa8e06SEd Maste 	}
4480afa8e06SEd Maste 
4490afa8e06SEd Maste 	if (ec != NULL)
4500afa8e06SEd Maste 		EC_KEY_free(ec);
4510afa8e06SEd Maste 
4520afa8e06SEd Maste 	if (ok < 0 && pkey != NULL) {
4530afa8e06SEd Maste 		EVP_PKEY_free(pkey);
4540afa8e06SEd Maste 		pkey = NULL;
4550afa8e06SEd Maste 	}
4560afa8e06SEd Maste 
4570afa8e06SEd Maste 	return (pkey);
4580afa8e06SEd Maste }
4590afa8e06SEd Maste 
4600afa8e06SEd Maste int
es256_derive_pk(const es256_sk_t * sk,es256_pk_t * pk)4610afa8e06SEd Maste es256_derive_pk(const es256_sk_t *sk, es256_pk_t *pk)
4620afa8e06SEd Maste {
4630afa8e06SEd Maste 	BIGNUM		*d = NULL;
4640afa8e06SEd Maste 	EC_KEY		*ec = NULL;
4650afa8e06SEd Maste 	EC_POINT	*q = NULL;
4660afa8e06SEd Maste 	const EC_GROUP	*g = NULL;
4670afa8e06SEd Maste 	int		 ok = -1;
4680afa8e06SEd Maste 
4690afa8e06SEd Maste 	if ((d = BN_bin2bn(sk->d, (int)sizeof(sk->d), NULL)) == NULL ||
470*2ccfa855SEd Maste 	    (ec = EC_KEY_new_by_curve_name(es256_nid)) == NULL ||
4710afa8e06SEd Maste 	    (g = EC_KEY_get0_group(ec)) == NULL ||
4720afa8e06SEd Maste 	    (q = EC_POINT_new(g)) == NULL) {
4730afa8e06SEd Maste 		fido_log_debug("%s: get", __func__);
4740afa8e06SEd Maste 		goto fail;
4750afa8e06SEd Maste 	}
4760afa8e06SEd Maste 
4770afa8e06SEd Maste 	if (EC_POINT_mul(g, q, d, NULL, NULL, NULL) == 0 ||
4780afa8e06SEd Maste 	    EC_KEY_set_public_key(ec, q) == 0 ||
4790afa8e06SEd Maste 	    es256_pk_from_EC_KEY(pk, ec) != FIDO_OK) {
4800afa8e06SEd Maste 		fido_log_debug("%s: set", __func__);
4810afa8e06SEd Maste 		goto fail;
4820afa8e06SEd Maste 	}
4830afa8e06SEd Maste 
4840afa8e06SEd Maste 	ok = 0;
4850afa8e06SEd Maste fail:
4860afa8e06SEd Maste 	if (d != NULL)
4870afa8e06SEd Maste 		BN_clear_free(d);
4880afa8e06SEd Maste 	if (q != NULL)
4890afa8e06SEd Maste 		EC_POINT_free(q);
4900afa8e06SEd Maste 	if (ec != NULL)
4910afa8e06SEd Maste 		EC_KEY_free(ec);
4920afa8e06SEd Maste 
4930afa8e06SEd Maste 	return (ok);
4940afa8e06SEd Maste }
495f540a430SEd Maste 
496f540a430SEd Maste int
es256_verify_sig(const fido_blob_t * dgst,EVP_PKEY * pkey,const fido_blob_t * sig)497f540a430SEd Maste es256_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
498f540a430SEd Maste     const fido_blob_t *sig)
499f540a430SEd Maste {
500f540a430SEd Maste 	EVP_PKEY_CTX	*pctx = NULL;
501f540a430SEd Maste 	int		 ok = -1;
502f540a430SEd Maste 
503f540a430SEd Maste 	if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
504f540a430SEd Maste 		fido_log_debug("%s: EVP_PKEY_base_id", __func__);
505f540a430SEd Maste 		goto fail;
506f540a430SEd Maste 	}
507f540a430SEd Maste 
508f540a430SEd Maste 	if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL ||
509f540a430SEd Maste 	    EVP_PKEY_verify_init(pctx) != 1 ||
510f540a430SEd Maste 	    EVP_PKEY_verify(pctx, sig->ptr, sig->len, dgst->ptr,
511f540a430SEd Maste 	    dgst->len) != 1) {
512f540a430SEd Maste 		fido_log_debug("%s: EVP_PKEY_verify", __func__);
513f540a430SEd Maste 		goto fail;
514f540a430SEd Maste 	}
515f540a430SEd Maste 
516f540a430SEd Maste 	ok = 0;
517f540a430SEd Maste fail:
518f540a430SEd Maste 	EVP_PKEY_CTX_free(pctx);
519f540a430SEd Maste 
520f540a430SEd Maste 	return (ok);
521f540a430SEd Maste }
522f540a430SEd Maste 
523f540a430SEd Maste int
es256_pk_verify_sig(const fido_blob_t * dgst,const es256_pk_t * pk,const fido_blob_t * sig)524f540a430SEd Maste es256_pk_verify_sig(const fido_blob_t *dgst, const es256_pk_t *pk,
525f540a430SEd Maste     const fido_blob_t *sig)
526f540a430SEd Maste {
527f540a430SEd Maste 	EVP_PKEY	*pkey;
528f540a430SEd Maste 	int		 ok = -1;
529f540a430SEd Maste 
530f540a430SEd Maste 	if ((pkey = es256_pk_to_EVP_PKEY(pk)) == NULL ||
531f540a430SEd Maste 	    es256_verify_sig(dgst, pkey, sig) < 0) {
532f540a430SEd Maste 		fido_log_debug("%s: es256_verify_sig", __func__);
533f540a430SEd Maste 		goto fail;
534f540a430SEd Maste 	}
535f540a430SEd Maste 
536f540a430SEd Maste 	ok = 0;
537f540a430SEd Maste fail:
538f540a430SEd Maste 	EVP_PKEY_free(pkey);
539f540a430SEd Maste 
540f540a430SEd Maste 	return (ok);
541f540a430SEd Maste }
542