| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 71 } |
| 72 | 72 |
| 73 TEST(EncryptorTest, CTRCounter) { | 73 TEST(EncryptorTest, CTRCounter) { |
| 74 const int kCounterSize = 16; | 74 const int kCounterSize = 16; |
| 75 const char kTest1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 75 const char kTest1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 76 uint8 buf[16]; | 76 uint8 buf[16]; |
| 77 | 77 |
| 78 // Increment 10 times. | 78 // Increment 10 times. |
| 79 crypto::Encryptor::Counter counter1(std::string(kTest1, kCounterSize)); | 79 crypto::Encryptor::Counter counter1(std::string(kTest1, kCounterSize)); |
| 80 for (int i = 0; i < 10; ++i) | 80 for (int i = 0; i < 10; ++i) |
| 81 counter1.Increment(); | 81 EXPECT_TRUE(counter1.Increment()); |
| 82 counter1.Write(buf); | 82 counter1.Write(buf); |
| 83 EXPECT_EQ(0, memcmp(buf, kTest1, 15)); | 83 EXPECT_EQ(0, memcmp(buf, kTest1, 15)); |
| 84 EXPECT_TRUE(buf[15] == 10); | 84 EXPECT_TRUE(buf[15] == 10); |
| 85 | 85 |
| 86 // Check corner cases. | 86 // Check corner cases. |
| 87 const char kTest2[] = {0, 0, 0, 0, 0, 0, 0, 0, | 87 const char kTest2[] = {0, 0, 0, 0, 0, 0, 0, 0, |
| 88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | 88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 89 const char kExpect2[] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; | 89 const char kExpect2[] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 90 crypto::Encryptor::Counter counter2(std::string(kTest2, kCounterSize)); | 90 crypto::Encryptor::Counter counter2(std::string(kTest2, kCounterSize)); |
| 91 counter2.Increment(); | 91 EXPECT_TRUE(counter2.Increment()); |
| 92 counter2.Write(buf); | 92 counter2.Write(buf); |
| 93 EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize)); | 93 EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize)); |
| 94 | 94 |
| 95 const char kTest3[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | 95 const char kTest3[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | 96 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 97 const char kExpect3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | 97 const char kExpect3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| 98 crypto::Encryptor::Counter counter3(std::string(kTest3, kCounterSize)); | 98 crypto::Encryptor::Counter counter3(std::string(kTest3, kCounterSize)); |
| 99 counter3.Increment(); | 99 EXPECT_TRUE(counter3.Increment()); |
| 100 counter3.Write(buf); | 100 counter3.Write(buf); |
| 101 EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize)); | 101 EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 #endif | 104 #endif |
| 105 | 105 |
| 106 // TODO(wtc): add more known-answer tests. Test vectors are available from | 106 // TODO(wtc): add more known-answer tests. Test vectors are available from |
| 107 // http://www.ietf.org/rfc/rfc3602 | 107 // http://www.ietf.org/rfc/rfc3602 |
| 108 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf | 108 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 109 // http://gladman.plushost.co.uk/oldsite/AES/index.php | 109 // http://gladman.plushost.co.uk/oldsite/AES/index.php |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 crypto::Encryptor encryptor; | 274 crypto::Encryptor encryptor; |
| 275 // The IV must be exactly as long a the cipher block size. | 275 // The IV must be exactly as long a the cipher block size. |
| 276 EXPECT_EQ(16U, iv.size()); | 276 EXPECT_EQ(16U, iv.size()); |
| 277 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 277 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
| 278 | 278 |
| 279 std::string ciphertext; | 279 std::string ciphertext; |
| 280 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 280 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 281 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), | 281 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(), |
| 282 ciphertext.size())); | 282 ciphertext.size())); |
| 283 } | 283 } |
| OLD | NEW |