OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/gcm_driver/crypto/gcm_message_cryptographer.h" | 5 #include "components/gcm_driver/crypto/gcm_message_cryptographer.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 const base::StringPiece& recipient_public_key, | 178 const base::StringPiece& recipient_public_key, |
179 const base::StringPiece& sender_public_key, | 179 const base::StringPiece& sender_public_key, |
180 const base::StringPiece& ecdh_shared_secret, | 180 const base::StringPiece& ecdh_shared_secret, |
181 const base::StringPiece& auth_secret, | 181 const base::StringPiece& auth_secret, |
182 const base::StringPiece& salt, | 182 const base::StringPiece& salt, |
183 const base::StringPiece& plaintext, | 183 const base::StringPiece& plaintext, |
184 size_t* record_size, | 184 size_t* record_size, |
185 std::string* ciphertext) const { | 185 std::string* ciphertext) const { |
186 DCHECK_EQ(recipient_public_key.size(), 65u); | 186 DCHECK_EQ(recipient_public_key.size(), 65u); |
187 DCHECK_EQ(sender_public_key.size(), 65u); | 187 DCHECK_EQ(sender_public_key.size(), 65u); |
| 188 DCHECK_EQ(ecdh_shared_secret.size(), 32u); |
| 189 DCHECK_EQ(auth_secret.size(), 16u); |
| 190 DCHECK_EQ(salt.size(), 16u); |
188 DCHECK(record_size); | 191 DCHECK(record_size); |
189 DCHECK(ciphertext); | 192 DCHECK(ciphertext); |
190 | 193 |
191 // TODO(peter): DCHECK the lengths of |ecdh_shared_secret|, |auth_secret| and | |
192 // |salt|. | |
193 | |
194 if (salt.size() != kSaltSize) | |
195 return false; | |
196 | |
197 std::string prk = encryption_scheme_->DerivePseudoRandomKey( | 194 std::string prk = encryption_scheme_->DerivePseudoRandomKey( |
198 ecdh_shared_secret, auth_secret); | 195 ecdh_shared_secret, auth_secret); |
199 | 196 |
200 std::string content_encryption_key = DeriveContentEncryptionKey( | 197 std::string content_encryption_key = DeriveContentEncryptionKey( |
201 recipient_public_key, sender_public_key, prk, salt); | 198 recipient_public_key, sender_public_key, prk, salt); |
202 std::string nonce = | 199 std::string nonce = |
203 DeriveNonce(recipient_public_key, sender_public_key, prk, salt); | 200 DeriveNonce(recipient_public_key, sender_public_key, prk, salt); |
204 | 201 |
205 std::string record = encryption_scheme_->CreateRecord(plaintext); | 202 std::string record = encryption_scheme_->CreateRecord(plaintext); |
206 std::string encrypted_record; | 203 std::string encrypted_record; |
(...skipping 15 matching lines...) Expand all Loading... |
222 const base::StringPiece& recipient_public_key, | 219 const base::StringPiece& recipient_public_key, |
223 const base::StringPiece& sender_public_key, | 220 const base::StringPiece& sender_public_key, |
224 const base::StringPiece& ecdh_shared_secret, | 221 const base::StringPiece& ecdh_shared_secret, |
225 const base::StringPiece& auth_secret, | 222 const base::StringPiece& auth_secret, |
226 const base::StringPiece& salt, | 223 const base::StringPiece& salt, |
227 const base::StringPiece& ciphertext, | 224 const base::StringPiece& ciphertext, |
228 size_t record_size, | 225 size_t record_size, |
229 std::string* plaintext) const { | 226 std::string* plaintext) const { |
230 DCHECK_EQ(recipient_public_key.size(), 65u); | 227 DCHECK_EQ(recipient_public_key.size(), 65u); |
231 DCHECK_EQ(sender_public_key.size(), 65u); | 228 DCHECK_EQ(sender_public_key.size(), 65u); |
| 229 DCHECK_EQ(ecdh_shared_secret.size(), 32u); |
| 230 DCHECK_EQ(auth_secret.size(), 16u); |
| 231 DCHECK_EQ(salt.size(), 16u); |
232 DCHECK(plaintext); | 232 DCHECK(plaintext); |
233 | 233 |
234 // TODO(peter): DCHECK the lengths of |ecdh_shared_secret|, |auth_secret| and | |
235 // |salt|. | |
236 | |
237 if (record_size <= 1) | 234 if (record_size <= 1) |
238 return false; | 235 return false; |
239 | 236 |
240 std::string prk = encryption_scheme_->DerivePseudoRandomKey( | 237 std::string prk = encryption_scheme_->DerivePseudoRandomKey( |
241 ecdh_shared_secret, auth_secret); | 238 ecdh_shared_secret, auth_secret); |
242 | 239 |
243 std::string content_encryption_key = DeriveContentEncryptionKey( | 240 std::string content_encryption_key = DeriveContentEncryptionKey( |
244 recipient_public_key, sender_public_key, prk, salt); | 241 recipient_public_key, sender_public_key, prk, salt); |
245 | 242 |
246 std::string nonce = | 243 std::string nonce = |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 353 |
357 // https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-02 | 354 // https://tools.ietf.org/html/draft-ietf-httpbis-encryption-encoding-02 |
358 // defines that the result should be XOR'ed with the record's sequence number, | 355 // defines that the result should be XOR'ed with the record's sequence number, |
359 // however, Web Push encryption is limited to a single record per | 356 // however, Web Push encryption is limited to a single record per |
360 // https://tools.ietf.org/html/draft-ietf-webpush-encryption-03. | 357 // https://tools.ietf.org/html/draft-ietf-webpush-encryption-03. |
361 | 358 |
362 return hkdf.client_write_key().as_string(); | 359 return hkdf.client_write_key().as_string(); |
363 } | 360 } |
364 | 361 |
365 } // namespace gcm | 362 } // namespace gcm |
OLD | NEW |