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