xref: /aosp_15_r20/external/cronet/crypto/ec_private_key.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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 CRYPTO_EC_PRIVATE_KEY_H_
6 #define CRYPTO_EC_PRIVATE_KEY_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 #include "base/containers/span.h"
16 #include "build/build_config.h"
17 #include "crypto/crypto_export.h"
18 #include "third_party/boringssl/src/include/openssl/base.h"
19 
20 namespace crypto {
21 
22 // Encapsulates an elliptic curve (EC) private key. Can be used to generate new
23 // keys, export keys to other formats, or to extract a public key.
24 // TODO(mattm): make this and RSAPrivateKey implement some PrivateKey interface.
25 // (The difference in types of key() and public_key() make this a little
26 // tricky.)
27 class CRYPTO_EXPORT ECPrivateKey {
28  public:
29   ECPrivateKey(const ECPrivateKey&) = delete;
30   ECPrivateKey& operator=(const ECPrivateKey&) = delete;
31 
32   ~ECPrivateKey();
33 
34   // Creates a new random instance. Can return nullptr if initialization fails.
35   // The created key will use the NIST P-256 curve.
36   static std::unique_ptr<ECPrivateKey> Create();
37 
38   // Create a new instance by importing an existing private key. The format is
39   // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return
40   // nullptr if initialization fails.
41   static std::unique_ptr<ECPrivateKey> CreateFromPrivateKeyInfo(
42       base::span<const uint8_t> input);
43 
44   // Creates a new instance by importing an existing key pair.
45   // The key pair is given as an ASN.1-encoded PKCS #8 EncryptedPrivateKeyInfo
46   // block with empty password and an X.509 SubjectPublicKeyInfo block.
47   // Returns nullptr if initialization fails.
48   //
49   // This function is deprecated. Use CreateFromPrivateKeyInfo for new code.
50   // See https://crbug.com/603319.
51   static std::unique_ptr<ECPrivateKey> CreateFromEncryptedPrivateKeyInfo(
52       base::span<const uint8_t> encrypted_private_key_info);
53 
54   // Returns a copy of the object.
55   std::unique_ptr<ECPrivateKey> Copy() const;
56 
key()57   EVP_PKEY* key() const { return key_.get(); }
58 
59   // Exports the private key to a PKCS #8 PrivateKeyInfo block.
60   bool ExportPrivateKey(std::vector<uint8_t>* output) const;
61 
62   // Exports the public key to an X.509 SubjectPublicKeyInfo block.
63   bool ExportPublicKey(std::vector<uint8_t>* output) const;
64 
65   // Exports the public key as an EC point in X9.62 uncompressed form. Note this
66   // includes the leading 0x04 byte.
67   bool ExportRawPublicKey(std::string* output) const;
68 
69  private:
70   // Constructor is private. Use one of the Create*() methods above instead.
71   ECPrivateKey();
72 
73   bssl::UniquePtr<EVP_PKEY> key_;
74 };
75 
76 }  // namespace crypto
77 
78 #endif  // CRYPTO_EC_PRIVATE_KEY_H_
79