Add supported_locales attribute to external_extensions.json
[chromium-blink-merge.git] / crypto / encryptor.cc
blob53e88f933f4ef206a044dc3956ed5b3d736b3dc7
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.
5 #include "crypto/encryptor.h"
7 #include "base/logging.h"
8 #include "build/build_config.h"
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
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
33 namespace crypto {
35 /////////////////////////////////////////////////////////////////////////////
36 // Encyptor::Counter Implementation.
37 Encryptor::Counter::Counter(const base::StringPiece& counter) {
38 CHECK(sizeof(counter_) == counter.length());
40 memcpy(&counter_, counter.data(), sizeof(counter_));
43 Encryptor::Counter::~Counter() {
46 bool Encryptor::Counter::Increment() {
47 uint64 low_num = ntoh_64(counter_.components64[1]);
48 uint64 new_low_num = low_num + 1;
49 counter_.components64[1] = hton_64(new_low_num);
51 // If overflow occured then increment the most significant component.
52 if (new_low_num < low_num) {
53 counter_.components64[0] =
54 hton_64(ntoh_64(counter_.components64[0]) + 1);
57 // TODO(hclam): Return false if counter value overflows.
58 return true;
61 void Encryptor::Counter::Write(void* buf) {
62 uint8* buf_ptr = reinterpret_cast<uint8*>(buf);
63 memcpy(buf_ptr, &counter_, sizeof(counter_));
66 size_t Encryptor::Counter::GetLengthInBytes() const {
67 return sizeof(counter_);
70 /////////////////////////////////////////////////////////////////////////////
71 // Partial Encryptor Implementation.
73 bool Encryptor::SetCounter(const base::StringPiece& counter) {
74 if (mode_ != CTR)
75 return false;
76 if (counter.length() != 16u)
77 return false;
79 counter_.reset(new Counter(counter));
80 return true;
83 bool Encryptor::GenerateCounterMask(size_t plaintext_len,
84 uint8* mask,
85 size_t* mask_len) {
86 DCHECK_EQ(CTR, mode_);
87 CHECK(mask);
88 CHECK(mask_len);
90 const size_t kBlockLength = counter_->GetLengthInBytes();
91 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
92 CHECK(blocks);
94 *mask_len = blocks * kBlockLength;
96 for (size_t i = 0; i < blocks; ++i) {
97 counter_->Write(mask);
98 mask += kBlockLength;
100 bool ret = counter_->Increment();
101 if (!ret)
102 return false;
104 return true;
107 void Encryptor::MaskMessage(const void* plaintext,
108 size_t plaintext_len,
109 const void* mask,
110 void* ciphertext) const {
111 DCHECK_EQ(CTR, mode_);
112 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext);
113 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask);
114 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext);
116 for (size_t i = 0; i < plaintext_len; ++i)
117 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i];
120 } // namespace crypto