2 * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
27 static const unsigned char *
28 api_generator(int curve
, size_t *len
)
32 return br_ec_p256_m31
.generator(curve
, len
);
33 case BR_EC_curve25519
:
34 #if BR_INT128 || BR_UMUL128
35 return br_ec_c25519_m62
.generator(curve
, len
);
37 return br_ec_c25519_m31
.generator(curve
, len
);
40 return br_ec_prime_i31
.generator(curve
, len
);
44 static const unsigned char *
45 api_order(int curve
, size_t *len
)
49 return br_ec_p256_m31
.order(curve
, len
);
50 case BR_EC_curve25519
:
51 #if BR_INT128 || BR_UMUL128
52 return br_ec_c25519_m62
.order(curve
, len
);
54 return br_ec_c25519_m31
.order(curve
, len
);
57 return br_ec_prime_i31
.order(curve
, len
);
62 api_xoff(int curve
, size_t *len
)
66 return br_ec_p256_m31
.xoff(curve
, len
);
67 case BR_EC_curve25519
:
68 #if BR_INT128 || BR_UMUL128
69 return br_ec_c25519_m62
.xoff(curve
, len
);
71 return br_ec_c25519_m31
.xoff(curve
, len
);
74 return br_ec_prime_i31
.xoff(curve
, len
);
79 api_mul(unsigned char *G
, size_t Glen
,
80 const unsigned char *kb
, size_t kblen
, int curve
)
84 return br_ec_p256_m31
.mul(G
, Glen
, kb
, kblen
, curve
);
85 case BR_EC_curve25519
:
86 #if BR_INT128 || BR_UMUL128
87 return br_ec_c25519_m62
.mul(G
, Glen
, kb
, kblen
, curve
);
89 return br_ec_c25519_m31
.mul(G
, Glen
, kb
, kblen
, curve
);
92 return br_ec_prime_i31
.mul(G
, Glen
, kb
, kblen
, curve
);
97 api_mulgen(unsigned char *R
,
98 const unsigned char *x
, size_t xlen
, int curve
)
101 case BR_EC_secp256r1
:
102 return br_ec_p256_m31
.mulgen(R
, x
, xlen
, curve
);
103 case BR_EC_curve25519
:
104 #if BR_INT128 || BR_UMUL128
105 return br_ec_c25519_m62
.mulgen(R
, x
, xlen
, curve
);
107 return br_ec_c25519_m31
.mulgen(R
, x
, xlen
, curve
);
110 return br_ec_prime_i31
.mulgen(R
, x
, xlen
, curve
);
115 api_muladd(unsigned char *A
, const unsigned char *B
, size_t len
,
116 const unsigned char *x
, size_t xlen
,
117 const unsigned char *y
, size_t ylen
, int curve
)
120 case BR_EC_secp256r1
:
121 return br_ec_p256_m31
.muladd(A
, B
, len
,
122 x
, xlen
, y
, ylen
, curve
);
123 case BR_EC_curve25519
:
124 #if BR_INT128 || BR_UMUL128
125 return br_ec_c25519_m62
.muladd(A
, B
, len
,
126 x
, xlen
, y
, ylen
, curve
);
128 return br_ec_c25519_m31
.muladd(A
, B
, len
,
129 x
, xlen
, y
, ylen
, curve
);
132 return br_ec_prime_i31
.muladd(A
, B
, len
,
133 x
, xlen
, y
, ylen
, curve
);
137 /* see bearssl_ec.h */
138 const br_ec_impl br_ec_all_m31
= {
139 (uint32_t)0x23800000,