Path: blob/main/contrib/bearssl/src/ec/ec_pubkey.c
39507 views
/*1* Copyright (c) 2018 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526static const unsigned char POINT_LEN[] = {270, /* 0: not a valid curve ID */2843, /* sect163k1 */2943, /* sect163r1 */3043, /* sect163r2 */3151, /* sect193r1 */3251, /* sect193r2 */3361, /* sect233k1 */3461, /* sect233r1 */3561, /* sect239k1 */3673, /* sect283k1 */3773, /* sect283r1 */38105, /* sect409k1 */39105, /* sect409r1 */40145, /* sect571k1 */41145, /* sect571r1 */4241, /* secp160k1 */4341, /* secp160r1 */4441, /* secp160r2 */4549, /* secp192k1 */4649, /* secp192r1 */4757, /* secp224k1 */4857, /* secp224r1 */4965, /* secp256k1 */5065, /* secp256r1 */5197, /* secp384r1 */52133, /* secp521r1 */5365, /* brainpoolP256r1 */5497, /* brainpoolP384r1 */55129, /* brainpoolP512r1 */5632, /* curve25519 */5756, /* curve448 */58};5960/* see bearssl_ec.h */61size_t62br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,63void *kbuf, const br_ec_private_key *sk)64{65int curve;66size_t len;6768curve = sk->curve;69if (curve < 0 || curve >= 32 || curve >= (int)(sizeof POINT_LEN)70|| ((impl->supported_curves >> curve) & 1) == 0)71{72return 0;73}74if (kbuf == NULL) {75return POINT_LEN[curve];76}77len = impl->mulgen(kbuf, sk->x, sk->xlen, curve);78if (pk != NULL) {79pk->curve = curve;80pk->q = kbuf;81pk->qlen = len;82}83return len;84}858687