| OLD | NEW |
| 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 "sync/util/nigori.h" | 5 #include "sync/util/nigori.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return false; | 143 return false; |
| 144 | 144 |
| 145 std::vector<unsigned char> hash(kHashSize); | 145 std::vector<unsigned char> hash(kHashSize); |
| 146 if (!hmac.Sign(ciphertext, &hash[0], hash.size())) | 146 if (!hmac.Sign(ciphertext, &hash[0], hash.size())) |
| 147 return false; | 147 return false; |
| 148 | 148 |
| 149 std::string output; | 149 std::string output; |
| 150 output.assign(ciphertext); | 150 output.assign(ciphertext); |
| 151 output.append(hash.begin(), hash.end()); | 151 output.append(hash.begin(), hash.end()); |
| 152 | 152 |
| 153 Base64Encode(output, permuted); | 153 return Base64Encode(output, permuted); |
| 154 return true; | |
| 155 } | 154 } |
| 156 | 155 |
| 157 // Enc[Kenc,Kmac](value) | 156 // Enc[Kenc,Kmac](value) |
| 158 bool Nigori::Encrypt(const std::string& value, std::string* encrypted) const { | 157 bool Nigori::Encrypt(const std::string& value, std::string* encrypted) const { |
| 159 if (0U >= value.size()) | 158 if (0U >= value.size()) |
| 160 return false; | 159 return false; |
| 161 | 160 |
| 162 std::string iv; | 161 std::string iv; |
| 163 crypto::RandBytes(WriteInto(&iv, kIvSize + 1), kIvSize); | 162 crypto::RandBytes(WriteInto(&iv, kIvSize + 1), kIvSize); |
| 164 | 163 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 180 | 179 |
| 181 std::vector<unsigned char> hash(kHashSize); | 180 std::vector<unsigned char> hash(kHashSize); |
| 182 if (!hmac.Sign(ciphertext, &hash[0], hash.size())) | 181 if (!hmac.Sign(ciphertext, &hash[0], hash.size())) |
| 183 return false; | 182 return false; |
| 184 | 183 |
| 185 std::string output; | 184 std::string output; |
| 186 output.assign(iv); | 185 output.assign(iv); |
| 187 output.append(ciphertext); | 186 output.append(ciphertext); |
| 188 output.append(hash.begin(), hash.end()); | 187 output.append(hash.begin(), hash.end()); |
| 189 | 188 |
| 190 Base64Encode(output, encrypted); | 189 return Base64Encode(output, encrypted); |
| 191 return true; | |
| 192 } | 190 } |
| 193 | 191 |
| 194 bool Nigori::Decrypt(const std::string& encrypted, std::string* value) const { | 192 bool Nigori::Decrypt(const std::string& encrypted, std::string* value) const { |
| 195 std::string input; | 193 std::string input; |
| 196 if (!Base64Decode(encrypted, &input)) | 194 if (!Base64Decode(encrypted, &input)) |
| 197 return false; | 195 return false; |
| 198 | 196 |
| 199 if (input.size() < kIvSize * 2 + kHashSize) | 197 if (input.size() < kIvSize * 2 + kHashSize) |
| 200 return false; | 198 return false; |
| 201 | 199 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 DCHECK(user_key); | 239 DCHECK(user_key); |
| 242 DCHECK(encryption_key); | 240 DCHECK(encryption_key); |
| 243 DCHECK(mac_key); | 241 DCHECK(mac_key); |
| 244 | 242 |
| 245 return user_key_->GetRawKey(user_key) && | 243 return user_key_->GetRawKey(user_key) && |
| 246 encryption_key_->GetRawKey(encryption_key) && | 244 encryption_key_->GetRawKey(encryption_key) && |
| 247 mac_key_->GetRawKey(mac_key); | 245 mac_key_->GetRawKey(mac_key); |
| 248 } | 246 } |
| 249 | 247 |
| 250 } // namespace syncer | 248 } // namespace syncer |
| OLD | NEW |