Debian Codegraph @

src/ecdh.c

raw
1 /*
2 * Copyright (c) 2018-2026 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 #include <openssl/evp.h>
9 #include <openssl/sha.h>
10 #if defined(LIBRESSL_VERSION_NUMBER)
11 #include <openssl/hkdf.h>
12 #else
13 #include <openssl/kdf.h>
14 #endif
15
16 #include "fido.h"
17 #include "fido/es256.h"
18
19 #if defined(LIBRESSL_VERSION_NUMBER)
20 int
21 hkdf_sha256(uint8_t *key, size_t keylen, const char *info,
22 const fido_blob_t *secret)
23 {
24 const EVP_MD *md;
25 uint8_t salt[32];
26
27 memset(salt, 0, sizeof(salt));
28 if ((md = EVP_sha256()) == NULL ||
29 HKDF(key, keylen, md, secret->ptr, secret->len, salt,
30 sizeof(salt), (const uint8_t *)info, strlen(info)) != 1)
31 return -1;
32
33 return 0;
34 }
35 #else
36 int
37 hkdf_sha256(uint8_t *key, size_t keylen, const char *info,
38 const fido_blob_t *secret)
39 {
40 const EVP_MD *md;
41 EVP_PKEY_CTX *ctx = NULL;
42 uint8_t salt[32];
43 int ok = -1;
44
45 memset(salt, 0, sizeof(salt));
46 if (secret->len > INT_MAX || strlen(info) > INT_MAX) {
47 fido_log_debug("%s: invalid param", __func__);
48 goto fail;
49 }
50 if ((md = EVP_sha256()) == NULL ||
51 (ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) {
52 fido_log_debug("%s: init", __func__);
53 goto fail;
54 }
55 if (EVP_PKEY_derive_init(ctx) < 1 ||
56 EVP_PKEY_CTX_set_hkdf_md(ctx, md) < 1 ||
57 EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)) < 1 ||
58 EVP_PKEY_CTX_set1_hkdf_key(ctx, secret->ptr, (int)secret->len) < 1 ||
59 EVP_PKEY_CTX_add1_hkdf_info(ctx, (const void *)info,
60 (int)strlen(info)) < 1) {
61 fido_log_debug("%s: EVP_PKEY_CTX", __func__);
62 goto fail;
63 }
64 if (EVP_PKEY_derive(ctx, key, &keylen) < 1) {
65 fido_log_debug("%s: EVP_PKEY_derive", __func__);
66 goto fail;
67 }
68
69 ok = 0;
70 fail:
71 if (ctx != NULL)
72 EVP_PKEY_CTX_free(ctx);
73
74 return ok;
75 }
76 #endif /* defined(LIBRESSL_VERSION_NUMBER) */
77
78 static int
79 kdf(uint8_t prot, fido_blob_t *key, const fido_blob_t *secret)
80 {
81 const char hmac_info[] = "CTAP2 HMAC key";
82 const char aes_info[] = "CTAP2 AES key";
83
84 switch (prot) {
85 case CTAP_PIN_PROTOCOL1:
86 /* use sha256 on the resulting secret */
87 key->len = SHA256_DIGEST_LENGTH;
88 if ((key->ptr = calloc(1, key->len)) == NULL ||
89 SHA256(secret->ptr, secret->len, key->ptr) != key->ptr) {
90 fido_log_debug("%s: SHA256", __func__);
91 return -1;
92 }
93 break;
94 case CTAP_PIN_PROTOCOL2:
95 /* use two instances of hkdf-sha256 on the resulting secret */
96 key->len = 2 * SHA256_DIGEST_LENGTH;
97 if ((key->ptr = calloc(1, key->len)) == NULL ||
98 hkdf_sha256(key->ptr, SHA256_DIGEST_LENGTH, hmac_info, secret) < 0 ||
99 hkdf_sha256(key->ptr + SHA256_DIGEST_LENGTH, SHA256_DIGEST_LENGTH,
100 aes_info, secret) < 0) {
101 fido_log_debug("%s: hkdf", __func__);
102 return -1;
103 }
104 break;
105 default:
106 fido_log_debug("%s: unknown pin protocol %u", __func__, prot);
107 return -1;
108 }
109
110 return 0;
111 }
112
113 static int
114 do_ecdh(const fido_dev_t *dev, const es256_sk_t *sk, const es256_pk_t *pk,
115 fido_blob_t **ecdh)
116 {
117 EVP_PKEY *pk_evp = NULL;
118 EVP_PKEY *sk_evp = NULL;
119 EVP_PKEY_CTX *ctx = NULL;
120 fido_blob_t *secret = NULL;
121 int ok = -1;
122
123 *ecdh = NULL;
124 if ((secret = fido_blob_new()) == NULL ||
125 (*ecdh = fido_blob_new()) == NULL)
126 goto fail;
127 if ((pk_evp = es256_pk_to_EVP_PKEY(pk)) == NULL ||
128 (sk_evp = es256_sk_to_EVP_PKEY(sk)) == NULL) {
129 fido_log_debug("%s: es256_to_EVP_PKEY", __func__);
130 goto fail;
131 }
132 if ((ctx = EVP_PKEY_CTX_new(sk_evp, NULL)) == NULL ||
133 EVP_PKEY_derive_init(ctx) <= 0 ||
134 EVP_PKEY_derive_set_peer(ctx, pk_evp) <= 0) {
135 fido_log_debug("%s: EVP_PKEY_derive_init", __func__);
136 goto fail;
137 }
138 if (EVP_PKEY_derive(ctx, NULL, &secret->len) <= 0 ||
139 (secret->ptr = calloc(1, secret->len)) == NULL ||
140 EVP_PKEY_derive(ctx, secret->ptr, &secret->len) <= 0) {
141 fido_log_debug("%s: EVP_PKEY_derive", __func__);
142 goto fail;
143 }
144 if (kdf(fido_dev_get_pin_protocol(dev), *ecdh, secret) < 0) {
145 fido_log_debug("%s: kdf", __func__);
146 goto fail;
147 }
148
149 ok = 0;
150 fail:
151 if (pk_evp != NULL)
152 EVP_PKEY_free(pk_evp);
153 if (sk_evp != NULL)
154 EVP_PKEY_free(sk_evp);
155 if (ctx != NULL)
156 EVP_PKEY_CTX_free(ctx);
157 if (ok < 0)
158 fido_blob_free(ecdh);
159
160 fido_blob_free(&secret);
161
162 return ok;
163 }
164
165 int
166 fido_do_ecdh(fido_dev_t *dev, es256_pk_t **pk, fido_blob_t **ecdh, int *ms)
167 {
168 es256_sk_t *sk = NULL; /* our private key */
169 es256_pk_t *ak = NULL; /* authenticator's public key */
170 int r;
171
172 *pk = NULL;
173 *ecdh = NULL;
174 if ((sk = es256_sk_new()) == NULL || (*pk = es256_pk_new()) == NULL) {
175 r = FIDO_ERR_INTERNAL;
176 goto fail;
177 }
178 if (es256_sk_create(sk) < 0 || es256_derive_pk(sk, *pk) < 0) {
179 fido_log_debug("%s: es256_derive_pk", __func__);
180 r = FIDO_ERR_INTERNAL;
181 goto fail;
182 }
183 if ((ak = es256_pk_new()) == NULL ||
184 fido_dev_authkey(dev, ak, ms) != FIDO_OK) {
185 fido_log_debug("%s: fido_dev_authkey", __func__);
186 r = FIDO_ERR_INTERNAL;
187 goto fail;
188 }
189 if (do_ecdh(dev, sk, ak, ecdh) < 0) {
190 fido_log_debug("%s: do_ecdh", __func__);
191 r = FIDO_ERR_INTERNAL;
192 goto fail;
193 }
194
195 r = FIDO_OK;
196 fail:
197 es256_sk_free(&sk);
198 es256_pk_free(&ak);
199
200 if (r != FIDO_OK) {
201 es256_pk_free(pk);
202 fido_blob_free(ecdh);
203 }
204
205 return r;
206 }