/src/h2o/deps/picotls/lib/hpke.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022 Fastly, Kazuho Oku |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
5 | | * of this software and associated documentation files (the "Software"), to |
6 | | * deal in the Software without restriction, including without limitation the |
7 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
8 | | * sell copies of the Software, and to permit persons to whom the Software is |
9 | | * furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included in |
12 | | * all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
17 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
20 | | * IN THE SOFTWARE. |
21 | | */ |
22 | | #include <stdlib.h> |
23 | | #include "picotls.h" |
24 | | |
25 | | #define HPKE_V1_LABEL "HPKE-v1" |
26 | | |
27 | | static int build_suite_id(ptls_buffer_t *buf, ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher) |
28 | 0 | { |
29 | 0 | int ret; |
30 | |
|
31 | 0 | if (cipher == NULL) { |
32 | 0 | ptls_buffer_pushv(buf, "KEM", 3); |
33 | 0 | ptls_buffer_push16(buf, kem->id); |
34 | 0 | } else { |
35 | 0 | ptls_buffer_pushv(buf, "HPKE", 4); |
36 | 0 | ptls_buffer_push16(buf, kem->id); |
37 | 0 | ptls_buffer_push16(buf, cipher->id.kdf); |
38 | 0 | ptls_buffer_push16(buf, cipher->id.aead); |
39 | 0 | } |
40 | | |
41 | 0 | ret = 0; |
42 | |
|
43 | 0 | Exit: |
44 | 0 | return ret; |
45 | 0 | } |
46 | | |
47 | | static int labeled_extract(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, void *output, ptls_iovec_t salt, |
48 | | const char *label, ptls_iovec_t ikm) |
49 | 0 | { |
50 | 0 | ptls_buffer_t labeled_ikm; |
51 | 0 | uint8_t labeled_ikm_smallbuf[64]; |
52 | 0 | int ret; |
53 | |
|
54 | 0 | ptls_buffer_init(&labeled_ikm, labeled_ikm_smallbuf, sizeof(labeled_ikm_smallbuf)); |
55 | |
|
56 | 0 | ptls_buffer_pushv(&labeled_ikm, HPKE_V1_LABEL, strlen(HPKE_V1_LABEL)); |
57 | 0 | if ((ret = build_suite_id(&labeled_ikm, kem, cipher)) != 0) |
58 | 0 | goto Exit; |
59 | 0 | ptls_buffer_pushv(&labeled_ikm, label, strlen(label)); |
60 | 0 | ptls_buffer_pushv(&labeled_ikm, ikm.base, ikm.len); |
61 | | |
62 | 0 | ret = ptls_hkdf_extract(cipher != NULL ? cipher->hash : kem->hash, output, salt, |
63 | 0 | ptls_iovec_init(labeled_ikm.base, labeled_ikm.off)); |
64 | |
|
65 | 0 | Exit: |
66 | 0 | ptls_buffer_dispose(&labeled_ikm); |
67 | 0 | return ret; |
68 | 0 | } |
69 | | |
70 | | static int labeled_expand(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, void *output, size_t outlen, ptls_iovec_t prk, |
71 | | const char *label, ptls_iovec_t info) |
72 | 0 | { |
73 | 0 | ptls_buffer_t labeled_info; |
74 | 0 | uint8_t labeled_info_smallbuf[64]; |
75 | 0 | int ret; |
76 | |
|
77 | 0 | assert(outlen < UINT16_MAX); |
78 | | |
79 | 0 | ptls_buffer_init(&labeled_info, labeled_info_smallbuf, sizeof(labeled_info_smallbuf)); |
80 | |
|
81 | 0 | ptls_buffer_push16(&labeled_info, (uint16_t)outlen); |
82 | 0 | ptls_buffer_pushv(&labeled_info, HPKE_V1_LABEL, strlen(HPKE_V1_LABEL)); |
83 | 0 | if ((ret = build_suite_id(&labeled_info, kem, cipher)) != 0) |
84 | 0 | goto Exit; |
85 | 0 | ptls_buffer_pushv(&labeled_info, label, strlen(label)); |
86 | 0 | ptls_buffer_pushv(&labeled_info, info.base, info.len); |
87 | | |
88 | 0 | ret = ptls_hkdf_expand(cipher != NULL ? cipher->hash : kem->hash, output, outlen, prk, |
89 | 0 | ptls_iovec_init(labeled_info.base, labeled_info.off)); |
90 | |
|
91 | 0 | Exit: |
92 | 0 | ptls_buffer_dispose(&labeled_info); |
93 | 0 | return ret; |
94 | 0 | } |
95 | | |
96 | | static int extract_and_expand(ptls_hpke_kem_t *kem, void *secret, size_t secret_len, ptls_iovec_t pk_s, ptls_iovec_t pk_r, |
97 | | ptls_iovec_t dh) |
98 | 0 | { |
99 | 0 | ptls_buffer_t kem_context; |
100 | 0 | uint8_t kem_context_smallbuf[128], eae_prk[PTLS_MAX_DIGEST_SIZE]; |
101 | 0 | int ret; |
102 | |
|
103 | 0 | ptls_buffer_init(&kem_context, kem_context_smallbuf, sizeof(kem_context_smallbuf)); |
104 | |
|
105 | 0 | ptls_buffer_pushv(&kem_context, pk_s.base, pk_s.len); |
106 | 0 | ptls_buffer_pushv(&kem_context, pk_r.base, pk_r.len); |
107 | | |
108 | 0 | if ((ret = labeled_extract(kem, NULL, eae_prk, ptls_iovec_init("", 0), "eae_prk", dh)) != 0) |
109 | 0 | goto Exit; |
110 | 0 | if ((ret = labeled_expand(kem, NULL, secret, secret_len, ptls_iovec_init(eae_prk, kem->hash->digest_size), "shared_secret", |
111 | 0 | ptls_iovec_init(kem_context.base, kem_context.off))) != 0) |
112 | 0 | goto Exit; |
113 | | |
114 | 0 | Exit: |
115 | 0 | ptls_buffer_dispose(&kem_context); |
116 | 0 | ptls_clear_memory(eae_prk, sizeof(eae_prk)); |
117 | 0 | return ret; |
118 | 0 | } |
119 | | |
120 | | static int dh_derive(ptls_hpke_kem_t *kem, void *secret, ptls_iovec_t pk_s, ptls_iovec_t pk_r, ptls_iovec_t dh) |
121 | 0 | { |
122 | 0 | return extract_and_expand(kem, secret, kem->hash->digest_size, pk_s, pk_r, dh); |
123 | 0 | } |
124 | | |
125 | | static int dh_encap(ptls_hpke_kem_t *kem, void *secret, ptls_iovec_t *pk_s, ptls_iovec_t pk_r) |
126 | 0 | { |
127 | 0 | ptls_iovec_t dh = {NULL}; |
128 | 0 | int ret; |
129 | |
|
130 | 0 | *pk_s = ptls_iovec_init(NULL, 0); |
131 | |
|
132 | 0 | if ((ret = kem->keyex->exchange(kem->keyex, pk_s, &dh, pk_r)) != 0) { |
133 | 0 | assert(pk_s->base == NULL); |
134 | 0 | assert(dh.base == NULL); |
135 | 0 | goto Exit; |
136 | 0 | } |
137 | | |
138 | 0 | if ((ret = dh_derive(kem, secret, *pk_s, pk_r, dh)) != 0) |
139 | 0 | goto Exit; |
140 | | |
141 | 0 | Exit: |
142 | 0 | if (dh.base != NULL) { |
143 | 0 | ptls_clear_memory(dh.base, dh.len); |
144 | 0 | free(dh.base); |
145 | 0 | } |
146 | 0 | if (ret != 0) { |
147 | 0 | free(pk_s->base); |
148 | 0 | *pk_s = ptls_iovec_init(NULL, 0); |
149 | 0 | } |
150 | 0 | return ret; |
151 | 0 | } |
152 | | |
153 | | static int dh_decap(ptls_hpke_kem_t *kem, void *secret, ptls_key_exchange_context_t *keyex, ptls_iovec_t pk_s, ptls_iovec_t pk_r) |
154 | 0 | { |
155 | 0 | ptls_iovec_t dh = {NULL}; |
156 | 0 | int ret; |
157 | |
|
158 | 0 | if ((ret = keyex->on_exchange(&keyex, 0, &dh, pk_s)) != 0) { |
159 | 0 | assert(dh.base == NULL); |
160 | 0 | goto Exit; |
161 | 0 | } |
162 | | |
163 | 0 | if ((ret = dh_derive(kem, secret, pk_s, pk_r, dh)) != 0) |
164 | 0 | goto Exit; |
165 | | |
166 | 0 | Exit: |
167 | 0 | if (dh.base != NULL) { |
168 | 0 | ptls_clear_memory(dh.base, dh.len); |
169 | 0 | free(dh.base); |
170 | 0 | } |
171 | 0 | return ret; |
172 | 0 | } |
173 | | |
174 | | #include <stdio.h> |
175 | | |
176 | | static int key_schedule(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_aead_context_t **ctx, int is_enc, |
177 | | const void *shared_secret, ptls_iovec_t info) |
178 | 0 | { |
179 | 0 | ptls_buffer_t key_schedule_context; |
180 | 0 | uint8_t key_schedule_context_smallbuf[128], secret[PTLS_MAX_DIGEST_SIZE], key[PTLS_MAX_SECRET_SIZE], |
181 | 0 | base_nonce[PTLS_MAX_IV_SIZE]; |
182 | 0 | int ret; |
183 | |
|
184 | 0 | *ctx = NULL; |
185 | |
|
186 | 0 | ptls_buffer_init(&key_schedule_context, key_schedule_context_smallbuf, sizeof(key_schedule_context_smallbuf)); |
187 | | |
188 | | /* key_schedule_context = concat(mode, LabeledExtract("", "psk_id_hash", psk_id), LabeledExtract("", "info_hash", info)) */ |
189 | 0 | ptls_buffer_push(&key_schedule_context, PTLS_HPKE_MODE_BASE); |
190 | 0 | if ((ret = ptls_buffer_reserve(&key_schedule_context, cipher->hash->digest_size)) != 0 || |
191 | 0 | (ret = labeled_extract(kem, cipher, key_schedule_context.base + key_schedule_context.off, ptls_iovec_init(NULL, 0), |
192 | 0 | "psk_id_hash", ptls_iovec_init(NULL, 0))) != 0) |
193 | 0 | goto Exit; |
194 | 0 | key_schedule_context.off += cipher->hash->digest_size; |
195 | 0 | if ((ret = ptls_buffer_reserve(&key_schedule_context, cipher->hash->digest_size)) != 0 || |
196 | 0 | (ret = labeled_extract(kem, cipher, key_schedule_context.base + key_schedule_context.off, ptls_iovec_init(NULL, 0), |
197 | 0 | "info_hash", info)) != 0) |
198 | 0 | goto Exit; |
199 | 0 | key_schedule_context.off += cipher->hash->digest_size; |
200 | | |
201 | | /* secret = LabeledExtract(shared_secret, "secret", psk) */ |
202 | 0 | if ((ret = labeled_extract(kem, cipher, secret, ptls_iovec_init(shared_secret, kem->hash->digest_size), "secret", |
203 | 0 | ptls_iovec_init("", 0))) != 0) |
204 | 0 | goto Exit; |
205 | | |
206 | | /* key, base_nonce */ |
207 | 0 | if ((ret = labeled_expand(kem, cipher, key, cipher->aead->key_size, ptls_iovec_init(secret, cipher->hash->digest_size), "key", |
208 | 0 | ptls_iovec_init(key_schedule_context.base, key_schedule_context.off))) != 0) |
209 | 0 | goto Exit; |
210 | 0 | if ((ret = labeled_expand(kem, cipher, base_nonce, cipher->aead->iv_size, ptls_iovec_init(secret, cipher->hash->digest_size), |
211 | 0 | "base_nonce", ptls_iovec_init(key_schedule_context.base, key_schedule_context.off))) != 0) |
212 | 0 | goto Exit; |
213 | | |
214 | 0 | *ctx = ptls_aead_new_direct(cipher->aead, is_enc, key, base_nonce); |
215 | |
|
216 | 0 | Exit: |
217 | 0 | ptls_buffer_dispose(&key_schedule_context); |
218 | 0 | ptls_clear_memory(secret, sizeof(secret)); |
219 | 0 | ptls_clear_memory(key, sizeof(key)); |
220 | 0 | ptls_clear_memory(base_nonce, sizeof(base_nonce)); |
221 | 0 | return ret; |
222 | 0 | } |
223 | | |
224 | | int ptls_hpke_setup_base_s(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_iovec_t *pk_s, ptls_aead_context_t **ctx, |
225 | | ptls_iovec_t pk_r, ptls_iovec_t info) |
226 | 0 | { |
227 | 0 | uint8_t secret[PTLS_MAX_DIGEST_SIZE]; |
228 | 0 | int ret; |
229 | |
|
230 | 0 | *pk_s = ptls_iovec_init(NULL, 0); |
231 | |
|
232 | 0 | if ((ret = dh_encap(kem, secret, pk_s, pk_r)) != 0) |
233 | 0 | goto Exit; |
234 | | |
235 | 0 | if ((ret = key_schedule(kem, cipher, ctx, 1, secret, info)) != 0) |
236 | 0 | goto Exit; |
237 | | |
238 | 0 | Exit: |
239 | 0 | if (ret != 0 && pk_s->len != 0) { |
240 | 0 | ptls_clear_memory(pk_s->base, pk_s->len); |
241 | 0 | free(pk_s->base); |
242 | 0 | *pk_s = ptls_iovec_init(NULL, 0); |
243 | 0 | } |
244 | 0 | ptls_clear_memory(secret, sizeof(secret)); |
245 | 0 | return ret; |
246 | 0 | } |
247 | | |
248 | | int ptls_hpke_setup_base_r(ptls_hpke_kem_t *kem, ptls_hpke_cipher_suite_t *cipher, ptls_key_exchange_context_t *keyex, |
249 | | ptls_aead_context_t **ctx, ptls_iovec_t pk_s, ptls_iovec_t info) |
250 | 0 | { |
251 | 0 | uint8_t secret[PTLS_MAX_DIGEST_SIZE]; |
252 | 0 | int ret; |
253 | |
|
254 | 0 | if ((ret = dh_decap(kem, secret, keyex, pk_s, keyex->pubkey)) != 0) |
255 | 0 | goto Exit; |
256 | | |
257 | 0 | if ((ret = key_schedule(kem, cipher, ctx, 0, secret, info)) != 0) |
258 | 0 | goto Exit; |
259 | | |
260 | 0 | Exit: |
261 | 0 | ptls_clear_memory(secret, sizeof(secret)); |
262 | 0 | return ret; |
263 | 0 | } |