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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 | 292 |
293 crypto::Encryptor encryptor; | 293 crypto::Encryptor encryptor; |
294 // The IV must be exactly as long a the cipher block size. | 294 // The IV must be exactly as long a the cipher block size. |
295 EXPECT_EQ(16U, iv.size()); | 295 EXPECT_EQ(16U, iv.size()); |
296 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); | 296 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); |
297 | 297 |
298 std::string decrypted; | 298 std::string decrypted; |
299 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); | 299 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); |
300 EXPECT_EQ("", decrypted); | 300 EXPECT_EQ("", decrypted); |
301 } | 301 } |
OLD | NEW |