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

Side by Side Diff: crypto/encryptor.cc

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: done 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
OLDNEW
(Empty)
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.
4
5 #include "crypto/encryptor.h"
6
7 #include "base/logging.h"
8
9 namespace crypto {
10
11 namespace {
12
13 const size_t kCounterLength = 16u;
14
15 inline void Set8(void* memory, size_t offset, uint8 v) {
16 static_cast<uint8*>(memory)[offset] = v;
17 }
18
19 inline uint8 Get8(const void* memory, size_t offset) {
20 return static_cast<const uint8*>(memory)[offset];
21 }
22
23 inline void SetBE64(void* memory, uint64 v) {
24 Set8(memory, 0, static_cast<uint8>(v >> 56));
25 Set8(memory, 1, static_cast<uint8>(v >> 48));
26 Set8(memory, 2, static_cast<uint8>(v >> 40));
27 Set8(memory, 3, static_cast<uint8>(v >> 32));
28 Set8(memory, 4, static_cast<uint8>(v >> 24));
29 Set8(memory, 5, static_cast<uint8>(v >> 16));
30 Set8(memory, 6, static_cast<uint8>(v >> 8));
31 Set8(memory, 7, static_cast<uint8>(v >> 0));
32 }
33
34 inline uint64 GetBE64(const void* memory) {
35 return (static_cast<uint64>(Get8(memory, 0)) << 56) |
36 (static_cast<uint64>(Get8(memory, 1)) << 48) |
37 (static_cast<uint64>(Get8(memory, 2)) << 40) |
38 (static_cast<uint64>(Get8(memory, 3)) << 32) |
39 (static_cast<uint64>(Get8(memory, 4)) << 24) |
40 (static_cast<uint64>(Get8(memory, 5)) << 16) |
41 (static_cast<uint64>(Get8(memory, 6)) << 8) |
42 (static_cast<uint64>(Get8(memory, 7)) << 0);
43 }
44
45 } // namespace
46
47 /////////////////////////////////////////////////////////////////////////////
48 // Encyptor::Counter Implementation.
49 Encryptor::Counter::Counter(const std::string& counter) {
50 CHECK_EQ(kCounterLength, counter.length());
51
52 high_num_ = GetBE64(counter.data());
53 low_num_ = GetBE64(counter.data() + sizeof(high_num_));
54 }
55
56 Encryptor::Counter::~Counter() {
57 }
58
59 void Encryptor::Counter::Increment() {
60 uint64 old_num = low_num_;
61 ++low_num_;
62
63 // Overflow occurs.
64 if (low_num_ < old_num)
65 ++high_num_;
66 }
67
68 void Encryptor::Counter::Write(void* buf) {
69 uint8* buf_ptr = reinterpret_cast<uint8*>(buf);
70
71 SetBE64(buf_ptr, high_num_);
72 SetBE64(buf_ptr + sizeof(high_num_), low_num_);
73 }
74
75 size_t Encryptor::Counter::GetLengthInBytes() const {
76 return kCounterLength;
77 }
78
79 /////////////////////////////////////////////////////////////////////////////
80 // Partial Encryptor Implementation.
81
82 bool Encryptor::UpdateCounter(const std::string& counter) {
83 if (mode_ != CTR)
84 return false;
85 if (counter.length() != kCounterLength)
86 return false;
87
88 counter_.reset(new Counter(counter));
89 return true;
90 }
91
92 void Encryptor::GenerateCounterMask(size_t plaintext_len,
Ryan Sleevi 2011/06/15 06:27:00 pathological abuse of/hostile attack of API: Step
93 scoped_array<uint8>* mask,
94 size_t* mask_len) {
95 DCHECK_EQ(CTR, mode_);
96 CHECK(mask);
97 CHECK(mask_len);
98
99 const size_t kBlockLength = counter_->GetLengthInBytes();
100 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
Ryan Sleevi 2011/06/15 06:27:00 Step 2: size_t blocks = (plaintext_len + kBlockLen
101
102 *mask_len = blocks * kBlockLength;
Ryan Sleevi 2011/06/15 06:27:00 Step 3: *mask_len = blocks (0) * kBlockLength (16)
103 mask->reset(new uint8[*mask_len]);
Ryan Sleevi 2011/06/15 06:27:00 Step 4: BUG?: mask->reset(new uint8[0]) I know in
Alpha Left Google 2011/06/15 18:54:42 I don't think de-referencing a [0] pointer would b
104
105 uint8* buf = mask->get();
106 for (size_t i = 0; i < blocks; ++i) {
107 counter_->Write(buf);
108 buf += kBlockLength;
109 counter_->Increment();
110 }
111 }
112
113 void Encryptor::MaskMessage(const void* plaintext,
114 size_t plaintext_len,
115 const void* mask,
116 void* ciphertext) const {
117 DCHECK_EQ(CTR, mode_);
118 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext);
119 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask);
120 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext);
121
122 for (size_t i = 0; i < plaintext_len; ++i)
123 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i];
124 }
125
126 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_nss.cc » ('j') | crypto/encryptor_nss.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698