• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tink/subtle/ecies_hkdf_sender_kem_boringssl.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/string_view.h"
26 #include "openssl/bn.h"
27 #include "openssl/evp.h"
28 #include "tink/internal/ec_util.h"
29 #include "tink/internal/ssl_unique_ptr.h"
30 #include "tink/subtle/common_enums.h"
31 #include "tink/subtle/hkdf.h"
32 #include "tink/util/secret_data.h"
33 
34 namespace crypto {
35 namespace tink {
36 namespace subtle {
37 
38 // static
39 util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl>>
New(subtle::EllipticCurveType curve,const std::string & pubx,const std::string & puby)40 EciesHkdfSenderKemBoringSsl::New(subtle::EllipticCurveType curve,
41                                  const std::string& pubx,
42                                  const std::string& puby) {
43   switch (curve) {
44     case EllipticCurveType::NIST_P256:
45     case EllipticCurveType::NIST_P384:
46     case EllipticCurveType::NIST_P521:
47       return EciesHkdfNistPCurveSendKemBoringSsl::New(curve, pubx, puby);
48     case EllipticCurveType::CURVE25519:
49       return EciesHkdfX25519SendKemBoringSsl::New(curve, pubx, puby);
50     default:
51       return util::Status(absl::StatusCode::kUnimplemented,
52                           "Unsupported elliptic curve");
53   }
54 }
55 
EciesHkdfNistPCurveSendKemBoringSsl(subtle::EllipticCurveType curve,const std::string & pubx,const std::string & puby,internal::SslUniquePtr<EC_POINT> peer_pub_key)56 EciesHkdfNistPCurveSendKemBoringSsl::EciesHkdfNistPCurveSendKemBoringSsl(
57     subtle::EllipticCurveType curve, const std::string& pubx,
58     const std::string& puby, internal::SslUniquePtr<EC_POINT> peer_pub_key)
59     : curve_(curve),
60       pubx_(pubx),
61       puby_(puby),
62       peer_pub_key_(std::move(peer_pub_key)) {}
63 
64 // static
65 util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl>>
New(subtle::EllipticCurveType curve,const std::string & pubx,const std::string & puby)66 EciesHkdfNistPCurveSendKemBoringSsl::New(subtle::EllipticCurveType curve,
67                                          const std::string& pubx,
68                                          const std::string& puby) {
69   auto status =
70       internal::CheckFipsCompatibility<EciesHkdfNistPCurveSendKemBoringSsl>();
71   if (!status.ok()) return status;
72 
73   auto status_or_ec_point = internal::GetEcPoint(curve, pubx, puby);
74   if (!status_or_ec_point.ok()) return status_or_ec_point.status();
75   std::unique_ptr<const EciesHkdfSenderKemBoringSsl> sender_kem(
76       new EciesHkdfNistPCurveSendKemBoringSsl(
77           curve, pubx, puby, std::move(status_or_ec_point.value())));
78   return std::move(sender_kem);
79 }
80 
81 util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl::KemKey>>
GenerateKey(subtle::HashType hash,absl::string_view hkdf_salt,absl::string_view hkdf_info,uint32_t key_size_in_bytes,subtle::EcPointFormat point_format) const82 EciesHkdfNistPCurveSendKemBoringSsl::GenerateKey(
83     subtle::HashType hash, absl::string_view hkdf_salt,
84     absl::string_view hkdf_info, uint32_t key_size_in_bytes,
85     subtle::EcPointFormat point_format) const {
86   if (peer_pub_key_.get() == nullptr) {
87     return util::Status(absl::StatusCode::kInternal,
88                         "peer_pub_key_ wasn't initialized");
89   }
90 
91   auto status_or_ec_group = internal::EcGroupFromCurveType(curve_);
92   if (!status_or_ec_group.ok()) {
93     return status_or_ec_group.status();
94   }
95   internal::SslUniquePtr<EC_GROUP> group =
96       std::move(status_or_ec_group.value());
97   internal::SslUniquePtr<EC_KEY> ephemeral_key(EC_KEY_new());
98   if (1 != EC_KEY_set_group(ephemeral_key.get(), group.get())) {
99     return util::Status(absl::StatusCode::kInternal, "EC_KEY_set_group failed");
100   }
101   if (1 != EC_KEY_generate_key(ephemeral_key.get())) {
102     return util::Status(absl::StatusCode::kInternal,
103                         "EC_KEY_generate_key failed");
104   }
105   const BIGNUM* ephemeral_priv = EC_KEY_get0_private_key(ephemeral_key.get());
106   const EC_POINT* ephemeral_pub = EC_KEY_get0_public_key(ephemeral_key.get());
107   auto status_or_string_kem =
108       internal::EcPointEncode(curve_, point_format, ephemeral_pub);
109   if (!status_or_string_kem.ok()) {
110     return status_or_string_kem.status();
111   }
112   std::string kem_bytes = status_or_string_kem.value();
113   auto status_or_string_shared_secret = internal::ComputeEcdhSharedSecret(
114       curve_, ephemeral_priv, peer_pub_key_.get());
115   if (!status_or_string_shared_secret.ok()) {
116     return status_or_string_shared_secret.status();
117   }
118   util::SecretData shared_secret = status_or_string_shared_secret.value();
119   auto symmetric_key_or = Hkdf::ComputeEciesHkdfSymmetricKey(
120       hash, kem_bytes, shared_secret, hkdf_salt, hkdf_info, key_size_in_bytes);
121   if (!symmetric_key_or.ok()) {
122     return symmetric_key_or.status();
123   }
124   util::SecretData symmetric_key = symmetric_key_or.value();
125   return absl::make_unique<const KemKey>(std::move(kem_bytes),
126                                          std::move(symmetric_key));
127 }
128 
EciesHkdfX25519SendKemBoringSsl(internal::SslUniquePtr<EVP_PKEY> peer_public_key)129 EciesHkdfX25519SendKemBoringSsl::EciesHkdfX25519SendKemBoringSsl(
130     internal::SslUniquePtr<EVP_PKEY> peer_public_key)
131     : peer_public_key_(std::move(peer_public_key)) {}
132 
133 // static
134 util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl>>
New(subtle::EllipticCurveType curve,const std::string & pubx,const std::string & puby)135 EciesHkdfX25519SendKemBoringSsl::New(subtle::EllipticCurveType curve,
136                                      const std::string& pubx,
137                                      const std::string& puby) {
138   auto status =
139       internal::CheckFipsCompatibility<EciesHkdfX25519SendKemBoringSsl>();
140   if (!status.ok()) return status;
141 
142   if (curve != CURVE25519) {
143     return util::Status(absl::StatusCode::kInvalidArgument,
144                         "curve is not CURVE25519");
145   }
146   if (pubx.size() != internal::X25519KeyPubKeySize()) {
147     return util::Status(absl::StatusCode::kInvalidArgument,
148                         "pubx has unexpected length");
149   }
150   if (!puby.empty()) {
151     return util::Status(absl::StatusCode::kInvalidArgument,
152                         "puby is not empty");
153   }
154 
155   internal::SslUniquePtr<EVP_PKEY> peer_public_key(EVP_PKEY_new_raw_public_key(
156       /*type=*/EVP_PKEY_X25519, /*unused=*/nullptr,
157       /*in=*/reinterpret_cast<const uint8_t*>(pubx.data()),
158       /*len=*/internal::Ed25519KeyPubKeySize()));
159   if (peer_public_key == nullptr) {
160     return util::Status(absl::StatusCode::kInternal,
161                         "EVP_PKEY_new_raw_public_key failed");
162   }
163   std::unique_ptr<const EciesHkdfSenderKemBoringSsl> sender_kem(
164       new EciesHkdfX25519SendKemBoringSsl(std::move(peer_public_key)));
165   return std::move(sender_kem);
166 }
167 
168 util::StatusOr<std::unique_ptr<const EciesHkdfSenderKemBoringSsl::KemKey>>
GenerateKey(subtle::HashType hash,absl::string_view hkdf_salt,absl::string_view hkdf_info,uint32_t key_size_in_bytes,subtle::EcPointFormat point_format) const169 EciesHkdfX25519SendKemBoringSsl::GenerateKey(
170     subtle::HashType hash, absl::string_view hkdf_salt,
171     absl::string_view hkdf_info, uint32_t key_size_in_bytes,
172     subtle::EcPointFormat point_format) const {
173   if (point_format != EcPointFormat::COMPRESSED) {
174     return util::Status(
175         absl::StatusCode::kInvalidArgument,
176         "X25519 only supports compressed elliptic curve points");
177   }
178 
179   // Generate an ephemeral key pair; the public key is the KEM key to use.
180   util::StatusOr<std::unique_ptr<internal::X25519Key>> ephemeral_key =
181       internal::NewX25519Key();
182 
183   internal::SslUniquePtr<EVP_PKEY> ssl_priv_key(EVP_PKEY_new_raw_private_key(
184       /*type=*/EVP_PKEY_X25519, /*unused=*/nullptr,
185       /*in=*/(*ephemeral_key)->private_key,
186       /*len=*/internal::Ed25519KeyPrivKeySize()));
187   if (ssl_priv_key == nullptr) {
188     return util::Status(absl::StatusCode::kInternal,
189                         "EVP_PKEY_new_raw_private_key failed");
190   }
191 
192   util::StatusOr<util::SecretData> shared_secret =
193       internal::ComputeX25519SharedSecret(ssl_priv_key.get(),
194                                           peer_public_key_.get());
195 
196   auto public_key = absl::string_view(
197       reinterpret_cast<const char*>((*ephemeral_key)->public_value),
198       internal::X25519KeyPubKeySize());
199 
200   util::StatusOr<util::SecretData> symmetric_key_or =
201       Hkdf::ComputeEciesHkdfSymmetricKey(hash, public_key, *shared_secret,
202                                          hkdf_salt, hkdf_info,
203                                          key_size_in_bytes);
204   if (!symmetric_key_or.ok()) {
205     return symmetric_key_or.status();
206   }
207   util::SecretData symmetric_key = *symmetric_key_or;
208   return absl::make_unique<const KemKey>(std::string(public_key),
209                                          symmetric_key);
210 }
211 
212 }  // namespace subtle
213 }  // namespace tink
214 }  // namespace crypto
215