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 // ECB mode encryption is only implemented using NSS. | |
| 39 #if defined(OS_LINUX) && defined(USE_NSS) | |
|
Ryan Sleevi
2011/06/02 01:19:23
nit: Just "defined(USE_NSS)" is sufficient.
| |
| 40 | |
| 41 TEST(EncryptorTest, EncryptDecryptECB) { | |
| 42 scoped_ptr<crypto::SymmetricKey> key( | |
| 43 crypto::SymmetricKey::DeriveKeyFromPassword( | |
|
Ryan Sleevi
2011/06/02 01:19:23
It would be better for the valgrind/heapchecker bo
| |
| 44 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 128)); | |
| 45 EXPECT_TRUE(NULL != key.get()); | |
|
Ryan Sleevi
2011/06/02 01:19:23
nit?: EXPECT_NE?
My own opinion is that it's fine
| |
| 46 | |
| 47 crypto::Encryptor encryptor; | |
| 48 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::ECB, "")); | |
| 49 | |
| 50 std::string plaintext("normal plaintext"); | |
|
Ryan Sleevi
2011/06/02 01:19:23
It's not clear from this string, compared with lin
| |
| 51 std::string ciphertext; | |
| 52 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | |
|
Ryan Sleevi
2011/06/02 01:19:23
nit: Since this is ECB mode, I think you may want
| |
| 53 | |
| 54 EXPECT_LT(0U, ciphertext.size()); | |
| 55 | |
| 56 std::string decypted; | |
| 57 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | |
|
Ryan Sleevi
2011/06/02 01:19:23
nit: Is the "normal" use case to use two Encryptor
| |
| 58 | |
| 59 EXPECT_EQ(plaintext, decypted); | |
| 60 } | |
| 61 | |
| 62 TEST(EncryptorTest, ECBNoPadding) { | |
| 63 scoped_ptr<crypto::SymmetricKey> key( | |
| 64 crypto::SymmetricKey::DeriveKeyFromPassword( | |
| 65 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 128)); | |
| 66 EXPECT_TRUE(NULL != key.get()); | |
| 67 | |
| 68 crypto::Encryptor encryptor; | |
| 69 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::ECB, "")); | |
| 70 | |
| 71 std::string plaintext("invalid plaintext"); | |
| 72 std::string ciphertext; | |
| 73 EXPECT_FALSE(encryptor.Encrypt(plaintext, &ciphertext)); | |
| 74 | |
| 75 ciphertext = "invalid cipher text"; | |
| 76 std::string decypted; | |
| 77 EXPECT_FALSE(encryptor.Decrypt(ciphertext, &decypted)); | |
| 78 } | |
| 79 | |
| 80 #endif | |
| 81 | |
| 38 // TODO(wtc): add more known-answer tests. Test vectors are available from | 82 // TODO(wtc): add more known-answer tests. Test vectors are available from |
| 39 // http://www.ietf.org/rfc/rfc3602 | 83 // http://www.ietf.org/rfc/rfc3602 |
| 40 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | 84 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 41 // http://gladman.plushost.co.uk/oldsite/AES/index.php | 85 // http://gladman.plushost.co.uk/oldsite/AES/index.php |
| 42 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip | 86 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip |
| 43 | 87 |
| 44 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. | 88 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. |
| 45 TEST(EncryptorTest, EncryptAES256CBC) { | 89 TEST(EncryptorTest, EncryptAES256CBC) { |
| 46 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. | 90 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. |
| 47 static const unsigned char raw_key[] = { | 91 static const unsigned char raw_key[] = { |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 | 268 |
| 225 crypto::Encryptor encryptor; | 269 crypto::Encryptor encryptor; |
| 226 // The IV must be exactly as long a the cipher block size. | 270 // The IV must be exactly as long a the cipher block size. |
| 227 EXPECT_EQ(16U, iv.size()); | 271 EXPECT_EQ(16U, iv.size()); |
| 228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 272 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
| 229 | 273 |
| 230 std::string decrypted; | 274 std::string decrypted; |
| 231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); | 275 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); |
| 232 EXPECT_EQ("", decrypted); | 276 EXPECT_EQ("", decrypted); |
| 233 } | 277 } |
| OLD | NEW |