| 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 <cstdint> |
| 18 | #include <limits> |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 |
| 22 | #include <gtest/gtest.h> |
| 23 |
| 24 | #include <openssl/base.h> |
| 25 | #include <openssl/curve25519.h> |
| 26 | #include <openssl/digest.h> |
| 27 | #include <openssl/err.h> |
| 28 | #include <openssl/evp.h> |
| 29 | #include <openssl/rand.h> |
| 30 | #include <openssl/sha.h> |
| 31 | #include <openssl/span.h> |
| 32 |
| 33 | #include "../test/file_test.h" |
| 34 | #include "../test/test_util.h" |
| 35 |
| 36 |
| 37 | BSSL_NAMESPACE_BEGIN |
| 38 | namespace { |
| 39 |
| 40 | const decltype(&EVP_hpke_x25519_hkdf_sha256) kAllKEMs[] = { |
| 41 | &EVP_hpke_p256_hkdf_sha256, |
| 42 | &EVP_hpke_x25519_hkdf_sha256, |
| 43 | }; |
| 44 |
| 45 | const decltype(&EVP_hpke_aes_128_gcm) kAllAEADs[] = { |
| 46 | &EVP_hpke_aes_128_gcm, |
| 47 | &EVP_hpke_aes_256_gcm, |
| 48 | &EVP_hpke_chacha20_poly1305, |
| 49 | }; |
| 50 |
| 51 | const decltype(&EVP_hpke_hkdf_sha256) kAllKDFs[] = { |
| 52 | &EVP_hpke_hkdf_sha256, |
| 53 | }; |
| 54 |
| 55 | // HPKETestVector corresponds to one array member in the published |
| 56 | // test-vectors.json. |
| 57 | class HPKETestVector { |
| 58 | public: |
| 59 | explicit HPKETestVector() = default; |
| 60 | ~HPKETestVector() = default; |
| 61 |
| 62 | bool ReadFromFileTest(FileTest *t); |
| 63 |
| 64 | void Verify() const { |
| 65 | const EVP_HPKE_KEM *kem = GetKEM(); |
| 66 | const EVP_HPKE_AEAD *aead = GetAEAD(); |
| 67 | ASSERT_TRUE(aead); |
| 68 | const EVP_HPKE_KDF *kdf = GetKDF(); |
| 69 | ASSERT_TRUE(kdf); |
| 70 |
| 71 | // Test the sender. |
| 72 | ScopedEVP_HPKE_CTX sender_ctx; |
| 73 | uint8_t enc[EVP_HPKE_MAX_ENC_LENGTH]; |
| 74 | size_t enc_len = 0; |
| 75 |
| 76 | // X25519 uses the secret key directly. P-256 uses the IKM to derive a key. |
| 77 | bssl::Span<const uint8_t> secret_input = secret_key_e_; |
| 78 | if (kem_id_ == EVP_HPKE_DHKEM_P256_HKDF_SHA256) { |
| 79 | secret_input = ikm_e_; |
| 80 | } |
| 81 |
| 82 | switch (mode_) { |
| 83 | case Mode::kBase: |
| 84 | ASSERT_TRUE(EVP_HPKE_CTX_setup_sender_with_seed_for_testing( |
| 85 | sender_ctx.get(), enc, &enc_len, sizeof(enc), kem, kdf, aead, |
| 86 | public_key_r_.data(), public_key_r_.size(), info_.data(), |
| 87 | info_.size(), secret_input.data(), secret_input.size())); |
| 88 | break; |
| 89 | case Mode::kAuth: { |
| 90 | ScopedEVP_HPKE_KEY sender_key; |
| 91 | ASSERT_TRUE(EVP_HPKE_KEY_init( |
| 92 | sender_key.get(), kem, secret_key_s_.data(), secret_key_s_.size())); |
| 93 | ASSERT_TRUE(EVP_HPKE_CTX_setup_auth_sender_with_seed_for_testing( |
| 94 | sender_ctx.get(), enc, &enc_len, sizeof(enc), sender_key.get(), kdf, |
| 95 | aead, public_key_r_.data(), public_key_r_.size(), info_.data(), |
| 96 | info_.size(), secret_input.data(), secret_input.size())); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 |
| 101 | EXPECT_EQ(Bytes(enc, enc_len), Bytes(public_key_e_)); |
| 102 | VerifySender(sender_ctx.get()); |
| 103 |
| 104 | // Test the recipient. |
| 105 | ScopedEVP_HPKE_KEY base_key; |
| 106 | ASSERT_TRUE(EVP_HPKE_KEY_init(base_key.get(), kem, secret_key_r_.data(), |
| 107 | secret_key_r_.size())); |
| 108 |
| 109 | enum class CopyMode { kOriginal, kCopy, kMove }; |
| 110 | for (CopyMode copy : |
| 111 | {CopyMode::kOriginal, CopyMode::kCopy, CopyMode::kMove}) { |
| 112 | SCOPED_TRACE(static_cast<int>(copy)); |
| 113 | const EVP_HPKE_KEY *key = base_key.get(); |
| 114 | ScopedEVP_HPKE_KEY key_copy; |
| 115 | switch (copy) { |
| 116 | case CopyMode::kOriginal: |
| 117 | break; |
| 118 | case CopyMode::kCopy: |
| 119 | ASSERT_TRUE(EVP_HPKE_KEY_copy(key_copy.get(), base_key.get())); |
| 120 | key = key_copy.get(); |
| 121 | break; |
| 122 | case CopyMode::kMove: |
| 123 | EVP_HPKE_KEY_move(key_copy.get(), base_key.get()); |
| 124 | key = key_copy.get(); |
| 125 | break; |
| 126 | } |
| 127 |
| 128 | uint8_t public_key[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 129 | size_t public_key_len; |
| 130 | ASSERT_TRUE(EVP_HPKE_KEY_public_key(key, public_key, &public_key_len, |
| 131 | sizeof(public_key))); |
| 132 | EXPECT_EQ(Bytes(public_key, public_key_len), Bytes(public_key_r_)); |
| 133 |
| 134 | uint8_t private_key[EVP_HPKE_MAX_PRIVATE_KEY_LENGTH]; |
| 135 | size_t private_key_len; |
| 136 | ASSERT_TRUE(EVP_HPKE_KEY_private_key(key, private_key, &private_key_len, |
| 137 | sizeof(private_key))); |
| 138 | EXPECT_EQ(Bytes(private_key, private_key_len), Bytes(secret_key_r_)); |
| 139 |
| 140 | // Set up the recipient. |
| 141 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 142 | switch (mode_) { |
| 143 | case Mode::kBase: |
| 144 | ASSERT_TRUE(EVP_HPKE_CTX_setup_recipient(recipient_ctx.get(), key, |
| 145 | kdf, aead, enc, enc_len, |
| 146 | info_.data(), info_.size())); |
| 147 | break; |
| 148 | case Mode::kAuth: |
| 149 | ASSERT_TRUE(EVP_HPKE_CTX_setup_auth_recipient( |
| 150 | recipient_ctx.get(), key, kdf, aead, enc, enc_len, info_.data(), |
| 151 | info_.size(), public_key_s_.data(), public_key_s_.size())); |
| 152 | break; |
| 153 | } |
| 154 |
| 155 | VerifyRecipient(recipient_ctx.get()); |
| 156 | } |
| 157 | } |
| 158 |
| 159 | private: |
| 160 | const EVP_HPKE_KEM *GetKEM() const { |
| 161 | for (const auto kem : kAllKEMs) { |
| 162 | if (EVP_HPKE_KEM_id(kem()) == kem_id_) { |
| 163 | return kem(); |
| 164 | } |
| 165 | } |
| 166 | return nullptr; |
| 167 | } |
| 168 |
| 169 | const EVP_HPKE_AEAD *GetAEAD() const { |
| 170 | for (const auto aead : kAllAEADs) { |
| 171 | if (EVP_HPKE_AEAD_id(aead()) == aead_id_) { |
| 172 | return aead(); |
| 173 | } |
| 174 | } |
| 175 | return nullptr; |
| 176 | } |
| 177 |
| 178 | const EVP_HPKE_KDF *GetKDF() const { |
| 179 | for (const auto kdf : kAllKDFs) { |
| 180 | if (EVP_HPKE_KDF_id(kdf()) == kdf_id_) { |
| 181 | return kdf(); |
| 182 | } |
| 183 | } |
| 184 | return nullptr; |
| 185 | } |
| 186 |
| 187 | void VerifySender(EVP_HPKE_CTX *ctx) const { |
| 188 | for (const Encryption &task : encryptions_) { |
| 189 | std::vector<uint8_t> encrypted(task.plaintext.size() + |
| 190 | EVP_HPKE_CTX_max_overhead(ctx)); |
| 191 | size_t encrypted_len; |
| 192 | ASSERT_TRUE(EVP_HPKE_CTX_seal(ctx, encrypted.data(), &encrypted_len, |
| 193 | encrypted.size(), task.plaintext.data(), |
| 194 | task.plaintext.size(), task.aad.data(), |
| 195 | task.aad.size())); |
| 196 |
| 197 | ASSERT_EQ(Bytes(encrypted.data(), encrypted_len), Bytes(task.ciphertext)); |
| 198 | } |
| 199 | VerifyExports(ctx); |
| 200 | } |
| 201 |
| 202 | void VerifyRecipient(EVP_HPKE_CTX *ctx) const { |
| 203 | for (const Encryption &task : encryptions_) { |
| 204 | std::vector<uint8_t> decrypted(task.ciphertext.size()); |
| 205 | size_t decrypted_len; |
| 206 | ASSERT_TRUE(EVP_HPKE_CTX_open(ctx, decrypted.data(), &decrypted_len, |
| 207 | decrypted.size(), task.ciphertext.data(), |
| 208 | task.ciphertext.size(), task.aad.data(), |
| 209 | task.aad.size())); |
| 210 |
| 211 | ASSERT_EQ(Bytes(decrypted.data(), decrypted_len), Bytes(task.plaintext)); |
| 212 | } |
| 213 | VerifyExports(ctx); |
| 214 | } |
| 215 |
| 216 | void VerifyExports(EVP_HPKE_CTX *ctx) const { |
| 217 | for (const Export &task : exports_) { |
| 218 | std::vector<uint8_t> exported_secret(task.export_length); |
| 219 |
| 220 | ASSERT_TRUE(EVP_HPKE_CTX_export( |
| 221 | ctx, exported_secret.data(), exported_secret.size(), |
| 222 | task.exporter_context.data(), task.exporter_context.size())); |
| 223 | ASSERT_EQ(Bytes(exported_secret), Bytes(task.exported_value)); |
| 224 | } |
| 225 | } |
| 226 |
| 227 | enum class Mode { |
| 228 | kBase = 0, |
| 229 | kAuth = 2, |
| 230 | }; |
| 231 |
| 232 | struct Encryption { |
| 233 | std::vector<uint8_t> aad; |
| 234 | std::vector<uint8_t> ciphertext; |
| 235 | std::vector<uint8_t> plaintext; |
| 236 | }; |
| 237 |
| 238 | struct Export { |
| 239 | std::vector<uint8_t> exporter_context; |
| 240 | size_t export_length; |
| 241 | std::vector<uint8_t> exported_value; |
| 242 | }; |
| 243 |
| 244 | Mode mode_; |
| 245 | uint16_t kem_id_; |
| 246 | uint16_t kdf_id_; |
| 247 | uint16_t aead_id_; |
| 248 | std::vector<uint8_t> context_; |
| 249 | std::vector<uint8_t> info_; |
| 250 | std::vector<uint8_t> public_key_e_; |
| 251 | std::vector<uint8_t> secret_key_e_; |
| 252 | std::vector<uint8_t> ikm_e_; |
| 253 | std::vector<uint8_t> public_key_r_; |
| 254 | std::vector<uint8_t> secret_key_r_; |
| 255 | std::vector<uint8_t> ikm_r_; |
| 256 | std::vector<uint8_t> public_key_s_; |
| 257 | std::vector<uint8_t> secret_key_s_; |
| 258 | std::vector<Encryption> encryptions_; |
| 259 | std::vector<Export> exports_; |
| 260 | }; |
| 261 |
| 262 | // Match FileTest's naming scheme for duplicated attribute names. |
| 263 | std::string BuildAttrName(const std::string &name, int iter) { |
| 264 | return iter == 1 ? name : name + "/" + std::to_string(iter); |
| 265 | } |
| 266 |
| 267 | // Parses |s| as an unsigned integer of type T and writes the value to |out|. |
| 268 | // Returns true on success. If the integer value exceeds the maximum T value, |
| 269 | // returns false. |
| 270 | template <typename T> |
| 271 | bool ParseIntSafe(T *out, const std::string &s) { |
| 272 | T value = 0; |
| 273 | for (char c : s) { |
| 274 | if (c < '0' || c > '9') { |
| 275 | return false; |
| 276 | } |
| 277 | if (value > (std::numeric_limits<T>::max() - (c - '0')) / 10) { |
| 278 | return false; |
| 279 | } |
| 280 | value = 10 * value + (c - '0'); |
| 281 | } |
| 282 | *out = value; |
| 283 | return true; |
| 284 | } |
| 285 |
| 286 | // Read the |key| attribute from |file_test| and convert it to an integer. |
| 287 | template <typename T> |
| 288 | bool FileTestReadInt(FileTest *file_test, T *out, const std::string &key) { |
| 289 | std::string s; |
| 290 | return file_test->GetAttribute(&s, key) && ParseIntSafe(out, s); |
| 291 | } |
| 292 |
| 293 |
| 294 | bool HPKETestVector::ReadFromFileTest(FileTest *t) { |
| 295 | uint8_t mode = 0; |
| 296 | if (!FileTestReadInt(t, &mode, "mode") || |
| 297 | !FileTestReadInt(t, &kem_id_, "kem_id") || |
| 298 | !FileTestReadInt(t, &kdf_id_, "kdf_id") || |
| 299 | !FileTestReadInt(t, &aead_id_, "aead_id") || |
| 300 | !t->GetBytes(&info_, "info") || // |
| 301 | !t->GetBytes(&secret_key_r_, "skRm") || |
| 302 | !t->GetBytes(&public_key_r_, "pkRm") || |
| 303 | !t->GetBytes(&ikm_r_, "ikmR") || // |
| 304 | !t->GetBytes(&secret_key_e_, "skEm") || |
| 305 | !t->GetBytes(&public_key_e_, "pkEm") || // |
| 306 | !t->GetBytes(&ikm_e_, "ikmE")) { |
| 307 | return false; |
| 308 | } |
| 309 |
| 310 | switch (mode) { |
| 311 | case static_cast<int>(Mode::kBase): |
| 312 | mode_ = Mode::kBase; |
| 313 | break; |
| 314 | case static_cast<int>(Mode::kAuth): |
| 315 | mode_ = Mode::kAuth; |
| 316 | if (!t->GetBytes(&secret_key_s_, "skSm") || |
| 317 | !t->GetBytes(&public_key_s_, "pkSm")) { |
| 318 | return false; |
| 319 | } |
| 320 | break; |
| 321 | default: |
| 322 | return false; |
| 323 | } |
| 324 |
| 325 | for (int i = 1; t->HasAttribute(BuildAttrName("aad", i)); i++) { |
| 326 | Encryption encryption; |
| 327 | if (!t->GetBytes(&encryption.aad, BuildAttrName("aad", i)) || |
| 328 | !t->GetBytes(&encryption.ciphertext, BuildAttrName("ct", i)) || |
| 329 | !t->GetBytes(&encryption.plaintext, BuildAttrName("pt", i))) { |
| 330 | return false; |
| 331 | } |
| 332 | encryptions_.push_back(std::move(encryption)); |
| 333 | } |
| 334 |
| 335 | for (int i = 1; t->HasAttribute(BuildAttrName("exporter_context", i)); i++) { |
| 336 | Export exp; |
| 337 | if (!t->GetBytes(&exp.exporter_context, |
| 338 | BuildAttrName("exporter_context", i)) || |
| 339 | !FileTestReadInt(t, &exp.export_length, BuildAttrName("L", i)) || |
| 340 | !t->GetBytes(&exp.exported_value, BuildAttrName("exported_value", i))) { |
| 341 | return false; |
| 342 | } |
| 343 | exports_.push_back(std::move(exp)); |
| 344 | } |
| 345 | return true; |
| 346 | } |
| 347 |
| 348 | } // namespace |
| 349 |
| 350 | TEST(HPKETest, VerifyTestVectors) { |
| 351 | FileTestGTest("crypto/hpke/hpke_test_vectors.txt", [](FileTest *t) { |
| 352 | HPKETestVector test_vec; |
| 353 | EXPECT_TRUE(test_vec.ReadFromFileTest(t)); |
| 354 | test_vec.Verify(); |
| 355 | }); |
| 356 | } |
| 357 |
| 358 | // The test vectors used fixed sender ephemeral keys, while HPKE itself |
| 359 | // generates new keys for each context. Test this codepath by checking we can |
| 360 | // decrypt our own messages. |
| 361 | TEST(HPKETest, RoundTrip) { |
| 362 | const uint8_t info_a[] = {1, 1, 2, 3, 5, 8}; |
| 363 | const uint8_t info_b[] = {42, 42, 42}; |
| 364 | const uint8_t ad_a[] = {1, 2, 4, 8, 16}; |
| 365 | const uint8_t ad_b[] = {7}; |
| 366 | Span<const uint8_t> info_values[] = {{nullptr, 0}, info_a, info_b}; |
| 367 | Span<const uint8_t> ad_values[] = {{nullptr, 0}, ad_a, ad_b}; |
| 368 |
| 369 | for (const auto kem : kAllKEMs) { |
| 370 | SCOPED_TRACE(EVP_HPKE_KEM_id(kem())); |
| 371 |
| 372 | // Generate the recipient's keypair. |
| 373 | ScopedEVP_HPKE_KEY key; |
| 374 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), kem())); |
| 375 | uint8_t public_key_r[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 376 | size_t public_key_r_len; |
| 377 | ASSERT_TRUE(EVP_HPKE_KEY_public_key( |
| 378 | key.get(), public_key_r, &public_key_r_len, sizeof(public_key_r))); |
| 379 |
| 380 | // Generate the sender's keypair, for auth modes. |
| 381 | ScopedEVP_HPKE_KEY sender_key; |
| 382 | ASSERT_TRUE(EVP_HPKE_KEY_generate(sender_key.get(), kem())); |
| 383 | uint8_t public_key_s[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 384 | size_t public_key_s_len; |
| 385 | ASSERT_TRUE(EVP_HPKE_KEY_public_key(sender_key.get(), public_key_s, |
| 386 | &public_key_s_len, |
| 387 | sizeof(public_key_s))); |
| 388 |
| 389 | for (const auto kdf : kAllKDFs) { |
| 390 | SCOPED_TRACE(EVP_HPKE_KDF_id(kdf())); |
| 391 | for (const auto aead : kAllAEADs) { |
| 392 | SCOPED_TRACE(EVP_HPKE_AEAD_id(aead())); |
| 393 | for (const Span<const uint8_t> &info : info_values) { |
| 394 | SCOPED_TRACE(Bytes(info)); |
| 395 | for (const Span<const uint8_t> &ad : ad_values) { |
| 396 | SCOPED_TRACE(Bytes(ad)); |
| 397 |
| 398 | auto check_messages = [&](EVP_HPKE_CTX *sender_ctx, |
| 399 | EVP_HPKE_CTX *recipient_ctx) { |
| 400 | const char kCleartextPayload[] = "foobar"; |
| 401 |
| 402 | // Have sender encrypt message for the recipient. |
| 403 | std::vector<uint8_t> ciphertext( |
| 404 | sizeof(kCleartextPayload) + |
| 405 | EVP_HPKE_CTX_max_overhead(sender_ctx)); |
| 406 | size_t ciphertext_len; |
| 407 | ASSERT_TRUE(EVP_HPKE_CTX_seal( |
| 408 | sender_ctx, ciphertext.data(), &ciphertext_len, |
| 409 | ciphertext.size(), |
| 410 | reinterpret_cast<const uint8_t *>(kCleartextPayload), |
| 411 | sizeof(kCleartextPayload), ad.data(), ad.size())); |
| 412 |
| 413 | // Have recipient decrypt the message. |
| 414 | std::vector<uint8_t> cleartext(ciphertext.size()); |
| 415 | size_t cleartext_len; |
| 416 | ASSERT_TRUE(EVP_HPKE_CTX_open(recipient_ctx, cleartext.data(), |
| 417 | &cleartext_len, cleartext.size(), |
| 418 | ciphertext.data(), ciphertext_len, |
| 419 | ad.data(), ad.size())); |
| 420 |
| 421 | // Verify that decrypted message matches the original. |
| 422 | ASSERT_EQ(Bytes(cleartext.data(), cleartext_len), |
| 423 | Bytes(kCleartextPayload, sizeof(kCleartextPayload))); |
| 424 | }; |
| 425 |
| 426 | // Test the base mode. |
| 427 | { |
| 428 | ScopedEVP_HPKE_CTX sender_ctx; |
| 429 | uint8_t enc[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 430 | size_t enc_len; |
| 431 | ASSERT_TRUE(EVP_HPKE_CTX_setup_sender( |
| 432 | sender_ctx.get(), enc, &enc_len, sizeof(enc), kem(), kdf(), |
| 433 | aead(), public_key_r, public_key_r_len, info.data(), |
| 434 | info.size())); |
| 435 |
| 436 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 437 | ASSERT_TRUE(EVP_HPKE_CTX_setup_recipient( |
| 438 | recipient_ctx.get(), key.get(), kdf(), aead(), enc, enc_len, |
| 439 | info.data(), info.size())); |
| 440 |
| 441 | check_messages(sender_ctx.get(), recipient_ctx.get()); |
| 442 | } |
| 443 |
| 444 | // Test the auth mode. |
| 445 | { |
| 446 | ScopedEVP_HPKE_CTX sender_ctx; |
| 447 | uint8_t enc[EVP_HPKE_MAX_PUBLIC_KEY_LENGTH]; |
| 448 | size_t enc_len; |
| 449 | ASSERT_TRUE(EVP_HPKE_CTX_setup_auth_sender( |
| 450 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 451 | sender_key.get(), kdf(), aead(), public_key_r, |
| 452 | public_key_r_len, info.data(), info.size())); |
| 453 |
| 454 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 455 | ASSERT_TRUE(EVP_HPKE_CTX_setup_auth_recipient( |
| 456 | recipient_ctx.get(), key.get(), kdf(), aead(), enc, enc_len, |
| 457 | info.data(), info.size(), public_key_s, public_key_s_len)); |
| 458 |
| 459 | check_messages(sender_ctx.get(), recipient_ctx.get()); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 |
| 468 | // Verify that the DH operations inside Encap() and Decap() both fail when the |
| 469 | // public key is on a small-order point in the curve. |
| 470 | TEST(HPKETest, X25519EncapSmallOrderPoint) { |
| 471 | // Borrowed from X25519Test.SmallOrder. |
| 472 | static const uint8_t kSmallOrderPoint[32] = { |
| 473 | 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3, |
| 474 | 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, |
| 475 | 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, |
| 476 | }; |
| 477 | static const uint8_t kValidPoint[32] = { |
| 478 | 0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94, 0xc1, |
| 479 | 0xa4, 0x24, 0xb1, 0x5f, 0x7c, 0x72, 0x66, 0x24, 0xec, 0x26, 0xb3, |
| 480 | 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, 0xd0, 0xab, 0x1c, 0x4c, |
| 481 | }; |
| 482 |
| 483 | ScopedEVP_HPKE_KEY key; |
| 484 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 485 |
| 486 | for (const auto kdf : kAllKDFs) { |
| 487 | SCOPED_TRACE(EVP_HPKE_KDF_id(kdf())); |
| 488 | for (const auto aead : kAllAEADs) { |
| 489 | SCOPED_TRACE(EVP_HPKE_AEAD_id(aead())); |
| 490 | // Set up the sender, passing in kSmallOrderPoint as |peer_public_key|. |
| 491 | ScopedEVP_HPKE_CTX sender_ctx; |
| 492 | uint8_t enc[X25519_PUBLIC_VALUE_LEN]; |
| 493 | size_t enc_len; |
| 494 | EXPECT_FALSE(EVP_HPKE_CTX_setup_sender( |
| 495 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 496 | EVP_hpke_x25519_hkdf_sha256(), kdf(), aead(), kSmallOrderPoint, |
| 497 | sizeof(kSmallOrderPoint), nullptr, 0)); |
| 498 |
| 499 | // Likewise with auth. |
| 500 | EXPECT_FALSE(EVP_HPKE_CTX_setup_auth_sender( |
| 501 | sender_ctx.get(), enc, &enc_len, sizeof(enc), key.get(), kdf(), |
| 502 | aead(), kSmallOrderPoint, sizeof(kSmallOrderPoint), nullptr, 0)); |
| 503 |
| 504 | // Set up the recipient, passing in kSmallOrderPoint as |enc|. |
| 505 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 506 | EXPECT_FALSE(EVP_HPKE_CTX_setup_recipient( |
| 507 | recipient_ctx.get(), key.get(), kdf(), aead(), kSmallOrderPoint, |
| 508 | sizeof(kSmallOrderPoint), nullptr, 0)); |
| 509 |
| 510 | // Likewise with auth. With auth, a small-order point could appear as |
| 511 | // either |enc| or the peer public key. |
| 512 | EXPECT_FALSE(EVP_HPKE_CTX_setup_auth_recipient( |
| 513 | recipient_ctx.get(), key.get(), kdf(), aead(), kSmallOrderPoint, |
| 514 | sizeof(kSmallOrderPoint), nullptr, 0, kValidPoint, |
| 515 | sizeof(kValidPoint))); |
| 516 | EXPECT_FALSE(EVP_HPKE_CTX_setup_auth_recipient( |
| 517 | recipient_ctx.get(), key.get(), kdf(), aead(), kValidPoint, |
| 518 | sizeof(kValidPoint), nullptr, 0, kSmallOrderPoint, |
| 519 | sizeof(kSmallOrderPoint))); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 |
| 524 | // Test that Seal() fails when the context has been initialized as a recipient. |
| 525 | TEST(HPKETest, RecipientInvalidSeal) { |
| 526 | const uint8_t kMockEnc[X25519_PUBLIC_VALUE_LEN] = {0xff}; |
| 527 | const char kCleartextPayload[] = "foobar"; |
| 528 |
| 529 | ScopedEVP_HPKE_KEY key; |
| 530 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 531 |
| 532 | // Set up the recipient. |
| 533 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 534 | ASSERT_TRUE(EVP_HPKE_CTX_setup_recipient( |
| 535 | recipient_ctx.get(), key.get(), EVP_hpke_hkdf_sha256(), |
| 536 | EVP_hpke_aes_128_gcm(), kMockEnc, sizeof(kMockEnc), nullptr, 0)); |
| 537 |
| 538 | // Call Seal() on the recipient. |
| 539 | size_t ciphertext_len; |
| 540 | uint8_t ciphertext[100]; |
| 541 | ASSERT_FALSE(EVP_HPKE_CTX_seal( |
| 542 | recipient_ctx.get(), ciphertext, &ciphertext_len, sizeof(ciphertext), |
| 543 | reinterpret_cast<const uint8_t *>(kCleartextPayload), |
| 544 | sizeof(kCleartextPayload), nullptr, 0)); |
| 545 | } |
| 546 |
| 547 | // Test that Open() fails when the context has been initialized as a sender. |
| 548 | TEST(HPKETest, SenderInvalidOpen) { |
| 549 | const uint8_t kMockCiphertext[100] = {0xff}; |
| 550 | const size_t kMockCiphertextLen = 80; |
| 551 |
| 552 | // Generate the recipient's keypair. |
| 553 | uint8_t secret_key_r[X25519_PRIVATE_KEY_LEN]; |
| 554 | uint8_t public_key_r[X25519_PUBLIC_VALUE_LEN]; |
| 555 | X25519_keypair(public_key_r, secret_key_r); |
| 556 |
| 557 | // Set up the sender. |
| 558 | ScopedEVP_HPKE_CTX sender_ctx; |
| 559 | uint8_t enc[X25519_PUBLIC_VALUE_LEN]; |
| 560 | size_t enc_len; |
| 561 | ASSERT_TRUE(EVP_HPKE_CTX_setup_sender( |
| 562 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 563 | EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), |
| 564 | EVP_hpke_aes_128_gcm(), public_key_r, sizeof(public_key_r), nullptr, 0)); |
| 565 |
| 566 | // Call Open() on the sender. |
| 567 | uint8_t cleartext[128]; |
| 568 | size_t cleartext_len; |
| 569 | ASSERT_FALSE(EVP_HPKE_CTX_open(sender_ctx.get(), cleartext, &cleartext_len, |
| 570 | sizeof(cleartext), kMockCiphertext, |
| 571 | kMockCiphertextLen, nullptr, 0)); |
| 572 | } |
| 573 |
| 574 | TEST(HPKETest, SetupSenderBufferTooSmall) { |
| 575 | uint8_t secret_key_r[X25519_PRIVATE_KEY_LEN]; |
| 576 | uint8_t public_key_r[X25519_PUBLIC_VALUE_LEN]; |
| 577 | X25519_keypair(public_key_r, secret_key_r); |
| 578 |
| 579 | ScopedEVP_HPKE_CTX sender_ctx; |
| 580 | uint8_t enc[X25519_PUBLIC_VALUE_LEN - 1]; |
| 581 | size_t enc_len; |
| 582 | ASSERT_FALSE(EVP_HPKE_CTX_setup_sender( |
| 583 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 584 | EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), |
| 585 | EVP_hpke_aes_128_gcm(), public_key_r, sizeof(public_key_r), nullptr, 0)); |
| 586 | EXPECT_TRUE( |
| 587 | ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_BUFFER_SIZE)); |
| 588 | ERR_clear_error(); |
| 589 | } |
| 590 |
| 591 | TEST(HPKETest, SetupSenderBufferTooLarge) { |
| 592 | uint8_t secret_key_r[X25519_PRIVATE_KEY_LEN]; |
| 593 | uint8_t public_key_r[X25519_PUBLIC_VALUE_LEN]; |
| 594 | X25519_keypair(public_key_r, secret_key_r); |
| 595 |
| 596 | // Too large of an output buffer is fine because the function reports the |
| 597 | // actual length. |
| 598 | ScopedEVP_HPKE_CTX sender_ctx; |
| 599 | uint8_t enc[X25519_PUBLIC_VALUE_LEN + 1]; |
| 600 | size_t enc_len; |
| 601 | EXPECT_TRUE(EVP_HPKE_CTX_setup_sender( |
| 602 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 603 | EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), |
| 604 | EVP_hpke_aes_128_gcm(), public_key_r, sizeof(public_key_r), nullptr, 0)); |
| 605 | EXPECT_EQ(size_t{X25519_PUBLIC_VALUE_LEN}, enc_len); |
| 606 | } |
| 607 |
| 608 | TEST(HPKETest, SetupRecipientWrongLengthEnc) { |
| 609 | ScopedEVP_HPKE_KEY key; |
| 610 | ASSERT_TRUE(EVP_HPKE_KEY_generate(key.get(), EVP_hpke_x25519_hkdf_sha256())); |
| 611 |
| 612 | const uint8_t bogus_enc[X25519_PUBLIC_VALUE_LEN + 5] = {0xff}; |
| 613 |
| 614 | ScopedEVP_HPKE_CTX recipient_ctx; |
| 615 | ASSERT_FALSE(EVP_HPKE_CTX_setup_recipient( |
| 616 | recipient_ctx.get(), key.get(), EVP_hpke_hkdf_sha256(), |
| 617 | EVP_hpke_aes_128_gcm(), bogus_enc, sizeof(bogus_enc), nullptr, 0)); |
| 618 | EXPECT_TRUE( |
| 619 | ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_PEER_KEY)); |
| 620 | ERR_clear_error(); |
| 621 | } |
| 622 |
| 623 | TEST(HPKETest, SetupSenderWrongLengthPeerPublicValue) { |
| 624 | const uint8_t bogus_public_key_r[X25519_PRIVATE_KEY_LEN + 5] = {0xff}; |
| 625 | ScopedEVP_HPKE_CTX sender_ctx; |
| 626 | uint8_t enc[X25519_PUBLIC_VALUE_LEN]; |
| 627 | size_t enc_len; |
| 628 | ASSERT_FALSE(EVP_HPKE_CTX_setup_sender( |
| 629 | sender_ctx.get(), enc, &enc_len, sizeof(enc), |
| 630 | EVP_hpke_x25519_hkdf_sha256(), EVP_hpke_hkdf_sha256(), |
| 631 | EVP_hpke_aes_128_gcm(), bogus_public_key_r, sizeof(bogus_public_key_r), |
| 632 | nullptr, 0)); |
| 633 | EXPECT_TRUE( |
| 634 | ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_INVALID_PEER_KEY)); |
| 635 | ERR_clear_error(); |
| 636 | } |
| 637 |
| 638 | TEST(HPKETest, InvalidRecipientKey) { |
| 639 | const uint8_t private_key[X25519_PUBLIC_VALUE_LEN + 5] = {0xff}; |
| 640 | ScopedEVP_HPKE_KEY key; |
| 641 | EXPECT_FALSE(EVP_HPKE_KEY_init(key.get(), EVP_hpke_x25519_hkdf_sha256(), |
| 642 | private_key, sizeof(private_key))); |
| 643 | } |
| 644 |
| 645 | TEST(HPKETest, InvalidP256PrivateKey) { |
| 646 | const uint8_t zero_key[32] = {0}; |
| 647 | ScopedEVP_HPKE_KEY key; |
| 648 | EXPECT_FALSE(EVP_HPKE_KEY_init(key.get(), EVP_hpke_p256_hkdf_sha256(), |
| 649 | zero_key, sizeof(zero_key))); |
| 650 |
| 651 | uint8_t all_ones_key[32]; |
| 652 | OPENSSL_memset(all_ones_key, 0xff, sizeof(all_ones_key)); |
| 653 | EXPECT_FALSE(EVP_HPKE_KEY_init(key.get(), EVP_hpke_p256_hkdf_sha256(), |
| 654 | all_ones_key, sizeof(all_ones_key))); |
| 655 | } |
| 656 |
| 657 | TEST(HPKETest, InternalParseIntSafe) { |
| 658 | uint8_t u8 = 0xff; |
| 659 | ASSERT_FALSE(ParseIntSafe(&u8, "-1")); |
| 660 |
| 661 | ASSERT_TRUE(ParseIntSafe(&u8, "0")); |
| 662 | ASSERT_EQ(u8, 0); |
| 663 |
| 664 | ASSERT_TRUE(ParseIntSafe(&u8, "255")); |
| 665 | ASSERT_EQ(u8, 255); |
| 666 |
| 667 | ASSERT_FALSE(ParseIntSafe(&u8, "256")); |
| 668 |
| 669 | uint16_t u16 = 0xffff; |
| 670 | ASSERT_TRUE(ParseIntSafe(&u16, "257")); |
| 671 | ASSERT_EQ(u16, 257); |
| 672 |
| 673 | ASSERT_TRUE(ParseIntSafe(&u16, "65535")); |
| 674 | ASSERT_EQ(u16, 65535); |
| 675 |
| 676 | ASSERT_FALSE(ParseIntSafe(&u16, "65536")); |
| 677 | } |
| 678 |
| 679 | BSSL_NAMESPACE_END |
| 680 |