QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_QUIC_CORE_CRYPTO_P256_KEY_EXCHANGE_H_ |
| 6 | #define QUICHE_QUIC_CORE_CRYPTO_P256_KEY_EXCHANGE_H_ |
| 7 | |
| 8 | #include <cstdint> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame^] | 9 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
| 11 | #include "base/macros.h" |
| 12 | #include "third_party/boringssl/src/include/openssl/base.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/crypto/key_exchange.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h" |
| 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | // P256KeyExchange implements a KeyExchange using elliptic-curve |
| 20 | // Diffie-Hellman on NIST P-256. |
| 21 | class QUIC_EXPORT_PRIVATE P256KeyExchange : public KeyExchange { |
| 22 | public: |
| 23 | ~P256KeyExchange() override; |
| 24 | |
| 25 | // New creates a new key exchange object from a private key. If |
| 26 | // |private_key| is invalid, nullptr is returned. |
| 27 | static std::unique_ptr<P256KeyExchange> New(QuicStringPiece private_key); |
| 28 | |
| 29 | // |NewPrivateKey| returns a private key, suitable for passing to |New|. |
| 30 | // If |NewPrivateKey| can't generate a private key, it returns an empty |
| 31 | // string. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 32 | static std::string NewPrivateKey(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | |
| 34 | // KeyExchange interface. |
| 35 | const Factory& GetFactory() const override; |
| 36 | bool CalculateSharedKey(QuicStringPiece peer_public_value, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 37 | std::string* shared_key) const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | void CalculateSharedKey(QuicStringPiece peer_public_value, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 39 | std::string* shared_key, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | std::unique_ptr<Callback> callback) const override; |
| 41 | QuicStringPiece public_value() const override; |
| 42 | |
| 43 | private: |
| 44 | enum { |
| 45 | // A P-256 field element consists of 32 bytes. |
| 46 | kP256FieldBytes = 32, |
| 47 | // A P-256 point in uncompressed form consists of 0x04 (to denote |
| 48 | // that the point is uncompressed) followed by two, 32-byte field |
| 49 | // elements. |
| 50 | kUncompressedP256PointBytes = 1 + 2 * kP256FieldBytes, |
| 51 | // The first byte in an uncompressed P-256 point. |
| 52 | kUncompressedECPointForm = 0x04, |
| 53 | }; |
| 54 | |
| 55 | // P256KeyExchange wraps |private_key|, and expects |public_key| consists of |
| 56 | // |kUncompressedP256PointBytes| bytes. |
| 57 | P256KeyExchange(bssl::UniquePtr<EC_KEY> private_key, |
| 58 | const uint8_t* public_key); |
| 59 | P256KeyExchange(const P256KeyExchange&) = delete; |
| 60 | P256KeyExchange& operator=(const P256KeyExchange&) = delete; |
| 61 | |
| 62 | bssl::UniquePtr<EC_KEY> private_key_; |
| 63 | // The public key stored as an uncompressed P-256 point. |
| 64 | uint8_t public_key_[kUncompressedP256PointBytes]; |
| 65 | }; |
| 66 | |
| 67 | } // namespace quic |
| 68 | |
| 69 | #endif // QUICHE_QUIC_CORE_CRYPTO_P256_KEY_EXCHANGE_H_ |