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