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 "build/build_config.h" |
9 | 9 |
10 // Include headers to provide bswap for all platforms. | 10 // Include headers to provide bswap for all platforms. |
(...skipping 16 matching lines...) Expand all Loading... |
27 #define hton_64(x) bswap_64(x) | 27 #define hton_64(x) bswap_64(x) |
28 #else | 28 #else |
29 #define ntoh_64(x) (x) | 29 #define ntoh_64(x) (x) |
30 #define hton_64(x) (x) | 30 #define hton_64(x) (x) |
31 #endif | 31 #endif |
32 | 32 |
33 namespace crypto { | 33 namespace crypto { |
34 | 34 |
35 ///////////////////////////////////////////////////////////////////////////// | 35 ///////////////////////////////////////////////////////////////////////////// |
36 // Encyptor::Counter Implementation. | 36 // Encyptor::Counter Implementation. |
37 Encryptor::Counter::Counter(const std::string& counter) { | 37 Encryptor::Counter::Counter(const base::StringPiece& counter) { |
38 CHECK(sizeof(counter_) == counter.length()); | 38 CHECK(sizeof(counter_) == counter.length()); |
39 | 39 |
40 memcpy(&counter_, counter.data(), sizeof(counter_)); | 40 memcpy(&counter_, counter.data(), sizeof(counter_)); |
41 } | 41 } |
42 | 42 |
43 Encryptor::Counter::~Counter() { | 43 Encryptor::Counter::~Counter() { |
44 } | 44 } |
45 | 45 |
46 bool Encryptor::Counter::Increment() { | 46 bool Encryptor::Counter::Increment() { |
47 uint64 low_num = ntoh_64(counter_.components64[1]); | 47 uint64 low_num = ntoh_64(counter_.components64[1]); |
(...skipping 15 matching lines...) Expand all Loading... |
63 memcpy(buf_ptr, &counter_, sizeof(counter_)); | 63 memcpy(buf_ptr, &counter_, sizeof(counter_)); |
64 } | 64 } |
65 | 65 |
66 size_t Encryptor::Counter::GetLengthInBytes() const { | 66 size_t Encryptor::Counter::GetLengthInBytes() const { |
67 return sizeof(counter_); | 67 return sizeof(counter_); |
68 } | 68 } |
69 | 69 |
70 ///////////////////////////////////////////////////////////////////////////// | 70 ///////////////////////////////////////////////////////////////////////////// |
71 // Partial Encryptor Implementation. | 71 // Partial Encryptor Implementation. |
72 | 72 |
73 bool Encryptor::SetCounter(const std::string& counter) { | 73 bool Encryptor::SetCounter(const base::StringPiece& counter) { |
74 if (mode_ != CTR) | 74 if (mode_ != CTR) |
75 return false; | 75 return false; |
76 if (counter.length() != 16u) | 76 if (counter.length() != 16u) |
77 return false; | 77 return false; |
78 | 78 |
79 counter_.reset(new Counter(counter)); | 79 counter_.reset(new Counter(counter)); |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 bool Encryptor::GenerateCounterMask(size_t plaintext_len, | 83 bool Encryptor::GenerateCounterMask(size_t plaintext_len, |
(...skipping 27 matching lines...) Expand all Loading... |
111 DCHECK_EQ(CTR, mode_); | 111 DCHECK_EQ(CTR, mode_); |
112 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); | 112 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); |
113 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); | 113 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); |
114 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); | 114 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); |
115 | 115 |
116 for (size_t i = 0; i < plaintext_len; ++i) | 116 for (size_t i = 0; i < plaintext_len; ++i) |
117 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | 117 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; |
118 } | 118 } |
119 | 119 |
120 } // namespace crypto | 120 } // namespace crypto |
OLD | NEW |