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); | |
43 } | |
wtc
2011/06/21 00:46:32
There are compiler built-in or intrinsic functions
Alpha Left Google
2011/06/22 21:25:17
Done.
| |
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, | |
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; | |
101 CHECK(blocks); | |
102 | |
103 *mask_len = blocks * kBlockLength; | |
104 mask->reset(new uint8[*mask_len]); | |
105 | |
106 uint8* buf = mask->get(); | |
107 for (size_t i = 0; i < blocks; ++i) { | |
108 counter_->Write(buf); | |
109 buf += kBlockLength; | |
110 counter_->Increment(); | |
111 } | |
112 } | |
113 | |
114 void Encryptor::MaskMessage(const void* plaintext, | |
115 size_t plaintext_len, | |
116 const void* mask, | |
117 void* ciphertext) const { | |
118 DCHECK_EQ(CTR, mode_); | |
119 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | |
120 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | |
121 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | |
122 | |
123 for (size_t i = 0; i < plaintext_len; ++i) | |
124 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | |
125 } | |
126 | |
127 } // namespace crypto | |
OLD | NEW |