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 | |
|
wtc
2011/06/23 18:10:24
To make your code correct for big-endian systems,
| |
| 24 | |
| 25 namespace crypto { | |
|
wtc
2011/06/23 18:10:24
Add a blank line after this line.
| |
| 26 namespace { | |
| 27 const size_t kCounterLength = 16u; | |
| 28 } // namespace | |
| 29 | |
| 30 ///////////////////////////////////////////////////////////////////////////// | |
| 31 // Encyptor::Counter Implementation. | |
| 32 Encryptor::Counter::Counter(const std::string& counter) | |
| 33 : counter_bits_(kCounterLength * 8) { | |
| 34 // Check the endianness at runtime and complain if this is not little | |
| 35 // endian. | |
| 36 int endian_check = 1; | |
| 37 CHECK_EQ(*reinterpret_cast<int8*>(&endian_check), 1) | |
| 38 << "This code only runs in little endian system"; | |
| 39 CHECK_EQ(kCounterLength, counter.length()); | |
| 40 | |
| 41 memcpy(counter_.buf, counter.data(), kCounterLength); | |
| 42 } | |
| 43 | |
| 44 Encryptor::Counter::~Counter() { | |
| 45 } | |
| 46 | |
| 47 void Encryptor::Counter::Increment() { | |
| 48 uint64* counter = reinterpret_cast<uint64*>(counter_.buf); | |
| 49 uint64 low_num = bswap_64(*(counter + 1)); | |
| 50 uint64 new_low_num = low_num + 1; | |
| 51 *(counter + 1) = bswap_64(new_low_num); | |
| 52 | |
| 53 // Overflow occured. | |
| 54 if (new_low_num < low_num) | |
| 55 *counter = bswap_64(bswap_64(*counter) + 1); | |
| 56 } | |
| 57 | |
| 58 void Encryptor::Counter::Write(void* buf) { | |
| 59 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); | |
| 60 memcpy(buf_ptr, counter_.buf, kCounterLength); | |
| 61 } | |
| 62 | |
| 63 size_t Encryptor::Counter::GetLengthInBytes() const { | |
| 64 return counter_bits_ / 8; | |
| 65 } | |
| 66 | |
| 67 ///////////////////////////////////////////////////////////////////////////// | |
| 68 // Partial Encryptor Implementation. | |
| 69 | |
| 70 bool Encryptor::UpdateCounter(const std::string& counter) { | |
| 71 if (mode_ != CTR) | |
| 72 return false; | |
| 73 if (counter.length() != kCounterLength) | |
| 74 return false; | |
| 75 | |
| 76 counter_.reset(new Counter(counter)); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 void Encryptor::GenerateCounterMask(size_t plaintext_len, | |
| 81 uint8* mask, | |
| 82 size_t* mask_len) { | |
| 83 DCHECK_EQ(CTR, mode_); | |
| 84 CHECK(mask); | |
| 85 CHECK(mask_len); | |
| 86 | |
| 87 const size_t kBlockLength = counter_->GetLengthInBytes(); | |
| 88 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | |
| 89 CHECK(blocks); | |
| 90 | |
| 91 *mask_len = blocks * kBlockLength; | |
| 92 | |
| 93 for (size_t i = 0; i < blocks; ++i) { | |
| 94 counter_->Write(mask); | |
| 95 mask += kBlockLength; | |
| 96 counter_->Increment(); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void Encryptor::MaskMessage(const void* plaintext, | |
| 101 size_t plaintext_len, | |
| 102 const void* mask, | |
| 103 void* ciphertext) const { | |
| 104 DCHECK_EQ(CTR, mode_); | |
| 105 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | |
| 106 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | |
| 107 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | |
| 108 | |
| 109 for (size_t i = 0; i < plaintext_len; ++i) | |
| 110 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | |
| 111 } | |
| 112 | |
| 113 } // namespace crypto | |
| OLD | NEW |