Chromium Code Reviews| 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 #include "build/build_config.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 #else | |
| 22 #include <byteswap.h> | |
| 23 #endif | |
| 24 | |
| 25 #if defined(ARCH_CPU_LITTLE_ENDIAN) | |
| 26 #define ntoh_64(x) bswap_64(x) | |
| 27 #define hton_64(x) bswap_64(x) | |
| 28 #else | |
| 29 #define ntoh_64(x) (x) | |
| 30 #define hton_64(x) (x) | |
| 31 #endif | |
| 32 | |
| 33 namespace crypto { | |
| 34 | |
| 35 namespace { | |
| 36 const size_t kCounterLength = 16u; | |
| 37 } // namespace | |
| 38 | |
| 39 ///////////////////////////////////////////////////////////////////////////// | |
| 40 // Encyptor::Counter Implementation. | |
| 41 Encryptor::Counter::Counter(const std::string& counter) | |
| 42 : counter_bits_(kCounterLength * 8) { | |
| 43 // Check the endianness at runtime and complain if this is not little | |
| 44 // endian. | |
| 45 int endian_check = 1; | |
| 46 CHECK_EQ(*reinterpret_cast<int8*>(&endian_check), 1) | |
| 47 << "This code only runs in little endian system"; | |
|
wtc
2011/06/24 18:06:06
Delete the runtime endianness check (lines 43-47).
Alpha Left Google
2011/06/24 18:52:27
Done.
| |
| 48 CHECK_EQ(kCounterLength, counter.length()); | |
| 49 | |
| 50 memcpy(counter_.buf, counter.data(), kCounterLength); | |
|
wtc
2011/06/24 18:06:06
This should be
memcpy(&counter_, counter.data(),
Alpha Left Google
2011/06/24 18:52:27
Done.
| |
| 51 } | |
| 52 | |
| 53 Encryptor::Counter::~Counter() { | |
| 54 } | |
| 55 | |
| 56 void Encryptor::Counter::Increment() { | |
| 57 uint64 low_num = ntoh_64(counter_.components64[1]); | |
| 58 uint64 new_low_num = low_num + 1; | |
| 59 counter_.components64[1] = hton_64(new_low_num); | |
| 60 | |
| 61 // Overflow occured then increment the most significant component. | |
|
wtc
2011/06/24 18:06:06
Add "If".
Alpha Left Google
2011/06/24 18:52:27
Done.
| |
| 62 if (new_low_num < low_num) { | |
| 63 counter_.components64[0] = | |
| 64 hton_64(ntoh_64(counter_.components64[0]) + 1); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void Encryptor::Counter::Write(void* buf) { | |
| 69 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); | |
| 70 memcpy(buf_ptr, counter_.buf, kCounterLength); | |
|
wtc
2011/06/24 18:06:06
This function can simply say:
memcpy(buf, &count
Alpha Left Google
2011/06/24 18:52:27
Done.
| |
| 71 } | |
| 72 | |
| 73 size_t Encryptor::Counter::GetLengthInBytes() const { | |
| 74 return counter_bits_ / 8; | |
|
wtc
2011/06/24 18:06:06
This should be
return sizeof(counter_);
Alpha Left Google
2011/06/24 18:52:27
Done.
| |
| 75 } | |
| 76 | |
| 77 ///////////////////////////////////////////////////////////////////////////// | |
| 78 // Partial Encryptor Implementation. | |
| 79 | |
| 80 bool Encryptor::UpdateCounter(const std::string& counter) { | |
| 81 if (mode_ != CTR) | |
| 82 return false; | |
| 83 if (counter.length() != kCounterLength) | |
| 84 return false; | |
| 85 | |
| 86 counter_.reset(new Counter(counter)); | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 void Encryptor::GenerateCounterMask(size_t plaintext_len, | |
| 91 uint8* mask, | |
| 92 size_t* mask_len) { | |
| 93 DCHECK_EQ(CTR, mode_); | |
| 94 CHECK(mask); | |
| 95 CHECK(mask_len); | |
| 96 | |
| 97 const size_t kBlockLength = counter_->GetLengthInBytes(); | |
| 98 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | |
| 99 CHECK(blocks); | |
| 100 | |
| 101 *mask_len = blocks * kBlockLength; | |
| 102 | |
| 103 for (size_t i = 0; i < blocks; ++i) { | |
| 104 counter_->Write(mask); | |
| 105 mask += kBlockLength; | |
| 106 counter_->Increment(); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 void Encryptor::MaskMessage(const void* plaintext, | |
| 111 size_t plaintext_len, | |
| 112 const void* mask, | |
| 113 void* ciphertext) const { | |
| 114 DCHECK_EQ(CTR, mode_); | |
| 115 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | |
| 116 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | |
| 117 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | |
| 118 | |
| 119 for (size_t i = 0; i < plaintext_len; ++i) | |
| 120 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | |
| 121 } | |
| 122 | |
| 123 } // namespace crypto | |
| OLD | NEW |