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_CRYPTO_AES_128_GCM_12_ENCRYPTER_H_ | 5 #ifndef NET_QUIC_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
6 #define NET_QUIC_CRYPTO_AES_128_GCM_12_ENCRYPTER_H_ | 6 #define NET_QUIC_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
7 | |
8 #include <string> | |
9 | 7 |
10 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
11 #include "net/quic/crypto/quic_encrypter.h" | 9 #include "net/quic/crypto/quic_encrypter.h" |
12 | 10 |
13 #if defined(USE_OPENSSL) | 11 #if defined(USE_OPENSSL) |
14 #include "net/quic/crypto/scoped_evp_aead_ctx.h" | 12 #include "net/quic/crypto/scoped_evp_aead_ctx.h" |
| 13 #else |
| 14 #include <pkcs11t.h> |
| 15 #include <seccomon.h> |
| 16 typedef struct PK11SymKeyStr PK11SymKey; |
| 17 typedef SECStatus (*PK11_EncryptFunction)( |
| 18 PK11SymKey* symKey, CK_MECHANISM_TYPE mechanism, SECItem* param, |
| 19 unsigned char* out, unsigned int* outLen, unsigned int maxLen, |
| 20 const unsigned char* data, unsigned int dataLen); |
15 #endif | 21 #endif |
16 | 22 |
17 namespace net { | 23 namespace net { |
18 | 24 |
19 namespace test { | 25 // AeadBaseEncrypter is the base class of AEAD QuicEncrypter subclasses. |
20 class Aes128Gcm12EncrypterPeer; | 26 class NET_EXPORT_PRIVATE AeadBaseEncrypter : public QuicEncrypter { |
21 } // namespace test | |
22 | |
23 // An Aes128Gcm12Encrypter is a QuicEncrypter that implements the | |
24 // AEAD_AES_128_GCM_12 algorithm specified in RFC 5282. Create an instance by | |
25 // calling QuicEncrypter::Create(kAESG). | |
26 // | |
27 // It uses an authentication tag of 12 bytes (96 bits). The fixed prefix | |
28 // of the nonce is four bytes. | |
29 class NET_EXPORT_PRIVATE Aes128Gcm12Encrypter : public QuicEncrypter { | |
30 public: | 27 public: |
31 enum { | 28 #if defined(USE_OPENSSL) |
32 // Authentication tags are truncated to 96 bits. | 29 AeadBaseEncrypter(const EVP_AEAD* aead_alg, |
33 kAuthTagSize = 12, | 30 size_t key_size, |
34 }; | 31 size_t auth_tag_size, |
35 | 32 size_t nonce_prefix_size); |
36 Aes128Gcm12Encrypter(); | 33 #else |
37 virtual ~Aes128Gcm12Encrypter(); | 34 AeadBaseEncrypter(CK_MECHANISM_TYPE aead_mechanism, |
| 35 PK11_EncryptFunction pk11_encrypt, |
| 36 size_t key_size, |
| 37 size_t auth_tag_size, |
| 38 size_t nonce_prefix_size); |
| 39 #endif |
| 40 virtual ~AeadBaseEncrypter(); |
38 | 41 |
39 // QuicEncrypter implementation | 42 // QuicEncrypter implementation |
40 virtual bool SetKey(base::StringPiece key) OVERRIDE; | 43 virtual bool SetKey(base::StringPiece key) OVERRIDE; |
41 virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; | 44 virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; |
42 virtual bool Encrypt(base::StringPiece nonce, | 45 virtual bool Encrypt(base::StringPiece nonce, |
43 base::StringPiece associated_data, | 46 base::StringPiece associated_data, |
44 base::StringPiece plaintext, | 47 base::StringPiece plaintext, |
45 unsigned char* output) OVERRIDE; | 48 unsigned char* output) OVERRIDE; |
46 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, | 49 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, |
47 base::StringPiece associated_data, | 50 base::StringPiece associated_data, |
48 base::StringPiece plaintext) OVERRIDE; | 51 base::StringPiece plaintext) OVERRIDE; |
49 virtual size_t GetKeySize() const OVERRIDE; | 52 virtual size_t GetKeySize() const OVERRIDE; |
50 virtual size_t GetNoncePrefixSize() const OVERRIDE; | 53 virtual size_t GetNoncePrefixSize() const OVERRIDE; |
51 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE; | 54 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE; |
52 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE; | 55 virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE; |
53 virtual base::StringPiece GetKey() const OVERRIDE; | 56 virtual base::StringPiece GetKey() const OVERRIDE; |
54 virtual base::StringPiece GetNoncePrefix() const OVERRIDE; | 57 virtual base::StringPiece GetNoncePrefix() const OVERRIDE; |
55 | 58 |
| 59 protected: |
| 60 // Make these constants available to the subclasses so that the subclasses |
| 61 // can assert at compile time their key_size_ and nonce_prefix_size_ do not |
| 62 // exceed the maximum. |
| 63 static const size_t kMaxKeySize = 32; |
| 64 static const size_t kMaxNoncePrefixSize = 4; |
| 65 |
| 66 #if !defined(USE_OPENSSL) |
| 67 struct AeadParams { |
| 68 unsigned int len; |
| 69 union { |
| 70 CK_GCM_PARAMS gcm_params; |
| 71 #if !defined(USE_NSS) |
| 72 // USE_NSS means we are using system NSS rather than our copy of NSS. |
| 73 // The system NSS <pkcs11n.h> header doesn't define this type yet. |
| 74 CK_NSS_AEAD_PARAMS nss_aead_params; |
| 75 #endif |
| 76 } data; |
| 77 }; |
| 78 |
| 79 virtual void FillAeadParams(base::StringPiece nonce, |
| 80 base::StringPiece associated_data, |
| 81 size_t auth_tag_size, |
| 82 AeadParams* aead_params) const = 0; |
| 83 #endif |
| 84 |
56 private: | 85 private: |
57 // The 128-bit AES key. | 86 #if defined(USE_OPENSSL) |
58 unsigned char key_[16]; | 87 const EVP_AEAD* const aead_alg_; |
| 88 #else |
| 89 const CK_MECHANISM_TYPE aead_mechanism_; |
| 90 const PK11_EncryptFunction pk11_encrypt_; |
| 91 #endif |
| 92 const size_t key_size_; |
| 93 const size_t auth_tag_size_; |
| 94 const size_t nonce_prefix_size_; |
| 95 |
| 96 // The key. |
| 97 unsigned char key_[kMaxKeySize]; |
59 // The nonce prefix. | 98 // The nonce prefix. |
60 unsigned char nonce_prefix_[4]; | 99 unsigned char nonce_prefix_[kMaxNoncePrefixSize]; |
61 | 100 |
62 #if defined(USE_OPENSSL) | 101 #if defined(USE_OPENSSL) |
63 ScopedEVPAEADCtx ctx_; | 102 ScopedEVPAEADCtx ctx_; |
64 #endif | 103 #endif |
65 }; | 104 }; |
66 | 105 |
67 } // namespace net | 106 } // namespace net |
68 | 107 |
69 #endif // NET_QUIC_CRYPTO_AES_128_GCM_12_ENCRYPTER_H_ | 108 #endif // NET_QUIC_CRYPTO_AEAD_BASE_ENCRYPTER_H_ |
OLD | NEW |