blob: 4e6d0b2ee403e979d785849f87a9c0874c2a96d8 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
vasilvv872e7a32019-03-12 16:42:44 -07009#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
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 teama6ef0a62019-03-07 20:34:33 -050015#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
16
17namespace quic {
18
19// P256KeyExchange implements a KeyExchange using elliptic-curve
20// Diffie-Hellman on NIST P-256.
21class 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.
vasilvvc48c8712019-03-11 13:38:16 -070032 static std::string NewPrivateKey();
QUICHE teama6ef0a62019-03-07 20:34:33 -050033
34 // KeyExchange interface.
35 const Factory& GetFactory() const override;
36 bool CalculateSharedKey(QuicStringPiece peer_public_value,
vasilvvc48c8712019-03-11 13:38:16 -070037 std::string* shared_key) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 void CalculateSharedKey(QuicStringPiece peer_public_value,
vasilvvc48c8712019-03-11 13:38:16 -070039 std::string* shared_key,
QUICHE teama6ef0a62019-03-07 20:34:33 -050040 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_