| 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 <stddef.h> |
| 9 #include <stdint.h> |
| 8 #include <vector> | 10 #include <vector> |
| 9 | 11 |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "crypto/nss_util.h" | 13 #include "crypto/nss_util.h" |
| 12 #include "crypto/symmetric_key.h" | 14 #include "crypto/symmetric_key.h" |
| 13 | 15 |
| 14 namespace crypto { | 16 namespace crypto { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 return Crypt(context.get(), ciphertext, plaintext); | 111 return Crypt(context.get(), ciphertext, plaintext); |
| 110 } | 112 } |
| 111 | 113 |
| 112 bool Encryptor::Crypt(PK11Context* context, | 114 bool Encryptor::Crypt(PK11Context* context, |
| 113 const base::StringPiece& input, | 115 const base::StringPiece& input, |
| 114 std::string* output) { | 116 std::string* output) { |
| 115 size_t output_len = input.size() + AES_BLOCK_SIZE; | 117 size_t output_len = input.size() + AES_BLOCK_SIZE; |
| 116 CHECK_GT(output_len, input.size()); | 118 CHECK_GT(output_len, input.size()); |
| 117 | 119 |
| 118 output->resize(output_len); | 120 output->resize(output_len); |
| 119 uint8* output_data = | 121 uint8_t* output_data = |
| 120 reinterpret_cast<uint8*>(const_cast<char*>(output->data())); | 122 reinterpret_cast<uint8_t*>(const_cast<char*>(output->data())); |
| 121 | 123 |
| 122 int input_len = input.size(); | 124 int input_len = input.size(); |
| 123 uint8* input_data = | 125 uint8_t* input_data = |
| 124 reinterpret_cast<uint8*>(const_cast<char*>(input.data())); | 126 reinterpret_cast<uint8_t*>(const_cast<char*>(input.data())); |
| 125 | 127 |
| 126 int op_len; | 128 int op_len; |
| 127 SECStatus rv = PK11_CipherOp(context, | 129 SECStatus rv = PK11_CipherOp(context, |
| 128 output_data, | 130 output_data, |
| 129 &op_len, | 131 &op_len, |
| 130 output_len, | 132 output_len, |
| 131 input_data, | 133 input_data, |
| 132 input_len); | 134 input_len); |
| 133 | 135 |
| 134 if (SECSuccess != rv) { | 136 if (SECSuccess != rv) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 std::string* output) { | 157 std::string* output) { |
| 156 if (!counter_.get()) { | 158 if (!counter_.get()) { |
| 157 LOG(ERROR) << "Counter value not set in CTR mode."; | 159 LOG(ERROR) << "Counter value not set in CTR mode."; |
| 158 return false; | 160 return false; |
| 159 } | 161 } |
| 160 | 162 |
| 161 size_t output_len = ((input.size() + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * | 163 size_t output_len = ((input.size() + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * |
| 162 AES_BLOCK_SIZE; | 164 AES_BLOCK_SIZE; |
| 163 CHECK_GE(output_len, input.size()); | 165 CHECK_GE(output_len, input.size()); |
| 164 output->resize(output_len); | 166 output->resize(output_len); |
| 165 uint8* output_data = | 167 uint8_t* output_data = |
| 166 reinterpret_cast<uint8*>(const_cast<char*>(output->data())); | 168 reinterpret_cast<uint8_t*>(const_cast<char*>(output->data())); |
| 167 | 169 |
| 168 size_t mask_len; | 170 size_t mask_len; |
| 169 bool ret = GenerateCounterMask(input.size(), output_data, &mask_len); | 171 bool ret = GenerateCounterMask(input.size(), output_data, &mask_len); |
| 170 if (!ret) | 172 if (!ret) |
| 171 return false; | 173 return false; |
| 172 | 174 |
| 173 CHECK_EQ(mask_len, output_len); | 175 CHECK_EQ(mask_len, output_len); |
| 174 int op_len; | 176 int op_len; |
| 175 SECStatus rv = PK11_CipherOp(context, | 177 SECStatus rv = PK11_CipherOp(context, |
| 176 output_data, | 178 output_data, |
| 177 &op_len, | 179 &op_len, |
| 178 output_len, | 180 output_len, |
| 179 output_data, | 181 output_data, |
| 180 mask_len); | 182 mask_len); |
| 181 if (SECSuccess != rv) | 183 if (SECSuccess != rv) |
| 182 return false; | 184 return false; |
| 183 CHECK_EQ(static_cast<int>(mask_len), op_len); | 185 CHECK_EQ(static_cast<int>(mask_len), op_len); |
| 184 | 186 |
| 185 unsigned int digest_len; | 187 unsigned int digest_len; |
| 186 rv = PK11_DigestFinal(context, | 188 rv = PK11_DigestFinal(context, |
| 187 NULL, | 189 NULL, |
| 188 &digest_len, | 190 &digest_len, |
| 189 0); | 191 0); |
| 190 if (SECSuccess != rv) | 192 if (SECSuccess != rv) |
| 191 return false; | 193 return false; |
| 192 CHECK(!digest_len); | 194 CHECK(!digest_len); |
| 193 | 195 |
| 194 // Use |output_data| to mask |input|. | 196 // Use |output_data| to mask |input|. |
| 195 MaskMessage( | 197 MaskMessage(reinterpret_cast<uint8_t*>(const_cast<char*>(input.data())), |
| 196 reinterpret_cast<uint8*>(const_cast<char*>(input.data())), | 198 input.length(), output_data, output_data); |
| 197 input.length(), output_data, output_data); | |
| 198 output->resize(input.length()); | 199 output->resize(input.length()); |
| 199 return true; | 200 return true; |
| 200 } | 201 } |
| 201 | 202 |
| 202 } // namespace crypto | 203 } // namespace crypto |
| OLD | NEW |