Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "crypto/encryptor.h" | 5 #include "crypto/encryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 29 | 29 |
| 30 EXPECT_LT(0U, ciphertext.size()); | 30 EXPECT_LT(0U, ciphertext.size()); |
| 31 | 31 |
| 32 std::string decypted; | 32 std::string decypted; |
| 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
| 34 | 34 |
| 35 EXPECT_EQ(plaintext, decypted); | 35 EXPECT_EQ(plaintext, decypted); |
| 36 } | 36 } |
| 37 | 37 |
| 38 TEST(EncryptorTest, EncryptDecryptECB) { | |
| 39 scoped_ptr<crypto::SymmetricKey> key( | |
| 40 crypto::SymmetricKey::DeriveKeyFromPassword( | |
| 41 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 128)); | |
| 42 EXPECT_TRUE(NULL != key.get()); | |
| 43 | |
| 44 crypto::Encryptor encryptor; | |
| 45 // The IV must be exactly as long as the cipher block size. | |
| 46 std::string iv("the iv: 16 bytes"); | |
| 47 EXPECT_EQ(16U, iv.size()); | |
| 48 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::ECB, iv)); | |
|
Ryan Sleevi
2011/05/23 05:55:04
|iv| has no meaning in ECB mode.
| |
| 49 | |
| 50 std::string plaintext("normal plaintext"); | |
| 51 std::string ciphertext; | |
| 52 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
| 53 | |
| 54 EXPECT_LT(0U, ciphertext.size()); | |
| 55 | |
| 56 std::string decypted; | |
| 57 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | |
| 58 | |
| 59 EXPECT_EQ(plaintext, decypted); | |
| 60 } | |
| 61 | |
| 38 // TODO(wtc): add more known-answer tests. Test vectors are available from | 62 // TODO(wtc): add more known-answer tests. Test vectors are available from |
| 39 // http://www.ietf.org/rfc/rfc3602 | 63 // http://www.ietf.org/rfc/rfc3602 |
| 40 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | 64 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 41 // http://gladman.plushost.co.uk/oldsite/AES/index.php | 65 // http://gladman.plushost.co.uk/oldsite/AES/index.php |
| 42 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip | 66 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip |
| 43 | 67 |
| 44 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. | 68 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. |
| 45 TEST(EncryptorTest, EncryptAES256CBC) { | 69 TEST(EncryptorTest, EncryptAES256CBC) { |
| 46 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. | 70 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. |
| 47 static const unsigned char raw_key[] = { | 71 static const unsigned char raw_key[] = { |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 | 248 |
| 225 crypto::Encryptor encryptor; | 249 crypto::Encryptor encryptor; |
| 226 // The IV must be exactly as long a the cipher block size. | 250 // The IV must be exactly as long a the cipher block size. |
| 227 EXPECT_EQ(16U, iv.size()); | 251 EXPECT_EQ(16U, iv.size()); |
| 228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 252 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
| 229 | 253 |
| 230 std::string decrypted; | 254 std::string decrypted; |
| 231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); | 255 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); |
| 232 EXPECT_EQ("", decrypted); | 256 EXPECT_EQ("", decrypted); |
| 233 } | 257 } |
| OLD | NEW |