xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/curve25519_key_exchange.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_CURVE25519_KEY_EXCHANGE_H_
6 #define QUICHE_QUIC_CORE_CRYPTO_CURVE25519_KEY_EXCHANGE_H_
7 
8 #include <cstdint>
9 #include <string>
10 
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/core/crypto/key_exchange.h"
13 #include "quiche/quic/core/crypto/quic_random.h"
14 #include "quiche/quic/platform/api/quic_export.h"
15 
16 namespace quic {
17 
18 // Curve25519KeyExchange implements a SynchronousKeyExchange using
19 // elliptic-curve Diffie-Hellman on curve25519. See http://cr.yp.to/ecdh.html
20 class QUICHE_EXPORT Curve25519KeyExchange : public SynchronousKeyExchange {
21  public:
22   ~Curve25519KeyExchange() override;
23 
24   // New generates a private key and then creates new key-exchange object.
25   static std::unique_ptr<Curve25519KeyExchange> New(QuicRandom* rand);
26 
27   // New creates a new key-exchange object from a private key. If |private_key|
28   // is invalid, nullptr is returned.
29   static std::unique_ptr<Curve25519KeyExchange> New(
30       absl::string_view private_key);
31 
32   // NewPrivateKey returns a private key, generated from |rand|, suitable for
33   // passing to |New|.
34   static std::string NewPrivateKey(QuicRandom* rand);
35 
36   // SynchronousKeyExchange interface.
37   bool CalculateSharedKeySync(absl::string_view peer_public_value,
38                               std::string* shared_key) const override;
39   absl::string_view public_value() const override;
type()40   QuicTag type() const override { return kC255; }
41 
42  private:
43   Curve25519KeyExchange();
44 
45   uint8_t private_key_[32];
46   uint8_t public_key_[32];
47 };
48 
49 }  // namespace quic
50 
51 #endif  // QUICHE_QUIC_CORE_CRYPTO_CURVE25519_KEY_EXCHANGE_H_
52