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 #ifndef NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ | 5 #ifndef NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
6 #define NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ | 6 #define NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
7 | 7 |
8 #include <cstddef> | 8 #include <cstddef> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "net/quic/core/crypto/quic_encrypter.h" | 12 #include "net/quic/core/crypto/quic_encrypter.h" |
13 #include "net/quic/core/crypto/scoped_evp_aead_ctx.h" | 13 #include "net/quic/core/crypto/scoped_evp_aead_ctx.h" |
14 #include "net/quic/platform/api/quic_export.h" | 14 #include "net/quic/platform/api/quic_export.h" |
| 15 #include "net/quic/platform/api/quic_string_piece.h" |
15 | 16 |
16 namespace net { | 17 namespace net { |
17 | 18 |
18 // AeadBaseEncrypter is the base class of AEAD QuicEncrypter subclasses. | 19 // AeadBaseEncrypter is the base class of AEAD QuicEncrypter subclasses. |
19 class QUIC_EXPORT_PRIVATE AeadBaseEncrypter : public QuicEncrypter { | 20 class QUIC_EXPORT_PRIVATE AeadBaseEncrypter : public QuicEncrypter { |
20 public: | 21 public: |
21 AeadBaseEncrypter(const EVP_AEAD* aead_alg, | 22 AeadBaseEncrypter(const EVP_AEAD* aead_alg, |
22 size_t key_size, | 23 size_t key_size, |
23 size_t auth_tag_size, | 24 size_t auth_tag_size, |
24 size_t nonce_prefix_size); | 25 size_t nonce_prefix_size); |
25 ~AeadBaseEncrypter() override; | 26 ~AeadBaseEncrypter() override; |
26 | 27 |
27 // QuicEncrypter implementation | 28 // QuicEncrypter implementation |
28 bool SetKey(base::StringPiece key) override; | 29 bool SetKey(QuicStringPiece key) override; |
29 bool SetNoncePrefix(base::StringPiece nonce_prefix) override; | 30 bool SetNoncePrefix(QuicStringPiece nonce_prefix) override; |
30 bool EncryptPacket(QuicVersion version, | 31 bool EncryptPacket(QuicVersion version, |
31 QuicPacketNumber packet_number, | 32 QuicPacketNumber packet_number, |
32 base::StringPiece associated_data, | 33 QuicStringPiece associated_data, |
33 base::StringPiece plaintext, | 34 QuicStringPiece plaintext, |
34 char* output, | 35 char* output, |
35 size_t* output_length, | 36 size_t* output_length, |
36 size_t max_output_length) override; | 37 size_t max_output_length) override; |
37 size_t GetKeySize() const override; | 38 size_t GetKeySize() const override; |
38 size_t GetNoncePrefixSize() const override; | 39 size_t GetNoncePrefixSize() const override; |
39 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override; | 40 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override; |
40 size_t GetCiphertextSize(size_t plaintext_size) const override; | 41 size_t GetCiphertextSize(size_t plaintext_size) const override; |
41 base::StringPiece GetKey() const override; | 42 QuicStringPiece GetKey() const override; |
42 base::StringPiece GetNoncePrefix() const override; | 43 QuicStringPiece GetNoncePrefix() const override; |
43 | 44 |
44 // Necessary so unit tests can explicitly specify a nonce, instead of a | 45 // Necessary so unit tests can explicitly specify a nonce, instead of a |
45 // nonce prefix and packet number. | 46 // nonce prefix and packet number. |
46 bool Encrypt(base::StringPiece nonce, | 47 bool Encrypt(QuicStringPiece nonce, |
47 base::StringPiece associated_data, | 48 QuicStringPiece associated_data, |
48 base::StringPiece plaintext, | 49 QuicStringPiece plaintext, |
49 unsigned char* output); | 50 unsigned char* output); |
50 | 51 |
51 protected: | 52 protected: |
52 // Make these constants available to the subclasses so that the subclasses | 53 // Make these constants available to the subclasses so that the subclasses |
53 // can assert at compile time their key_size_ and nonce_prefix_size_ do not | 54 // can assert at compile time their key_size_ and nonce_prefix_size_ do not |
54 // exceed the maximum. | 55 // exceed the maximum. |
55 static const size_t kMaxKeySize = 32; | 56 static const size_t kMaxKeySize = 32; |
56 static const size_t kMaxNoncePrefixSize = 4; | 57 static const size_t kMaxNoncePrefixSize = 4; |
57 | 58 |
58 private: | 59 private: |
59 const EVP_AEAD* const aead_alg_; | 60 const EVP_AEAD* const aead_alg_; |
60 const size_t key_size_; | 61 const size_t key_size_; |
61 const size_t auth_tag_size_; | 62 const size_t auth_tag_size_; |
62 const size_t nonce_prefix_size_; | 63 const size_t nonce_prefix_size_; |
63 | 64 |
64 // The key. | 65 // The key. |
65 unsigned char key_[kMaxKeySize]; | 66 unsigned char key_[kMaxKeySize]; |
66 // The nonce prefix. | 67 // The nonce prefix. |
67 unsigned char nonce_prefix_[kMaxNoncePrefixSize]; | 68 unsigned char nonce_prefix_[kMaxNoncePrefixSize]; |
68 | 69 |
69 ScopedEVPAEADCtx ctx_; | 70 ScopedEVPAEADCtx ctx_; |
70 | 71 |
71 DISALLOW_COPY_AND_ASSIGN(AeadBaseEncrypter); | 72 DISALLOW_COPY_AND_ASSIGN(AeadBaseEncrypter); |
72 }; | 73 }; |
73 | 74 |
74 } // namespace net | 75 } // namespace net |
75 | 76 |
76 #endif // NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ | 77 #endif // NET_QUIC_CORE_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
OLD | NEW |