xref: /freebsd/contrib/libfido2/regress/es256.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/es256.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 "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEAAeeHTZj4LEbt7Czs+u5gEZJfnGE\n"
29*2ccfa855SEd Maste "6Z+YLe4AYu7SoGY7IH/2jKifsA7w+lkURL4DL63oEjd3f8foH9bX4eaVug==\n"
30*2ccfa855SEd Maste "-----END PUBLIC KEY-----";
31*2ccfa855SEd Maste 
32*2ccfa855SEd Maste static const char short_y[] = \
33*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
34*2ccfa855SEd Maste "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEL8CWUP1r0tpJ5QmkzLc69O74C/Ti\n"
35*2ccfa855SEd Maste "83hTiys/JFNVkp0ArW3pKt5jNRrgWSZYE4S/D3AMtpqifFXz/FLCzJqojQ==\n"
36*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
37*2ccfa855SEd Maste 
38*2ccfa855SEd Maste static const char p256k1[] = \
39*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
40*2ccfa855SEd Maste "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEU1y8c0Jg9FGr3vYChpEo9c4dpkijriYM\n"
41*2ccfa855SEd Maste "QzU/DeskC89hZjLNH1Sj8ra2MsBlVGGJTNPCZSyx8Jo7ERapxdN7UQ==\n"
42*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
43*2ccfa855SEd Maste 
44*2ccfa855SEd Maste static const char p256v1[] = \
45*2ccfa855SEd Maste "-----BEGIN PUBLIC KEY-----\n"
46*2ccfa855SEd Maste "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEOwiq14c80b7C1Jzsx5w1zMvk2GgW\n"
47*2ccfa855SEd Maste "5kfGMOKXjwF/U+51ZfBDKehs3ivdeXAJBkxIh7E3iA32s+HyNqk+ntl9fg==\n"
48*2ccfa855SEd Maste "-----END PUBLIC KEY-----\n";
49*2ccfa855SEd Maste 
50*2ccfa855SEd Maste static const unsigned char p256k1_raw[] = {
51*2ccfa855SEd Maste 	0x04, 0x53, 0x5c, 0xbc, 0x73, 0x42, 0x60, 0xf4,
52*2ccfa855SEd Maste 	0x51, 0xab, 0xde, 0xf6, 0x02, 0x86, 0x91, 0x28,
53*2ccfa855SEd Maste 	0xf5, 0xce, 0x1d, 0xa6, 0x48, 0xa3, 0xae, 0x26,
54*2ccfa855SEd Maste 	0x0c, 0x43, 0x35, 0x3f, 0x0d, 0xeb, 0x24, 0x0b,
55*2ccfa855SEd Maste 	0xcf, 0x61, 0x66, 0x32, 0xcd, 0x1f, 0x54, 0xa3,
56*2ccfa855SEd Maste 	0xf2, 0xb6, 0xb6, 0x32, 0xc0, 0x65, 0x54, 0x61,
57*2ccfa855SEd Maste 	0x89, 0x4c, 0xd3, 0xc2, 0x65, 0x2c, 0xb1, 0xf0,
58*2ccfa855SEd Maste 	0x9a, 0x3b, 0x11, 0x16, 0xa9, 0xc5, 0xd3, 0x7b,
59*2ccfa855SEd Maste 	0x51,
60*2ccfa855SEd Maste };
61*2ccfa855SEd Maste 
62*2ccfa855SEd Maste static const unsigned char p256v1_raw[] = {
63*2ccfa855SEd Maste 	0x04, 0x3b, 0x08, 0xaa, 0xd7, 0x87, 0x3c, 0xd1,
64*2ccfa855SEd Maste 	0xbe, 0xc2, 0xd4, 0x9c, 0xec, 0xc7, 0x9c, 0x35,
65*2ccfa855SEd Maste 	0xcc, 0xcb, 0xe4, 0xd8, 0x68, 0x16, 0xe6, 0x47,
66*2ccfa855SEd Maste 	0xc6, 0x30, 0xe2, 0x97, 0x8f, 0x01, 0x7f, 0x53,
67*2ccfa855SEd Maste 	0xee, 0x75, 0x65, 0xf0, 0x43, 0x29, 0xe8, 0x6c,
68*2ccfa855SEd Maste 	0xde, 0x2b, 0xdd, 0x79, 0x70, 0x09, 0x06, 0x4c,
69*2ccfa855SEd Maste 	0x48, 0x87, 0xb1, 0x37, 0x88, 0x0d, 0xf6, 0xb3,
70*2ccfa855SEd Maste 	0xe1, 0xf2, 0x36, 0xa9, 0x3e, 0x9e, 0xd9, 0x7d,
71*2ccfa855SEd Maste 	0x7e,
72*2ccfa855SEd Maste };
73*2ccfa855SEd Maste 
74*2ccfa855SEd Maste static EVP_PKEY *
EVP_PKEY_from_PEM(const char * ptr,size_t len)75*2ccfa855SEd Maste EVP_PKEY_from_PEM(const char *ptr, size_t len)
76*2ccfa855SEd Maste {
77*2ccfa855SEd Maste 	BIO *bio = NULL;
78*2ccfa855SEd Maste 	EVP_PKEY *pkey = NULL;
79*2ccfa855SEd Maste 
80*2ccfa855SEd Maste 	if ((bio = BIO_new(BIO_s_mem())) == NULL) {
81*2ccfa855SEd Maste 		warnx("BIO_new");
82*2ccfa855SEd Maste 		goto out;
83*2ccfa855SEd Maste 	}
84*2ccfa855SEd Maste 	if (len > INT_MAX || BIO_write(bio, ptr, (int)len) != (int)len) {
85*2ccfa855SEd Maste 		warnx("BIO_write");
86*2ccfa855SEd Maste 		goto out;
87*2ccfa855SEd Maste 	}
88*2ccfa855SEd Maste 	if ((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)) == NULL)
89*2ccfa855SEd Maste 		warnx("PEM_read_bio_PUBKEY");
90*2ccfa855SEd Maste out:
91*2ccfa855SEd Maste 	BIO_free(bio);
92*2ccfa855SEd Maste 
93*2ccfa855SEd Maste 	return pkey;
94*2ccfa855SEd Maste }
95*2ccfa855SEd Maste 
96*2ccfa855SEd Maste static int
es256_pk_cmp(const char * ptr,size_t len)97*2ccfa855SEd Maste es256_pk_cmp(const char *ptr, size_t len)
98*2ccfa855SEd Maste {
99*2ccfa855SEd Maste 	EVP_PKEY *pkA = NULL;
100*2ccfa855SEd Maste 	EVP_PKEY *pkB = NULL;
101*2ccfa855SEd Maste 	es256_pk_t *k = NULL;
102*2ccfa855SEd Maste 	int r, ok = -1;
103*2ccfa855SEd Maste 
104*2ccfa855SEd Maste 	if ((pkA = EVP_PKEY_from_PEM(ptr, len)) == NULL) {
105*2ccfa855SEd Maste 		warnx("EVP_PKEY_from_PEM");
106*2ccfa855SEd Maste 		goto out;
107*2ccfa855SEd Maste 	}
108*2ccfa855SEd Maste 	if ((k = es256_pk_new()) == NULL) {
109*2ccfa855SEd Maste 		warnx("es256_pk_new");
110*2ccfa855SEd Maste 		goto out;
111*2ccfa855SEd Maste 	}
112*2ccfa855SEd Maste 	if ((r = es256_pk_from_EVP_PKEY(k, pkA)) != FIDO_OK) {
113*2ccfa855SEd Maste 		warnx("es256_pk_from_EVP_PKEY: 0x%x", r);
114*2ccfa855SEd Maste 		goto out;
115*2ccfa855SEd Maste 	}
116*2ccfa855SEd Maste 	if ((pkB = es256_pk_to_EVP_PKEY(k)) == NULL) {
117*2ccfa855SEd Maste 		warnx("es256_pk_to_EVP_PKEY");
118*2ccfa855SEd Maste 		goto out;
119*2ccfa855SEd Maste 	}
120*2ccfa855SEd Maste 	if ((r = EVP_PKEY_cmp(pkA, pkB)) != 1) {
121*2ccfa855SEd Maste 		warnx("EVP_PKEY_cmp: %d", r);
122*2ccfa855SEd Maste 		goto out;
123*2ccfa855SEd Maste 	}
124*2ccfa855SEd Maste 
125*2ccfa855SEd Maste 	ok = 0;
126*2ccfa855SEd Maste out:
127*2ccfa855SEd Maste 	EVP_PKEY_free(pkA);
128*2ccfa855SEd Maste 	EVP_PKEY_free(pkB);
129*2ccfa855SEd Maste 	es256_pk_free(&k);
130*2ccfa855SEd Maste 
131*2ccfa855SEd Maste 	return ok;
132*2ccfa855SEd Maste }
133*2ccfa855SEd Maste 
134*2ccfa855SEd Maste static void
short_coord(void)135*2ccfa855SEd Maste short_coord(void)
136*2ccfa855SEd Maste {
137*2ccfa855SEd Maste 	assert(es256_pk_cmp(short_x, sizeof(short_x)) == 0);
138*2ccfa855SEd Maste 	assert(es256_pk_cmp(short_y, sizeof(short_y)) == 0);
139*2ccfa855SEd Maste }
140*2ccfa855SEd Maste 
141*2ccfa855SEd Maste static void
invalid_curve(const unsigned char * raw,size_t raw_len)142*2ccfa855SEd Maste invalid_curve(const unsigned char *raw, size_t raw_len)
143*2ccfa855SEd Maste {
144*2ccfa855SEd Maste 	EVP_PKEY *pkey;
145*2ccfa855SEd Maste 	es256_pk_t *pk;
146*2ccfa855SEd Maste 
147*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkey = EVP_PKEY_from_PEM(p256k1, sizeof(p256k1))));
148*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pk = es256_pk_new()));
149*2ccfa855SEd Maste 	ASSERT_INVAL(es256_pk_from_EVP_PKEY(pk, pkey));
150*2ccfa855SEd Maste 	ASSERT_INVAL(es256_pk_from_ptr(pk, raw, raw_len));
151*2ccfa855SEd Maste 	ASSERT_NULL(es256_pk_to_EVP_PKEY((const es256_pk_t *)raw));
152*2ccfa855SEd Maste 
153*2ccfa855SEd Maste 	EVP_PKEY_free(pkey);
154*2ccfa855SEd Maste 	es256_pk_free(&pk);
155*2ccfa855SEd Maste }
156*2ccfa855SEd Maste 
157*2ccfa855SEd Maste static void
full_coord(void)158*2ccfa855SEd Maste full_coord(void)
159*2ccfa855SEd Maste {
160*2ccfa855SEd Maste 	assert(es256_pk_cmp(p256v1, sizeof(p256v1)) == 0);
161*2ccfa855SEd Maste }
162*2ccfa855SEd Maste 
163*2ccfa855SEd Maste static void
valid_curve(const unsigned char * raw,size_t raw_len)164*2ccfa855SEd Maste valid_curve(const unsigned char *raw, size_t raw_len)
165*2ccfa855SEd Maste {
166*2ccfa855SEd Maste 	EVP_PKEY *pkeyA;
167*2ccfa855SEd Maste 	EVP_PKEY *pkeyB;
168*2ccfa855SEd Maste 	es256_pk_t *pkA;
169*2ccfa855SEd Maste 	es256_pk_t *pkB;
170*2ccfa855SEd Maste 
171*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyA = EVP_PKEY_from_PEM(p256v1, sizeof(p256v1))));
172*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkA = es256_pk_new()));
173*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkB = es256_pk_new()));
174*2ccfa855SEd Maste 	ASSERT_OK(es256_pk_from_EVP_PKEY(pkA, pkeyA));
175*2ccfa855SEd Maste 	ASSERT_OK(es256_pk_from_ptr(pkB, raw, raw_len));
176*2ccfa855SEd Maste 	ASSERT_NOT_NULL((pkeyB = es256_pk_to_EVP_PKEY(pkB)));
177*2ccfa855SEd Maste 	assert(EVP_PKEY_cmp(pkeyA, pkeyB) == 1);
178*2ccfa855SEd Maste 
179*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyA);
180*2ccfa855SEd Maste 	EVP_PKEY_free(pkeyB);
181*2ccfa855SEd Maste 	es256_pk_free(&pkA);
182*2ccfa855SEd Maste 	es256_pk_free(&pkB);
183*2ccfa855SEd Maste }
184*2ccfa855SEd Maste 
185*2ccfa855SEd Maste int
main(void)186*2ccfa855SEd Maste main(void)
187*2ccfa855SEd Maste {
188*2ccfa855SEd Maste 	fido_init(0);
189*2ccfa855SEd Maste 
190*2ccfa855SEd Maste 	short_coord();
191*2ccfa855SEd Maste 	full_coord();
192*2ccfa855SEd Maste 
193*2ccfa855SEd Maste 	invalid_curve(p256k1_raw, sizeof(p256k1_raw)); /* uncompressed */
194*2ccfa855SEd Maste 	invalid_curve(p256k1_raw + 1, sizeof(p256k1_raw) - 1); /* libfido2 */
195*2ccfa855SEd Maste 	valid_curve(p256v1_raw, sizeof(p256v1_raw)); /* uncompressed */
196*2ccfa855SEd Maste 	valid_curve(p256v1_raw + 1, sizeof(p256v1_raw) - 1); /* libfido2 */
197*2ccfa855SEd Maste 
198*2ccfa855SEd Maste 	exit(0);
199*2ccfa855SEd Maste }
200