blob: c695523ef32ad1cd6615ff529e36a10260dab8c5 [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_KEY_EXCHANGE_H_
6#define QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_
7
8#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07009#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
11#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
12#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
14
15namespace quic {
16
17class QuicRandom;
18
QUICHE teamfe1aca62019-03-14 13:39:01 -070019// Interface for a Diffie-Hellman key exchange with an asynchronous interface.
20// This allows for implementations which hold the private key locally, as well
21// as ones which make an RPC to an external key-exchange service.
22class QUIC_EXPORT_PRIVATE AsynchronousKeyExchange {
QUICHE teama6ef0a62019-03-07 20:34:33 -050023 public:
QUICHE teamfe1aca62019-03-14 13:39:01 -070024 virtual ~AsynchronousKeyExchange() = default;
QUICHE teama6ef0a62019-03-07 20:34:33 -050025
26 // Callback base class for receiving the results of an async call to
27 // CalculateSharedKeys.
28 class Callback {
29 public:
30 Callback() = default;
31 virtual ~Callback() = default;
32
QUICHE teamfe1aca62019-03-14 13:39:01 -070033 // Invoked upon completion of CalculateSharedKeysAsync.
QUICHE teama6ef0a62019-03-07 20:34:33 -050034 //
35 // |ok| indicates whether the operation completed successfully. If false,
QUICHE teamfe1aca62019-03-14 13:39:01 -070036 // then the value pointed to by |shared_key| passed in to
37 // CalculateSharedKeyAsync is undefined.
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 virtual void Run(bool ok) = 0;
39
40 private:
41 Callback(const Callback&) = delete;
42 Callback& operator=(const Callback&) = delete;
43 };
44
QUICHE teamfe1aca62019-03-14 13:39:01 -070045 // CalculateSharedKey computes the shared key between a private key which is
46 // conceptually owned by this object (though it may not be physically located
47 // in this process) and a public value from the peer. Callers should expect
48 // that |callback| might be invoked synchronously. Results will be written
49 // into |*shared_key|.
50 virtual void CalculateSharedKeyAsync(
51 QuicStringPiece peer_public_value,
52 std::string* shared_key,
53 std::unique_ptr<Callback> callback) const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050054
QUICHE teamfe1aca62019-03-14 13:39:01 -070055 // Tag indicating the key-exchange algorithm this object will use.
56 virtual QuicTag type() const = 0;
57};
QUICHE teama6ef0a62019-03-07 20:34:33 -050058
QUICHE teamfe1aca62019-03-14 13:39:01 -070059// Interface for a Diffie-Hellman key exchange with both synchronous and
60// asynchronous interfaces. Only implementations which hold the private key
61// locally should implement this interface.
62class QUIC_EXPORT_PRIVATE SynchronousKeyExchange
63 : public AsynchronousKeyExchange {
64 public:
65 virtual ~SynchronousKeyExchange() = default;
66
67 // AyncKeyExchange API. Note that this method is marked 'final.' Subclasses
68 // should implement CalculateSharedKeySync only.
69 void CalculateSharedKeyAsync(QuicStringPiece peer_public_value,
70 std::string* shared_key,
71 std::unique_ptr<Callback> callback) const final {
72 const bool ok = CalculateSharedKeySync(peer_public_value, shared_key);
73 callback->Run(ok);
74 }
75
76 // CalculateSharedKey computes the shared key between a local private key and
77 // a public value from the peer. Results will be written into |*shared_key|.
78 virtual bool CalculateSharedKeySync(QuicStringPiece peer_public_value,
79 std::string* shared_key) const = 0;
QUICHE teama6ef0a62019-03-07 20:34:33 -050080
81 // public_value returns the local public key which can be sent to a peer in
82 // order to complete a key exchange. The returned QuicStringPiece is a
QUICHE teamfe1aca62019-03-14 13:39:01 -070083 // reference to a member of this object and is only valid for as long as it
84 // exists.
QUICHE teama6ef0a62019-03-07 20:34:33 -050085 virtual QuicStringPiece public_value() const = 0;
86};
87
QUICHE teamfe1aca62019-03-14 13:39:01 -070088// Create a SynchronousKeyExchange object which will use a keypair generated
89// from |private_key|, and a key-exchange algorithm specified by |type|, which
90// must be one of {kC255, kC256}. Returns nullptr if |private_key| or |type| is
91// invalid.
92std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange(
93 QuicTag type,
94 QuicStringPiece private_key);
95
96// Create a SynchronousKeyExchange object which will use a keypair generated
97// from |rand|, and a key-exchange algorithm specified by |type|, which must be
98// one of {kC255, kC256}. Returns nullptr if |type| is invalid.
99std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange(
100 QuicTag type,
101 QuicRandom* rand);
102
QUICHE teama6ef0a62019-03-07 20:34:33 -0500103} // namespace quic
104
105#endif // QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_