| 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_DECRYPTER_H_ | 5 #ifndef NET_QUIC_CRYPTO_AEAD_BASE_DECRYPTER_H_ |
| 6 #define NET_QUIC_CRYPTO_AES_128_GCM_12_DECRYPTER_H_ | 6 #define NET_QUIC_CRYPTO_AEAD_BASE_DECRYPTER_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_decrypter.h" | 9 #include "net/quic/crypto/quic_decrypter.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 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 namespace test { | 19 // AeadBaseDecrypter is the base class of AEAD QuicDecrypter subclasses. |
| 20 class Aes128Gcm12DecrypterPeer; | 20 class AeadBaseDecrypter : public QuicDecrypter { |
| 21 } // namespace test | |
| 22 | |
| 23 // An Aes128Gcm12Decrypter is a QuicDecrypter that implements the | |
| 24 // AEAD_AES_128_GCM_12 algorithm specified in RFC 5282. Create an instance by | |
| 25 // calling QuicDecrypter::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 Aes128Gcm12Decrypter : public QuicDecrypter { | |
| 30 public: | 21 public: |
| 31 enum { | 22 #if defined(USE_OPENSSL) |
| 32 // Authentication tags are truncated to 96 bits. | 23 AeadBaseDecrypter(const EVP_AEAD* aead_alg, |
| 33 kAuthTagSize = 12, | 24 size_t key_size, |
| 34 }; | 25 size_t auth_tag_size, |
| 35 | 26 size_t nonce_prefix_size); |
| 36 Aes128Gcm12Decrypter(); | 27 #else |
| 37 virtual ~Aes128Gcm12Decrypter(); | 28 AeadBaseDecrypter(CK_MECHANISM_TYPE aead_mechanism, |
| 38 | 29 size_t key_size, |
| 39 // Returns true if the underlying crypto library supports AES GCM. | 30 size_t auth_tag_size, |
| 40 static bool IsSupported(); | 31 size_t nonce_prefix_size); |
| 32 #endif |
| 33 virtual ~AeadBaseDecrypter(); |
| 41 | 34 |
| 42 // QuicDecrypter implementation | 35 // QuicDecrypter implementation |
| 43 virtual bool SetKey(base::StringPiece key) OVERRIDE; | 36 virtual bool SetKey(base::StringPiece key) OVERRIDE; |
| 44 virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; | 37 virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; |
| 45 virtual bool Decrypt(base::StringPiece nonce, | 38 virtual bool Decrypt(base::StringPiece nonce, |
| 46 base::StringPiece associated_data, | 39 base::StringPiece associated_data, |
| 47 base::StringPiece ciphertext, | 40 base::StringPiece ciphertext, |
| 48 unsigned char* output, | 41 unsigned char* output, |
| 49 size_t* output_length) OVERRIDE; | 42 size_t* output_length) OVERRIDE; |
| 50 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, | 43 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, |
| 51 base::StringPiece associated_data, | 44 base::StringPiece associated_data, |
| 52 base::StringPiece ciphertext) OVERRIDE; | 45 base::StringPiece ciphertext) OVERRIDE; |
| 53 virtual base::StringPiece GetKey() const OVERRIDE; | 46 virtual base::StringPiece GetKey() const OVERRIDE; |
| 54 virtual base::StringPiece GetNoncePrefix() const OVERRIDE; | 47 virtual base::StringPiece GetNoncePrefix() const OVERRIDE; |
| 55 | 48 |
| 49 protected: |
| 50 // Make these constants available to the subclasses so that the subclasses |
| 51 // can assert at compile time their key_size_ and nonce_prefix_size_ do not |
| 52 // exceed the maximum. |
| 53 static const size_t kMaxKeySize = 32; |
| 54 static const size_t kMaxNoncePrefixSize = 4; |
| 55 |
| 56 #if !defined(USE_OPENSSL) |
| 57 struct AeadParams { |
| 58 unsigned int len; |
| 59 union { |
| 60 CK_GCM_PARAMS gcm_params; |
| 61 #if !defined(USE_NSS) |
| 62 // The system NSS <pkcs11n.h> header doesn't define this type yet. |
| 63 CK_NSS_AEAD_PARAMS nss_aead_params; |
| 64 #endif |
| 65 } data; |
| 66 }; |
| 67 |
| 68 virtual void FillAeadParams(base::StringPiece nonce, |
| 69 base::StringPiece associated_data, |
| 70 size_t auth_tag_size, |
| 71 AeadParams* aead_params) const = 0; |
| 72 #endif |
| 73 |
| 56 private: | 74 private: |
| 57 // The 128-bit AES key. | 75 #if defined(USE_OPENSSL) |
| 58 unsigned char key_[16]; | 76 const EVP_AEAD* aead_alg_; |
| 77 #else |
| 78 CK_MECHANISM_TYPE aead_mechanism_; |
| 79 #endif |
| 80 const size_t key_size_; |
| 81 const size_t auth_tag_size_; |
| 82 const size_t nonce_prefix_size_; |
| 83 |
| 84 // The key. |
| 85 unsigned char key_[kMaxKeySize]; |
| 59 // The nonce prefix. | 86 // The nonce prefix. |
| 60 unsigned char nonce_prefix_[4]; | 87 unsigned char nonce_prefix_[kMaxNoncePrefixSize]; |
| 61 | 88 |
| 62 #if defined(USE_OPENSSL) | 89 #if defined(USE_OPENSSL) |
| 63 ScopedEVPAEADCtx ctx_; | 90 ScopedEVPAEADCtx ctx_; |
| 64 #endif | 91 #endif |
| 65 }; | 92 }; |
| 66 | 93 |
| 67 } // namespace net | 94 } // namespace net |
| 68 | 95 |
| 69 #endif // NET_QUIC_CRYPTO_AES_128_GCM_12_DECRYPTER_H_ | 96 #endif // NET_QUIC_CRYPTO_AEAD_BASE_DECRYPTER_H_ |
| OLD | NEW |