/src/openssl/crypto/hpke/hpke.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* An OpenSSL-based HPKE implementation of RFC9180 */ |
11 | | |
12 | | #include <string.h> |
13 | | #include <openssl/rand.h> |
14 | | #include <openssl/kdf.h> |
15 | | #include <openssl/core_names.h> |
16 | | #include <openssl/hpke.h> |
17 | | #include <openssl/sha.h> |
18 | | #include <openssl/evp.h> |
19 | | #include <openssl/err.h> |
20 | | #include "internal/hpke_util.h" |
21 | | #include "internal/nelem.h" |
22 | | #include "internal/common.h" |
23 | | |
24 | | /* default buffer size for keys and internal buffers we use */ |
25 | 0 | #define OSSL_HPKE_MAXSIZE 512 |
26 | | |
27 | | /* Define HPKE labels from RFC9180 in hex for EBCDIC compatibility */ |
28 | | /* "HPKE" - "suite_id" label for section 5.1 */ |
29 | | static const char OSSL_HPKE_SEC51LABEL[] = "\x48\x50\x4b\x45"; |
30 | | /* "psk_id_hash" - in key_schedule_context */ |
31 | | static const char OSSL_HPKE_PSKIDHASH_LABEL[] = "\x70\x73\x6b\x5f\x69\x64\x5f\x68\x61\x73\x68"; |
32 | | /* "info_hash" - in key_schedule_context */ |
33 | | static const char OSSL_HPKE_INFOHASH_LABEL[] = "\x69\x6e\x66\x6f\x5f\x68\x61\x73\x68"; |
34 | | /* "base_nonce" - base nonce calc label */ |
35 | | static const char OSSL_HPKE_NONCE_LABEL[] = "\x62\x61\x73\x65\x5f\x6e\x6f\x6e\x63\x65"; |
36 | | /* "exp" - internal exporter secret generation label */ |
37 | | static const char OSSL_HPKE_EXP_LABEL[] = "\x65\x78\x70"; |
38 | | /* "sec" - external label for exporting secret */ |
39 | | static const char OSSL_HPKE_EXP_SEC_LABEL[] = "\x73\x65\x63"; |
40 | | /* "key" - label for use when generating key from shared secret */ |
41 | | static const char OSSL_HPKE_KEY_LABEL[] = "\x6b\x65\x79"; |
42 | | /* "secret" - for generating shared secret */ |
43 | | static const char OSSL_HPKE_SECRET_LABEL[] = "\x73\x65\x63\x72\x65\x74"; |
44 | | |
45 | | /** |
46 | | * @brief sender or receiver context |
47 | | */ |
48 | | struct ossl_hpke_ctx_st { |
49 | | OSSL_LIB_CTX *libctx; /* library context */ |
50 | | char *propq; /* properties */ |
51 | | int mode; /* HPKE mode */ |
52 | | OSSL_HPKE_SUITE suite; /* suite */ |
53 | | const OSSL_HPKE_KEM_INFO *kem_info; |
54 | | const OSSL_HPKE_KDF_INFO *kdf_info; |
55 | | const OSSL_HPKE_AEAD_INFO *aead_info; |
56 | | EVP_CIPHER *aead_ciph; |
57 | | int role; /* sender(0) or receiver(1) */ |
58 | | uint64_t seq; /* aead sequence number */ |
59 | | unsigned char *shared_secret; /* KEM output, zz */ |
60 | | size_t shared_secretlen; |
61 | | unsigned char *key; /* final aead key */ |
62 | | size_t keylen; |
63 | | unsigned char *nonce; /* aead base nonce */ |
64 | | size_t noncelen; |
65 | | unsigned char *exportersec; /* exporter secret */ |
66 | | size_t exporterseclen; |
67 | | char *pskid; /* PSK stuff */ |
68 | | unsigned char *psk; |
69 | | size_t psklen; |
70 | | EVP_PKEY *authpriv; /* sender's authentication private key */ |
71 | | unsigned char *authpub; /* auth public key */ |
72 | | size_t authpublen; |
73 | | unsigned char *ikme; /* IKM for sender deterministic key gen */ |
74 | | size_t ikmelen; |
75 | | }; |
76 | | |
77 | | /** |
78 | | * @brief check if KEM uses NIST curve or not |
79 | | * @param kem_id is the externally supplied kem_id |
80 | | * @return 1 for NIST curves, 0 for other |
81 | | */ |
82 | | static int hpke_kem_id_nist_curve(uint16_t kem_id) |
83 | 0 | { |
84 | 0 | const OSSL_HPKE_KEM_INFO *kem_info; |
85 | |
|
86 | 0 | kem_info = ossl_HPKE_KEM_INFO_find_id(kem_id); |
87 | 0 | return kem_info != NULL && kem_info->groupname != NULL; |
88 | 0 | } |
89 | | |
90 | | /** |
91 | | * @brief wrapper to import NIST curve public key as easily as x25519/x448 |
92 | | * @param libctx is the context to use |
93 | | * @param propq is a properties string |
94 | | * @param gname is the curve groupname |
95 | | * @param buf is the binary buffer with the (uncompressed) public value |
96 | | * @param buflen is the length of the private key buffer |
97 | | * @return a working EVP_PKEY * or NULL |
98 | | * |
99 | | * Note that this could be a useful function to make public in |
100 | | * future, but would likely require a name change. |
101 | | */ |
102 | | static EVP_PKEY *evp_pkey_new_raw_nist_public_key(OSSL_LIB_CTX *libctx, |
103 | | const char *propq, |
104 | | const char *gname, |
105 | | const unsigned char *buf, |
106 | | size_t buflen) |
107 | 0 | { |
108 | 0 | OSSL_PARAM params[2]; |
109 | 0 | EVP_PKEY *ret = NULL; |
110 | 0 | EVP_PKEY_CTX *cctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq); |
111 | |
|
112 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
113 | 0 | (char *)gname, 0); |
114 | 0 | params[1] = OSSL_PARAM_construct_end(); |
115 | 0 | if (cctx == NULL |
116 | 0 | || EVP_PKEY_paramgen_init(cctx) <= 0 |
117 | 0 | || EVP_PKEY_CTX_set_params(cctx, params) <= 0 |
118 | 0 | || EVP_PKEY_paramgen(cctx, &ret) <= 0 |
119 | 0 | || EVP_PKEY_set1_encoded_public_key(ret, buf, buflen) != 1) { |
120 | 0 | EVP_PKEY_CTX_free(cctx); |
121 | 0 | EVP_PKEY_free(ret); |
122 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
123 | 0 | return NULL; |
124 | 0 | } |
125 | 0 | EVP_PKEY_CTX_free(cctx); |
126 | 0 | return ret; |
127 | 0 | } |
128 | | |
129 | | /** |
130 | | * @brief do the AEAD decryption |
131 | | * @param hctx is the context to use |
132 | | * @param iv is the initialisation vector |
133 | | * @param aad is the additional authenticated data |
134 | | * @param aadlen is the length of the aad |
135 | | * @param ct is the ciphertext buffer |
136 | | * @param ctlen is the ciphertext length (including tag). |
137 | | * @param pt is the output buffer |
138 | | * @param ptlen input/output, better be big enough on input, exact on output |
139 | | * @return 1 on success, 0 otherwise |
140 | | */ |
141 | | static int hpke_aead_dec(OSSL_HPKE_CTX *hctx, const unsigned char *iv, |
142 | | const unsigned char *aad, size_t aadlen, |
143 | | const unsigned char *ct, size_t ctlen, |
144 | | unsigned char *pt, size_t *ptlen) |
145 | 0 | { |
146 | 0 | int erv = 0; |
147 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
148 | 0 | int len = 0; |
149 | 0 | size_t taglen; |
150 | |
|
151 | 0 | taglen = hctx->aead_info->taglen; |
152 | 0 | if (ctlen < taglen || *ptlen < ctlen - taglen |
153 | 0 | || aadlen > INT_MAX || ctlen > INT_MAX) { |
154 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
155 | 0 | return 0; |
156 | 0 | } |
157 | | /* Create and initialise the context */ |
158 | 0 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) |
159 | 0 | return 0; |
160 | | /* Initialise the decryption operation. */ |
161 | 0 | if (EVP_DecryptInit_ex(ctx, hctx->aead_ciph, NULL, NULL, NULL) != 1) { |
162 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
163 | 0 | goto err; |
164 | 0 | } |
165 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, |
166 | 0 | (int)hctx->noncelen, NULL) |
167 | 0 | != 1) { |
168 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
169 | 0 | goto err; |
170 | 0 | } |
171 | | /* Initialise key and IV */ |
172 | 0 | if (EVP_DecryptInit_ex(ctx, NULL, NULL, hctx->key, iv) != 1) { |
173 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
174 | 0 | goto err; |
175 | 0 | } |
176 | | /* Provide AAD. */ |
177 | 0 | if (aadlen != 0 && aad != NULL) { |
178 | 0 | if (EVP_DecryptUpdate(ctx, NULL, &len, aad, (int)aadlen) != 1) { |
179 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
180 | 0 | goto err; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | if (ctlen > taglen) { |
184 | 0 | if (EVP_DecryptUpdate(ctx, pt, &len, ct, (int)(ctlen - taglen)) != 1) { |
185 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
186 | 0 | goto err; |
187 | 0 | } |
188 | 0 | } else { |
189 | 0 | len = 0; |
190 | 0 | } |
191 | 0 | *ptlen = len; |
192 | 0 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, |
193 | 0 | (int)taglen, (void *)(ct + ctlen - taglen))) { |
194 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
195 | 0 | goto err; |
196 | 0 | } |
197 | | /* |
198 | | * Finalise decryption, which checks the tag. Ideally, we would simply write |
199 | | * pt + len here, but the caller may have passed a zero-length output buffer |
200 | | * if ctlen == taglen. pt may be NULL for a zero-length buffer. However, |
201 | | * before C2y, NULL + 0 was undefined. While this will be fixed and applied |
202 | | * to old C versions retroactively, we support old compilers and sanitizers. |
203 | | * https://developers.redhat.com/articles/2024/12/11/making-memcpynull-null-0-well-defined |
204 | | */ |
205 | 0 | if (EVP_DecryptFinal_ex(ctx, len ? pt + len : pt, &len) <= 0) { |
206 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
207 | 0 | goto err; |
208 | 0 | } |
209 | 0 | *ptlen += len; |
210 | 0 | erv = 1; |
211 | |
|
212 | 0 | err: |
213 | 0 | if (erv != 1) |
214 | 0 | OPENSSL_cleanse(pt, *ptlen); |
215 | 0 | EVP_CIPHER_CTX_free(ctx); |
216 | 0 | return erv; |
217 | 0 | } |
218 | | |
219 | | /** |
220 | | * @brief do AEAD encryption as per the RFC |
221 | | * @param hctx is the context to use |
222 | | * @param iv is the initialisation vector |
223 | | * @param aad is the additional authenticated data |
224 | | * @param aadlen is the length of the aad |
225 | | * @param pt is the plaintext buffer |
226 | | * @param ptlen is the length of pt |
227 | | * @param ct is the output buffer |
228 | | * @param ctlen input/output, needs space for tag on input, exact on output |
229 | | * @return 1 for success, 0 otherwise |
230 | | */ |
231 | | static int hpke_aead_enc(OSSL_HPKE_CTX *hctx, const unsigned char *iv, |
232 | | const unsigned char *aad, size_t aadlen, |
233 | | const unsigned char *pt, size_t ptlen, |
234 | | unsigned char *ct, size_t *ctlen) |
235 | 0 | { |
236 | 0 | int erv = 0; |
237 | 0 | EVP_CIPHER_CTX *ctx = NULL; |
238 | 0 | int len; |
239 | 0 | size_t taglen = 0; |
240 | 0 | unsigned char tag[EVP_MAX_AEAD_TAG_LENGTH]; |
241 | |
|
242 | 0 | taglen = hctx->aead_info->taglen; |
243 | 0 | if (*ctlen < taglen || ptlen > *ctlen - taglen |
244 | 0 | || aadlen > INT_MAX || ptlen > INT_MAX) { |
245 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
246 | 0 | return 0; |
247 | 0 | } |
248 | 0 | if (!ossl_assert(taglen <= sizeof(tag))) { |
249 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
250 | 0 | return 0; |
251 | 0 | } |
252 | | /* Create and initialise the context */ |
253 | 0 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) |
254 | 0 | return 0; |
255 | | /* Initialise the encryption operation. */ |
256 | 0 | if (EVP_EncryptInit_ex(ctx, hctx->aead_ciph, NULL, NULL, NULL) != 1) { |
257 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, |
261 | 0 | (int)hctx->noncelen, NULL) |
262 | 0 | != 1) { |
263 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
264 | 0 | goto err; |
265 | 0 | } |
266 | | /* Initialise key and IV */ |
267 | 0 | if (EVP_EncryptInit_ex(ctx, NULL, NULL, hctx->key, iv) != 1) { |
268 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
269 | 0 | goto err; |
270 | 0 | } |
271 | | /* Provide any AAD data. */ |
272 | 0 | if (aadlen != 0 && aad != NULL) { |
273 | 0 | if (EVP_EncryptUpdate(ctx, NULL, &len, aad, (int)aadlen) != 1) { |
274 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
275 | 0 | goto err; |
276 | 0 | } |
277 | 0 | } |
278 | 0 | if (ptlen != 0) { |
279 | 0 | if (EVP_EncryptUpdate(ctx, ct, &len, pt, (int)ptlen) != 1) { |
280 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
281 | 0 | goto err; |
282 | 0 | } |
283 | 0 | } else { |
284 | 0 | len = 0; |
285 | 0 | } |
286 | 0 | *ctlen = len; |
287 | | /* Finalise the encryption. */ |
288 | 0 | if (EVP_EncryptFinal_ex(ctx, ct + len, &len) != 1) { |
289 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
290 | 0 | goto err; |
291 | 0 | } |
292 | 0 | *ctlen += len; |
293 | | /* Get tag. Not a duplicate so needs to be added to the ciphertext */ |
294 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, (int)taglen, tag) != 1) { |
295 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
296 | 0 | goto err; |
297 | 0 | } |
298 | 0 | memcpy(ct + *ctlen, tag, taglen); |
299 | 0 | *ctlen += taglen; |
300 | 0 | erv = 1; |
301 | |
|
302 | 0 | err: |
303 | 0 | if (erv != 1) |
304 | 0 | OPENSSL_cleanse(ct, *ctlen); |
305 | 0 | EVP_CIPHER_CTX_free(ctx); |
306 | 0 | return erv; |
307 | 0 | } |
308 | | |
309 | | /** |
310 | | * @brief check mode is in-range and supported |
311 | | * @param mode is the caller's chosen mode |
312 | | * @return 1 for good mode, 0 otherwise |
313 | | */ |
314 | | static int hpke_mode_check(unsigned int mode) |
315 | 0 | { |
316 | 0 | switch (mode) { |
317 | 0 | case OSSL_HPKE_MODE_BASE: |
318 | 0 | case OSSL_HPKE_MODE_PSK: |
319 | 0 | case OSSL_HPKE_MODE_AUTH: |
320 | 0 | case OSSL_HPKE_MODE_PSKAUTH: |
321 | 0 | break; |
322 | 0 | default: |
323 | 0 | return 0; |
324 | 0 | } |
325 | 0 | return 1; |
326 | 0 | } |
327 | | |
328 | | /** |
329 | | * @brief check if a suite is supported locally |
330 | | * @param suite is the suite to check |
331 | | * @return 1 for good, 0 otherwise |
332 | | */ |
333 | | static int hpke_suite_check(OSSL_HPKE_SUITE suite, |
334 | | const OSSL_HPKE_KEM_INFO **kem_info, |
335 | | const OSSL_HPKE_KDF_INFO **kdf_info, |
336 | | const OSSL_HPKE_AEAD_INFO **aead_info) |
337 | 0 | { |
338 | 0 | const OSSL_HPKE_KEM_INFO *kem_info_; |
339 | 0 | const OSSL_HPKE_KDF_INFO *kdf_info_; |
340 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info_; |
341 | | |
342 | | /* check KEM, KDF and AEAD are supported here */ |
343 | 0 | if ((kem_info_ = ossl_HPKE_KEM_INFO_find_id(suite.kem_id)) == NULL) |
344 | 0 | return 0; |
345 | 0 | if ((kdf_info_ = ossl_HPKE_KDF_INFO_find_id(suite.kdf_id)) == NULL) |
346 | 0 | return 0; |
347 | 0 | if ((aead_info_ = ossl_HPKE_AEAD_INFO_find_id(suite.aead_id)) == NULL) |
348 | 0 | return 0; |
349 | | |
350 | 0 | if (kem_info != NULL) |
351 | 0 | *kem_info = kem_info_; |
352 | 0 | if (kdf_info != NULL) |
353 | 0 | *kdf_info = kdf_info_; |
354 | 0 | if (aead_info != NULL) |
355 | 0 | *aead_info = aead_info_; |
356 | |
|
357 | 0 | return 1; |
358 | 0 | } |
359 | | |
360 | | /* |
361 | | * @brief randomly pick a suite |
362 | | * @param libctx is the context to use |
363 | | * @param propq is a properties string |
364 | | * @param suite is the result |
365 | | * @return 1 for success, 0 otherwise |
366 | | */ |
367 | | static int hpke_random_suite(OSSL_LIB_CTX *libctx, |
368 | | const char *propq, |
369 | | OSSL_HPKE_SUITE *suite) |
370 | 0 | { |
371 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
372 | 0 | const OSSL_HPKE_KDF_INFO *kdf_info = NULL; |
373 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info = NULL; |
374 | | |
375 | | /* random kem, kdf and aead */ |
376 | 0 | kem_info = ossl_HPKE_KEM_INFO_find_random(libctx); |
377 | 0 | if (kem_info == NULL) |
378 | 0 | return 0; |
379 | 0 | suite->kem_id = kem_info->kem_id; |
380 | 0 | kdf_info = ossl_HPKE_KDF_INFO_find_random(libctx); |
381 | 0 | if (kdf_info == NULL) |
382 | 0 | return 0; |
383 | 0 | suite->kdf_id = kdf_info->kdf_id; |
384 | 0 | aead_info = ossl_HPKE_AEAD_INFO_find_random(libctx); |
385 | 0 | if (aead_info == NULL) |
386 | 0 | return 0; |
387 | 0 | suite->aead_id = aead_info->aead_id; |
388 | 0 | return 1; |
389 | 0 | } |
390 | | |
391 | | /* |
392 | | * @brief tell the caller how big the ciphertext will be |
393 | | * |
394 | | * AEAD algorithms add a tag for data authentication. |
395 | | * Those are almost always, but not always, 16 octets |
396 | | * long, and who knows what will be true in the future. |
397 | | * So this function allows a caller to find out how |
398 | | * much data expansion they will see with a given suite. |
399 | | * |
400 | | * "enc" is the name used in RFC9180 for the encapsulated |
401 | | * public value of the sender, who calls OSSL_HPKE_seal(), |
402 | | * that is sent to the recipient, who calls OSSL_HPKE_open(). |
403 | | * |
404 | | * @param suite is the suite to be used |
405 | | * @param enclen points to what will be enc length |
406 | | * @param clearlen is the length of plaintext |
407 | | * @param cipherlen points to what will be ciphertext length (including tag) |
408 | | * @return 1 for success, 0 otherwise |
409 | | */ |
410 | | static int hpke_expansion(OSSL_HPKE_SUITE suite, |
411 | | size_t *enclen, |
412 | | size_t clearlen, |
413 | | size_t *cipherlen) |
414 | 0 | { |
415 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info = NULL; |
416 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
417 | |
|
418 | 0 | if (cipherlen == NULL || enclen == NULL) { |
419 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | if (hpke_suite_check(suite, &kem_info, NULL, &aead_info) != 1) { |
423 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
424 | 0 | return 0; |
425 | 0 | } |
426 | 0 | *cipherlen = clearlen + aead_info->taglen; |
427 | 0 | *enclen = kem_info->Nenc; |
428 | 0 | return 1; |
429 | 0 | } |
430 | | |
431 | | /* |
432 | | * @brief expand and XOR the 64-bit unsigned seq with (nonce) buffer |
433 | | * @param ctx is the HPKE context |
434 | | * @param buf is the buffer for the XOR'd seq and nonce |
435 | | * @param blen is the size of buf |
436 | | * @return 0 for error, otherwise blen |
437 | | */ |
438 | | static size_t hpke_seqnonce2buf(OSSL_HPKE_CTX *ctx, |
439 | | unsigned char *buf, size_t blen) |
440 | 0 | { |
441 | 0 | size_t i; |
442 | 0 | uint64_t seq_copy; |
443 | |
|
444 | 0 | if (ctx == NULL || blen < sizeof(seq_copy) || blen != ctx->noncelen) |
445 | 0 | return 0; |
446 | 0 | seq_copy = ctx->seq; |
447 | 0 | memset(buf, 0, blen); |
448 | 0 | for (i = 0; i < sizeof(seq_copy); i++) { |
449 | 0 | buf[blen - i - 1] = seq_copy & 0xff; |
450 | 0 | seq_copy >>= 8; |
451 | 0 | } |
452 | 0 | for (i = 0; i < blen; i++) |
453 | 0 | buf[i] ^= ctx->nonce[i]; |
454 | 0 | return blen; |
455 | 0 | } |
456 | | |
457 | | /* |
458 | | * @brief call the underlying KEM to encap |
459 | | * @param ctx is the OSSL_HPKE_CTX |
460 | | * @param enc is a buffer for the sender's ephemeral public value |
461 | | * @param enclen is the size of enc on input, number of octets used on output |
462 | | * @param pub is the recipient's public value |
463 | | * @param publen is the length of pub |
464 | | * @return 1 for success, 0 for error |
465 | | */ |
466 | | static int hpke_encap(OSSL_HPKE_CTX *ctx, unsigned char *enc, size_t *enclen, |
467 | | const unsigned char *pub, size_t publen) |
468 | 0 | { |
469 | 0 | int erv = 0; |
470 | 0 | OSSL_PARAM params[3], *p = params; |
471 | 0 | size_t lsslen = 0, lenclen = 0; |
472 | 0 | EVP_PKEY_CTX *pctx = NULL; |
473 | 0 | EVP_PKEY *pkR = NULL; |
474 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
475 | |
|
476 | 0 | if (ctx == NULL || enc == NULL || enclen == NULL || *enclen == 0 |
477 | 0 | || pub == NULL || publen == 0) { |
478 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
479 | 0 | return 0; |
480 | 0 | } |
481 | 0 | if (ctx->shared_secret != NULL) { |
482 | | /* only run the KEM once per OSSL_HPKE_CTX */ |
483 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
484 | 0 | return 0; |
485 | 0 | } |
486 | 0 | kem_info = ossl_HPKE_KEM_INFO_find_id(ctx->suite.kem_id); |
487 | 0 | if (kem_info == NULL) { |
488 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 | if (hpke_kem_id_nist_curve(ctx->suite.kem_id) == 1) { |
492 | 0 | pkR = evp_pkey_new_raw_nist_public_key(ctx->libctx, ctx->propq, |
493 | 0 | kem_info->groupname, |
494 | 0 | pub, publen); |
495 | 0 | } else { |
496 | 0 | pkR = EVP_PKEY_new_raw_public_key_ex(ctx->libctx, |
497 | 0 | kem_info->keytype, |
498 | 0 | ctx->propq, pub, publen); |
499 | 0 | } |
500 | 0 | if (pkR == NULL) { |
501 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
502 | 0 | goto err; |
503 | 0 | } |
504 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, pkR, ctx->propq); |
505 | 0 | if (pctx == NULL) { |
506 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
507 | 0 | goto err; |
508 | 0 | } |
509 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, |
510 | 0 | OSSL_KEM_PARAM_OPERATION_DHKEM, |
511 | 0 | 0); |
512 | 0 | if (ctx->ikme != NULL) { |
513 | 0 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KEM_PARAM_IKME, |
514 | 0 | ctx->ikme, ctx->ikmelen); |
515 | 0 | } |
516 | 0 | *p = OSSL_PARAM_construct_end(); |
517 | 0 | if (ctx->mode == OSSL_HPKE_MODE_AUTH |
518 | 0 | || ctx->mode == OSSL_HPKE_MODE_PSKAUTH) { |
519 | 0 | if (EVP_PKEY_auth_encapsulate_init(pctx, ctx->authpriv, |
520 | 0 | params) |
521 | 0 | != 1) { |
522 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
523 | 0 | goto err; |
524 | 0 | } |
525 | 0 | } else { |
526 | 0 | if (EVP_PKEY_encapsulate_init(pctx, params) != 1) { |
527 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
528 | 0 | goto err; |
529 | 0 | } |
530 | 0 | } |
531 | 0 | lenclen = *enclen; |
532 | 0 | if (EVP_PKEY_encapsulate(pctx, NULL, &lenclen, NULL, &lsslen) != 1) { |
533 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
534 | 0 | goto err; |
535 | 0 | } |
536 | 0 | if (lenclen > *enclen) { |
537 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
538 | 0 | goto err; |
539 | 0 | } |
540 | 0 | ctx->shared_secret = OPENSSL_malloc(lsslen); |
541 | 0 | if (ctx->shared_secret == NULL) |
542 | 0 | goto err; |
543 | 0 | ctx->shared_secretlen = lsslen; |
544 | 0 | if (EVP_PKEY_encapsulate(pctx, enc, enclen, ctx->shared_secret, |
545 | 0 | &ctx->shared_secretlen) |
546 | 0 | != 1) { |
547 | 0 | ctx->shared_secretlen = 0; |
548 | 0 | OPENSSL_free(ctx->shared_secret); |
549 | 0 | ctx->shared_secret = NULL; |
550 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
551 | 0 | goto err; |
552 | 0 | } |
553 | 0 | erv = 1; |
554 | |
|
555 | 0 | err: |
556 | 0 | EVP_PKEY_CTX_free(pctx); |
557 | 0 | EVP_PKEY_free(pkR); |
558 | 0 | return erv; |
559 | 0 | } |
560 | | |
561 | | /* |
562 | | * @brief call the underlying KEM to decap |
563 | | * @param ctx is the OSSL_HPKE_CTX |
564 | | * @param enc is a buffer for the sender's ephemeral public value |
565 | | * @param enclen is the length of enc |
566 | | * @param priv is the recipient's private value |
567 | | * @return 1 for success, 0 for error |
568 | | */ |
569 | | static int hpke_decap(OSSL_HPKE_CTX *ctx, |
570 | | const unsigned char *enc, size_t enclen, |
571 | | EVP_PKEY *priv) |
572 | 0 | { |
573 | 0 | int erv = 0; |
574 | 0 | EVP_PKEY_CTX *pctx = NULL; |
575 | 0 | EVP_PKEY *spub = NULL; |
576 | 0 | OSSL_PARAM params[2], *p = params; |
577 | 0 | size_t lsslen = 0; |
578 | |
|
579 | 0 | if (ctx == NULL || enc == NULL || enclen == 0 || priv == NULL) { |
580 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
581 | 0 | return 0; |
582 | 0 | } |
583 | 0 | if (ctx->shared_secret != NULL) { |
584 | | /* only run the KEM once per OSSL_HPKE_CTX */ |
585 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
586 | 0 | return 0; |
587 | 0 | } |
588 | 0 | pctx = EVP_PKEY_CTX_new_from_pkey(ctx->libctx, priv, ctx->propq); |
589 | 0 | if (pctx == NULL) { |
590 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
591 | 0 | goto err; |
592 | 0 | } |
593 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, |
594 | 0 | OSSL_KEM_PARAM_OPERATION_DHKEM, |
595 | 0 | 0); |
596 | 0 | *p = OSSL_PARAM_construct_end(); |
597 | 0 | if (ctx->mode == OSSL_HPKE_MODE_AUTH |
598 | 0 | || ctx->mode == OSSL_HPKE_MODE_PSKAUTH) { |
599 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
600 | |
|
601 | 0 | kem_info = ossl_HPKE_KEM_INFO_find_id(ctx->suite.kem_id); |
602 | 0 | if (kem_info == NULL) { |
603 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
604 | 0 | goto err; |
605 | 0 | } |
606 | 0 | if (hpke_kem_id_nist_curve(ctx->suite.kem_id) == 1) { |
607 | 0 | spub = evp_pkey_new_raw_nist_public_key(ctx->libctx, ctx->propq, |
608 | 0 | kem_info->groupname, |
609 | 0 | ctx->authpub, |
610 | 0 | ctx->authpublen); |
611 | 0 | } else { |
612 | 0 | spub = EVP_PKEY_new_raw_public_key_ex(ctx->libctx, |
613 | 0 | kem_info->keytype, |
614 | 0 | ctx->propq, |
615 | 0 | ctx->authpub, |
616 | 0 | ctx->authpublen); |
617 | 0 | } |
618 | 0 | if (spub == NULL) { |
619 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
620 | 0 | goto err; |
621 | 0 | } |
622 | 0 | if (EVP_PKEY_auth_decapsulate_init(pctx, spub, params) != 1) { |
623 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
624 | 0 | goto err; |
625 | 0 | } |
626 | 0 | } else { |
627 | 0 | if (EVP_PKEY_decapsulate_init(pctx, params) != 1) { |
628 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
629 | 0 | goto err; |
630 | 0 | } |
631 | 0 | } |
632 | 0 | if (EVP_PKEY_decapsulate(pctx, NULL, &lsslen, enc, enclen) != 1) { |
633 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
634 | 0 | goto err; |
635 | 0 | } |
636 | 0 | ctx->shared_secret = OPENSSL_malloc(lsslen); |
637 | 0 | if (ctx->shared_secret == NULL) |
638 | 0 | goto err; |
639 | 0 | if (EVP_PKEY_decapsulate(pctx, ctx->shared_secret, &lsslen, |
640 | 0 | enc, enclen) |
641 | 0 | != 1) { |
642 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
643 | 0 | goto err; |
644 | 0 | } |
645 | 0 | ctx->shared_secretlen = lsslen; |
646 | 0 | erv = 1; |
647 | |
|
648 | 0 | err: |
649 | 0 | EVP_PKEY_CTX_free(pctx); |
650 | 0 | EVP_PKEY_free(spub); |
651 | 0 | if (erv == 0) { |
652 | 0 | OPENSSL_free(ctx->shared_secret); |
653 | 0 | ctx->shared_secret = NULL; |
654 | 0 | ctx->shared_secretlen = 0; |
655 | 0 | } |
656 | 0 | return erv; |
657 | 0 | } |
658 | | |
659 | | /* |
660 | | * @brief do "middle" of HPKE, between KEM and AEAD |
661 | | * @param ctx is the OSSL_HPKE_CTX |
662 | | * @param info is a buffer for the added binding information |
663 | | * @param infolen is the length of info |
664 | | * @return 0 for error, 1 for success |
665 | | * |
666 | | * This does all the HPKE extracts and expands as defined in RFC9180 |
667 | | * section 5.1, (badly termed there as a "key schedule") and sets the |
668 | | * ctx fields for the shared_secret, nonce, key and exporter_secret |
669 | | */ |
670 | | static int hpke_do_middle(OSSL_HPKE_CTX *ctx, |
671 | | const unsigned char *info, size_t infolen) |
672 | 0 | { |
673 | 0 | int erv = 0; |
674 | 0 | size_t ks_contextlen = OSSL_HPKE_MAXSIZE; |
675 | 0 | unsigned char ks_context[OSSL_HPKE_MAXSIZE]; |
676 | 0 | size_t halflen = 0; |
677 | 0 | size_t pskidlen = 0; |
678 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info = NULL; |
679 | 0 | const OSSL_HPKE_KDF_INFO *kdf_info = NULL; |
680 | 0 | size_t secretlen = OSSL_HPKE_MAXSIZE; |
681 | 0 | unsigned char secret[OSSL_HPKE_MAXSIZE]; |
682 | 0 | EVP_KDF_CTX *kctx = NULL; |
683 | 0 | unsigned char suitebuf[6]; |
684 | 0 | const char *mdname = NULL; |
685 | | |
686 | | /* only let this be done once */ |
687 | 0 | if (ctx->exportersec != NULL) { |
688 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
689 | 0 | return 0; |
690 | 0 | } |
691 | 0 | if (ossl_HPKE_KEM_INFO_find_id(ctx->suite.kem_id) == NULL) { |
692 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
693 | 0 | return 0; |
694 | 0 | } |
695 | 0 | aead_info = ossl_HPKE_AEAD_INFO_find_id(ctx->suite.aead_id); |
696 | 0 | if (aead_info == NULL) { |
697 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
698 | 0 | return 0; |
699 | 0 | } |
700 | 0 | kdf_info = ossl_HPKE_KDF_INFO_find_id(ctx->suite.kdf_id); |
701 | 0 | if (kdf_info == NULL) { |
702 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
703 | 0 | return 0; |
704 | 0 | } |
705 | 0 | mdname = kdf_info->mdname; |
706 | | /* create key schedule context */ |
707 | 0 | memset(ks_context, 0, sizeof(ks_context)); |
708 | 0 | ks_context[0] = (unsigned char)(ctx->mode % 256); |
709 | 0 | ks_contextlen--; /* remaining space */ |
710 | 0 | halflen = kdf_info->Nh; |
711 | 0 | if ((2 * halflen) > ks_contextlen) { |
712 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
713 | 0 | return 0; |
714 | 0 | } |
715 | | /* check a psk was set if in that mode */ |
716 | 0 | if (ctx->mode == OSSL_HPKE_MODE_PSK |
717 | 0 | || ctx->mode == OSSL_HPKE_MODE_PSKAUTH) { |
718 | 0 | if (ctx->psk == NULL || ctx->psklen == 0 || ctx->pskid == NULL) { |
719 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
720 | 0 | return 0; |
721 | 0 | } |
722 | 0 | } |
723 | 0 | kctx = ossl_kdf_ctx_create("HKDF", mdname, ctx->libctx, ctx->propq); |
724 | 0 | if (kctx == NULL) { |
725 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
726 | 0 | return 0; |
727 | 0 | } |
728 | 0 | pskidlen = (ctx->psk == NULL ? 0 : strlen(ctx->pskid)); |
729 | | /* full suite details as per RFC9180 sec 5.1 */ |
730 | 0 | suitebuf[0] = ctx->suite.kem_id / 256; |
731 | 0 | suitebuf[1] = ctx->suite.kem_id % 256; |
732 | 0 | suitebuf[2] = ctx->suite.kdf_id / 256; |
733 | 0 | suitebuf[3] = ctx->suite.kdf_id % 256; |
734 | 0 | suitebuf[4] = ctx->suite.aead_id / 256; |
735 | 0 | suitebuf[5] = ctx->suite.aead_id % 256; |
736 | | /* Extract and Expand variously... */ |
737 | 0 | if (ossl_hpke_labeled_extract(kctx, ks_context + 1, halflen, |
738 | 0 | NULL, 0, OSSL_HPKE_SEC51LABEL, |
739 | 0 | suitebuf, sizeof(suitebuf), |
740 | 0 | OSSL_HPKE_PSKIDHASH_LABEL, |
741 | 0 | (unsigned char *)ctx->pskid, pskidlen) |
742 | 0 | != 1) { |
743 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
744 | 0 | goto err; |
745 | 0 | } |
746 | 0 | if (ossl_hpke_labeled_extract(kctx, ks_context + 1 + halflen, halflen, |
747 | 0 | NULL, 0, OSSL_HPKE_SEC51LABEL, |
748 | 0 | suitebuf, sizeof(suitebuf), |
749 | 0 | OSSL_HPKE_INFOHASH_LABEL, |
750 | 0 | (unsigned char *)info, infolen) |
751 | 0 | != 1) { |
752 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
753 | 0 | goto err; |
754 | 0 | } |
755 | 0 | ks_contextlen = 1 + 2 * halflen; |
756 | 0 | secretlen = kdf_info->Nh; |
757 | 0 | if (secretlen > OSSL_HPKE_MAXSIZE) { |
758 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
759 | 0 | goto err; |
760 | 0 | } |
761 | 0 | if (ossl_hpke_labeled_extract(kctx, secret, secretlen, |
762 | 0 | ctx->shared_secret, ctx->shared_secretlen, |
763 | 0 | OSSL_HPKE_SEC51LABEL, |
764 | 0 | suitebuf, sizeof(suitebuf), |
765 | 0 | OSSL_HPKE_SECRET_LABEL, |
766 | 0 | ctx->psk, ctx->psklen) |
767 | 0 | != 1) { |
768 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
769 | 0 | goto err; |
770 | 0 | } |
771 | 0 | if (ctx->suite.aead_id != OSSL_HPKE_AEAD_ID_EXPORTONLY) { |
772 | | /* we only need nonce/key for non export AEADs */ |
773 | 0 | ctx->noncelen = aead_info->Nn; |
774 | 0 | ctx->nonce = OPENSSL_malloc(ctx->noncelen); |
775 | 0 | if (ctx->nonce == NULL) |
776 | 0 | goto err; |
777 | 0 | if (ossl_hpke_labeled_expand(kctx, ctx->nonce, ctx->noncelen, |
778 | 0 | secret, secretlen, OSSL_HPKE_SEC51LABEL, |
779 | 0 | suitebuf, sizeof(suitebuf), |
780 | 0 | OSSL_HPKE_NONCE_LABEL, |
781 | 0 | ks_context, ks_contextlen) |
782 | 0 | != 1) { |
783 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
784 | 0 | goto err; |
785 | 0 | } |
786 | 0 | ctx->keylen = aead_info->Nk; |
787 | 0 | ctx->key = OPENSSL_malloc(ctx->keylen); |
788 | 0 | if (ctx->key == NULL) |
789 | 0 | goto err; |
790 | 0 | if (ossl_hpke_labeled_expand(kctx, ctx->key, ctx->keylen, |
791 | 0 | secret, secretlen, OSSL_HPKE_SEC51LABEL, |
792 | 0 | suitebuf, sizeof(suitebuf), |
793 | 0 | OSSL_HPKE_KEY_LABEL, |
794 | 0 | ks_context, ks_contextlen) |
795 | 0 | != 1) { |
796 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
797 | 0 | goto err; |
798 | 0 | } |
799 | 0 | } |
800 | 0 | ctx->exporterseclen = kdf_info->Nh; |
801 | 0 | ctx->exportersec = OPENSSL_malloc(ctx->exporterseclen); |
802 | 0 | if (ctx->exportersec == NULL) |
803 | 0 | goto err; |
804 | 0 | if (ossl_hpke_labeled_expand(kctx, ctx->exportersec, ctx->exporterseclen, |
805 | 0 | secret, secretlen, OSSL_HPKE_SEC51LABEL, |
806 | 0 | suitebuf, sizeof(suitebuf), |
807 | 0 | OSSL_HPKE_EXP_LABEL, |
808 | 0 | ks_context, ks_contextlen) |
809 | 0 | != 1) { |
810 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
811 | 0 | goto err; |
812 | 0 | } |
813 | 0 | erv = 1; |
814 | |
|
815 | 0 | err: |
816 | 0 | OPENSSL_cleanse(ks_context, OSSL_HPKE_MAXSIZE); |
817 | 0 | OPENSSL_cleanse(secret, OSSL_HPKE_MAXSIZE); |
818 | 0 | EVP_KDF_CTX_free(kctx); |
819 | 0 | return erv; |
820 | 0 | } |
821 | | |
822 | | /* |
823 | | * externally visible functions from below here, API documentation is |
824 | | * in doc/man3/OSSL_HPKE_CTX_new.pod to avoid duplication |
825 | | */ |
826 | | |
827 | | OSSL_HPKE_CTX *OSSL_HPKE_CTX_new(int mode, OSSL_HPKE_SUITE suite, int role, |
828 | | OSSL_LIB_CTX *libctx, const char *propq) |
829 | 0 | { |
830 | 0 | OSSL_HPKE_CTX *ctx = NULL; |
831 | 0 | const OSSL_HPKE_KEM_INFO *kem_info; |
832 | 0 | const OSSL_HPKE_KDF_INFO *kdf_info; |
833 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info; |
834 | |
|
835 | 0 | if (hpke_mode_check(mode) != 1) { |
836 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
837 | 0 | return NULL; |
838 | 0 | } |
839 | 0 | if (hpke_suite_check(suite, &kem_info, &kdf_info, &aead_info) != 1) { |
840 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
841 | 0 | return NULL; |
842 | 0 | } |
843 | 0 | if (role != OSSL_HPKE_ROLE_SENDER && role != OSSL_HPKE_ROLE_RECEIVER) { |
844 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
845 | 0 | return 0; |
846 | 0 | } |
847 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
848 | 0 | if (ctx == NULL) |
849 | 0 | return NULL; |
850 | 0 | ctx->libctx = libctx; |
851 | 0 | if (propq != NULL) { |
852 | 0 | ctx->propq = OPENSSL_strdup(propq); |
853 | 0 | if (ctx->propq == NULL) |
854 | 0 | goto err; |
855 | 0 | } |
856 | 0 | if (suite.aead_id != OSSL_HPKE_AEAD_ID_EXPORTONLY) { |
857 | 0 | ctx->aead_ciph = EVP_CIPHER_fetch(libctx, aead_info->name, propq); |
858 | 0 | if (ctx->aead_ciph == NULL) { |
859 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_FETCH_FAILED); |
860 | 0 | goto err; |
861 | 0 | } |
862 | 0 | } |
863 | 0 | ctx->role = role; |
864 | 0 | ctx->mode = mode; |
865 | 0 | ctx->suite = suite; |
866 | 0 | ctx->kem_info = kem_info; |
867 | 0 | ctx->kdf_info = kdf_info; |
868 | 0 | ctx->aead_info = aead_info; |
869 | 0 | return ctx; |
870 | | |
871 | 0 | err: |
872 | 0 | EVP_CIPHER_free(ctx->aead_ciph); |
873 | 0 | OPENSSL_free(ctx->propq); |
874 | 0 | OPENSSL_free(ctx); |
875 | 0 | return NULL; |
876 | 0 | } |
877 | | |
878 | | void OSSL_HPKE_CTX_free(OSSL_HPKE_CTX *ctx) |
879 | 0 | { |
880 | 0 | if (ctx == NULL) |
881 | 0 | return; |
882 | 0 | EVP_CIPHER_free(ctx->aead_ciph); |
883 | 0 | OPENSSL_free(ctx->propq); |
884 | 0 | OPENSSL_clear_free(ctx->exportersec, ctx->exporterseclen); |
885 | 0 | OPENSSL_free(ctx->pskid); |
886 | 0 | OPENSSL_clear_free(ctx->psk, ctx->psklen); |
887 | 0 | OPENSSL_clear_free(ctx->key, ctx->keylen); |
888 | 0 | OPENSSL_clear_free(ctx->nonce, ctx->noncelen); |
889 | 0 | OPENSSL_clear_free(ctx->shared_secret, ctx->shared_secretlen); |
890 | 0 | OPENSSL_clear_free(ctx->ikme, ctx->ikmelen); |
891 | 0 | EVP_PKEY_free(ctx->authpriv); |
892 | 0 | OPENSSL_free(ctx->authpub); |
893 | |
|
894 | 0 | OPENSSL_free(ctx); |
895 | 0 | return; |
896 | 0 | } |
897 | | |
898 | | int OSSL_HPKE_CTX_set1_psk(OSSL_HPKE_CTX *ctx, |
899 | | const char *pskid, |
900 | | const unsigned char *psk, size_t psklen) |
901 | 0 | { |
902 | 0 | if (ctx == NULL || pskid == NULL || psk == NULL || psklen == 0) { |
903 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
904 | 0 | return 0; |
905 | 0 | } |
906 | 0 | if (psklen > OSSL_HPKE_MAX_PARMLEN) { |
907 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
908 | 0 | return 0; |
909 | 0 | } |
910 | 0 | if (psklen < OSSL_HPKE_MIN_PSKLEN) { |
911 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
912 | 0 | return 0; |
913 | 0 | } |
914 | 0 | if (strlen(pskid) > OSSL_HPKE_MAX_PARMLEN) { |
915 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
916 | 0 | return 0; |
917 | 0 | } |
918 | 0 | if (strlen(pskid) == 0) { |
919 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
920 | 0 | return 0; |
921 | 0 | } |
922 | 0 | if (ctx->mode != OSSL_HPKE_MODE_PSK |
923 | 0 | && ctx->mode != OSSL_HPKE_MODE_PSKAUTH) { |
924 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
925 | 0 | return 0; |
926 | 0 | } |
927 | | /* free previous values if any */ |
928 | 0 | OPENSSL_clear_free(ctx->psk, ctx->psklen); |
929 | 0 | ctx->psk = OPENSSL_memdup(psk, psklen); |
930 | 0 | if (ctx->psk == NULL) |
931 | 0 | return 0; |
932 | 0 | ctx->psklen = psklen; |
933 | 0 | OPENSSL_free(ctx->pskid); |
934 | 0 | ctx->pskid = OPENSSL_strdup(pskid); |
935 | 0 | if (ctx->pskid == NULL) { |
936 | 0 | OPENSSL_clear_free(ctx->psk, ctx->psklen); |
937 | 0 | ctx->psk = NULL; |
938 | 0 | ctx->psklen = 0; |
939 | 0 | return 0; |
940 | 0 | } |
941 | 0 | return 1; |
942 | 0 | } |
943 | | |
944 | | int OSSL_HPKE_CTX_set1_ikme(OSSL_HPKE_CTX *ctx, |
945 | | const unsigned char *ikme, size_t ikmelen) |
946 | 0 | { |
947 | 0 | if (ctx == NULL || ikme == NULL) { |
948 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
949 | 0 | return 0; |
950 | 0 | } |
951 | 0 | if (ikmelen == 0 || ikmelen > OSSL_HPKE_MAX_PARMLEN) { |
952 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
953 | 0 | return 0; |
954 | 0 | } |
955 | 0 | if (ctx->role != OSSL_HPKE_ROLE_SENDER) { |
956 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
957 | 0 | return 0; |
958 | 0 | } |
959 | 0 | OPENSSL_clear_free(ctx->ikme, ctx->ikmelen); |
960 | 0 | ctx->ikme = OPENSSL_memdup(ikme, ikmelen); |
961 | 0 | if (ctx->ikme == NULL) |
962 | 0 | return 0; |
963 | 0 | ctx->ikmelen = ikmelen; |
964 | 0 | return 1; |
965 | 0 | } |
966 | | |
967 | | int OSSL_HPKE_CTX_set1_authpriv(OSSL_HPKE_CTX *ctx, EVP_PKEY *priv) |
968 | 0 | { |
969 | 0 | if (ctx == NULL || priv == NULL) { |
970 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
971 | 0 | return 0; |
972 | 0 | } |
973 | 0 | if (ctx->mode != OSSL_HPKE_MODE_AUTH |
974 | 0 | && ctx->mode != OSSL_HPKE_MODE_PSKAUTH) { |
975 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
976 | 0 | return 0; |
977 | 0 | } |
978 | 0 | if (ctx->role != OSSL_HPKE_ROLE_SENDER) { |
979 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
980 | 0 | return 0; |
981 | 0 | } |
982 | 0 | EVP_PKEY_free(ctx->authpriv); |
983 | 0 | ctx->authpriv = EVP_PKEY_dup(priv); |
984 | 0 | if (ctx->authpriv == NULL) |
985 | 0 | return 0; |
986 | 0 | return 1; |
987 | 0 | } |
988 | | |
989 | | int OSSL_HPKE_CTX_set1_authpub(OSSL_HPKE_CTX *ctx, |
990 | | const unsigned char *pub, size_t publen) |
991 | 0 | { |
992 | 0 | int erv = 0; |
993 | 0 | EVP_PKEY *pubp = NULL; |
994 | 0 | unsigned char *lpub = NULL; |
995 | 0 | size_t lpublen = 0; |
996 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
997 | |
|
998 | 0 | if (ctx == NULL || pub == NULL || publen == 0) { |
999 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
1000 | 0 | return 0; |
1001 | 0 | } |
1002 | 0 | if (ctx->mode != OSSL_HPKE_MODE_AUTH |
1003 | 0 | && ctx->mode != OSSL_HPKE_MODE_PSKAUTH) { |
1004 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1005 | 0 | return 0; |
1006 | 0 | } |
1007 | 0 | if (ctx->role != OSSL_HPKE_ROLE_RECEIVER) { |
1008 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1009 | 0 | return 0; |
1010 | 0 | } |
1011 | | /* check the value seems like a good public key for this kem */ |
1012 | 0 | kem_info = ossl_HPKE_KEM_INFO_find_id(ctx->suite.kem_id); |
1013 | 0 | if (kem_info == NULL) |
1014 | 0 | return 0; |
1015 | 0 | if (hpke_kem_id_nist_curve(ctx->suite.kem_id) == 1) { |
1016 | 0 | pubp = evp_pkey_new_raw_nist_public_key(ctx->libctx, ctx->propq, |
1017 | 0 | kem_info->groupname, |
1018 | 0 | pub, publen); |
1019 | 0 | } else { |
1020 | 0 | pubp = EVP_PKEY_new_raw_public_key_ex(ctx->libctx, |
1021 | 0 | kem_info->keytype, |
1022 | 0 | ctx->propq, |
1023 | 0 | pub, publen); |
1024 | 0 | } |
1025 | 0 | if (pubp == NULL) { |
1026 | | /* can happen based on external input - buffer value may be garbage */ |
1027 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1028 | 0 | goto err; |
1029 | 0 | } |
1030 | | /* |
1031 | | * extract out the public key in encoded form so we |
1032 | | * should be fine even if given compressed form |
1033 | | */ |
1034 | 0 | lpub = OPENSSL_malloc(OSSL_HPKE_MAXSIZE); |
1035 | 0 | if (lpub == NULL) |
1036 | 0 | goto err; |
1037 | 0 | if (EVP_PKEY_get_octet_string_param(pubp, |
1038 | 0 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
1039 | 0 | lpub, OSSL_HPKE_MAXSIZE, &lpublen) |
1040 | 0 | != 1) { |
1041 | 0 | OPENSSL_free(lpub); |
1042 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1043 | 0 | goto err; |
1044 | 0 | } |
1045 | | /* free up old value */ |
1046 | 0 | OPENSSL_free(ctx->authpub); |
1047 | 0 | ctx->authpub = lpub; |
1048 | 0 | ctx->authpublen = lpublen; |
1049 | 0 | erv = 1; |
1050 | |
|
1051 | 0 | err: |
1052 | 0 | EVP_PKEY_free(pubp); |
1053 | 0 | return erv; |
1054 | 0 | } |
1055 | | |
1056 | | int OSSL_HPKE_CTX_get_seq(OSSL_HPKE_CTX *ctx, uint64_t *seq) |
1057 | 0 | { |
1058 | 0 | if (ctx == NULL || seq == NULL) { |
1059 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
1060 | 0 | return 0; |
1061 | 0 | } |
1062 | 0 | *seq = ctx->seq; |
1063 | 0 | return 1; |
1064 | 0 | } |
1065 | | |
1066 | | int OSSL_HPKE_CTX_set_seq(OSSL_HPKE_CTX *ctx, uint64_t seq) |
1067 | 0 | { |
1068 | 0 | if (ctx == NULL) { |
1069 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER); |
1070 | 0 | return 0; |
1071 | 0 | } |
1072 | | /* |
1073 | | * We disallow senders from doing this as it's dangerous |
1074 | | * Receivers are ok to use this, as no harm should ensue |
1075 | | * if they go wrong. |
1076 | | */ |
1077 | 0 | if (ctx->role == OSSL_HPKE_ROLE_SENDER) { |
1078 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1079 | 0 | return 0; |
1080 | 0 | } |
1081 | 0 | ctx->seq = seq; |
1082 | 0 | return 1; |
1083 | 0 | } |
1084 | | |
1085 | | int OSSL_HPKE_encap(OSSL_HPKE_CTX *ctx, |
1086 | | unsigned char *enc, size_t *enclen, |
1087 | | const unsigned char *pub, size_t publen, |
1088 | | const unsigned char *info, size_t infolen) |
1089 | 0 | { |
1090 | 0 | int erv = 1; |
1091 | 0 | size_t minenc = 0; |
1092 | |
|
1093 | 0 | if (ctx == NULL || enc == NULL || enclen == NULL || *enclen == 0 |
1094 | 0 | || pub == NULL || publen == 0) { |
1095 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1096 | 0 | return 0; |
1097 | 0 | } |
1098 | 0 | if (ctx->role != OSSL_HPKE_ROLE_SENDER) { |
1099 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1100 | 0 | return 0; |
1101 | 0 | } |
1102 | 0 | if (infolen > OSSL_HPKE_MAX_INFOLEN) { |
1103 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1104 | 0 | return 0; |
1105 | 0 | } |
1106 | 0 | if (infolen > 0 && info == NULL) { |
1107 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1108 | 0 | return 0; |
1109 | 0 | } |
1110 | 0 | minenc = OSSL_HPKE_get_public_encap_size(ctx->suite); |
1111 | 0 | if (minenc == 0 || minenc > *enclen) { |
1112 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1113 | 0 | return 0; |
1114 | 0 | } |
1115 | 0 | if (ctx->shared_secret != NULL) { |
1116 | | /* only allow one encap per OSSL_HPKE_CTX */ |
1117 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1118 | 0 | return 0; |
1119 | 0 | } |
1120 | 0 | if (hpke_encap(ctx, enc, enclen, pub, publen) != 1) { |
1121 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1122 | 0 | return 0; |
1123 | 0 | } |
1124 | | /* |
1125 | | * note that the info is not part of the context as it |
1126 | | * only needs to be used once here so doesn't need to |
1127 | | * be stored |
1128 | | */ |
1129 | 0 | erv = hpke_do_middle(ctx, info, infolen); |
1130 | 0 | return erv; |
1131 | 0 | } |
1132 | | |
1133 | | int OSSL_HPKE_decap(OSSL_HPKE_CTX *ctx, |
1134 | | const unsigned char *enc, size_t enclen, |
1135 | | EVP_PKEY *recippriv, |
1136 | | const unsigned char *info, size_t infolen) |
1137 | 0 | { |
1138 | 0 | int erv = 1; |
1139 | 0 | size_t minenc = 0; |
1140 | |
|
1141 | 0 | if (ctx == NULL || enc == NULL || enclen == 0 || recippriv == NULL) { |
1142 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1143 | 0 | return 0; |
1144 | 0 | } |
1145 | 0 | if (ctx->role != OSSL_HPKE_ROLE_RECEIVER) { |
1146 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1147 | 0 | return 0; |
1148 | 0 | } |
1149 | 0 | if (infolen > OSSL_HPKE_MAX_INFOLEN) { |
1150 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1151 | 0 | return 0; |
1152 | 0 | } |
1153 | 0 | if (infolen > 0 && info == NULL) { |
1154 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1155 | 0 | return 0; |
1156 | 0 | } |
1157 | 0 | minenc = OSSL_HPKE_get_public_encap_size(ctx->suite); |
1158 | 0 | if (minenc == 0 || minenc > enclen) { |
1159 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1160 | 0 | return 0; |
1161 | 0 | } |
1162 | 0 | if (ctx->shared_secret != NULL) { |
1163 | | /* only allow one encap per OSSL_HPKE_CTX */ |
1164 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1165 | 0 | return 0; |
1166 | 0 | } |
1167 | 0 | erv = hpke_decap(ctx, enc, enclen, recippriv); |
1168 | 0 | if (erv != 1) { |
1169 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1170 | 0 | return 0; |
1171 | 0 | } |
1172 | | /* |
1173 | | * note that the info is not part of the context as it |
1174 | | * only needs to be used once here so doesn't need to |
1175 | | * be stored |
1176 | | */ |
1177 | 0 | erv = hpke_do_middle(ctx, info, infolen); |
1178 | 0 | return erv; |
1179 | 0 | } |
1180 | | |
1181 | | int OSSL_HPKE_seal(OSSL_HPKE_CTX *ctx, |
1182 | | unsigned char *ct, size_t *ctlen, |
1183 | | const unsigned char *aad, size_t aadlen, |
1184 | | const unsigned char *pt, size_t ptlen) |
1185 | 0 | { |
1186 | 0 | unsigned char seqbuf[OSSL_HPKE_MAX_NONCELEN]; |
1187 | 0 | size_t seqlen = 0; |
1188 | |
|
1189 | 0 | if (ctx == NULL || ct == NULL || ctlen == NULL || *ctlen == 0 |
1190 | 0 | || (pt == NULL && ptlen != 0)) { |
1191 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1192 | 0 | return 0; |
1193 | 0 | } |
1194 | 0 | if (ctx->role != OSSL_HPKE_ROLE_SENDER) { |
1195 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1196 | 0 | return 0; |
1197 | 0 | } |
1198 | 0 | if ((ctx->seq + 1) == 0) { /* wrap around imminent !!! */ |
1199 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1200 | 0 | return 0; |
1201 | 0 | } |
1202 | 0 | if (ctx->key == NULL || ctx->nonce == NULL) { |
1203 | | /* need to have done an encap first, info can be NULL */ |
1204 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1205 | 0 | return 0; |
1206 | 0 | } |
1207 | 0 | seqlen = hpke_seqnonce2buf(ctx, seqbuf, sizeof(seqbuf)); |
1208 | 0 | if (seqlen == 0) { |
1209 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1210 | 0 | return 0; |
1211 | 0 | } |
1212 | 0 | if (hpke_aead_enc(ctx, seqbuf, aad, aadlen, pt, ptlen, ct, ctlen) != 1) { |
1213 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1214 | 0 | OPENSSL_cleanse(seqbuf, sizeof(seqbuf)); |
1215 | 0 | return 0; |
1216 | 0 | } else { |
1217 | 0 | ctx->seq++; |
1218 | 0 | } |
1219 | 0 | OPENSSL_cleanse(seqbuf, sizeof(seqbuf)); |
1220 | 0 | return 1; |
1221 | 0 | } |
1222 | | |
1223 | | int OSSL_HPKE_open(OSSL_HPKE_CTX *ctx, |
1224 | | unsigned char *pt, size_t *ptlen, |
1225 | | const unsigned char *aad, size_t aadlen, |
1226 | | const unsigned char *ct, size_t ctlen) |
1227 | 0 | { |
1228 | 0 | unsigned char seqbuf[OSSL_HPKE_MAX_NONCELEN]; |
1229 | 0 | size_t seqlen = 0; |
1230 | |
|
1231 | 0 | if (ctx == NULL || ptlen == NULL || (pt == NULL && *ptlen != 0) |
1232 | 0 | || ct == NULL || ctlen == 0) { |
1233 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1234 | 0 | return 0; |
1235 | 0 | } |
1236 | 0 | if (ctx->role != OSSL_HPKE_ROLE_RECEIVER) { |
1237 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1238 | 0 | return 0; |
1239 | 0 | } |
1240 | 0 | if ((ctx->seq + 1) == 0) { /* wrap around imminent !!! */ |
1241 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1242 | 0 | return 0; |
1243 | 0 | } |
1244 | 0 | if (ctx->key == NULL || ctx->nonce == NULL) { |
1245 | | /* need to have done an encap first, info can be NULL */ |
1246 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1247 | 0 | return 0; |
1248 | 0 | } |
1249 | 0 | seqlen = hpke_seqnonce2buf(ctx, seqbuf, sizeof(seqbuf)); |
1250 | 0 | if (seqlen == 0) { |
1251 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1252 | 0 | return 0; |
1253 | 0 | } |
1254 | 0 | if (hpke_aead_dec(ctx, seqbuf, aad, aadlen, ct, ctlen, pt, ptlen) != 1) { |
1255 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1256 | 0 | OPENSSL_cleanse(seqbuf, sizeof(seqbuf)); |
1257 | 0 | return 0; |
1258 | 0 | } |
1259 | 0 | ctx->seq++; |
1260 | 0 | OPENSSL_cleanse(seqbuf, sizeof(seqbuf)); |
1261 | 0 | return 1; |
1262 | 0 | } |
1263 | | |
1264 | | int OSSL_HPKE_export(OSSL_HPKE_CTX *ctx, |
1265 | | unsigned char *secret, size_t secretlen, |
1266 | | const unsigned char *label, size_t labellen) |
1267 | 0 | { |
1268 | 0 | int erv = 0; |
1269 | 0 | EVP_KDF_CTX *kctx = NULL; |
1270 | 0 | unsigned char suitebuf[6]; |
1271 | 0 | const char *mdname = NULL; |
1272 | 0 | const OSSL_HPKE_KDF_INFO *kdf_info = NULL; |
1273 | |
|
1274 | 0 | if (ctx == NULL || secret == NULL || secretlen == 0) { |
1275 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1276 | 0 | return 0; |
1277 | 0 | } |
1278 | 0 | if (labellen > OSSL_HPKE_MAX_PARMLEN) { |
1279 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1280 | 0 | return 0; |
1281 | 0 | } |
1282 | 0 | if (labellen > 0 && label == NULL) { |
1283 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1284 | 0 | return 0; |
1285 | 0 | } |
1286 | 0 | if (ctx->exportersec == NULL) { |
1287 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
1288 | 0 | return 0; |
1289 | 0 | } |
1290 | 0 | kdf_info = ossl_HPKE_KDF_INFO_find_id(ctx->suite.kdf_id); |
1291 | 0 | if (kdf_info == NULL) { |
1292 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1293 | 0 | return 0; |
1294 | 0 | } |
1295 | 0 | mdname = kdf_info->mdname; |
1296 | 0 | kctx = ossl_kdf_ctx_create("HKDF", mdname, ctx->libctx, ctx->propq); |
1297 | 0 | if (kctx == NULL) { |
1298 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1299 | 0 | return 0; |
1300 | 0 | } |
1301 | | /* full suiteid as per RFC9180 sec 5.3 */ |
1302 | 0 | suitebuf[0] = ctx->suite.kem_id / 256; |
1303 | 0 | suitebuf[1] = ctx->suite.kem_id % 256; |
1304 | 0 | suitebuf[2] = ctx->suite.kdf_id / 256; |
1305 | 0 | suitebuf[3] = ctx->suite.kdf_id % 256; |
1306 | 0 | suitebuf[4] = ctx->suite.aead_id / 256; |
1307 | 0 | suitebuf[5] = ctx->suite.aead_id % 256; |
1308 | 0 | erv = ossl_hpke_labeled_expand(kctx, secret, secretlen, |
1309 | 0 | ctx->exportersec, ctx->exporterseclen, |
1310 | 0 | OSSL_HPKE_SEC51LABEL, |
1311 | 0 | suitebuf, sizeof(suitebuf), |
1312 | 0 | OSSL_HPKE_EXP_SEC_LABEL, |
1313 | 0 | label, labellen); |
1314 | 0 | EVP_KDF_CTX_free(kctx); |
1315 | 0 | if (erv != 1) |
1316 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1317 | 0 | return erv; |
1318 | 0 | } |
1319 | | |
1320 | | int OSSL_HPKE_keygen(OSSL_HPKE_SUITE suite, |
1321 | | unsigned char *pub, size_t *publen, EVP_PKEY **priv, |
1322 | | const unsigned char *ikm, size_t ikmlen, |
1323 | | OSSL_LIB_CTX *libctx, const char *propq) |
1324 | 0 | { |
1325 | 0 | int erv = 0; /* Our error return value - 1 is success */ |
1326 | 0 | EVP_PKEY_CTX *pctx = NULL; |
1327 | 0 | EVP_PKEY *skR = NULL; |
1328 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
1329 | 0 | OSSL_PARAM params[3], *p = params; |
1330 | |
|
1331 | 0 | if (pub == NULL || publen == NULL || *publen == 0 || priv == NULL) { |
1332 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1333 | 0 | return 0; |
1334 | 0 | } |
1335 | 0 | if (hpke_suite_check(suite, &kem_info, NULL, NULL) != 1) { |
1336 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1337 | 0 | return 0; |
1338 | 0 | } |
1339 | 0 | if ((ikmlen > 0 && ikm == NULL) |
1340 | 0 | || (ikmlen == 0 && ikm != NULL) |
1341 | 0 | || ikmlen > OSSL_HPKE_MAX_PARMLEN) { |
1342 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1343 | 0 | return 0; |
1344 | 0 | } |
1345 | | |
1346 | 0 | if (hpke_kem_id_nist_curve(suite.kem_id) == 1) { |
1347 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
1348 | 0 | (char *)kem_info->groupname, 0); |
1349 | 0 | pctx = EVP_PKEY_CTX_new_from_name(libctx, "EC", propq); |
1350 | 0 | } else { |
1351 | 0 | pctx = EVP_PKEY_CTX_new_from_name(libctx, kem_info->keytype, propq); |
1352 | 0 | } |
1353 | 0 | if (pctx == NULL |
1354 | 0 | || EVP_PKEY_keygen_init(pctx) <= 0) { |
1355 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1356 | 0 | goto err; |
1357 | 0 | } |
1358 | 0 | if (ikm != NULL) |
1359 | 0 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_DHKEM_IKM, |
1360 | 0 | (char *)ikm, ikmlen); |
1361 | 0 | *p = OSSL_PARAM_construct_end(); |
1362 | 0 | if (EVP_PKEY_CTX_set_params(pctx, params) <= 0) { |
1363 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1364 | 0 | goto err; |
1365 | 0 | } |
1366 | 0 | if (EVP_PKEY_generate(pctx, &skR) <= 0) { |
1367 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1368 | 0 | goto err; |
1369 | 0 | } |
1370 | 0 | EVP_PKEY_CTX_free(pctx); |
1371 | 0 | pctx = NULL; |
1372 | 0 | if (EVP_PKEY_get_octet_string_param(skR, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
1373 | 0 | pub, *publen, publen) |
1374 | 0 | != 1) { |
1375 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1376 | 0 | goto err; |
1377 | 0 | } |
1378 | 0 | *priv = skR; |
1379 | 0 | erv = 1; |
1380 | |
|
1381 | 0 | err: |
1382 | 0 | if (erv != 1) |
1383 | 0 | EVP_PKEY_free(skR); |
1384 | 0 | EVP_PKEY_CTX_free(pctx); |
1385 | 0 | return erv; |
1386 | 0 | } |
1387 | | |
1388 | | int OSSL_HPKE_suite_check(OSSL_HPKE_SUITE suite) |
1389 | 0 | { |
1390 | 0 | return hpke_suite_check(suite, NULL, NULL, NULL); |
1391 | 0 | } |
1392 | | |
1393 | | int OSSL_HPKE_get_grease_value(const OSSL_HPKE_SUITE *suite_in, |
1394 | | OSSL_HPKE_SUITE *suite, |
1395 | | unsigned char *enc, size_t *enclen, |
1396 | | unsigned char *ct, size_t ctlen, |
1397 | | OSSL_LIB_CTX *libctx, const char *propq) |
1398 | 0 | { |
1399 | 0 | OSSL_HPKE_SUITE chosen; |
1400 | 0 | size_t plen = 0; |
1401 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
1402 | 0 | const OSSL_HPKE_AEAD_INFO *aead_info = NULL; |
1403 | 0 | EVP_PKEY *fakepriv = NULL; |
1404 | |
|
1405 | 0 | if (enc == NULL || enclen == 0 |
1406 | 0 | || ct == NULL || ctlen == 0 || suite == NULL) { |
1407 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT); |
1408 | 0 | return 0; |
1409 | 0 | } |
1410 | 0 | if (suite_in == NULL) { |
1411 | | /* choose a random suite */ |
1412 | 0 | if (hpke_random_suite(libctx, propq, &chosen) != 1) { |
1413 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1414 | 0 | goto err; |
1415 | 0 | } |
1416 | 0 | } else { |
1417 | 0 | chosen = *suite_in; |
1418 | 0 | } |
1419 | 0 | if (hpke_suite_check(chosen, &kem_info, NULL, &aead_info) != 1) { |
1420 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1421 | 0 | goto err; |
1422 | 0 | } |
1423 | 0 | *suite = chosen; |
1424 | | /* make sure room for tag and one plaintext octet */ |
1425 | 0 | if (aead_info->taglen >= ctlen) { |
1426 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1427 | 0 | goto err; |
1428 | 0 | } |
1429 | | /* publen */ |
1430 | 0 | plen = kem_info->Npk; |
1431 | 0 | if (plen > *enclen) { |
1432 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1433 | 0 | goto err; |
1434 | 0 | } |
1435 | | /* |
1436 | | * In order for our enc to look good for sure, we generate and then |
1437 | | * delete a real key for that curve - bit OTT but it ensures we do |
1438 | | * get the encoding right (e.g. 0x04 as 1st octet for NIST curves in |
1439 | | * uncompressed form) and that the value really does map to a point on |
1440 | | * the relevant curve. |
1441 | | */ |
1442 | 0 | if (OSSL_HPKE_keygen(chosen, enc, enclen, &fakepriv, NULL, 0, |
1443 | 0 | libctx, propq) |
1444 | 0 | != 1) { |
1445 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1446 | 0 | goto err; |
1447 | 0 | } |
1448 | 0 | EVP_PKEY_free(fakepriv); |
1449 | 0 | if (RAND_bytes_ex(libctx, ct, ctlen, 0) <= 0) { |
1450 | 0 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
1451 | 0 | goto err; |
1452 | 0 | } |
1453 | 0 | return 1; |
1454 | 0 | err: |
1455 | 0 | return 0; |
1456 | 0 | } |
1457 | | |
1458 | | int OSSL_HPKE_str2suite(const char *str, OSSL_HPKE_SUITE *suite) |
1459 | 0 | { |
1460 | 0 | return ossl_hpke_str2suite(str, suite); |
1461 | 0 | } |
1462 | | |
1463 | | size_t OSSL_HPKE_get_ciphertext_size(OSSL_HPKE_SUITE suite, size_t clearlen) |
1464 | 0 | { |
1465 | 0 | size_t enclen = 0; |
1466 | 0 | size_t cipherlen = 0; |
1467 | |
|
1468 | 0 | if (hpke_expansion(suite, &enclen, clearlen, &cipherlen) != 1) |
1469 | 0 | return 0; |
1470 | 0 | return cipherlen; |
1471 | 0 | } |
1472 | | |
1473 | | size_t OSSL_HPKE_get_public_encap_size(OSSL_HPKE_SUITE suite) |
1474 | 0 | { |
1475 | 0 | size_t enclen = 0; |
1476 | 0 | size_t cipherlen = 0; |
1477 | 0 | size_t clearlen = 16; |
1478 | |
|
1479 | 0 | if (hpke_expansion(suite, &enclen, clearlen, &cipherlen) != 1) |
1480 | 0 | return 0; |
1481 | 0 | return enclen; |
1482 | 0 | } |
1483 | | |
1484 | | size_t OSSL_HPKE_get_recommended_ikmelen(OSSL_HPKE_SUITE suite) |
1485 | 0 | { |
1486 | 0 | const OSSL_HPKE_KEM_INFO *kem_info = NULL; |
1487 | |
|
1488 | 0 | if (hpke_suite_check(suite, &kem_info, NULL, NULL) != 1) |
1489 | 0 | return 0; |
1490 | 0 | if (kem_info == NULL) |
1491 | 0 | return 0; |
1492 | | |
1493 | 0 | return kem_info->Nsk; |
1494 | 0 | } |