OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/crypto/chacha20_poly1305_encrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_encrypter.h" |
6 | 6 |
7 #include "net/quic/test_tools/quic_test_utils.h" | 7 #include "net/quic/test_tools/quic_test_utils.h" |
8 | 8 |
9 using base::StringPiece; | 9 using base::StringPiece; |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // in an nonce and also to allocate the buffer needed for the ciphertext. | 43 // in an nonce and also to allocate the buffer needed for the ciphertext. |
44 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, | 44 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, |
45 StringPiece nonce, | 45 StringPiece nonce, |
46 StringPiece associated_data, | 46 StringPiece associated_data, |
47 StringPiece plaintext) { | 47 StringPiece plaintext) { |
48 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | 48 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); |
49 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); | 49 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); |
50 | 50 |
51 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | 51 if (!encrypter->Encrypt(nonce, associated_data, plaintext, |
52 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 52 reinterpret_cast<unsigned char*>(ciphertext.get()))) { |
53 return NULL; | 53 return nullptr; |
54 } | 54 } |
55 | 55 |
56 return new QuicData(ciphertext.release(), ciphertext_size, true); | 56 return new QuicData(ciphertext.release(), ciphertext_size, true); |
57 } | 57 } |
58 | 58 |
59 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { | 59 TEST(ChaCha20Poly1305EncrypterTest, Encrypt) { |
60 if (!ChaCha20Poly1305Encrypter::IsSupported()) { | 60 if (!ChaCha20Poly1305Encrypter::IsSupported()) { |
61 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 61 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
62 return; | 62 return; |
63 } | 63 } |
64 | 64 |
65 for (size_t i = 0; test_vectors[i].key != NULL; i++) { | 65 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
66 // Decode the test vector. | 66 // Decode the test vector. |
67 string key; | 67 string key; |
68 string pt; | 68 string pt; |
69 string iv; | 69 string iv; |
70 string aad; | 70 string aad; |
71 string ct; | 71 string ct; |
72 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 72 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
73 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 73 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
74 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 74 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
75 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 75 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
76 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 76 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
77 | 77 |
78 ChaCha20Poly1305Encrypter encrypter; | 78 ChaCha20Poly1305Encrypter encrypter; |
79 ASSERT_TRUE(encrypter.SetKey(key)); | 79 ASSERT_TRUE(encrypter.SetKey(key)); |
80 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | 80 scoped_ptr<QuicData> encrypted(EncryptWithNonce( |
81 &encrypter, iv, | 81 &encrypter, iv, |
82 // This deliberately tests that the encrypter can handle an AAD that | 82 // This deliberately tests that the encrypter can handle an AAD that |
83 // is set to NULL, as opposed to a zero-length, non-NULL pointer. | 83 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
84 StringPiece(aad.length() ? aad.data() : NULL, aad.length()), pt)); | 84 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), pt)); |
85 ASSERT_TRUE(encrypted.get()); | 85 ASSERT_TRUE(encrypted.get()); |
86 | 86 |
87 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | 87 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), |
88 encrypted->length(), ct.data(), | 88 encrypted->length(), ct.data(), |
89 ct.length()); | 89 ct.length()); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { | 93 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { |
94 ChaCha20Poly1305Encrypter encrypter; | 94 ChaCha20Poly1305Encrypter encrypter; |
95 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | 95 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); |
96 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | 96 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); |
97 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | 97 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); |
98 } | 98 } |
99 | 99 |
100 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { | 100 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { |
101 ChaCha20Poly1305Encrypter encrypter; | 101 ChaCha20Poly1305Encrypter encrypter; |
102 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 102 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
103 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 103 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
104 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 104 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
105 } | 105 } |
106 | 106 |
107 } // namespace test | 107 } // namespace test |
108 } // namespace net | 108 } // namespace net |
OLD | NEW |