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_decrypter.h" | 5 #include "net/quic/crypto/chacha20_poly1305_decrypter.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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, | 73 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
74 StringPiece nonce, | 74 StringPiece nonce, |
75 StringPiece associated_data, | 75 StringPiece associated_data, |
76 StringPiece ciphertext) { | 76 StringPiece ciphertext) { |
77 size_t plaintext_size = ciphertext.length(); | 77 size_t plaintext_size = ciphertext.length(); |
78 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 78 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
79 | 79 |
80 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 80 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, |
81 reinterpret_cast<unsigned char*>(plaintext.get()), | 81 reinterpret_cast<unsigned char*>(plaintext.get()), |
82 &plaintext_size)) { | 82 &plaintext_size)) { |
83 return NULL; | 83 return nullptr; |
84 } | 84 } |
85 return new QuicData(plaintext.release(), plaintext_size, true); | 85 return new QuicData(plaintext.release(), plaintext_size, true); |
86 } | 86 } |
87 | 87 |
88 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { | 88 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
89 if (!ChaCha20Poly1305Decrypter::IsSupported()) { | 89 if (!ChaCha20Poly1305Decrypter::IsSupported()) { |
90 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 90 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
91 return; | 91 return; |
92 } | 92 } |
93 | 93 |
94 for (size_t i = 0; test_vectors[i].key != NULL; i++) { | 94 for (size_t i = 0; test_vectors[i].key != nullptr; i++) { |
95 // If not present then decryption is expected to fail. | 95 // If not present then decryption is expected to fail. |
96 bool has_pt = test_vectors[i].pt; | 96 bool has_pt = test_vectors[i].pt; |
97 | 97 |
98 // Decode the test vector. | 98 // Decode the test vector. |
99 string key; | 99 string key; |
100 string iv; | 100 string iv; |
101 string aad; | 101 string aad; |
102 string ct; | 102 string ct; |
103 string pt; | 103 string pt; |
104 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); | 104 ASSERT_TRUE(DecodeHexString(test_vectors[i].key, &key)); |
105 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 105 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
106 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 106 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
107 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 107 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
108 if (has_pt) { | 108 if (has_pt) { |
109 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 109 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
110 } | 110 } |
111 | 111 |
112 ChaCha20Poly1305Decrypter decrypter; | 112 ChaCha20Poly1305Decrypter decrypter; |
113 ASSERT_TRUE(decrypter.SetKey(key)); | 113 ASSERT_TRUE(decrypter.SetKey(key)); |
114 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 114 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
115 &decrypter, iv, | 115 &decrypter, iv, |
116 // This deliberately tests that the decrypter can handle an AAD that | 116 // This deliberately tests that the decrypter can handle an AAD that |
117 // is set to NULL, as opposed to a zero-length, non-NULL pointer. | 117 // is set to nullptr, as opposed to a zero-length, non-nullptr pointer. |
118 StringPiece(aad.length() ? aad.data() : NULL, aad.length()), ct)); | 118 StringPiece(aad.length() ? aad.data() : nullptr, aad.length()), ct)); |
119 if (!decrypted.get()) { | 119 if (!decrypted.get()) { |
120 EXPECT_FALSE(has_pt); | 120 EXPECT_FALSE(has_pt); |
121 continue; | 121 continue; |
122 } | 122 } |
123 EXPECT_TRUE(has_pt); | 123 EXPECT_TRUE(has_pt); |
124 | 124 |
125 ASSERT_EQ(pt.length(), decrypted->length()); | 125 ASSERT_EQ(pt.length(), decrypted->length()); |
126 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 126 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), |
127 pt.length(), pt.data(), pt.length()); | 127 pt.length(), pt.data(), pt.length()); |
128 } | 128 } |
129 } | 129 } |
130 | 130 |
131 } // namespace test | 131 } // namespace test |
132 } // namespace net | 132 } // namespace net |
OLD | NEW |