2 * Copyright (c) 2018 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.
7 #include <openssl/bn.h>
8 #include <openssl/obj_mac.h>
11 #include "fido/es256.h"
14 decode_coord(const cbor_item_t *item, void *xy, size_t xy_len)
16 if (cbor_isa_bytestring(item) == false ||
17 cbor_bytestring_is_definite(item) == false ||
18 cbor_bytestring_length(item) != xy_len) {
19 fido_log_debug("%s: cbor type", __func__);
23 memcpy(xy, cbor_bytestring_handle(item), xy_len);
29 decode_pubkey_point(const cbor_item_t *key, const cbor_item_t *val, void *arg)
33 if (cbor_isa_negint(key) == false ||
34 cbor_int_get_width(key) != CBOR_INT_8)
35 return (0); /* ignore */
37 switch (cbor_get_uint8(key)) {
38 case 1: /* x coordinate */
39 return (decode_coord(val, &k->x, sizeof(k->x)));
40 case 2: /* y coordinate */
41 return (decode_coord(val, &k->y, sizeof(k->y)));
44 return (0); /* ignore */
48 es256_pk_decode(const cbor_item_t *item, es256_pk_t *k)
50 if (cbor_isa_map(item) == false ||
51 cbor_map_is_definite(item) == false ||
52 cbor_map_iter(item, k, decode_pubkey_point) < 0) {
53 fido_log_debug("%s: cbor type", __func__);
61 es256_pk_encode(const es256_pk_t *pk, int ecdh)
63 cbor_item_t *item = NULL;
64 struct cbor_pair argv[5];
68 memset(argv, 0, sizeof(argv));
70 if ((item = cbor_new_definite_map(5)) == NULL)
74 if ((argv[0].key = cbor_build_uint8(1)) == NULL ||
75 (argv[0].value = cbor_build_uint8(2)) == NULL ||
76 !cbor_map_add(item, argv[0]))
80 * "The COSEAlgorithmIdentifier used is -25 (ECDH-ES +
81 * HKDF-256) although this is NOT the algorithm actually
82 * used. Setting this to a different value may result in
83 * compatibility issues."
86 alg = COSE_ECDH_ES256;
91 if ((argv[1].key = cbor_build_uint8(3)) == NULL ||
92 (argv[1].value = cbor_build_negint8((uint8_t)(-alg - 1))) == NULL ||
93 !cbor_map_add(item, argv[1]))
97 if ((argv[2].key = cbor_build_negint8(0)) == NULL ||
98 (argv[2].value = cbor_build_uint8(1)) == NULL ||
99 !cbor_map_add(item, argv[2]))
103 if ((argv[3].key = cbor_build_negint8(1)) == NULL ||
104 (argv[3].value = cbor_build_bytestring(pk->x,
105 sizeof(pk->x))) == NULL || !cbor_map_add(item, argv[3]))
109 if ((argv[4].key = cbor_build_negint8(2)) == NULL ||
110 (argv[4].value = cbor_build_bytestring(pk->y,
111 sizeof(pk->y))) == NULL || !cbor_map_add(item, argv[4]))
123 for (size_t i = 0; i < 5; i++) {
125 cbor_decref(&argv[i].key);
127 cbor_decref(&argv[i].value);
136 return (calloc(1, sizeof(es256_sk_t)));
140 es256_sk_free(es256_sk_t **skp)
144 if (skp == NULL || (sk = *skp) == NULL)
147 freezero(sk, sizeof(*sk));
154 return (calloc(1, sizeof(es256_pk_t)));
158 es256_pk_free(es256_pk_t **pkp)
162 if (pkp == NULL || (pk = *pkp) == NULL)
165 freezero(pk, sizeof(*pk));
170 es256_pk_from_ptr(es256_pk_t *pk, const void *ptr, size_t len)
172 const uint8_t *p = ptr;
174 if (len < sizeof(*pk))
175 return (FIDO_ERR_INVALID_ARGUMENT);
177 if (len == sizeof(*pk) + 1 && *p == 0x04)
178 memcpy(pk, ++p, sizeof(*pk)); /* uncompressed format */
180 memcpy(pk, ptr, sizeof(*pk)); /* libfido2 x||y format */
186 es256_pk_set_x(es256_pk_t *pk, const unsigned char *x)
188 memcpy(pk->x, x, sizeof(pk->x));
194 es256_pk_set_y(es256_pk_t *pk, const unsigned char *y)
196 memcpy(pk->y, y, sizeof(pk->y));
202 es256_sk_create(es256_sk_t *key)
204 EVP_PKEY_CTX *pctx = NULL;
205 EVP_PKEY_CTX *kctx = NULL;
210 const int nid = NID_X9_62_prime256v1;
214 if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL ||
215 EVP_PKEY_paramgen_init(pctx) <= 0 ||
216 EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid) <= 0 ||
217 EVP_PKEY_paramgen(pctx, &p) <= 0) {
218 fido_log_debug("%s: EVP_PKEY_paramgen", __func__);
222 if ((kctx = EVP_PKEY_CTX_new(p, NULL)) == NULL ||
223 EVP_PKEY_keygen_init(kctx) <= 0 || EVP_PKEY_keygen(kctx, &k) <= 0) {
224 fido_log_debug("%s: EVP_PKEY_keygen", __func__);
228 if ((ec = EVP_PKEY_get0_EC_KEY(k)) == NULL ||
229 (d = EC_KEY_get0_private_key(ec)) == NULL ||
230 (n = BN_num_bytes(d)) < 0 || (size_t)n > sizeof(key->d) ||
231 (n = BN_bn2bin(d, key->d)) < 0 || (size_t)n > sizeof(key->d)) {
232 fido_log_debug("%s: EC_KEY_get0_private_key", __func__);
243 EVP_PKEY_CTX_free(pctx);
245 EVP_PKEY_CTX_free(kctx);
251 es256_pk_to_EVP_PKEY(const es256_pk_t *k)
253 BN_CTX *bnctx = NULL;
256 EVP_PKEY *pkey = NULL;
259 const EC_GROUP *g = NULL;
260 const int nid = NID_X9_62_prime256v1;
263 if ((bnctx = BN_CTX_new()) == NULL)
268 if ((x = BN_CTX_get(bnctx)) == NULL ||
269 (y = BN_CTX_get(bnctx)) == NULL)
272 if (BN_bin2bn(k->x, sizeof(k->x), x) == NULL ||
273 BN_bin2bn(k->y, sizeof(k->y), y) == NULL) {
274 fido_log_debug("%s: BN_bin2bn", __func__);
278 if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
279 (g = EC_KEY_get0_group(ec)) == NULL) {
280 fido_log_debug("%s: EC_KEY init", __func__);
284 if ((q = EC_POINT_new(g)) == NULL ||
285 EC_POINT_set_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
286 EC_KEY_set_public_key(ec, q) == 0) {
287 fido_log_debug("%s: EC_KEY_set_public_key", __func__);
291 if ((pkey = EVP_PKEY_new()) == NULL ||
292 EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
293 fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
297 ec = NULL; /* at this point, ec belongs to evp */
311 if (ok < 0 && pkey != NULL) {
320 es256_pk_from_EC_KEY(es256_pk_t *pk, const EC_KEY *ec)
322 BN_CTX *bnctx = NULL;
325 const EC_POINT *q = NULL;
326 const EC_GROUP *g = NULL;
327 int ok = FIDO_ERR_INTERNAL;
330 if ((q = EC_KEY_get0_public_key(ec)) == NULL ||
331 (g = EC_KEY_get0_group(ec)) == NULL ||
332 (bnctx = BN_CTX_new()) == NULL)
337 if ((x = BN_CTX_get(bnctx)) == NULL ||
338 (y = BN_CTX_get(bnctx)) == NULL)
341 if (EC_POINT_get_affine_coordinates_GFp(g, q, x, y, bnctx) == 0 ||
342 (n = BN_num_bytes(x)) < 0 || (size_t)n > sizeof(pk->x) ||
343 (n = BN_num_bytes(y)) < 0 || (size_t)n > sizeof(pk->y)) {
344 fido_log_debug("%s: EC_POINT_get_affine_coordinates_GFp",
349 if ((n = BN_bn2bin(x, pk->x)) < 0 || (size_t)n > sizeof(pk->x) ||
350 (n = BN_bn2bin(y, pk->y)) < 0 || (size_t)n > sizeof(pk->y)) {
351 fido_log_debug("%s: BN_bn2bin", __func__);
366 es256_sk_to_EVP_PKEY(const es256_sk_t *k)
368 BN_CTX *bnctx = NULL;
370 EVP_PKEY *pkey = NULL;
372 const int nid = NID_X9_62_prime256v1;
375 if ((bnctx = BN_CTX_new()) == NULL)
380 if ((d = BN_CTX_get(bnctx)) == NULL ||
381 BN_bin2bn(k->d, sizeof(k->d), d) == NULL) {
382 fido_log_debug("%s: BN_bin2bn", __func__);
386 if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
387 EC_KEY_set_private_key(ec, d) == 0) {
388 fido_log_debug("%s: EC_KEY_set_private_key", __func__);
392 if ((pkey = EVP_PKEY_new()) == NULL ||
393 EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
394 fido_log_debug("%s: EVP_PKEY_assign_EC_KEY", __func__);
398 ec = NULL; /* at this point, ec belongs to evp */
410 if (ok < 0 && pkey != NULL) {
419 es256_derive_pk(const es256_sk_t *sk, es256_pk_t *pk)
424 const EC_GROUP *g = NULL;
425 const int nid = NID_X9_62_prime256v1;
428 if ((d = BN_bin2bn(sk->d, (int)sizeof(sk->d), NULL)) == NULL ||
429 (ec = EC_KEY_new_by_curve_name(nid)) == NULL ||
430 (g = EC_KEY_get0_group(ec)) == NULL ||
431 (q = EC_POINT_new(g)) == NULL) {
432 fido_log_debug("%s: get", __func__);
436 if (EC_POINT_mul(g, q, d, NULL, NULL, NULL) == 0 ||
437 EC_KEY_set_public_key(ec, q) == 0 ||
438 es256_pk_from_EC_KEY(pk, ec) != FIDO_OK) {
439 fido_log_debug("%s: set", __func__);