| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 217 |
| 218 if (!encrypter->Encrypt(nonce, associated_data, plaintext, | 218 if (!encrypter->Encrypt(nonce, associated_data, plaintext, |
| 219 reinterpret_cast<unsigned char*>(ciphertext.get()))) { | 219 reinterpret_cast<unsigned char*>(ciphertext.get()))) { |
| 220 return NULL; | 220 return NULL; |
| 221 } | 221 } |
| 222 | 222 |
| 223 return new QuicData(ciphertext.release(), ciphertext_size, true); | 223 return new QuicData(ciphertext.release(), ciphertext_size, true); |
| 224 } | 224 } |
| 225 | 225 |
| 226 TEST(Aes128Gcm12EncrypterTest, Encrypt) { | 226 TEST(Aes128Gcm12EncrypterTest, Encrypt) { |
| 227 string key; | |
| 228 string iv; | |
| 229 string pt; | |
| 230 string aad; | |
| 231 string ct; | |
| 232 string tag; | |
| 233 | |
| 234 for (size_t i = 0; i < arraysize(test_group_array); i++) { | 227 for (size_t i = 0; i < arraysize(test_group_array); i++) { |
| 235 SCOPED_TRACE(i); | 228 SCOPED_TRACE(i); |
| 236 const TestVector* test_vector = test_group_array[i]; | 229 const TestVector* test_vectors = test_group_array[i]; |
| 237 const TestGroupInfo& test_info = test_group_info[i]; | 230 const TestGroupInfo& test_info = test_group_info[i]; |
| 238 for (size_t j = 0; test_vector[j].key != NULL; j++) { | 231 for (size_t j = 0; test_vectors[j].key != NULL; j++) { |
| 239 // Decode the test vector. | 232 // Decode the test vector. |
| 240 ASSERT_TRUE(DecodeHexString(test_vector[j].key, &key)); | 233 string key; |
| 241 ASSERT_TRUE(DecodeHexString(test_vector[j].iv, &iv)); | 234 string iv; |
| 242 ASSERT_TRUE(DecodeHexString(test_vector[j].pt, &pt)); | 235 string pt; |
| 243 ASSERT_TRUE(DecodeHexString(test_vector[j].aad, &aad)); | 236 string aad; |
| 244 ASSERT_TRUE(DecodeHexString(test_vector[j].ct, &ct)); | 237 string ct; |
| 245 ASSERT_TRUE(DecodeHexString(test_vector[j].tag, &tag)); | 238 string tag; |
| 239 ASSERT_TRUE(DecodeHexString(test_vectors[j].key, &key)); |
| 240 ASSERT_TRUE(DecodeHexString(test_vectors[j].iv, &iv)); |
| 241 ASSERT_TRUE(DecodeHexString(test_vectors[j].pt, &pt)); |
| 242 ASSERT_TRUE(DecodeHexString(test_vectors[j].aad, &aad)); |
| 243 ASSERT_TRUE(DecodeHexString(test_vectors[j].ct, &ct)); |
| 244 ASSERT_TRUE(DecodeHexString(test_vectors[j].tag, &tag)); |
| 246 | 245 |
| 247 // The test vector's lengths should look sane. Note that the lengths | 246 // The test vector's lengths should look sane. Note that the lengths |
| 248 // in |test_info| are in bits. | 247 // in |test_info| are in bits. |
| 249 EXPECT_EQ(test_info.key_len, key.size() * 8); | 248 EXPECT_EQ(test_info.key_len, key.size() * 8); |
| 250 EXPECT_EQ(test_info.iv_len, iv.size() * 8); | 249 EXPECT_EQ(test_info.iv_len, iv.size() * 8); |
| 251 EXPECT_EQ(test_info.pt_len, pt.size() * 8); | 250 EXPECT_EQ(test_info.pt_len, pt.size() * 8); |
| 252 EXPECT_EQ(test_info.aad_len, aad.size() * 8); | 251 EXPECT_EQ(test_info.aad_len, aad.size() * 8); |
| 253 EXPECT_EQ(test_info.pt_len, ct.size() * 8); | 252 EXPECT_EQ(test_info.pt_len, ct.size() * 8); |
| 254 EXPECT_EQ(test_info.tag_len, tag.size() * 8); | 253 EXPECT_EQ(test_info.tag_len, tag.size() * 8); |
| 255 | 254 |
| 256 Aes128Gcm12Encrypter encrypter; | 255 Aes128Gcm12Encrypter encrypter; |
| 257 ASSERT_TRUE(encrypter.SetKey(key)); | 256 ASSERT_TRUE(encrypter.SetKey(key)); |
| 258 scoped_ptr<QuicData> encrypted(EncryptWithNonce( | 257 scoped_ptr<QuicData> encrypted(EncryptWithNonce( |
| 259 &encrypter, iv, | 258 &encrypter, iv, |
| 260 // OpenSSL fails if NULL is set as the AAD, as opposed to a | 259 // This deliberately tests that the encrypter can handle an AAD that |
| 261 // zero-length, non-NULL pointer. This deliberately tests that we | 260 // is set to NULL, as opposed to a zero-length, non-NULL pointer. |
| 262 // handle this case. | |
| 263 aad.size() ? aad : StringPiece(), pt)); | 261 aad.size() ? aad : StringPiece(), pt)); |
| 264 ASSERT_TRUE(encrypted.get()); | 262 ASSERT_TRUE(encrypted.get()); |
| 265 | 263 |
| 266 // The test vectors have 16 byte authenticators but this code only uses | 264 // The test vectors have 16 byte authenticators but this code only uses |
| 267 // the first 12. | 265 // the first 12. |
| 268 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize), | 266 ASSERT_LE(static_cast<size_t>(Aes128Gcm12Encrypter::kAuthTagSize), |
| 269 tag.size()); | 267 tag.size()); |
| 270 size_t tag_len = Aes128Gcm12Encrypter::kAuthTagSize; | 268 size_t tag_len = Aes128Gcm12Encrypter::kAuthTagSize; |
| 271 | 269 |
| 272 ASSERT_EQ(ct.size() + tag_len, encrypted->length()); | 270 ASSERT_EQ(ct.size() + tag_len, encrypted->length()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 288 | 286 |
| 289 TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) { | 287 TEST(Aes128Gcm12EncrypterTest, GetCiphertextSize) { |
| 290 Aes128Gcm12Encrypter encrypter; | 288 Aes128Gcm12Encrypter encrypter; |
| 291 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); | 289 EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); |
| 292 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); | 290 EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); |
| 293 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); | 291 EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); |
| 294 } | 292 } |
| 295 | 293 |
| 296 } // namespace test | 294 } // namespace test |
| 297 } // namespace net | 295 } // namespace net |
| OLD | NEW |