Updating trunk VERSION from 799.0 to 800.0
[chromium-blink-merge.git] / crypto / encryptor_unittest.cc
blobb916854a9d81e9b71cc51457bc4512dcdf1ae602
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
3 // found in the LICENSE file.
5 #include "crypto/encryptor.h"
7 #include <string>
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_number_conversions.h"
11 #include "crypto/symmetric_key.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 TEST(EncryptorTest, EncryptDecrypt) {
15 scoped_ptr<crypto::SymmetricKey> key(
16 crypto::SymmetricKey::DeriveKeyFromPassword(
17 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
18 EXPECT_TRUE(NULL != key.get());
20 crypto::Encryptor encryptor;
21 // The IV must be exactly as long as the cipher block size.
22 std::string iv("the iv: 16 bytes");
23 EXPECT_EQ(16U, iv.size());
24 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
26 std::string plaintext("this is the plaintext");
27 std::string ciphertext;
28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
30 EXPECT_LT(0U, ciphertext.size());
32 std::string decypted;
33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
35 EXPECT_EQ(plaintext, decypted);
38 // TODO(wtc): add more known-answer tests. Test vectors are available from
39 // http://www.ietf.org/rfc/rfc3602
40 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
41 // http://gladman.plushost.co.uk/oldsite/AES/index.php
42 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
44 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt.
45 TEST(EncryptorTest, EncryptAES256CBC) {
46 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt.
47 static const unsigned char raw_key[] = {
48 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
49 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
50 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
51 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
53 static const unsigned char raw_iv[] = {
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
57 static const unsigned char raw_plaintext[] = {
58 // Block #1
59 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
60 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
61 // Block #2
62 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
63 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
64 // Block #3
65 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
66 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
67 // Block #4
68 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
69 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
71 static const unsigned char raw_ciphertext[] = {
72 // Block #1
73 0xf5, 0x8c, 0x4c, 0x04, 0xd6, 0xe5, 0xf1, 0xba,
74 0x77, 0x9e, 0xab, 0xfb, 0x5f, 0x7b, 0xfb, 0xd6,
75 // Block #2
76 0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d,
77 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d,
78 // Block #3
79 0x39, 0xf2, 0x33, 0x69, 0xa9, 0xd9, 0xba, 0xcf,
80 0xa5, 0x30, 0xe2, 0x63, 0x04, 0x23, 0x14, 0x61,
81 // Block #4
82 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
83 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b,
84 // PKCS #5 padding, encrypted.
85 0x3f, 0x46, 0x17, 0x96, 0xd6, 0xb0, 0xd6, 0xb2,
86 0xe0, 0xc2, 0xa7, 0x2b, 0x4d, 0x80, 0xe6, 0x44
89 std::string key(reinterpret_cast<const char*>(raw_key), sizeof(raw_key));
90 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
91 crypto::SymmetricKey::AES, key));
92 ASSERT_TRUE(NULL != sym_key.get());
94 crypto::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(sym_key.get(), crypto::Encryptor::CBC, iv));
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));
105 EXPECT_EQ(sizeof(raw_ciphertext), ciphertext.size());
106 EXPECT_EQ(0, memcmp(ciphertext.data(), raw_ciphertext, ciphertext.size()));
108 std::string decypted;
109 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
111 EXPECT_EQ(plaintext, decypted);
114 // Expected output derived from the NSS implementation.
115 TEST(EncryptorTest, EncryptAES128CBCRegression) {
116 std::string key = "128=SixteenBytes";
117 std::string iv = "Sweet Sixteen IV";
118 std::string plaintext = "Plain text with a g-clef U+1D11E \360\235\204\236";
119 std::string expected_ciphertext_hex =
120 "D4A67A0BA33C30F207344D81D1E944BBE65587C3D7D9939A"
121 "C070C62B9C15A3EA312EA4AD1BC7929F4D3C16B03AD5ADA8";
123 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
124 crypto::SymmetricKey::AES, key));
125 ASSERT_TRUE(NULL != sym_key.get());
127 crypto::Encryptor encryptor;
128 // The IV must be exactly as long a the cipher block size.
129 EXPECT_EQ(16U, iv.size());
130 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
132 std::string ciphertext;
133 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
134 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
135 ciphertext.size()));
137 std::string decypted;
138 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
139 EXPECT_EQ(plaintext, decypted);
142 // Expected output derived from the NSS implementation.
143 TEST(EncryptorTest, EncryptAES192CBCRegression) {
144 std::string key = "192bitsIsTwentyFourByte!";
145 std::string iv = "Sweet Sixteen IV";
146 std::string plaintext = "Small text";
147 std::string expected_ciphertext_hex = "78DE5D7C2714FC5C61346C5416F6C89A";
149 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
150 crypto::SymmetricKey::AES, key));
151 ASSERT_TRUE(NULL != sym_key.get());
153 crypto::Encryptor encryptor;
154 // The IV must be exactly as long a the cipher block size.
155 EXPECT_EQ(16U, iv.size());
156 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
158 std::string ciphertext;
159 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
160 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
161 ciphertext.size()));
163 std::string decypted;
164 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
165 EXPECT_EQ(plaintext, decypted);
168 // Not all platforms allow import/generation of symmetric keys with an
169 // unsupported size.
170 #if !defined(OS_WIN) && !defined(USE_NSS)
171 TEST(EncryptorTest, UnsupportedKeySize) {
172 std::string key = "7 = bad";
173 std::string iv = "Sweet Sixteen IV";
174 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
175 crypto::SymmetricKey::AES, key));
176 ASSERT_TRUE(NULL != sym_key.get());
178 crypto::Encryptor encryptor;
179 // The IV must be exactly as long a the cipher block size.
180 EXPECT_EQ(16U, iv.size());
181 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
183 #endif // unsupported platforms.
185 TEST(EncryptorTest, UnsupportedIV) {
186 std::string key = "128=SixteenBytes";
187 std::string iv = "OnlyForteen :(";
188 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
189 crypto::SymmetricKey::AES, key));
190 ASSERT_TRUE(NULL != sym_key.get());
192 crypto::Encryptor encryptor;
193 EXPECT_FALSE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
196 TEST(EncryptorTest, EmptyEncrypt) {
197 std::string key = "128=SixteenBytes";
198 std::string iv = "Sweet Sixteen IV";
199 std::string plaintext;
200 std::string expected_ciphertext_hex = "8518B8878D34E7185E300D0FCC426396";
202 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
203 crypto::SymmetricKey::AES, key));
204 ASSERT_TRUE(NULL != sym_key.get());
206 crypto::Encryptor encryptor;
207 // The IV must be exactly as long a the cipher block size.
208 EXPECT_EQ(16U, iv.size());
209 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
211 std::string ciphertext;
212 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
213 EXPECT_EQ(expected_ciphertext_hex, base::HexEncode(ciphertext.data(),
214 ciphertext.size()));
217 TEST(EncryptorTest, EmptyDecrypt) {
218 std::string key = "128=SixteenBytes";
219 std::string iv = "Sweet Sixteen IV";
221 scoped_ptr<crypto::SymmetricKey> sym_key(crypto::SymmetricKey::Import(
222 crypto::SymmetricKey::AES, key));
223 ASSERT_TRUE(NULL != sym_key.get());
225 crypto::Encryptor encryptor;
226 // The IV must be exactly as long a the cipher block size.
227 EXPECT_EQ(16U, iv.size());
228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
230 std::string decrypted;
231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted));
232 EXPECT_EQ("", decrypted);