| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/aes_128_gcm_12_encrypter.h" | 5 #include "net/quic/crypto/aes_128_gcm_12_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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 struct TestVector { | 57 struct TestVector { |
| 58 const char* key; | 58 const char* key; |
| 59 const char* iv; | 59 const char* iv; |
| 60 const char* pt; | 60 const char* pt; |
| 61 const char* aad; | 61 const char* aad; |
| 62 const char* ct; | 62 const char* ct; |
| 63 const char* tag; | 63 const char* tag; |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 const TestGroupInfo test_group_info[] = { | 66 const TestGroupInfo test_group_info[] = { |
| 67 { 128, 96, 0, 0, 128 }, | 67 {128, 96, 0, 0, 128}, |
| 68 { 128, 96, 0, 128, 128 }, | 68 {128, 96, 0, 128, 128}, |
| 69 { 128, 96, 128, 0, 128 }, | 69 {128, 96, 128, 0, 128}, |
| 70 { 128, 96, 408, 160, 128 }, | 70 {128, 96, 408, 160, 128}, |
| 71 { 128, 96, 408, 720, 128 }, | 71 {128, 96, 408, 720, 128}, |
| 72 { 128, 96, 104, 0, 128 }, | 72 {128, 96, 104, 0, 128}, |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 const TestVector test_group_0[] = { | 75 const TestVector test_group_0[] = { |
| 76 { "11754cd72aec309bf52f7687212e8957", | 76 {"11754cd72aec309bf52f7687212e8957", "3c819d9a9bed087615030b65", "", "", "", |
| 77 "3c819d9a9bed087615030b65", | 77 "250327c674aaf477aef2675748cf6971"}, |
| 78 "", | 78 {"ca47248ac0b6f8372a97ac43508308ed", "ffd2b598feabc9019262d2be", "", "", "", |
| 79 "", | 79 "60d20404af527d248d893ae495707d1a"}, |
| 80 "", | 80 {NULL}}; |
| 81 "250327c674aaf477aef2675748cf6971" | |
| 82 }, | |
| 83 { "ca47248ac0b6f8372a97ac43508308ed", | |
| 84 "ffd2b598feabc9019262d2be", | |
| 85 "", | |
| 86 "", | |
| 87 "", | |
| 88 "60d20404af527d248d893ae495707d1a" | |
| 89 }, | |
| 90 { NULL } | |
| 91 }; | |
| 92 | 81 |
| 93 const TestVector test_group_1[] = { | 82 const TestVector test_group_1[] = { |
| 94 { "77be63708971c4e240d1cb79e8d77feb", | 83 {"77be63708971c4e240d1cb79e8d77feb", "e0e00f19fed7ba0136a797f3", "", |
| 95 "e0e00f19fed7ba0136a797f3", | 84 "7a43ec1d9c0a5a78a0b16533a6213cab", "", |
| 96 "", | 85 "209fcc8d3675ed938e9c7166709dd946"}, |
| 97 "7a43ec1d9c0a5a78a0b16533a6213cab", | 86 {"7680c5d3ca6154758e510f4d25b98820", "f8f105f9c3df4965780321f8", "", |
| 98 "", | 87 "c94c410194c765e3dcc7964379758ed3", "", |
| 99 "209fcc8d3675ed938e9c7166709dd946" | 88 "94dca8edfcf90bb74b153c8d48a17930"}, |
| 100 }, | 89 {NULL}}; |
| 101 { "7680c5d3ca6154758e510f4d25b98820", | |
| 102 "f8f105f9c3df4965780321f8", | |
| 103 "", | |
| 104 "c94c410194c765e3dcc7964379758ed3", | |
| 105 "", | |
| 106 "94dca8edfcf90bb74b153c8d48a17930" | |
| 107 }, | |
| 108 { NULL } | |
| 109 }; | |
| 110 | 90 |
| 111 const TestVector test_group_2[] = { | 91 const TestVector test_group_2[] = { |
| 112 { "7fddb57453c241d03efbed3ac44e371c", | 92 {"7fddb57453c241d03efbed3ac44e371c", "ee283a3fc75575e33efd4887", |
| 113 "ee283a3fc75575e33efd4887", | 93 "d5de42b461646c255c87bd2962d3b9a2", "", "2ccda4a5415cb91e135c2a0f78c9b2fd", |
| 114 "d5de42b461646c255c87bd2962d3b9a2", | 94 "b36d1df9b9d5e596f83e8b7f52971cb3"}, |
| 115 "", | 95 {"ab72c77b97cb5fe9a382d9fe81ffdbed", "54cc7dc2c37ec006bcc6d1da", |
| 116 "2ccda4a5415cb91e135c2a0f78c9b2fd", | 96 "007c5e5b3e59df24a7c355584fc1518d", "", "0e1bde206a07a9c2c1b65300f8c64997", |
| 117 "b36d1df9b9d5e596f83e8b7f52971cb3" | 97 "2b4401346697138c7a4891ee59867d0c"}, |
| 118 }, | 98 {NULL}}; |
| 119 { "ab72c77b97cb5fe9a382d9fe81ffdbed", | |
| 120 "54cc7dc2c37ec006bcc6d1da", | |
| 121 "007c5e5b3e59df24a7c355584fc1518d", | |
| 122 "", | |
| 123 "0e1bde206a07a9c2c1b65300f8c64997", | |
| 124 "2b4401346697138c7a4891ee59867d0c" | |
| 125 }, | |
| 126 { NULL } | |
| 127 }; | |
| 128 | 99 |
| 129 const TestVector test_group_3[] = { | 100 const TestVector test_group_3[] = { |
| 130 { "fe47fcce5fc32665d2ae399e4eec72ba", | 101 {"fe47fcce5fc32665d2ae399e4eec72ba", "5adb9609dbaeb58cbd6e7275", |
| 131 "5adb9609dbaeb58cbd6e7275", | 102 "7c0e88c88899a779228465074797cd4c2e1498d259b54390b85e3eef1c02df60e743f1" |
| 132 "7c0e88c88899a779228465074797cd4c2e1498d259b54390b85e3eef1c02df60e743f1" | 103 "b840382c4bccaf3bafb4ca8429bea063", |
| 133 "b840382c4bccaf3bafb4ca8429bea063", | 104 "88319d6e1d3ffa5f987199166c8a9b56c2aeba5a", |
| 134 "88319d6e1d3ffa5f987199166c8a9b56c2aeba5a", | 105 "98f4826f05a265e6dd2be82db241c0fbbbf9ffb1c173aa83964b7cf539304373636525" |
| 135 "98f4826f05a265e6dd2be82db241c0fbbbf9ffb1c173aa83964b7cf539304373636525" | 106 "3ddbc5db8778371495da76d269e5db3e", |
| 136 "3ddbc5db8778371495da76d269e5db3e", | 107 "291ef1982e4defedaa2249f898556b47"}, |
| 137 "291ef1982e4defedaa2249f898556b47" | 108 {"ec0c2ba17aa95cd6afffe949da9cc3a8", "296bce5b50b7d66096d627ef", |
| 138 }, | 109 "b85b3753535b825cbe5f632c0b843c741351f18aa484281aebec2f45bb9eea2d79d987" |
| 139 { "ec0c2ba17aa95cd6afffe949da9cc3a8", | 110 "b764b9611f6c0f8641843d5d58f3a242", |
| 140 "296bce5b50b7d66096d627ef", | 111 "f8d00f05d22bf68599bcdeb131292ad6e2df5d14", |
| 141 "b85b3753535b825cbe5f632c0b843c741351f18aa484281aebec2f45bb9eea2d79d987" | 112 "a7443d31c26bdf2a1c945e29ee4bd344a99cfaf3aa71f8b3f191f83c2adfc7a0716299" |
| 142 "b764b9611f6c0f8641843d5d58f3a242", | 113 "5506fde6309ffc19e716eddf1a828c5a", |
| 143 "f8d00f05d22bf68599bcdeb131292ad6e2df5d14", | 114 "890147971946b627c40016da1ecf3e77"}, |
| 144 "a7443d31c26bdf2a1c945e29ee4bd344a99cfaf3aa71f8b3f191f83c2adfc7a0716299" | 115 {NULL}}; |
| 145 "5506fde6309ffc19e716eddf1a828c5a", | |
| 146 "890147971946b627c40016da1ecf3e77" | |
| 147 }, | |
| 148 { NULL } | |
| 149 }; | |
| 150 | 116 |
| 151 const TestVector test_group_4[] = { | 117 const TestVector test_group_4[] = { |
| 152 { "2c1f21cf0f6fb3661943155c3e3d8492", | 118 {"2c1f21cf0f6fb3661943155c3e3d8492", "23cb5ff362e22426984d1907", |
| 153 "23cb5ff362e22426984d1907", | 119 "42f758836986954db44bf37c6ef5e4ac0adaf38f27252a1b82d02ea949c8a1a2dbc0d6" |
| 154 "42f758836986954db44bf37c6ef5e4ac0adaf38f27252a1b82d02ea949c8a1a2dbc0d6" | 120 "8b5615ba7c1220ff6510e259f06655d8", |
| 155 "8b5615ba7c1220ff6510e259f06655d8", | 121 "5d3624879d35e46849953e45a32a624d6a6c536ed9857c613b572b0333e701557a713e" |
| 156 "5d3624879d35e46849953e45a32a624d6a6c536ed9857c613b572b0333e701557a713e" | 122 "3f010ecdf9a6bd6c9e3e44b065208645aff4aabee611b391528514170084ccf587177f" |
| 157 "3f010ecdf9a6bd6c9e3e44b065208645aff4aabee611b391528514170084ccf587177f" | 123 "4488f33cfb5e979e42b6e1cfc0a60238982a7aec", |
| 158 "4488f33cfb5e979e42b6e1cfc0a60238982a7aec", | 124 "81824f0e0d523db30d3da369fdc0d60894c7a0a20646dd015073ad2732bd989b14a222" |
| 159 "81824f0e0d523db30d3da369fdc0d60894c7a0a20646dd015073ad2732bd989b14a222" | 125 "b6ad57af43e1895df9dca2a5344a62cc", |
| 160 "b6ad57af43e1895df9dca2a5344a62cc", | 126 "57a3ee28136e94c74838997ae9823f3a"}, |
| 161 "57a3ee28136e94c74838997ae9823f3a" | 127 {"d9f7d2411091f947b4d6f1e2d1f0fb2e", "e1934f5db57cc983e6b180e7", |
| 162 }, | 128 "73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490" |
| 163 { "d9f7d2411091f947b4d6f1e2d1f0fb2e", | 129 "c2c6f6166f4a59431e182663fcaea05a", |
| 164 "e1934f5db57cc983e6b180e7", | 130 "0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d" |
| 165 "73ed042327f70fe9c572a61545eda8b2a0c6e1d6c291ef19248e973aee6c312012f490" | 131 "0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a201" |
| 166 "c2c6f6166f4a59431e182663fcaea05a", | 132 "15d2e51398344b16bee1ed7c499b353d6c597af8", |
| 167 "0a8a18a7150e940c3d87b38e73baee9a5c049ee21795663e264b694a949822b639092d" | 133 "aaadbd5c92e9151ce3db7210b8714126b73e43436d242677afa50384f2149b831f1d57" |
| 168 "0e67015e86363583fcf0ca645af9f43375f05fdb4ce84f411dcbca73c2220dea03a201" | 134 "3c7891c2a91fbc48db29967ec9542b23", |
| 169 "15d2e51398344b16bee1ed7c499b353d6c597af8", | 135 "21b51ca862cb637cdd03b99a0f93b134"}, |
| 170 "aaadbd5c92e9151ce3db7210b8714126b73e43436d242677afa50384f2149b831f1d57" | 136 {NULL}}; |
| 171 "3c7891c2a91fbc48db29967ec9542b23", | |
| 172 "21b51ca862cb637cdd03b99a0f93b134" | |
| 173 }, | |
| 174 { NULL } | |
| 175 }; | |
| 176 | 137 |
| 177 const TestVector test_group_5[] = { | 138 const TestVector test_group_5[] = { |
| 178 { "fe9bb47deb3a61e423c2231841cfd1fb", | 139 {"fe9bb47deb3a61e423c2231841cfd1fb", "4d328eb776f500a2f7fb47aa", |
| 179 "4d328eb776f500a2f7fb47aa", | 140 "f1cc3818e421876bb6b8bbd6c9", "", "b88c5c1977b35b517b0aeae967", |
| 180 "f1cc3818e421876bb6b8bbd6c9", | 141 "43fd4727fe5cdb4b5b42818dea7ef8c9"}, |
| 181 "", | 142 {"6703df3701a7f54911ca72e24dca046a", "12823ab601c350ea4bc2488c", |
| 182 "b88c5c1977b35b517b0aeae967", | 143 "793cd125b0b84a043e3ac67717", "", "b2051c80014f42f08735a7b0cd", |
| 183 "43fd4727fe5cdb4b5b42818dea7ef8c9" | 144 "38e6bcd29962e5f2c13626b85a877101"}, |
| 184 }, | 145 {NULL}}; |
| 185 { "6703df3701a7f54911ca72e24dca046a", | |
| 186 "12823ab601c350ea4bc2488c", | |
| 187 "793cd125b0b84a043e3ac67717", | |
| 188 "", | |
| 189 "b2051c80014f42f08735a7b0cd", | |
| 190 "38e6bcd29962e5f2c13626b85a877101" | |
| 191 }, | |
| 192 { NULL } | |
| 193 }; | |
| 194 | 146 |
| 195 const TestVector* const test_group_array[] = { | 147 const TestVector* const test_group_array[] = { |
| 196 test_group_0, | 148 test_group_0, test_group_1, test_group_2, |
| 197 test_group_1, | 149 test_group_3, test_group_4, test_group_5, |
| 198 test_group_2, | |
| 199 test_group_3, | |
| 200 test_group_4, | |
| 201 test_group_5, | |
| 202 }; | 150 }; |
| 203 | 151 |
| 204 } // namespace | 152 } // namespace |
| 205 | 153 |
| 206 namespace net { | 154 namespace net { |
| 207 namespace test { | 155 namespace test { |
| 208 | 156 |
| 209 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing | 157 // EncryptWithNonce wraps the |Encrypt| method of |encrypter| to allow passing |
| 210 // in an nonce and also to allocate the buffer needed for the ciphertext. | 158 // in an nonce and also to allocate the buffer needed for the ciphertext. |
| 211 QuicData* EncryptWithNonce(Aes128Gcm12Encrypter* encrypter, | 159 QuicData* EncryptWithNonce(Aes128Gcm12Encrypter* encrypter, |
| 212 StringPiece nonce, | 160 StringPiece nonce, |
| 213 StringPiece associated_data, | 161 StringPiece associated_data, |
| 214 StringPiece plaintext) { | 162 StringPiece plaintext) { |
| 215 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); | 163 size_t ciphertext_size = encrypter->GetCiphertextSize(plaintext.length()); |
| 216 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); | 164 scoped_ptr<char[]> ciphertext(new char[ciphertext_size]); |
| 217 | 165 |
| 218 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | 166 if (!encrypter->Encrypt(nonce, |
| 167 associated_data, |
| 168 plaintext, |
| 219 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 169 reinterpret_cast<unsigned char*>(ciphertext.get()))) { |
| 220 return NULL; | 170 return NULL; |
| 221 } | 171 } |
| 222 | 172 |
| 223 return new QuicData(ciphertext.release(), ciphertext_size, true); | 173 return new QuicData(ciphertext.release(), ciphertext_size, true); |
| 224 } | 174 } |
| 225 | 175 |
| 226 TEST(Aes128Gcm12EncrypterTest, Encrypt) { | 176 TEST(Aes128Gcm12EncrypterTest, Encrypt) { |
| 227 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 177 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
| 228 SCOPED_TRACE(i); | 178 SCOPED_TRACE(i); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 248 EXPECT_EQ(test_info.key_len, key.length() * 8); | 198 EXPECT_EQ(test_info.key_len, key.length() * 8); |
| 249 EXPECT_EQ(test_info.iv_len, iv.length() * 8); | 199 EXPECT_EQ(test_info.iv_len, iv.length() * 8); |
| 250 EXPECT_EQ(test_info.pt_len, pt.length() * 8); | 200 EXPECT_EQ(test_info.pt_len, pt.length() * 8); |
| 251 EXPECT_EQ(test_info.aad_len, aad.length() * 8); | 201 EXPECT_EQ(test_info.aad_len, aad.length() * 8); |
| 252 EXPECT_EQ(test_info.pt_len, ct.length() * 8); | 202 EXPECT_EQ(test_info.pt_len, ct.length() * 8); |
| 253 EXPECT_EQ(test_info.tag_len, tag.length() * 8); | 203 EXPECT_EQ(test_info.tag_len, tag.length() * 8); |
| 254 | 204 |
| 255 Aes128Gcm12Encrypter encrypter; | 205 Aes128Gcm12Encrypter encrypter; |
| 256 ASSERT_TRUE(encrypter.SetKey(key)); | 206 ASSERT_TRUE(encrypter.SetKey(key)); |
| 257 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | 207 scoped_ptr<QuicData> encrypted(EncryptWithNonce( |
| 258 &encrypter, iv, | 208 &encrypter, |
| 209 iv, |
| 259 // This deliberately tests that the encrypter can handle an AAD that | 210 // This deliberately tests that the encrypter can handle an AAD that |
| 260 // is set to NULL, as opposed to a zero-length, non-NULL pointer. | 211 // is set to NULL, as opposed to a zero-length, non-NULL pointer. |
| 261 aad.length() ? aad : StringPiece(), pt)); | 212 aad.length() ? aad : StringPiece(), |
| 213 pt)); |
| 262 ASSERT_TRUE(encrypted.get()); | 214 ASSERT_TRUE(encrypted.get()); |
| 263 | 215 |
| 264 // The test vectors have 16 byte authenticators but this code only uses | 216 // The test vectors have 16 byte authenticators but this code only uses |
| 265 // the first 12. | 217 // the first 12. |
| 266 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize), | 218 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize), |
| 267 tag.length()); | 219 tag.length()); |
| 268 tag.resize(Aes128Gcm12Encrypter::kAuthTagSize); | 220 tag.resize(Aes128Gcm12Encrypter::kAuthTagSize); |
| 269 | 221 |
| 270 ASSERT_EQ(ct.length() + tag.length(), encrypted->length()); | 222 ASSERT_EQ(ct.length() + tag.length(), encrypted->length()); |
| 271 test::CompareCharArraysWithHexError("ciphertext", encrypted->data(), | |
| 272 ct.length(), ct.data(), ct.length()); | |
| 273 test::CompareCharArraysWithHexError( | 223 test::CompareCharArraysWithHexError( |
| 274 "authentication tag", encrypted->data() + ct.length(), tag.length(), | 224 "ciphertext", encrypted->data(), ct.length(), ct.data(), ct.length()); |
| 275 tag.data(), tag.length()); | 225 test::CompareCharArraysWithHexError("authentication tag", |
| 226 encrypted->data() + ct.length(), |
| 227 tag.length(), |
| 228 tag.data(), |
| 229 tag.length()); |
| 276 } | 230 } |
| 277 } | 231 } |
| 278 } | 232 } |
| 279 | 233 |
| 280 TEST(Aes128Gcm12EncrypterTest, GetMaxPlaintextSize) { | 234 TEST(Aes128Gcm12EncrypterTest, GetMaxPlaintextSize) { |
| 281 Aes128Gcm12Encrypter encrypter; | 235 Aes128Gcm12Encrypter encrypter; |
| 282 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); | 236 EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); |
| 283 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); | 237 EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); |
| 284 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); | 238 EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); |
| 285 } | 239 } |
| 286 | 240 |
| 287 TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) { | 241 TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) { |
| 288 Aes128Gcm12Encrypter encrypter; | 242 Aes128Gcm12Encrypter encrypter; |
| 289 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 243 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
| 290 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 244 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
| 291 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 245 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
| 292 } | 246 } |
| 293 | 247 |
| 294 } // namespace test | 248 } // namespace test |
| 295 } // namespace net | 249 } // namespace net |
| OLD | NEW |