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

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: endian 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/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 17 matching lines...) Expand all
28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); 28 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
29 29
30 EXPECT_LT(0U, ciphertext.size()); 30 EXPECT_LT(0U, ciphertext.size());
31 31
32 std::string decypted; 32 std::string decypted;
33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); 33 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
34 34
35 EXPECT_EQ(plaintext, decypted); 35 EXPECT_EQ(plaintext, decypted);
36 } 36 }
37 37
38 // ECB mode encryption is only implemented using NSS.
wtc 2011/06/24 18:06:06 ECB => CTR
Alpha Left Google 2011/06/24 18:52:27 Done.
39 #if defined(USE_NSS)
40
41 TEST(EncryptorTest, EncryptDecryptCTR) {
42 scoped_ptr<crypto::SymmetricKey> key(
43 crypto::SymmetricKey::GenerateRandomKey(
44 crypto::SymmetricKey::AES, 128));
45
46 EXPECT_TRUE(NULL != key.get());
47 const std::string kInitialCounter = "0000000000000000";
Chris Palmer 2011/10/03 19:50:09 Since you make callers provide this value, chances
48
49 crypto::Encryptor encryptor;
50 EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CTR, ""));
51 EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter));
52
53 std::string plaintext("normal plaintext of random length");
54 std::string ciphertext;
55 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext));
56 EXPECT_LT(0U, ciphertext.size());
Chris Palmer 2011/10/03 19:50:09 Check for the exact ciphertext size. As I mentione
57
58 std::string decypted;
59 EXPECT_TRUE(encryptor.UpdateCounter(kInitialCounter));
60 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted));
61
62 EXPECT_EQ(plaintext, decypted);
63 }
64
65 TEST(EncryptorTest, CTRCounter) {
66 const int kCounterSize = 16;
67 const char kTest1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
68 uint8 buf[16];
69
70 // Increment 10 times.
71 crypto::Encryptor::Counter counter1(std::string(kTest1, kCounterSize));
72 for (int i = 0; i < 10; ++i)
73 counter1.Increment();
74 counter1.Write(buf);
75 EXPECT_EQ(0, memcmp(buf, kTest1, 15));
76 EXPECT_TRUE(buf[15] == 10);
77
78 // Check corner cases.
79 const char kTest2[] = {0, 0, 0, 0, 0, 0, 0, 0,
80 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
81 const char kExpect2[] = {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
82 crypto::Encryptor::Counter counter2(std::string(kTest2, kCounterSize));
83 counter2.Increment();
84 counter2.Write(buf);
85 EXPECT_EQ(0, memcmp(buf, kExpect2, kCounterSize));
86
87 const char kTest3[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
89 const char kExpect3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
90 crypto::Encryptor::Counter counter3(std::string(kTest3, kCounterSize));
91 counter3.Increment();
92 counter3.Write(buf);
93 EXPECT_EQ(0, memcmp(buf, kExpect3, kCounterSize));
94 }
95
96 #endif
97
38 // TODO(wtc): add more known-answer tests. Test vectors are available from 98 // TODO(wtc): add more known-answer tests. Test vectors are available from
39 // http://www.ietf.org/rfc/rfc3602 99 // http://www.ietf.org/rfc/rfc3602
40 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 100 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
41 // http://gladman.plushost.co.uk/oldsite/AES/index.php 101 // http://gladman.plushost.co.uk/oldsite/AES/index.php
42 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip 102 // http://csrc.nist.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
43 103
44 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt. 104 // NIST SP 800-38A test vector F.2.5 CBC-AES256.Encrypt.
45 TEST(EncryptorTest, EncryptAES256CBC) { 105 TEST(EncryptorTest, EncryptAES256CBC) {
46 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt. 106 // From NIST SP 800-38a test cast F.2.5 CBC-AES256.Encrypt.
47 static const unsigned char raw_key[] = { 107 static const unsigned char raw_key[] = {
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 284
225 crypto::Encryptor encryptor; 285 crypto::Encryptor encryptor;
226 // The IV must be exactly as long a the cipher block size. 286 // The IV must be exactly as long a the cipher block size.
227 EXPECT_EQ(16U, iv.size()); 287 EXPECT_EQ(16U, iv.size());
228 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv)); 288 EXPECT_TRUE(encryptor.Init(sym_key.get(), crypto::Encryptor::CBC, iv));
229 289
230 std::string decrypted; 290 std::string decrypted;
231 EXPECT_FALSE(encryptor.Decrypt("", &decrypted)); 291 EXPECT_FALSE(encryptor.Decrypt("", &decrypted));
232 EXPECT_EQ("", decrypted); 292 EXPECT_EQ("", decrypted);
233 } 293 }
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