Chromium Code Reviews| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "crypto/symmetric_key.h" | 9 #include "crypto/symmetric_key.h" |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 : key_(NULL), | 32 : key_(NULL), |
| 33 mode_(CBC), | 33 mode_(CBC), |
| 34 block_size_(0) { | 34 block_size_(0) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 Encryptor::~Encryptor() { | 37 Encryptor::~Encryptor() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 40 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
| 41 DCHECK(key); | 41 DCHECK(key); |
| 42 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; | 42 DCHECK(CBC == mode || ECB == mode) << "Unsupported mode of operation"; |
| 43 | 43 |
| 44 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining | 44 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining |
| 45 // mode) are properties of a key, so we have to create a copy of the key for | 45 // mode) are properties of a key, so we have to create a copy of the key for |
| 46 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. | 46 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. |
| 47 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); | 47 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); |
| 48 if (!ok) | 48 if (!ok) |
| 49 return false; | 49 return false; |
| 50 | 50 |
| 51 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider, | 51 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider, |
| 52 // but we set it anyway to be safe. | 52 // but we set it anyway to be safe. |
| 53 DWORD cipher_mode = CRYPT_MODE_CBC; | 53 DWORD cipher_mode = CRYPT_MODE_CBC; |
|
Ryan Sleevi
2011/05/23 05:55:04
CRYPT_MODE_ECB for ECB. I'm not sure of KP_PADDING
| |
| 54 ok = CryptSetKeyParam(capi_key_.get(), KP_MODE, | 54 ok = CryptSetKeyParam(capi_key_.get(), KP_MODE, |
| 55 reinterpret_cast<BYTE*>(&cipher_mode), 0); | 55 reinterpret_cast<BYTE*>(&cipher_mode), 0); |
| 56 if (!ok) | 56 if (!ok) |
| 57 return false; | 57 return false; |
| 58 | 58 |
| 59 block_size_ = GetCipherBlockSize(capi_key_.get()); | 59 block_size_ = GetCipherBlockSize(capi_key_.get()); |
| 60 if (block_size_ == 0) | 60 if (block_size_ == 0) |
| 61 return false; | 61 return false; |
| 62 | 62 |
| 63 if (iv.size() != block_size_) | 63 if (iv.size() != block_size_) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 if (!ok) | 106 if (!ok) |
| 107 return false; | 107 return false; |
| 108 | 108 |
| 109 DCHECK_GT(tmp.size(), data_len); | 109 DCHECK_GT(tmp.size(), data_len); |
| 110 | 110 |
| 111 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); | 111 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
| 112 return true; | 112 return true; |
| 113 } | 113 } |
| 114 | 114 |
| 115 } // namespace crypto | 115 } // namespace crypto |
| OLD | NEW |