| 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 11 matching lines...) Expand all Loading... |
| 22 const char* aad; | 22 const char* aad; |
| 23 const char* ct; | 23 const char* ct; |
| 24 | 24 |
| 25 // Expected output: | 25 // Expected output: |
| 26 const char* pt; // An empty string "" means decryption succeeded and | 26 const char* pt; // An empty string "" means decryption succeeded and |
| 27 // the plaintext is zero-length. NULL means decryption | 27 // the plaintext is zero-length. NULL means decryption |
| 28 // failed. | 28 // failed. |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 const TestVector test_vectors[] = { | 31 const TestVector test_vectors[] = { |
| 32 { "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" | 32 {"4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" |
| 33 "0a1007", | 33 "0a1007", |
| 34 "cd7cf67be39c794a", | 34 "cd7cf67be39c794a", "87e229d4500845a079c0", |
| 35 "87e229d4500845a079c0", | 35 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. |
| 36 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. | 36 "86d09974840bded2a5ca"}, |
| 37 "86d09974840bded2a5ca" | 37 // Modify the ciphertext (ChaCha20 encryption output). |
| 38 }, | 38 { |
| 39 // Modify the ciphertext (ChaCha20 encryption output). | 39 "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" |
| 40 { "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" | 40 "0a1007", |
| 41 "0a1007", | 41 "cd7cf67be39c794a", "87e229d4500845a079c0", |
| 42 "cd7cf67be39c794a", | 42 "f3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. |
| 43 "87e229d4500845a079c0", | 43 NULL // FAIL |
| 44 "f3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. | 44 }, |
| 45 NULL // FAIL | 45 // Modify the ciphertext (Poly1305 authenticator). |
| 46 }, | 46 { |
| 47 // Modify the ciphertext (Poly1305 authenticator). | 47 "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" |
| 48 { "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" | 48 "0a1007", |
| 49 "0a1007", | 49 "cd7cf67be39c794a", "87e229d4500845a079c0", |
| 50 "cd7cf67be39c794a", | 50 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28476", // "3896e1d6" truncated. |
| 51 "87e229d4500845a079c0", | 51 NULL // FAIL |
| 52 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28476", // "3896e1d6" truncated. | 52 }, |
| 53 NULL // FAIL | 53 // Modify the associated data. |
| 54 }, | 54 { |
| 55 // Modify the associated data. | 55 "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" |
| 56 { "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" | 56 "0a1007", |
| 57 "0a1007", | 57 "dd7cf67be39c794a", "87e229d4500845a079c0", |
| 58 "dd7cf67be39c794a", | 58 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. |
| 59 "87e229d4500845a079c0", | 59 NULL // FAIL |
| 60 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475", // "3896e1d6" truncated. | 60 }, |
| 61 NULL // FAIL | 61 {NULL}}; |
| 62 }, | |
| 63 { NULL } | |
| 64 }; | |
| 65 | 62 |
| 66 } // namespace | 63 } // namespace |
| 67 | 64 |
| 68 namespace net { | 65 namespace net { |
| 69 namespace test { | 66 namespace test { |
| 70 | 67 |
| 71 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing | 68 // DecryptWithNonce wraps the |Decrypt| method of |decrypter| to allow passing |
| 72 // in an nonce and also to allocate the buffer needed for the plaintext. | 69 // in an nonce and also to allocate the buffer needed for the plaintext. |
| 73 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, | 70 QuicData* DecryptWithNonce(ChaCha20Poly1305Decrypter* decrypter, |
| 74 StringPiece nonce, | 71 StringPiece nonce, |
| 75 StringPiece associated_data, | 72 StringPiece associated_data, |
| 76 StringPiece ciphertext) { | 73 StringPiece ciphertext) { |
| 77 size_t plaintext_size = ciphertext.length(); | 74 size_t plaintext_size = ciphertext.length(); |
| 78 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 75 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
| 79 | 76 |
| 80 if (!decrypter->Decrypt(nonce, associated_data, ciphertext, | 77 if (!decrypter->Decrypt(nonce, |
| 78 associated_data, |
| 79 ciphertext, |
| 81 reinterpret_cast<unsigned char*>(plaintext.get()), | 80 reinterpret_cast<unsigned char*>(plaintext.get()), |
| 82 &plaintext_size)) { | 81 &plaintext_size)) { |
| 83 return NULL; | 82 return NULL; |
| 84 } | 83 } |
| 85 return new QuicData(plaintext.release(), plaintext_size, true); | 84 return new QuicData(plaintext.release(), plaintext_size, true); |
| 86 } | 85 } |
| 87 | 86 |
| 88 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { | 87 TEST(ChaCha20Poly1305DecrypterTest, Decrypt) { |
| 89 if (!ChaCha20Poly1305Decrypter::IsSupported()) { | 88 if (!ChaCha20Poly1305Decrypter::IsSupported()) { |
| 90 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; | 89 LOG(INFO) << "ChaCha20+Poly1305 not supported. Test skipped."; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 105 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); | 104 ASSERT_TRUE(DecodeHexString(test_vectors[i].iv, &iv)); |
| 106 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); | 105 ASSERT_TRUE(DecodeHexString(test_vectors[i].aad, &aad)); |
| 107 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); | 106 ASSERT_TRUE(DecodeHexString(test_vectors[i].ct, &ct)); |
| 108 if (has_pt) { | 107 if (has_pt) { |
| 109 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); | 108 ASSERT_TRUE(DecodeHexString(test_vectors[i].pt, &pt)); |
| 110 } | 109 } |
| 111 | 110 |
| 112 ChaCha20Poly1305Decrypter decrypter; | 111 ChaCha20Poly1305Decrypter decrypter; |
| 113 ASSERT_TRUE(decrypter.SetKey(key)); | 112 ASSERT_TRUE(decrypter.SetKey(key)); |
| 114 scoped_ptr<QuicData> decrypted(DecryptWithNonce( | 113 scoped_ptr<QuicData> decrypted(DecryptWithNonce( |
| 115 &decrypter, iv, | 114 &decrypter, |
| 115 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 NULL, as opposed to a zero-length, non-NULL pointer. |
| 118 StringPiece(aad.length() ? aad.data() : NULL, aad.length()), ct)); | 118 StringPiece(aad.length() ? aad.data() : NULL, aad.length()), |
| 119 ct)); |
| 119 if (!decrypted.get()) { | 120 if (!decrypted.get()) { |
| 120 EXPECT_FALSE(has_pt); | 121 EXPECT_FALSE(has_pt); |
| 121 continue; | 122 continue; |
| 122 } | 123 } |
| 123 EXPECT_TRUE(has_pt); | 124 EXPECT_TRUE(has_pt); |
| 124 | 125 |
| 125 ASSERT_EQ(pt.length(), decrypted->length()); | 126 ASSERT_EQ(pt.length(), decrypted->length()); |
| 126 test::CompareCharArraysWithHexError("plaintext", decrypted->data(), | 127 test::CompareCharArraysWithHexError( |
| 127 pt.length(), pt.data(), pt.length()); | 128 "plaintext", decrypted->data(), pt.length(), pt.data(), pt.length()); |
| 128 } | 129 } |
| 129 } | 130 } |
| 130 | 131 |
| 131 } // namespace test | 132 } // namespace test |
| 132 } // namespace net | 133 } // namespace net |
| OLD | NEW |