OLD | NEW |
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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 // Include headers to provide bswap for all platforms. | 10 // Include headers to provide bswap for all platforms. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 if (counter.length() != 16u) | 81 if (counter.length() != 16u) |
82 return false; | 82 return false; |
83 | 83 |
84 counter_.reset(new Counter(counter)); | 84 counter_.reset(new Counter(counter)); |
85 return true; | 85 return true; |
86 } | 86 } |
87 | 87 |
88 bool Encryptor::GenerateCounterMask(size_t plaintext_len, | 88 bool Encryptor::GenerateCounterMask(size_t plaintext_len, |
89 uint8* mask, | 89 uint8* mask, |
90 size_t* mask_len) { | 90 size_t* mask_len) { |
91 DCHECK_EQ(CTR, mode_); | 91 if (!mask || !mask_len || mode_ != CTR) |
92 CHECK(mask); | 92 return false; |
93 CHECK(mask_len); | |
94 | 93 |
95 const size_t kBlockLength = counter_->GetLengthInBytes(); | 94 const size_t kBlockLength = counter_->GetLengthInBytes(); |
96 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | 95 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; |
97 CHECK(blocks); | 96 if (blocks == 0) |
| 97 return false; |
98 | 98 |
99 *mask_len = blocks * kBlockLength; | 99 *mask_len = blocks * kBlockLength; |
100 | 100 |
101 for (size_t i = 0; i < blocks; ++i) { | 101 for (size_t i = 0; i < blocks; ++i) { |
102 counter_->Write(mask); | 102 counter_->Write(mask); |
103 mask += kBlockLength; | 103 mask += kBlockLength; |
104 | 104 |
105 bool ret = counter_->Increment(); | 105 bool ret = counter_->Increment(); |
106 if (!ret) | 106 if (!ret) |
107 return false; | 107 return false; |
108 } | 108 } |
109 return true; | 109 return true; |
110 } | 110 } |
111 | 111 |
112 void Encryptor::MaskMessage(const void* plaintext, | 112 void Encryptor::MaskMessage(const void* plaintext, |
113 size_t plaintext_len, | 113 size_t plaintext_len, |
114 const void* mask, | 114 const void* mask, |
115 void* ciphertext) const { | 115 void* ciphertext) const { |
116 DCHECK_EQ(CTR, mode_); | 116 DCHECK_EQ(CTR, mode_); |
117 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | 117 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); |
118 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | 118 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); |
119 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | 119 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); |
120 | 120 |
121 for (size_t i = 0; i < plaintext_len; ++i) | 121 for (size_t i = 0; i < plaintext_len; ++i) |
122 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | 122 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; |
123 } | 123 } |
124 | 124 |
125 } // namespace crypto | 125 } // namespace crypto |
OLD | NEW |