@pulse-compute/crypto 1.0.0-beta.5 → 1.0.0-beta.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -4
- package/dist/contracts.d.ts +34 -12
- package/dist/contracts.d.ts.map +1 -1
- package/dist/contracts.js +9 -3
- package/dist/contracts.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/rsa.d.ts +58 -0
- package/dist/internal/rsa.d.ts.map +1 -0
- package/dist/internal/rsa.js +185 -0
- package/dist/internal/rsa.js.map +1 -0
- package/dist/internal/verification.d.ts +3 -3
- package/dist/internal/verification.d.ts.map +1 -1
- package/dist/internal/verification.js +2 -2
- package/dist/internal/verification.js.map +1 -1
- package/guests/es256-rustcrypto/README.md +43 -0
- package/guests/es256-rustcrypto/build.cjs +297 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.unoptimized.wasm +0 -0
- package/guests/es256-rustcrypto/prebuilt/es256-verifier.wasm +0 -0
- package/guests/es256-rustcrypto/probe/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/probe/Cargo.lock +238 -0
- package/guests/es256-rustcrypto/probe/Cargo.toml +26 -0
- package/guests/es256-rustcrypto/probe/README.md +35 -0
- package/guests/es256-rustcrypto/probe/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/probe/src/lib.rs +108 -0
- package/guests/es256-rustcrypto/pulse.guest-unit.json +119 -0
- package/guests/es256-rustcrypto/source/.cargo/config.toml +18 -0
- package/guests/es256-rustcrypto/source/Cargo.lock +265 -0
- package/guests/es256-rustcrypto/source/Cargo.toml +23 -0
- package/guests/es256-rustcrypto/source/README.md +26 -0
- package/guests/es256-rustcrypto/source/bearssl/LICENSE.txt +21 -0
- package/guests/es256-rustcrypto/source/bearssl/UPSTREAM.json +49 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl.h +170 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_aead.h +1059 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_block.h +2618 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ec.h +883 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hash.h +1346 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_hmac.h +241 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_kdf.h +185 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_pem.h +294 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_prf.h +150 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rand.h +397 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_rsa.h +1366 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_ssl.h +4296 -0
- package/guests/es256-rustcrypto/source/bearssl/inc/bearssl_x509.h +1397 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/ccopy.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/dec32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/codec/enc32be.c +38 -0
- package/guests/es256-rustcrypto/source/bearssl/src/config.h +229 -0
- package/guests/es256-rustcrypto/source/bearssl/src/inner.h +2532 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_add.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_bitlen.c +44 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decmod.c +124 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decode.c +57 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_decred.c +103 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_encode.c +79 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_fmont.c +60 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_iszero.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_moddiv.c +488 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow.c +65 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_modpow2.c +160 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_montmul.c +93 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_mulacc.c +61 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_muladd.c +157 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_ninv31.c +39 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_reduce.c +66 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_rshift.c +47 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_sub.c +46 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i31_tmont.c +36 -0
- package/guests/es256-rustcrypto/source/bearssl/src/int/i32_div32.c +56 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_priv.c +203 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_i31_pub.c +106 -0
- package/guests/es256-rustcrypto/source/bearssl/src/rsa/rsa_pkcs1_sig_pad.c +100 -0
- package/guests/es256-rustcrypto/source/build.rs +38 -0
- package/guests/es256-rustcrypto/source/freestanding/string.h +8 -0
- package/guests/es256-rustcrypto/source/rs256.c +103 -0
- package/guests/es256-rustcrypto/source/rust-toolchain.toml +5 -0
- package/guests/es256-rustcrypto/source/src/lib.rs +308 -0
- package/package.json +6 -4
- package/pulsewasm.native.cjs +66 -0
- package/src/provider.cjs +71 -1
- package/src/provider.d.cts +6 -0
|
@@ -0,0 +1,2532 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2016 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
|
+
#ifndef INNER_H__
|
|
26
|
+
#define INNER_H__
|
|
27
|
+
|
|
28
|
+
#include <string.h>
|
|
29
|
+
#include <limits.h>
|
|
30
|
+
|
|
31
|
+
#include "config.h"
|
|
32
|
+
#include "bearssl.h"
|
|
33
|
+
|
|
34
|
+
/*
|
|
35
|
+
* On MSVC, disable the warning about applying unary minus on an
|
|
36
|
+
* unsigned type: it is standard, we do it all the time, and for
|
|
37
|
+
* good reasons.
|
|
38
|
+
*/
|
|
39
|
+
#if _MSC_VER
|
|
40
|
+
#pragma warning( disable : 4146 )
|
|
41
|
+
#endif
|
|
42
|
+
|
|
43
|
+
/*
|
|
44
|
+
* Maximum size for a RSA modulus (in bits). Allocated stack buffers
|
|
45
|
+
* depend on that size, so this value should be kept small. Currently,
|
|
46
|
+
* 2048-bit RSA keys offer adequate security, and should still do so for
|
|
47
|
+
* the next few decades; however, a number of widespread PKI have
|
|
48
|
+
* already set their root keys to RSA-4096, so we should be able to
|
|
49
|
+
* process such keys.
|
|
50
|
+
*
|
|
51
|
+
* This value MUST be a multiple of 64. This value MUST NOT exceed 47666
|
|
52
|
+
* (some computations in RSA key generation rely on the factor size being
|
|
53
|
+
* no more than 23833 bits). RSA key sizes beyond 3072 bits don't make a
|
|
54
|
+
* lot of sense anyway.
|
|
55
|
+
*/
|
|
56
|
+
#define BR_MAX_RSA_SIZE 4096
|
|
57
|
+
|
|
58
|
+
/*
|
|
59
|
+
* Minimum size for a RSA modulus (in bits); this value is used only to
|
|
60
|
+
* filter out invalid parameters for key pair generation. Normally,
|
|
61
|
+
* applications should not use RSA keys smaller than 2048 bits; but some
|
|
62
|
+
* specific cases might need shorter keys, for legacy or research
|
|
63
|
+
* purposes.
|
|
64
|
+
*/
|
|
65
|
+
#define BR_MIN_RSA_SIZE 512
|
|
66
|
+
|
|
67
|
+
/*
|
|
68
|
+
* Maximum size for a RSA factor (in bits). This is for RSA private-key
|
|
69
|
+
* operations. Default is to support factors up to a bit more than half
|
|
70
|
+
* the maximum modulus size.
|
|
71
|
+
*
|
|
72
|
+
* This value MUST be a multiple of 32.
|
|
73
|
+
*/
|
|
74
|
+
#define BR_MAX_RSA_FACTOR ((BR_MAX_RSA_SIZE + 64) >> 1)
|
|
75
|
+
|
|
76
|
+
/*
|
|
77
|
+
* Maximum size for an EC curve (modulus or order), in bits. Size of
|
|
78
|
+
* stack buffers depends on that parameter. This size MUST be a multiple
|
|
79
|
+
* of 8 (so that decoding an integer with that many bytes does not
|
|
80
|
+
* overflow).
|
|
81
|
+
*/
|
|
82
|
+
#define BR_MAX_EC_SIZE 528
|
|
83
|
+
|
|
84
|
+
/*
|
|
85
|
+
* Some macros to recognize the current architecture. Right now, we are
|
|
86
|
+
* interested into automatically recognizing architecture with efficient
|
|
87
|
+
* 64-bit types so that we may automatically use implementations that
|
|
88
|
+
* use 64-bit registers in that case. Future versions may detect, e.g.,
|
|
89
|
+
* availability of SSE2 intrinsics.
|
|
90
|
+
*
|
|
91
|
+
* If 'unsigned long' is a 64-bit type, then we assume that 64-bit types
|
|
92
|
+
* are efficient. Otherwise, we rely on macros that depend on compiler,
|
|
93
|
+
* OS and architecture. In any case, failure to detect the architecture
|
|
94
|
+
* as 64-bit means that the 32-bit code will be used, and that code
|
|
95
|
+
* works also on 64-bit architectures (the 64-bit code may simply be
|
|
96
|
+
* more efficient).
|
|
97
|
+
*
|
|
98
|
+
* The test on 'unsigned long' should already catch most cases, the one
|
|
99
|
+
* notable exception being Windows code where 'unsigned long' is kept to
|
|
100
|
+
* 32-bit for compatibility with all the legacy code that liberally uses
|
|
101
|
+
* the 'DWORD' type for 32-bit values.
|
|
102
|
+
*
|
|
103
|
+
* Macro names are taken from: http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
|
|
104
|
+
*/
|
|
105
|
+
#ifndef BR_64
|
|
106
|
+
#if ((ULONG_MAX >> 31) >> 31) == 3
|
|
107
|
+
#define BR_64 1
|
|
108
|
+
#elif defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
|
|
109
|
+
#define BR_64 1
|
|
110
|
+
#elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \
|
|
111
|
+
|| defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
|
|
112
|
+
#define BR_64 1
|
|
113
|
+
#elif defined(__sparc64__)
|
|
114
|
+
#define BR_64 1
|
|
115
|
+
#elif defined(__x86_64__) || defined(_M_X64)
|
|
116
|
+
#define BR_64 1
|
|
117
|
+
#endif
|
|
118
|
+
#endif
|
|
119
|
+
|
|
120
|
+
/*
|
|
121
|
+
* Set BR_LOMUL on platforms where it makes sense.
|
|
122
|
+
*/
|
|
123
|
+
#ifndef BR_LOMUL
|
|
124
|
+
#if BR_ARMEL_CORTEXM_GCC
|
|
125
|
+
#define BR_LOMUL 1
|
|
126
|
+
#endif
|
|
127
|
+
#endif
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* Architecture detection.
|
|
131
|
+
*/
|
|
132
|
+
#ifndef BR_i386
|
|
133
|
+
#if __i386__ || _M_IX86
|
|
134
|
+
#define BR_i386 1
|
|
135
|
+
#endif
|
|
136
|
+
#endif
|
|
137
|
+
|
|
138
|
+
#ifndef BR_amd64
|
|
139
|
+
#if __x86_64__ || _M_X64
|
|
140
|
+
#define BR_amd64 1
|
|
141
|
+
#endif
|
|
142
|
+
#endif
|
|
143
|
+
|
|
144
|
+
/*
|
|
145
|
+
* Compiler brand and version.
|
|
146
|
+
*
|
|
147
|
+
* Implementations that use intrinsics need to detect the compiler type
|
|
148
|
+
* and version because some specific actions may be needed to activate
|
|
149
|
+
* the corresponding opcodes, both for header inclusion, and when using
|
|
150
|
+
* them in a function.
|
|
151
|
+
*
|
|
152
|
+
* BR_GCC, BR_CLANG and BR_MSC will be set to 1 for, respectively, GCC,
|
|
153
|
+
* Clang and MS Visual C. For each of them, sub-macros will be defined
|
|
154
|
+
* for versions; each sub-macro is set whenever the compiler version is
|
|
155
|
+
* at least as recent as the one corresponding to the macro.
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
/*
|
|
159
|
+
* GCC thresholds are on versions 4.4 to 4.9 and 5.0.
|
|
160
|
+
*/
|
|
161
|
+
#ifndef BR_GCC
|
|
162
|
+
#if __GNUC__ && !__clang__
|
|
163
|
+
#define BR_GCC 1
|
|
164
|
+
|
|
165
|
+
#if __GNUC__ > 4
|
|
166
|
+
#define BR_GCC_5_0 1
|
|
167
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
|
|
168
|
+
#define BR_GCC_4_9 1
|
|
169
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 8
|
|
170
|
+
#define BR_GCC_4_8 1
|
|
171
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 7
|
|
172
|
+
#define BR_GCC_4_7 1
|
|
173
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
|
|
174
|
+
#define BR_GCC_4_6 1
|
|
175
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 5
|
|
176
|
+
#define BR_GCC_4_5 1
|
|
177
|
+
#elif __GNUC__ == 4 && __GNUC_MINOR__ >= 4
|
|
178
|
+
#define BR_GCC_4_4 1
|
|
179
|
+
#endif
|
|
180
|
+
|
|
181
|
+
#if BR_GCC_5_0
|
|
182
|
+
#define BR_GCC_4_9 1
|
|
183
|
+
#endif
|
|
184
|
+
#if BR_GCC_4_9
|
|
185
|
+
#define BR_GCC_4_8 1
|
|
186
|
+
#endif
|
|
187
|
+
#if BR_GCC_4_8
|
|
188
|
+
#define BR_GCC_4_7 1
|
|
189
|
+
#endif
|
|
190
|
+
#if BR_GCC_4_7
|
|
191
|
+
#define BR_GCC_4_6 1
|
|
192
|
+
#endif
|
|
193
|
+
#if BR_GCC_4_6
|
|
194
|
+
#define BR_GCC_4_5 1
|
|
195
|
+
#endif
|
|
196
|
+
#if BR_GCC_4_5
|
|
197
|
+
#define BR_GCC_4_4 1
|
|
198
|
+
#endif
|
|
199
|
+
|
|
200
|
+
#endif
|
|
201
|
+
#endif
|
|
202
|
+
|
|
203
|
+
/*
|
|
204
|
+
* Clang thresholds are on versions 3.7.0 and 3.8.0.
|
|
205
|
+
*/
|
|
206
|
+
#ifndef BR_CLANG
|
|
207
|
+
#if __clang__
|
|
208
|
+
#define BR_CLANG 1
|
|
209
|
+
|
|
210
|
+
#if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 8)
|
|
211
|
+
#define BR_CLANG_3_8 1
|
|
212
|
+
#elif __clang_major__ == 3 && __clang_minor__ >= 7
|
|
213
|
+
#define BR_CLANG_3_7 1
|
|
214
|
+
#endif
|
|
215
|
+
|
|
216
|
+
#if BR_CLANG_3_8
|
|
217
|
+
#define BR_CLANG_3_7 1
|
|
218
|
+
#endif
|
|
219
|
+
|
|
220
|
+
#endif
|
|
221
|
+
#endif
|
|
222
|
+
|
|
223
|
+
/*
|
|
224
|
+
* MS Visual C thresholds are on Visual Studio 2005 to 2015.
|
|
225
|
+
*/
|
|
226
|
+
#ifndef BR_MSC
|
|
227
|
+
#if _MSC_VER
|
|
228
|
+
#define BR_MSC 1
|
|
229
|
+
|
|
230
|
+
#if _MSC_VER >= 1900
|
|
231
|
+
#define BR_MSC_2015 1
|
|
232
|
+
#elif _MSC_VER >= 1800
|
|
233
|
+
#define BR_MSC_2013 1
|
|
234
|
+
#elif _MSC_VER >= 1700
|
|
235
|
+
#define BR_MSC_2012 1
|
|
236
|
+
#elif _MSC_VER >= 1600
|
|
237
|
+
#define BR_MSC_2010 1
|
|
238
|
+
#elif _MSC_VER >= 1500
|
|
239
|
+
#define BR_MSC_2008 1
|
|
240
|
+
#elif _MSC_VER >= 1400
|
|
241
|
+
#define BR_MSC_2005 1
|
|
242
|
+
#endif
|
|
243
|
+
|
|
244
|
+
#if BR_MSC_2015
|
|
245
|
+
#define BR_MSC_2013 1
|
|
246
|
+
#endif
|
|
247
|
+
#if BR_MSC_2013
|
|
248
|
+
#define BR_MSC_2012 1
|
|
249
|
+
#endif
|
|
250
|
+
#if BR_MSC_2012
|
|
251
|
+
#define BR_MSC_2010 1
|
|
252
|
+
#endif
|
|
253
|
+
#if BR_MSC_2010
|
|
254
|
+
#define BR_MSC_2008 1
|
|
255
|
+
#endif
|
|
256
|
+
#if BR_MSC_2008
|
|
257
|
+
#define BR_MSC_2005 1
|
|
258
|
+
#endif
|
|
259
|
+
|
|
260
|
+
#endif
|
|
261
|
+
#endif
|
|
262
|
+
|
|
263
|
+
/*
|
|
264
|
+
* GCC 4.4+ and Clang 3.7+ allow tagging specific functions with a
|
|
265
|
+
* 'target' attribute that activates support for specific opcodes.
|
|
266
|
+
*/
|
|
267
|
+
#if BR_GCC_4_4 || BR_CLANG_3_7
|
|
268
|
+
#define BR_TARGET(x) __attribute__((target(x)))
|
|
269
|
+
#else
|
|
270
|
+
#define BR_TARGET(x)
|
|
271
|
+
#endif
|
|
272
|
+
|
|
273
|
+
/*
|
|
274
|
+
* AES-NI intrinsics are available on x86 (32-bit and 64-bit) with
|
|
275
|
+
* GCC 4.8+, Clang 3.7+ and MSC 2012+.
|
|
276
|
+
*/
|
|
277
|
+
#ifndef BR_AES_X86NI
|
|
278
|
+
#if (BR_i386 || BR_amd64) && (BR_GCC_4_8 || BR_CLANG_3_7 || BR_MSC_2012)
|
|
279
|
+
#define BR_AES_X86NI 1
|
|
280
|
+
#endif
|
|
281
|
+
#endif
|
|
282
|
+
|
|
283
|
+
/*
|
|
284
|
+
* SSE2 intrinsics are available on x86 (32-bit and 64-bit) with
|
|
285
|
+
* GCC 4.4+, Clang 3.7+ and MSC 2005+.
|
|
286
|
+
*/
|
|
287
|
+
#ifndef BR_SSE2
|
|
288
|
+
#if (BR_i386 || BR_amd64) && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
|
|
289
|
+
#define BR_SSE2 1
|
|
290
|
+
#endif
|
|
291
|
+
#endif
|
|
292
|
+
|
|
293
|
+
/*
|
|
294
|
+
* RDRAND intrinsics are available on x86 (32-bit and 64-bit) with
|
|
295
|
+
* GCC 4.6+, Clang 3.7+ and MSC 2012+.
|
|
296
|
+
*/
|
|
297
|
+
#ifndef BR_RDRAND
|
|
298
|
+
#if (BR_i386 || BR_amd64) && (BR_GCC_4_6 || BR_CLANG_3_7 || BR_MSC_2012)
|
|
299
|
+
#define BR_RDRAND 1
|
|
300
|
+
#endif
|
|
301
|
+
#endif
|
|
302
|
+
|
|
303
|
+
/*
|
|
304
|
+
* Determine type of OS for random number generation. Macro names and
|
|
305
|
+
* values are documented on:
|
|
306
|
+
* https://sourceforge.net/p/predef/wiki/OperatingSystems/
|
|
307
|
+
*
|
|
308
|
+
* TODO: enrich the list of detected system. Also add detection for
|
|
309
|
+
* alternate system calls like getentropy(), which are usually
|
|
310
|
+
* preferable when available.
|
|
311
|
+
*/
|
|
312
|
+
|
|
313
|
+
#ifndef BR_USE_URANDOM
|
|
314
|
+
#if defined _AIX \
|
|
315
|
+
|| defined __ANDROID__ \
|
|
316
|
+
|| defined __FreeBSD__ \
|
|
317
|
+
|| defined __NetBSD__ \
|
|
318
|
+
|| defined __OpenBSD__ \
|
|
319
|
+
|| defined __DragonFly__ \
|
|
320
|
+
|| defined __linux__ \
|
|
321
|
+
|| (defined __sun && (defined __SVR4 || defined __svr4__)) \
|
|
322
|
+
|| (defined __APPLE__ && defined __MACH__)
|
|
323
|
+
#define BR_USE_URANDOM 1
|
|
324
|
+
#endif
|
|
325
|
+
#endif
|
|
326
|
+
|
|
327
|
+
#ifndef BR_USE_WIN32_RAND
|
|
328
|
+
#if defined _WIN32 || defined _WIN64
|
|
329
|
+
#define BR_USE_WIN32_RAND 1
|
|
330
|
+
#endif
|
|
331
|
+
#endif
|
|
332
|
+
|
|
333
|
+
/*
|
|
334
|
+
* POWER8 crypto support. We rely on compiler macros for the
|
|
335
|
+
* architecture, since we do not have a reliable, simple way to detect
|
|
336
|
+
* the required support at runtime (we could try running an opcode, and
|
|
337
|
+
* trapping the exception or signal on illegal instruction, but this
|
|
338
|
+
* induces some non-trivial OS dependencies that we would prefer to
|
|
339
|
+
* avoid if possible).
|
|
340
|
+
*/
|
|
341
|
+
#ifndef BR_POWER8
|
|
342
|
+
#if __GNUC__ && ((_ARCH_PWR8 || _ARCH_PPC) && __CRYPTO__)
|
|
343
|
+
#define BR_POWER8 1
|
|
344
|
+
#endif
|
|
345
|
+
#endif
|
|
346
|
+
|
|
347
|
+
/*
|
|
348
|
+
* Detect endinanness on POWER8.
|
|
349
|
+
*/
|
|
350
|
+
#if BR_POWER8
|
|
351
|
+
#if defined BR_POWER8_LE
|
|
352
|
+
#undef BR_POWER8_BE
|
|
353
|
+
#if BR_POWER8_LE
|
|
354
|
+
#define BR_POWER8_BE 0
|
|
355
|
+
#else
|
|
356
|
+
#define BR_POWER8_BE 1
|
|
357
|
+
#endif
|
|
358
|
+
#elif defined BR_POWER8_BE
|
|
359
|
+
#undef BR_POWER8_LE
|
|
360
|
+
#if BR_POWER8_BE
|
|
361
|
+
#define BR_POWER8_LE 0
|
|
362
|
+
#else
|
|
363
|
+
#define BR_POWER8_LE 1
|
|
364
|
+
#endif
|
|
365
|
+
#else
|
|
366
|
+
#if __LITTLE_ENDIAN__
|
|
367
|
+
#define BR_POWER8_LE 1
|
|
368
|
+
#define BR_POWER8_BE 0
|
|
369
|
+
#else
|
|
370
|
+
#define BR_POWER8_LE 0
|
|
371
|
+
#define BR_POWER8_BE 1
|
|
372
|
+
#endif
|
|
373
|
+
#endif
|
|
374
|
+
#endif
|
|
375
|
+
|
|
376
|
+
/*
|
|
377
|
+
* Detect support for 128-bit integers.
|
|
378
|
+
*/
|
|
379
|
+
#if !defined BR_INT128 && !defined BR_UMUL128
|
|
380
|
+
#ifdef __SIZEOF_INT128__
|
|
381
|
+
#define BR_INT128 1
|
|
382
|
+
#elif _M_X64
|
|
383
|
+
#define BR_UMUL128 1
|
|
384
|
+
#endif
|
|
385
|
+
#endif
|
|
386
|
+
|
|
387
|
+
/*
|
|
388
|
+
* Detect support for unaligned accesses with known endianness.
|
|
389
|
+
*
|
|
390
|
+
* x86 (both 32-bit and 64-bit) is little-endian and allows unaligned
|
|
391
|
+
* accesses.
|
|
392
|
+
*
|
|
393
|
+
* POWER/PowerPC allows unaligned accesses when big-endian. POWER8 and
|
|
394
|
+
* later also allow unaligned accesses when little-endian.
|
|
395
|
+
*/
|
|
396
|
+
#if !defined BR_LE_UNALIGNED && !defined BR_BE_UNALIGNED
|
|
397
|
+
|
|
398
|
+
#if __i386 || __i386__ || __x86_64__ || _M_IX86 || _M_X64
|
|
399
|
+
#define BR_LE_UNALIGNED 1
|
|
400
|
+
#elif BR_POWER8_BE
|
|
401
|
+
#define BR_BE_UNALIGNED 1
|
|
402
|
+
#elif BR_POWER8_LE
|
|
403
|
+
#define BR_LE_UNALIGNED 1
|
|
404
|
+
#elif (__powerpc__ || __powerpc64__ || _M_PPC || _ARCH_PPC || _ARCH_PPC64) \
|
|
405
|
+
&& __BIG_ENDIAN__
|
|
406
|
+
#define BR_BE_UNALIGNED 1
|
|
407
|
+
#endif
|
|
408
|
+
|
|
409
|
+
#endif
|
|
410
|
+
|
|
411
|
+
/*
|
|
412
|
+
* Detect support for an OS-provided time source.
|
|
413
|
+
*/
|
|
414
|
+
|
|
415
|
+
#ifndef BR_USE_UNIX_TIME
|
|
416
|
+
#if defined __unix__ || defined __linux__ \
|
|
417
|
+
|| defined _POSIX_SOURCE || defined _POSIX_C_SOURCE \
|
|
418
|
+
|| (defined __APPLE__ && defined __MACH__)
|
|
419
|
+
#define BR_USE_UNIX_TIME 1
|
|
420
|
+
#endif
|
|
421
|
+
#endif
|
|
422
|
+
|
|
423
|
+
#ifndef BR_USE_WIN32_TIME
|
|
424
|
+
#if defined _WIN32 || defined _WIN64
|
|
425
|
+
#define BR_USE_WIN32_TIME 1
|
|
426
|
+
#endif
|
|
427
|
+
#endif
|
|
428
|
+
|
|
429
|
+
/* ==================================================================== */
|
|
430
|
+
/*
|
|
431
|
+
* Encoding/decoding functions.
|
|
432
|
+
*
|
|
433
|
+
* 32-bit and 64-bit decoding, both little-endian and big-endian, is
|
|
434
|
+
* implemented with the inline functions below.
|
|
435
|
+
*
|
|
436
|
+
* When allowed by some compile-time options (autodetected or provided),
|
|
437
|
+
* optimised code is used, to perform direct memory access when the
|
|
438
|
+
* underlying architecture supports it, both for endianness and
|
|
439
|
+
* alignment. This, however, may trigger strict aliasing issues; the
|
|
440
|
+
* code below uses unions to perform (supposedly) safe type punning.
|
|
441
|
+
* Since the C aliasing rules are relatively complex and were amended,
|
|
442
|
+
* or at least re-explained with different phrasing, in all successive
|
|
443
|
+
* versions of the C standard, it is always a bit risky to bet that any
|
|
444
|
+
* specific version of a C compiler got it right, for some notion of
|
|
445
|
+
* "right".
|
|
446
|
+
*/
|
|
447
|
+
|
|
448
|
+
typedef union {
|
|
449
|
+
uint16_t u;
|
|
450
|
+
unsigned char b[sizeof(uint16_t)];
|
|
451
|
+
} br_union_u16;
|
|
452
|
+
|
|
453
|
+
typedef union {
|
|
454
|
+
uint32_t u;
|
|
455
|
+
unsigned char b[sizeof(uint32_t)];
|
|
456
|
+
} br_union_u32;
|
|
457
|
+
|
|
458
|
+
typedef union {
|
|
459
|
+
uint64_t u;
|
|
460
|
+
unsigned char b[sizeof(uint64_t)];
|
|
461
|
+
} br_union_u64;
|
|
462
|
+
|
|
463
|
+
static inline void
|
|
464
|
+
br_enc16le(void *dst, unsigned x)
|
|
465
|
+
{
|
|
466
|
+
#if BR_LE_UNALIGNED
|
|
467
|
+
((br_union_u16 *)dst)->u = x;
|
|
468
|
+
#else
|
|
469
|
+
unsigned char *buf;
|
|
470
|
+
|
|
471
|
+
buf = dst;
|
|
472
|
+
buf[0] = (unsigned char)x;
|
|
473
|
+
buf[1] = (unsigned char)(x >> 8);
|
|
474
|
+
#endif
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
static inline void
|
|
478
|
+
br_enc16be(void *dst, unsigned x)
|
|
479
|
+
{
|
|
480
|
+
#if BR_BE_UNALIGNED
|
|
481
|
+
((br_union_u16 *)dst)->u = x;
|
|
482
|
+
#else
|
|
483
|
+
unsigned char *buf;
|
|
484
|
+
|
|
485
|
+
buf = dst;
|
|
486
|
+
buf[0] = (unsigned char)(x >> 8);
|
|
487
|
+
buf[1] = (unsigned char)x;
|
|
488
|
+
#endif
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
static inline unsigned
|
|
492
|
+
br_dec16le(const void *src)
|
|
493
|
+
{
|
|
494
|
+
#if BR_LE_UNALIGNED
|
|
495
|
+
return ((const br_union_u16 *)src)->u;
|
|
496
|
+
#else
|
|
497
|
+
const unsigned char *buf;
|
|
498
|
+
|
|
499
|
+
buf = src;
|
|
500
|
+
return (unsigned)buf[0] | ((unsigned)buf[1] << 8);
|
|
501
|
+
#endif
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
static inline unsigned
|
|
505
|
+
br_dec16be(const void *src)
|
|
506
|
+
{
|
|
507
|
+
#if BR_BE_UNALIGNED
|
|
508
|
+
return ((const br_union_u16 *)src)->u;
|
|
509
|
+
#else
|
|
510
|
+
const unsigned char *buf;
|
|
511
|
+
|
|
512
|
+
buf = src;
|
|
513
|
+
return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
|
|
514
|
+
#endif
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
static inline void
|
|
518
|
+
br_enc32le(void *dst, uint32_t x)
|
|
519
|
+
{
|
|
520
|
+
#if BR_LE_UNALIGNED
|
|
521
|
+
((br_union_u32 *)dst)->u = x;
|
|
522
|
+
#else
|
|
523
|
+
unsigned char *buf;
|
|
524
|
+
|
|
525
|
+
buf = dst;
|
|
526
|
+
buf[0] = (unsigned char)x;
|
|
527
|
+
buf[1] = (unsigned char)(x >> 8);
|
|
528
|
+
buf[2] = (unsigned char)(x >> 16);
|
|
529
|
+
buf[3] = (unsigned char)(x >> 24);
|
|
530
|
+
#endif
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
static inline void
|
|
534
|
+
br_enc32be(void *dst, uint32_t x)
|
|
535
|
+
{
|
|
536
|
+
#if BR_BE_UNALIGNED
|
|
537
|
+
((br_union_u32 *)dst)->u = x;
|
|
538
|
+
#else
|
|
539
|
+
unsigned char *buf;
|
|
540
|
+
|
|
541
|
+
buf = dst;
|
|
542
|
+
buf[0] = (unsigned char)(x >> 24);
|
|
543
|
+
buf[1] = (unsigned char)(x >> 16);
|
|
544
|
+
buf[2] = (unsigned char)(x >> 8);
|
|
545
|
+
buf[3] = (unsigned char)x;
|
|
546
|
+
#endif
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
static inline uint32_t
|
|
550
|
+
br_dec32le(const void *src)
|
|
551
|
+
{
|
|
552
|
+
#if BR_LE_UNALIGNED
|
|
553
|
+
return ((const br_union_u32 *)src)->u;
|
|
554
|
+
#else
|
|
555
|
+
const unsigned char *buf;
|
|
556
|
+
|
|
557
|
+
buf = src;
|
|
558
|
+
return (uint32_t)buf[0]
|
|
559
|
+
| ((uint32_t)buf[1] << 8)
|
|
560
|
+
| ((uint32_t)buf[2] << 16)
|
|
561
|
+
| ((uint32_t)buf[3] << 24);
|
|
562
|
+
#endif
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
static inline uint32_t
|
|
566
|
+
br_dec32be(const void *src)
|
|
567
|
+
{
|
|
568
|
+
#if BR_BE_UNALIGNED
|
|
569
|
+
return ((const br_union_u32 *)src)->u;
|
|
570
|
+
#else
|
|
571
|
+
const unsigned char *buf;
|
|
572
|
+
|
|
573
|
+
buf = src;
|
|
574
|
+
return ((uint32_t)buf[0] << 24)
|
|
575
|
+
| ((uint32_t)buf[1] << 16)
|
|
576
|
+
| ((uint32_t)buf[2] << 8)
|
|
577
|
+
| (uint32_t)buf[3];
|
|
578
|
+
#endif
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
static inline void
|
|
582
|
+
br_enc64le(void *dst, uint64_t x)
|
|
583
|
+
{
|
|
584
|
+
#if BR_LE_UNALIGNED
|
|
585
|
+
((br_union_u64 *)dst)->u = x;
|
|
586
|
+
#else
|
|
587
|
+
unsigned char *buf;
|
|
588
|
+
|
|
589
|
+
buf = dst;
|
|
590
|
+
br_enc32le(buf, (uint32_t)x);
|
|
591
|
+
br_enc32le(buf + 4, (uint32_t)(x >> 32));
|
|
592
|
+
#endif
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
static inline void
|
|
596
|
+
br_enc64be(void *dst, uint64_t x)
|
|
597
|
+
{
|
|
598
|
+
#if BR_BE_UNALIGNED
|
|
599
|
+
((br_union_u64 *)dst)->u = x;
|
|
600
|
+
#else
|
|
601
|
+
unsigned char *buf;
|
|
602
|
+
|
|
603
|
+
buf = dst;
|
|
604
|
+
br_enc32be(buf, (uint32_t)(x >> 32));
|
|
605
|
+
br_enc32be(buf + 4, (uint32_t)x);
|
|
606
|
+
#endif
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
static inline uint64_t
|
|
610
|
+
br_dec64le(const void *src)
|
|
611
|
+
{
|
|
612
|
+
#if BR_LE_UNALIGNED
|
|
613
|
+
return ((const br_union_u64 *)src)->u;
|
|
614
|
+
#else
|
|
615
|
+
const unsigned char *buf;
|
|
616
|
+
|
|
617
|
+
buf = src;
|
|
618
|
+
return (uint64_t)br_dec32le(buf)
|
|
619
|
+
| ((uint64_t)br_dec32le(buf + 4) << 32);
|
|
620
|
+
#endif
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
static inline uint64_t
|
|
624
|
+
br_dec64be(const void *src)
|
|
625
|
+
{
|
|
626
|
+
#if BR_BE_UNALIGNED
|
|
627
|
+
return ((const br_union_u64 *)src)->u;
|
|
628
|
+
#else
|
|
629
|
+
const unsigned char *buf;
|
|
630
|
+
|
|
631
|
+
buf = src;
|
|
632
|
+
return ((uint64_t)br_dec32be(buf) << 32)
|
|
633
|
+
| (uint64_t)br_dec32be(buf + 4);
|
|
634
|
+
#endif
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/*
|
|
638
|
+
* Range decoding and encoding (for several successive values).
|
|
639
|
+
*/
|
|
640
|
+
void br_range_dec16le(uint16_t *v, size_t num, const void *src);
|
|
641
|
+
void br_range_dec16be(uint16_t *v, size_t num, const void *src);
|
|
642
|
+
void br_range_enc16le(void *dst, const uint16_t *v, size_t num);
|
|
643
|
+
void br_range_enc16be(void *dst, const uint16_t *v, size_t num);
|
|
644
|
+
|
|
645
|
+
void br_range_dec32le(uint32_t *v, size_t num, const void *src);
|
|
646
|
+
void br_range_dec32be(uint32_t *v, size_t num, const void *src);
|
|
647
|
+
void br_range_enc32le(void *dst, const uint32_t *v, size_t num);
|
|
648
|
+
void br_range_enc32be(void *dst, const uint32_t *v, size_t num);
|
|
649
|
+
|
|
650
|
+
void br_range_dec64le(uint64_t *v, size_t num, const void *src);
|
|
651
|
+
void br_range_dec64be(uint64_t *v, size_t num, const void *src);
|
|
652
|
+
void br_range_enc64le(void *dst, const uint64_t *v, size_t num);
|
|
653
|
+
void br_range_enc64be(void *dst, const uint64_t *v, size_t num);
|
|
654
|
+
|
|
655
|
+
/*
|
|
656
|
+
* Byte-swap a 32-bit integer.
|
|
657
|
+
*/
|
|
658
|
+
static inline uint32_t
|
|
659
|
+
br_swap32(uint32_t x)
|
|
660
|
+
{
|
|
661
|
+
x = ((x & (uint32_t)0x00FF00FF) << 8)
|
|
662
|
+
| ((x >> 8) & (uint32_t)0x00FF00FF);
|
|
663
|
+
return (x << 16) | (x >> 16);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/* ==================================================================== */
|
|
667
|
+
/*
|
|
668
|
+
* Support code for hash functions.
|
|
669
|
+
*/
|
|
670
|
+
|
|
671
|
+
/*
|
|
672
|
+
* IV for MD5, SHA-1, SHA-224 and SHA-256.
|
|
673
|
+
*/
|
|
674
|
+
extern const uint32_t br_md5_IV[];
|
|
675
|
+
extern const uint32_t br_sha1_IV[];
|
|
676
|
+
extern const uint32_t br_sha224_IV[];
|
|
677
|
+
extern const uint32_t br_sha256_IV[];
|
|
678
|
+
|
|
679
|
+
/*
|
|
680
|
+
* Round functions for MD5, SHA-1, SHA-224 and SHA-256 (SHA-224 and
|
|
681
|
+
* SHA-256 use the same round function).
|
|
682
|
+
*/
|
|
683
|
+
void br_md5_round(const unsigned char *buf, uint32_t *val);
|
|
684
|
+
void br_sha1_round(const unsigned char *buf, uint32_t *val);
|
|
685
|
+
void br_sha2small_round(const unsigned char *buf, uint32_t *val);
|
|
686
|
+
|
|
687
|
+
/*
|
|
688
|
+
* The core function for the TLS PRF. It computes
|
|
689
|
+
* P_hash(secret, label + seed), and XORs the result into the dst buffer.
|
|
690
|
+
*/
|
|
691
|
+
void br_tls_phash(void *dst, size_t len,
|
|
692
|
+
const br_hash_class *dig,
|
|
693
|
+
const void *secret, size_t secret_len, const char *label,
|
|
694
|
+
size_t seed_num, const br_tls_prf_seed_chunk *seed);
|
|
695
|
+
|
|
696
|
+
/*
|
|
697
|
+
* Copy all configured hash implementations from a multihash context
|
|
698
|
+
* to another.
|
|
699
|
+
*/
|
|
700
|
+
static inline void
|
|
701
|
+
br_multihash_copyimpl(br_multihash_context *dst,
|
|
702
|
+
const br_multihash_context *src)
|
|
703
|
+
{
|
|
704
|
+
memcpy((void *)dst->impl, src->impl, sizeof src->impl);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/* ==================================================================== */
|
|
708
|
+
/*
|
|
709
|
+
* Constant-time primitives. These functions manipulate 32-bit values in
|
|
710
|
+
* order to provide constant-time comparisons and multiplexers.
|
|
711
|
+
*
|
|
712
|
+
* Boolean values (the "ctl" bits) MUST have value 0 or 1.
|
|
713
|
+
*
|
|
714
|
+
* Implementation notes:
|
|
715
|
+
* =====================
|
|
716
|
+
*
|
|
717
|
+
* The uintN_t types are unsigned and with width exactly N bits; the C
|
|
718
|
+
* standard guarantees that computations are performed modulo 2^N, and
|
|
719
|
+
* there can be no overflow. Negation (unary '-') works on unsigned types
|
|
720
|
+
* as well.
|
|
721
|
+
*
|
|
722
|
+
* The intN_t types are guaranteed to have width exactly N bits, with no
|
|
723
|
+
* padding bit, and using two's complement representation. Casting
|
|
724
|
+
* intN_t to uintN_t really is conversion modulo 2^N. Beware that intN_t
|
|
725
|
+
* types, being signed, trigger implementation-defined behaviour on
|
|
726
|
+
* overflow (including raising some signal): with GCC, while modular
|
|
727
|
+
* arithmetics are usually applied, the optimizer may assume that
|
|
728
|
+
* overflows don't occur (unless the -fwrapv command-line option is
|
|
729
|
+
* added); Clang has the additional -ftrapv option to explicitly trap on
|
|
730
|
+
* integer overflow or underflow.
|
|
731
|
+
*/
|
|
732
|
+
|
|
733
|
+
/*
|
|
734
|
+
* Negate a boolean.
|
|
735
|
+
*/
|
|
736
|
+
static inline uint32_t
|
|
737
|
+
NOT(uint32_t ctl)
|
|
738
|
+
{
|
|
739
|
+
return ctl ^ 1;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/*
|
|
743
|
+
* Multiplexer: returns x if ctl == 1, y if ctl == 0.
|
|
744
|
+
*/
|
|
745
|
+
static inline uint32_t
|
|
746
|
+
MUX(uint32_t ctl, uint32_t x, uint32_t y)
|
|
747
|
+
{
|
|
748
|
+
return y ^ (-ctl & (x ^ y));
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/*
|
|
752
|
+
* Equality check: returns 1 if x == y, 0 otherwise.
|
|
753
|
+
*/
|
|
754
|
+
static inline uint32_t
|
|
755
|
+
EQ(uint32_t x, uint32_t y)
|
|
756
|
+
{
|
|
757
|
+
uint32_t q;
|
|
758
|
+
|
|
759
|
+
q = x ^ y;
|
|
760
|
+
return NOT((q | -q) >> 31);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/*
|
|
764
|
+
* Inequality check: returns 1 if x != y, 0 otherwise.
|
|
765
|
+
*/
|
|
766
|
+
static inline uint32_t
|
|
767
|
+
NEQ(uint32_t x, uint32_t y)
|
|
768
|
+
{
|
|
769
|
+
uint32_t q;
|
|
770
|
+
|
|
771
|
+
q = x ^ y;
|
|
772
|
+
return (q | -q) >> 31;
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
/*
|
|
776
|
+
* Comparison: returns 1 if x > y, 0 otherwise.
|
|
777
|
+
*/
|
|
778
|
+
static inline uint32_t
|
|
779
|
+
GT(uint32_t x, uint32_t y)
|
|
780
|
+
{
|
|
781
|
+
/*
|
|
782
|
+
* If both x < 2^31 and x < 2^31, then y-x will have its high
|
|
783
|
+
* bit set if x > y, cleared otherwise.
|
|
784
|
+
*
|
|
785
|
+
* If either x >= 2^31 or y >= 2^31 (but not both), then the
|
|
786
|
+
* result is the high bit of x.
|
|
787
|
+
*
|
|
788
|
+
* If both x >= 2^31 and y >= 2^31, then we can virtually
|
|
789
|
+
* subtract 2^31 from both, and we are back to the first case.
|
|
790
|
+
* Since (y-2^31)-(x-2^31) = y-x, the subtraction is already
|
|
791
|
+
* fine.
|
|
792
|
+
*/
|
|
793
|
+
uint32_t z;
|
|
794
|
+
|
|
795
|
+
z = y - x;
|
|
796
|
+
return (z ^ ((x ^ y) & (x ^ z))) >> 31;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
/*
|
|
800
|
+
* Other comparisons (greater-or-equal, lower-than, lower-or-equal).
|
|
801
|
+
*/
|
|
802
|
+
#define GE(x, y) NOT(GT(y, x))
|
|
803
|
+
#define LT(x, y) GT(y, x)
|
|
804
|
+
#define LE(x, y) NOT(GT(x, y))
|
|
805
|
+
|
|
806
|
+
/*
|
|
807
|
+
* General comparison: returned value is -1, 0 or 1, depending on
|
|
808
|
+
* whether x is lower than, equal to, or greater than y.
|
|
809
|
+
*/
|
|
810
|
+
static inline int32_t
|
|
811
|
+
CMP(uint32_t x, uint32_t y)
|
|
812
|
+
{
|
|
813
|
+
return (int32_t)GT(x, y) | -(int32_t)GT(y, x);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/*
|
|
817
|
+
* Returns 1 if x == 0, 0 otherwise. Take care that the operand is signed.
|
|
818
|
+
*/
|
|
819
|
+
static inline uint32_t
|
|
820
|
+
EQ0(int32_t x)
|
|
821
|
+
{
|
|
822
|
+
uint32_t q;
|
|
823
|
+
|
|
824
|
+
q = (uint32_t)x;
|
|
825
|
+
return ~(q | -q) >> 31;
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
/*
|
|
829
|
+
* Returns 1 if x > 0, 0 otherwise. Take care that the operand is signed.
|
|
830
|
+
*/
|
|
831
|
+
static inline uint32_t
|
|
832
|
+
GT0(int32_t x)
|
|
833
|
+
{
|
|
834
|
+
/*
|
|
835
|
+
* High bit of -x is 0 if x == 0, but 1 if x > 0.
|
|
836
|
+
*/
|
|
837
|
+
uint32_t q;
|
|
838
|
+
|
|
839
|
+
q = (uint32_t)x;
|
|
840
|
+
return (~q & -q) >> 31;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
/*
|
|
844
|
+
* Returns 1 if x >= 0, 0 otherwise. Take care that the operand is signed.
|
|
845
|
+
*/
|
|
846
|
+
static inline uint32_t
|
|
847
|
+
GE0(int32_t x)
|
|
848
|
+
{
|
|
849
|
+
return ~(uint32_t)x >> 31;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/*
|
|
853
|
+
* Returns 1 if x < 0, 0 otherwise. Take care that the operand is signed.
|
|
854
|
+
*/
|
|
855
|
+
static inline uint32_t
|
|
856
|
+
LT0(int32_t x)
|
|
857
|
+
{
|
|
858
|
+
return (uint32_t)x >> 31;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/*
|
|
862
|
+
* Returns 1 if x <= 0, 0 otherwise. Take care that the operand is signed.
|
|
863
|
+
*/
|
|
864
|
+
static inline uint32_t
|
|
865
|
+
LE0(int32_t x)
|
|
866
|
+
{
|
|
867
|
+
uint32_t q;
|
|
868
|
+
|
|
869
|
+
/*
|
|
870
|
+
* ~-x has its high bit set if and only if -x is nonnegative (as
|
|
871
|
+
* a signed int), i.e. x is in the -(2^31-1) to 0 range. We must
|
|
872
|
+
* do an OR with x itself to account for x = -2^31.
|
|
873
|
+
*/
|
|
874
|
+
q = (uint32_t)x;
|
|
875
|
+
return (q | ~-q) >> 31;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/*
|
|
879
|
+
* Conditional copy: src[] is copied into dst[] if and only if ctl is 1.
|
|
880
|
+
* dst[] and src[] may overlap completely (but not partially).
|
|
881
|
+
*/
|
|
882
|
+
void br_ccopy(uint32_t ctl, void *dst, const void *src, size_t len);
|
|
883
|
+
|
|
884
|
+
#define CCOPY br_ccopy
|
|
885
|
+
|
|
886
|
+
/*
|
|
887
|
+
* Compute the bit length of a 32-bit integer. Returned value is between 0
|
|
888
|
+
* and 32 (inclusive).
|
|
889
|
+
*/
|
|
890
|
+
static inline uint32_t
|
|
891
|
+
BIT_LENGTH(uint32_t x)
|
|
892
|
+
{
|
|
893
|
+
uint32_t k, c;
|
|
894
|
+
|
|
895
|
+
k = NEQ(x, 0);
|
|
896
|
+
c = GT(x, 0xFFFF); x = MUX(c, x >> 16, x); k += c << 4;
|
|
897
|
+
c = GT(x, 0x00FF); x = MUX(c, x >> 8, x); k += c << 3;
|
|
898
|
+
c = GT(x, 0x000F); x = MUX(c, x >> 4, x); k += c << 2;
|
|
899
|
+
c = GT(x, 0x0003); x = MUX(c, x >> 2, x); k += c << 1;
|
|
900
|
+
k += GT(x, 0x0001);
|
|
901
|
+
return k;
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/*
|
|
905
|
+
* Compute the minimum of x and y.
|
|
906
|
+
*/
|
|
907
|
+
static inline uint32_t
|
|
908
|
+
MIN(uint32_t x, uint32_t y)
|
|
909
|
+
{
|
|
910
|
+
return MUX(GT(x, y), y, x);
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/*
|
|
914
|
+
* Compute the maximum of x and y.
|
|
915
|
+
*/
|
|
916
|
+
static inline uint32_t
|
|
917
|
+
MAX(uint32_t x, uint32_t y)
|
|
918
|
+
{
|
|
919
|
+
return MUX(GT(x, y), x, y);
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
/*
|
|
923
|
+
* Multiply two 32-bit integers, with a 64-bit result. This default
|
|
924
|
+
* implementation assumes that the basic multiplication operator
|
|
925
|
+
* yields constant-time code.
|
|
926
|
+
*/
|
|
927
|
+
#define MUL(x, y) ((uint64_t)(x) * (uint64_t)(y))
|
|
928
|
+
|
|
929
|
+
#if BR_CT_MUL31
|
|
930
|
+
|
|
931
|
+
/*
|
|
932
|
+
* Alternate implementation of MUL31, that will be constant-time on some
|
|
933
|
+
* (old) platforms where the default MUL31 is not. Unfortunately, it is
|
|
934
|
+
* also substantially slower, and yields larger code, on more modern
|
|
935
|
+
* platforms, which is why it is deactivated by default.
|
|
936
|
+
*
|
|
937
|
+
* MUL31_lo() must do some extra work because on some platforms, the
|
|
938
|
+
* _signed_ multiplication may return early if the top bits are 1.
|
|
939
|
+
* Simply truncating (casting) the output of MUL31() would not be
|
|
940
|
+
* sufficient, because the compiler may notice that we keep only the low
|
|
941
|
+
* word, and then replace automatically the unsigned multiplication with
|
|
942
|
+
* a signed multiplication opcode.
|
|
943
|
+
*/
|
|
944
|
+
#define MUL31(x, y) ((uint64_t)((x) | (uint32_t)0x80000000) \
|
|
945
|
+
* (uint64_t)((y) | (uint32_t)0x80000000) \
|
|
946
|
+
- ((uint64_t)(x) << 31) - ((uint64_t)(y) << 31) \
|
|
947
|
+
- ((uint64_t)1 << 62))
|
|
948
|
+
static inline uint32_t
|
|
949
|
+
MUL31_lo(uint32_t x, uint32_t y)
|
|
950
|
+
{
|
|
951
|
+
uint32_t xl, xh;
|
|
952
|
+
uint32_t yl, yh;
|
|
953
|
+
|
|
954
|
+
xl = (x & 0xFFFF) | (uint32_t)0x80000000;
|
|
955
|
+
xh = (x >> 16) | (uint32_t)0x80000000;
|
|
956
|
+
yl = (y & 0xFFFF) | (uint32_t)0x80000000;
|
|
957
|
+
yh = (y >> 16) | (uint32_t)0x80000000;
|
|
958
|
+
return (xl * yl + ((xl * yh + xh * yl) << 16)) & (uint32_t)0x7FFFFFFF;
|
|
959
|
+
}
|
|
960
|
+
|
|
961
|
+
#else
|
|
962
|
+
|
|
963
|
+
/*
|
|
964
|
+
* Multiply two 31-bit integers, with a 62-bit result. This default
|
|
965
|
+
* implementation assumes that the basic multiplication operator
|
|
966
|
+
* yields constant-time code.
|
|
967
|
+
* The MUL31_lo() macro returns only the low 31 bits of the product.
|
|
968
|
+
*/
|
|
969
|
+
#define MUL31(x, y) ((uint64_t)(x) * (uint64_t)(y))
|
|
970
|
+
#define MUL31_lo(x, y) (((uint32_t)(x) * (uint32_t)(y)) & (uint32_t)0x7FFFFFFF)
|
|
971
|
+
|
|
972
|
+
#endif
|
|
973
|
+
|
|
974
|
+
/*
|
|
975
|
+
* Multiply two words together; the sum of the lengths of the two
|
|
976
|
+
* operands must not exceed 31 (for instance, one operand may use 16
|
|
977
|
+
* bits if the other fits on 15). If BR_CT_MUL15 is non-zero, then the
|
|
978
|
+
* macro will contain some extra operations that help in making the
|
|
979
|
+
* operation constant-time on some platforms, where the basic 32-bit
|
|
980
|
+
* multiplication is not constant-time.
|
|
981
|
+
*/
|
|
982
|
+
#if BR_CT_MUL15
|
|
983
|
+
#define MUL15(x, y) (((uint32_t)(x) | (uint32_t)0x80000000) \
|
|
984
|
+
* ((uint32_t)(y) | (uint32_t)0x80000000) \
|
|
985
|
+
& (uint32_t)0x7FFFFFFF)
|
|
986
|
+
#else
|
|
987
|
+
#define MUL15(x, y) ((uint32_t)(x) * (uint32_t)(y))
|
|
988
|
+
#endif
|
|
989
|
+
|
|
990
|
+
/*
|
|
991
|
+
* Arithmetic right shift (sign bit is copied). What happens when
|
|
992
|
+
* right-shifting a negative value is _implementation-defined_, so it
|
|
993
|
+
* does not trigger undefined behaviour, but it is still up to each
|
|
994
|
+
* compiler to define (and document) what it does. Most/all compilers
|
|
995
|
+
* will do an arithmetic shift, the sign bit being used to fill the
|
|
996
|
+
* holes; this is a native operation on the underlying CPU, and it would
|
|
997
|
+
* make little sense for the compiler to do otherwise. GCC explicitly
|
|
998
|
+
* documents that it follows that convention.
|
|
999
|
+
*
|
|
1000
|
+
* Still, if BR_NO_ARITH_SHIFT is defined (and non-zero), then an
|
|
1001
|
+
* alternate version will be used, that does not rely on such
|
|
1002
|
+
* implementation-defined behaviour. Unfortunately, it is also slower
|
|
1003
|
+
* and yields bigger code, which is why it is deactivated by default.
|
|
1004
|
+
*/
|
|
1005
|
+
#if BR_NO_ARITH_SHIFT
|
|
1006
|
+
#define ARSH(x, n) (((uint32_t)(x) >> (n)) \
|
|
1007
|
+
| ((-((uint32_t)(x) >> 31)) << (32 - (n))))
|
|
1008
|
+
#else
|
|
1009
|
+
#define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
|
|
1010
|
+
#endif
|
|
1011
|
+
|
|
1012
|
+
/*
|
|
1013
|
+
* Constant-time division. The dividend hi:lo is divided by the
|
|
1014
|
+
* divisor d; the quotient is returned and the remainder is written
|
|
1015
|
+
* in *r. If hi == d, then the quotient does not fit on 32 bits;
|
|
1016
|
+
* returned value is thus truncated. If hi > d, returned values are
|
|
1017
|
+
* indeterminate.
|
|
1018
|
+
*/
|
|
1019
|
+
uint32_t br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r);
|
|
1020
|
+
|
|
1021
|
+
/*
|
|
1022
|
+
* Wrapper for br_divrem(); the remainder is returned, and the quotient
|
|
1023
|
+
* is discarded.
|
|
1024
|
+
*/
|
|
1025
|
+
static inline uint32_t
|
|
1026
|
+
br_rem(uint32_t hi, uint32_t lo, uint32_t d)
|
|
1027
|
+
{
|
|
1028
|
+
uint32_t r;
|
|
1029
|
+
|
|
1030
|
+
br_divrem(hi, lo, d, &r);
|
|
1031
|
+
return r;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/*
|
|
1035
|
+
* Wrapper for br_divrem(); the quotient is returned, and the remainder
|
|
1036
|
+
* is discarded.
|
|
1037
|
+
*/
|
|
1038
|
+
static inline uint32_t
|
|
1039
|
+
br_div(uint32_t hi, uint32_t lo, uint32_t d)
|
|
1040
|
+
{
|
|
1041
|
+
uint32_t r;
|
|
1042
|
+
|
|
1043
|
+
return br_divrem(hi, lo, d, &r);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/* ==================================================================== */
|
|
1047
|
+
|
|
1048
|
+
/*
|
|
1049
|
+
* Integers 'i32'
|
|
1050
|
+
* --------------
|
|
1051
|
+
*
|
|
1052
|
+
* The 'i32' functions implement computations on big integers using
|
|
1053
|
+
* an internal representation as an array of 32-bit integers. For
|
|
1054
|
+
* an array x[]:
|
|
1055
|
+
* -- x[0] contains the "announced bit length" of the integer
|
|
1056
|
+
* -- x[1], x[2]... contain the value in little-endian order (x[1]
|
|
1057
|
+
* contains the least significant 32 bits)
|
|
1058
|
+
*
|
|
1059
|
+
* Multiplications rely on the elementary 32x32->64 multiplication.
|
|
1060
|
+
*
|
|
1061
|
+
* The announced bit length specifies the number of bits that are
|
|
1062
|
+
* significant in the subsequent 32-bit words. Unused bits in the
|
|
1063
|
+
* last (most significant) word are set to 0; subsequent words are
|
|
1064
|
+
* uninitialized and need not exist at all.
|
|
1065
|
+
*
|
|
1066
|
+
* The execution time and memory access patterns of all computations
|
|
1067
|
+
* depend on the announced bit length, but not on the actual word
|
|
1068
|
+
* values. For modular integers, the announced bit length of any integer
|
|
1069
|
+
* modulo n is equal to the actual bit length of n; thus, computations
|
|
1070
|
+
* on modular integers are "constant-time" (only the modulus length may
|
|
1071
|
+
* leak).
|
|
1072
|
+
*/
|
|
1073
|
+
|
|
1074
|
+
/*
|
|
1075
|
+
* Compute the actual bit length of an integer. The argument x should
|
|
1076
|
+
* point to the first (least significant) value word of the integer.
|
|
1077
|
+
* The len 'xlen' contains the number of 32-bit words to access.
|
|
1078
|
+
*
|
|
1079
|
+
* CT: value or length of x does not leak.
|
|
1080
|
+
*/
|
|
1081
|
+
uint32_t br_i32_bit_length(uint32_t *x, size_t xlen);
|
|
1082
|
+
|
|
1083
|
+
/*
|
|
1084
|
+
* Decode an integer from its big-endian unsigned representation. The
|
|
1085
|
+
* "true" bit length of the integer is computed, but all words of x[]
|
|
1086
|
+
* corresponding to the full 'len' bytes of the source are set.
|
|
1087
|
+
*
|
|
1088
|
+
* CT: value or length of x does not leak.
|
|
1089
|
+
*/
|
|
1090
|
+
void br_i32_decode(uint32_t *x, const void *src, size_t len);
|
|
1091
|
+
|
|
1092
|
+
/*
|
|
1093
|
+
* Decode an integer from its big-endian unsigned representation. The
|
|
1094
|
+
* integer MUST be lower than m[]; the announced bit length written in
|
|
1095
|
+
* x[] will be equal to that of m[]. All 'len' bytes from the source are
|
|
1096
|
+
* read.
|
|
1097
|
+
*
|
|
1098
|
+
* Returned value is 1 if the decode value fits within the modulus, 0
|
|
1099
|
+
* otherwise. In the latter case, the x[] buffer will be set to 0 (but
|
|
1100
|
+
* still with the announced bit length of m[]).
|
|
1101
|
+
*
|
|
1102
|
+
* CT: value or length of x does not leak. Memory access pattern depends
|
|
1103
|
+
* only of 'len' and the announced bit length of m. Whether x fits or
|
|
1104
|
+
* not does not leak either.
|
|
1105
|
+
*/
|
|
1106
|
+
uint32_t br_i32_decode_mod(uint32_t *x,
|
|
1107
|
+
const void *src, size_t len, const uint32_t *m);
|
|
1108
|
+
|
|
1109
|
+
/*
|
|
1110
|
+
* Reduce an integer (a[]) modulo another (m[]). The result is written
|
|
1111
|
+
* in x[] and its announced bit length is set to be equal to that of m[].
|
|
1112
|
+
*
|
|
1113
|
+
* x[] MUST be distinct from a[] and m[].
|
|
1114
|
+
*
|
|
1115
|
+
* CT: only announced bit lengths leak, not values of x, a or m.
|
|
1116
|
+
*/
|
|
1117
|
+
void br_i32_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
|
|
1118
|
+
|
|
1119
|
+
/*
|
|
1120
|
+
* Decode an integer from its big-endian unsigned representation, and
|
|
1121
|
+
* reduce it modulo the provided modulus m[]. The announced bit length
|
|
1122
|
+
* of the result is set to be equal to that of the modulus.
|
|
1123
|
+
*
|
|
1124
|
+
* x[] MUST be distinct from m[].
|
|
1125
|
+
*/
|
|
1126
|
+
void br_i32_decode_reduce(uint32_t *x,
|
|
1127
|
+
const void *src, size_t len, const uint32_t *m);
|
|
1128
|
+
|
|
1129
|
+
/*
|
|
1130
|
+
* Encode an integer into its big-endian unsigned representation. The
|
|
1131
|
+
* output length in bytes is provided (parameter 'len'); if the length
|
|
1132
|
+
* is too short then the integer is appropriately truncated; if it is
|
|
1133
|
+
* too long then the extra bytes are set to 0.
|
|
1134
|
+
*/
|
|
1135
|
+
void br_i32_encode(void *dst, size_t len, const uint32_t *x);
|
|
1136
|
+
|
|
1137
|
+
/*
|
|
1138
|
+
* Multiply x[] by 2^32 and then add integer z, modulo m[]. This
|
|
1139
|
+
* function assumes that x[] and m[] have the same announced bit
|
|
1140
|
+
* length, and the announced bit length of m[] matches its true
|
|
1141
|
+
* bit length.
|
|
1142
|
+
*
|
|
1143
|
+
* x[] and m[] MUST be distinct arrays.
|
|
1144
|
+
*
|
|
1145
|
+
* CT: only the common announced bit length of x and m leaks, not
|
|
1146
|
+
* the values of x, z or m.
|
|
1147
|
+
*/
|
|
1148
|
+
void br_i32_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
|
|
1149
|
+
|
|
1150
|
+
/*
|
|
1151
|
+
* Extract one word from an integer. The offset is counted in bits.
|
|
1152
|
+
* The word MUST entirely fit within the word elements corresponding
|
|
1153
|
+
* to the announced bit length of a[].
|
|
1154
|
+
*/
|
|
1155
|
+
static inline uint32_t
|
|
1156
|
+
br_i32_word(const uint32_t *a, uint32_t off)
|
|
1157
|
+
{
|
|
1158
|
+
size_t u;
|
|
1159
|
+
unsigned j;
|
|
1160
|
+
|
|
1161
|
+
u = (size_t)(off >> 5) + 1;
|
|
1162
|
+
j = (unsigned)off & 31;
|
|
1163
|
+
if (j == 0) {
|
|
1164
|
+
return a[u];
|
|
1165
|
+
} else {
|
|
1166
|
+
return (a[u] >> j) | (a[u + 1] << (32 - j));
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/*
|
|
1171
|
+
* Test whether an integer is zero.
|
|
1172
|
+
*/
|
|
1173
|
+
uint32_t br_i32_iszero(const uint32_t *x);
|
|
1174
|
+
|
|
1175
|
+
/*
|
|
1176
|
+
* Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
|
|
1177
|
+
* is unmodified, but the carry is still computed and returned. The
|
|
1178
|
+
* arrays a[] and b[] MUST have the same announced bit length.
|
|
1179
|
+
*
|
|
1180
|
+
* a[] and b[] MAY be the same array, but partial overlap is not allowed.
|
|
1181
|
+
*/
|
|
1182
|
+
uint32_t br_i32_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
|
|
1183
|
+
|
|
1184
|
+
/*
|
|
1185
|
+
* Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
|
|
1186
|
+
* then a[] is unmodified, but the carry is still computed and returned.
|
|
1187
|
+
* The arrays a[] and b[] MUST have the same announced bit length.
|
|
1188
|
+
*
|
|
1189
|
+
* a[] and b[] MAY be the same array, but partial overlap is not allowed.
|
|
1190
|
+
*/
|
|
1191
|
+
uint32_t br_i32_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
|
|
1192
|
+
|
|
1193
|
+
/*
|
|
1194
|
+
* Compute d+a*b, result in d. The initial announced bit length of d[]
|
|
1195
|
+
* MUST match that of a[]. The d[] array MUST be large enough to
|
|
1196
|
+
* accommodate the full result, plus (possibly) an extra word. The
|
|
1197
|
+
* resulting announced bit length of d[] will be the sum of the announced
|
|
1198
|
+
* bit lengths of a[] and b[] (therefore, it may be larger than the actual
|
|
1199
|
+
* bit length of the numerical result).
|
|
1200
|
+
*
|
|
1201
|
+
* a[] and b[] may be the same array. d[] must be disjoint from both a[]
|
|
1202
|
+
* and b[].
|
|
1203
|
+
*/
|
|
1204
|
+
void br_i32_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
|
|
1205
|
+
|
|
1206
|
+
/*
|
|
1207
|
+
* Zeroize an integer. The announced bit length is set to the provided
|
|
1208
|
+
* value, and the corresponding words are set to 0.
|
|
1209
|
+
*/
|
|
1210
|
+
static inline void
|
|
1211
|
+
br_i32_zero(uint32_t *x, uint32_t bit_len)
|
|
1212
|
+
{
|
|
1213
|
+
*x ++ = bit_len;
|
|
1214
|
+
memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
/*
|
|
1218
|
+
* Compute -(1/x) mod 2^32. If x is even, then this function returns 0.
|
|
1219
|
+
*/
|
|
1220
|
+
uint32_t br_i32_ninv32(uint32_t x);
|
|
1221
|
+
|
|
1222
|
+
/*
|
|
1223
|
+
* Convert a modular integer to Montgomery representation. The integer x[]
|
|
1224
|
+
* MUST be lower than m[], but with the same announced bit length.
|
|
1225
|
+
*/
|
|
1226
|
+
void br_i32_to_monty(uint32_t *x, const uint32_t *m);
|
|
1227
|
+
|
|
1228
|
+
/*
|
|
1229
|
+
* Convert a modular integer back from Montgomery representation. The
|
|
1230
|
+
* integer x[] MUST be lower than m[], but with the same announced bit
|
|
1231
|
+
* length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
|
|
1232
|
+
* the least significant value word of m[] (this works only if m[] is
|
|
1233
|
+
* an odd integer).
|
|
1234
|
+
*/
|
|
1235
|
+
void br_i32_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
|
|
1236
|
+
|
|
1237
|
+
/*
|
|
1238
|
+
* Compute a modular Montgomery multiplication. d[] is filled with the
|
|
1239
|
+
* value of x*y/R modulo m[] (where R is the Montgomery factor). The
|
|
1240
|
+
* array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
|
|
1241
|
+
* numerically lower than m[]. x[] and y[] MAY be the same array. The
|
|
1242
|
+
* "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
|
|
1243
|
+
* significant value word of m[] (this works only if m[] is an odd
|
|
1244
|
+
* integer).
|
|
1245
|
+
*/
|
|
1246
|
+
void br_i32_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
|
|
1247
|
+
const uint32_t *m, uint32_t m0i);
|
|
1248
|
+
|
|
1249
|
+
/*
|
|
1250
|
+
* Compute a modular exponentiation. x[] MUST be an integer modulo m[]
|
|
1251
|
+
* (same announced bit length, lower value). m[] MUST be odd. The
|
|
1252
|
+
* exponent is in big-endian unsigned notation, over 'elen' bytes. The
|
|
1253
|
+
* "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
|
|
1254
|
+
* significant value word of m[] (this works only if m[] is an odd
|
|
1255
|
+
* integer). The t1[] and t2[] parameters must be temporary arrays,
|
|
1256
|
+
* each large enough to accommodate an integer with the same size as m[].
|
|
1257
|
+
*/
|
|
1258
|
+
void br_i32_modpow(uint32_t *x, const unsigned char *e, size_t elen,
|
|
1259
|
+
const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
|
|
1260
|
+
|
|
1261
|
+
/* ==================================================================== */
|
|
1262
|
+
|
|
1263
|
+
/*
|
|
1264
|
+
* Integers 'i31'
|
|
1265
|
+
* --------------
|
|
1266
|
+
*
|
|
1267
|
+
* The 'i31' functions implement computations on big integers using
|
|
1268
|
+
* an internal representation as an array of 32-bit integers. For
|
|
1269
|
+
* an array x[]:
|
|
1270
|
+
* -- x[0] encodes the array length and the "announced bit length"
|
|
1271
|
+
* of the integer: namely, if the announced bit length is k,
|
|
1272
|
+
* then x[0] = ((k / 31) << 5) + (k % 31).
|
|
1273
|
+
* -- x[1], x[2]... contain the value in little-endian order, 31
|
|
1274
|
+
* bits per word (x[1] contains the least significant 31 bits).
|
|
1275
|
+
* The upper bit of each word is 0.
|
|
1276
|
+
*
|
|
1277
|
+
* Multiplications rely on the elementary 32x32->64 multiplication.
|
|
1278
|
+
*
|
|
1279
|
+
* The announced bit length specifies the number of bits that are
|
|
1280
|
+
* significant in the subsequent 32-bit words. Unused bits in the
|
|
1281
|
+
* last (most significant) word are set to 0; subsequent words are
|
|
1282
|
+
* uninitialized and need not exist at all.
|
|
1283
|
+
*
|
|
1284
|
+
* The execution time and memory access patterns of all computations
|
|
1285
|
+
* depend on the announced bit length, but not on the actual word
|
|
1286
|
+
* values. For modular integers, the announced bit length of any integer
|
|
1287
|
+
* modulo n is equal to the actual bit length of n; thus, computations
|
|
1288
|
+
* on modular integers are "constant-time" (only the modulus length may
|
|
1289
|
+
* leak).
|
|
1290
|
+
*/
|
|
1291
|
+
|
|
1292
|
+
/*
|
|
1293
|
+
* Test whether an integer is zero.
|
|
1294
|
+
*/
|
|
1295
|
+
uint32_t br_i31_iszero(const uint32_t *x);
|
|
1296
|
+
|
|
1297
|
+
/*
|
|
1298
|
+
* Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
|
|
1299
|
+
* is unmodified, but the carry is still computed and returned. The
|
|
1300
|
+
* arrays a[] and b[] MUST have the same announced bit length.
|
|
1301
|
+
*
|
|
1302
|
+
* a[] and b[] MAY be the same array, but partial overlap is not allowed.
|
|
1303
|
+
*/
|
|
1304
|
+
uint32_t br_i31_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
|
|
1305
|
+
|
|
1306
|
+
/*
|
|
1307
|
+
* Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
|
|
1308
|
+
* then a[] is unmodified, but the carry is still computed and returned.
|
|
1309
|
+
* The arrays a[] and b[] MUST have the same announced bit length.
|
|
1310
|
+
*
|
|
1311
|
+
* a[] and b[] MAY be the same array, but partial overlap is not allowed.
|
|
1312
|
+
*/
|
|
1313
|
+
uint32_t br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
|
|
1314
|
+
|
|
1315
|
+
/*
|
|
1316
|
+
* Compute the ENCODED actual bit length of an integer. The argument x
|
|
1317
|
+
* should point to the first (least significant) value word of the
|
|
1318
|
+
* integer. The len 'xlen' contains the number of 32-bit words to
|
|
1319
|
+
* access. The upper bit of each value word MUST be 0.
|
|
1320
|
+
* Returned value is ((k / 31) << 5) + (k % 31) if the bit length is k.
|
|
1321
|
+
*
|
|
1322
|
+
* CT: value or length of x does not leak.
|
|
1323
|
+
*/
|
|
1324
|
+
uint32_t br_i31_bit_length(uint32_t *x, size_t xlen);
|
|
1325
|
+
|
|
1326
|
+
/*
|
|
1327
|
+
* Decode an integer from its big-endian unsigned representation. The
|
|
1328
|
+
* "true" bit length of the integer is computed and set in the encoded
|
|
1329
|
+
* announced bit length (x[0]), but all words of x[] corresponding to
|
|
1330
|
+
* the full 'len' bytes of the source are set.
|
|
1331
|
+
*
|
|
1332
|
+
* CT: value or length of x does not leak.
|
|
1333
|
+
*/
|
|
1334
|
+
void br_i31_decode(uint32_t *x, const void *src, size_t len);
|
|
1335
|
+
|
|
1336
|
+
/*
|
|
1337
|
+
* Decode an integer from its big-endian unsigned representation. The
|
|
1338
|
+
* integer MUST be lower than m[]; the (encoded) announced bit length
|
|
1339
|
+
* written in x[] will be equal to that of m[]. All 'len' bytes from the
|
|
1340
|
+
* source are read.
|
|
1341
|
+
*
|
|
1342
|
+
* Returned value is 1 if the decode value fits within the modulus, 0
|
|
1343
|
+
* otherwise. In the latter case, the x[] buffer will be set to 0 (but
|
|
1344
|
+
* still with the announced bit length of m[]).
|
|
1345
|
+
*
|
|
1346
|
+
* CT: value or length of x does not leak. Memory access pattern depends
|
|
1347
|
+
* only of 'len' and the announced bit length of m. Whether x fits or
|
|
1348
|
+
* not does not leak either.
|
|
1349
|
+
*/
|
|
1350
|
+
uint32_t br_i31_decode_mod(uint32_t *x,
|
|
1351
|
+
const void *src, size_t len, const uint32_t *m);
|
|
1352
|
+
|
|
1353
|
+
/*
|
|
1354
|
+
* Zeroize an integer. The announced bit length is set to the provided
|
|
1355
|
+
* value, and the corresponding words are set to 0. The ENCODED bit length
|
|
1356
|
+
* is expected here.
|
|
1357
|
+
*/
|
|
1358
|
+
static inline void
|
|
1359
|
+
br_i31_zero(uint32_t *x, uint32_t bit_len)
|
|
1360
|
+
{
|
|
1361
|
+
*x ++ = bit_len;
|
|
1362
|
+
memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
/*
|
|
1366
|
+
* Right-shift an integer. The shift amount must be lower than 31
|
|
1367
|
+
* bits.
|
|
1368
|
+
*/
|
|
1369
|
+
void br_i31_rshift(uint32_t *x, int count);
|
|
1370
|
+
|
|
1371
|
+
/*
|
|
1372
|
+
* Reduce an integer (a[]) modulo another (m[]). The result is written
|
|
1373
|
+
* in x[] and its announced bit length is set to be equal to that of m[].
|
|
1374
|
+
*
|
|
1375
|
+
* x[] MUST be distinct from a[] and m[].
|
|
1376
|
+
*
|
|
1377
|
+
* CT: only announced bit lengths leak, not values of x, a or m.
|
|
1378
|
+
*/
|
|
1379
|
+
void br_i31_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
|
|
1380
|
+
|
|
1381
|
+
/*
|
|
1382
|
+
* Decode an integer from its big-endian unsigned representation, and
|
|
1383
|
+
* reduce it modulo the provided modulus m[]. The announced bit length
|
|
1384
|
+
* of the result is set to be equal to that of the modulus.
|
|
1385
|
+
*
|
|
1386
|
+
* x[] MUST be distinct from m[].
|
|
1387
|
+
*/
|
|
1388
|
+
void br_i31_decode_reduce(uint32_t *x,
|
|
1389
|
+
const void *src, size_t len, const uint32_t *m);
|
|
1390
|
+
|
|
1391
|
+
/*
|
|
1392
|
+
* Multiply x[] by 2^31 and then add integer z, modulo m[]. This
|
|
1393
|
+
* function assumes that x[] and m[] have the same announced bit
|
|
1394
|
+
* length, the announced bit length of m[] matches its true
|
|
1395
|
+
* bit length.
|
|
1396
|
+
*
|
|
1397
|
+
* x[] and m[] MUST be distinct arrays. z MUST fit in 31 bits (upper
|
|
1398
|
+
* bit set to 0).
|
|
1399
|
+
*
|
|
1400
|
+
* CT: only the common announced bit length of x and m leaks, not
|
|
1401
|
+
* the values of x, z or m.
|
|
1402
|
+
*/
|
|
1403
|
+
void br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
|
|
1404
|
+
|
|
1405
|
+
/*
|
|
1406
|
+
* Encode an integer into its big-endian unsigned representation. The
|
|
1407
|
+
* output length in bytes is provided (parameter 'len'); if the length
|
|
1408
|
+
* is too short then the integer is appropriately truncated; if it is
|
|
1409
|
+
* too long then the extra bytes are set to 0.
|
|
1410
|
+
*/
|
|
1411
|
+
void br_i31_encode(void *dst, size_t len, const uint32_t *x);
|
|
1412
|
+
|
|
1413
|
+
/*
|
|
1414
|
+
* Compute -(1/x) mod 2^31. If x is even, then this function returns 0.
|
|
1415
|
+
*/
|
|
1416
|
+
uint32_t br_i31_ninv31(uint32_t x);
|
|
1417
|
+
|
|
1418
|
+
/*
|
|
1419
|
+
* Compute a modular Montgomery multiplication. d[] is filled with the
|
|
1420
|
+
* value of x*y/R modulo m[] (where R is the Montgomery factor). The
|
|
1421
|
+
* array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
|
|
1422
|
+
* numerically lower than m[]. x[] and y[] MAY be the same array. The
|
|
1423
|
+
* "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
|
|
1424
|
+
* significant value word of m[] (this works only if m[] is an odd
|
|
1425
|
+
* integer).
|
|
1426
|
+
*/
|
|
1427
|
+
void br_i31_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
|
|
1428
|
+
const uint32_t *m, uint32_t m0i);
|
|
1429
|
+
|
|
1430
|
+
/*
|
|
1431
|
+
* Convert a modular integer to Montgomery representation. The integer x[]
|
|
1432
|
+
* MUST be lower than m[], but with the same announced bit length.
|
|
1433
|
+
*/
|
|
1434
|
+
void br_i31_to_monty(uint32_t *x, const uint32_t *m);
|
|
1435
|
+
|
|
1436
|
+
/*
|
|
1437
|
+
* Convert a modular integer back from Montgomery representation. The
|
|
1438
|
+
* integer x[] MUST be lower than m[], but with the same announced bit
|
|
1439
|
+
* length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
|
|
1440
|
+
* the least significant value word of m[] (this works only if m[] is
|
|
1441
|
+
* an odd integer).
|
|
1442
|
+
*/
|
|
1443
|
+
void br_i31_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
|
|
1444
|
+
|
|
1445
|
+
/*
|
|
1446
|
+
* Compute a modular exponentiation. x[] MUST be an integer modulo m[]
|
|
1447
|
+
* (same announced bit length, lower value). m[] MUST be odd. The
|
|
1448
|
+
* exponent is in big-endian unsigned notation, over 'elen' bytes. The
|
|
1449
|
+
* "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
|
|
1450
|
+
* significant value word of m[] (this works only if m[] is an odd
|
|
1451
|
+
* integer). The t1[] and t2[] parameters must be temporary arrays,
|
|
1452
|
+
* each large enough to accommodate an integer with the same size as m[].
|
|
1453
|
+
*/
|
|
1454
|
+
void br_i31_modpow(uint32_t *x, const unsigned char *e, size_t elen,
|
|
1455
|
+
const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
|
|
1456
|
+
|
|
1457
|
+
/*
|
|
1458
|
+
* Compute a modular exponentiation. x[] MUST be an integer modulo m[]
|
|
1459
|
+
* (same announced bit length, lower value). m[] MUST be odd. The
|
|
1460
|
+
* exponent is in big-endian unsigned notation, over 'elen' bytes. The
|
|
1461
|
+
* "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
|
|
1462
|
+
* significant value word of m[] (this works only if m[] is an odd
|
|
1463
|
+
* integer). The tmp[] array is used for temporaries, and has size
|
|
1464
|
+
* 'twlen' words; it must be large enough to accommodate at least two
|
|
1465
|
+
* temporary values with the same size as m[] (including the leading
|
|
1466
|
+
* "bit length" word). If there is room for more temporaries, then this
|
|
1467
|
+
* function may use the extra room for window-based optimisation,
|
|
1468
|
+
* resulting in faster computations.
|
|
1469
|
+
*
|
|
1470
|
+
* Returned value is 1 on success, 0 on error. An error is reported if
|
|
1471
|
+
* the provided tmp[] array is too short.
|
|
1472
|
+
*/
|
|
1473
|
+
uint32_t br_i31_modpow_opt(uint32_t *x, const unsigned char *e, size_t elen,
|
|
1474
|
+
const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
|
|
1475
|
+
|
|
1476
|
+
/*
|
|
1477
|
+
* Compute d+a*b, result in d. The initial announced bit length of d[]
|
|
1478
|
+
* MUST match that of a[]. The d[] array MUST be large enough to
|
|
1479
|
+
* accommodate the full result, plus (possibly) an extra word. The
|
|
1480
|
+
* resulting announced bit length of d[] will be the sum of the announced
|
|
1481
|
+
* bit lengths of a[] and b[] (therefore, it may be larger than the actual
|
|
1482
|
+
* bit length of the numerical result).
|
|
1483
|
+
*
|
|
1484
|
+
* a[] and b[] may be the same array. d[] must be disjoint from both a[]
|
|
1485
|
+
* and b[].
|
|
1486
|
+
*/
|
|
1487
|
+
void br_i31_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
|
|
1488
|
+
|
|
1489
|
+
/*
|
|
1490
|
+
* Compute x/y mod m, result in x. Values x and y must be between 0 and
|
|
1491
|
+
* m-1, and have the same announced bit length as m. Modulus m must be
|
|
1492
|
+
* odd. The "m0i" parameter is equal to -1/m mod 2^31. The array 't'
|
|
1493
|
+
* must point to a temporary area that can hold at least three integers
|
|
1494
|
+
* of the size of m.
|
|
1495
|
+
*
|
|
1496
|
+
* m may not overlap x and y. x and y may overlap each other (this can
|
|
1497
|
+
* be useful to test whether a value is invertible modulo m). t must be
|
|
1498
|
+
* disjoint from all other arrays.
|
|
1499
|
+
*
|
|
1500
|
+
* Returned value is 1 on success, 0 otherwise. Success is attained if
|
|
1501
|
+
* y is invertible modulo m.
|
|
1502
|
+
*/
|
|
1503
|
+
uint32_t br_i31_moddiv(uint32_t *x, const uint32_t *y,
|
|
1504
|
+
const uint32_t *m, uint32_t m0i, uint32_t *t);
|
|
1505
|
+
|
|
1506
|
+
/* ==================================================================== */
|
|
1507
|
+
|
|
1508
|
+
/*
|
|
1509
|
+
* FIXME: document "i15" functions.
|
|
1510
|
+
*/
|
|
1511
|
+
|
|
1512
|
+
static inline void
|
|
1513
|
+
br_i15_zero(uint16_t *x, uint16_t bit_len)
|
|
1514
|
+
{
|
|
1515
|
+
*x ++ = bit_len;
|
|
1516
|
+
memset(x, 0, ((bit_len + 15) >> 4) * sizeof *x);
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
uint32_t br_i15_iszero(const uint16_t *x);
|
|
1520
|
+
|
|
1521
|
+
uint16_t br_i15_ninv15(uint16_t x);
|
|
1522
|
+
|
|
1523
|
+
uint32_t br_i15_add(uint16_t *a, const uint16_t *b, uint32_t ctl);
|
|
1524
|
+
|
|
1525
|
+
uint32_t br_i15_sub(uint16_t *a, const uint16_t *b, uint32_t ctl);
|
|
1526
|
+
|
|
1527
|
+
void br_i15_muladd_small(uint16_t *x, uint16_t z, const uint16_t *m);
|
|
1528
|
+
|
|
1529
|
+
void br_i15_montymul(uint16_t *d, const uint16_t *x, const uint16_t *y,
|
|
1530
|
+
const uint16_t *m, uint16_t m0i);
|
|
1531
|
+
|
|
1532
|
+
void br_i15_to_monty(uint16_t *x, const uint16_t *m);
|
|
1533
|
+
|
|
1534
|
+
void br_i15_modpow(uint16_t *x, const unsigned char *e, size_t elen,
|
|
1535
|
+
const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2);
|
|
1536
|
+
|
|
1537
|
+
uint32_t br_i15_modpow_opt(uint16_t *x, const unsigned char *e, size_t elen,
|
|
1538
|
+
const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen);
|
|
1539
|
+
|
|
1540
|
+
void br_i15_encode(void *dst, size_t len, const uint16_t *x);
|
|
1541
|
+
|
|
1542
|
+
uint32_t br_i15_decode_mod(uint16_t *x,
|
|
1543
|
+
const void *src, size_t len, const uint16_t *m);
|
|
1544
|
+
|
|
1545
|
+
void br_i15_rshift(uint16_t *x, int count);
|
|
1546
|
+
|
|
1547
|
+
uint32_t br_i15_bit_length(uint16_t *x, size_t xlen);
|
|
1548
|
+
|
|
1549
|
+
void br_i15_decode(uint16_t *x, const void *src, size_t len);
|
|
1550
|
+
|
|
1551
|
+
void br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i);
|
|
1552
|
+
|
|
1553
|
+
void br_i15_decode_reduce(uint16_t *x,
|
|
1554
|
+
const void *src, size_t len, const uint16_t *m);
|
|
1555
|
+
|
|
1556
|
+
void br_i15_reduce(uint16_t *x, const uint16_t *a, const uint16_t *m);
|
|
1557
|
+
|
|
1558
|
+
void br_i15_mulacc(uint16_t *d, const uint16_t *a, const uint16_t *b);
|
|
1559
|
+
|
|
1560
|
+
uint32_t br_i15_moddiv(uint16_t *x, const uint16_t *y,
|
|
1561
|
+
const uint16_t *m, uint16_t m0i, uint16_t *t);
|
|
1562
|
+
|
|
1563
|
+
/*
|
|
1564
|
+
* Variant of br_i31_modpow_opt() that internally uses 64x64->128
|
|
1565
|
+
* multiplications. It expects the same parameters as br_i31_modpow_opt(),
|
|
1566
|
+
* except that the temporaries should be 64-bit integers, not 32-bit
|
|
1567
|
+
* integers.
|
|
1568
|
+
*/
|
|
1569
|
+
uint32_t br_i62_modpow_opt(uint32_t *x31, const unsigned char *e, size_t elen,
|
|
1570
|
+
const uint32_t *m31, uint32_t m0i31, uint64_t *tmp, size_t twlen);
|
|
1571
|
+
|
|
1572
|
+
/*
|
|
1573
|
+
* Type for a function with the same API as br_i31_modpow_opt() (some
|
|
1574
|
+
* implementations of this type may have stricter alignment requirements
|
|
1575
|
+
* on the temporaries).
|
|
1576
|
+
*/
|
|
1577
|
+
typedef uint32_t (*br_i31_modpow_opt_type)(uint32_t *x,
|
|
1578
|
+
const unsigned char *e, size_t elen,
|
|
1579
|
+
const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
|
|
1580
|
+
|
|
1581
|
+
/*
|
|
1582
|
+
* Wrapper for br_i62_modpow_opt() that uses the same type as
|
|
1583
|
+
* br_i31_modpow_opt(); however, it requires its 'tmp' argument to the
|
|
1584
|
+
* 64-bit aligned.
|
|
1585
|
+
*/
|
|
1586
|
+
uint32_t br_i62_modpow_opt_as_i31(uint32_t *x,
|
|
1587
|
+
const unsigned char *e, size_t elen,
|
|
1588
|
+
const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
|
|
1589
|
+
|
|
1590
|
+
/* ==================================================================== */
|
|
1591
|
+
|
|
1592
|
+
static inline size_t
|
|
1593
|
+
br_digest_size(const br_hash_class *digest_class)
|
|
1594
|
+
{
|
|
1595
|
+
return (size_t)(digest_class->desc >> BR_HASHDESC_OUT_OFF)
|
|
1596
|
+
& BR_HASHDESC_OUT_MASK;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/*
|
|
1600
|
+
* Get the output size (in bytes) of a hash function.
|
|
1601
|
+
*/
|
|
1602
|
+
size_t br_digest_size_by_ID(int digest_id);
|
|
1603
|
+
|
|
1604
|
+
/*
|
|
1605
|
+
* Get the OID (encoded OBJECT IDENTIFIER value, without tag and length)
|
|
1606
|
+
* for a hash function. If digest_id is not a supported digest identifier
|
|
1607
|
+
* (in particular if it is equal to 0, i.e. br_md5sha1_ID), then NULL is
|
|
1608
|
+
* returned and *len is set to 0.
|
|
1609
|
+
*/
|
|
1610
|
+
const unsigned char *br_digest_OID(int digest_id, size_t *len);
|
|
1611
|
+
|
|
1612
|
+
/* ==================================================================== */
|
|
1613
|
+
/*
|
|
1614
|
+
* DES support functions.
|
|
1615
|
+
*/
|
|
1616
|
+
|
|
1617
|
+
/*
|
|
1618
|
+
* Apply DES Initial Permutation.
|
|
1619
|
+
*/
|
|
1620
|
+
void br_des_do_IP(uint32_t *xl, uint32_t *xr);
|
|
1621
|
+
|
|
1622
|
+
/*
|
|
1623
|
+
* Apply DES Final Permutation (inverse of IP).
|
|
1624
|
+
*/
|
|
1625
|
+
void br_des_do_invIP(uint32_t *xl, uint32_t *xr);
|
|
1626
|
+
|
|
1627
|
+
/*
|
|
1628
|
+
* Key schedule unit: for a DES key (8 bytes), compute 16 subkeys. Each
|
|
1629
|
+
* subkey is two 28-bit words represented as two 32-bit words; the PC-2
|
|
1630
|
+
* bit extration is NOT applied.
|
|
1631
|
+
*/
|
|
1632
|
+
void br_des_keysched_unit(uint32_t *skey, const void *key);
|
|
1633
|
+
|
|
1634
|
+
/*
|
|
1635
|
+
* Reversal of 16 DES sub-keys (for decryption).
|
|
1636
|
+
*/
|
|
1637
|
+
void br_des_rev_skey(uint32_t *skey);
|
|
1638
|
+
|
|
1639
|
+
/*
|
|
1640
|
+
* DES/3DES key schedule for 'des_tab' (encryption direction). Returned
|
|
1641
|
+
* value is the number of rounds.
|
|
1642
|
+
*/
|
|
1643
|
+
unsigned br_des_tab_keysched(uint32_t *skey, const void *key, size_t key_len);
|
|
1644
|
+
|
|
1645
|
+
/*
|
|
1646
|
+
* DES/3DES key schedule for 'des_ct' (encryption direction). Returned
|
|
1647
|
+
* value is the number of rounds.
|
|
1648
|
+
*/
|
|
1649
|
+
unsigned br_des_ct_keysched(uint32_t *skey, const void *key, size_t key_len);
|
|
1650
|
+
|
|
1651
|
+
/*
|
|
1652
|
+
* DES/3DES subkey decompression (from the compressed bitsliced subkeys).
|
|
1653
|
+
*/
|
|
1654
|
+
void br_des_ct_skey_expand(uint32_t *sk_exp,
|
|
1655
|
+
unsigned num_rounds, const uint32_t *skey);
|
|
1656
|
+
|
|
1657
|
+
/*
|
|
1658
|
+
* DES/3DES block encryption/decryption ('des_tab').
|
|
1659
|
+
*/
|
|
1660
|
+
void br_des_tab_process_block(unsigned num_rounds,
|
|
1661
|
+
const uint32_t *skey, void *block);
|
|
1662
|
+
|
|
1663
|
+
/*
|
|
1664
|
+
* DES/3DES block encryption/decryption ('des_ct').
|
|
1665
|
+
*/
|
|
1666
|
+
void br_des_ct_process_block(unsigned num_rounds,
|
|
1667
|
+
const uint32_t *skey, void *block);
|
|
1668
|
+
|
|
1669
|
+
/* ==================================================================== */
|
|
1670
|
+
/*
|
|
1671
|
+
* AES support functions.
|
|
1672
|
+
*/
|
|
1673
|
+
|
|
1674
|
+
/*
|
|
1675
|
+
* The AES S-box (256-byte table).
|
|
1676
|
+
*/
|
|
1677
|
+
extern const unsigned char br_aes_S[];
|
|
1678
|
+
|
|
1679
|
+
/*
|
|
1680
|
+
* AES key schedule. skey[] is filled with n+1 128-bit subkeys, where n
|
|
1681
|
+
* is the number of rounds (10 to 14, depending on key size). The number
|
|
1682
|
+
* of rounds is returned. If the key size is invalid (not 16, 24 or 32),
|
|
1683
|
+
* then 0 is returned.
|
|
1684
|
+
*
|
|
1685
|
+
* This implementation uses a 256-byte table and is NOT constant-time.
|
|
1686
|
+
*/
|
|
1687
|
+
unsigned br_aes_keysched(uint32_t *skey, const void *key, size_t key_len);
|
|
1688
|
+
|
|
1689
|
+
/*
|
|
1690
|
+
* AES key schedule for decryption ('aes_big' implementation).
|
|
1691
|
+
*/
|
|
1692
|
+
unsigned br_aes_big_keysched_inv(uint32_t *skey,
|
|
1693
|
+
const void *key, size_t key_len);
|
|
1694
|
+
|
|
1695
|
+
/*
|
|
1696
|
+
* AES block encryption with the 'aes_big' implementation (fast, but
|
|
1697
|
+
* not constant-time). This function encrypts a single block "in place".
|
|
1698
|
+
*/
|
|
1699
|
+
void br_aes_big_encrypt(unsigned num_rounds, const uint32_t *skey, void *data);
|
|
1700
|
+
|
|
1701
|
+
/*
|
|
1702
|
+
* AES block decryption with the 'aes_big' implementation (fast, but
|
|
1703
|
+
* not constant-time). This function decrypts a single block "in place".
|
|
1704
|
+
*/
|
|
1705
|
+
void br_aes_big_decrypt(unsigned num_rounds, const uint32_t *skey, void *data);
|
|
1706
|
+
|
|
1707
|
+
/*
|
|
1708
|
+
* AES block encryption with the 'aes_small' implementation (small, but
|
|
1709
|
+
* slow and not constant-time). This function encrypts a single block
|
|
1710
|
+
* "in place".
|
|
1711
|
+
*/
|
|
1712
|
+
void br_aes_small_encrypt(unsigned num_rounds,
|
|
1713
|
+
const uint32_t *skey, void *data);
|
|
1714
|
+
|
|
1715
|
+
/*
|
|
1716
|
+
* AES block decryption with the 'aes_small' implementation (small, but
|
|
1717
|
+
* slow and not constant-time). This function decrypts a single block
|
|
1718
|
+
* "in place".
|
|
1719
|
+
*/
|
|
1720
|
+
void br_aes_small_decrypt(unsigned num_rounds,
|
|
1721
|
+
const uint32_t *skey, void *data);
|
|
1722
|
+
|
|
1723
|
+
/*
|
|
1724
|
+
* The constant-time implementation is "bitsliced": the 128-bit state is
|
|
1725
|
+
* split over eight 32-bit words q* in the following way:
|
|
1726
|
+
*
|
|
1727
|
+
* -- Input block consists in 16 bytes:
|
|
1728
|
+
* a00 a10 a20 a30 a01 a11 a21 a31 a02 a12 a22 a32 a03 a13 a23 a33
|
|
1729
|
+
* In the terminology of FIPS 197, this is a 4x4 matrix which is read
|
|
1730
|
+
* column by column.
|
|
1731
|
+
*
|
|
1732
|
+
* -- Each byte is split into eight bits which are distributed over the
|
|
1733
|
+
* eight words, at the same rank. Thus, for a byte x at rank k, bit 0
|
|
1734
|
+
* (least significant) of x will be at rank k in q0 (if that bit is b,
|
|
1735
|
+
* then it contributes "b << k" to the value of q0), bit 1 of x will be
|
|
1736
|
+
* at rank k in q1, and so on.
|
|
1737
|
+
*
|
|
1738
|
+
* -- Ranks given to bits are in "row order" and are either all even, or
|
|
1739
|
+
* all odd. Two independent AES states are thus interleaved, one using
|
|
1740
|
+
* the even ranks, the other the odd ranks. Row order means:
|
|
1741
|
+
* a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33
|
|
1742
|
+
*
|
|
1743
|
+
* Converting input bytes from two AES blocks to bitslice representation
|
|
1744
|
+
* is done in the following way:
|
|
1745
|
+
* -- Decode first block into the four words q0 q2 q4 q6, in that order,
|
|
1746
|
+
* using little-endian convention.
|
|
1747
|
+
* -- Decode second block into the four words q1 q3 q5 q7, in that order,
|
|
1748
|
+
* using little-endian convention.
|
|
1749
|
+
* -- Call br_aes_ct_ortho().
|
|
1750
|
+
*
|
|
1751
|
+
* Converting back to bytes is done by using the reverse operations. Note
|
|
1752
|
+
* that br_aes_ct_ortho() is its own inverse.
|
|
1753
|
+
*/
|
|
1754
|
+
|
|
1755
|
+
/*
|
|
1756
|
+
* Perform bytewise orthogonalization of eight 32-bit words. Bytes
|
|
1757
|
+
* of q0..q7 are spread over all words: for a byte x that occurs
|
|
1758
|
+
* at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
|
|
1759
|
+
* of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
|
|
1760
|
+
*
|
|
1761
|
+
* This operation is an involution.
|
|
1762
|
+
*/
|
|
1763
|
+
void br_aes_ct_ortho(uint32_t *q);
|
|
1764
|
+
|
|
1765
|
+
/*
|
|
1766
|
+
* The AES S-box, as a bitsliced constant-time version. The input array
|
|
1767
|
+
* consists in eight 32-bit words; 32 S-box instances are computed in
|
|
1768
|
+
* parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
|
|
1769
|
+
* are spread over the words 0 to 7, at the same rank.
|
|
1770
|
+
*/
|
|
1771
|
+
void br_aes_ct_bitslice_Sbox(uint32_t *q);
|
|
1772
|
+
|
|
1773
|
+
/*
|
|
1774
|
+
* Like br_aes_bitslice_Sbox(), but for the inverse S-box.
|
|
1775
|
+
*/
|
|
1776
|
+
void br_aes_ct_bitslice_invSbox(uint32_t *q);
|
|
1777
|
+
|
|
1778
|
+
/*
|
|
1779
|
+
* Compute AES encryption on bitsliced data. Since input is stored on
|
|
1780
|
+
* eight 32-bit words, two block encryptions are actually performed
|
|
1781
|
+
* in parallel.
|
|
1782
|
+
*/
|
|
1783
|
+
void br_aes_ct_bitslice_encrypt(unsigned num_rounds,
|
|
1784
|
+
const uint32_t *skey, uint32_t *q);
|
|
1785
|
+
|
|
1786
|
+
/*
|
|
1787
|
+
* Compute AES decryption on bitsliced data. Since input is stored on
|
|
1788
|
+
* eight 32-bit words, two block decryptions are actually performed
|
|
1789
|
+
* in parallel.
|
|
1790
|
+
*/
|
|
1791
|
+
void br_aes_ct_bitslice_decrypt(unsigned num_rounds,
|
|
1792
|
+
const uint32_t *skey, uint32_t *q);
|
|
1793
|
+
|
|
1794
|
+
/*
|
|
1795
|
+
* AES key schedule, constant-time version. skey[] is filled with n+1
|
|
1796
|
+
* 128-bit subkeys, where n is the number of rounds (10 to 14, depending
|
|
1797
|
+
* on key size). The number of rounds is returned. If the key size is
|
|
1798
|
+
* invalid (not 16, 24 or 32), then 0 is returned.
|
|
1799
|
+
*/
|
|
1800
|
+
unsigned br_aes_ct_keysched(uint32_t *comp_skey,
|
|
1801
|
+
const void *key, size_t key_len);
|
|
1802
|
+
|
|
1803
|
+
/*
|
|
1804
|
+
* Expand AES subkeys as produced by br_aes_ct_keysched(), into
|
|
1805
|
+
* a larger array suitable for br_aes_ct_bitslice_encrypt() and
|
|
1806
|
+
* br_aes_ct_bitslice_decrypt().
|
|
1807
|
+
*/
|
|
1808
|
+
void br_aes_ct_skey_expand(uint32_t *skey,
|
|
1809
|
+
unsigned num_rounds, const uint32_t *comp_skey);
|
|
1810
|
+
|
|
1811
|
+
/*
|
|
1812
|
+
* For the ct64 implementation, the same bitslicing technique is used,
|
|
1813
|
+
* but four instances are interleaved. First instance uses bits 0, 4,
|
|
1814
|
+
* 8, 12,... of each word; second instance uses bits 1, 5, 9, 13,...
|
|
1815
|
+
* and so on.
|
|
1816
|
+
*/
|
|
1817
|
+
|
|
1818
|
+
/*
|
|
1819
|
+
* Perform bytewise orthogonalization of eight 64-bit words. Bytes
|
|
1820
|
+
* of q0..q7 are spread over all words: for a byte x that occurs
|
|
1821
|
+
* at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
|
|
1822
|
+
* of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
|
|
1823
|
+
*
|
|
1824
|
+
* This operation is an involution.
|
|
1825
|
+
*/
|
|
1826
|
+
void br_aes_ct64_ortho(uint64_t *q);
|
|
1827
|
+
|
|
1828
|
+
/*
|
|
1829
|
+
* Interleave bytes for an AES input block. If input bytes are
|
|
1830
|
+
* denoted 0123456789ABCDEF, and have been decoded with little-endian
|
|
1831
|
+
* convention (w[0] contains 0123, with '3' being most significant;
|
|
1832
|
+
* w[1] contains 4567, and so on), then output word q0 will be
|
|
1833
|
+
* set to 08192A3B (again little-endian convention) and q1 will
|
|
1834
|
+
* be set to 4C5D6E7F.
|
|
1835
|
+
*/
|
|
1836
|
+
void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w);
|
|
1837
|
+
|
|
1838
|
+
/*
|
|
1839
|
+
* Perform the opposite of br_aes_ct64_interleave_in().
|
|
1840
|
+
*/
|
|
1841
|
+
void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1);
|
|
1842
|
+
|
|
1843
|
+
/*
|
|
1844
|
+
* The AES S-box, as a bitsliced constant-time version. The input array
|
|
1845
|
+
* consists in eight 64-bit words; 64 S-box instances are computed in
|
|
1846
|
+
* parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
|
|
1847
|
+
* are spread over the words 0 to 7, at the same rank.
|
|
1848
|
+
*/
|
|
1849
|
+
void br_aes_ct64_bitslice_Sbox(uint64_t *q);
|
|
1850
|
+
|
|
1851
|
+
/*
|
|
1852
|
+
* Like br_aes_bitslice_Sbox(), but for the inverse S-box.
|
|
1853
|
+
*/
|
|
1854
|
+
void br_aes_ct64_bitslice_invSbox(uint64_t *q);
|
|
1855
|
+
|
|
1856
|
+
/*
|
|
1857
|
+
* Compute AES encryption on bitsliced data. Since input is stored on
|
|
1858
|
+
* eight 64-bit words, four block encryptions are actually performed
|
|
1859
|
+
* in parallel.
|
|
1860
|
+
*/
|
|
1861
|
+
void br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
|
|
1862
|
+
const uint64_t *skey, uint64_t *q);
|
|
1863
|
+
|
|
1864
|
+
/*
|
|
1865
|
+
* Compute AES decryption on bitsliced data. Since input is stored on
|
|
1866
|
+
* eight 64-bit words, four block decryptions are actually performed
|
|
1867
|
+
* in parallel.
|
|
1868
|
+
*/
|
|
1869
|
+
void br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
|
|
1870
|
+
const uint64_t *skey, uint64_t *q);
|
|
1871
|
+
|
|
1872
|
+
/*
|
|
1873
|
+
* AES key schedule, constant-time version. skey[] is filled with n+1
|
|
1874
|
+
* 128-bit subkeys, where n is the number of rounds (10 to 14, depending
|
|
1875
|
+
* on key size). The number of rounds is returned. If the key size is
|
|
1876
|
+
* invalid (not 16, 24 or 32), then 0 is returned.
|
|
1877
|
+
*/
|
|
1878
|
+
unsigned br_aes_ct64_keysched(uint64_t *comp_skey,
|
|
1879
|
+
const void *key, size_t key_len);
|
|
1880
|
+
|
|
1881
|
+
/*
|
|
1882
|
+
* Expand AES subkeys as produced by br_aes_ct64_keysched(), into
|
|
1883
|
+
* a larger array suitable for br_aes_ct64_bitslice_encrypt() and
|
|
1884
|
+
* br_aes_ct64_bitslice_decrypt().
|
|
1885
|
+
*/
|
|
1886
|
+
void br_aes_ct64_skey_expand(uint64_t *skey,
|
|
1887
|
+
unsigned num_rounds, const uint64_t *comp_skey);
|
|
1888
|
+
|
|
1889
|
+
/*
|
|
1890
|
+
* Test support for AES-NI opcodes.
|
|
1891
|
+
*/
|
|
1892
|
+
int br_aes_x86ni_supported(void);
|
|
1893
|
+
|
|
1894
|
+
/*
|
|
1895
|
+
* AES key schedule, using x86 AES-NI instructions. This yields the
|
|
1896
|
+
* subkeys in the encryption direction. Number of rounds is returned.
|
|
1897
|
+
* Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
|
|
1898
|
+
*/
|
|
1899
|
+
unsigned br_aes_x86ni_keysched_enc(unsigned char *skni,
|
|
1900
|
+
const void *key, size_t len);
|
|
1901
|
+
|
|
1902
|
+
/*
|
|
1903
|
+
* AES key schedule, using x86 AES-NI instructions. This yields the
|
|
1904
|
+
* subkeys in the decryption direction. Number of rounds is returned.
|
|
1905
|
+
* Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
|
|
1906
|
+
*/
|
|
1907
|
+
unsigned br_aes_x86ni_keysched_dec(unsigned char *skni,
|
|
1908
|
+
const void *key, size_t len);
|
|
1909
|
+
|
|
1910
|
+
/*
|
|
1911
|
+
* Test support for AES POWER8 opcodes.
|
|
1912
|
+
*/
|
|
1913
|
+
int br_aes_pwr8_supported(void);
|
|
1914
|
+
|
|
1915
|
+
/*
|
|
1916
|
+
* AES key schedule, using POWER8 instructions. This yields the
|
|
1917
|
+
* subkeys in the encryption direction. Number of rounds is returned.
|
|
1918
|
+
* Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
|
|
1919
|
+
*/
|
|
1920
|
+
unsigned br_aes_pwr8_keysched(unsigned char *skni,
|
|
1921
|
+
const void *key, size_t len);
|
|
1922
|
+
|
|
1923
|
+
/* ==================================================================== */
|
|
1924
|
+
/*
|
|
1925
|
+
* RSA.
|
|
1926
|
+
*/
|
|
1927
|
+
|
|
1928
|
+
/*
|
|
1929
|
+
* Apply proper PKCS#1 v1.5 padding (for signatures). 'hash_oid' is
|
|
1930
|
+
* the encoded hash function OID, or NULL.
|
|
1931
|
+
*/
|
|
1932
|
+
uint32_t br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid,
|
|
1933
|
+
const unsigned char *hash, size_t hash_len,
|
|
1934
|
+
uint32_t n_bitlen, unsigned char *x);
|
|
1935
|
+
|
|
1936
|
+
/*
|
|
1937
|
+
* Check PKCS#1 v1.5 padding (for signatures). 'hash_oid' is the encoded
|
|
1938
|
+
* hash function OID, or NULL. The provided 'sig' value is _after_ the
|
|
1939
|
+
* modular exponentiation, i.e. it should be the padded hash. On
|
|
1940
|
+
* success, the hashed message is extracted.
|
|
1941
|
+
*/
|
|
1942
|
+
uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
|
|
1943
|
+
const unsigned char *hash_oid, size_t hash_len,
|
|
1944
|
+
unsigned char *hash_out);
|
|
1945
|
+
|
|
1946
|
+
/*
|
|
1947
|
+
* Apply OAEP padding. Returned value is the actual padded string length,
|
|
1948
|
+
* or zero on error.
|
|
1949
|
+
*/
|
|
1950
|
+
size_t br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
|
|
1951
|
+
const void *label, size_t label_len, const br_rsa_public_key *pk,
|
|
1952
|
+
void *dst, size_t dst_nax_len, const void *src, size_t src_len);
|
|
1953
|
+
|
|
1954
|
+
/*
|
|
1955
|
+
* Unravel and check OAEP padding. If the padding is correct, then 1 is
|
|
1956
|
+
* returned, '*len' is adjusted to the length of the message, and the
|
|
1957
|
+
* data is moved to the start of the 'data' buffer. If the padding is
|
|
1958
|
+
* incorrect, then 0 is returned and '*len' is untouched. Either way,
|
|
1959
|
+
* the complete buffer contents are altered.
|
|
1960
|
+
*/
|
|
1961
|
+
uint32_t br_rsa_oaep_unpad(const br_hash_class *dig,
|
|
1962
|
+
const void *label, size_t label_len, void *data, size_t *len);
|
|
1963
|
+
|
|
1964
|
+
/*
|
|
1965
|
+
* Compute MGF1 for a given seed, and XOR the output into the provided
|
|
1966
|
+
* buffer.
|
|
1967
|
+
*/
|
|
1968
|
+
void br_mgf1_xor(void *data, size_t len,
|
|
1969
|
+
const br_hash_class *dig, const void *seed, size_t seed_len);
|
|
1970
|
+
|
|
1971
|
+
/*
|
|
1972
|
+
* Inner function for RSA key generation; used by the "i31" and "i62"
|
|
1973
|
+
* implementations.
|
|
1974
|
+
*/
|
|
1975
|
+
uint32_t br_rsa_i31_keygen_inner(const br_prng_class **rng,
|
|
1976
|
+
br_rsa_private_key *sk, void *kbuf_priv,
|
|
1977
|
+
br_rsa_public_key *pk, void *kbuf_pub,
|
|
1978
|
+
unsigned size, uint32_t pubexp, br_i31_modpow_opt_type mp31);
|
|
1979
|
+
|
|
1980
|
+
/* ==================================================================== */
|
|
1981
|
+
/*
|
|
1982
|
+
* Elliptic curves.
|
|
1983
|
+
*/
|
|
1984
|
+
|
|
1985
|
+
/*
|
|
1986
|
+
* Type for generic EC parameters: curve order (unsigned big-endian
|
|
1987
|
+
* encoding) and encoded conventional generator.
|
|
1988
|
+
*/
|
|
1989
|
+
typedef struct {
|
|
1990
|
+
int curve;
|
|
1991
|
+
const unsigned char *order;
|
|
1992
|
+
size_t order_len;
|
|
1993
|
+
const unsigned char *generator;
|
|
1994
|
+
size_t generator_len;
|
|
1995
|
+
} br_ec_curve_def;
|
|
1996
|
+
|
|
1997
|
+
extern const br_ec_curve_def br_secp256r1;
|
|
1998
|
+
extern const br_ec_curve_def br_secp384r1;
|
|
1999
|
+
extern const br_ec_curve_def br_secp521r1;
|
|
2000
|
+
|
|
2001
|
+
/*
|
|
2002
|
+
* For Curve25519, the advertised "order" really is 2^255-1, since the
|
|
2003
|
+
* point multipliction function really works over arbitrary 255-bit
|
|
2004
|
+
* scalars. This value is only meant as a hint for ECDH key generation;
|
|
2005
|
+
* only ECDSA uses the exact curve order, and ECDSA is not used with
|
|
2006
|
+
* that specific curve.
|
|
2007
|
+
*/
|
|
2008
|
+
extern const br_ec_curve_def br_curve25519;
|
|
2009
|
+
|
|
2010
|
+
/*
|
|
2011
|
+
* Decode some bytes as an i31 integer, with truncation (corresponding
|
|
2012
|
+
* to the 'bits2int' operation in RFC 6979). The target ENCODED bit
|
|
2013
|
+
* length is provided as last parameter. The resulting value will have
|
|
2014
|
+
* this declared bit length, and consists the big-endian unsigned decoding
|
|
2015
|
+
* of exactly that many bits in the source (capped at the source length).
|
|
2016
|
+
*/
|
|
2017
|
+
void br_ecdsa_i31_bits2int(uint32_t *x,
|
|
2018
|
+
const void *src, size_t len, uint32_t ebitlen);
|
|
2019
|
+
|
|
2020
|
+
/*
|
|
2021
|
+
* Decode some bytes as an i15 integer, with truncation (corresponding
|
|
2022
|
+
* to the 'bits2int' operation in RFC 6979). The target ENCODED bit
|
|
2023
|
+
* length is provided as last parameter. The resulting value will have
|
|
2024
|
+
* this declared bit length, and consists the big-endian unsigned decoding
|
|
2025
|
+
* of exactly that many bits in the source (capped at the source length).
|
|
2026
|
+
*/
|
|
2027
|
+
void br_ecdsa_i15_bits2int(uint16_t *x,
|
|
2028
|
+
const void *src, size_t len, uint32_t ebitlen);
|
|
2029
|
+
|
|
2030
|
+
/* ==================================================================== */
|
|
2031
|
+
/*
|
|
2032
|
+
* ASN.1 support functions.
|
|
2033
|
+
*/
|
|
2034
|
+
|
|
2035
|
+
/*
|
|
2036
|
+
* A br_asn1_uint structure contains encoding information about an
|
|
2037
|
+
* INTEGER nonnegative value: pointer to the integer contents (unsigned
|
|
2038
|
+
* big-endian representation), length of the integer contents,
|
|
2039
|
+
* and length of the encoded value. The data shall have minimal length:
|
|
2040
|
+
* - If the integer value is zero, then 'len' must be zero.
|
|
2041
|
+
* - If the integer value is not zero, then data[0] must be non-zero.
|
|
2042
|
+
*
|
|
2043
|
+
* Under these conditions, 'asn1len' is necessarily equal to either len
|
|
2044
|
+
* or len+1.
|
|
2045
|
+
*/
|
|
2046
|
+
typedef struct {
|
|
2047
|
+
const unsigned char *data;
|
|
2048
|
+
size_t len;
|
|
2049
|
+
size_t asn1len;
|
|
2050
|
+
} br_asn1_uint;
|
|
2051
|
+
|
|
2052
|
+
/*
|
|
2053
|
+
* Given an encoded integer (unsigned big-endian, with possible leading
|
|
2054
|
+
* bytes of value 0), returned the "prepared INTEGER" structure.
|
|
2055
|
+
*/
|
|
2056
|
+
br_asn1_uint br_asn1_uint_prepare(const void *xdata, size_t xlen);
|
|
2057
|
+
|
|
2058
|
+
/*
|
|
2059
|
+
* Encode an ASN.1 length. The length of the encoded length is returned.
|
|
2060
|
+
* If 'dest' is NULL, then no encoding is performed, but the length of
|
|
2061
|
+
* the encoded length is still computed and returned.
|
|
2062
|
+
*/
|
|
2063
|
+
size_t br_asn1_encode_length(void *dest, size_t len);
|
|
2064
|
+
|
|
2065
|
+
/*
|
|
2066
|
+
* Convenient macro for computing lengths of lengths.
|
|
2067
|
+
*/
|
|
2068
|
+
#define len_of_len(len) br_asn1_encode_length(NULL, len)
|
|
2069
|
+
|
|
2070
|
+
/*
|
|
2071
|
+
* Encode a (prepared) ASN.1 INTEGER. The encoded length is returned.
|
|
2072
|
+
* If 'dest' is NULL, then no encoding is performed, but the length of
|
|
2073
|
+
* the encoded integer is still computed and returned.
|
|
2074
|
+
*/
|
|
2075
|
+
size_t br_asn1_encode_uint(void *dest, br_asn1_uint pp);
|
|
2076
|
+
|
|
2077
|
+
/*
|
|
2078
|
+
* Get the OID that identifies an elliptic curve. Returned value is
|
|
2079
|
+
* the DER-encoded OID, with the length (always one byte) but without
|
|
2080
|
+
* the tag. Thus, the first byte of the returned buffer contains the
|
|
2081
|
+
* number of subsequent bytes in the value. If the curve is not
|
|
2082
|
+
* recognised, NULL is returned.
|
|
2083
|
+
*/
|
|
2084
|
+
const unsigned char *br_get_curve_OID(int curve);
|
|
2085
|
+
|
|
2086
|
+
/*
|
|
2087
|
+
* Inner function for EC private key encoding. This is equivalent to
|
|
2088
|
+
* the API function br_encode_ec_raw_der(), except for an extra
|
|
2089
|
+
* parameter: if 'include_curve_oid' is zero, then the curve OID is
|
|
2090
|
+
* _not_ included in the output blob (this is for PKCS#8 support).
|
|
2091
|
+
*/
|
|
2092
|
+
size_t br_encode_ec_raw_der_inner(void *dest,
|
|
2093
|
+
const br_ec_private_key *sk, const br_ec_public_key *pk,
|
|
2094
|
+
int include_curve_oid);
|
|
2095
|
+
|
|
2096
|
+
/* ==================================================================== */
|
|
2097
|
+
/*
|
|
2098
|
+
* SSL/TLS support functions.
|
|
2099
|
+
*/
|
|
2100
|
+
|
|
2101
|
+
/*
|
|
2102
|
+
* Record types.
|
|
2103
|
+
*/
|
|
2104
|
+
#define BR_SSL_CHANGE_CIPHER_SPEC 20
|
|
2105
|
+
#define BR_SSL_ALERT 21
|
|
2106
|
+
#define BR_SSL_HANDSHAKE 22
|
|
2107
|
+
#define BR_SSL_APPLICATION_DATA 23
|
|
2108
|
+
|
|
2109
|
+
/*
|
|
2110
|
+
* Handshake message types.
|
|
2111
|
+
*/
|
|
2112
|
+
#define BR_SSL_HELLO_REQUEST 0
|
|
2113
|
+
#define BR_SSL_CLIENT_HELLO 1
|
|
2114
|
+
#define BR_SSL_SERVER_HELLO 2
|
|
2115
|
+
#define BR_SSL_CERTIFICATE 11
|
|
2116
|
+
#define BR_SSL_SERVER_KEY_EXCHANGE 12
|
|
2117
|
+
#define BR_SSL_CERTIFICATE_REQUEST 13
|
|
2118
|
+
#define BR_SSL_SERVER_HELLO_DONE 14
|
|
2119
|
+
#define BR_SSL_CERTIFICATE_VERIFY 15
|
|
2120
|
+
#define BR_SSL_CLIENT_KEY_EXCHANGE 16
|
|
2121
|
+
#define BR_SSL_FINISHED 20
|
|
2122
|
+
|
|
2123
|
+
/*
|
|
2124
|
+
* Alert levels.
|
|
2125
|
+
*/
|
|
2126
|
+
#define BR_LEVEL_WARNING 1
|
|
2127
|
+
#define BR_LEVEL_FATAL 2
|
|
2128
|
+
|
|
2129
|
+
/*
|
|
2130
|
+
* Low-level I/O state.
|
|
2131
|
+
*/
|
|
2132
|
+
#define BR_IO_FAILED 0
|
|
2133
|
+
#define BR_IO_IN 1
|
|
2134
|
+
#define BR_IO_OUT 2
|
|
2135
|
+
#define BR_IO_INOUT 3
|
|
2136
|
+
|
|
2137
|
+
/*
|
|
2138
|
+
* Mark a SSL engine as failed. The provided error code is recorded if
|
|
2139
|
+
* the engine was not already marked as failed. If 'err' is 0, then the
|
|
2140
|
+
* engine is marked as closed (without error).
|
|
2141
|
+
*/
|
|
2142
|
+
void br_ssl_engine_fail(br_ssl_engine_context *cc, int err);
|
|
2143
|
+
|
|
2144
|
+
/*
|
|
2145
|
+
* Test whether the engine is closed (normally or as a failure).
|
|
2146
|
+
*/
|
|
2147
|
+
static inline int
|
|
2148
|
+
br_ssl_engine_closed(const br_ssl_engine_context *cc)
|
|
2149
|
+
{
|
|
2150
|
+
return cc->iomode == BR_IO_FAILED;
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
/*
|
|
2154
|
+
* Configure a new maximum fragment length. If possible, the maximum
|
|
2155
|
+
* length for outgoing records is immediately adjusted (if there are
|
|
2156
|
+
* not already too many buffered bytes for that).
|
|
2157
|
+
*/
|
|
2158
|
+
void br_ssl_engine_new_max_frag_len(
|
|
2159
|
+
br_ssl_engine_context *rc, unsigned max_frag_len);
|
|
2160
|
+
|
|
2161
|
+
/*
|
|
2162
|
+
* Test whether the current incoming record has been fully received
|
|
2163
|
+
* or not. This functions returns 0 only if a complete record header
|
|
2164
|
+
* has been received, but some of the (possibly encrypted) payload
|
|
2165
|
+
* has not yet been obtained.
|
|
2166
|
+
*/
|
|
2167
|
+
int br_ssl_engine_recvrec_finished(const br_ssl_engine_context *rc);
|
|
2168
|
+
|
|
2169
|
+
/*
|
|
2170
|
+
* Flush the current record (if not empty). This is meant to be called
|
|
2171
|
+
* from the handshake processor only.
|
|
2172
|
+
*/
|
|
2173
|
+
void br_ssl_engine_flush_record(br_ssl_engine_context *cc);
|
|
2174
|
+
|
|
2175
|
+
/*
|
|
2176
|
+
* Test whether there is some accumulated payload to send.
|
|
2177
|
+
*/
|
|
2178
|
+
static inline int
|
|
2179
|
+
br_ssl_engine_has_pld_to_send(const br_ssl_engine_context *rc)
|
|
2180
|
+
{
|
|
2181
|
+
return rc->oxa != rc->oxb && rc->oxa != rc->oxc;
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
/*
|
|
2185
|
+
* Initialize RNG in engine. Returned value is 1 on success, 0 on error.
|
|
2186
|
+
* This function will try to use the OS-provided RNG, if available. If
|
|
2187
|
+
* there is no OS-provided RNG, or if it failed, and no entropy was
|
|
2188
|
+
* injected by the caller, then a failure will be reported. On error,
|
|
2189
|
+
* the context error code is set.
|
|
2190
|
+
*/
|
|
2191
|
+
int br_ssl_engine_init_rand(br_ssl_engine_context *cc);
|
|
2192
|
+
|
|
2193
|
+
/*
|
|
2194
|
+
* Reset the handshake-related parts of the engine.
|
|
2195
|
+
*/
|
|
2196
|
+
void br_ssl_engine_hs_reset(br_ssl_engine_context *cc,
|
|
2197
|
+
void (*hsinit)(void *), void (*hsrun)(void *));
|
|
2198
|
+
|
|
2199
|
+
/*
|
|
2200
|
+
* Get the PRF to use for this context, for the provided PRF hash
|
|
2201
|
+
* function ID.
|
|
2202
|
+
*/
|
|
2203
|
+
br_tls_prf_impl br_ssl_engine_get_PRF(br_ssl_engine_context *cc, int prf_id);
|
|
2204
|
+
|
|
2205
|
+
/*
|
|
2206
|
+
* Consume the provided pre-master secret and compute the corresponding
|
|
2207
|
+
* master secret. The 'prf_id' is the ID of the hash function to use
|
|
2208
|
+
* with the TLS 1.2 PRF (ignored if the version is TLS 1.0 or 1.1).
|
|
2209
|
+
*/
|
|
2210
|
+
void br_ssl_engine_compute_master(br_ssl_engine_context *cc,
|
|
2211
|
+
int prf_id, const void *pms, size_t len);
|
|
2212
|
+
|
|
2213
|
+
/*
|
|
2214
|
+
* Switch to CBC decryption for incoming records.
|
|
2215
|
+
* cc the engine context
|
|
2216
|
+
* is_client non-zero for a client, zero for a server
|
|
2217
|
+
* prf_id id of hash function for PRF (ignored if not TLS 1.2+)
|
|
2218
|
+
* mac_id id of hash function for HMAC
|
|
2219
|
+
* bc_impl block cipher implementation (CBC decryption)
|
|
2220
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2221
|
+
*/
|
|
2222
|
+
void br_ssl_engine_switch_cbc_in(br_ssl_engine_context *cc,
|
|
2223
|
+
int is_client, int prf_id, int mac_id,
|
|
2224
|
+
const br_block_cbcdec_class *bc_impl, size_t cipher_key_len);
|
|
2225
|
+
|
|
2226
|
+
/*
|
|
2227
|
+
* Switch to CBC encryption for outgoing records.
|
|
2228
|
+
* cc the engine context
|
|
2229
|
+
* is_client non-zero for a client, zero for a server
|
|
2230
|
+
* prf_id id of hash function for PRF (ignored if not TLS 1.2+)
|
|
2231
|
+
* mac_id id of hash function for HMAC
|
|
2232
|
+
* bc_impl block cipher implementation (CBC encryption)
|
|
2233
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2234
|
+
*/
|
|
2235
|
+
void br_ssl_engine_switch_cbc_out(br_ssl_engine_context *cc,
|
|
2236
|
+
int is_client, int prf_id, int mac_id,
|
|
2237
|
+
const br_block_cbcenc_class *bc_impl, size_t cipher_key_len);
|
|
2238
|
+
|
|
2239
|
+
/*
|
|
2240
|
+
* Switch to GCM decryption for incoming records.
|
|
2241
|
+
* cc the engine context
|
|
2242
|
+
* is_client non-zero for a client, zero for a server
|
|
2243
|
+
* prf_id id of hash function for PRF
|
|
2244
|
+
* bc_impl block cipher implementation (CTR)
|
|
2245
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2246
|
+
*/
|
|
2247
|
+
void br_ssl_engine_switch_gcm_in(br_ssl_engine_context *cc,
|
|
2248
|
+
int is_client, int prf_id,
|
|
2249
|
+
const br_block_ctr_class *bc_impl, size_t cipher_key_len);
|
|
2250
|
+
|
|
2251
|
+
/*
|
|
2252
|
+
* Switch to GCM encryption for outgoing records.
|
|
2253
|
+
* cc the engine context
|
|
2254
|
+
* is_client non-zero for a client, zero for a server
|
|
2255
|
+
* prf_id id of hash function for PRF
|
|
2256
|
+
* bc_impl block cipher implementation (CTR)
|
|
2257
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2258
|
+
*/
|
|
2259
|
+
void br_ssl_engine_switch_gcm_out(br_ssl_engine_context *cc,
|
|
2260
|
+
int is_client, int prf_id,
|
|
2261
|
+
const br_block_ctr_class *bc_impl, size_t cipher_key_len);
|
|
2262
|
+
|
|
2263
|
+
/*
|
|
2264
|
+
* Switch to ChaCha20+Poly1305 decryption for incoming records.
|
|
2265
|
+
* cc the engine context
|
|
2266
|
+
* is_client non-zero for a client, zero for a server
|
|
2267
|
+
* prf_id id of hash function for PRF
|
|
2268
|
+
*/
|
|
2269
|
+
void br_ssl_engine_switch_chapol_in(br_ssl_engine_context *cc,
|
|
2270
|
+
int is_client, int prf_id);
|
|
2271
|
+
|
|
2272
|
+
/*
|
|
2273
|
+
* Switch to ChaCha20+Poly1305 encryption for outgoing records.
|
|
2274
|
+
* cc the engine context
|
|
2275
|
+
* is_client non-zero for a client, zero for a server
|
|
2276
|
+
* prf_id id of hash function for PRF
|
|
2277
|
+
*/
|
|
2278
|
+
void br_ssl_engine_switch_chapol_out(br_ssl_engine_context *cc,
|
|
2279
|
+
int is_client, int prf_id);
|
|
2280
|
+
|
|
2281
|
+
/*
|
|
2282
|
+
* Switch to CCM decryption for incoming records.
|
|
2283
|
+
* cc the engine context
|
|
2284
|
+
* is_client non-zero for a client, zero for a server
|
|
2285
|
+
* prf_id id of hash function for PRF
|
|
2286
|
+
* bc_impl block cipher implementation (CTR+CBC)
|
|
2287
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2288
|
+
* tag_len tag length (in bytes)
|
|
2289
|
+
*/
|
|
2290
|
+
void br_ssl_engine_switch_ccm_in(br_ssl_engine_context *cc,
|
|
2291
|
+
int is_client, int prf_id,
|
|
2292
|
+
const br_block_ctrcbc_class *bc_impl,
|
|
2293
|
+
size_t cipher_key_len, size_t tag_len);
|
|
2294
|
+
|
|
2295
|
+
/*
|
|
2296
|
+
* Switch to GCM encryption for outgoing records.
|
|
2297
|
+
* cc the engine context
|
|
2298
|
+
* is_client non-zero for a client, zero for a server
|
|
2299
|
+
* prf_id id of hash function for PRF
|
|
2300
|
+
* bc_impl block cipher implementation (CTR+CBC)
|
|
2301
|
+
* cipher_key_len block cipher key length (in bytes)
|
|
2302
|
+
* tag_len tag length (in bytes)
|
|
2303
|
+
*/
|
|
2304
|
+
void br_ssl_engine_switch_ccm_out(br_ssl_engine_context *cc,
|
|
2305
|
+
int is_client, int prf_id,
|
|
2306
|
+
const br_block_ctrcbc_class *bc_impl,
|
|
2307
|
+
size_t cipher_key_len, size_t tag_len);
|
|
2308
|
+
|
|
2309
|
+
/*
|
|
2310
|
+
* Calls to T0-generated code.
|
|
2311
|
+
*/
|
|
2312
|
+
void br_ssl_hs_client_init_main(void *ctx);
|
|
2313
|
+
void br_ssl_hs_client_run(void *ctx);
|
|
2314
|
+
void br_ssl_hs_server_init_main(void *ctx);
|
|
2315
|
+
void br_ssl_hs_server_run(void *ctx);
|
|
2316
|
+
|
|
2317
|
+
/*
|
|
2318
|
+
* Get the hash function to use for signatures, given a bit mask of
|
|
2319
|
+
* supported hash functions. This implements a strict choice order
|
|
2320
|
+
* (namely SHA-256, SHA-384, SHA-512, SHA-224, SHA-1). If the mask
|
|
2321
|
+
* does not document support of any of these hash functions, then this
|
|
2322
|
+
* functions returns 0.
|
|
2323
|
+
*/
|
|
2324
|
+
int br_ssl_choose_hash(unsigned bf);
|
|
2325
|
+
|
|
2326
|
+
/* ==================================================================== */
|
|
2327
|
+
|
|
2328
|
+
/*
|
|
2329
|
+
* PowerPC / POWER assembly stuff. The special BR_POWER_ASM_MACROS macro
|
|
2330
|
+
* must be defined before including this file; this is done by source
|
|
2331
|
+
* files that use some inline assembly for PowerPC / POWER machines.
|
|
2332
|
+
*/
|
|
2333
|
+
|
|
2334
|
+
#if BR_POWER_ASM_MACROS
|
|
2335
|
+
|
|
2336
|
+
#define lxvw4x(xt, ra, rb) lxvw4x_(xt, ra, rb)
|
|
2337
|
+
#define stxvw4x(xt, ra, rb) stxvw4x_(xt, ra, rb)
|
|
2338
|
+
|
|
2339
|
+
#define bdnz(foo) bdnz_(foo)
|
|
2340
|
+
#define bdz(foo) bdz_(foo)
|
|
2341
|
+
#define beq(foo) beq_(foo)
|
|
2342
|
+
|
|
2343
|
+
#define li(rx, value) li_(rx, value)
|
|
2344
|
+
#define addi(rx, ra, imm) addi_(rx, ra, imm)
|
|
2345
|
+
#define cmpldi(rx, imm) cmpldi_(rx, imm)
|
|
2346
|
+
#define mtctr(rx) mtctr_(rx)
|
|
2347
|
+
#define vspltb(vrt, vrb, uim) vspltb_(vrt, vrb, uim)
|
|
2348
|
+
#define vspltw(vrt, vrb, uim) vspltw_(vrt, vrb, uim)
|
|
2349
|
+
#define vspltisb(vrt, imm) vspltisb_(vrt, imm)
|
|
2350
|
+
#define vspltisw(vrt, imm) vspltisw_(vrt, imm)
|
|
2351
|
+
#define vrlw(vrt, vra, vrb) vrlw_(vrt, vra, vrb)
|
|
2352
|
+
#define vsbox(vrt, vra) vsbox_(vrt, vra)
|
|
2353
|
+
#define vxor(vrt, vra, vrb) vxor_(vrt, vra, vrb)
|
|
2354
|
+
#define vand(vrt, vra, vrb) vand_(vrt, vra, vrb)
|
|
2355
|
+
#define vsro(vrt, vra, vrb) vsro_(vrt, vra, vrb)
|
|
2356
|
+
#define vsl(vrt, vra, vrb) vsl_(vrt, vra, vrb)
|
|
2357
|
+
#define vsldoi(vt, va, vb, sh) vsldoi_(vt, va, vb, sh)
|
|
2358
|
+
#define vsr(vrt, vra, vrb) vsr_(vrt, vra, vrb)
|
|
2359
|
+
#define vaddcuw(vrt, vra, vrb) vaddcuw_(vrt, vra, vrb)
|
|
2360
|
+
#define vadduwm(vrt, vra, vrb) vadduwm_(vrt, vra, vrb)
|
|
2361
|
+
#define vsububm(vrt, vra, vrb) vsububm_(vrt, vra, vrb)
|
|
2362
|
+
#define vsubuwm(vrt, vra, vrb) vsubuwm_(vrt, vra, vrb)
|
|
2363
|
+
#define vsrw(vrt, vra, vrb) vsrw_(vrt, vra, vrb)
|
|
2364
|
+
#define vcipher(vt, va, vb) vcipher_(vt, va, vb)
|
|
2365
|
+
#define vcipherlast(vt, va, vb) vcipherlast_(vt, va, vb)
|
|
2366
|
+
#define vncipher(vt, va, vb) vncipher_(vt, va, vb)
|
|
2367
|
+
#define vncipherlast(vt, va, vb) vncipherlast_(vt, va, vb)
|
|
2368
|
+
#define vperm(vt, va, vb, vc) vperm_(vt, va, vb, vc)
|
|
2369
|
+
#define vpmsumd(vt, va, vb) vpmsumd_(vt, va, vb)
|
|
2370
|
+
#define xxpermdi(vt, va, vb, d) xxpermdi_(vt, va, vb, d)
|
|
2371
|
+
|
|
2372
|
+
#define lxvw4x_(xt, ra, rb) "\tlxvw4x\t" #xt "," #ra "," #rb "\n"
|
|
2373
|
+
#define stxvw4x_(xt, ra, rb) "\tstxvw4x\t" #xt "," #ra "," #rb "\n"
|
|
2374
|
+
|
|
2375
|
+
#define label(foo) #foo "%=:\n"
|
|
2376
|
+
#define bdnz_(foo) "\tbdnz\t" #foo "%=\n"
|
|
2377
|
+
#define bdz_(foo) "\tbdz\t" #foo "%=\n"
|
|
2378
|
+
#define beq_(foo) "\tbeq\t" #foo "%=\n"
|
|
2379
|
+
|
|
2380
|
+
#define li_(rx, value) "\tli\t" #rx "," #value "\n"
|
|
2381
|
+
#define addi_(rx, ra, imm) "\taddi\t" #rx "," #ra "," #imm "\n"
|
|
2382
|
+
#define cmpldi_(rx, imm) "\tcmpldi\t" #rx "," #imm "\n"
|
|
2383
|
+
#define mtctr_(rx) "\tmtctr\t" #rx "\n"
|
|
2384
|
+
#define vspltb_(vrt, vrb, uim) "\tvspltb\t" #vrt "," #vrb "," #uim "\n"
|
|
2385
|
+
#define vspltw_(vrt, vrb, uim) "\tvspltw\t" #vrt "," #vrb "," #uim "\n"
|
|
2386
|
+
#define vspltisb_(vrt, imm) "\tvspltisb\t" #vrt "," #imm "\n"
|
|
2387
|
+
#define vspltisw_(vrt, imm) "\tvspltisw\t" #vrt "," #imm "\n"
|
|
2388
|
+
#define vrlw_(vrt, vra, vrb) "\tvrlw\t" #vrt "," #vra "," #vrb "\n"
|
|
2389
|
+
#define vsbox_(vrt, vra) "\tvsbox\t" #vrt "," #vra "\n"
|
|
2390
|
+
#define vxor_(vrt, vra, vrb) "\tvxor\t" #vrt "," #vra "," #vrb "\n"
|
|
2391
|
+
#define vand_(vrt, vra, vrb) "\tvand\t" #vrt "," #vra "," #vrb "\n"
|
|
2392
|
+
#define vsro_(vrt, vra, vrb) "\tvsro\t" #vrt "," #vra "," #vrb "\n"
|
|
2393
|
+
#define vsl_(vrt, vra, vrb) "\tvsl\t" #vrt "," #vra "," #vrb "\n"
|
|
2394
|
+
#define vsldoi_(vt, va, vb, sh) "\tvsldoi\t" #vt "," #va "," #vb "," #sh "\n"
|
|
2395
|
+
#define vsr_(vrt, vra, vrb) "\tvsr\t" #vrt "," #vra "," #vrb "\n"
|
|
2396
|
+
#define vaddcuw_(vrt, vra, vrb) "\tvaddcuw\t" #vrt "," #vra "," #vrb "\n"
|
|
2397
|
+
#define vadduwm_(vrt, vra, vrb) "\tvadduwm\t" #vrt "," #vra "," #vrb "\n"
|
|
2398
|
+
#define vsububm_(vrt, vra, vrb) "\tvsububm\t" #vrt "," #vra "," #vrb "\n"
|
|
2399
|
+
#define vsubuwm_(vrt, vra, vrb) "\tvsubuwm\t" #vrt "," #vra "," #vrb "\n"
|
|
2400
|
+
#define vsrw_(vrt, vra, vrb) "\tvsrw\t" #vrt "," #vra "," #vrb "\n"
|
|
2401
|
+
#define vcipher_(vt, va, vb) "\tvcipher\t" #vt "," #va "," #vb "\n"
|
|
2402
|
+
#define vcipherlast_(vt, va, vb) "\tvcipherlast\t" #vt "," #va "," #vb "\n"
|
|
2403
|
+
#define vncipher_(vt, va, vb) "\tvncipher\t" #vt "," #va "," #vb "\n"
|
|
2404
|
+
#define vncipherlast_(vt, va, vb) "\tvncipherlast\t" #vt "," #va "," #vb "\n"
|
|
2405
|
+
#define vperm_(vt, va, vb, vc) "\tvperm\t" #vt "," #va "," #vb "," #vc "\n"
|
|
2406
|
+
#define vpmsumd_(vt, va, vb) "\tvpmsumd\t" #vt "," #va "," #vb "\n"
|
|
2407
|
+
#define xxpermdi_(vt, va, vb, d) "\txxpermdi\t" #vt "," #va "," #vb "," #d "\n"
|
|
2408
|
+
|
|
2409
|
+
#endif
|
|
2410
|
+
|
|
2411
|
+
/* ==================================================================== */
|
|
2412
|
+
/*
|
|
2413
|
+
* Special "activate intrinsics" code, needed for some compiler versions.
|
|
2414
|
+
* This is defined at the end of this file, so that it won't impact any
|
|
2415
|
+
* of the inline functions defined previously; and it is controlled by
|
|
2416
|
+
* a specific macro defined in the caller code.
|
|
2417
|
+
*
|
|
2418
|
+
* Calling code conventions:
|
|
2419
|
+
*
|
|
2420
|
+
* - Caller must define BR_ENABLE_INTRINSICS before including "inner.h".
|
|
2421
|
+
* - Functions that use intrinsics must be enclosed in an "enabled"
|
|
2422
|
+
* region (between BR_TARGETS_X86_UP and BR_TARGETS_X86_DOWN).
|
|
2423
|
+
* - Functions that use intrinsics must be tagged with the appropriate
|
|
2424
|
+
* BR_TARGET().
|
|
2425
|
+
*/
|
|
2426
|
+
|
|
2427
|
+
#if BR_ENABLE_INTRINSICS && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
|
|
2428
|
+
|
|
2429
|
+
/*
|
|
2430
|
+
* x86 intrinsics (both 32-bit and 64-bit).
|
|
2431
|
+
*/
|
|
2432
|
+
#if BR_i386 || BR_amd64
|
|
2433
|
+
|
|
2434
|
+
/*
|
|
2435
|
+
* On GCC before version 5.0, we need to use the pragma to enable the
|
|
2436
|
+
* target options globally, because the 'target' function attribute
|
|
2437
|
+
* appears to be unreliable. Before 4.6 we must also avoid the
|
|
2438
|
+
* push_options / pop_options mechanism, because it tends to trigger
|
|
2439
|
+
* some internal compiler errors.
|
|
2440
|
+
*/
|
|
2441
|
+
#if BR_GCC && !BR_GCC_5_0
|
|
2442
|
+
#if BR_GCC_4_6
|
|
2443
|
+
#define BR_TARGETS_X86_UP \
|
|
2444
|
+
_Pragma("GCC push_options") \
|
|
2445
|
+
_Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul,rdrnd\")")
|
|
2446
|
+
#define BR_TARGETS_X86_DOWN \
|
|
2447
|
+
_Pragma("GCC pop_options")
|
|
2448
|
+
#else
|
|
2449
|
+
#define BR_TARGETS_X86_UP \
|
|
2450
|
+
_Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul\")")
|
|
2451
|
+
#endif
|
|
2452
|
+
#define BR_TARGETS_X86_DOWN
|
|
2453
|
+
#pragma GCC diagnostic ignored "-Wpsabi"
|
|
2454
|
+
#endif
|
|
2455
|
+
|
|
2456
|
+
#if BR_CLANG && !BR_CLANG_3_8
|
|
2457
|
+
#undef __SSE2__
|
|
2458
|
+
#undef __SSE3__
|
|
2459
|
+
#undef __SSSE3__
|
|
2460
|
+
#undef __SSE4_1__
|
|
2461
|
+
#undef __AES__
|
|
2462
|
+
#undef __PCLMUL__
|
|
2463
|
+
#undef __RDRND__
|
|
2464
|
+
#define __SSE2__ 1
|
|
2465
|
+
#define __SSE3__ 1
|
|
2466
|
+
#define __SSSE3__ 1
|
|
2467
|
+
#define __SSE4_1__ 1
|
|
2468
|
+
#define __AES__ 1
|
|
2469
|
+
#define __PCLMUL__ 1
|
|
2470
|
+
#define __RDRND__ 1
|
|
2471
|
+
#endif
|
|
2472
|
+
|
|
2473
|
+
#ifndef BR_TARGETS_X86_UP
|
|
2474
|
+
#define BR_TARGETS_X86_UP
|
|
2475
|
+
#endif
|
|
2476
|
+
#ifndef BR_TARGETS_X86_DOWN
|
|
2477
|
+
#define BR_TARGETS_X86_DOWN
|
|
2478
|
+
#endif
|
|
2479
|
+
|
|
2480
|
+
#if BR_GCC || BR_CLANG
|
|
2481
|
+
BR_TARGETS_X86_UP
|
|
2482
|
+
#include <x86intrin.h>
|
|
2483
|
+
#include <cpuid.h>
|
|
2484
|
+
#define br_bswap32 __builtin_bswap32
|
|
2485
|
+
BR_TARGETS_X86_DOWN
|
|
2486
|
+
#endif
|
|
2487
|
+
|
|
2488
|
+
#if BR_MSC
|
|
2489
|
+
#include <stdlib.h>
|
|
2490
|
+
#include <intrin.h>
|
|
2491
|
+
#include <immintrin.h>
|
|
2492
|
+
#define br_bswap32 _byteswap_ulong
|
|
2493
|
+
#endif
|
|
2494
|
+
|
|
2495
|
+
static inline int
|
|
2496
|
+
br_cpuid(uint32_t mask_eax, uint32_t mask_ebx,
|
|
2497
|
+
uint32_t mask_ecx, uint32_t mask_edx)
|
|
2498
|
+
{
|
|
2499
|
+
#if BR_GCC || BR_CLANG
|
|
2500
|
+
unsigned eax, ebx, ecx, edx;
|
|
2501
|
+
|
|
2502
|
+
if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
|
|
2503
|
+
if ((eax & mask_eax) == mask_eax
|
|
2504
|
+
&& (ebx & mask_ebx) == mask_ebx
|
|
2505
|
+
&& (ecx & mask_ecx) == mask_ecx
|
|
2506
|
+
&& (edx & mask_edx) == mask_edx)
|
|
2507
|
+
{
|
|
2508
|
+
return 1;
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
#elif BR_MSC
|
|
2512
|
+
int info[4];
|
|
2513
|
+
|
|
2514
|
+
__cpuid(info, 1);
|
|
2515
|
+
if (((uint32_t)info[0] & mask_eax) == mask_eax
|
|
2516
|
+
&& ((uint32_t)info[1] & mask_ebx) == mask_ebx
|
|
2517
|
+
&& ((uint32_t)info[2] & mask_ecx) == mask_ecx
|
|
2518
|
+
&& ((uint32_t)info[3] & mask_edx) == mask_edx)
|
|
2519
|
+
{
|
|
2520
|
+
return 1;
|
|
2521
|
+
}
|
|
2522
|
+
#endif
|
|
2523
|
+
return 0;
|
|
2524
|
+
}
|
|
2525
|
+
|
|
2526
|
+
#endif
|
|
2527
|
+
|
|
2528
|
+
#endif
|
|
2529
|
+
|
|
2530
|
+
/* ==================================================================== */
|
|
2531
|
+
|
|
2532
|
+
#endif
|