| 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 "base/sys_byteorder.h" |
| 9 | |
| 10 // Include headers to provide bswap for all platforms. | |
| 11 #if defined(COMPILER_MSVC) | |
| 12 #include <stdlib.h> | |
| 13 #define bswap_16(x) _byteswap_ushort(x) | |
| 14 #define bswap_32(x) _byteswap_ulong(x) | |
| 15 #define bswap_64(x) _byteswap_uint64(x) | |
| 16 #elif defined(OS_MACOSX) | |
| 17 #include <libkern/OSByteOrder.h> | |
| 18 #define bswap_16(x) OSSwapInt16(x) | |
| 19 #define bswap_32(x) OSSwapInt32(x) | |
| 20 #define bswap_64(x) OSSwapInt64(x) | |
| 21 #elif defined(OS_OPENBSD) | |
| 22 #include <sys/endian.h> | |
| 23 #define bswap_16(x) swap16(x) | |
| 24 #define bswap_32(x) swap32(x) | |
| 25 #define bswap_64(x) swap64(x) | |
| 26 #else | |
| 27 #include <byteswap.h> | |
| 28 #endif | |
| 29 | |
| 30 #if defined(ARCH_CPU_LITTLE_ENDIAN) | |
| 31 #define ntoh_64(x) bswap_64(x) | |
| 32 #define hton_64(x) bswap_64(x) | |
| 33 #else | |
| 34 #define ntoh_64(x) (x) | |
| 35 #define hton_64(x) (x) | |
| 36 #endif | |
| 37 | 9 |
| 38 namespace crypto { | 10 namespace crypto { |
| 39 | 11 |
| 40 ///////////////////////////////////////////////////////////////////////////// | 12 ///////////////////////////////////////////////////////////////////////////// |
| 41 // Encyptor::Counter Implementation. | 13 // Encyptor::Counter Implementation. |
| 42 Encryptor::Counter::Counter(const base::StringPiece& counter) { | 14 Encryptor::Counter::Counter(const base::StringPiece& counter) { |
| 43 CHECK(sizeof(counter_) == counter.length()); | 15 CHECK(sizeof(counter_) == counter.length()); |
| 44 | 16 |
| 45 memcpy(&counter_, counter.data(), sizeof(counter_)); | 17 memcpy(&counter_, counter.data(), sizeof(counter_)); |
| 46 } | 18 } |
| 47 | 19 |
| 48 Encryptor::Counter::~Counter() { | 20 Encryptor::Counter::~Counter() { |
| 49 } | 21 } |
| 50 | 22 |
| 51 bool Encryptor::Counter::Increment() { | 23 bool Encryptor::Counter::Increment() { |
| 52 uint64 low_num = ntoh_64(counter_.components64[1]); | 24 uint64 low_num = base::ntohll(counter_.components64[1]); |
| 53 uint64 new_low_num = low_num + 1; | 25 uint64 new_low_num = low_num + 1; |
| 54 counter_.components64[1] = hton_64(new_low_num); | 26 counter_.components64[1] = base::htonll(new_low_num); |
| 55 | 27 |
| 56 // If overflow occured then increment the most significant component. | 28 // If overflow occured then increment the most significant component. |
| 57 if (new_low_num < low_num) { | 29 if (new_low_num < low_num) { |
| 58 counter_.components64[0] = | 30 counter_.components64[0] = |
| 59 hton_64(ntoh_64(counter_.components64[0]) + 1); | 31 base::htonll(base::ntohll(counter_.components64[0]) + 1); |
| 60 } | 32 } |
| 61 | 33 |
| 62 // TODO(hclam): Return false if counter value overflows. | 34 // TODO(hclam): Return false if counter value overflows. |
| 63 return true; | 35 return true; |
| 64 } | 36 } |
| 65 | 37 |
| 66 void Encryptor::Counter::Write(void* buf) { | 38 void Encryptor::Counter::Write(void* buf) { |
| 67 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); | 39 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); |
| 68 memcpy(buf_ptr, &counter_, sizeof(counter_)); | 40 memcpy(buf_ptr, &counter_, sizeof(counter_)); |
| 69 } | 41 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 DCHECK_EQ(CTR, mode_); | 88 DCHECK_EQ(CTR, mode_); |
| 117 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | 89 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); |
| 118 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | 90 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); |
| 119 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | 91 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); |
| 120 | 92 |
| 121 for (size_t i = 0; i < plaintext_len; ++i) | 93 for (size_t i = 0; i < plaintext_len; ++i) |
| 122 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | 94 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; |
| 123 } | 95 } |
| 124 | 96 |
| 125 } // namespace crypto | 97 } // namespace crypto |
| OLD | NEW |