Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/hpke/hpke_test.cc

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