Chromium Code Reviews| Index: crypto/encryptor_unittest.cc |
| diff --git a/crypto/encryptor_unittest.cc b/crypto/encryptor_unittest.cc |
| index b916854a9d81e9b71cc51457bc4512dcdf1ae602..3a36641fb65bf7f6fcf372969f9a5daa505e1115 100644 |
| --- a/crypto/encryptor_unittest.cc |
| +++ b/crypto/encryptor_unittest.cc |
| @@ -6,6 +6,7 @@ |
| #include <string> |
| +#include "base/rand_util.h" |
|
Ryan Sleevi
2011/06/15 06:27:00
not needed; see below.
|
| #include "base/memory/scoped_ptr.h" |
| #include "base/string_number_conversions.h" |
| #include "crypto/symmetric_key.h" |
| @@ -35,6 +36,67 @@ TEST(EncryptorTest, EncryptDecrypt) { |
| EXPECT_EQ(plaintext, decypted); |
| } |
| +// ECB mode encryption is only implemented using NSS. |
| +#if defined(USE_NSS) |
| + |
| +TEST(EncryptorTest, EncryptDecryptCTR) { |
| + scoped_ptr<crypto::SymmetricKey> key( |
| + crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, |
| + base::RandBytesAsString(16))); |
|
Ryan Sleevi
2011/06/15 06:27:00
nit: crypto::SymmetricKey::GenerateRandomKey(crypt
Alpha Left Google
2011/06/15 18:54:42
Done.
|
| + |
| + EXPECT_TRUE(NULL != key.get()); |
| + const std::string kInitialCounter = "0000000000000000"; |
| + |
| + crypto::Encryptor encryptor; |
| + EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, "")); |
| + EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter)); |
| + |
| + std::string plaintext("normal plaintext of random length"); |
| + std::string ciphertext; |
| + EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| + |
| + EXPECT_LT(0U, ciphertext.size()); |
| + |
| + std::string decypted; |
| + EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter)); |
| + EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
| + |
| + EXPECT_EQ(plaintext, decypted); |
| +} |
| + |
| +TEST(EncryptorTest, CTRCounter) { |
| + const int kCounterSize = 16; |
| + const char kTest1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| + uint8 buf[16]; |
| + |
| + // Increment 10 times. |
| + crypto::Encryptor::Counter counter1(std::string(kTest1, kCounterSize)); |
| + for (int i = 0; i < 10; ++i) |
| + counter1.Increment(); |
| + counter1.Write(buf); |
| + EXPECT_EQ(0, memcmp(buf, kTest1, 15)); |
| + EXPECT_TRUE(buf[15] == 10); |
| + |
| + // Check corner cases. |
| + const char kTest2[] = {0, 0, 0, 0, 0, 0, 0, 0, |
| + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| + const char kExpect2[] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
| + crypto::Encryptor::Counter counter2(std::string(kTest2, kCounterSize)); |
| + counter2.Increment(); |
| + counter2.Write(buf); |
| + EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize)); |
| + |
| + const char kTest3[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| + const char kExpect3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| + crypto::Encryptor::Counter counter3(std::string(kTest3, kCounterSize)); |
| + counter3.Increment(); |
| + counter3.Write(buf); |
| + EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize)); |
| +} |
| + |
| +#endif |
| + |
| // TODO(wtc): add more known-answer tests. Test vectors are available from |
| // http://www.ietf.org/rfc/rfc3602 |
| // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |