OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "net/quic/crypto/crypto_secret_boxer.h" | 5 #include "net/quic/crypto/crypto_secret_boxer.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "net/quic/crypto/crypto_protocol.h" | 9 #include "net/quic/crypto/crypto_protocol.h" |
10 #include "net/quic/crypto/quic_decrypter.h" | 10 #include "net/quic/crypto/quic_decrypter.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 return ret; | 67 return ret; |
68 } | 68 } |
69 | 69 |
70 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, | 70 bool CryptoSecretBoxer::Unbox(StringPiece ciphertext, |
71 string* out_storage, | 71 string* out_storage, |
72 StringPiece* out) const { | 72 StringPiece* out) const { |
73 if (ciphertext.size() < kBoxNonceSize) { | 73 if (ciphertext.size() < kBoxNonceSize) { |
74 return false; | 74 return false; |
75 } | 75 } |
76 | 76 |
77 char nonce[kBoxNonceSize]; | 77 StringPiece nonce(ciphertext.data(), kBoxNonceSize); |
78 memcpy(nonce, ciphertext.data(), kBoxNonceSize); | |
79 ciphertext.remove_prefix(kBoxNonceSize); | 78 ciphertext.remove_prefix(kBoxNonceSize); |
80 | 79 QuicPacketSequenceNumber sequence_number; |
81 size_t len = ciphertext.size(); | 80 StringPiece nonce_prefix(nonce.data(), |
82 out_storage->resize(len); | 81 nonce.size() - sizeof(sequence_number)); |
83 char* data = const_cast<char*>(out_storage->data()); | 82 memcpy(&sequence_number, nonce.data() + nonce_prefix.size(), |
| 83 sizeof(sequence_number)); |
84 | 84 |
85 scoped_ptr<QuicDecrypter> decrypter(QuicDecrypter::Create(kAESG)); | 85 scoped_ptr<QuicDecrypter> decrypter(QuicDecrypter::Create(kAESG)); |
86 if (!decrypter->SetKey(key_)) { | 86 if (!decrypter->SetKey(key_)) { |
87 DLOG(DFATAL) << "CryptoSecretBoxer's decrypter->SetKey failed."; | 87 DLOG(DFATAL) << "CryptoSecretBoxer's decrypter->SetKey failed."; |
88 return false; | 88 return false; |
89 } | 89 } |
90 if (!decrypter->Decrypt(StringPiece(nonce, kBoxNonceSize), StringPiece(), | 90 decrypter->SetNoncePrefix(nonce_prefix); |
91 ciphertext, reinterpret_cast<unsigned char*>(data), | 91 scoped_ptr<QuicData> decrypted( |
92 &len)) { | 92 decrypter->DecryptPacket(sequence_number, StringPiece(), ciphertext)); |
| 93 if (!decrypted.get()) { |
93 return false; | 94 return false; |
94 } | 95 } |
95 | 96 |
96 out->set(data, len); | 97 out_storage->resize(decrypted->length()); |
| 98 out_storage->assign(decrypted->data(), decrypted->length()); |
| 99 out->set(out_storage->data(), decrypted->length()); |
97 return true; | 100 return true; |
98 } | 101 } |
99 | 102 |
100 } // namespace net | 103 } // namespace net |
OLD | NEW |