| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/hpke.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 |
| 20 | #include <openssl/aead.h> |
| 21 | #include <openssl/bytestring.h> |
| 22 | #include <openssl/curve25519.h> |
| 23 | #include <openssl/digest.h> |
| 24 | #include <openssl/ec.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/evp_errors.h> |
| 27 | #include <openssl/hkdf.h> |
| 28 | #include <openssl/mem.h> |
| 29 | #include <openssl/rand.h> |
| 30 | #include <openssl/sha.h> |
| 31 |
| 32 | #include "../fipsmodule/ec/internal.h" |
| 33 | #include "../internal.h" |
| 34 |
| 35 |
| 36 | // This file implements RFC 9180. |
| 37 |
| 38 | #define MAX_SEED_LEN X25519_PRIVATE_KEY_LEN |
| 39 | #define MAX_SHARED_SECRET_LEN SHA256_DIGEST_LENGTH |
| 40 |
| 41 | struct evp_hpke_kem_st { |
| 42 | uint16_t id; |
| 43 | size_t public_key_len; |
| 44 | size_t private_key_len; |
| 45 | size_t seed_len; |
| 46 | size_t enc_len; |
| 47 | int (*init_key)(EVP_HPKE_KEY *key, const uint8_t *priv_key, |
| 48 | size_t priv_key_len); |
| 49 | int (*generate_key)(EVP_HPKE_KEY *key); |
| 50 | int (*encap_with_seed)(const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret, |
| 51 | size_t *out_shared_secret_len, uint8_t *out_enc, |
| 52 | size_t *out_enc_len, size_t max_enc, |
| 53 | const uint8_t *peer_public_key, |
| 54 | size_t peer_public_key_len, const uint8_t *seed, |
| 55 | size_t seed_len); |
| 56 | int (*decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 57 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 58 | size_t enc_len); |
| 59 | int (*auth_encap_with_seed)(const EVP_HPKE_KEY *key, |
| 60 | uint8_t *out_shared_secret, |
| 61 | size_t *out_shared_secret_len, uint8_t *out_enc, |
| 62 | size_t *out_enc_len, size_t max_enc, |
| 63 | const uint8_t *peer_public_key, |
| 64 | size_t peer_public_key_len, const uint8_t *seed, |
| 65 | size_t seed_len); |
| 66 | int (*auth_decap)(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 67 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 68 | size_t enc_len, const uint8_t *peer_public_key, |
| 69 | size_t peer_public_key_len); |
| 70 | }; |
| 71 |
| 72 | struct evp_hpke_kdf_st { |
| 73 | uint16_t id; |
| 74 | // We only support HKDF-based KDFs. |
| 75 | const EVP_MD *(*hkdf_md_func)(void); |
| 76 | }; |
| 77 |
| 78 | struct evp_hpke_aead_st { |
| 79 | uint16_t id; |
| 80 | const EVP_AEAD *(*aead_func)(void); |
| 81 | }; |
| 82 |
| 83 |
| 84 | // Low-level labeled KDF functions. |
| 85 |
| 86 | static const char kHpkeVersionId[] = "HPKE-v1"; |
| 87 |
| 88 | static int add_label_string(CBB *cbb, const char *label) { |
| 89 | return CBB_add_bytes(cbb, (const uint8_t *)label, strlen(label)); |
| 90 | } |
| 91 |
| 92 | static int hpke_labeled_extract(const EVP_MD *hkdf_md, uint8_t *out_key, |
| 93 | size_t *out_len, const uint8_t *salt, |
| 94 | size_t salt_len, const uint8_t *suite_id, |
| 95 | size_t suite_id_len, const char *label, |
| 96 | const uint8_t *ikm, size_t ikm_len) { |
| 97 | // labeledIKM = concat("HPKE-v1", suite_id, label, IKM) |
| 98 | CBB labeled_ikm; |
| 99 | int ok = CBB_init(&labeled_ikm, 0) && |
| 100 | add_label_string(&labeled_ikm, kHpkeVersionId) && |
| 101 | CBB_add_bytes(&labeled_ikm, suite_id, suite_id_len) && |
| 102 | add_label_string(&labeled_ikm, label) && |
| 103 | CBB_add_bytes(&labeled_ikm, ikm, ikm_len) && |
| 104 | HKDF_extract(out_key, out_len, hkdf_md, CBB_data(&labeled_ikm), |
| 105 | CBB_len(&labeled_ikm), salt, salt_len); |
| 106 | CBB_cleanup(&labeled_ikm); |
| 107 | return ok; |
| 108 | } |
| 109 |
| 110 | static int hpke_labeled_expand(const EVP_MD *hkdf_md, uint8_t *out_key, |
| 111 | size_t out_len, const uint8_t *prk, |
| 112 | size_t prk_len, const uint8_t *suite_id, |
| 113 | size_t suite_id_len, const char *label, |
| 114 | const uint8_t *info, size_t info_len) { |
| 115 | // labeledInfo = concat(I2OSP(L, 2), "HPKE-v1", suite_id, label, info) |
| 116 | CBB labeled_info; |
| 117 | int ok = CBB_init(&labeled_info, 0) && // |
| 118 | CBB_add_u16(&labeled_info, out_len) && |
| 119 | add_label_string(&labeled_info, kHpkeVersionId) && |
| 120 | CBB_add_bytes(&labeled_info, suite_id, suite_id_len) && |
| 121 | add_label_string(&labeled_info, label) && |
| 122 | CBB_add_bytes(&labeled_info, info, info_len) && |
| 123 | HKDF_expand(out_key, out_len, hkdf_md, prk, prk_len, |
| 124 | CBB_data(&labeled_info), CBB_len(&labeled_info)); |
| 125 | CBB_cleanup(&labeled_info); |
| 126 | return ok; |
| 127 | } |
| 128 |
| 129 |
| 130 | // KEM implementations. |
| 131 |
| 132 | // dhkem_extract_and_expand implements the ExtractAndExpand operation in the |
| 133 | // DHKEM construction. See section 4.1 of RFC 9180. |
| 134 | static int dhkem_extract_and_expand(uint16_t kem_id, const EVP_MD *hkdf_md, |
| 135 | uint8_t *out_key, size_t out_len, |
| 136 | const uint8_t *dh, size_t dh_len, |
| 137 | const uint8_t *kem_context, |
| 138 | size_t kem_context_len) { |
| 139 | // concat("KEM", I2OSP(kem_id, 2)) |
| 140 | uint8_t suite_id[5] = {'K', 'E', 'M', static_cast<uint8_t>(kem_id >> 8), |
| 141 | static_cast<uint8_t>(kem_id & 0xff)}; |
| 142 | uint8_t prk[EVP_MAX_MD_SIZE]; |
| 143 | size_t prk_len; |
| 144 | return hpke_labeled_extract(hkdf_md, prk, &prk_len, NULL, 0, suite_id, |
| 145 | sizeof(suite_id), "eae_prk", dh, dh_len) && |
| 146 | hpke_labeled_expand(hkdf_md, out_key, out_len, prk, prk_len, suite_id, |
| 147 | sizeof(suite_id), "shared_secret", kem_context, |
| 148 | kem_context_len); |
| 149 | } |
| 150 |
| 151 | static int x25519_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key, |
| 152 | size_t priv_key_len) { |
| 153 | if (priv_key_len != X25519_PRIVATE_KEY_LEN) { |
| 154 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 155 | return 0; |
| 156 | } |
| 157 |
| 158 | OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); |
| 159 | X25519_public_from_private(key->public_key, priv_key); |
| 160 | return 1; |
| 161 | } |
| 162 |
| 163 | static int x25519_generate_key(EVP_HPKE_KEY *key) { |
| 164 | X25519_keypair(key->public_key, key->private_key); |
| 165 | return 1; |
| 166 | } |
| 167 |
| 168 | static int x25519_encap_with_seed( |
| 169 | const EVP_HPKE_KEM *kem, uint8_t *out_shared_secret, |
| 170 | size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, |
| 171 | size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 172 | const uint8_t *seed, size_t seed_len) { |
| 173 | if (max_enc < X25519_PUBLIC_VALUE_LEN) { |
| 174 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 175 | return 0; |
| 176 | } |
| 177 | if (seed_len != X25519_PRIVATE_KEY_LEN) { |
| 178 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 179 | return 0; |
| 180 | } |
| 181 | X25519_public_from_private(out_enc, seed); |
| 182 |
| 183 | uint8_t dh[X25519_SHARED_KEY_LEN]; |
| 184 | if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN || |
| 185 | !X25519(dh, seed, peer_public_key)) { |
| 186 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 187 | return 0; |
| 188 | } |
| 189 |
| 190 | uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN]; |
| 191 | OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN); |
| 192 | OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key, |
| 193 | X25519_PUBLIC_VALUE_LEN); |
| 194 | if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret, |
| 195 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 196 | kem_context, sizeof(kem_context))) { |
| 197 | return 0; |
| 198 | } |
| 199 |
| 200 | *out_enc_len = X25519_PUBLIC_VALUE_LEN; |
| 201 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 202 | return 1; |
| 203 | } |
| 204 |
| 205 | static int x25519_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 206 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 207 | size_t enc_len) { |
| 208 | uint8_t dh[X25519_SHARED_KEY_LEN]; |
| 209 | if (enc_len != X25519_PUBLIC_VALUE_LEN || |
| 210 | !X25519(dh, key->private_key, enc)) { |
| 211 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 212 | return 0; |
| 213 | } |
| 214 |
| 215 | uint8_t kem_context[2 * X25519_PUBLIC_VALUE_LEN]; |
| 216 | OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN); |
| 217 | OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key, |
| 218 | X25519_PUBLIC_VALUE_LEN); |
| 219 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 220 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 221 | kem_context, sizeof(kem_context))) { |
| 222 | return 0; |
| 223 | } |
| 224 |
| 225 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 226 | return 1; |
| 227 | } |
| 228 |
| 229 | static int x25519_auth_encap_with_seed( |
| 230 | const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 231 | size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, |
| 232 | size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 233 | const uint8_t *seed, size_t seed_len) { |
| 234 | if (max_enc < X25519_PUBLIC_VALUE_LEN) { |
| 235 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 236 | return 0; |
| 237 | } |
| 238 | if (seed_len != X25519_PRIVATE_KEY_LEN) { |
| 239 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 240 | return 0; |
| 241 | } |
| 242 | X25519_public_from_private(out_enc, seed); |
| 243 |
| 244 | uint8_t dh[2 * X25519_SHARED_KEY_LEN]; |
| 245 | if (peer_public_key_len != X25519_PUBLIC_VALUE_LEN || |
| 246 | !X25519(dh, seed, peer_public_key) || |
| 247 | !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) { |
| 248 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 249 | return 0; |
| 250 | } |
| 251 |
| 252 | uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN]; |
| 253 | OPENSSL_memcpy(kem_context, out_enc, X25519_PUBLIC_VALUE_LEN); |
| 254 | OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, peer_public_key, |
| 255 | X25519_PUBLIC_VALUE_LEN); |
| 256 | OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, key->public_key, |
| 257 | X25519_PUBLIC_VALUE_LEN); |
| 258 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 259 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 260 | kem_context, sizeof(kem_context))) { |
| 261 | return 0; |
| 262 | } |
| 263 |
| 264 | *out_enc_len = X25519_PUBLIC_VALUE_LEN; |
| 265 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 266 | return 1; |
| 267 | } |
| 268 |
| 269 | static int x25519_auth_decap(const EVP_HPKE_KEY *key, |
| 270 | uint8_t *out_shared_secret, |
| 271 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 272 | size_t enc_len, const uint8_t *peer_public_key, |
| 273 | size_t peer_public_key_len) { |
| 274 | uint8_t dh[2 * X25519_SHARED_KEY_LEN]; |
| 275 | if (enc_len != X25519_PUBLIC_VALUE_LEN || |
| 276 | peer_public_key_len != X25519_PUBLIC_VALUE_LEN || |
| 277 | !X25519(dh, key->private_key, enc) || |
| 278 | !X25519(dh + X25519_SHARED_KEY_LEN, key->private_key, peer_public_key)) { |
| 279 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 280 | return 0; |
| 281 | } |
| 282 |
| 283 | uint8_t kem_context[3 * X25519_PUBLIC_VALUE_LEN]; |
| 284 | OPENSSL_memcpy(kem_context, enc, X25519_PUBLIC_VALUE_LEN); |
| 285 | OPENSSL_memcpy(kem_context + X25519_PUBLIC_VALUE_LEN, key->public_key, |
| 286 | X25519_PUBLIC_VALUE_LEN); |
| 287 | OPENSSL_memcpy(kem_context + 2 * X25519_PUBLIC_VALUE_LEN, peer_public_key, |
| 288 | X25519_PUBLIC_VALUE_LEN); |
| 289 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 290 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 291 | kem_context, sizeof(kem_context))) { |
| 292 | return 0; |
| 293 | } |
| 294 |
| 295 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 296 | return 1; |
| 297 | } |
| 298 |
| 299 | const EVP_HPKE_KEM *EVP_hpke_x25519_hkdf_sha256(void) { |
| 300 | static const EVP_HPKE_KEM kKEM = { |
| 301 | /*id=*/EVP_HPKE_DHKEM_X25519_HKDF_SHA256, |
| 302 | /*public_key_len=*/X25519_PUBLIC_VALUE_LEN, |
| 303 | /*private_key_len=*/X25519_PRIVATE_KEY_LEN, |
| 304 | /*seed_len=*/X25519_PRIVATE_KEY_LEN, |
| 305 | /*enc_len=*/X25519_PUBLIC_VALUE_LEN, |
| 306 | x25519_init_key, |
| 307 | x25519_generate_key, |
| 308 | x25519_encap_with_seed, |
| 309 | x25519_decap, |
| 310 | x25519_auth_encap_with_seed, |
| 311 | x25519_auth_decap, |
| 312 | }; |
| 313 | return &kKEM; |
| 314 | } |
| 315 |
| 316 | #define P256_PRIVATE_KEY_LEN 32 |
| 317 | #define P256_PUBLIC_KEY_LEN 65 |
| 318 | #define P256_PUBLIC_VALUE_LEN 65 |
| 319 | #define P256_SEED_LEN 32 |
| 320 | #define P256_SHARED_KEY_LEN 32 |
| 321 |
| 322 | static int p256_public_from_private(uint8_t out_pub[P256_PUBLIC_VALUE_LEN], |
| 323 | const uint8_t priv[P256_PRIVATE_KEY_LEN]) { |
| 324 | const EC_GROUP *const group = EC_group_p256(); |
| 325 | const uint8_t kAllZeros[P256_PRIVATE_KEY_LEN] = {0}; |
| 326 | EC_SCALAR private_scalar; |
| 327 | EC_JACOBIAN public_point; |
| 328 | EC_AFFINE public_point_affine; |
| 329 |
| 330 | if (CRYPTO_memcmp(kAllZeros, priv, sizeof(kAllZeros)) == 0) { |
| 331 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 332 | return 0; |
| 333 | } |
| 334 |
| 335 | if (!ec_scalar_from_bytes(group, &private_scalar, priv, |
| 336 | P256_PRIVATE_KEY_LEN) || |
| 337 | !ec_point_mul_scalar_base(group, &public_point, &private_scalar) || |
| 338 | !ec_jacobian_to_affine(group, &public_point_affine, &public_point)) { |
| 339 | return 0; |
| 340 | } |
| 341 |
| 342 | size_t out_len_x, out_len_y; |
| 343 | out_pub[0] = POINT_CONVERSION_UNCOMPRESSED; |
| 344 | ec_felem_to_bytes(group, &out_pub[1], &out_len_x, &public_point_affine.X); |
| 345 | ec_felem_to_bytes(group, &out_pub[33], &out_len_y, &public_point_affine.Y); |
| 346 | return 1; |
| 347 | } |
| 348 |
| 349 | static int p256_init_key(EVP_HPKE_KEY *key, const uint8_t *priv_key, |
| 350 | size_t priv_key_len) { |
| 351 | if (priv_key_len != P256_PRIVATE_KEY_LEN) { |
| 352 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 353 | return 0; |
| 354 | } |
| 355 |
| 356 | if (!p256_public_from_private(key->public_key, priv_key)) { |
| 357 | return 0; |
| 358 | } |
| 359 |
| 360 | OPENSSL_memcpy(key->private_key, priv_key, priv_key_len); |
| 361 | return 1; |
| 362 | } |
| 363 |
| 364 | static int p256_private_key_from_seed(uint8_t out_priv[P256_PRIVATE_KEY_LEN], |
| 365 | const uint8_t seed[P256_SEED_LEN]) { |
| 366 | // https://www.rfc-editor.org/rfc/rfc9180.html#name-derivekeypair |
| 367 | const uint8_t suite_id[5] = {'K', 'E', 'M', |
| 368 | EVP_HPKE_DHKEM_P256_HKDF_SHA256 >> 8, |
| 369 | EVP_HPKE_DHKEM_P256_HKDF_SHA256 & 0xff}; |
| 370 |
| 371 | uint8_t dkp_prk[32]; |
| 372 | size_t dkp_prk_len; |
| 373 | if (!hpke_labeled_extract(EVP_sha256(), dkp_prk, &dkp_prk_len, NULL, 0, |
| 374 | suite_id, sizeof(suite_id), "dkp_prk", seed, |
| 375 | P256_SEED_LEN)) { |
| 376 | return 0; |
| 377 | } |
| 378 | assert(dkp_prk_len == sizeof(dkp_prk)); |
| 379 |
| 380 | const EC_GROUP *const group = EC_group_p256(); |
| 381 | EC_SCALAR private_scalar; |
| 382 |
| 383 | for (unsigned counter = 0; counter < 256; counter++) { |
| 384 | const uint8_t counter_byte = counter & 0xff; |
| 385 | if (!hpke_labeled_expand(EVP_sha256(), out_priv, P256_PRIVATE_KEY_LEN, |
| 386 | dkp_prk, sizeof(dkp_prk), suite_id, |
| 387 | sizeof(suite_id), "candidate", &counter_byte, |
| 388 | sizeof(counter_byte))) { |
| 389 | return 0; |
| 390 | } |
| 391 |
| 392 | // This checks that the scalar is less than the order. |
| 393 | if (ec_scalar_from_bytes(group, &private_scalar, out_priv, |
| 394 | P256_PRIVATE_KEY_LEN)) { |
| 395 | return 1; |
| 396 | } |
| 397 | } |
| 398 |
| 399 | // This happens with probability of 2^-(32*256). |
| 400 | OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); |
| 401 | return 0; |
| 402 | } |
| 403 |
| 404 | static int p256_generate_key(EVP_HPKE_KEY *key) { |
| 405 | uint8_t seed[P256_SEED_LEN]; |
| 406 | RAND_bytes(seed, sizeof(seed)); |
| 407 | if (!p256_private_key_from_seed(key->private_key, seed) || |
| 408 | !p256_public_from_private(key->public_key, key->private_key)) { |
| 409 | return 0; |
| 410 | } |
| 411 | return 1; |
| 412 | } |
| 413 |
| 414 | static int p256(uint8_t out_dh[P256_SHARED_KEY_LEN], |
| 415 | const uint8_t my_private[P256_PRIVATE_KEY_LEN], |
| 416 | const uint8_t their_public[P256_PUBLIC_VALUE_LEN]) { |
| 417 | const EC_GROUP *const group = EC_group_p256(); |
| 418 | EC_SCALAR private_scalar; |
| 419 | EC_FELEM x, y; |
| 420 | EC_JACOBIAN shared_point, their_point; |
| 421 | EC_AFFINE their_point_affine, shared_point_affine; |
| 422 |
| 423 | if (their_public[0] != POINT_CONVERSION_UNCOMPRESSED || |
| 424 | !ec_felem_from_bytes(group, &x, &their_public[1], 32) || |
| 425 | !ec_felem_from_bytes(group, &y, &their_public[33], 32) || |
| 426 | !ec_point_set_affine_coordinates(group, &their_point_affine, &x, &y) || |
| 427 | !ec_scalar_from_bytes(group, &private_scalar, my_private, |
| 428 | P256_PRIVATE_KEY_LEN)) { |
| 429 | OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); |
| 430 | return 0; |
| 431 | } |
| 432 |
| 433 | ec_affine_to_jacobian(group, &their_point, &their_point_affine); |
| 434 | if (!ec_point_mul_scalar(group, &shared_point, &their_point, |
| 435 | &private_scalar) || |
| 436 | !ec_jacobian_to_affine(group, &shared_point_affine, &shared_point)) { |
| 437 | OPENSSL_PUT_ERROR(EVP, ERR_R_INTERNAL_ERROR); |
| 438 | return 0; |
| 439 | } |
| 440 |
| 441 | size_t out_len; |
| 442 | ec_felem_to_bytes(group, out_dh, &out_len, &shared_point_affine.X); |
| 443 | assert(out_len == P256_SHARED_KEY_LEN); |
| 444 | return 1; |
| 445 | } |
| 446 |
| 447 | static int p256_encap_with_seed(const EVP_HPKE_KEM *kem, |
| 448 | uint8_t *out_shared_secret, |
| 449 | size_t *out_shared_secret_len, uint8_t *out_enc, |
| 450 | size_t *out_enc_len, size_t max_enc, |
| 451 | const uint8_t *peer_public_key, |
| 452 | size_t peer_public_key_len, const uint8_t *seed, |
| 453 | size_t seed_len) { |
| 454 | if (max_enc < P256_PUBLIC_VALUE_LEN) { |
| 455 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 456 | return 0; |
| 457 | } |
| 458 | if (seed_len != P256_SEED_LEN) { |
| 459 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 460 | return 0; |
| 461 | } |
| 462 | uint8_t private_key[P256_PRIVATE_KEY_LEN]; |
| 463 | if (!p256_private_key_from_seed(private_key, seed)) { |
| 464 | return 0; |
| 465 | } |
| 466 | p256_public_from_private(out_enc, private_key); |
| 467 |
| 468 | uint8_t dh[P256_SHARED_KEY_LEN]; |
| 469 | if (peer_public_key_len != P256_PUBLIC_VALUE_LEN || |
| 470 | !p256(dh, private_key, peer_public_key)) { |
| 471 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 472 | return 0; |
| 473 | } |
| 474 |
| 475 | uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN]; |
| 476 | OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN); |
| 477 | OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key, |
| 478 | P256_PUBLIC_VALUE_LEN); |
| 479 | if (!dhkem_extract_and_expand(kem->id, EVP_sha256(), out_shared_secret, |
| 480 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 481 | kem_context, sizeof(kem_context))) { |
| 482 | return 0; |
| 483 | } |
| 484 |
| 485 | *out_enc_len = P256_PUBLIC_VALUE_LEN; |
| 486 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 487 | return 1; |
| 488 | } |
| 489 |
| 490 | static int p256_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 491 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 492 | size_t enc_len) { |
| 493 | uint8_t dh[P256_SHARED_KEY_LEN]; |
| 494 | if (enc_len != P256_PUBLIC_VALUE_LEN || // |
| 495 | !p256(dh, key->private_key, enc)) { |
| 496 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 497 | return 0; |
| 498 | } |
| 499 |
| 500 | uint8_t kem_context[2 * P256_PUBLIC_VALUE_LEN]; |
| 501 | OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN); |
| 502 | OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key, |
| 503 | P256_PUBLIC_VALUE_LEN); |
| 504 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 505 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 506 | kem_context, sizeof(kem_context))) { |
| 507 | return 0; |
| 508 | } |
| 509 |
| 510 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 511 | return 1; |
| 512 | } |
| 513 |
| 514 | static int p256_auth_encap_with_seed( |
| 515 | const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 516 | size_t *out_shared_secret_len, uint8_t *out_enc, size_t *out_enc_len, |
| 517 | size_t max_enc, const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 518 | const uint8_t *seed, size_t seed_len) { |
| 519 | if (max_enc < P256_PUBLIC_VALUE_LEN) { |
| 520 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 521 | return 0; |
| 522 | } |
| 523 | if (seed_len != P256_SEED_LEN) { |
| 524 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 525 | return 0; |
| 526 | } |
| 527 | uint8_t private_key[P256_PRIVATE_KEY_LEN]; |
| 528 | if (!p256_private_key_from_seed(private_key, seed)) { |
| 529 | return 0; |
| 530 | } |
| 531 | p256_public_from_private(out_enc, private_key); |
| 532 |
| 533 | uint8_t dh[2 * P256_SHARED_KEY_LEN]; |
| 534 | if (peer_public_key_len != P256_PUBLIC_VALUE_LEN || |
| 535 | !p256(dh, private_key, peer_public_key) || |
| 536 | !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) { |
| 537 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 538 | return 0; |
| 539 | } |
| 540 |
| 541 | uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN]; |
| 542 | OPENSSL_memcpy(kem_context, out_enc, P256_PUBLIC_VALUE_LEN); |
| 543 | OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, peer_public_key, |
| 544 | P256_PUBLIC_VALUE_LEN); |
| 545 | OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, key->public_key, |
| 546 | P256_PUBLIC_VALUE_LEN); |
| 547 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 548 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 549 | kem_context, sizeof(kem_context))) { |
| 550 | return 0; |
| 551 | } |
| 552 |
| 553 | *out_enc_len = P256_PUBLIC_VALUE_LEN; |
| 554 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 555 | return 1; |
| 556 | } |
| 557 |
| 558 | static int p256_auth_decap(const EVP_HPKE_KEY *key, uint8_t *out_shared_secret, |
| 559 | size_t *out_shared_secret_len, const uint8_t *enc, |
| 560 | size_t enc_len, const uint8_t *peer_public_key, |
| 561 | size_t peer_public_key_len) { |
| 562 | uint8_t dh[2 * P256_SHARED_KEY_LEN]; |
| 563 | if (enc_len != P256_PUBLIC_VALUE_LEN || |
| 564 | peer_public_key_len != P256_PUBLIC_VALUE_LEN || |
| 565 | !p256(dh, key->private_key, enc) || |
| 566 | !p256(dh + P256_SHARED_KEY_LEN, key->private_key, peer_public_key)) { |
| 567 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY); |
| 568 | return 0; |
| 569 | } |
| 570 |
| 571 | uint8_t kem_context[3 * P256_PUBLIC_VALUE_LEN]; |
| 572 | OPENSSL_memcpy(kem_context, enc, P256_PUBLIC_VALUE_LEN); |
| 573 | OPENSSL_memcpy(kem_context + P256_PUBLIC_VALUE_LEN, key->public_key, |
| 574 | P256_PUBLIC_VALUE_LEN); |
| 575 | OPENSSL_memcpy(kem_context + 2 * P256_PUBLIC_VALUE_LEN, peer_public_key, |
| 576 | P256_PUBLIC_VALUE_LEN); |
| 577 | if (!dhkem_extract_and_expand(key->kem->id, EVP_sha256(), out_shared_secret, |
| 578 | SHA256_DIGEST_LENGTH, dh, sizeof(dh), |
| 579 | kem_context, sizeof(kem_context))) { |
| 580 | return 0; |
| 581 | } |
| 582 |
| 583 | *out_shared_secret_len = SHA256_DIGEST_LENGTH; |
| 584 | return 1; |
| 585 | } |
| 586 |
| 587 | const EVP_HPKE_KEM *EVP_hpke_p256_hkdf_sha256(void) { |
| 588 | static const EVP_HPKE_KEM kKEM = { |
| 589 | /*id=*/EVP_HPKE_DHKEM_P256_HKDF_SHA256, |
| 590 | /*public_key_len=*/P256_PUBLIC_KEY_LEN, |
| 591 | /*private_key_len=*/P256_PRIVATE_KEY_LEN, |
| 592 | /*seed_len=*/P256_SEED_LEN, |
| 593 | /*enc_len=*/P256_PUBLIC_VALUE_LEN, |
| 594 | p256_init_key, |
| 595 | p256_generate_key, |
| 596 | p256_encap_with_seed, |
| 597 | p256_decap, |
| 598 | p256_auth_encap_with_seed, |
| 599 | p256_auth_decap, |
| 600 | }; |
| 601 | return &kKEM; |
| 602 | } |
| 603 |
| 604 | uint16_t EVP_HPKE_KEM_id(const EVP_HPKE_KEM *kem) { return kem->id; } |
| 605 |
| 606 | size_t EVP_HPKE_KEM_public_key_len(const EVP_HPKE_KEM *kem) { |
| 607 | return kem->public_key_len; |
| 608 | } |
| 609 |
| 610 | size_t EVP_HPKE_KEM_private_key_len(const EVP_HPKE_KEM *kem) { |
| 611 | return kem->private_key_len; |
| 612 | } |
| 613 |
| 614 | size_t EVP_HPKE_KEM_enc_len(const EVP_HPKE_KEM *kem) { return kem->enc_len; } |
| 615 |
| 616 | void EVP_HPKE_KEY_zero(EVP_HPKE_KEY *key) { |
| 617 | OPENSSL_memset(key, 0, sizeof(EVP_HPKE_KEY)); |
| 618 | } |
| 619 |
| 620 | void EVP_HPKE_KEY_cleanup(EVP_HPKE_KEY *key) { |
| 621 | // Nothing to clean up for now, but we may introduce a cleanup process in the |
| 622 | // future. |
| 623 | } |
| 624 |
| 625 | EVP_HPKE_KEY *EVP_HPKE_KEY_new(void) { |
| 626 | EVP_HPKE_KEY *key = |
| 627 | reinterpret_cast<EVP_HPKE_KEY *>(OPENSSL_malloc(sizeof(EVP_HPKE_KEY))); |
| 628 | if (key == NULL) { |
| 629 | return NULL; |
| 630 | } |
| 631 | EVP_HPKE_KEY_zero(key); |
| 632 | return key; |
| 633 | } |
| 634 |
| 635 | void EVP_HPKE_KEY_free(EVP_HPKE_KEY *key) { |
| 636 | if (key != NULL) { |
| 637 | EVP_HPKE_KEY_cleanup(key); |
| 638 | OPENSSL_free(key); |
| 639 | } |
| 640 | } |
| 641 |
| 642 | int EVP_HPKE_KEY_copy(EVP_HPKE_KEY *dst, const EVP_HPKE_KEY *src) { |
| 643 | // For now, |EVP_HPKE_KEY| is trivially copyable. |
| 644 | OPENSSL_memcpy(dst, src, sizeof(EVP_HPKE_KEY)); |
| 645 | return 1; |
| 646 | } |
| 647 |
| 648 | void EVP_HPKE_KEY_move(EVP_HPKE_KEY *out, EVP_HPKE_KEY *in) { |
| 649 | EVP_HPKE_KEY_cleanup(out); |
| 650 | // For now, |EVP_HPKE_KEY| is trivially movable. |
| 651 | // Note that Rust may move this structure. See |
| 652 | // bssl-crypto/src/scoped.rs:EvpHpkeKey. |
| 653 | OPENSSL_memcpy(out, in, sizeof(EVP_HPKE_KEY)); |
| 654 | EVP_HPKE_KEY_zero(in); |
| 655 | } |
| 656 |
| 657 | int EVP_HPKE_KEY_init(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem, |
| 658 | const uint8_t *priv_key, size_t priv_key_len) { |
| 659 | EVP_HPKE_KEY_zero(key); |
| 660 | key->kem = kem; |
| 661 | if (!kem->init_key(key, priv_key, priv_key_len)) { |
| 662 | key->kem = NULL; |
| 663 | return 0; |
| 664 | } |
| 665 | return 1; |
| 666 | } |
| 667 |
| 668 | int EVP_HPKE_KEY_generate(EVP_HPKE_KEY *key, const EVP_HPKE_KEM *kem) { |
| 669 | EVP_HPKE_KEY_zero(key); |
| 670 | key->kem = kem; |
| 671 | if (!kem->generate_key(key)) { |
| 672 | key->kem = NULL; |
| 673 | return 0; |
| 674 | } |
| 675 | return 1; |
| 676 | } |
| 677 |
| 678 | const EVP_HPKE_KEM *EVP_HPKE_KEY_kem(const EVP_HPKE_KEY *key) { |
| 679 | return key->kem; |
| 680 | } |
| 681 |
| 682 | int EVP_HPKE_KEY_public_key(const EVP_HPKE_KEY *key, uint8_t *out, |
| 683 | size_t *out_len, size_t max_out) { |
| 684 | if (max_out < key->kem->public_key_len) { |
| 685 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 686 | return 0; |
| 687 | } |
| 688 | OPENSSL_memcpy(out, key->public_key, key->kem->public_key_len); |
| 689 | *out_len = key->kem->public_key_len; |
| 690 | return 1; |
| 691 | } |
| 692 |
| 693 | int EVP_HPKE_KEY_private_key(const EVP_HPKE_KEY *key, uint8_t *out, |
| 694 | size_t *out_len, size_t max_out) { |
| 695 | if (max_out < key->kem->private_key_len) { |
| 696 | OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE); |
| 697 | return 0; |
| 698 | } |
| 699 | OPENSSL_memcpy(out, key->private_key, key->kem->private_key_len); |
| 700 | *out_len = key->kem->private_key_len; |
| 701 | return 1; |
| 702 | } |
| 703 |
| 704 |
| 705 | // Supported KDFs and AEADs. |
| 706 |
| 707 | const EVP_HPKE_KDF *EVP_hpke_hkdf_sha256(void) { |
| 708 | static const EVP_HPKE_KDF kKDF = {EVP_HPKE_HKDF_SHA256, &EVP_sha256}; |
| 709 | return &kKDF; |
| 710 | } |
| 711 |
| 712 | uint16_t EVP_HPKE_KDF_id(const EVP_HPKE_KDF *kdf) { return kdf->id; } |
| 713 |
| 714 | const EVP_MD *EVP_HPKE_KDF_hkdf_md(const EVP_HPKE_KDF *kdf) { |
| 715 | return kdf->hkdf_md_func(); |
| 716 | } |
| 717 |
| 718 | const EVP_HPKE_AEAD *EVP_hpke_aes_128_gcm(void) { |
| 719 | static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_128_GCM, |
| 720 | &EVP_aead_aes_128_gcm}; |
| 721 | return &kAEAD; |
| 722 | } |
| 723 |
| 724 | const EVP_HPKE_AEAD *EVP_hpke_aes_256_gcm(void) { |
| 725 | static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_AES_256_GCM, |
| 726 | &EVP_aead_aes_256_gcm}; |
| 727 | return &kAEAD; |
| 728 | } |
| 729 |
| 730 | const EVP_HPKE_AEAD *EVP_hpke_chacha20_poly1305(void) { |
| 731 | static const EVP_HPKE_AEAD kAEAD = {EVP_HPKE_CHACHA20_POLY1305, |
| 732 | &EVP_aead_chacha20_poly1305}; |
| 733 | return &kAEAD; |
| 734 | } |
| 735 |
| 736 | uint16_t EVP_HPKE_AEAD_id(const EVP_HPKE_AEAD *aead) { return aead->id; } |
| 737 |
| 738 | const EVP_AEAD *EVP_HPKE_AEAD_aead(const EVP_HPKE_AEAD *aead) { |
| 739 | return aead->aead_func(); |
| 740 | } |
| 741 |
| 742 |
| 743 | // HPKE implementation. |
| 744 |
| 745 | // This is strlen("HPKE") + 3 * sizeof(uint16_t). |
| 746 | #define HPKE_SUITE_ID_LEN 10 |
| 747 |
| 748 | // The suite_id for non-KEM pieces of HPKE is defined as concat("HPKE", |
| 749 | // I2OSP(kem_id, 2), I2OSP(kdf_id, 2), I2OSP(aead_id, 2)). |
| 750 | static int hpke_build_suite_id(const EVP_HPKE_CTX *ctx, |
| 751 | uint8_t out[HPKE_SUITE_ID_LEN]) { |
| 752 | CBB cbb; |
| 753 | CBB_init_fixed(&cbb, out, HPKE_SUITE_ID_LEN); |
| 754 | return add_label_string(&cbb, "HPKE") && // |
| 755 | CBB_add_u16(&cbb, ctx->kem->id) && // |
| 756 | CBB_add_u16(&cbb, ctx->kdf->id) && // |
| 757 | CBB_add_u16(&cbb, ctx->aead->id); |
| 758 | } |
| 759 |
| 760 | #define HPKE_MODE_BASE 0 |
| 761 | #define HPKE_MODE_AUTH 2 |
| 762 |
| 763 | static int hpke_key_schedule(EVP_HPKE_CTX *ctx, uint8_t mode, |
| 764 | const uint8_t *shared_secret, |
| 765 | size_t shared_secret_len, const uint8_t *info, |
| 766 | size_t info_len) { |
| 767 | uint8_t suite_id[HPKE_SUITE_ID_LEN]; |
| 768 | if (!hpke_build_suite_id(ctx, suite_id)) { |
| 769 | return 0; |
| 770 | } |
| 771 |
| 772 | // psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id) |
| 773 | // TODO(davidben): Precompute this value and store it with the EVP_HPKE_KDF. |
| 774 | const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func(); |
| 775 | uint8_t psk_id_hash[EVP_MAX_MD_SIZE]; |
| 776 | size_t psk_id_hash_len; |
| 777 | if (!hpke_labeled_extract(hkdf_md, psk_id_hash, &psk_id_hash_len, NULL, 0, |
| 778 | suite_id, sizeof(suite_id), "psk_id_hash", NULL, |
| 779 | 0)) { |
| 780 | return 0; |
| 781 | } |
| 782 |
| 783 | // info_hash = LabeledExtract("", "info_hash", info) |
| 784 | uint8_t info_hash[EVP_MAX_MD_SIZE]; |
| 785 | size_t info_hash_len; |
| 786 | if (!hpke_labeled_extract(hkdf_md, info_hash, &info_hash_len, NULL, 0, |
| 787 | suite_id, sizeof(suite_id), "info_hash", info, |
| 788 | info_len)) { |
| 789 | return 0; |
| 790 | } |
| 791 |
| 792 | // key_schedule_context = concat(mode, psk_id_hash, info_hash) |
| 793 | uint8_t context[sizeof(uint8_t) + 2 * EVP_MAX_MD_SIZE]; |
| 794 | size_t context_len; |
| 795 | CBB context_cbb; |
| 796 | CBB_init_fixed(&context_cbb, context, sizeof(context)); |
| 797 | if (!CBB_add_u8(&context_cbb, mode) || |
| 798 | !CBB_add_bytes(&context_cbb, psk_id_hash, psk_id_hash_len) || |
| 799 | !CBB_add_bytes(&context_cbb, info_hash, info_hash_len) || |
| 800 | !CBB_finish(&context_cbb, NULL, &context_len)) { |
| 801 | return 0; |
| 802 | } |
| 803 |
| 804 | // secret = LabeledExtract(shared_secret, "secret", psk) |
| 805 | uint8_t secret[EVP_MAX_MD_SIZE]; |
| 806 | size_t secret_len; |
| 807 | if (!hpke_labeled_extract(hkdf_md, secret, &secret_len, shared_secret, |
| 808 | shared_secret_len, suite_id, sizeof(suite_id), |
| 809 | "secret", NULL, 0)) { |
| 810 | return 0; |
| 811 | } |
| 812 |
| 813 | // key = LabeledExpand(secret, "key", key_schedule_context, Nk) |
| 814 | const EVP_AEAD *aead = EVP_HPKE_AEAD_aead(ctx->aead); |
| 815 | uint8_t key[EVP_AEAD_MAX_KEY_LENGTH]; |
| 816 | const size_t kKeyLen = EVP_AEAD_key_length(aead); |
| 817 | if (!hpke_labeled_expand(hkdf_md, key, kKeyLen, secret, secret_len, suite_id, |
| 818 | sizeof(suite_id), "key", context, context_len) || |
| 819 | !EVP_AEAD_CTX_init(&ctx->aead_ctx, aead, key, kKeyLen, |
| 820 | EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) { |
| 821 | return 0; |
| 822 | } |
| 823 |
| 824 | // base_nonce = LabeledExpand(secret, "base_nonce", key_schedule_context, Nn) |
| 825 | if (!hpke_labeled_expand(hkdf_md, ctx->base_nonce, |
| 826 | EVP_AEAD_nonce_length(aead), secret, secret_len, |
| 827 | suite_id, sizeof(suite_id), "base_nonce", context, |
| 828 | context_len)) { |
| 829 | return 0; |
| 830 | } |
| 831 |
| 832 | // exporter_secret = LabeledExpand(secret, "exp", key_schedule_context, Nh) |
| 833 | if (!hpke_labeled_expand(hkdf_md, ctx->exporter_secret, EVP_MD_size(hkdf_md), |
| 834 | secret, secret_len, suite_id, sizeof(suite_id), |
| 835 | "exp", context, context_len)) { |
| 836 | return 0; |
| 837 | } |
| 838 |
| 839 | return 1; |
| 840 | } |
| 841 |
| 842 | void EVP_HPKE_CTX_zero(EVP_HPKE_CTX *ctx) { |
| 843 | OPENSSL_memset(ctx, 0, sizeof(EVP_HPKE_CTX)); |
| 844 | EVP_AEAD_CTX_zero(&ctx->aead_ctx); |
| 845 | } |
| 846 |
| 847 | void EVP_HPKE_CTX_cleanup(EVP_HPKE_CTX *ctx) { |
| 848 | EVP_AEAD_CTX_cleanup(&ctx->aead_ctx); |
| 849 | } |
| 850 |
| 851 | EVP_HPKE_CTX *EVP_HPKE_CTX_new(void) { |
| 852 | EVP_HPKE_CTX *ctx = |
| 853 | reinterpret_cast<EVP_HPKE_CTX *>(OPENSSL_malloc(sizeof(EVP_HPKE_CTX))); |
| 854 | if (ctx == NULL) { |
| 855 | return NULL; |
| 856 | } |
| 857 | EVP_HPKE_CTX_zero(ctx); |
| 858 | return ctx; |
| 859 | } |
| 860 |
| 861 | void EVP_HPKE_CTX_free(EVP_HPKE_CTX *ctx) { |
| 862 | if (ctx != NULL) { |
| 863 | EVP_HPKE_CTX_cleanup(ctx); |
| 864 | OPENSSL_free(ctx); |
| 865 | } |
| 866 | } |
| 867 |
| 868 | int EVP_HPKE_CTX_setup_sender(EVP_HPKE_CTX *ctx, uint8_t *out_enc, |
| 869 | size_t *out_enc_len, size_t max_enc, |
| 870 | const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, |
| 871 | const EVP_HPKE_AEAD *aead, |
| 872 | const uint8_t *peer_public_key, |
| 873 | size_t peer_public_key_len, const uint8_t *info, |
| 874 | size_t info_len) { |
| 875 | uint8_t seed[MAX_SEED_LEN]; |
| 876 | RAND_bytes(seed, kem->seed_len); |
| 877 | return EVP_HPKE_CTX_setup_sender_with_seed_for_testing( |
| 878 | ctx, out_enc, out_enc_len, max_enc, kem, kdf, aead, peer_public_key, |
| 879 | peer_public_key_len, info, info_len, seed, kem->seed_len); |
| 880 | } |
| 881 |
| 882 | int EVP_HPKE_CTX_setup_sender_with_seed_for_testing( |
| 883 | EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, |
| 884 | const EVP_HPKE_KEM *kem, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, |
| 885 | const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 886 | const uint8_t *info, size_t info_len, const uint8_t *seed, |
| 887 | size_t seed_len) { |
| 888 | EVP_HPKE_CTX_zero(ctx); |
| 889 | ctx->is_sender = 1; |
| 890 | ctx->kem = kem; |
| 891 | ctx->kdf = kdf; |
| 892 | ctx->aead = aead; |
| 893 | uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; |
| 894 | size_t shared_secret_len; |
| 895 | if (!kem->encap_with_seed(kem, shared_secret, &shared_secret_len, out_enc, |
| 896 | out_enc_len, max_enc, peer_public_key, |
| 897 | peer_public_key_len, seed, seed_len) || |
| 898 | !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len, |
| 899 | info, info_len)) { |
| 900 | EVP_HPKE_CTX_cleanup(ctx); |
| 901 | return 0; |
| 902 | } |
| 903 | return 1; |
| 904 | } |
| 905 |
| 906 | int EVP_HPKE_CTX_setup_recipient(EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, |
| 907 | const EVP_HPKE_KDF *kdf, |
| 908 | const EVP_HPKE_AEAD *aead, const uint8_t *enc, |
| 909 | size_t enc_len, const uint8_t *info, |
| 910 | size_t info_len) { |
| 911 | EVP_HPKE_CTX_zero(ctx); |
| 912 | ctx->is_sender = 0; |
| 913 | ctx->kem = key->kem; |
| 914 | ctx->kdf = kdf; |
| 915 | ctx->aead = aead; |
| 916 | uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; |
| 917 | size_t shared_secret_len; |
| 918 | if (!key->kem->decap(key, shared_secret, &shared_secret_len, enc, enc_len) || |
| 919 | !hpke_key_schedule(ctx, HPKE_MODE_BASE, shared_secret, shared_secret_len, |
| 920 | info, info_len)) { |
| 921 | EVP_HPKE_CTX_cleanup(ctx); |
| 922 | return 0; |
| 923 | } |
| 924 | return 1; |
| 925 | } |
| 926 |
| 927 |
| 928 | int EVP_HPKE_CTX_setup_auth_sender( |
| 929 | EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, |
| 930 | const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, |
| 931 | const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 932 | const uint8_t *info, size_t info_len) { |
| 933 | uint8_t seed[MAX_SEED_LEN]; |
| 934 | RAND_bytes(seed, key->kem->seed_len); |
| 935 | return EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( |
| 936 | ctx, out_enc, out_enc_len, max_enc, key, kdf, aead, peer_public_key, |
| 937 | peer_public_key_len, info, info_len, seed, key->kem->seed_len); |
| 938 | } |
| 939 |
| 940 | int EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( |
| 941 | EVP_HPKE_CTX *ctx, uint8_t *out_enc, size_t *out_enc_len, size_t max_enc, |
| 942 | const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, const EVP_HPKE_AEAD *aead, |
| 943 | const uint8_t *peer_public_key, size_t peer_public_key_len, |
| 944 | const uint8_t *info, size_t info_len, const uint8_t *seed, |
| 945 | size_t seed_len) { |
| 946 | if (key->kem->auth_encap_with_seed == NULL) { |
| 947 | // Not all HPKE KEMs support AuthEncap. |
| 948 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 949 | return 0; |
| 950 | } |
| 951 |
| 952 | EVP_HPKE_CTX_zero(ctx); |
| 953 | ctx->is_sender = 1; |
| 954 | ctx->kem = key->kem; |
| 955 | ctx->kdf = kdf; |
| 956 | ctx->aead = aead; |
| 957 | uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; |
| 958 | size_t shared_secret_len; |
| 959 | if (!key->kem->auth_encap_with_seed( |
| 960 | key, shared_secret, &shared_secret_len, out_enc, out_enc_len, max_enc, |
| 961 | peer_public_key, peer_public_key_len, seed, seed_len) || |
| 962 | !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len, |
| 963 | info, info_len)) { |
| 964 | EVP_HPKE_CTX_cleanup(ctx); |
| 965 | return 0; |
| 966 | } |
| 967 | return 1; |
| 968 | } |
| 969 |
| 970 | int EVP_HPKE_CTX_setup_auth_recipient( |
| 971 | EVP_HPKE_CTX *ctx, const EVP_HPKE_KEY *key, const EVP_HPKE_KDF *kdf, |
| 972 | const EVP_HPKE_AEAD *aead, const uint8_t *enc, size_t enc_len, |
| 973 | const uint8_t *info, size_t info_len, const uint8_t *peer_public_key, |
| 974 | size_t peer_public_key_len) { |
| 975 | if (key->kem->auth_decap == NULL) { |
| 976 | // Not all HPKE KEMs support AuthDecap. |
| 977 | OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
| 978 | return 0; |
| 979 | } |
| 980 |
| 981 | EVP_HPKE_CTX_zero(ctx); |
| 982 | ctx->is_sender = 0; |
| 983 | ctx->kem = key->kem; |
| 984 | ctx->kdf = kdf; |
| 985 | ctx->aead = aead; |
| 986 | uint8_t shared_secret[MAX_SHARED_SECRET_LEN]; |
| 987 | size_t shared_secret_len; |
| 988 | if (!key->kem->auth_decap(key, shared_secret, &shared_secret_len, enc, |
| 989 | enc_len, peer_public_key, peer_public_key_len) || |
| 990 | !hpke_key_schedule(ctx, HPKE_MODE_AUTH, shared_secret, shared_secret_len, |
| 991 | info, info_len)) { |
| 992 | EVP_HPKE_CTX_cleanup(ctx); |
| 993 | return 0; |
| 994 | } |
| 995 | return 1; |
| 996 | } |
| 997 |
| 998 | static void hpke_nonce(const EVP_HPKE_CTX *ctx, uint8_t *out_nonce, |
| 999 | size_t nonce_len) { |
| 1000 | assert(nonce_len >= 8); |
| 1001 |
| 1002 | // Write padded big-endian bytes of |ctx->seq| to |out_nonce|. |
| 1003 | OPENSSL_memset(out_nonce, 0, nonce_len); |
| 1004 | uint64_t seq_copy = ctx->seq; |
| 1005 | for (size_t i = 0; i < 8; i++) { |
| 1006 | out_nonce[nonce_len - i - 1] = seq_copy & 0xff; |
| 1007 | seq_copy >>= 8; |
| 1008 | } |
| 1009 |
| 1010 | // XOR the encoded sequence with the |ctx->base_nonce|. |
| 1011 | for (size_t i = 0; i < nonce_len; i++) { |
| 1012 | out_nonce[i] ^= ctx->base_nonce[i]; |
| 1013 | } |
| 1014 | } |
| 1015 |
| 1016 | int EVP_HPKE_CTX_open(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len, |
| 1017 | size_t max_out_len, const uint8_t *in, size_t in_len, |
| 1018 | const uint8_t *ad, size_t ad_len) { |
| 1019 | if (ctx->is_sender) { |
| 1020 | OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | if (ctx->seq == UINT64_MAX) { |
| 1024 | OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW); |
| 1025 | return 0; |
| 1026 | } |
| 1027 |
| 1028 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 1029 | const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead); |
| 1030 | hpke_nonce(ctx, nonce, nonce_len); |
| 1031 |
| 1032 | if (!EVP_AEAD_CTX_open(&ctx->aead_ctx, out, out_len, max_out_len, nonce, |
| 1033 | nonce_len, in, in_len, ad, ad_len)) { |
| 1034 | return 0; |
| 1035 | } |
| 1036 | ctx->seq++; |
| 1037 | return 1; |
| 1038 | } |
| 1039 |
| 1040 | int EVP_HPKE_CTX_seal(EVP_HPKE_CTX *ctx, uint8_t *out, size_t *out_len, |
| 1041 | size_t max_out_len, const uint8_t *in, size_t in_len, |
| 1042 | const uint8_t *ad, size_t ad_len) { |
| 1043 | if (!ctx->is_sender) { |
| 1044 | OPENSSL_PUT_ERROR(EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1045 | return 0; |
| 1046 | } |
| 1047 | if (ctx->seq == UINT64_MAX) { |
| 1048 | OPENSSL_PUT_ERROR(EVP, ERR_R_OVERFLOW); |
| 1049 | return 0; |
| 1050 | } |
| 1051 |
| 1052 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 1053 | const size_t nonce_len = EVP_AEAD_nonce_length(ctx->aead_ctx.aead); |
| 1054 | hpke_nonce(ctx, nonce, nonce_len); |
| 1055 |
| 1056 | if (!EVP_AEAD_CTX_seal(&ctx->aead_ctx, out, out_len, max_out_len, nonce, |
| 1057 | nonce_len, in, in_len, ad, ad_len)) { |
| 1058 | return 0; |
| 1059 | } |
| 1060 | ctx->seq++; |
| 1061 | return 1; |
| 1062 | } |
| 1063 |
| 1064 | int EVP_HPKE_CTX_export(const EVP_HPKE_CTX *ctx, uint8_t *out, |
| 1065 | size_t secret_len, const uint8_t *context, |
| 1066 | size_t context_len) { |
| 1067 | uint8_t suite_id[HPKE_SUITE_ID_LEN]; |
| 1068 | if (!hpke_build_suite_id(ctx, suite_id)) { |
| 1069 | return 0; |
| 1070 | } |
| 1071 | const EVP_MD *hkdf_md = ctx->kdf->hkdf_md_func(); |
| 1072 | if (!hpke_labeled_expand(hkdf_md, out, secret_len, ctx->exporter_secret, |
| 1073 | EVP_MD_size(hkdf_md), suite_id, sizeof(suite_id), |
| 1074 | "sec", context, context_len)) { |
| 1075 | return 0; |
| 1076 | } |
| 1077 | return 1; |
| 1078 | } |
| 1079 |
| 1080 | size_t EVP_HPKE_CTX_max_overhead(const EVP_HPKE_CTX *ctx) { |
| 1081 | assert(ctx->is_sender); |
| 1082 | return EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(&ctx->aead_ctx)); |
| 1083 | } |
| 1084 |
| 1085 | const EVP_HPKE_KEM *EVP_HPKE_CTX_kem(const EVP_HPKE_CTX *ctx) { |
| 1086 | return ctx->kem; |
| 1087 | } |
| 1088 |
| 1089 | const EVP_HPKE_AEAD *EVP_HPKE_CTX_aead(const EVP_HPKE_CTX *ctx) { |
| 1090 | return ctx->aead; |
| 1091 | } |
| 1092 |
| 1093 | const EVP_HPKE_KDF *EVP_HPKE_CTX_kdf(const EVP_HPKE_CTX *ctx) { |
| 1094 | return ctx->kdf; |
| 1095 | } |
| 1096 |