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

Side by Side Diff: crypto/encryptor_unittest.cc

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fixed comments Created 9 years, 6 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
« crypto/encryptor_nss.cc ('K') | « crypto/encryptor_nss.cc ('k') | no next file » | 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) 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/rand_util.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
11 #include "crypto/symmetric_key.h" 12 #include "crypto/symmetric_key.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 TEST(EncryptorTest, EncryptDecrypt) { 15 TEST(EncryptorTest, EncryptDecrypt) {
15 scoped_ptr<crypto::SymmetricKey> key( 16 scoped_ptr<crypto::SymmetricKey> key(
16 crypto::SymmetricKey::DeriveKeyFromPassword( 17 crypto::SymmetricKey::DeriveKeyFromPassword(
17 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); 18 crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256));
18 EXPECT_TRUE(NULL != key.get()); 19 EXPECT_TRUE(NULL != key.get());
19 20
20 crypto::Encryptor encryptor; 21 crypto::Encryptor encryptor;
21 // The IV must be exactly as long as the cipher block size. 22 // The IV must be exactly as long as the cipher block size.
22 std::string iv("the iv: 16 bytes"); 23 std::string iv("the iv: 16 bytes");
23 EXPECT_EQ(16U, iv.size()); 24 EXPECT_EQ(16U, iv.size());
24 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); 25 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv));
25 26
26 std::string plaintext("this is the plaintext"); 27 std::string plaintext("this is the plaintext");
27 std::string ciphertext; 28 std::string ciphertext;
28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 29 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
29 30
30 EXPECT_LT(0U, ciphertext.size()); 31 EXPECT_LT(0U, ciphertext.size());
31 32
32 std::string decypted; 33 std::string decypted;
33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); 34 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
34 35
35 EXPECT_EQ(plaintext, decypted); 36 EXPECT_EQ(plaintext, decypted);
36 } 37 }
37 38
39 // ECB mode encryption is only implemented using NSS.
40 #if defined(USE_NSS)
41
42 TEST(EncryptorTest, EncryptDecryptCTR) {
43 scoped_ptr<crypto::SymmetricKey> key(
44 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES,
45 base::RandBytesAsString(16)));
46
47 EXPECT_TRUE(NULL != key.get());
48 const std::string kInitialCounter = "0000000000000000";
49
50 crypto::Encryptor encryptor;
51 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, ""));
52 EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter));
53
54 std::string plaintext("normal plaintext of random length");
55 std::string ciphertext;
56 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
57
58 EXPECT_LT(0U, ciphertext.size());
59
60 std::string decypted;
61 EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter));
62 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
63
64 EXPECT_EQ(plaintext, decypted);
65 }
66
67 TEST(EncryptorTest, CTRCounter) {
68 const int kCounterSize = 16;
69 const char kTest1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
70 uint8 buf[16];
71
72 // Increment 10 times.
73 crypto::Encryptor::Counter counter1(std::string(kTest1, kCounterSize));
74 for (int i = 0; i < 10; ++i)
75 counter1.Increment();
76 counter1.Write(buf);
77 EXPECT_EQ(0, memcmp(buf, kTest1, 15));
78 EXPECT_TRUE(buf[15] == 10);
79
80 // Check corner cases.
81 const char kTest2[] = {0, 0, 0, 0, 0, 0, 0, 0,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
83 const char kExpect2[] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
84 crypto::Encryptor::Counter counter2(std::string(kTest2, kCounterSize));
85 counter2.Increment();
86 counter2.Write(buf);
87 EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize));
88
89 const char kTest3[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
91 const char kExpect3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
92 crypto::Encryptor::Counter counter3(std::string(kTest3, kCounterSize));
93 counter3.Increment();
94 counter3.Write(buf);
95 EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize));
96 }
97
98 #endif
99
38 // TODO(wtc): add more known-answer tests. Test vectors are available from 100 // TODO(wtc): add more known-answer tests. Test vectors are available from
39 // http://www.ietf.org/rfc/rfc3602 101 // http://www.ietf.org/rfc/rfc3602
40 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 102 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
41 // http://gladman.plushost.co.uk/oldsite/AES/index.php 103 // http://gladman.plushost.co.uk/oldsite/AES/index.php
42 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip 104 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
43 105
44 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. 106 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt.
45 TEST(EncryptorTest, EncryptAES256CBC) { 107 TEST(EncryptorTest, EncryptAES256CBC) {
46 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. 108 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt.
47 static const unsigned char raw_key[] = { 109 static const unsigned char raw_key[] = {
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 286
225 crypto::Encryptor encryptor; 287 crypto::Encryptor encryptor;
226 // The IV must be exactly as long a the cipher block size. 288 // The IV must be exactly as long a the cipher block size.
227 EXPECT_EQ(16U, iv.size()); 289 EXPECT_EQ(16U, iv.size());
228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 290 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
229 291
230 std::string decrypted; 292 std::string decrypted;
231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); 293 EXPECT_FALSE(encryptor.Decrypt("", &decrypted));
232 EXPECT_EQ("", decrypted); 294 EXPECT_EQ("", decrypted);
233 } 295 }
OLDNEW
« crypto/encryptor_nss.cc ('K') | « crypto/encryptor_nss.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698