xref: /freebsd/contrib/libfido2/regress/es384.c (revision 2ccfa855b2fc331819953e3de1b1c15ce5b95a7e)
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 #undef NDEBUG
9*2ccfa855SEd Maste 
10*2ccfa855SEd Maste #include <assert.h>
11*2ccfa855SEd Maste #include <string.h>
12*2ccfa855SEd Maste 
13*2ccfa855SEd Maste #define _FIDO_INTERNAL
14*2ccfa855SEd Maste 
15*2ccfa855SEd Maste #include <fido.h>
16*2ccfa855SEd Maste #include <fido/es384.h>
17*2ccfa855SEd Maste 
18*2ccfa855SEd Maste #include <openssl/bio.h>
19*2ccfa855SEd Maste #include <openssl/pem.h>
20*2ccfa855SEd Maste 
21*2ccfa855SEd Maste #define ASSERT_NOT_NULL(e)	assert((e) != NULL)
22*2ccfa855SEd Maste #define ASSERT_NULL(e)		assert((e) == NULL)
23*2ccfa855SEd Maste #define ASSERT_INVAL(e)		assert((e) == FIDO_ERR_INVALID_ARGUMENT)
24*2ccfa855SEd Maste #define ASSERT_OK(e)		assert((e) == FIDO_OK)
25*2ccfa855SEd Maste 
26*2ccfa855SEd Maste static const char short_x[] = \
27*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
28*2ccfa855SEd Maste "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEAAZ/VVCUmFU6aH9kJdDnUHCCglkatFTX\n"
29*2ccfa855SEd Maste "onMwIvNYyS8BW/HOoZiOQLs2Hg+qifwaP1pHKILzCVfFmWuZMhxhtmjNXFuOPDnS\n"
30*2ccfa855SEd Maste "Wa1PMdkCoWXA2BbXxnqL9v36gIOcFBil\n"
31*2ccfa855SEd Maste "-----END PUBLIC KEY-----";
32*2ccfa855SEd Maste 
33*2ccfa855SEd Maste static const char short_y[] = \
34*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
35*2ccfa855SEd Maste "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEuDpRBAg87cnWVhxbWnaWlnj100w9pm5k\n"
36*2ccfa855SEd Maste "6T4eYToISaIhEK70TnGwULHX0+qHCYEGACOM7B/ZJbqjo6I7MIXaKZLemGi+tqvy\n"
37*2ccfa855SEd Maste "ajBAsTVSyrYBLQjTMMcaFmYmsxvFx7pK\n"
38*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
39*2ccfa855SEd Maste 
40*2ccfa855SEd Maste static const char brainpoolP384r1[] = \
41*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
42*2ccfa855SEd Maste "MHowFAYHKoZIzj0CAQYJKyQDAwIIAQELA2IABFKswbBzqqyZ4h1zz8rivqHzJxAO\n"
43*2ccfa855SEd Maste "XC2aLyC9x5gwBM7GVu8k6jkX7VypRpg3yyCneiIQ+vVCNXgbDchJ0cPVuhwm3Zru\n"
44*2ccfa855SEd Maste "AK49dezUPahWF0YiJRFVeV+KyB/MEaaZvinzqw==\n"
45*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
46*2ccfa855SEd Maste 
47*2ccfa855SEd Maste static const char secp384r1[] = \
48*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
49*2ccfa855SEd Maste "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEdJN9DoqPtTNAOmjnECHBIqnJgyBW0rct\n"
50*2ccfa855SEd Maste "tbUSqQjb6UG2lldmrQJbgCP/ywuXvkkJl4yfXxOr0UP3rgcnqTVA1/46s2TG+R5u\n"
51*2ccfa855SEd Maste "NSQbCM1JPQuvTyFlAn5mdR8ZJJ8yPBQm\n"
52*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
53*2ccfa855SEd Maste 
54*2ccfa855SEd Maste static const unsigned char brainpoolP384r1_raw[] = {
55*2ccfa855SEd Maste 	0x04, 0x52, 0xac, 0xc1, 0xb0, 0x73, 0xaa, 0xac,
56*2ccfa855SEd Maste 	0x99, 0xe2, 0x1d, 0x73, 0xcf, 0xca, 0xe2, 0xbe,
57*2ccfa855SEd Maste 	0xa1, 0xf3, 0x27, 0x10, 0x0e, 0x5c, 0x2d, 0x9a,
58*2ccfa855SEd Maste 	0x2f, 0x20, 0xbd, 0xc7, 0x98, 0x30, 0x04, 0xce,
59*2ccfa855SEd Maste 	0xc6, 0x56, 0xef, 0x24, 0xea, 0x39, 0x17, 0xed,
60*2ccfa855SEd Maste 	0x5c, 0xa9, 0x46, 0x98, 0x37, 0xcb, 0x20, 0xa7,
61*2ccfa855SEd Maste 	0x7a, 0x22, 0x10, 0xfa, 0xf5, 0x42, 0x35, 0x78,
62*2ccfa855SEd Maste 	0x1b, 0x0d, 0xc8, 0x49, 0xd1, 0xc3, 0xd5, 0xba,
63*2ccfa855SEd Maste 	0x1c, 0x26, 0xdd, 0x9a, 0xee, 0x00, 0xae, 0x3d,
64*2ccfa855SEd Maste 	0x75, 0xec, 0xd4, 0x3d, 0xa8, 0x56, 0x17, 0x46,
65*2ccfa855SEd Maste 	0x22, 0x25, 0x11, 0x55, 0x79, 0x5f, 0x8a, 0xc8,
66*2ccfa855SEd Maste 	0x1f, 0xcc, 0x11, 0xa6, 0x99, 0xbe, 0x29, 0xf3,
67*2ccfa855SEd Maste 	0xab,
68*2ccfa855SEd Maste };
69*2ccfa855SEd Maste 
70*2ccfa855SEd Maste static const unsigned char secp384r1_raw[] = {
71*2ccfa855SEd Maste 	0x04, 0x74, 0x93, 0x7d, 0x0e, 0x8a, 0x8f, 0xb5,
72*2ccfa855SEd Maste 	0x33, 0x40, 0x3a, 0x68, 0xe7, 0x10, 0x21, 0xc1,
73*2ccfa855SEd Maste 	0x22, 0xa9, 0xc9, 0x83, 0x20, 0x56, 0xd2, 0xb7,
74*2ccfa855SEd Maste 	0x2d, 0xb5, 0xb5, 0x12, 0xa9, 0x08, 0xdb, 0xe9,
75*2ccfa855SEd Maste 	0x41, 0xb6, 0x96, 0x57, 0x66, 0xad, 0x02, 0x5b,
76*2ccfa855SEd Maste 	0x80, 0x23, 0xff, 0xcb, 0x0b, 0x97, 0xbe, 0x49,
77*2ccfa855SEd Maste 	0x09, 0x97, 0x8c, 0x9f, 0x5f, 0x13, 0xab, 0xd1,
78*2ccfa855SEd Maste 	0x43, 0xf7, 0xae, 0x07, 0x27, 0xa9, 0x35, 0x40,
79*2ccfa855SEd Maste 	0xd7, 0xfe, 0x3a, 0xb3, 0x64, 0xc6, 0xf9, 0x1e,
80*2ccfa855SEd Maste 	0x6e, 0x35, 0x24, 0x1b, 0x08, 0xcd, 0x49, 0x3d,
81*2ccfa855SEd Maste 	0x0b, 0xaf, 0x4f, 0x21, 0x65, 0x02, 0x7e, 0x66,
82*2ccfa855SEd Maste 	0x75, 0x1f, 0x19, 0x24, 0x9f, 0x32, 0x3c, 0x14,
83*2ccfa855SEd Maste 	0x26,
84*2ccfa855SEd Maste };
85*2ccfa855SEd Maste 
86*2ccfa855SEd Maste static EVP_PKEY *
EVP_PKEY_from_PEM(const char * ptr,size_t len)87*2ccfa855SEd Maste EVP_PKEY_from_PEM(const char *ptr, size_t len)
88*2ccfa855SEd Maste {
89*2ccfa855SEd Maste 	BIO *bio = NULL;
90*2ccfa855SEd Maste 	EVP_PKEY *pkey = NULL;
91*2ccfa855SEd Maste 
92*2ccfa855SEd Maste 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
93*2ccfa855SEd Maste 		warnx("BIO_new");
94*2ccfa855SEd Maste 		goto out;
95*2ccfa855SEd Maste 	}
96*2ccfa855SEd Maste 	if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) {
97*2ccfa855SEd Maste 		warnx("BIO_write");
98*2ccfa855SEd Maste 		goto out;
99*2ccfa855SEd Maste 	}
100*2ccfa855SEd Maste 	if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL)
101*2ccfa855SEd Maste 		warnx("PEM_read_bio_PUBKEY");
102*2ccfa855SEd Maste out:
103*2ccfa855SEd Maste 	BIO_free(bio);
104*2ccfa855SEd Maste 
105*2ccfa855SEd Maste 	return pkey;
106*2ccfa855SEd Maste }
107*2ccfa855SEd Maste 
108*2ccfa855SEd Maste static int
es384_pk_cmp(const char * ptr,size_t len)109*2ccfa855SEd Maste es384_pk_cmp(const char *ptr, size_t len)
110*2ccfa855SEd Maste {
111*2ccfa855SEd Maste 	EVP_PKEY *pkA = NULL;
112*2ccfa855SEd Maste 	EVP_PKEY *pkB = NULL;
113*2ccfa855SEd Maste 	es384_pk_t *k = NULL;
114*2ccfa855SEd Maste 	int r, ok = -1;
115*2ccfa855SEd Maste 
116*2ccfa855SEd Maste 	if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) {
117*2ccfa855SEd Maste 		warnx("EVP_PKEY_from_PEM");
118*2ccfa855SEd Maste 		goto out;
119*2ccfa855SEd Maste 	}
120*2ccfa855SEd Maste 	if ((k = es384_pk_new()) == NULL) {
121*2ccfa855SEd Maste 		warnx("es384_pk_new");
122*2ccfa855SEd Maste 		goto out;
123*2ccfa855SEd Maste 	}
124*2ccfa855SEd Maste 	if ((r = es384_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) {
125*2ccfa855SEd Maste 		warnx("es384_pk_from_EVP_PKEY: 0x%x", r);
126*2ccfa855SEd Maste 		goto out;
127*2ccfa855SEd Maste 	}
128*2ccfa855SEd Maste 	if ((pkB = es384_pk_to_EVP_PKEY(k)) == NULL) {
129*2ccfa855SEd Maste 		warnx("es384_pk_to_EVP_PKEY");
130*2ccfa855SEd Maste 		goto out;
131*2ccfa855SEd Maste 	}
132*2ccfa855SEd Maste 	if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) {
133*2ccfa855SEd Maste 		warnx("EVP_PKEY_cmp: %d", r);
134*2ccfa855SEd Maste 		goto out;
135*2ccfa855SEd Maste 	}
136*2ccfa855SEd Maste 
137*2ccfa855SEd Maste 	ok = 0;
138*2ccfa855SEd Maste out:
139*2ccfa855SEd Maste 	EVP_PKEY_free(pkA);
140*2ccfa855SEd Maste 	EVP_PKEY_free(pkB);
141*2ccfa855SEd Maste 	es384_pk_free(&k);
142*2ccfa855SEd Maste 
143*2ccfa855SEd Maste 	return ok;
144*2ccfa855SEd Maste }
145*2ccfa855SEd Maste 
146*2ccfa855SEd Maste static void
short_coord(void)147*2ccfa855SEd Maste short_coord(void)
148*2ccfa855SEd Maste {
149*2ccfa855SEd Maste 	assert(es384_pk_cmp(short_x, sizeof(short_x)) == 0);
150*2ccfa855SEd Maste 	assert(es384_pk_cmp(short_y, sizeof(short_y)) == 0);
151*2ccfa855SEd Maste }
152*2ccfa855SEd Maste 
153*2ccfa855SEd Maste static void
invalid_curve(const unsigned char * raw,size_t raw_len)154*2ccfa855SEd Maste invalid_curve(const unsigned char *raw, size_t raw_len)
155*2ccfa855SEd Maste {
156*2ccfa855SEd Maste 	EVP_PKEY *pkey;
157*2ccfa855SEd Maste 	es384_pk_t *pk;
158*2ccfa855SEd Maste 
159*2ccfa855SEd Maste 	pkey = EVP_PKEY_from_PEM(brainpoolP384r1, sizeof(brainpoolP384r1));
160*2ccfa855SEd Maste 	if (pkey == NULL)
161*2ccfa855SEd Maste 		return; /* assume no brainpool support in libcrypto */
162*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pk = es384_pk_new()));
163*2ccfa855SEd Maste 	ASSERT_INVAL(es384_pk_from_EVP_PKEY(pk, pkey));
164*2ccfa855SEd Maste 	ASSERT_INVAL(es384_pk_from_ptr(pk, raw, raw_len));
165*2ccfa855SEd Maste 	ASSERT_NULL(es384_pk_to_EVP_PKEY((const es384_pk_t *)raw));
166*2ccfa855SEd Maste 
167*2ccfa855SEd Maste 	EVP_PKEY_free(pkey);
168*2ccfa855SEd Maste 	es384_pk_free(&pk);
169*2ccfa855SEd Maste }
170*2ccfa855SEd Maste 
171*2ccfa855SEd Maste static void
full_coord(void)172*2ccfa855SEd Maste full_coord(void)
173*2ccfa855SEd Maste {
174*2ccfa855SEd Maste 	assert(es384_pk_cmp(secp384r1, sizeof(secp384r1)) == 0);
175*2ccfa855SEd Maste }
176*2ccfa855SEd Maste 
177*2ccfa855SEd Maste static void
valid_curve(const unsigned char * raw,size_t raw_len)178*2ccfa855SEd Maste valid_curve(const unsigned char *raw, size_t raw_len)
179*2ccfa855SEd Maste {
180*2ccfa855SEd Maste 	EVP_PKEY *pkeyA;
181*2ccfa855SEd Maste 	EVP_PKEY *pkeyB;
182*2ccfa855SEd Maste 	es384_pk_t *pkA;
183*2ccfa855SEd Maste 	es384_pk_t *pkB;
184*2ccfa855SEd Maste 
185*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(secp384r1, sizeof(secp384r1))));
186*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkA = es384_pk_new()));
187*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkB = es384_pk_new()));
188*2ccfa855SEd Maste 	ASSERT_OK(es384_pk_from_EVP_PKEY(pkA, pkeyA));
189*2ccfa855SEd Maste 	ASSERT_OK(es384_pk_from_ptr(pkB, raw, raw_len));
190*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyB = es384_pk_to_EVP_PKEY(pkB)));
191*2ccfa855SEd Maste 	assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1);
192*2ccfa855SEd Maste 
193*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyA);
194*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyB);
195*2ccfa855SEd Maste 	es384_pk_free(&pkA);
196*2ccfa855SEd Maste 	es384_pk_free(&pkB);
197*2ccfa855SEd Maste }
198*2ccfa855SEd Maste 
199*2ccfa855SEd Maste int
main(void)200*2ccfa855SEd Maste main(void)
201*2ccfa855SEd Maste {
202*2ccfa855SEd Maste 	fido_init(0);
203*2ccfa855SEd Maste 
204*2ccfa855SEd Maste 	short_coord();
205*2ccfa855SEd Maste 	full_coord();
206*2ccfa855SEd Maste 
207*2ccfa855SEd Maste 	invalid_curve(brainpoolP384r1_raw, sizeof(brainpoolP384r1_raw)); /* uncompressed */
208*2ccfa855SEd Maste 	invalid_curve(brainpoolP384r1_raw + 1, sizeof(brainpoolP384r1_raw) - 1); /* libfido2 */
209*2ccfa855SEd Maste 	valid_curve(secp384r1_raw, sizeof(secp384r1_raw)); /* uncompressed */
210*2ccfa855SEd Maste 	valid_curve(secp384r1_raw + 1, sizeof(secp384r1_raw) - 1); /* libfido2 */
211*2ccfa855SEd Maste 
212*2ccfa855SEd Maste 	exit(0);
213*2ccfa855SEd Maste }
214