blob: d70d9cc63fab082ab074930eb1b3a8e8b4db5009 [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>
9
10#include "base/macros.h"
11#include "third_party/boringssl/src/include/openssl/base.h"
12#include "net/third_party/quiche/src/quic/core/crypto/key_exchange.h"
13#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
14#include "net/third_party/quiche/src/quic/platform/api/quic_string.h"
15#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.
32 static QuicString NewPrivateKey();
33
34 // KeyExchange interface.
35 const Factory& GetFactory() const override;
36 bool CalculateSharedKey(QuicStringPiece peer_public_value,
37 QuicString* shared_key) const override;
38 void CalculateSharedKey(QuicStringPiece peer_public_value,
39 QuicString* shared_key,
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_