blob: 8885246f3e7b8e682d91e2639e89101307b7d3fd [file] [log] [blame]
David Benjaminc642aca2017-07-19 23:28:43 -04001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
18#include <string.h>
19
David Benjamin499742c2017-07-22 12:45:38 -040020#include <utility>
21
David Benjaminc642aca2017-07-19 23:28:43 -040022#include <openssl/bn.h>
23#include <openssl/bytestring.h>
24#include <openssl/curve25519.h>
25#include <openssl/ec.h>
26#include <openssl/err.h>
Sophie Schmieg58472cc2023-03-07 00:39:31 +000027#include <openssl/kyber.h>
Adam Langley7b935932018-11-12 13:53:42 -080028#include <openssl/hrss.h>
David Benjaminc642aca2017-07-19 23:28:43 -040029#include <openssl/mem.h>
30#include <openssl/nid.h>
Adam Langley7b935932018-11-12 13:53:42 -080031#include <openssl/rand.h>
David Benjaminc642aca2017-07-19 23:28:43 -040032
33#include "internal.h"
34#include "../crypto/internal.h"
David Benjaminc642aca2017-07-19 23:28:43 -040035
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070036BSSL_NAMESPACE_BEGIN
David Benjaminc642aca2017-07-19 23:28:43 -040037
38namespace {
39
40class ECKeyShare : public SSLKeyShare {
41 public:
David Benjamin9cbff812023-01-26 17:04:04 -050042 ECKeyShare(int nid, uint16_t group_id)
43 : group_(EC_GROUP_new_by_curve_name(nid)), group_id_(group_id) {}
David Benjaminc642aca2017-07-19 23:28:43 -040044
45 uint16_t GroupID() const override { return group_id_; }
46
David Benjamin08b1f382023-02-28 17:22:23 -050047 bool Generate(CBB *out) override {
David Benjaminc642aca2017-07-19 23:28:43 -040048 assert(!private_key_);
David Benjaminc11ea9422017-08-29 16:33:21 -040049 // Generate a private key.
David Benjaminc642aca2017-07-19 23:28:43 -040050 private_key_.reset(BN_new());
David Benjamin9cbff812023-01-26 17:04:04 -050051 if (!group_ || !private_key_ ||
David Benjaminc642aca2017-07-19 23:28:43 -040052 !BN_rand_range_ex(private_key_.get(), 1,
David Benjamin9cbff812023-01-26 17:04:04 -050053 EC_GROUP_get0_order(group_))) {
David Benjaminc642aca2017-07-19 23:28:43 -040054 return false;
55 }
56
David Benjaminc11ea9422017-08-29 16:33:21 -040057 // Compute the corresponding public key and serialize it.
David Benjamin9cbff812023-01-26 17:04:04 -050058 UniquePtr<EC_POINT> public_key(EC_POINT_new(group_));
David Benjaminc642aca2017-07-19 23:28:43 -040059 if (!public_key ||
David Benjamin9cbff812023-01-26 17:04:04 -050060 !EC_POINT_mul(group_, public_key.get(), private_key_.get(),
61 nullptr, nullptr, /*ctx=*/nullptr) ||
62 !EC_POINT_point2cbb(out, group_, public_key.get(),
63 POINT_CONVERSION_UNCOMPRESSED, /*ctx=*/nullptr)) {
David Benjaminc642aca2017-07-19 23:28:43 -040064 return false;
65 }
66
67 return true;
68 }
69
David Benjamin08b1f382023-02-28 17:22:23 -050070 bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret,
71 uint8_t *out_alert, Span<const uint8_t> peer_key) override {
72 // ECDH may be fit into a KEM-like abstraction by using a second keypair's
73 // public key as the ciphertext.
74 *out_alert = SSL_AD_INTERNAL_ERROR;
75 return Generate(out_ciphertext) && Decap(out_secret, out_alert, peer_key);
76 }
77
78 bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert,
79 Span<const uint8_t> ciphertext) override {
David Benjamin9cbff812023-01-26 17:04:04 -050080 assert(group_);
David Benjaminc642aca2017-07-19 23:28:43 -040081 assert(private_key_);
82 *out_alert = SSL_AD_INTERNAL_ERROR;
83
David Benjamin9cbff812023-01-26 17:04:04 -050084 UniquePtr<EC_POINT> peer_point(EC_POINT_new(group_));
85 UniquePtr<EC_POINT> result(EC_POINT_new(group_));
86 UniquePtr<BIGNUM> x(BN_new());
David Benjaminc642aca2017-07-19 23:28:43 -040087 if (!peer_point || !result || !x) {
88 return false;
89 }
90
David Benjamin08b1f382023-02-28 17:22:23 -050091 if (ciphertext.empty() || ciphertext[0] != POINT_CONVERSION_UNCOMPRESSED ||
92 !EC_POINT_oct2point(group_, peer_point.get(), ciphertext.data(),
93 ciphertext.size(), /*ctx=*/nullptr)) {
David Benjamin1bf23372018-02-14 19:50:38 -050094 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
David Benjaminc642aca2017-07-19 23:28:43 -040095 *out_alert = SSL_AD_DECODE_ERROR;
96 return false;
97 }
98
David Benjaminc11ea9422017-08-29 16:33:21 -040099 // Compute the x-coordinate of |peer_key| * |private_key_|.
David Benjamin9cbff812023-01-26 17:04:04 -0500100 if (!EC_POINT_mul(group_, result.get(), NULL, peer_point.get(),
101 private_key_.get(), /*ctx=*/nullptr) ||
102 !EC_POINT_get_affine_coordinates_GFp(group_, result.get(), x.get(),
103 NULL,
104 /*ctx=*/nullptr)) {
David Benjaminc642aca2017-07-19 23:28:43 -0400105 return false;
106 }
107
David Benjaminc11ea9422017-08-29 16:33:21 -0400108 // Encode the x-coordinate left-padded with zeros.
David Benjamin499742c2017-07-22 12:45:38 -0400109 Array<uint8_t> secret;
David Benjamin9cbff812023-01-26 17:04:04 -0500110 if (!secret.Init((EC_GROUP_get_degree(group_) + 7) / 8) ||
111 !BN_bn2bin_padded(secret.data(), secret.size(), x.get())) {
David Benjaminc642aca2017-07-19 23:28:43 -0400112 return false;
113 }
114
David Benjamin499742c2017-07-22 12:45:38 -0400115 *out_secret = std::move(secret);
David Benjaminc642aca2017-07-19 23:28:43 -0400116 return true;
117 }
118
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400119 bool SerializePrivateKey(CBB *out) override {
David Benjamin9cbff812023-01-26 17:04:04 -0500120 assert(group_);
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700121 assert(private_key_);
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700122 // Padding is added to avoid leaking the length.
David Benjamin9cbff812023-01-26 17:04:04 -0500123 size_t len = BN_num_bytes(EC_GROUP_get0_order(group_));
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400124 return BN_bn2cbb_padded(out, len, private_key_.get());
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700125 }
126
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400127 bool DeserializePrivateKey(CBS *in) override {
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700128 assert(!private_key_);
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400129 private_key_.reset(BN_bin2bn(CBS_data(in), CBS_len(in), nullptr));
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700130 return private_key_ != nullptr;
131 }
132
David Benjaminc642aca2017-07-19 23:28:43 -0400133 private:
134 UniquePtr<BIGNUM> private_key_;
David Benjamin9cbff812023-01-26 17:04:04 -0500135 const EC_GROUP *const group_ = nullptr;
David Benjaminc642aca2017-07-19 23:28:43 -0400136 uint16_t group_id_;
137};
138
139class X25519KeyShare : public SSLKeyShare {
140 public:
141 X25519KeyShare() {}
David Benjaminc642aca2017-07-19 23:28:43 -0400142
143 uint16_t GroupID() const override { return SSL_CURVE_X25519; }
144
David Benjamin08b1f382023-02-28 17:22:23 -0500145 bool Generate(CBB *out) override {
David Benjaminc642aca2017-07-19 23:28:43 -0400146 uint8_t public_key[32];
147 X25519_keypair(public_key, private_key_);
148 return !!CBB_add_bytes(out, public_key, sizeof(public_key));
149 }
150
David Benjamin08b1f382023-02-28 17:22:23 -0500151 bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret,
152 uint8_t *out_alert, Span<const uint8_t> peer_key) override {
153 // X25519 may be fit into a KEM-like abstraction by using a second keypair's
154 // public key as the ciphertext.
155 *out_alert = SSL_AD_INTERNAL_ERROR;
156 return Generate(out_ciphertext) && Decap(out_secret, out_alert, peer_key);
157 }
158
159 bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert,
160 Span<const uint8_t> ciphertext) override {
David Benjaminc642aca2017-07-19 23:28:43 -0400161 *out_alert = SSL_AD_INTERNAL_ERROR;
162
David Benjamin499742c2017-07-22 12:45:38 -0400163 Array<uint8_t> secret;
164 if (!secret.Init(32)) {
David Benjaminc642aca2017-07-19 23:28:43 -0400165 return false;
166 }
167
David Benjamin08b1f382023-02-28 17:22:23 -0500168 if (ciphertext.size() != 32 || //
169 !X25519(secret.data(), private_key_, ciphertext.data())) {
David Benjaminc642aca2017-07-19 23:28:43 -0400170 *out_alert = SSL_AD_DECODE_ERROR;
171 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
172 return false;
173 }
174
David Benjamin499742c2017-07-22 12:45:38 -0400175 *out_secret = std::move(secret);
David Benjaminc642aca2017-07-19 23:28:43 -0400176 return true;
177 }
178
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400179 bool SerializePrivateKey(CBB *out) override {
180 return CBB_add_bytes(out, private_key_, sizeof(private_key_));
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700181 }
182
David Benjamin0a6c3fc2021-03-29 16:11:12 -0400183 bool DeserializePrivateKey(CBS *in) override {
184 if (CBS_len(in) != sizeof(private_key_) ||
185 !CBS_copy_bytes(in, private_key_, sizeof(private_key_))) {
Matthew Braithwaite56986f92018-03-22 11:48:33 -0700186 return false;
187 }
188 return true;
189 }
190
David Benjaminc642aca2017-07-19 23:28:43 -0400191 private:
192 uint8_t private_key_[32];
193};
194
Adam Langleyfc077382023-01-08 16:22:31 -0800195class X25519Kyber768KeyShare : public SSLKeyShare {
196 public:
197 X25519Kyber768KeyShare() {}
198
199 uint16_t GroupID() const override { return SSL_CURVE_X25519KYBER768; }
200
David Benjamin08b1f382023-02-28 17:22:23 -0500201 bool Generate(CBB *out) override {
Sophie Schmieg58472cc2023-03-07 00:39:31 +0000202 uint8_t x25519_public_key[32];
203 X25519_keypair(x25519_public_key, x25519_private_key_);
204
205 uint8_t kyber_public_key[KYBER_PUBLIC_KEY_BYTES];
206 KYBER_generate_key(kyber_public_key, &kyber_private_key_);
207
208 if (!CBB_add_bytes(out, x25519_public_key, sizeof(x25519_public_key)) ||
209 !CBB_add_bytes(out, kyber_public_key, sizeof(kyber_public_key))) {
210 return false;
211 }
212
213 return true;
Adam Langleyfc077382023-01-08 16:22:31 -0800214 }
215
David Benjamin08b1f382023-02-28 17:22:23 -0500216 bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret,
217 uint8_t *out_alert, Span<const uint8_t> peer_key) override {
Sophie Schmieg58472cc2023-03-07 00:39:31 +0000218 Array<uint8_t> secret;
219 if (!secret.Init(32 + 32)) {
220 return false;
221 }
222
223 uint8_t x25519_public_key[32];
224 X25519_keypair(x25519_public_key, x25519_private_key_);
225 KYBER_public_key peer_kyber_pub;
226 CBS peer_key_cbs;
227 CBS peer_x25519_cbs;
228 CBS peer_kyber_cbs;
229 CBS_init(&peer_key_cbs, peer_key.data(), peer_key.size());
230 if (!CBS_get_bytes(&peer_key_cbs, &peer_x25519_cbs, 32) ||
231 !CBS_get_bytes(&peer_key_cbs, &peer_kyber_cbs,
232 KYBER_PUBLIC_KEY_BYTES) ||
233 CBS_len(&peer_key_cbs) != 0 ||
234 !X25519(secret.data(), x25519_private_key_,
235 CBS_data(&peer_x25519_cbs)) ||
236 !KYBER_parse_public_key(&peer_kyber_pub, &peer_kyber_cbs)) {
237 *out_alert = SSL_AD_DECODE_ERROR;
238 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
239 return false;
240 }
241
242 uint8_t kyber_ciphertext[KYBER_CIPHERTEXT_BYTES];
243 KYBER_encap(kyber_ciphertext, secret.data() + 32, secret.size() - 32,
244 &peer_kyber_pub);
245
246 if (!CBB_add_bytes(out_ciphertext, x25519_public_key,
247 sizeof(x25519_public_key)) ||
248 !CBB_add_bytes(out_ciphertext, kyber_ciphertext,
249 sizeof(kyber_ciphertext))) {
250 return false;
251 }
252
253 *out_secret = std::move(secret);
254 return true;
Adam Langleyfc077382023-01-08 16:22:31 -0800255 }
256
David Benjamin08b1f382023-02-28 17:22:23 -0500257 bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert,
258 Span<const uint8_t> ciphertext) override {
Sophie Schmieg58472cc2023-03-07 00:39:31 +0000259 *out_alert = SSL_AD_INTERNAL_ERROR;
260
261 Array<uint8_t> secret;
262 if (!secret.Init(32 + 32)) {
263 return false;
264 }
265
266 if (ciphertext.size() != 32 + KYBER_CIPHERTEXT_BYTES ||
267 !X25519(secret.data(), x25519_private_key_, ciphertext.data())) {
268 *out_alert = SSL_AD_DECODE_ERROR;
269 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
270 return false;
271 }
272
273 KYBER_decap(secret.data() + 32, secret.size() - 32, ciphertext.data() + 32,
274 &kyber_private_key_);
275 *out_secret = std::move(secret);
276 return true;
Adam Langleyfc077382023-01-08 16:22:31 -0800277 }
Sophie Schmieg58472cc2023-03-07 00:39:31 +0000278
279 private:
280 uint8_t x25519_private_key_[32];
281 KYBER_private_key kyber_private_key_;
Adam Langleyfc077382023-01-08 16:22:31 -0800282};
283
284class P256Kyber768KeyShare : public SSLKeyShare {
285 public:
286 P256Kyber768KeyShare() {}
287
288 uint16_t GroupID() const override { return SSL_CURVE_P256KYBER768; }
289
David Benjamin08b1f382023-02-28 17:22:23 -0500290 bool Generate(CBB *out) override {
Adam Langleyfc077382023-01-08 16:22:31 -0800291 // There is no implementation on Kyber in BoringSSL. BoringSSL must be
David Benjamin08b1f382023-02-28 17:22:23 -0500292 // patched for this KEM to be workable. It is not enabled by default.
Adam Langleyfc077382023-01-08 16:22:31 -0800293 return false;
294 }
295
David Benjamin08b1f382023-02-28 17:22:23 -0500296 bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret,
297 uint8_t *out_alert, Span<const uint8_t> peer_key) override {
Adam Langleyfc077382023-01-08 16:22:31 -0800298 return false;
299 }
300
David Benjamin08b1f382023-02-28 17:22:23 -0500301 bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert,
302 Span<const uint8_t> ciphertext) override {
Adam Langleyfc077382023-01-08 16:22:31 -0800303 return false;
304 }
305};
306
David Benjamin21440762022-03-19 18:39:18 -0400307constexpr NamedGroup kNamedGroups[] = {
David Benjamin6dda1662017-11-02 20:44:26 -0400308 {NID_secp224r1, SSL_CURVE_SECP224R1, "P-224", "secp224r1"},
309 {NID_X9_62_prime256v1, SSL_CURVE_SECP256R1, "P-256", "prime256v1"},
310 {NID_secp384r1, SSL_CURVE_SECP384R1, "P-384", "secp384r1"},
311 {NID_secp521r1, SSL_CURVE_SECP521R1, "P-521", "secp521r1"},
312 {NID_X25519, SSL_CURVE_X25519, "X25519", "x25519"},
Adam Langleyfc077382023-01-08 16:22:31 -0800313 {NID_X25519Kyber768, SSL_CURVE_X25519KYBER768, "X25519KYBER",
314 "X25519Kyber"},
315 {NID_P256Kyber768, SSL_CURVE_P256KYBER768, "P256KYBER", "P256Kyber"},
David Benjaminc642aca2017-07-19 23:28:43 -0400316};
317
318} // namespace
319
Matthew Braithwaitec65eb2c2018-11-02 17:29:35 -0700320Span<const NamedGroup> NamedGroups() {
321 return MakeConstSpan(kNamedGroups, OPENSSL_ARRAY_SIZE(kNamedGroups));
322}
323
David Benjaminc642aca2017-07-19 23:28:43 -0400324UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) {
325 switch (group_id) {
326 case SSL_CURVE_SECP224R1:
David Benjamin971b3302023-01-25 15:59:39 -0500327 return MakeUnique<ECKeyShare>(NID_secp224r1, SSL_CURVE_SECP224R1);
David Benjaminc642aca2017-07-19 23:28:43 -0400328 case SSL_CURVE_SECP256R1:
David Benjamin971b3302023-01-25 15:59:39 -0500329 return MakeUnique<ECKeyShare>(NID_X9_62_prime256v1, SSL_CURVE_SECP256R1);
David Benjaminc642aca2017-07-19 23:28:43 -0400330 case SSL_CURVE_SECP384R1:
David Benjamin971b3302023-01-25 15:59:39 -0500331 return MakeUnique<ECKeyShare>(NID_secp384r1, SSL_CURVE_SECP384R1);
David Benjaminc642aca2017-07-19 23:28:43 -0400332 case SSL_CURVE_SECP521R1:
David Benjamin971b3302023-01-25 15:59:39 -0500333 return MakeUnique<ECKeyShare>(NID_secp521r1, SSL_CURVE_SECP521R1);
David Benjaminc642aca2017-07-19 23:28:43 -0400334 case SSL_CURVE_X25519:
David Benjamin971b3302023-01-25 15:59:39 -0500335 return MakeUnique<X25519KeyShare>();
Adam Langleyfc077382023-01-08 16:22:31 -0800336 case SSL_CURVE_X25519KYBER768:
David Benjamin971b3302023-01-25 15:59:39 -0500337 return MakeUnique<X25519Kyber768KeyShare>();
Adam Langleyfc077382023-01-08 16:22:31 -0800338 case SSL_CURVE_P256KYBER768:
David Benjamin971b3302023-01-25 15:59:39 -0500339 return MakeUnique<P256Kyber768KeyShare>();
David Benjaminc642aca2017-07-19 23:28:43 -0400340 default:
341 return nullptr;
342 }
343}
344
David Benjamin8525ff32018-09-05 18:44:15 -0500345bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
David Benjaminc642aca2017-07-19 23:28:43 -0400346 for (const auto &group : kNamedGroups) {
347 if (group.nid == nid) {
348 *out_group_id = group.group_id;
David Benjamin8525ff32018-09-05 18:44:15 -0500349 return true;
David Benjaminc642aca2017-07-19 23:28:43 -0400350 }
351 }
David Benjamin8525ff32018-09-05 18:44:15 -0500352 return false;
David Benjaminc642aca2017-07-19 23:28:43 -0400353}
354
David Benjamin8525ff32018-09-05 18:44:15 -0500355bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
David Benjaminc642aca2017-07-19 23:28:43 -0400356 for (const auto &group : kNamedGroups) {
357 if (len == strlen(group.name) &&
358 !strncmp(group.name, name, len)) {
359 *out_group_id = group.group_id;
David Benjamin8525ff32018-09-05 18:44:15 -0500360 return true;
David Benjaminc642aca2017-07-19 23:28:43 -0400361 }
David Benjamin6dda1662017-11-02 20:44:26 -0400362 if (len == strlen(group.alias) &&
363 !strncmp(group.alias, name, len)) {
364 *out_group_id = group.group_id;
David Benjamin8525ff32018-09-05 18:44:15 -0500365 return true;
David Benjamin6dda1662017-11-02 20:44:26 -0400366 }
David Benjaminc642aca2017-07-19 23:28:43 -0400367 }
David Benjamin8525ff32018-09-05 18:44:15 -0500368 return false;
David Benjaminc642aca2017-07-19 23:28:43 -0400369}
370
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700371BSSL_NAMESPACE_END
David Benjaminc642aca2017-07-19 23:28:43 -0400372
373using namespace bssl;
374
375const char* SSL_get_curve_name(uint16_t group_id) {
376 for (const auto &group : kNamedGroups) {
377 if (group.group_id == group_id) {
378 return group.name;
379 }
380 }
381 return nullptr;
382}