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