OLD | NEW |
---|---|
(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); | |
wtc
2011/06/14 18:11:06
Nit: it may be better to violate the Style Guide a
Alpha Left Google
2011/06/14 22:22:43
Done.
| |
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); | |
wtc
2011/06/14 18:11:06
This typecast to unit8* is not necessary because
y
Alpha Left Google
2011/06/14 22:22:43
This is because of the pointer arithemetic below a
| |
70 | |
71 SetBE64(buf_ptr, high_num_); | |
72 SetBE64(buf_ptr + sizeof(high_num_), low_num_); | |
73 } | |
74 | |
75 const int Encryptor::Counter::GetLengthInBytes() const { | |
76 return kCounterLength; | |
77 } | |
78 | |
79 ///////////////////////////////////////////////////////////////////////////// | |
80 // Partial Encryptor Implementation. | |
wtc
2011/06/14 18:11:06
Nit: add a blank line.
Alpha Left Google
2011/06/14 22:22:43
Done.
| |
81 bool Encryptor::UpdateCounter(const std::string& counter) { | |
82 if (mode_ != CTR) | |
83 return false; | |
84 if (counter.length() != kCounterLength) | |
85 return false; | |
86 | |
87 counter_.reset(new Counter(counter)); | |
88 return true; | |
89 } | |
90 | |
91 void Encryptor::GenerateCounterMask(size_t plaintext_len, | |
92 scoped_array<uint8>* mask, | |
93 size_t* mask_len) { | |
94 DCHECK_EQ(CTR, mode_); | |
95 CHECK(mask); | |
96 CHECK(mask_len); | |
97 | |
98 const int kBlockLength = counter_->GetLengthInBytes(); | |
wtc
2011/06/14 18:11:06
Use size_t.
Alpha Left Google
2011/06/14 22:22:43
Done.
| |
99 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | |
100 | |
101 *mask_len = blocks * kBlockLength; | |
102 mask->reset(new uint8[*mask_len]); | |
103 | |
104 uint8* buf = mask->get(); | |
105 for (size_t i = 0; i < blocks; ++i) { | |
106 counter_->Write(buf); | |
107 buf += kBlockLength; | |
108 counter_->Increment(); | |
109 } | |
110 } | |
111 | |
112 void Encryptor::MaskMessage(const void* plaintext, | |
113 size_t plaintext_len, | |
114 const void* mask, | |
115 void* ciphertext) const { | |
116 DCHECK_EQ(CTR, mode_); | |
117 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | |
118 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | |
119 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | |
120 | |
121 for (size_t i = 0; i < plaintext_len; ++i) | |
122 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | |
123 } | |
124 | |
125 } // namespace crypto | |
OLD | NEW |