| 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/aead_base_decrypter.h" | 5 #include "net/quic/crypto/aead_base_decrypter.h" |
| 6 | 6 |
| 7 #include <openssl/err.h> | 7 #include <openssl/err.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 | 52 |
| 53 bool AeadBaseDecrypter::SetKey(StringPiece key) { | 53 bool AeadBaseDecrypter::SetKey(StringPiece key) { |
| 54 DCHECK_EQ(key.size(), key_size_); | 54 DCHECK_EQ(key.size(), key_size_); |
| 55 if (key.size() != key_size_) { | 55 if (key.size() != key_size_) { |
| 56 return false; | 56 return false; |
| 57 } | 57 } |
| 58 memcpy(key_, key.data(), key.size()); | 58 memcpy(key_, key.data(), key.size()); |
| 59 | 59 |
| 60 EVP_AEAD_CTX_cleanup(ctx_.get()); | 60 EVP_AEAD_CTX_cleanup(ctx_.get()); |
| 61 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, | 61 if (!EVP_AEAD_CTX_init(ctx_.get(), aead_alg_, key_, key_size_, |
| 62 auth_tag_size_, NULL)) { | 62 auth_tag_size_, nullptr)) { |
| 63 DLogOpenSslErrors(); | 63 DLogOpenSslErrors(); |
| 64 return false; | 64 return false; |
| 65 } | 65 } |
| 66 | 66 |
| 67 return true; | 67 return true; |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { | 70 bool AeadBaseDecrypter::SetNoncePrefix(StringPiece nonce_prefix) { |
| 71 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); | 71 DCHECK_EQ(nonce_prefix.size(), nonce_prefix_size_); |
| 72 if (nonce_prefix.size() != nonce_prefix_size_) { | 72 if (nonce_prefix.size() != nonce_prefix_size_) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 99 } | 99 } |
| 100 | 100 |
| 101 return true; | 101 return true; |
| 102 } | 102 } |
| 103 | 103 |
| 104 QuicData* AeadBaseDecrypter::DecryptPacket( | 104 QuicData* AeadBaseDecrypter::DecryptPacket( |
| 105 QuicPacketSequenceNumber sequence_number, | 105 QuicPacketSequenceNumber sequence_number, |
| 106 StringPiece associated_data, | 106 StringPiece associated_data, |
| 107 StringPiece ciphertext) { | 107 StringPiece ciphertext) { |
| 108 if (ciphertext.length() < auth_tag_size_) { | 108 if (ciphertext.length() < auth_tag_size_) { |
| 109 return NULL; | 109 return nullptr; |
| 110 } | 110 } |
| 111 size_t plaintext_size = ciphertext.length(); | 111 size_t plaintext_size = ciphertext.length(); |
| 112 scoped_ptr<char[]> plaintext(new char[plaintext_size]); | 112 scoped_ptr<char[]> plaintext(new char[plaintext_size]); |
| 113 | 113 |
| 114 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; | 114 uint8 nonce[sizeof(nonce_prefix_) + sizeof(sequence_number)]; |
| 115 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); | 115 const size_t nonce_size = nonce_prefix_size_ + sizeof(sequence_number); |
| 116 DCHECK_LE(nonce_size, sizeof(nonce)); | 116 DCHECK_LE(nonce_size, sizeof(nonce)); |
| 117 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); | 117 memcpy(nonce, nonce_prefix_, nonce_prefix_size_); |
| 118 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); | 118 memcpy(nonce + nonce_prefix_size_, &sequence_number, sizeof(sequence_number)); |
| 119 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), | 119 if (!Decrypt(StringPiece(reinterpret_cast<char*>(nonce), nonce_size), |
| 120 associated_data, ciphertext, | 120 associated_data, ciphertext, |
| 121 reinterpret_cast<uint8*>(plaintext.get()), | 121 reinterpret_cast<uint8*>(plaintext.get()), |
| 122 &plaintext_size)) { | 122 &plaintext_size)) { |
| 123 return NULL; | 123 return nullptr; |
| 124 } | 124 } |
| 125 return new QuicData(plaintext.release(), plaintext_size, true); | 125 return new QuicData(plaintext.release(), plaintext_size, true); |
| 126 } | 126 } |
| 127 | 127 |
| 128 StringPiece AeadBaseDecrypter::GetKey() const { | 128 StringPiece AeadBaseDecrypter::GetKey() const { |
| 129 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); | 129 return StringPiece(reinterpret_cast<const char*>(key_), key_size_); |
| 130 } | 130 } |
| 131 | 131 |
| 132 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { | 132 StringPiece AeadBaseDecrypter::GetNoncePrefix() const { |
| 133 if (nonce_prefix_size_ == 0) { | 133 if (nonce_prefix_size_ == 0) { |
| 134 return StringPiece(); | 134 return StringPiece(); |
| 135 } | 135 } |
| 136 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), | 136 return StringPiece(reinterpret_cast<const char*>(nonce_prefix_), |
| 137 nonce_prefix_size_); | 137 nonce_prefix_size_); |
| 138 } | 138 } |
| 139 | 139 |
| 140 } // namespace net | 140 } // namespace net |
| OLD | NEW |