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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "crypto/symmetric_key.h" | 10 #include "crypto/symmetric_key.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 reinterpret_cast<BYTE*>(&padding_method), 0); | 76 reinterpret_cast<BYTE*>(&padding_method), 0); |
77 if (!ok) | 77 if (!ok) |
78 return false; | 78 return false; |
79 | 79 |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 83 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
84 std::string* ciphertext) { | 84 std::string* ciphertext) { |
85 DWORD data_len = plaintext.size(); | 85 DWORD data_len = plaintext.size(); |
86 DCHECK((data_len > 0u) || (mode_ == CBC)); | |
86 DWORD total_len = data_len + block_size_; | 87 DWORD total_len = data_len + block_size_; |
87 CHECK_GT(total_len, data_len); | 88 DCHECK_GT(total_len, 0u); |
89 DCHECK_GT(total_len + 1, data_len); | |
Ryan Sleevi
2011/11/01 23:08:59
86, 88, 89 -> CHECK variants
| |
88 | 90 |
89 // CryptoAPI encrypts/decrypts in place. | 91 // CryptoAPI encrypts/decrypts in place. |
90 char* ciphertext_data = WriteInto(ciphertext, total_len + 1); | 92 char* ciphertext_data = WriteInto(ciphertext, total_len + 1); |
91 memcpy(ciphertext_data, plaintext.data(), data_len); | 93 memcpy(ciphertext_data, plaintext.data(), data_len); |
92 | 94 |
93 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, | 95 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, |
94 reinterpret_cast<BYTE*>(ciphertext_data), &data_len, | 96 reinterpret_cast<BYTE*>(ciphertext_data), &data_len, |
95 total_len); | 97 total_len); |
96 if (!ok) { | 98 if (!ok) { |
97 ciphertext->clear(); | 99 ciphertext->clear(); |
98 return false; | 100 return false; |
99 } | 101 } |
100 | 102 |
101 ciphertext->resize(data_len); | 103 ciphertext->resize(data_len); |
102 return true; | 104 return true; |
103 } | 105 } |
104 | 106 |
105 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 107 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
106 std::string* plaintext) { | 108 std::string* plaintext) { |
107 DWORD data_len = ciphertext.size(); | 109 DWORD data_len = ciphertext.size(); |
108 if (data_len == 0 || (data_len + 1) < data_len) | 110 DCHECK_GT(data_len, 0u); |
109 return false; | 111 DCHECK_GT(data_len + 1, data_len); |
Ryan Sleevi
2011/11/01 23:08:59
both of these to CHECK_GT
| |
110 | 112 |
111 // CryptoAPI encrypts/decrypts in place. | 113 // CryptoAPI encrypts/decrypts in place. |
112 char* plaintext_data = WriteInto(plaintext, data_len + 1); | 114 char* plaintext_data = WriteInto(plaintext, data_len + 1); |
113 memcpy(plaintext_data, ciphertext.data(), data_len); | 115 memcpy(plaintext_data, ciphertext.data(), data_len); |
114 | 116 |
115 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, | 117 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, |
116 reinterpret_cast<BYTE*>(plaintext_data), &data_len); | 118 reinterpret_cast<BYTE*>(plaintext_data), &data_len); |
117 if (!ok) { | 119 if (!ok) { |
118 plaintext->clear(); | 120 plaintext->clear(); |
119 return false; | 121 return false; |
120 } | 122 } |
121 | 123 |
122 plaintext->resize(data_len); | 124 plaintext->resize(data_len); |
123 return true; | 125 return true; |
124 } | 126 } |
125 | 127 |
126 } // namespace crypto | 128 } // namespace crypto |
OLD | NEW |