Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: crypto/encryptor.cc

Issue 1539353003: Switch to standard integer types in crypto/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_nss.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h>
8 #include <stdint.h>
9
7 #include "base/logging.h" 10 #include "base/logging.h"
8 #include "base/sys_byteorder.h" 11 #include "base/sys_byteorder.h"
9 12
10 namespace crypto { 13 namespace crypto {
11 14
12 ///////////////////////////////////////////////////////////////////////////// 15 /////////////////////////////////////////////////////////////////////////////
13 // Encyptor::Counter Implementation. 16 // Encyptor::Counter Implementation.
14 Encryptor::Counter::Counter(const base::StringPiece& counter) { 17 Encryptor::Counter::Counter(const base::StringPiece& counter) {
15 CHECK(sizeof(counter_) == counter.length()); 18 CHECK(sizeof(counter_) == counter.length());
16 19
17 memcpy(&counter_, counter.data(), sizeof(counter_)); 20 memcpy(&counter_, counter.data(), sizeof(counter_));
18 } 21 }
19 22
20 Encryptor::Counter::~Counter() { 23 Encryptor::Counter::~Counter() {
21 } 24 }
22 25
23 bool Encryptor::Counter::Increment() { 26 bool Encryptor::Counter::Increment() {
24 uint64 low_num = base::NetToHost64(counter_.components64[1]); 27 uint64_t low_num = base::NetToHost64(counter_.components64[1]);
25 uint64 new_low_num = low_num + 1; 28 uint64_t new_low_num = low_num + 1;
26 counter_.components64[1] = base::HostToNet64(new_low_num); 29 counter_.components64[1] = base::HostToNet64(new_low_num);
27 30
28 // If overflow occured then increment the most significant component. 31 // If overflow occured then increment the most significant component.
29 if (new_low_num < low_num) { 32 if (new_low_num < low_num) {
30 counter_.components64[0] = 33 counter_.components64[0] =
31 base::HostToNet64(base::NetToHost64(counter_.components64[0]) + 1); 34 base::HostToNet64(base::NetToHost64(counter_.components64[0]) + 1);
32 } 35 }
33 36
34 // TODO(hclam): Return false if counter value overflows. 37 // TODO(hclam): Return false if counter value overflows.
35 return true; 38 return true;
36 } 39 }
37 40
38 void Encryptor::Counter::Write(void* buf) { 41 void Encryptor::Counter::Write(void* buf) {
39 uint8* buf_ptr = reinterpret_cast<uint8*>(buf); 42 uint8_t* buf_ptr = reinterpret_cast<uint8_t*>(buf);
40 memcpy(buf_ptr, &counter_, sizeof(counter_)); 43 memcpy(buf_ptr, &counter_, sizeof(counter_));
41 } 44 }
42 45
43 size_t Encryptor::Counter::GetLengthInBytes() const { 46 size_t Encryptor::Counter::GetLengthInBytes() const {
44 return sizeof(counter_); 47 return sizeof(counter_);
45 } 48 }
46 49
47 ///////////////////////////////////////////////////////////////////////////// 50 /////////////////////////////////////////////////////////////////////////////
48 // Partial Encryptor Implementation. 51 // Partial Encryptor Implementation.
49 52
50 bool Encryptor::SetCounter(const base::StringPiece& counter) { 53 bool Encryptor::SetCounter(const base::StringPiece& counter) {
51 if (mode_ != CTR) 54 if (mode_ != CTR)
52 return false; 55 return false;
53 if (counter.length() != 16u) 56 if (counter.length() != 16u)
54 return false; 57 return false;
55 58
56 counter_.reset(new Counter(counter)); 59 counter_.reset(new Counter(counter));
57 return true; 60 return true;
58 } 61 }
59 62
60 bool Encryptor::GenerateCounterMask(size_t plaintext_len, 63 bool Encryptor::GenerateCounterMask(size_t plaintext_len,
61 uint8* mask, 64 uint8_t* mask,
62 size_t* mask_len) { 65 size_t* mask_len) {
63 DCHECK_EQ(CTR, mode_); 66 DCHECK_EQ(CTR, mode_);
64 CHECK(mask); 67 CHECK(mask);
65 CHECK(mask_len); 68 CHECK(mask_len);
66 69
67 const size_t kBlockLength = counter_->GetLengthInBytes(); 70 const size_t kBlockLength = counter_->GetLengthInBytes();
68 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; 71 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
69 CHECK(blocks); 72 CHECK(blocks);
70 73
71 *mask_len = blocks * kBlockLength; 74 *mask_len = blocks * kBlockLength;
72 75
73 for (size_t i = 0; i < blocks; ++i) { 76 for (size_t i = 0; i < blocks; ++i) {
74 counter_->Write(mask); 77 counter_->Write(mask);
75 mask += kBlockLength; 78 mask += kBlockLength;
76 79
77 bool ret = counter_->Increment(); 80 bool ret = counter_->Increment();
78 if (!ret) 81 if (!ret)
79 return false; 82 return false;
80 } 83 }
81 return true; 84 return true;
82 } 85 }
83 86
84 void Encryptor::MaskMessage(const void* plaintext, 87 void Encryptor::MaskMessage(const void* plaintext,
85 size_t plaintext_len, 88 size_t plaintext_len,
86 const void* mask, 89 const void* mask,
87 void* ciphertext) const { 90 void* ciphertext) const {
88 DCHECK_EQ(CTR, mode_); 91 DCHECK_EQ(CTR, mode_);
89 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); 92 const uint8_t* plaintext_ptr = reinterpret_cast<const uint8_t*>(plaintext);
90 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); 93 const uint8_t* mask_ptr = reinterpret_cast<const uint8_t*>(mask);
91 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); 94 uint8_t* ciphertext_ptr = reinterpret_cast<uint8_t*>(ciphertext);
92 95
93 for (size_t i = 0; i < plaintext_len; ++i) 96 for (size_t i = 0; i < plaintext_len; ++i)
94 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; 97 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i];
95 } 98 }
96 99
97 } // namespace crypto 100 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_nss.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698