Made ec_c25519_m62 implementation the default on supported architectures.
[BearSSL] / src / ec / ec_all_m31.c
1 /*
2 * Copyright (c) 2017 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 *
28 api_generator(int curve, size_t *len)
29 {
30 switch (curve) {
31 case BR_EC_secp256r1:
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);
36 #else
37 return br_ec_c25519_m31.generator(curve, len);
38 #endif
39 default:
40 return br_ec_prime_i31.generator(curve, len);
41 }
42 }
43
44 static const unsigned char *
45 api_order(int curve, size_t *len)
46 {
47 switch (curve) {
48 case BR_EC_secp256r1:
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);
53 #else
54 return br_ec_c25519_m31.order(curve, len);
55 #endif
56 default:
57 return br_ec_prime_i31.order(curve, len);
58 }
59 }
60
61 static size_t
62 api_xoff(int curve, size_t *len)
63 {
64 switch (curve) {
65 case BR_EC_secp256r1:
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);
70 #else
71 return br_ec_c25519_m31.xoff(curve, len);
72 #endif
73 default:
74 return br_ec_prime_i31.xoff(curve, len);
75 }
76 }
77
78 static uint32_t
79 api_mul(unsigned char *G, size_t Glen,
80 const unsigned char *kb, size_t kblen, int curve)
81 {
82 switch (curve) {
83 case BR_EC_secp256r1:
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);
88 #else
89 return br_ec_c25519_m31.mul(G, Glen, kb, kblen, curve);
90 #endif
91 default:
92 return br_ec_prime_i31.mul(G, Glen, kb, kblen, curve);
93 }
94 }
95
96 static size_t
97 api_mulgen(unsigned char *R,
98 const unsigned char *x, size_t xlen, int curve)
99 {
100 switch (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);
106 #else
107 return br_ec_c25519_m31.mulgen(R, x, xlen, curve);
108 #endif
109 default:
110 return br_ec_prime_i31.mulgen(R, x, xlen, curve);
111 }
112 }
113
114 static uint32_t
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)
118 {
119 switch (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);
127 #else
128 return br_ec_c25519_m31.muladd(A, B, len,
129 x, xlen, y, ylen, curve);
130 #endif
131 default:
132 return br_ec_prime_i31.muladd(A, B, len,
133 x, xlen, y, ylen, curve);
134 }
135 }
136
137 /* see bearssl_ec.h */
138 const br_ec_impl br_ec_all_m31 = {
139 (uint32_t)0x23800000,
140 &api_generator,
141 &api_order,
142 &api_xoff,
143 &api_mul,
144 &api_mulgen,
145 &api_muladd
146 };