xref: /aosp_15_r20/external/tink/cc/subtle/ecies_hkdf_recipient_kem_boringssl.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_SUBTLE_ECIES_HKDF_RECIPIENT_KEM_BORINGSSL_H_
18 #define TINK_SUBTLE_ECIES_HKDF_RECIPIENT_KEM_BORINGSSL_H_
19 
20 #include <memory>
21 
22 #include "absl/strings/string_view.h"
23 #include "openssl/ec.h"
24 #include "openssl/evp.h"
25 #include "tink/internal/fips_utils.h"
26 #include "tink/internal/ssl_unique_ptr.h"
27 #include "tink/subtle/common_enums.h"
28 #include "tink/util/secret_data.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace subtle {
34 
35 // HKDF-based KEM (key encapsulation mechanism) for ECIES recipient,
36 // using Boring SSL for the underlying cryptographic operations.
37 class EciesHkdfRecipientKemBoringSsl {
38  public:
39   // Constructs a recipient KEM for the specified curve and recipient's
40   // private key, which must be a big-endian byte array.
41   static crypto::tink::util::StatusOr<
42       std::unique_ptr<EciesHkdfRecipientKemBoringSsl>>
43   New(EllipticCurveType curve, util::SecretData priv_key);
44 
45   // Computes the ecdh's shared secret from our private key and peer's encoded
46   // public key, then uses hkdf to derive the symmetric key from the shared
47   // secret, hkdf info and hkdf salt.
48   virtual crypto::tink::util::StatusOr<util::SecretData> GenerateKey(
49       absl::string_view kem_bytes, HashType hash, absl::string_view hkdf_salt,
50       absl::string_view hkdf_info, uint32_t key_size_in_bytes,
51       EcPointFormat point_format) const = 0;
52 
53   virtual ~EciesHkdfRecipientKemBoringSsl() = default;
54 };
55 
56 // Implementation of EciesHkdfRecipientKemBoringSsl for the NIST P-curves.
57 class EciesHkdfNistPCurveRecipientKemBoringSsl
58     : public EciesHkdfRecipientKemBoringSsl {
59  public:
60   // Constructs a recipient KEM for the specified curve and recipient's
61   // private key, which must be a big-endian byte array.
62   static crypto::tink::util::StatusOr<
63       std::unique_ptr<EciesHkdfRecipientKemBoringSsl>>
64   New(EllipticCurveType curve, util::SecretData priv_key);
65 
66   // Computes the ecdh's shared secret from our private key and peer's encoded
67   // public key, then uses hkdf to derive the symmetric key from the shared
68   // secret, hkdf info and hkdf salt.
69   crypto::tink::util::StatusOr<util::SecretData> GenerateKey(
70       absl::string_view kem_bytes, HashType hash, absl::string_view hkdf_salt,
71       absl::string_view hkdf_info, uint32_t key_size_in_bytes,
72       EcPointFormat point_format) const override;
73 
74   static constexpr crypto::tink::internal::FipsCompatibility kFipsStatus =
75       crypto::tink::internal::FipsCompatibility::kNotFips;
76 
77  private:
78   EciesHkdfNistPCurveRecipientKemBoringSsl(
79       EllipticCurveType curve, util::SecretData priv_key_value,
80       internal::SslUniquePtr<EC_GROUP> ec_group);
81 
82   EllipticCurveType curve_;
83   util::SecretData priv_key_value_;
84   internal::SslUniquePtr<EC_GROUP> ec_group_;
85 };
86 
87 // Implementation of EciesHkdfRecipientKemBoringSsl for curve25519.
88 class EciesHkdfX25519RecipientKemBoringSsl
89     : public EciesHkdfRecipientKemBoringSsl {
90  public:
91   // Constructs a recipient KEM for the specified curve and recipient's
92   // private key, which must be a big-endian byte array.
93   static crypto::tink::util::StatusOr<
94       std::unique_ptr<EciesHkdfRecipientKemBoringSsl>>
95   New(EllipticCurveType curve, util::SecretData priv_key);
96 
97   // Computes the ecdh's shared secret from our private key and peer's encoded
98   // public key, then uses hkdf to derive the symmetric key from the shared
99   // secret, hkdf info and hkdf salt.
100   crypto::tink::util::StatusOr<util::SecretData> GenerateKey(
101       absl::string_view kem_bytes, HashType hash, absl::string_view hkdf_salt,
102       absl::string_view hkdf_info, uint32_t key_size_in_bytes,
103       EcPointFormat point_format) const override;
104 
105   static constexpr crypto::tink::internal::FipsCompatibility kFipsStatus =
106       crypto::tink::internal::FipsCompatibility::kNotFips;
107 
108  private:
109   explicit EciesHkdfX25519RecipientKemBoringSsl(
110       internal::SslUniquePtr<EVP_PKEY> private_key);
111 
112   const internal::SslUniquePtr<EVP_PKEY> private_key_;
113 };
114 
115 }  // namespace subtle
116 }  // namespace tink
117 }  // namespace crypto
118 
119 #endif  // TINK_SUBTLE_ECIES_HKDF_RECIPIENT_KEM_BORINGSSL_H_
120