1 /*
2 * Copyright (c) 2018 Thomas Pornin <pornin@bolet.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "inner.h"
26
27 static const unsigned char POINT_LEN[] = {
28 0, /* 0: not a valid curve ID */
29 43, /* sect163k1 */
30 43, /* sect163r1 */
31 43, /* sect163r2 */
32 51, /* sect193r1 */
33 51, /* sect193r2 */
34 61, /* sect233k1 */
35 61, /* sect233r1 */
36 61, /* sect239k1 */
37 73, /* sect283k1 */
38 73, /* sect283r1 */
39 105, /* sect409k1 */
40 105, /* sect409r1 */
41 145, /* sect571k1 */
42 145, /* sect571r1 */
43 41, /* secp160k1 */
44 41, /* secp160r1 */
45 41, /* secp160r2 */
46 49, /* secp192k1 */
47 49, /* secp192r1 */
48 57, /* secp224k1 */
49 57, /* secp224r1 */
50 65, /* secp256k1 */
51 65, /* secp256r1 */
52 97, /* secp384r1 */
53 133, /* secp521r1 */
54 65, /* brainpoolP256r1 */
55 97, /* brainpoolP384r1 */
56 129, /* brainpoolP512r1 */
57 32, /* curve25519 */
58 56, /* curve448 */
59 };
60
61 /* see bearssl_ec.h */
62 size_t
br_ec_compute_pub(const br_ec_impl * impl,br_ec_public_key * pk,void * kbuf,const br_ec_private_key * sk)63 br_ec_compute_pub(const br_ec_impl *impl, br_ec_public_key *pk,
64 void *kbuf, const br_ec_private_key *sk)
65 {
66 int curve;
67 size_t len;
68
69 curve = sk->curve;
70 if (curve < 0 || curve >= 32 || curve >= (int)(sizeof POINT_LEN)
71 || ((impl->supported_curves >> curve) & 1) == 0)
72 {
73 return 0;
74 }
75 if (kbuf == NULL) {
76 return POINT_LEN[curve];
77 }
78 len = impl->mulgen(kbuf, sk->x, sk->xlen, curve);
79 if (pk != NULL) {
80 pk->curve = curve;
81 pk->q = kbuf;
82 pk->qlen = len;
83 }
84 return len;
85 }
86