| 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 { |
| 11 | 11 |
| 12 ///////////////////////////////////////////////////////////////////////////// | 12 ///////////////////////////////////////////////////////////////////////////// |
| 13 // Encyptor::Counter Implementation. | 13 // Encyptor::Counter Implementation. |
| 14 Encryptor::Counter::Counter(const base::StringPiece& counter) { | 14 Encryptor::Counter::Counter(const base::StringPiece& counter) { |
| 15 CHECK(sizeof(counter_) == counter.length()); | 15 CHECK(sizeof(counter_) == counter.length()); |
| 16 | 16 |
| 17 memcpy(&counter_, counter.data(), sizeof(counter_)); | 17 memcpy(&counter_, counter.data(), sizeof(counter_)); |
| 18 } | 18 } |
| 19 | 19 |
| 20 Encryptor::Counter::~Counter() { | 20 Encryptor::Counter::~Counter() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 bool Encryptor::Counter::Increment() { | 23 bool Encryptor::Counter::Increment() { |
| 24 uint64 low_num = base::ntohll(counter_.components64[1]); | 24 uint64 low_num = base::NetToHost64(counter_.components64[1]); |
| 25 uint64 new_low_num = low_num + 1; | 25 uint64 new_low_num = low_num + 1; |
| 26 counter_.components64[1] = base::htonll(new_low_num); | 26 counter_.components64[1] = base::HostToNet64(new_low_num); |
| 27 | 27 |
| 28 // If overflow occured then increment the most significant component. | 28 // If overflow occured then increment the most significant component. |
| 29 if (new_low_num < low_num) { | 29 if (new_low_num < low_num) { |
| 30 counter_.components64[0] = | 30 counter_.components64[0] = |
| 31 base::htonll(base::ntohll(counter_.components64[0]) + 1); | 31 base::HostToNet64(base::NetToHost64(counter_.components64[0]) + 1); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // TODO(hclam): Return false if counter value overflows. | 34 // TODO(hclam): Return false if counter value overflows. |
| 35 return true; | 35 return true; |
| 36 } | 36 } |
| 37 | 37 |
| 38 void Encryptor::Counter::Write(void* buf) { | 38 void Encryptor::Counter::Write(void* buf) { |
| 39 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); | 39 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); |
| 40 memcpy(buf_ptr, &counter_, sizeof(counter_)); | 40 memcpy(buf_ptr, &counter_, sizeof(counter_)); |
| 41 } | 41 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 DCHECK_EQ(CTR, mode_); | 88 DCHECK_EQ(CTR, mode_); |
| 89 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | 89 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); |
| 90 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | 90 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); |
| 91 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | 91 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); |
| 92 | 92 |
| 93 for (size_t i = 0; i < plaintext_len; ++i) | 93 for (size_t i = 0; i < plaintext_len; ++i) |
| 94 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | 94 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; |
| 95 } | 95 } |
| 96 | 96 |
| 97 } // namespace crypto | 97 } // namespace crypto |
| OLD | NEW |