OLD | NEW |
| (Empty) |
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 NET_QUIC_CRYPTO_P256_KEY_EXCHANGE_H_ | |
6 #define NET_QUIC_CRYPTO_P256_KEY_EXCHANGE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <string> | |
12 | |
13 #include "base/macros.h" | |
14 #include "base/strings/string_piece.h" | |
15 #include "crypto/openssl_util.h" | |
16 #include "crypto/scoped_openssl_types.h" | |
17 #include "net/base/net_export.h" | |
18 #include "net/quic/crypto/key_exchange.h" | |
19 | |
20 | |
21 namespace net { | |
22 | |
23 // P256KeyExchange implements a KeyExchange using elliptic-curve | |
24 // Diffie-Hellman on NIST P-256. | |
25 class NET_EXPORT_PRIVATE P256KeyExchange : public KeyExchange { | |
26 public: | |
27 ~P256KeyExchange() override; | |
28 | |
29 // New creates a new key exchange object from a private key. If | |
30 // |private_key| is invalid, nullptr is returned. | |
31 static P256KeyExchange* New(base::StringPiece private_key); | |
32 | |
33 // |NewPrivateKey| returns a private key, suitable for passing to |New|. | |
34 // If |NewPrivateKey| can't generate a private key, it returns an empty | |
35 // string. | |
36 static std::string NewPrivateKey(); | |
37 | |
38 // KeyExchange interface. | |
39 KeyExchange* NewKeyPair(QuicRandom* rand) const override; | |
40 bool CalculateSharedKey(base::StringPiece peer_public_value, | |
41 std::string* shared_key) const override; | |
42 base::StringPiece public_value() const override; | |
43 QuicTag tag() const override; | |
44 | |
45 private: | |
46 enum { | |
47 // A P-256 field element consists of 32 bytes. | |
48 kP256FieldBytes = 32, | |
49 // A P-256 point in uncompressed form consists of 0x04 (to denote | |
50 // that the point is uncompressed) followed by two, 32-byte field | |
51 // elements. | |
52 kUncompressedP256PointBytes = 1 + 2 * kP256FieldBytes, | |
53 // The first byte in an uncompressed P-256 point. | |
54 kUncompressedECPointForm = 0x04, | |
55 }; | |
56 | |
57 // P256KeyExchange takes ownership of |private_key|, and expects | |
58 // |public_key| consists of |kUncompressedP256PointBytes| bytes. | |
59 P256KeyExchange(EC_KEY* private_key, const uint8_t* public_key); | |
60 | |
61 crypto::ScopedEC_KEY private_key_; | |
62 // The public key stored as an uncompressed P-256 point. | |
63 uint8_t public_key_[kUncompressedP256PointBytes]; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(P256KeyExchange); | |
66 }; | |
67 | |
68 } // namespace net | |
69 #endif // NET_QUIC_CRYPTO_P256_KEY_EXCHANGE_H_ | |
OLD | NEW |