| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_QUIC_ENCRYPTER_H_ | 5 #ifndef NET_QUIC_CORE_CRYPTO_QUIC_ENCRYPTER_H_ |
| 6 #define NET_QUIC_CORE_CRYPTO_QUIC_ENCRYPTER_H_ | 6 #define NET_QUIC_CORE_CRYPTO_QUIC_ENCRYPTER_H_ |
| 7 | 7 |
| 8 #include <cstddef> | 8 #include <cstddef> |
| 9 | 9 |
| 10 #include "net/quic/core/quic_packets.h" | 10 #include "net/quic/core/quic_packets.h" |
| 11 #include "net/quic/platform/api/quic_export.h" | 11 #include "net/quic/platform/api/quic_export.h" |
| 12 #include "net/quic/platform/api/quic_string_piece.h" |
| 12 | 13 |
| 13 namespace net { | 14 namespace net { |
| 14 | 15 |
| 15 class QUIC_EXPORT_PRIVATE QuicEncrypter { | 16 class QUIC_EXPORT_PRIVATE QuicEncrypter { |
| 16 public: | 17 public: |
| 17 virtual ~QuicEncrypter() {} | 18 virtual ~QuicEncrypter() {} |
| 18 | 19 |
| 19 static QuicEncrypter* Create(QuicTag algorithm); | 20 static QuicEncrypter* Create(QuicTag algorithm); |
| 20 | 21 |
| 21 // Sets the encryption key. Returns true on success, false on failure. | 22 // Sets the encryption key. Returns true on success, false on failure. |
| 22 // | 23 // |
| 23 // NOTE: The key is the client_write_key or server_write_key derived from | 24 // NOTE: The key is the client_write_key or server_write_key derived from |
| 24 // the master secret. | 25 // the master secret. |
| 25 virtual bool SetKey(base::StringPiece key) = 0; | 26 virtual bool SetKey(QuicStringPiece key) = 0; |
| 26 | 27 |
| 27 // Sets the fixed initial bytes of the nonce. Returns true on success, | 28 // Sets the fixed initial bytes of the nonce. Returns true on success, |
| 28 // false on failure. | 29 // false on failure. |
| 29 // | 30 // |
| 30 // NOTE: The nonce prefix is the client_write_iv or server_write_iv | 31 // NOTE: The nonce prefix is the client_write_iv or server_write_iv |
| 31 // derived from the master secret. A 64-bit packet number will | 32 // derived from the master secret. A 64-bit packet number will |
| 32 // be appended to form the nonce. | 33 // be appended to form the nonce. |
| 33 // | 34 // |
| 34 // <------------ 64 bits -----------> | 35 // <------------ 64 bits -----------> |
| 35 // +---------------------+----------------------------------+ | 36 // +---------------------+----------------------------------+ |
| 36 // | Fixed prefix | packet number | | 37 // | Fixed prefix | packet number | |
| 37 // +---------------------+----------------------------------+ | 38 // +---------------------+----------------------------------+ |
| 38 // Nonce format | 39 // Nonce format |
| 39 // | 40 // |
| 40 // The security of the nonce format requires that QUIC never reuse a | 41 // The security of the nonce format requires that QUIC never reuse a |
| 41 // packet number, even when retransmitting a lost packet. | 42 // packet number, even when retransmitting a lost packet. |
| 42 virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) = 0; | 43 virtual bool SetNoncePrefix(QuicStringPiece nonce_prefix) = 0; |
| 43 | 44 |
| 44 // Writes encrypted |plaintext| and a MAC over |plaintext| and | 45 // Writes encrypted |plaintext| and a MAC over |plaintext| and |
| 45 // |associated_data| into output. Sets |output_length| to the number of | 46 // |associated_data| into output. Sets |output_length| to the number of |
| 46 // bytes written. Returns true on success or false if there was an error. | 47 // bytes written. Returns true on success or false if there was an error. |
| 47 // |packet_number| is appended to the |nonce_prefix| value provided in | 48 // |packet_number| is appended to the |nonce_prefix| value provided in |
| 48 // SetNoncePrefix() to form the nonce. |output| must not overlap with | 49 // SetNoncePrefix() to form the nonce. |output| must not overlap with |
| 49 // |associated_data|. If |output| overlaps with |plaintext| then | 50 // |associated_data|. If |output| overlaps with |plaintext| then |
| 50 // |plaintext| must be <= |output|. | 51 // |plaintext| must be <= |output|. |
| 51 virtual bool EncryptPacket(QuicVersion version, | 52 virtual bool EncryptPacket(QuicVersion version, |
| 52 QuicPacketNumber packet_number, | 53 QuicPacketNumber packet_number, |
| 53 base::StringPiece associated_data, | 54 QuicStringPiece associated_data, |
| 54 base::StringPiece plaintext, | 55 QuicStringPiece plaintext, |
| 55 char* output, | 56 char* output, |
| 56 size_t* output_length, | 57 size_t* output_length, |
| 57 size_t max_output_length) = 0; | 58 size_t max_output_length) = 0; |
| 58 | 59 |
| 59 // GetKeySize() and GetNoncePrefixSize() tell the HKDF class how many bytes | 60 // GetKeySize() and GetNoncePrefixSize() tell the HKDF class how many bytes |
| 60 // of key material needs to be derived from the master secret. | 61 // of key material needs to be derived from the master secret. |
| 61 // NOTE: the sizes returned by GetKeySize() and GetNoncePrefixSize() are | 62 // NOTE: the sizes returned by GetKeySize() and GetNoncePrefixSize() are |
| 62 // also correct for the QuicDecrypter of the same algorithm. So only | 63 // also correct for the QuicDecrypter of the same algorithm. So only |
| 63 // QuicEncrypter has these two methods. | 64 // QuicEncrypter has these two methods. |
| 64 | 65 |
| 65 // Returns the size in bytes of a key for the algorithm. | 66 // Returns the size in bytes of a key for the algorithm. |
| 66 virtual size_t GetKeySize() const = 0; | 67 virtual size_t GetKeySize() const = 0; |
| 67 // Returns the size in bytes of the fixed initial part of the nonce. | 68 // Returns the size in bytes of the fixed initial part of the nonce. |
| 68 virtual size_t GetNoncePrefixSize() const = 0; | 69 virtual size_t GetNoncePrefixSize() const = 0; |
| 69 | 70 |
| 70 // Returns the maximum length of plaintext that can be encrypted | 71 // Returns the maximum length of plaintext that can be encrypted |
| 71 // to ciphertext no larger than |ciphertext_size|. | 72 // to ciphertext no larger than |ciphertext_size|. |
| 72 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const = 0; | 73 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const = 0; |
| 73 | 74 |
| 74 // Returns the length of the ciphertext that would be generated by encrypting | 75 // Returns the length of the ciphertext that would be generated by encrypting |
| 75 // to plaintext of size |plaintext_size|. | 76 // to plaintext of size |plaintext_size|. |
| 76 virtual size_t GetCiphertextSize(size_t plaintext_size) const = 0; | 77 virtual size_t GetCiphertextSize(size_t plaintext_size) const = 0; |
| 77 | 78 |
| 78 // For use by unit tests only. | 79 // For use by unit tests only. |
| 79 virtual base::StringPiece GetKey() const = 0; | 80 virtual QuicStringPiece GetKey() const = 0; |
| 80 virtual base::StringPiece GetNoncePrefix() const = 0; | 81 virtual QuicStringPiece GetNoncePrefix() const = 0; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 } // namespace net | 84 } // namespace net |
| 84 | 85 |
| 85 #endif // NET_QUIC_CORE_CRYPTO_QUIC_ENCRYPTER_H_ | 86 #endif // NET_QUIC_CORE_CRYPTO_QUIC_ENCRYPTER_H_ |
| OLD | NEW |