David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 1 | /* 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 Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 22 | #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 Schmieg | 58472cc | 2023-03-07 00:39:31 +0000 | [diff] [blame] | 27 | #include <openssl/kyber.h> |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 28 | #include <openssl/hrss.h> |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 29 | #include <openssl/mem.h> |
| 30 | #include <openssl/nid.h> |
Adam Langley | 7b93593 | 2018-11-12 13:53:42 -0800 | [diff] [blame] | 31 | #include <openssl/rand.h> |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 32 | |
| 33 | #include "internal.h" |
| 34 | #include "../crypto/internal.h" |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 35 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 36 | BSSL_NAMESPACE_BEGIN |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | |
| 40 | class ECKeyShare : public SSLKeyShare { |
| 41 | public: |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 42 | ECKeyShare(int nid, uint16_t group_id) |
| 43 | : group_(EC_GROUP_new_by_curve_name(nid)), group_id_(group_id) {} |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 44 | |
| 45 | uint16_t GroupID() const override { return group_id_; } |
| 46 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 47 | bool Generate(CBB *out) override { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 48 | assert(!private_key_); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 49 | // Generate a private key. |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 50 | private_key_.reset(BN_new()); |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 51 | if (!group_ || !private_key_ || |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 52 | !BN_rand_range_ex(private_key_.get(), 1, |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 53 | EC_GROUP_get0_order(group_))) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 57 | // Compute the corresponding public key and serialize it. |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 58 | UniquePtr<EC_POINT> public_key(EC_POINT_new(group_)); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 59 | if (!public_key || |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 60 | !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 Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 70 | 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 Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 80 | assert(group_); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 81 | assert(private_key_); |
| 82 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 83 | |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 84 | 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 Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 87 | if (!peer_point || !result || !x) { |
| 88 | return false; |
| 89 | } |
| 90 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 91 | if (ciphertext.empty() || ciphertext[0] != POINT_CONVERSION_UNCOMPRESSED || |
| 92 | !EC_POINT_oct2point(group_, peer_point.get(), ciphertext.data(), |
| 93 | ciphertext.size(), /*ctx=*/nullptr)) { |
David Benjamin | 1bf2337 | 2018-02-14 19:50:38 -0500 | [diff] [blame] | 94 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 95 | *out_alert = SSL_AD_DECODE_ERROR; |
| 96 | return false; |
| 97 | } |
| 98 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 99 | // Compute the x-coordinate of |peer_key| * |private_key_|. |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 100 | 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 Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 108 | // Encode the x-coordinate left-padded with zeros. |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 109 | Array<uint8_t> secret; |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 110 | if (!secret.Init((EC_GROUP_get_degree(group_) + 7) / 8) || |
| 111 | !BN_bn2bin_padded(secret.data(), secret.size(), x.get())) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 112 | return false; |
| 113 | } |
| 114 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 115 | *out_secret = std::move(secret); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 119 | bool SerializePrivateKey(CBB *out) override { |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 120 | assert(group_); |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 121 | assert(private_key_); |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 122 | // Padding is added to avoid leaking the length. |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 123 | size_t len = BN_num_bytes(EC_GROUP_get0_order(group_)); |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 124 | return BN_bn2cbb_padded(out, len, private_key_.get()); |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 125 | } |
| 126 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 127 | bool DeserializePrivateKey(CBS *in) override { |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 128 | assert(!private_key_); |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 129 | private_key_.reset(BN_bin2bn(CBS_data(in), CBS_len(in), nullptr)); |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 130 | return private_key_ != nullptr; |
| 131 | } |
| 132 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 133 | private: |
| 134 | UniquePtr<BIGNUM> private_key_; |
David Benjamin | 9cbff81 | 2023-01-26 17:04:04 -0500 | [diff] [blame] | 135 | const EC_GROUP *const group_ = nullptr; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 136 | uint16_t group_id_; |
| 137 | }; |
| 138 | |
| 139 | class X25519KeyShare : public SSLKeyShare { |
| 140 | public: |
| 141 | X25519KeyShare() {} |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 142 | |
| 143 | uint16_t GroupID() const override { return SSL_CURVE_X25519; } |
| 144 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 145 | bool Generate(CBB *out) override { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 146 | 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 Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 151 | 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 Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 161 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 162 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 163 | Array<uint8_t> secret; |
| 164 | if (!secret.Init(32)) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 168 | if (ciphertext.size() != 32 || // |
| 169 | !X25519(secret.data(), private_key_, ciphertext.data())) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 170 | *out_alert = SSL_AD_DECODE_ERROR; |
| 171 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
| 172 | return false; |
| 173 | } |
| 174 | |
David Benjamin | 499742c | 2017-07-22 12:45:38 -0400 | [diff] [blame] | 175 | *out_secret = std::move(secret); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 176 | return true; |
| 177 | } |
| 178 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 179 | bool SerializePrivateKey(CBB *out) override { |
| 180 | return CBB_add_bytes(out, private_key_, sizeof(private_key_)); |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 181 | } |
| 182 | |
David Benjamin | 0a6c3fc | 2021-03-29 16:11:12 -0400 | [diff] [blame] | 183 | bool DeserializePrivateKey(CBS *in) override { |
| 184 | if (CBS_len(in) != sizeof(private_key_) || |
| 185 | !CBS_copy_bytes(in, private_key_, sizeof(private_key_))) { |
Matthew Braithwaite | 56986f9 | 2018-03-22 11:48:33 -0700 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | return true; |
| 189 | } |
| 190 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 191 | private: |
| 192 | uint8_t private_key_[32]; |
| 193 | }; |
| 194 | |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 195 | class X25519Kyber768KeyShare : public SSLKeyShare { |
| 196 | public: |
| 197 | X25519Kyber768KeyShare() {} |
| 198 | |
| 199 | uint16_t GroupID() const override { return SSL_CURVE_X25519KYBER768; } |
| 200 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 201 | bool Generate(CBB *out) override { |
Sophie Schmieg | 58472cc | 2023-03-07 00:39:31 +0000 | [diff] [blame] | 202 | 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 Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 214 | } |
| 215 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 216 | bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret, |
| 217 | uint8_t *out_alert, Span<const uint8_t> peer_key) override { |
Sophie Schmieg | 58472cc | 2023-03-07 00:39:31 +0000 | [diff] [blame] | 218 | 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 Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 255 | } |
| 256 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 257 | bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 258 | Span<const uint8_t> ciphertext) override { |
Sophie Schmieg | 58472cc | 2023-03-07 00:39:31 +0000 | [diff] [blame] | 259 | *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 Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 277 | } |
Sophie Schmieg | 58472cc | 2023-03-07 00:39:31 +0000 | [diff] [blame] | 278 | |
| 279 | private: |
| 280 | uint8_t x25519_private_key_[32]; |
| 281 | KYBER_private_key kyber_private_key_; |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 282 | }; |
| 283 | |
| 284 | class P256Kyber768KeyShare : public SSLKeyShare { |
| 285 | public: |
| 286 | P256Kyber768KeyShare() {} |
| 287 | |
| 288 | uint16_t GroupID() const override { return SSL_CURVE_P256KYBER768; } |
| 289 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 290 | bool Generate(CBB *out) override { |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 291 | // There is no implementation on Kyber in BoringSSL. BoringSSL must be |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 292 | // patched for this KEM to be workable. It is not enabled by default. |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 296 | bool Encap(CBB *out_ciphertext, Array<uint8_t> *out_secret, |
| 297 | uint8_t *out_alert, Span<const uint8_t> peer_key) override { |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 298 | return false; |
| 299 | } |
| 300 | |
David Benjamin | 08b1f38 | 2023-02-28 17:22:23 -0500 | [diff] [blame] | 301 | bool Decap(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 302 | Span<const uint8_t> ciphertext) override { |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | }; |
| 306 | |
David Benjamin | 2144076 | 2022-03-19 18:39:18 -0400 | [diff] [blame] | 307 | constexpr NamedGroup kNamedGroups[] = { |
David Benjamin | 6dda166 | 2017-11-02 20:44:26 -0400 | [diff] [blame] | 308 | {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 Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 313 | {NID_X25519Kyber768, SSL_CURVE_X25519KYBER768, "X25519KYBER", |
| 314 | "X25519Kyber"}, |
| 315 | {NID_P256Kyber768, SSL_CURVE_P256KYBER768, "P256KYBER", "P256Kyber"}, |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | } // namespace |
| 319 | |
Matthew Braithwaite | c65eb2c | 2018-11-02 17:29:35 -0700 | [diff] [blame] | 320 | Span<const NamedGroup> NamedGroups() { |
| 321 | return MakeConstSpan(kNamedGroups, OPENSSL_ARRAY_SIZE(kNamedGroups)); |
| 322 | } |
| 323 | |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 324 | UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) { |
| 325 | switch (group_id) { |
| 326 | case SSL_CURVE_SECP224R1: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 327 | return MakeUnique<ECKeyShare>(NID_secp224r1, SSL_CURVE_SECP224R1); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 328 | case SSL_CURVE_SECP256R1: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 329 | return MakeUnique<ECKeyShare>(NID_X9_62_prime256v1, SSL_CURVE_SECP256R1); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 330 | case SSL_CURVE_SECP384R1: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 331 | return MakeUnique<ECKeyShare>(NID_secp384r1, SSL_CURVE_SECP384R1); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 332 | case SSL_CURVE_SECP521R1: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 333 | return MakeUnique<ECKeyShare>(NID_secp521r1, SSL_CURVE_SECP521R1); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 334 | case SSL_CURVE_X25519: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 335 | return MakeUnique<X25519KeyShare>(); |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 336 | case SSL_CURVE_X25519KYBER768: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 337 | return MakeUnique<X25519Kyber768KeyShare>(); |
Adam Langley | fc07738 | 2023-01-08 16:22:31 -0800 | [diff] [blame] | 338 | case SSL_CURVE_P256KYBER768: |
David Benjamin | 971b330 | 2023-01-25 15:59:39 -0500 | [diff] [blame] | 339 | return MakeUnique<P256Kyber768KeyShare>(); |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 340 | default: |
| 341 | return nullptr; |
| 342 | } |
| 343 | } |
| 344 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 345 | bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 346 | for (const auto &group : kNamedGroups) { |
| 347 | if (group.nid == nid) { |
| 348 | *out_group_id = group.group_id; |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 349 | return true; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 350 | } |
| 351 | } |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 352 | return false; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 353 | } |
| 354 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 355 | bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) { |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 356 | 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 Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 360 | return true; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 361 | } |
David Benjamin | 6dda166 | 2017-11-02 20:44:26 -0400 | [diff] [blame] | 362 | if (len == strlen(group.alias) && |
| 363 | !strncmp(group.alias, name, len)) { |
| 364 | *out_group_id = group.group_id; |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 365 | return true; |
David Benjamin | 6dda166 | 2017-11-02 20:44:26 -0400 | [diff] [blame] | 366 | } |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 367 | } |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 368 | return false; |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 371 | BSSL_NAMESPACE_END |
David Benjamin | c642aca | 2017-07-19 23:28:43 -0400 | [diff] [blame] | 372 | |
| 373 | using namespace bssl; |
| 374 | |
| 375 | const 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 | } |