1*2ccfa855SEd Maste /*
2*2ccfa855SEd Maste * Copyright (c) 2022 Yubico AB. All rights reserved.
3*2ccfa855SEd Maste * Use of this source code is governed by a BSD-style
4*2ccfa855SEd Maste * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste * SPDX-License-Identifier: BSD-2-Clause
6*2ccfa855SEd Maste */
7*2ccfa855SEd Maste
8*2ccfa855SEd Maste #include <openssl/bn.h>
9*2ccfa855SEd Maste #include <openssl/ecdsa.h>
10*2ccfa855SEd Maste #include <openssl/obj_mac.h>
11*2ccfa855SEd Maste
12*2ccfa855SEd Maste #include "fido.h"
13*2ccfa855SEd Maste #include "fido/es384.h"
14*2ccfa855SEd 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 int
decode_coord(const cbor_item_t * item,void * xy,size_t xy_len)22*2ccfa855SEd Maste decode_coord(const cbor_item_t *item, void *xy, size_t xy_len)
23*2ccfa855SEd Maste {
24*2ccfa855SEd Maste if (cbor_isa_bytestring(item) == false ||
25*2ccfa855SEd Maste cbor_bytestring_is_definite(item) == false ||
26*2ccfa855SEd Maste cbor_bytestring_length(item) != xy_len) {
27*2ccfa855SEd Maste fido_log_debug("%s: cbor type", __func__);
28*2ccfa855SEd Maste return (-1);
29*2ccfa855SEd Maste }
30*2ccfa855SEd Maste
31*2ccfa855SEd Maste memcpy(xy, cbor_bytestring_handle(item), xy_len);
32*2ccfa855SEd Maste
33*2ccfa855SEd Maste return (0);
34*2ccfa855SEd Maste }
35*2ccfa855SEd Maste
36*2ccfa855SEd Maste static int
decode_pubkey_point(const cbor_item_t * key,const cbor_item_t * val,void * arg)37*2ccfa855SEd Maste decode_pubkey_point(const cbor_item_t *key, const cbor_item_t *val, void *arg)
38*2ccfa855SEd Maste {
39*2ccfa855SEd Maste es384_pk_t *k = arg;
40*2ccfa855SEd Maste
41*2ccfa855SEd Maste if (cbor_isa_negint(key) == false ||
42*2ccfa855SEd Maste cbor_int_get_width(key) != CBOR_INT_8)
43*2ccfa855SEd Maste return (0); /* ignore */
44*2ccfa855SEd Maste
45*2ccfa855SEd Maste switch (cbor_get_uint8(key)) {
46*2ccfa855SEd Maste case 1: /* x coordinate */
47*2ccfa855SEd Maste return (decode_coord(val, &k->x, sizeof(k->x)));
48*2ccfa855SEd Maste case 2: /* y coordinate */
49*2ccfa855SEd Maste return (decode_coord(val, &k->y, sizeof(k->y)));
50*2ccfa855SEd Maste }
51*2ccfa855SEd Maste
52*2ccfa855SEd Maste return (0); /* ignore */
53*2ccfa855SEd Maste }
54*2ccfa855SEd Maste
55*2ccfa855SEd Maste int
es384_pk_decode(const cbor_item_t * item,es384_pk_t * k)56*2ccfa855SEd Maste es384_pk_decode(const cbor_item_t *item, es384_pk_t *k)
57*2ccfa855SEd Maste {
58*2ccfa855SEd Maste if (cbor_isa_map(item) == false ||
59*2ccfa855SEd Maste cbor_map_is_definite(item) == false ||
60*2ccfa855SEd Maste cbor_map_iter(item, k, decode_pubkey_point) < 0) {
61*2ccfa855SEd Maste fido_log_debug("%s: cbor type", __func__);
62*2ccfa855SEd Maste return (-1);
63*2ccfa855SEd Maste }
64*2ccfa855SEd Maste
65*2ccfa855SEd Maste return (0);
66*2ccfa855SEd Maste }
67*2ccfa855SEd Maste
68*2ccfa855SEd Maste es384_pk_t *
es384_pk_new(void)69*2ccfa855SEd Maste es384_pk_new(void)
70*2ccfa855SEd Maste {
71*2ccfa855SEd Maste return (calloc(1, sizeof(es384_pk_t)));
72*2ccfa855SEd Maste }
73*2ccfa855SEd Maste
74*2ccfa855SEd Maste void
es384_pk_free(es384_pk_t ** pkp)75*2ccfa855SEd Maste es384_pk_free(es384_pk_t **pkp)
76*2ccfa855SEd Maste {
77*2ccfa855SEd Maste es384_pk_t *pk;
78*2ccfa855SEd Maste
79*2ccfa855SEd Maste if (pkp == NULL || (pk = *pkp) == NULL)
80*2ccfa855SEd Maste return;
81*2ccfa855SEd Maste
82*2ccfa855SEd Maste freezero(pk, sizeof(*pk));
83*2ccfa855SEd Maste *pkp = NULL;
84*2ccfa855SEd Maste }
85*2ccfa855SEd Maste
86*2ccfa855SEd Maste int
es384_pk_from_ptr(es384_pk_t * pk,const void * ptr,size_t len)87*2ccfa855SEd Maste es384_pk_from_ptr(es384_pk_t *pk, const void *ptr, size_t len)
88*2ccfa855SEd Maste {
89*2ccfa855SEd Maste const uint8_t *p = ptr;
90*2ccfa855SEd Maste EVP_PKEY *pkey;
91*2ccfa855SEd Maste
92*2ccfa855SEd Maste if (len < sizeof(*pk))
93*2ccfa855SEd Maste return (FIDO_ERR_INVALID_ARGUMENT);
94*2ccfa855SEd Maste
95*2ccfa855SEd Maste if (len == sizeof(*pk) + 1 && *p == 0x04)
96*2ccfa855SEd Maste memcpy(pk, ++p, sizeof(*pk)); /* uncompressed format */
97*2ccfa855SEd Maste else
98*2ccfa855SEd Maste memcpy(pk, ptr, sizeof(*pk)); /* libfido2 x||y format */
99*2ccfa855SEd Maste
100*2ccfa855SEd Maste if ((pkey = es384_pk_to_EVP_PKEY(pk)) == NULL) {
101*2ccfa855SEd Maste fido_log_debug("%s: es384_pk_to_EVP_PKEY", __func__);
102*2ccfa855SEd Maste explicit_bzero(pk, sizeof(*pk));
103*2ccfa855SEd Maste return (FIDO_ERR_INVALID_ARGUMENT);
104*2ccfa855SEd Maste }
105*2ccfa855SEd Maste
106*2ccfa855SEd Maste EVP_PKEY_free(pkey);
107*2ccfa855SEd Maste
108*2ccfa855SEd Maste return (FIDO_OK);
109*2ccfa855SEd Maste }
110*2ccfa855SEd Maste
111*2ccfa855SEd Maste EVP_PKEY *
es384_pk_to_EVP_PKEY(const es384_pk_t * k)112*2ccfa855SEd Maste es384_pk_to_EVP_PKEY(const es384_pk_t *k)
113*2ccfa855SEd Maste {
114*2ccfa855SEd Maste BN_CTX *bnctx = NULL;
115*2ccfa855SEd Maste EC_KEY *ec = NULL;
116*2ccfa855SEd Maste EC_POINT *q = NULL;
117*2ccfa855SEd Maste EVP_PKEY *pkey = NULL;
118*2ccfa855SEd Maste BIGNUM *x = NULL;
119*2ccfa855SEd Maste BIGNUM *y = NULL;
120*2ccfa855SEd Maste const EC_GROUP *g = NULL;
121*2ccfa855SEd Maste int ok = -1;
122*2ccfa855SEd Maste
123*2ccfa855SEd Maste if ((bnctx = BN_CTX_new()) == NULL)
124*2ccfa855SEd Maste goto fail;
125*2ccfa855SEd Maste
126*2ccfa855SEd Maste BN_CTX_start(bnctx);
127*2ccfa855SEd Maste
128*2ccfa855SEd Maste if ((x = BN_CTX_get(bnctx)) == NULL ||
129*2ccfa855SEd Maste (y = BN_CTX_get(bnctx)) == NULL)
130*2ccfa855SEd Maste goto fail;
131*2ccfa855SEd Maste
132*2ccfa855SEd Maste if (BN_bin2bn(k->x, sizeof(k->x), x) == NULL ||
133*2ccfa855SEd Maste BN_bin2bn(k->y, sizeof(k->y), y) == NULL) {
134*2ccfa855SEd Maste fido_log_debug("%s: BN_bin2bn", __func__);
135*2ccfa855SEd Maste goto fail;
136*2ccfa855SEd Maste }
137*2ccfa855SEd Maste
138*2ccfa855SEd Maste if ((ec = EC_KEY_new_by_curve_name(NID_secp384r1)) == NULL ||
139*2ccfa855SEd Maste (g = EC_KEY_get0_group(ec)) == NULL) {
140*2ccfa855SEd Maste fido_log_debug("%s: EC_KEY init", __func__);
141*2ccfa855SEd Maste goto fail;
142*2ccfa855SEd Maste }
143*2ccfa855SEd Maste
144*2ccfa855SEd Maste if ((q = EC_POINT_new(g)) == NULL ||
145*2ccfa855SEd Maste EC_POINT_set_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
146*2ccfa855SEd Maste EC_KEY_set_public_key(ec, q) == 0) {
147*2ccfa855SEd Maste fido_log_debug("%s: EC_KEY_set_public_key", __func__);
148*2ccfa855SEd Maste goto fail;
149*2ccfa855SEd Maste }
150*2ccfa855SEd Maste
151*2ccfa855SEd Maste if ((pkey = EVP_PKEY_new()) == NULL ||
152*2ccfa855SEd Maste EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
153*2ccfa855SEd Maste fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
154*2ccfa855SEd Maste goto fail;
155*2ccfa855SEd Maste }
156*2ccfa855SEd Maste
157*2ccfa855SEd Maste ec = NULL; /* at this point, ec belongs to evp */
158*2ccfa855SEd Maste
159*2ccfa855SEd Maste ok = 0;
160*2ccfa855SEd Maste fail:
161*2ccfa855SEd Maste if (bnctx != NULL) {
162*2ccfa855SEd Maste BN_CTX_end(bnctx);
163*2ccfa855SEd Maste BN_CTX_free(bnctx);
164*2ccfa855SEd Maste }
165*2ccfa855SEd Maste
166*2ccfa855SEd Maste if (ec != NULL)
167*2ccfa855SEd Maste EC_KEY_free(ec);
168*2ccfa855SEd Maste if (q != NULL)
169*2ccfa855SEd Maste EC_POINT_free(q);
170*2ccfa855SEd Maste
171*2ccfa855SEd Maste if (ok < 0 && pkey != NULL) {
172*2ccfa855SEd Maste EVP_PKEY_free(pkey);
173*2ccfa855SEd Maste pkey = NULL;
174*2ccfa855SEd Maste }
175*2ccfa855SEd Maste
176*2ccfa855SEd Maste return (pkey);
177*2ccfa855SEd Maste }
178*2ccfa855SEd Maste
179*2ccfa855SEd Maste int
es384_pk_from_EC_KEY(es384_pk_t * pk,const EC_KEY * ec)180*2ccfa855SEd Maste es384_pk_from_EC_KEY(es384_pk_t *pk, const EC_KEY *ec)
181*2ccfa855SEd Maste {
182*2ccfa855SEd Maste BN_CTX *bnctx = NULL;
183*2ccfa855SEd Maste BIGNUM *x = NULL;
184*2ccfa855SEd Maste BIGNUM *y = NULL;
185*2ccfa855SEd Maste const EC_POINT *q = NULL;
186*2ccfa855SEd Maste EC_GROUP *g = NULL;
187*2ccfa855SEd Maste size_t dx;
188*2ccfa855SEd Maste size_t dy;
189*2ccfa855SEd Maste int ok = FIDO_ERR_INTERNAL;
190*2ccfa855SEd Maste int nx;
191*2ccfa855SEd Maste int ny;
192*2ccfa855SEd Maste
193*2ccfa855SEd Maste if ((q = EC_KEY_get0_public_key(ec)) == NULL ||
194*2ccfa855SEd Maste (g = EC_GROUP_new_by_curve_name(NID_secp384r1)) == NULL ||
195*2ccfa855SEd Maste (bnctx = BN_CTX_new()) == NULL)
196*2ccfa855SEd Maste goto fail;
197*2ccfa855SEd Maste
198*2ccfa855SEd Maste BN_CTX_start(bnctx);
199*2ccfa855SEd Maste
200*2ccfa855SEd Maste if ((x = BN_CTX_get(bnctx)) == NULL ||
201*2ccfa855SEd Maste (y = BN_CTX_get(bnctx)) == NULL)
202*2ccfa855SEd Maste goto fail;
203*2ccfa855SEd Maste
204*2ccfa855SEd Maste if (EC_POINT_is_on_curve(g, q, bnctx) != 1) {
205*2ccfa855SEd Maste fido_log_debug("%s: EC_POINT_is_on_curve", __func__);
206*2ccfa855SEd Maste ok = FIDO_ERR_INVALID_ARGUMENT;
207*2ccfa855SEd Maste goto fail;
208*2ccfa855SEd Maste }
209*2ccfa855SEd Maste
210*2ccfa855SEd Maste if (EC_POINT_get_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
211*2ccfa855SEd Maste (nx = BN_num_bytes(x)) < 0 || (size_t)nx > sizeof(pk->x) ||
212*2ccfa855SEd Maste (ny = BN_num_bytes(y)) < 0 || (size_t)ny > sizeof(pk->y)) {
213*2ccfa855SEd Maste fido_log_debug("%s: EC_POINT_get_affine_coordinates_GFp",
214*2ccfa855SEd Maste __func__);
215*2ccfa855SEd Maste goto fail;
216*2ccfa855SEd Maste }
217*2ccfa855SEd Maste
218*2ccfa855SEd Maste dx = sizeof(pk->x) - (size_t)nx;
219*2ccfa855SEd Maste dy = sizeof(pk->y) - (size_t)ny;
220*2ccfa855SEd Maste
221*2ccfa855SEd Maste if ((nx = BN_bn2bin(x, pk->x + dx)) < 0 || (size_t)nx > sizeof(pk->x) ||
222*2ccfa855SEd Maste (ny = BN_bn2bin(y, pk->y + dy)) < 0 || (size_t)ny > sizeof(pk->y)) {
223*2ccfa855SEd Maste fido_log_debug("%s: BN_bn2bin", __func__);
224*2ccfa855SEd Maste goto fail;
225*2ccfa855SEd Maste }
226*2ccfa855SEd Maste
227*2ccfa855SEd Maste ok = FIDO_OK;
228*2ccfa855SEd Maste fail:
229*2ccfa855SEd Maste EC_GROUP_free(g);
230*2ccfa855SEd Maste
231*2ccfa855SEd Maste if (bnctx != NULL) {
232*2ccfa855SEd Maste BN_CTX_end(bnctx);
233*2ccfa855SEd Maste BN_CTX_free(bnctx);
234*2ccfa855SEd Maste }
235*2ccfa855SEd Maste
236*2ccfa855SEd Maste return (ok);
237*2ccfa855SEd Maste }
238*2ccfa855SEd Maste
239*2ccfa855SEd Maste int
es384_pk_from_EVP_PKEY(es384_pk_t * pk,const EVP_PKEY * pkey)240*2ccfa855SEd Maste es384_pk_from_EVP_PKEY(es384_pk_t *pk, const EVP_PKEY *pkey)
241*2ccfa855SEd Maste {
242*2ccfa855SEd Maste const EC_KEY *ec;
243*2ccfa855SEd Maste
244*2ccfa855SEd Maste if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC ||
245*2ccfa855SEd Maste (ec = get0_EC_KEY(pkey)) == NULL)
246*2ccfa855SEd Maste return (FIDO_ERR_INVALID_ARGUMENT);
247*2ccfa855SEd Maste
248*2ccfa855SEd Maste return (es384_pk_from_EC_KEY(pk, ec));
249*2ccfa855SEd Maste }
250*2ccfa855SEd Maste
251*2ccfa855SEd Maste int
es384_verify_sig(const fido_blob_t * dgst,EVP_PKEY * pkey,const fido_blob_t * sig)252*2ccfa855SEd Maste es384_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
253*2ccfa855SEd Maste const fido_blob_t *sig)
254*2ccfa855SEd Maste {
255*2ccfa855SEd Maste EVP_PKEY_CTX *pctx = NULL;
256*2ccfa855SEd Maste int ok = -1;
257*2ccfa855SEd Maste
258*2ccfa855SEd Maste if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
259*2ccfa855SEd Maste fido_log_debug("%s: EVP_PKEY_base_id", __func__);
260*2ccfa855SEd Maste goto fail;
261*2ccfa855SEd Maste }
262*2ccfa855SEd Maste
263*2ccfa855SEd Maste if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL ||
264*2ccfa855SEd Maste EVP_PKEY_verify_init(pctx) != 1 ||
265*2ccfa855SEd Maste EVP_PKEY_verify(pctx, sig->ptr, sig->len, dgst->ptr,
266*2ccfa855SEd Maste dgst->len) != 1) {
267*2ccfa855SEd Maste fido_log_debug("%s: EVP_PKEY_verify", __func__);
268*2ccfa855SEd Maste goto fail;
269*2ccfa855SEd Maste }
270*2ccfa855SEd Maste
271*2ccfa855SEd Maste ok = 0;
272*2ccfa855SEd Maste fail:
273*2ccfa855SEd Maste EVP_PKEY_CTX_free(pctx);
274*2ccfa855SEd Maste
275*2ccfa855SEd Maste return (ok);
276*2ccfa855SEd Maste }
277*2ccfa855SEd Maste
278*2ccfa855SEd Maste int
es384_pk_verify_sig(const fido_blob_t * dgst,const es384_pk_t * pk,const fido_blob_t * sig)279*2ccfa855SEd Maste es384_pk_verify_sig(const fido_blob_t *dgst, const es384_pk_t *pk,
280*2ccfa855SEd Maste const fido_blob_t *sig)
281*2ccfa855SEd Maste {
282*2ccfa855SEd Maste EVP_PKEY *pkey;
283*2ccfa855SEd Maste int ok = -1;
284*2ccfa855SEd Maste
285*2ccfa855SEd Maste if ((pkey = es384_pk_to_EVP_PKEY(pk)) == NULL ||
286*2ccfa855SEd Maste es384_verify_sig(dgst, pkey, sig) < 0) {
287*2ccfa855SEd Maste fido_log_debug("%s: es384_verify_sig", __func__);
288*2ccfa855SEd Maste goto fail;
289*2ccfa855SEd Maste }
290*2ccfa855SEd Maste
291*2ccfa855SEd Maste ok = 0;
292*2ccfa855SEd Maste fail:
293*2ccfa855SEd Maste EVP_PKEY_free(pkey);
294*2ccfa855SEd Maste
295*2ccfa855SEd Maste return (ok);
296*2ccfa855SEd Maste }
297