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