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