| 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 "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 9 | 9 |
| 10 namespace crypto { | 10 namespace crypto { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 bool Encryptor::GenerateCounterMask(size_t plaintext_len, | 60 bool Encryptor::GenerateCounterMask(size_t plaintext_len, |
| 61 uint8* mask, | 61 uint8* mask, |
| 62 size_t* mask_len) { | 62 size_t* mask_len) { |
| 63 DCHECK_EQ(CTR, mode_); | 63 DCHECK_EQ(CTR, mode_); |
| 64 CHECK(mask); | 64 CHECK(mask); |
| 65 CHECK(mask_len); | 65 CHECK(mask_len); |
| 66 | 66 |
| 67 const size_t kBlockLength = counter_->GetLengthInBytes(); | 67 const size_t kBlockLength = counter_->GetLengthInBytes(); |
| 68 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | 68 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; |
| 69 CHECK(blocks); | 69 if (blocks == 0) |
| 70 return false; |
| 70 | 71 |
| 71 *mask_len = blocks * kBlockLength; | 72 *mask_len = blocks * kBlockLength; |
| 72 | 73 |
| 73 for (size_t i = 0; i < blocks; ++i) { | 74 for (size_t i = 0; i < blocks; ++i) { |
| 74 counter_->Write(mask); | 75 counter_->Write(mask); |
| 75 mask += kBlockLength; | 76 mask += kBlockLength; |
| 76 | 77 |
| 77 bool ret = counter_->Increment(); | 78 bool ret = counter_->Increment(); |
| 78 if (!ret) | 79 if (!ret) |
| 79 return false; | 80 return false; |
| 80 } | 81 } |
| 81 return true; | 82 return true; |
| 82 } | 83 } |
| 83 | 84 |
| 84 void Encryptor::MaskMessage(const void* plaintext, | 85 void Encryptor::MaskMessage(const void* plaintext, |
| 85 size_t plaintext_len, | 86 size_t plaintext_len, |
| 86 const void* mask, | 87 const void* mask, |
| 87 void* ciphertext) const { | 88 void* ciphertext) const { |
| 88 DCHECK_EQ(CTR, mode_); | 89 DCHECK_EQ(CTR, mode_); |
| 89 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | 90 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); |
| 90 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | 91 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); |
| 91 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | 92 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); |
| 92 | 93 |
| 93 for (size_t i = 0; i < plaintext_len; ++i) | 94 for (size_t i = 0; i < plaintext_len; ++i) |
| 94 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | 95 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; |
| 95 } | 96 } |
| 96 | 97 |
| 97 } // namespace crypto | 98 } // namespace crypto |
| OLD | NEW |