Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: base/crypto/encryptor_unittest.cc

Issue 1528021: Implement PBKDF2-based key derivation, random key generation,... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Make albertb's suggested changes. Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/crypto/encryptor_nss.cc ('k') | base/crypto/encryptor_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" 5 #include "base/crypto/encryptor.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/crypto/symmetric_key.h" 9 #include "base/crypto/symmetric_key.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
11 #include "base/string_util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
13 12
14 #if defined(USE_NSS) || defined(OS_MACOSX) 13 TEST(EncryptorTest, EncryptDecrypt) {
15 #define MAYBE(name) name
16 #else
17 #define MAYBE(name) DISABLED_ ## name
18 #endif
19
20 TEST(EncryptorTest, MAYBE(EncryptDecrypt)) {
21 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( 14 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword(
22 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); 15 base::SymmetricKey::AES, "password", "saltiest", 1000, 256));
23 EXPECT_TRUE(NULL != key.get()); 16 EXPECT_TRUE(NULL != key.get());
24 17
25 base::Encryptor encryptor; 18 base::Encryptor encryptor;
26 // The IV must be exactly as long a the cipher block size. 19 // The IV must be exactly as long as the cipher block size.
27 std::string iv("the iv: 16 bytes"); 20 std::string iv("the iv: 16 bytes");
21 EXPECT_EQ(16U, iv.size());
28 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv)); 22 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv));
29 23
30 std::string plaintext("this is the plaintext"); 24 std::string plaintext("this is the plaintext");
31 std::string ciphertext; 25 std::string ciphertext;
32 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 26 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
33 27
34 EXPECT_LT(0U, ciphertext.size()); 28 EXPECT_LT(0U, ciphertext.size());
35 29
36 std::string decypted; 30 std::string decypted;
37 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); 31 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
38 32
39 EXPECT_EQ(plaintext, decypted); 33 EXPECT_EQ(plaintext, decypted);
40 } 34 }
35
36 // TODO(wtc): add more known-answer tests. Test vectors are available from
37 // http://www.ietf.org/rfc/rfc3602
38 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
39 // http://gladman.plushost.co.uk/oldsite/AES/index.php
40 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
41
42 // TODO(wtc): enable this test for other platforms after
43 // base::SymmetricKey::Import is implemented.
44 #if defined(OS_WIN)
45 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt.
46 TEST(EncryptorTest, EncryptAES256CBC) {
47 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt.
48 static const unsigned char raw_key[] = {
49 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
50 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
51 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
52 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
53 };
54 static const unsigned char raw_iv[] = {
55 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
56 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
57 };
58 static const unsigned char raw_plaintext[] = {
59 // Block #1
60 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
61 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
62 // Block #2
63 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
64 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
65 // Block #3
66 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
67 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
68 // Block #4
69 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
70 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
71 };
72 static const unsigned char raw_ciphertext[] = {
73 // Block #1
74 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba,
75 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
76 // Block #2
77 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d,
78 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
79 // Block #3
80 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf,
81 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
82 // Block #4
83 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
84 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
85 // PKCS #5 padding, encrypted.
86 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2,
87 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44
88 };
89
90 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::Import(
91 base::SymmetricKey::AES, raw_key, sizeof(raw_key)));
92 EXPECT_TRUE(NULL != key.get());
93
94 base::Encryptor encryptor;
95 // The IV must be exactly as long a the cipher block size.
96 std::string iv(reinterpret_cast<const char*>(raw_iv), sizeof(raw_iv));
97 EXPECT_EQ(16U, iv.size());
98 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv));
99
100 std::string plaintext(reinterpret_cast<const char*>(raw_plaintext),
101 sizeof(raw_plaintext));
102 std::string ciphertext;
103 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
104
105 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size());
106 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size()));
107
108 std::string decypted;
109 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
110
111 EXPECT_EQ(plaintext, decypted);
112 }
113 #endif // OS_WIN
OLDNEW
« no previous file with comments | « base/crypto/encryptor_nss.cc ('k') | base/crypto/encryptor_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698