Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(919)

Side by Side Diff: net/quic/crypto/chacha20_poly1305_encrypter_test.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 namespace { 11 namespace {
12 12
13 // The test vectors come from draft-agl-tls-chacha20poly1305-04 Section 7. 13 // The test vectors come from draft-agl-tls-chacha20poly1305-04 Section 7.
14 14
15 // Each test vector consists of five strings of lowercase hexadecimal digits. 15 // Each test vector consists of five strings of lowercase hexadecimal digits.
16 // The strings may be empty (zero length). A test vector with a NULL |key| 16 // The strings may be empty (zero length). A test vector with a NULL |key|
17 // marks the end of an array of test vectors. 17 // marks the end of an array of test vectors.
18 struct TestVector { 18 struct TestVector {
19 const char* key; 19 const char* key;
20 const char* pt; 20 const char* pt;
21 const char* iv; 21 const char* iv;
22 const char* aad; 22 const char* aad;
23 const char* ct; 23 const char* ct;
24 }; 24 };
25 25
26 const TestVector test_vectors[] = { 26 const TestVector test_vectors[] = {
27 { "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110" 27 {
28 "0a1007", 28 "4290bcb154173531f314af57f3be3b5006da371ece272afa1b5dbdd110"
29 "86d09974840bded2a5ca", 29 "0a1007",
30 "cd7cf67be39c794a", 30 "86d09974840bded2a5ca", "cd7cf67be39c794a", "87e229d4500845a079c0",
31 "87e229d4500845a079c0", 31 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475" // "3896e1d6" truncated.
32 "e3e446f7ede9a19b62a4677dabf4e3d24b876bb28475" // "3896e1d6" truncated. 32 },
33 }, 33 {NULL}};
34 { NULL }
35 };
36 34
37 } // namespace 35 } // namespace
38 36
39 namespace net { 37 namespace net {
40 namespace test { 38 namespace test {
41 39
42 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing 40 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing
43 // in an nonce and also to allocate the buffer needed for the ciphertext. 41 // in an nonce and also to allocate the buffer needed for the ciphertext.
44 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter, 42 QuicData* EncryptWithNonce(ChaCha20Poly1305Encrypter* encrypter,
45 StringPiece nonce, 43 StringPiece nonce,
46 StringPiece associated_data, 44 StringPiece associated_data,
47 StringPiece plaintext) { 45 StringPiece plaintext) {
48 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); 46 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length());
49 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); 47 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]);
50 48
51 if (!encrypter->Encrypt(nonce, associated_data, plaintext, 49 if (!encrypter->Encrypt(nonce,
50 associated_data,
51 plaintext,
52 reinterpret_cast<unsigned char*>(ciphertext.get()))) { 52 reinterpret_cast<unsigned char*>(ciphertext.get()))) {
53 return NULL; 53 return NULL;
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 != NULL; 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,
82 iv,
82 // This deliberately tests that the encrypter can handle an AAD that 83 // 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. 84 // is set to NULL, as opposed to a zero-length, non-NULL pointer.
84 StringPiece(aad.length() ? aad.data() : NULL, aad.length()), pt)); 85 StringPiece(aad.length() ? aad.data() : NULL, aad.length()),
86 pt));
85 ASSERT_TRUE(encrypted.get()); 87 ASSERT_TRUE(encrypted.get());
86 88
87 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), 89 test::CompareCharArraysWithHexError("ciphertext",
88 encrypted->length(), ct.data(), 90 encrypted->data(),
91 encrypted->length(),
92 ct.data(),
89 ct.length()); 93 ct.length());
90 } 94 }
91 } 95 }
92 96
93 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) { 97 TEST(ChaCha20Poly1305EncrypterTest, GetMaxPlaintextSize) {
94 ChaCha20Poly1305Encrypter encrypter; 98 ChaCha20Poly1305Encrypter encrypter;
95 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); 99 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012));
96 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); 100 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112));
97 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); 101 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22));
98 } 102 }
99 103
100 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) { 104 TEST(ChaCha20Poly1305EncrypterTest, GetCiphertextSize) {
101 ChaCha20Poly1305Encrypter encrypter; 105 ChaCha20Poly1305Encrypter encrypter;
102 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); 106 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000));
103 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); 107 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100));
104 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); 108 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10));
105 } 109 }
106 110
107 } // namespace test 111 } // namespace test
108 } // namespace net 112 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698