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 <cryptohi.h> | 7 #include <cryptohi.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 Encryptor::Encryptor() | 35 Encryptor::Encryptor() |
36 : key_(NULL), | 36 : key_(NULL), |
37 mode_(CBC) { | 37 mode_(CBC) { |
38 EnsureNSSInit(); | 38 EnsureNSSInit(); |
39 } | 39 } |
40 | 40 |
41 Encryptor::~Encryptor() { | 41 Encryptor::~Encryptor() { |
42 } | 42 } |
43 | 43 |
44 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 44 bool Encryptor::Init(SymmetricKey* key, |
| 45 Mode mode, |
| 46 const base::StringPiece& iv) { |
45 DCHECK(key); | 47 DCHECK(key); |
46 DCHECK(CBC == mode || CTR == mode) << "Unsupported mode of operation"; | 48 DCHECK(CBC == mode || CTR == mode) << "Unsupported mode of operation"; |
47 | 49 |
48 key_ = key; | 50 key_ = key; |
49 mode_ = mode; | 51 mode_ = mode; |
50 | 52 |
51 if (mode == CBC && iv.size() != AES_BLOCK_SIZE) | 53 if (mode == CBC && iv.size() != AES_BLOCK_SIZE) |
52 return false; | 54 return false; |
53 | 55 |
54 slot_.reset(PK11_GetBestSlot(GetMechanism(mode), NULL)); | 56 slot_.reset(PK11_GetBestSlot(GetMechanism(mode), NULL)); |
(...skipping 13 matching lines...) Expand all Loading... |
68 case CTR: | 70 case CTR: |
69 param_.reset(PK11_ParamFromIV(GetMechanism(mode), NULL)); | 71 param_.reset(PK11_ParamFromIV(GetMechanism(mode), NULL)); |
70 break; | 72 break; |
71 } | 73 } |
72 | 74 |
73 if (!param_.get()) | 75 if (!param_.get()) |
74 return false; | 76 return false; |
75 return true; | 77 return true; |
76 } | 78 } |
77 | 79 |
78 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 80 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 81 std::string* ciphertext) { |
79 ScopedPK11Context context(PK11_CreateContextBySymKey(GetMechanism(mode_), | 82 ScopedPK11Context context(PK11_CreateContextBySymKey(GetMechanism(mode_), |
80 CKA_ENCRYPT, | 83 CKA_ENCRYPT, |
81 key_->key(), | 84 key_->key(), |
82 param_.get())); | 85 param_.get())); |
83 if (!context.get()) | 86 if (!context.get()) |
84 return false; | 87 return false; |
85 | 88 |
86 if (mode_ == CTR) | 89 if (mode_ == CTR) |
87 return CryptCTR(context.get(), plaintext, ciphertext); | 90 return CryptCTR(context.get(), plaintext, ciphertext); |
88 else | 91 else |
89 return Crypt(context.get(), plaintext, ciphertext); | 92 return Crypt(context.get(), plaintext, ciphertext); |
90 } | 93 } |
91 | 94 |
92 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 95 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 96 std::string* plaintext) { |
93 if (ciphertext.empty()) | 97 if (ciphertext.empty()) |
94 return false; | 98 return false; |
95 | 99 |
96 ScopedPK11Context context(PK11_CreateContextBySymKey( | 100 ScopedPK11Context context(PK11_CreateContextBySymKey( |
97 GetMechanism(mode_), (mode_ == CTR ? CKA_ENCRYPT : CKA_DECRYPT), | 101 GetMechanism(mode_), (mode_ == CTR ? CKA_ENCRYPT : CKA_DECRYPT), |
98 key_->key(), param_.get())); | 102 key_->key(), param_.get())); |
99 if (!context.get()) | 103 if (!context.get()) |
100 return false; | 104 return false; |
101 | 105 |
102 if (mode_ == CTR) | 106 if (mode_ == CTR) |
103 return CryptCTR(context.get(), ciphertext, plaintext); | 107 return CryptCTR(context.get(), ciphertext, plaintext); |
104 else | 108 else |
105 return Crypt(context.get(), ciphertext, plaintext); | 109 return Crypt(context.get(), ciphertext, plaintext); |
106 } | 110 } |
107 | 111 |
108 bool Encryptor::Crypt(PK11Context* context, const std::string& input, | 112 bool Encryptor::Crypt(PK11Context* context, |
| 113 const base::StringPiece& input, |
109 std::string* output) { | 114 std::string* output) { |
110 size_t output_len = input.size() + AES_BLOCK_SIZE; | 115 size_t output_len = input.size() + AES_BLOCK_SIZE; |
111 CHECK(output_len > input.size()) << "Output size overflow"; | 116 CHECK(output_len > input.size()) << "Output size overflow"; |
112 | 117 |
113 output->resize(output_len); | 118 output->resize(output_len); |
114 uint8* output_data = | 119 uint8* output_data = |
115 reinterpret_cast<uint8*>(const_cast<char*>(output->data())); | 120 reinterpret_cast<uint8*>(const_cast<char*>(output->data())); |
116 | 121 |
117 int input_len = input.size(); | 122 int input_len = input.size(); |
118 uint8* input_data = | 123 uint8* input_data = |
(...skipping 19 matching lines...) Expand all Loading... |
138 output_len - op_len); | 143 output_len - op_len); |
139 if (SECSuccess != rv) { | 144 if (SECSuccess != rv) { |
140 output->clear(); | 145 output->clear(); |
141 return false; | 146 return false; |
142 } | 147 } |
143 | 148 |
144 output->resize(op_len + digest_len); | 149 output->resize(op_len + digest_len); |
145 return true; | 150 return true; |
146 } | 151 } |
147 | 152 |
148 bool Encryptor::CryptCTR(PK11Context* context, const std::string& input, | 153 bool Encryptor::CryptCTR(PK11Context* context, |
| 154 const base::StringPiece& input, |
149 std::string* output) { | 155 std::string* output) { |
150 if (!counter_.get()) { | 156 if (!counter_.get()) { |
151 LOG(ERROR) << "Counter value not set in CTR mode."; | 157 LOG(ERROR) << "Counter value not set in CTR mode."; |
152 return false; | 158 return false; |
153 } | 159 } |
154 | 160 |
155 size_t output_len = ((input.size() + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * | 161 size_t output_len = ((input.size() + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * |
156 AES_BLOCK_SIZE; | 162 AES_BLOCK_SIZE; |
157 CHECK(output_len >= input.size()) << "Output size overflow"; | 163 CHECK(output_len >= input.size()) << "Output size overflow"; |
158 output->resize(output_len); | 164 output->resize(output_len); |
(...skipping 28 matching lines...) Expand all Loading... |
187 | 193 |
188 // Use |output_data| to mask |input|. | 194 // Use |output_data| to mask |input|. |
189 MaskMessage( | 195 MaskMessage( |
190 reinterpret_cast<uint8*>(const_cast<char*>(input.data())), | 196 reinterpret_cast<uint8*>(const_cast<char*>(input.data())), |
191 input.length(), output_data, output_data); | 197 input.length(), output_data, output_data); |
192 output->resize(input.length()); | 198 output->resize(input.length()); |
193 return true; | 199 return true; |
194 } | 200 } |
195 | 201 |
196 } // namespace crypto | 202 } // namespace crypto |
OLD | NEW |