Coverage Report

Created: 2024-11-21 06:38

/src/BearSSL/src/ec/ec_pubkey.c
Line
Count
Source (jump to first uncovered line)
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
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
1.99k
{
66
1.99k
  int curve;
67
1.99k
  size_t len;
68
69
1.99k
  curve = sk->curve;
70
1.99k
  if (curve < 0 || curve >= 32 || curve >= (int)(sizeof POINT_LEN)
71
1.99k
    || ((impl->supported_curves >> curve) & 1) == 0)
72
204
  {
73
204
    return 0;
74
204
  }
75
1.78k
  if (kbuf == NULL) {
76
0
    return POINT_LEN[curve];
77
0
  }
78
1.78k
  len = impl->mulgen(kbuf, sk->x, sk->xlen, curve);
79
1.78k
  if (pk != NULL) {
80
1.78k
    pk->curve = curve;
81
1.78k
    pk->q = kbuf;
82
1.78k
    pk->qlen = len;
83
1.78k
  }
84
1.78k
  return len;
85
1.78k
}