10957b409SSimon J. Gerraty /*
20957b409SSimon J. Gerraty * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
30957b409SSimon J. Gerraty *
40957b409SSimon J. Gerraty * Permission is hereby granted, free of charge, to any person obtaining
50957b409SSimon J. Gerraty * a copy of this software and associated documentation files (the
60957b409SSimon J. Gerraty * "Software"), to deal in the Software without restriction, including
70957b409SSimon J. Gerraty * without limitation the rights to use, copy, modify, merge, publish,
80957b409SSimon J. Gerraty * distribute, sublicense, and/or sell copies of the Software, and to
90957b409SSimon J. Gerraty * permit persons to whom the Software is furnished to do so, subject to
100957b409SSimon J. Gerraty * the following conditions:
110957b409SSimon J. Gerraty *
120957b409SSimon J. Gerraty * The above copyright notice and this permission notice shall be
130957b409SSimon J. Gerraty * included in all copies or substantial portions of the Software.
140957b409SSimon J. Gerraty *
150957b409SSimon J. Gerraty * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
160957b409SSimon J. Gerraty * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
170957b409SSimon J. Gerraty * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
180957b409SSimon J. Gerraty * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
190957b409SSimon J. Gerraty * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
200957b409SSimon J. Gerraty * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
210957b409SSimon J. Gerraty * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
220957b409SSimon J. Gerraty * SOFTWARE.
230957b409SSimon J. Gerraty */
240957b409SSimon J. Gerraty
250957b409SSimon J. Gerraty #ifndef INNER_H__
260957b409SSimon J. Gerraty #define INNER_H__
270957b409SSimon J. Gerraty
280957b409SSimon J. Gerraty #include <string.h>
290957b409SSimon J. Gerraty #include <limits.h>
300957b409SSimon J. Gerraty
310957b409SSimon J. Gerraty #include "config.h"
320957b409SSimon J. Gerraty #include "bearssl.h"
330957b409SSimon J. Gerraty
340957b409SSimon J. Gerraty /*
350957b409SSimon J. Gerraty * On MSVC, disable the warning about applying unary minus on an
360957b409SSimon J. Gerraty * unsigned type: it is standard, we do it all the time, and for
370957b409SSimon J. Gerraty * good reasons.
380957b409SSimon J. Gerraty */
390957b409SSimon J. Gerraty #if _MSC_VER
400957b409SSimon J. Gerraty #pragma warning( disable : 4146 )
410957b409SSimon J. Gerraty #endif
420957b409SSimon J. Gerraty
430957b409SSimon J. Gerraty /*
440957b409SSimon J. Gerraty * Maximum size for a RSA modulus (in bits). Allocated stack buffers
450957b409SSimon J. Gerraty * depend on that size, so this value should be kept small. Currently,
460957b409SSimon J. Gerraty * 2048-bit RSA keys offer adequate security, and should still do so for
470957b409SSimon J. Gerraty * the next few decades; however, a number of widespread PKI have
480957b409SSimon J. Gerraty * already set their root keys to RSA-4096, so we should be able to
490957b409SSimon J. Gerraty * process such keys.
500957b409SSimon J. Gerraty *
510957b409SSimon J. Gerraty * This value MUST be a multiple of 64. This value MUST NOT exceed 47666
520957b409SSimon J. Gerraty * (some computations in RSA key generation rely on the factor size being
530957b409SSimon J. Gerraty * no more than 23833 bits). RSA key sizes beyond 3072 bits don't make a
540957b409SSimon J. Gerraty * lot of sense anyway.
550957b409SSimon J. Gerraty */
560957b409SSimon J. Gerraty #define BR_MAX_RSA_SIZE 4096
570957b409SSimon J. Gerraty
580957b409SSimon J. Gerraty /*
590957b409SSimon J. Gerraty * Minimum size for a RSA modulus (in bits); this value is used only to
600957b409SSimon J. Gerraty * filter out invalid parameters for key pair generation. Normally,
610957b409SSimon J. Gerraty * applications should not use RSA keys smaller than 2048 bits; but some
620957b409SSimon J. Gerraty * specific cases might need shorter keys, for legacy or research
630957b409SSimon J. Gerraty * purposes.
640957b409SSimon J. Gerraty */
650957b409SSimon J. Gerraty #define BR_MIN_RSA_SIZE 512
660957b409SSimon J. Gerraty
670957b409SSimon J. Gerraty /*
680957b409SSimon J. Gerraty * Maximum size for a RSA factor (in bits). This is for RSA private-key
690957b409SSimon J. Gerraty * operations. Default is to support factors up to a bit more than half
700957b409SSimon J. Gerraty * the maximum modulus size.
710957b409SSimon J. Gerraty *
720957b409SSimon J. Gerraty * This value MUST be a multiple of 32.
730957b409SSimon J. Gerraty */
740957b409SSimon J. Gerraty #define BR_MAX_RSA_FACTOR ((BR_MAX_RSA_SIZE + 64) >> 1)
750957b409SSimon J. Gerraty
760957b409SSimon J. Gerraty /*
770957b409SSimon J. Gerraty * Maximum size for an EC curve (modulus or order), in bits. Size of
780957b409SSimon J. Gerraty * stack buffers depends on that parameter. This size MUST be a multiple
790957b409SSimon J. Gerraty * of 8 (so that decoding an integer with that many bytes does not
800957b409SSimon J. Gerraty * overflow).
810957b409SSimon J. Gerraty */
820957b409SSimon J. Gerraty #define BR_MAX_EC_SIZE 528
830957b409SSimon J. Gerraty
840957b409SSimon J. Gerraty /*
850957b409SSimon J. Gerraty * Some macros to recognize the current architecture. Right now, we are
860957b409SSimon J. Gerraty * interested into automatically recognizing architecture with efficient
870957b409SSimon J. Gerraty * 64-bit types so that we may automatically use implementations that
880957b409SSimon J. Gerraty * use 64-bit registers in that case. Future versions may detect, e.g.,
890957b409SSimon J. Gerraty * availability of SSE2 intrinsics.
900957b409SSimon J. Gerraty *
910957b409SSimon J. Gerraty * If 'unsigned long' is a 64-bit type, then we assume that 64-bit types
920957b409SSimon J. Gerraty * are efficient. Otherwise, we rely on macros that depend on compiler,
930957b409SSimon J. Gerraty * OS and architecture. In any case, failure to detect the architecture
940957b409SSimon J. Gerraty * as 64-bit means that the 32-bit code will be used, and that code
950957b409SSimon J. Gerraty * works also on 64-bit architectures (the 64-bit code may simply be
960957b409SSimon J. Gerraty * more efficient).
970957b409SSimon J. Gerraty *
980957b409SSimon J. Gerraty * The test on 'unsigned long' should already catch most cases, the one
990957b409SSimon J. Gerraty * notable exception being Windows code where 'unsigned long' is kept to
1000957b409SSimon J. Gerraty * 32-bit for compatibility with all the legacy code that liberally uses
1010957b409SSimon J. Gerraty * the 'DWORD' type for 32-bit values.
1020957b409SSimon J. Gerraty *
1030957b409SSimon J. Gerraty * Macro names are taken from: http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
1040957b409SSimon J. Gerraty */
1050957b409SSimon J. Gerraty #ifndef BR_64
1060957b409SSimon J. Gerraty #if ((ULONG_MAX >> 31) >> 31) == 3
1070957b409SSimon J. Gerraty #define BR_64 1
1080957b409SSimon J. Gerraty #elif defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
1090957b409SSimon J. Gerraty #define BR_64 1
1100957b409SSimon J. Gerraty #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \
1110957b409SSimon J. Gerraty || defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
1120957b409SSimon J. Gerraty #define BR_64 1
1130957b409SSimon J. Gerraty #elif defined(__sparc64__)
1140957b409SSimon J. Gerraty #define BR_64 1
1150957b409SSimon J. Gerraty #elif defined(__x86_64__) || defined(_M_X64)
1160957b409SSimon J. Gerraty #define BR_64 1
1170957b409SSimon J. Gerraty #elif defined(__aarch64__) || defined(_M_ARM64)
1180957b409SSimon J. Gerraty #define BR_64 1
1190957b409SSimon J. Gerraty #elif defined(__mips64)
1200957b409SSimon J. Gerraty #define BR_64 1
1210957b409SSimon J. Gerraty #endif
1220957b409SSimon J. Gerraty #endif
1230957b409SSimon J. Gerraty
1240957b409SSimon J. Gerraty /*
1250957b409SSimon J. Gerraty * Set BR_LOMUL on platforms where it makes sense.
1260957b409SSimon J. Gerraty */
1270957b409SSimon J. Gerraty #ifndef BR_LOMUL
1280957b409SSimon J. Gerraty #if BR_ARMEL_CORTEXM_GCC
1290957b409SSimon J. Gerraty #define BR_LOMUL 1
1300957b409SSimon J. Gerraty #endif
1310957b409SSimon J. Gerraty #endif
1320957b409SSimon J. Gerraty
1330957b409SSimon J. Gerraty /*
1340957b409SSimon J. Gerraty * Architecture detection.
1350957b409SSimon J. Gerraty */
1360957b409SSimon J. Gerraty #ifndef BR_i386
1370957b409SSimon J. Gerraty #if __i386__ || _M_IX86
1380957b409SSimon J. Gerraty #define BR_i386 1
1390957b409SSimon J. Gerraty #endif
1400957b409SSimon J. Gerraty #endif
1410957b409SSimon J. Gerraty
1420957b409SSimon J. Gerraty #ifndef BR_amd64
1430957b409SSimon J. Gerraty #if __x86_64__ || _M_X64
1440957b409SSimon J. Gerraty #define BR_amd64 1
1450957b409SSimon J. Gerraty #endif
1460957b409SSimon J. Gerraty #endif
1470957b409SSimon J. Gerraty
1480957b409SSimon J. Gerraty /*
1490957b409SSimon J. Gerraty * Compiler brand and version.
1500957b409SSimon J. Gerraty *
1510957b409SSimon J. Gerraty * Implementations that use intrinsics need to detect the compiler type
1520957b409SSimon J. Gerraty * and version because some specific actions may be needed to activate
1530957b409SSimon J. Gerraty * the corresponding opcodes, both for header inclusion, and when using
1540957b409SSimon J. Gerraty * them in a function.
1550957b409SSimon J. Gerraty *
1560957b409SSimon J. Gerraty * BR_GCC, BR_CLANG and BR_MSC will be set to 1 for, respectively, GCC,
1570957b409SSimon J. Gerraty * Clang and MS Visual C. For each of them, sub-macros will be defined
1580957b409SSimon J. Gerraty * for versions; each sub-macro is set whenever the compiler version is
1590957b409SSimon J. Gerraty * at least as recent as the one corresponding to the macro.
1600957b409SSimon J. Gerraty */
1610957b409SSimon J. Gerraty
1620957b409SSimon J. Gerraty /*
1630957b409SSimon J. Gerraty * GCC thresholds are on versions 4.4 to 4.9 and 5.0.
1640957b409SSimon J. Gerraty */
1650957b409SSimon J. Gerraty #ifndef BR_GCC
1660957b409SSimon J. Gerraty #if __GNUC__ && !__clang__
1670957b409SSimon J. Gerraty #define BR_GCC 1
1680957b409SSimon J. Gerraty
1690957b409SSimon J. Gerraty #if __GNUC__ > 4
1700957b409SSimon J. Gerraty #define BR_GCC_5_0 1
1710957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 9
1720957b409SSimon J. Gerraty #define BR_GCC_4_9 1
1730957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 8
1740957b409SSimon J. Gerraty #define BR_GCC_4_8 1
1750957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 7
1760957b409SSimon J. Gerraty #define BR_GCC_4_7 1
1770957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 6
1780957b409SSimon J. Gerraty #define BR_GCC_4_6 1
1790957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 5
1800957b409SSimon J. Gerraty #define BR_GCC_4_5 1
1810957b409SSimon J. Gerraty #elif __GNUC__ == 4 && __GNUC_MINOR__ >= 4
1820957b409SSimon J. Gerraty #define BR_GCC_4_4 1
1830957b409SSimon J. Gerraty #endif
1840957b409SSimon J. Gerraty
1850957b409SSimon J. Gerraty #if BR_GCC_5_0
1860957b409SSimon J. Gerraty #define BR_GCC_4_9 1
1870957b409SSimon J. Gerraty #endif
1880957b409SSimon J. Gerraty #if BR_GCC_4_9
1890957b409SSimon J. Gerraty #define BR_GCC_4_8 1
1900957b409SSimon J. Gerraty #endif
1910957b409SSimon J. Gerraty #if BR_GCC_4_8
1920957b409SSimon J. Gerraty #define BR_GCC_4_7 1
1930957b409SSimon J. Gerraty #endif
1940957b409SSimon J. Gerraty #if BR_GCC_4_7
1950957b409SSimon J. Gerraty #define BR_GCC_4_6 1
1960957b409SSimon J. Gerraty #endif
1970957b409SSimon J. Gerraty #if BR_GCC_4_6
1980957b409SSimon J. Gerraty #define BR_GCC_4_5 1
1990957b409SSimon J. Gerraty #endif
2000957b409SSimon J. Gerraty #if BR_GCC_4_5
2010957b409SSimon J. Gerraty #define BR_GCC_4_4 1
2020957b409SSimon J. Gerraty #endif
2030957b409SSimon J. Gerraty
2040957b409SSimon J. Gerraty #endif
2050957b409SSimon J. Gerraty #endif
2060957b409SSimon J. Gerraty
2070957b409SSimon J. Gerraty /*
2080957b409SSimon J. Gerraty * Clang thresholds are on versions 3.7.0 and 3.8.0.
2090957b409SSimon J. Gerraty */
2100957b409SSimon J. Gerraty #ifndef BR_CLANG
2110957b409SSimon J. Gerraty #if __clang__
2120957b409SSimon J. Gerraty #define BR_CLANG 1
2130957b409SSimon J. Gerraty
2140957b409SSimon J. Gerraty #if __clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 8)
2150957b409SSimon J. Gerraty #define BR_CLANG_3_8 1
2160957b409SSimon J. Gerraty #elif __clang_major__ == 3 && __clang_minor__ >= 7
2170957b409SSimon J. Gerraty #define BR_CLANG_3_7 1
2180957b409SSimon J. Gerraty #endif
2190957b409SSimon J. Gerraty
2200957b409SSimon J. Gerraty #if BR_CLANG_3_8
2210957b409SSimon J. Gerraty #define BR_CLANG_3_7 1
2220957b409SSimon J. Gerraty #endif
2230957b409SSimon J. Gerraty
2240957b409SSimon J. Gerraty #endif
2250957b409SSimon J. Gerraty #endif
2260957b409SSimon J. Gerraty
2270957b409SSimon J. Gerraty /*
2280957b409SSimon J. Gerraty * MS Visual C thresholds are on Visual Studio 2005 to 2015.
2290957b409SSimon J. Gerraty */
2300957b409SSimon J. Gerraty #ifndef BR_MSC
2310957b409SSimon J. Gerraty #if _MSC_VER
2320957b409SSimon J. Gerraty #define BR_MSC 1
2330957b409SSimon J. Gerraty
2340957b409SSimon J. Gerraty #if _MSC_VER >= 1900
2350957b409SSimon J. Gerraty #define BR_MSC_2015 1
2360957b409SSimon J. Gerraty #elif _MSC_VER >= 1800
2370957b409SSimon J. Gerraty #define BR_MSC_2013 1
2380957b409SSimon J. Gerraty #elif _MSC_VER >= 1700
2390957b409SSimon J. Gerraty #define BR_MSC_2012 1
2400957b409SSimon J. Gerraty #elif _MSC_VER >= 1600
2410957b409SSimon J. Gerraty #define BR_MSC_2010 1
2420957b409SSimon J. Gerraty #elif _MSC_VER >= 1500
2430957b409SSimon J. Gerraty #define BR_MSC_2008 1
2440957b409SSimon J. Gerraty #elif _MSC_VER >= 1400
2450957b409SSimon J. Gerraty #define BR_MSC_2005 1
2460957b409SSimon J. Gerraty #endif
2470957b409SSimon J. Gerraty
2480957b409SSimon J. Gerraty #if BR_MSC_2015
2490957b409SSimon J. Gerraty #define BR_MSC_2013 1
2500957b409SSimon J. Gerraty #endif
2510957b409SSimon J. Gerraty #if BR_MSC_2013
2520957b409SSimon J. Gerraty #define BR_MSC_2012 1
2530957b409SSimon J. Gerraty #endif
2540957b409SSimon J. Gerraty #if BR_MSC_2012
2550957b409SSimon J. Gerraty #define BR_MSC_2010 1
2560957b409SSimon J. Gerraty #endif
2570957b409SSimon J. Gerraty #if BR_MSC_2010
2580957b409SSimon J. Gerraty #define BR_MSC_2008 1
2590957b409SSimon J. Gerraty #endif
2600957b409SSimon J. Gerraty #if BR_MSC_2008
2610957b409SSimon J. Gerraty #define BR_MSC_2005 1
2620957b409SSimon J. Gerraty #endif
2630957b409SSimon J. Gerraty
2640957b409SSimon J. Gerraty #endif
2650957b409SSimon J. Gerraty #endif
2660957b409SSimon J. Gerraty
2670957b409SSimon J. Gerraty /*
2680957b409SSimon J. Gerraty * GCC 4.4+ and Clang 3.7+ allow tagging specific functions with a
2690957b409SSimon J. Gerraty * 'target' attribute that activates support for specific opcodes.
2700957b409SSimon J. Gerraty */
2710957b409SSimon J. Gerraty #if BR_GCC_4_4 || BR_CLANG_3_7
2720957b409SSimon J. Gerraty #define BR_TARGET(x) __attribute__((target(x)))
2730957b409SSimon J. Gerraty #else
2740957b409SSimon J. Gerraty #define BR_TARGET(x)
2750957b409SSimon J. Gerraty #endif
2760957b409SSimon J. Gerraty
2770957b409SSimon J. Gerraty /*
2780957b409SSimon J. Gerraty * AES-NI intrinsics are available on x86 (32-bit and 64-bit) with
2790957b409SSimon J. Gerraty * GCC 4.8+, Clang 3.7+ and MSC 2012+.
2800957b409SSimon J. Gerraty */
2810957b409SSimon J. Gerraty #ifndef BR_AES_X86NI
2820957b409SSimon J. Gerraty #if (BR_i386 || BR_amd64) && (BR_GCC_4_8 || BR_CLANG_3_7 || BR_MSC_2012)
2830957b409SSimon J. Gerraty #define BR_AES_X86NI 1
2840957b409SSimon J. Gerraty #endif
2850957b409SSimon J. Gerraty #endif
2860957b409SSimon J. Gerraty
2870957b409SSimon J. Gerraty /*
2880957b409SSimon J. Gerraty * SSE2 intrinsics are available on x86 (32-bit and 64-bit) with
2890957b409SSimon J. Gerraty * GCC 4.4+, Clang 3.7+ and MSC 2005+.
2900957b409SSimon J. Gerraty */
2910957b409SSimon J. Gerraty #ifndef BR_SSE2
2920957b409SSimon J. Gerraty #if (BR_i386 || BR_amd64) && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
2930957b409SSimon J. Gerraty #define BR_SSE2 1
2940957b409SSimon J. Gerraty #endif
2950957b409SSimon J. Gerraty #endif
2960957b409SSimon J. Gerraty
2970957b409SSimon J. Gerraty /*
2980957b409SSimon J. Gerraty * RDRAND intrinsics are available on x86 (32-bit and 64-bit) with
2990957b409SSimon J. Gerraty * GCC 4.6+, Clang 3.7+ and MSC 2012+.
3000957b409SSimon J. Gerraty */
3010957b409SSimon J. Gerraty #ifndef BR_RDRAND
3020957b409SSimon J. Gerraty #if (BR_i386 || BR_amd64) && (BR_GCC_4_6 || BR_CLANG_3_7 || BR_MSC_2012)
3030957b409SSimon J. Gerraty #define BR_RDRAND 1
3040957b409SSimon J. Gerraty #endif
3050957b409SSimon J. Gerraty #endif
3060957b409SSimon J. Gerraty
3070957b409SSimon J. Gerraty /*
3080957b409SSimon J. Gerraty * Determine type of OS for random number generation. Macro names and
3090957b409SSimon J. Gerraty * values are documented on:
3100957b409SSimon J. Gerraty * https://sourceforge.net/p/predef/wiki/OperatingSystems/
3110957b409SSimon J. Gerraty *
312*cc9e6590SSimon J. Gerraty * Win32's CryptGenRandom() should be available on Windows systems.
313*cc9e6590SSimon J. Gerraty *
314*cc9e6590SSimon J. Gerraty * /dev/urandom should work on all Unix-like systems (including macOS X).
315*cc9e6590SSimon J. Gerraty *
316*cc9e6590SSimon J. Gerraty * getentropy() is present on Linux (Glibc 2.25+), FreeBSD (12.0+) and
317*cc9e6590SSimon J. Gerraty * OpenBSD (5.6+). For OpenBSD, there does not seem to be easy to use
318*cc9e6590SSimon J. Gerraty * macros to test the minimum version, so we just assume that it is
319*cc9e6590SSimon J. Gerraty * recent enough (last version without getentropy() has gone out of
320*cc9e6590SSimon J. Gerraty * support in May 2015).
321*cc9e6590SSimon J. Gerraty *
322*cc9e6590SSimon J. Gerraty * Ideally we should use getentropy() on macOS (10.12+) too, but I don't
323*cc9e6590SSimon J. Gerraty * know how to test the exact OS version with preprocessor macros.
324*cc9e6590SSimon J. Gerraty *
325*cc9e6590SSimon J. Gerraty * TODO: enrich the list of detected system.
3260957b409SSimon J. Gerraty */
3270957b409SSimon J. Gerraty
3280957b409SSimon J. Gerraty #ifndef BR_USE_URANDOM
3290957b409SSimon J. Gerraty #if defined _AIX \
3300957b409SSimon J. Gerraty || defined __ANDROID__ \
3310957b409SSimon J. Gerraty || defined __FreeBSD__ \
3320957b409SSimon J. Gerraty || defined __NetBSD__ \
3330957b409SSimon J. Gerraty || defined __OpenBSD__ \
3340957b409SSimon J. Gerraty || defined __DragonFly__ \
3350957b409SSimon J. Gerraty || defined __linux__ \
3360957b409SSimon J. Gerraty || (defined __sun && (defined __SVR4 || defined __svr4__)) \
3370957b409SSimon J. Gerraty || (defined __APPLE__ && defined __MACH__)
3380957b409SSimon J. Gerraty #define BR_USE_URANDOM 1
3390957b409SSimon J. Gerraty #endif
3400957b409SSimon J. Gerraty #endif
3410957b409SSimon J. Gerraty
342*cc9e6590SSimon J. Gerraty #ifndef BR_USE_GETENTROPY
343*cc9e6590SSimon J. Gerraty #if (defined __linux__ \
344*cc9e6590SSimon J. Gerraty && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))) \
345*cc9e6590SSimon J. Gerraty || (defined __FreeBSD__ && __FreeBSD__ >= 12) \
346*cc9e6590SSimon J. Gerraty || defined __OpenBSD__
347*cc9e6590SSimon J. Gerraty #define BR_USE_GETENTROPY 1
348*cc9e6590SSimon J. Gerraty #endif
349*cc9e6590SSimon J. Gerraty #endif
350*cc9e6590SSimon J. Gerraty
3510957b409SSimon J. Gerraty #ifndef BR_USE_WIN32_RAND
3520957b409SSimon J. Gerraty #if defined _WIN32 || defined _WIN64
3530957b409SSimon J. Gerraty #define BR_USE_WIN32_RAND 1
3540957b409SSimon J. Gerraty #endif
3550957b409SSimon J. Gerraty #endif
3560957b409SSimon J. Gerraty
3570957b409SSimon J. Gerraty /*
3580957b409SSimon J. Gerraty * POWER8 crypto support. We rely on compiler macros for the
3590957b409SSimon J. Gerraty * architecture, since we do not have a reliable, simple way to detect
3600957b409SSimon J. Gerraty * the required support at runtime (we could try running an opcode, and
3610957b409SSimon J. Gerraty * trapping the exception or signal on illegal instruction, but this
3620957b409SSimon J. Gerraty * induces some non-trivial OS dependencies that we would prefer to
3630957b409SSimon J. Gerraty * avoid if possible).
3640957b409SSimon J. Gerraty */
3650957b409SSimon J. Gerraty #ifndef BR_POWER8
3660957b409SSimon J. Gerraty #if __GNUC__ && ((_ARCH_PWR8 || _ARCH_PPC) && __CRYPTO__)
3670957b409SSimon J. Gerraty #define BR_POWER8 1
3680957b409SSimon J. Gerraty #endif
3690957b409SSimon J. Gerraty #endif
3700957b409SSimon J. Gerraty
3710957b409SSimon J. Gerraty /*
3720957b409SSimon J. Gerraty * Detect endinanness on POWER8.
3730957b409SSimon J. Gerraty */
3740957b409SSimon J. Gerraty #if BR_POWER8
3750957b409SSimon J. Gerraty #if defined BR_POWER8_LE
3760957b409SSimon J. Gerraty #undef BR_POWER8_BE
3770957b409SSimon J. Gerraty #if BR_POWER8_LE
3780957b409SSimon J. Gerraty #define BR_POWER8_BE 0
3790957b409SSimon J. Gerraty #else
3800957b409SSimon J. Gerraty #define BR_POWER8_BE 1
3810957b409SSimon J. Gerraty #endif
3820957b409SSimon J. Gerraty #elif defined BR_POWER8_BE
3830957b409SSimon J. Gerraty #undef BR_POWER8_LE
3840957b409SSimon J. Gerraty #if BR_POWER8_BE
3850957b409SSimon J. Gerraty #define BR_POWER8_LE 0
3860957b409SSimon J. Gerraty #else
3870957b409SSimon J. Gerraty #define BR_POWER8_LE 1
3880957b409SSimon J. Gerraty #endif
3890957b409SSimon J. Gerraty #else
3900957b409SSimon J. Gerraty #if __LITTLE_ENDIAN__
3910957b409SSimon J. Gerraty #define BR_POWER8_LE 1
3920957b409SSimon J. Gerraty #define BR_POWER8_BE 0
3930957b409SSimon J. Gerraty #else
3940957b409SSimon J. Gerraty #define BR_POWER8_LE 0
3950957b409SSimon J. Gerraty #define BR_POWER8_BE 1
3960957b409SSimon J. Gerraty #endif
3970957b409SSimon J. Gerraty #endif
3980957b409SSimon J. Gerraty #endif
3990957b409SSimon J. Gerraty
4000957b409SSimon J. Gerraty /*
4010957b409SSimon J. Gerraty * Detect support for 128-bit integers.
4020957b409SSimon J. Gerraty */
4030957b409SSimon J. Gerraty #if !defined BR_INT128 && !defined BR_UMUL128
4040957b409SSimon J. Gerraty #ifdef __SIZEOF_INT128__
4050957b409SSimon J. Gerraty #define BR_INT128 1
4060957b409SSimon J. Gerraty #elif _M_X64
4070957b409SSimon J. Gerraty #define BR_UMUL128 1
4080957b409SSimon J. Gerraty #endif
4090957b409SSimon J. Gerraty #endif
4100957b409SSimon J. Gerraty
4110957b409SSimon J. Gerraty /*
4120957b409SSimon J. Gerraty * Detect support for unaligned accesses with known endianness.
4130957b409SSimon J. Gerraty *
4140957b409SSimon J. Gerraty * x86 (both 32-bit and 64-bit) is little-endian and allows unaligned
4150957b409SSimon J. Gerraty * accesses.
4160957b409SSimon J. Gerraty *
4170957b409SSimon J. Gerraty * POWER/PowerPC allows unaligned accesses when big-endian. POWER8 and
4180957b409SSimon J. Gerraty * later also allow unaligned accesses when little-endian.
4190957b409SSimon J. Gerraty */
4200957b409SSimon J. Gerraty #if !defined BR_LE_UNALIGNED && !defined BR_BE_UNALIGNED
4210957b409SSimon J. Gerraty
4220957b409SSimon J. Gerraty #if __i386 || __i386__ || __x86_64__ || _M_IX86 || _M_X64
4230957b409SSimon J. Gerraty #define BR_LE_UNALIGNED 1
4240957b409SSimon J. Gerraty #elif BR_POWER8_BE
4250957b409SSimon J. Gerraty #define BR_BE_UNALIGNED 1
4260957b409SSimon J. Gerraty #elif BR_POWER8_LE
4270957b409SSimon J. Gerraty #define BR_LE_UNALIGNED 1
4280957b409SSimon J. Gerraty #elif (__powerpc__ || __powerpc64__ || _M_PPC || _ARCH_PPC || _ARCH_PPC64) \
4290957b409SSimon J. Gerraty && __BIG_ENDIAN__
4300957b409SSimon J. Gerraty #define BR_BE_UNALIGNED 1
4310957b409SSimon J. Gerraty #endif
4320957b409SSimon J. Gerraty
4330957b409SSimon J. Gerraty #endif
4340957b409SSimon J. Gerraty
4350957b409SSimon J. Gerraty /*
4360957b409SSimon J. Gerraty * Detect support for an OS-provided time source.
4370957b409SSimon J. Gerraty */
4380957b409SSimon J. Gerraty
4390957b409SSimon J. Gerraty #ifndef BR_USE_UNIX_TIME
4400957b409SSimon J. Gerraty #if defined __unix__ || defined __linux__ \
4410957b409SSimon J. Gerraty || defined _POSIX_SOURCE || defined _POSIX_C_SOURCE \
4420957b409SSimon J. Gerraty || (defined __APPLE__ && defined __MACH__)
4430957b409SSimon J. Gerraty #define BR_USE_UNIX_TIME 1
4440957b409SSimon J. Gerraty #endif
4450957b409SSimon J. Gerraty #endif
4460957b409SSimon J. Gerraty
4470957b409SSimon J. Gerraty #ifndef BR_USE_WIN32_TIME
4480957b409SSimon J. Gerraty #if defined _WIN32 || defined _WIN64
4490957b409SSimon J. Gerraty #define BR_USE_WIN32_TIME 1
4500957b409SSimon J. Gerraty #endif
4510957b409SSimon J. Gerraty #endif
4520957b409SSimon J. Gerraty
4530957b409SSimon J. Gerraty /* ==================================================================== */
4540957b409SSimon J. Gerraty /*
4550957b409SSimon J. Gerraty * Encoding/decoding functions.
4560957b409SSimon J. Gerraty *
4570957b409SSimon J. Gerraty * 32-bit and 64-bit decoding, both little-endian and big-endian, is
4580957b409SSimon J. Gerraty * implemented with the inline functions below.
4590957b409SSimon J. Gerraty *
4600957b409SSimon J. Gerraty * When allowed by some compile-time options (autodetected or provided),
4610957b409SSimon J. Gerraty * optimised code is used, to perform direct memory access when the
4620957b409SSimon J. Gerraty * underlying architecture supports it, both for endianness and
4630957b409SSimon J. Gerraty * alignment. This, however, may trigger strict aliasing issues; the
4640957b409SSimon J. Gerraty * code below uses unions to perform (supposedly) safe type punning.
4650957b409SSimon J. Gerraty * Since the C aliasing rules are relatively complex and were amended,
4660957b409SSimon J. Gerraty * or at least re-explained with different phrasing, in all successive
4670957b409SSimon J. Gerraty * versions of the C standard, it is always a bit risky to bet that any
4680957b409SSimon J. Gerraty * specific version of a C compiler got it right, for some notion of
4690957b409SSimon J. Gerraty * "right".
4700957b409SSimon J. Gerraty */
4710957b409SSimon J. Gerraty
4720957b409SSimon J. Gerraty typedef union {
4730957b409SSimon J. Gerraty uint16_t u;
4740957b409SSimon J. Gerraty unsigned char b[sizeof(uint16_t)];
4750957b409SSimon J. Gerraty } br_union_u16;
4760957b409SSimon J. Gerraty
4770957b409SSimon J. Gerraty typedef union {
4780957b409SSimon J. Gerraty uint32_t u;
4790957b409SSimon J. Gerraty unsigned char b[sizeof(uint32_t)];
4800957b409SSimon J. Gerraty } br_union_u32;
4810957b409SSimon J. Gerraty
4820957b409SSimon J. Gerraty typedef union {
4830957b409SSimon J. Gerraty uint64_t u;
4840957b409SSimon J. Gerraty unsigned char b[sizeof(uint64_t)];
4850957b409SSimon J. Gerraty } br_union_u64;
4860957b409SSimon J. Gerraty
4870957b409SSimon J. Gerraty static inline void
br_enc16le(void * dst,unsigned x)4880957b409SSimon J. Gerraty br_enc16le(void *dst, unsigned x)
4890957b409SSimon J. Gerraty {
4900957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
4910957b409SSimon J. Gerraty ((br_union_u16 *)dst)->u = x;
4920957b409SSimon J. Gerraty #else
4930957b409SSimon J. Gerraty unsigned char *buf;
4940957b409SSimon J. Gerraty
4950957b409SSimon J. Gerraty buf = dst;
4960957b409SSimon J. Gerraty buf[0] = (unsigned char)x;
4970957b409SSimon J. Gerraty buf[1] = (unsigned char)(x >> 8);
4980957b409SSimon J. Gerraty #endif
4990957b409SSimon J. Gerraty }
5000957b409SSimon J. Gerraty
5010957b409SSimon J. Gerraty static inline void
br_enc16be(void * dst,unsigned x)5020957b409SSimon J. Gerraty br_enc16be(void *dst, unsigned x)
5030957b409SSimon J. Gerraty {
5040957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
5050957b409SSimon J. Gerraty ((br_union_u16 *)dst)->u = x;
5060957b409SSimon J. Gerraty #else
5070957b409SSimon J. Gerraty unsigned char *buf;
5080957b409SSimon J. Gerraty
5090957b409SSimon J. Gerraty buf = dst;
5100957b409SSimon J. Gerraty buf[0] = (unsigned char)(x >> 8);
5110957b409SSimon J. Gerraty buf[1] = (unsigned char)x;
5120957b409SSimon J. Gerraty #endif
5130957b409SSimon J. Gerraty }
5140957b409SSimon J. Gerraty
5150957b409SSimon J. Gerraty static inline unsigned
br_dec16le(const void * src)5160957b409SSimon J. Gerraty br_dec16le(const void *src)
5170957b409SSimon J. Gerraty {
5180957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
5190957b409SSimon J. Gerraty return ((const br_union_u16 *)src)->u;
5200957b409SSimon J. Gerraty #else
5210957b409SSimon J. Gerraty const unsigned char *buf;
5220957b409SSimon J. Gerraty
5230957b409SSimon J. Gerraty buf = src;
5240957b409SSimon J. Gerraty return (unsigned)buf[0] | ((unsigned)buf[1] << 8);
5250957b409SSimon J. Gerraty #endif
5260957b409SSimon J. Gerraty }
5270957b409SSimon J. Gerraty
5280957b409SSimon J. Gerraty static inline unsigned
br_dec16be(const void * src)5290957b409SSimon J. Gerraty br_dec16be(const void *src)
5300957b409SSimon J. Gerraty {
5310957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
5320957b409SSimon J. Gerraty return ((const br_union_u16 *)src)->u;
5330957b409SSimon J. Gerraty #else
5340957b409SSimon J. Gerraty const unsigned char *buf;
5350957b409SSimon J. Gerraty
5360957b409SSimon J. Gerraty buf = src;
5370957b409SSimon J. Gerraty return ((unsigned)buf[0] << 8) | (unsigned)buf[1];
5380957b409SSimon J. Gerraty #endif
5390957b409SSimon J. Gerraty }
5400957b409SSimon J. Gerraty
5410957b409SSimon J. Gerraty static inline void
br_enc32le(void * dst,uint32_t x)5420957b409SSimon J. Gerraty br_enc32le(void *dst, uint32_t x)
5430957b409SSimon J. Gerraty {
5440957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
5450957b409SSimon J. Gerraty ((br_union_u32 *)dst)->u = x;
5460957b409SSimon J. Gerraty #else
5470957b409SSimon J. Gerraty unsigned char *buf;
5480957b409SSimon J. Gerraty
5490957b409SSimon J. Gerraty buf = dst;
5500957b409SSimon J. Gerraty buf[0] = (unsigned char)x;
5510957b409SSimon J. Gerraty buf[1] = (unsigned char)(x >> 8);
5520957b409SSimon J. Gerraty buf[2] = (unsigned char)(x >> 16);
5530957b409SSimon J. Gerraty buf[3] = (unsigned char)(x >> 24);
5540957b409SSimon J. Gerraty #endif
5550957b409SSimon J. Gerraty }
5560957b409SSimon J. Gerraty
5570957b409SSimon J. Gerraty static inline void
br_enc32be(void * dst,uint32_t x)5580957b409SSimon J. Gerraty br_enc32be(void *dst, uint32_t x)
5590957b409SSimon J. Gerraty {
5600957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
5610957b409SSimon J. Gerraty ((br_union_u32 *)dst)->u = x;
5620957b409SSimon J. Gerraty #else
5630957b409SSimon J. Gerraty unsigned char *buf;
5640957b409SSimon J. Gerraty
5650957b409SSimon J. Gerraty buf = dst;
5660957b409SSimon J. Gerraty buf[0] = (unsigned char)(x >> 24);
5670957b409SSimon J. Gerraty buf[1] = (unsigned char)(x >> 16);
5680957b409SSimon J. Gerraty buf[2] = (unsigned char)(x >> 8);
5690957b409SSimon J. Gerraty buf[3] = (unsigned char)x;
5700957b409SSimon J. Gerraty #endif
5710957b409SSimon J. Gerraty }
5720957b409SSimon J. Gerraty
5730957b409SSimon J. Gerraty static inline uint32_t
br_dec32le(const void * src)5740957b409SSimon J. Gerraty br_dec32le(const void *src)
5750957b409SSimon J. Gerraty {
5760957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
5770957b409SSimon J. Gerraty return ((const br_union_u32 *)src)->u;
5780957b409SSimon J. Gerraty #else
5790957b409SSimon J. Gerraty const unsigned char *buf;
5800957b409SSimon J. Gerraty
5810957b409SSimon J. Gerraty buf = src;
5820957b409SSimon J. Gerraty return (uint32_t)buf[0]
5830957b409SSimon J. Gerraty | ((uint32_t)buf[1] << 8)
5840957b409SSimon J. Gerraty | ((uint32_t)buf[2] << 16)
5850957b409SSimon J. Gerraty | ((uint32_t)buf[3] << 24);
5860957b409SSimon J. Gerraty #endif
5870957b409SSimon J. Gerraty }
5880957b409SSimon J. Gerraty
5890957b409SSimon J. Gerraty static inline uint32_t
br_dec32be(const void * src)5900957b409SSimon J. Gerraty br_dec32be(const void *src)
5910957b409SSimon J. Gerraty {
5920957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
5930957b409SSimon J. Gerraty return ((const br_union_u32 *)src)->u;
5940957b409SSimon J. Gerraty #else
5950957b409SSimon J. Gerraty const unsigned char *buf;
5960957b409SSimon J. Gerraty
5970957b409SSimon J. Gerraty buf = src;
5980957b409SSimon J. Gerraty return ((uint32_t)buf[0] << 24)
5990957b409SSimon J. Gerraty | ((uint32_t)buf[1] << 16)
6000957b409SSimon J. Gerraty | ((uint32_t)buf[2] << 8)
6010957b409SSimon J. Gerraty | (uint32_t)buf[3];
6020957b409SSimon J. Gerraty #endif
6030957b409SSimon J. Gerraty }
6040957b409SSimon J. Gerraty
6050957b409SSimon J. Gerraty static inline void
br_enc64le(void * dst,uint64_t x)6060957b409SSimon J. Gerraty br_enc64le(void *dst, uint64_t x)
6070957b409SSimon J. Gerraty {
6080957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
6090957b409SSimon J. Gerraty ((br_union_u64 *)dst)->u = x;
6100957b409SSimon J. Gerraty #else
6110957b409SSimon J. Gerraty unsigned char *buf;
6120957b409SSimon J. Gerraty
6130957b409SSimon J. Gerraty buf = dst;
6140957b409SSimon J. Gerraty br_enc32le(buf, (uint32_t)x);
6150957b409SSimon J. Gerraty br_enc32le(buf + 4, (uint32_t)(x >> 32));
6160957b409SSimon J. Gerraty #endif
6170957b409SSimon J. Gerraty }
6180957b409SSimon J. Gerraty
6190957b409SSimon J. Gerraty static inline void
br_enc64be(void * dst,uint64_t x)6200957b409SSimon J. Gerraty br_enc64be(void *dst, uint64_t x)
6210957b409SSimon J. Gerraty {
6220957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
6230957b409SSimon J. Gerraty ((br_union_u64 *)dst)->u = x;
6240957b409SSimon J. Gerraty #else
6250957b409SSimon J. Gerraty unsigned char *buf;
6260957b409SSimon J. Gerraty
6270957b409SSimon J. Gerraty buf = dst;
6280957b409SSimon J. Gerraty br_enc32be(buf, (uint32_t)(x >> 32));
6290957b409SSimon J. Gerraty br_enc32be(buf + 4, (uint32_t)x);
6300957b409SSimon J. Gerraty #endif
6310957b409SSimon J. Gerraty }
6320957b409SSimon J. Gerraty
6330957b409SSimon J. Gerraty static inline uint64_t
br_dec64le(const void * src)6340957b409SSimon J. Gerraty br_dec64le(const void *src)
6350957b409SSimon J. Gerraty {
6360957b409SSimon J. Gerraty #if BR_LE_UNALIGNED
6370957b409SSimon J. Gerraty return ((const br_union_u64 *)src)->u;
6380957b409SSimon J. Gerraty #else
6390957b409SSimon J. Gerraty const unsigned char *buf;
6400957b409SSimon J. Gerraty
6410957b409SSimon J. Gerraty buf = src;
6420957b409SSimon J. Gerraty return (uint64_t)br_dec32le(buf)
6430957b409SSimon J. Gerraty | ((uint64_t)br_dec32le(buf + 4) << 32);
6440957b409SSimon J. Gerraty #endif
6450957b409SSimon J. Gerraty }
6460957b409SSimon J. Gerraty
6470957b409SSimon J. Gerraty static inline uint64_t
br_dec64be(const void * src)6480957b409SSimon J. Gerraty br_dec64be(const void *src)
6490957b409SSimon J. Gerraty {
6500957b409SSimon J. Gerraty #if BR_BE_UNALIGNED
6510957b409SSimon J. Gerraty return ((const br_union_u64 *)src)->u;
6520957b409SSimon J. Gerraty #else
6530957b409SSimon J. Gerraty const unsigned char *buf;
6540957b409SSimon J. Gerraty
6550957b409SSimon J. Gerraty buf = src;
6560957b409SSimon J. Gerraty return ((uint64_t)br_dec32be(buf) << 32)
6570957b409SSimon J. Gerraty | (uint64_t)br_dec32be(buf + 4);
6580957b409SSimon J. Gerraty #endif
6590957b409SSimon J. Gerraty }
6600957b409SSimon J. Gerraty
6610957b409SSimon J. Gerraty /*
6620957b409SSimon J. Gerraty * Range decoding and encoding (for several successive values).
6630957b409SSimon J. Gerraty */
6640957b409SSimon J. Gerraty void br_range_dec16le(uint16_t *v, size_t num, const void *src);
6650957b409SSimon J. Gerraty void br_range_dec16be(uint16_t *v, size_t num, const void *src);
6660957b409SSimon J. Gerraty void br_range_enc16le(void *dst, const uint16_t *v, size_t num);
6670957b409SSimon J. Gerraty void br_range_enc16be(void *dst, const uint16_t *v, size_t num);
6680957b409SSimon J. Gerraty
6690957b409SSimon J. Gerraty void br_range_dec32le(uint32_t *v, size_t num, const void *src);
6700957b409SSimon J. Gerraty void br_range_dec32be(uint32_t *v, size_t num, const void *src);
6710957b409SSimon J. Gerraty void br_range_enc32le(void *dst, const uint32_t *v, size_t num);
6720957b409SSimon J. Gerraty void br_range_enc32be(void *dst, const uint32_t *v, size_t num);
6730957b409SSimon J. Gerraty
6740957b409SSimon J. Gerraty void br_range_dec64le(uint64_t *v, size_t num, const void *src);
6750957b409SSimon J. Gerraty void br_range_dec64be(uint64_t *v, size_t num, const void *src);
6760957b409SSimon J. Gerraty void br_range_enc64le(void *dst, const uint64_t *v, size_t num);
6770957b409SSimon J. Gerraty void br_range_enc64be(void *dst, const uint64_t *v, size_t num);
6780957b409SSimon J. Gerraty
6790957b409SSimon J. Gerraty /*
6800957b409SSimon J. Gerraty * Byte-swap a 32-bit integer.
6810957b409SSimon J. Gerraty */
6820957b409SSimon J. Gerraty static inline uint32_t
br_swap32(uint32_t x)6830957b409SSimon J. Gerraty br_swap32(uint32_t x)
6840957b409SSimon J. Gerraty {
6850957b409SSimon J. Gerraty x = ((x & (uint32_t)0x00FF00FF) << 8)
6860957b409SSimon J. Gerraty | ((x >> 8) & (uint32_t)0x00FF00FF);
6870957b409SSimon J. Gerraty return (x << 16) | (x >> 16);
6880957b409SSimon J. Gerraty }
6890957b409SSimon J. Gerraty
6900957b409SSimon J. Gerraty /* ==================================================================== */
6910957b409SSimon J. Gerraty /*
6920957b409SSimon J. Gerraty * Support code for hash functions.
6930957b409SSimon J. Gerraty */
6940957b409SSimon J. Gerraty
6950957b409SSimon J. Gerraty /*
6960957b409SSimon J. Gerraty * IV for MD5, SHA-1, SHA-224 and SHA-256.
6970957b409SSimon J. Gerraty */
6980957b409SSimon J. Gerraty extern const uint32_t br_md5_IV[];
6990957b409SSimon J. Gerraty extern const uint32_t br_sha1_IV[];
7000957b409SSimon J. Gerraty extern const uint32_t br_sha224_IV[];
7010957b409SSimon J. Gerraty extern const uint32_t br_sha256_IV[];
7020957b409SSimon J. Gerraty
7030957b409SSimon J. Gerraty /*
7040957b409SSimon J. Gerraty * Round functions for MD5, SHA-1, SHA-224 and SHA-256 (SHA-224 and
7050957b409SSimon J. Gerraty * SHA-256 use the same round function).
7060957b409SSimon J. Gerraty */
7070957b409SSimon J. Gerraty void br_md5_round(const unsigned char *buf, uint32_t *val);
7080957b409SSimon J. Gerraty void br_sha1_round(const unsigned char *buf, uint32_t *val);
7090957b409SSimon J. Gerraty void br_sha2small_round(const unsigned char *buf, uint32_t *val);
7100957b409SSimon J. Gerraty
7110957b409SSimon J. Gerraty /*
7120957b409SSimon J. Gerraty * The core function for the TLS PRF. It computes
7130957b409SSimon J. Gerraty * P_hash(secret, label + seed), and XORs the result into the dst buffer.
7140957b409SSimon J. Gerraty */
7150957b409SSimon J. Gerraty void br_tls_phash(void *dst, size_t len,
7160957b409SSimon J. Gerraty const br_hash_class *dig,
7170957b409SSimon J. Gerraty const void *secret, size_t secret_len, const char *label,
7180957b409SSimon J. Gerraty size_t seed_num, const br_tls_prf_seed_chunk *seed);
7190957b409SSimon J. Gerraty
7200957b409SSimon J. Gerraty /*
7210957b409SSimon J. Gerraty * Copy all configured hash implementations from a multihash context
7220957b409SSimon J. Gerraty * to another.
7230957b409SSimon J. Gerraty */
7240957b409SSimon J. Gerraty static inline void
br_multihash_copyimpl(br_multihash_context * dst,const br_multihash_context * src)7250957b409SSimon J. Gerraty br_multihash_copyimpl(br_multihash_context *dst,
7260957b409SSimon J. Gerraty const br_multihash_context *src)
7270957b409SSimon J. Gerraty {
7280957b409SSimon J. Gerraty memcpy((void *)dst->impl, src->impl, sizeof src->impl);
7290957b409SSimon J. Gerraty }
7300957b409SSimon J. Gerraty
7310957b409SSimon J. Gerraty /* ==================================================================== */
7320957b409SSimon J. Gerraty /*
7330957b409SSimon J. Gerraty * Constant-time primitives. These functions manipulate 32-bit values in
7340957b409SSimon J. Gerraty * order to provide constant-time comparisons and multiplexers.
7350957b409SSimon J. Gerraty *
7360957b409SSimon J. Gerraty * Boolean values (the "ctl" bits) MUST have value 0 or 1.
7370957b409SSimon J. Gerraty *
7380957b409SSimon J. Gerraty * Implementation notes:
7390957b409SSimon J. Gerraty * =====================
7400957b409SSimon J. Gerraty *
7410957b409SSimon J. Gerraty * The uintN_t types are unsigned and with width exactly N bits; the C
7420957b409SSimon J. Gerraty * standard guarantees that computations are performed modulo 2^N, and
7430957b409SSimon J. Gerraty * there can be no overflow. Negation (unary '-') works on unsigned types
7440957b409SSimon J. Gerraty * as well.
7450957b409SSimon J. Gerraty *
7460957b409SSimon J. Gerraty * The intN_t types are guaranteed to have width exactly N bits, with no
7470957b409SSimon J. Gerraty * padding bit, and using two's complement representation. Casting
7480957b409SSimon J. Gerraty * intN_t to uintN_t really is conversion modulo 2^N. Beware that intN_t
7490957b409SSimon J. Gerraty * types, being signed, trigger implementation-defined behaviour on
7500957b409SSimon J. Gerraty * overflow (including raising some signal): with GCC, while modular
7510957b409SSimon J. Gerraty * arithmetics are usually applied, the optimizer may assume that
7520957b409SSimon J. Gerraty * overflows don't occur (unless the -fwrapv command-line option is
7530957b409SSimon J. Gerraty * added); Clang has the additional -ftrapv option to explicitly trap on
7540957b409SSimon J. Gerraty * integer overflow or underflow.
7550957b409SSimon J. Gerraty */
7560957b409SSimon J. Gerraty
7570957b409SSimon J. Gerraty /*
7580957b409SSimon J. Gerraty * Negate a boolean.
7590957b409SSimon J. Gerraty */
7600957b409SSimon J. Gerraty static inline uint32_t
NOT(uint32_t ctl)7610957b409SSimon J. Gerraty NOT(uint32_t ctl)
7620957b409SSimon J. Gerraty {
7630957b409SSimon J. Gerraty return ctl ^ 1;
7640957b409SSimon J. Gerraty }
7650957b409SSimon J. Gerraty
7660957b409SSimon J. Gerraty /*
7670957b409SSimon J. Gerraty * Multiplexer: returns x if ctl == 1, y if ctl == 0.
7680957b409SSimon J. Gerraty */
7690957b409SSimon J. Gerraty static inline uint32_t
MUX(uint32_t ctl,uint32_t x,uint32_t y)7700957b409SSimon J. Gerraty MUX(uint32_t ctl, uint32_t x, uint32_t y)
7710957b409SSimon J. Gerraty {
7720957b409SSimon J. Gerraty return y ^ (-ctl & (x ^ y));
7730957b409SSimon J. Gerraty }
7740957b409SSimon J. Gerraty
7750957b409SSimon J. Gerraty /*
7760957b409SSimon J. Gerraty * Equality check: returns 1 if x == y, 0 otherwise.
7770957b409SSimon J. Gerraty */
7780957b409SSimon J. Gerraty static inline uint32_t
EQ(uint32_t x,uint32_t y)7790957b409SSimon J. Gerraty EQ(uint32_t x, uint32_t y)
7800957b409SSimon J. Gerraty {
7810957b409SSimon J. Gerraty uint32_t q;
7820957b409SSimon J. Gerraty
7830957b409SSimon J. Gerraty q = x ^ y;
7840957b409SSimon J. Gerraty return NOT((q | -q) >> 31);
7850957b409SSimon J. Gerraty }
7860957b409SSimon J. Gerraty
7870957b409SSimon J. Gerraty /*
7880957b409SSimon J. Gerraty * Inequality check: returns 1 if x != y, 0 otherwise.
7890957b409SSimon J. Gerraty */
7900957b409SSimon J. Gerraty static inline uint32_t
NEQ(uint32_t x,uint32_t y)7910957b409SSimon J. Gerraty NEQ(uint32_t x, uint32_t y)
7920957b409SSimon J. Gerraty {
7930957b409SSimon J. Gerraty uint32_t q;
7940957b409SSimon J. Gerraty
7950957b409SSimon J. Gerraty q = x ^ y;
7960957b409SSimon J. Gerraty return (q | -q) >> 31;
7970957b409SSimon J. Gerraty }
7980957b409SSimon J. Gerraty
7990957b409SSimon J. Gerraty /*
8000957b409SSimon J. Gerraty * Comparison: returns 1 if x > y, 0 otherwise.
8010957b409SSimon J. Gerraty */
8020957b409SSimon J. Gerraty static inline uint32_t
GT(uint32_t x,uint32_t y)8030957b409SSimon J. Gerraty GT(uint32_t x, uint32_t y)
8040957b409SSimon J. Gerraty {
8050957b409SSimon J. Gerraty /*
8060957b409SSimon J. Gerraty * If both x < 2^31 and x < 2^31, then y-x will have its high
8070957b409SSimon J. Gerraty * bit set if x > y, cleared otherwise.
8080957b409SSimon J. Gerraty *
8090957b409SSimon J. Gerraty * If either x >= 2^31 or y >= 2^31 (but not both), then the
8100957b409SSimon J. Gerraty * result is the high bit of x.
8110957b409SSimon J. Gerraty *
8120957b409SSimon J. Gerraty * If both x >= 2^31 and y >= 2^31, then we can virtually
8130957b409SSimon J. Gerraty * subtract 2^31 from both, and we are back to the first case.
8140957b409SSimon J. Gerraty * Since (y-2^31)-(x-2^31) = y-x, the subtraction is already
8150957b409SSimon J. Gerraty * fine.
8160957b409SSimon J. Gerraty */
8170957b409SSimon J. Gerraty uint32_t z;
8180957b409SSimon J. Gerraty
8190957b409SSimon J. Gerraty z = y - x;
8200957b409SSimon J. Gerraty return (z ^ ((x ^ y) & (x ^ z))) >> 31;
8210957b409SSimon J. Gerraty }
8220957b409SSimon J. Gerraty
8230957b409SSimon J. Gerraty /*
8240957b409SSimon J. Gerraty * Other comparisons (greater-or-equal, lower-than, lower-or-equal).
8250957b409SSimon J. Gerraty */
8260957b409SSimon J. Gerraty #define GE(x, y) NOT(GT(y, x))
8270957b409SSimon J. Gerraty #define LT(x, y) GT(y, x)
8280957b409SSimon J. Gerraty #define LE(x, y) NOT(GT(x, y))
8290957b409SSimon J. Gerraty
8300957b409SSimon J. Gerraty /*
8310957b409SSimon J. Gerraty * General comparison: returned value is -1, 0 or 1, depending on
8320957b409SSimon J. Gerraty * whether x is lower than, equal to, or greater than y.
8330957b409SSimon J. Gerraty */
8340957b409SSimon J. Gerraty static inline int32_t
CMP(uint32_t x,uint32_t y)8350957b409SSimon J. Gerraty CMP(uint32_t x, uint32_t y)
8360957b409SSimon J. Gerraty {
8370957b409SSimon J. Gerraty return (int32_t)GT(x, y) | -(int32_t)GT(y, x);
8380957b409SSimon J. Gerraty }
8390957b409SSimon J. Gerraty
8400957b409SSimon J. Gerraty /*
8410957b409SSimon J. Gerraty * Returns 1 if x == 0, 0 otherwise. Take care that the operand is signed.
8420957b409SSimon J. Gerraty */
8430957b409SSimon J. Gerraty static inline uint32_t
EQ0(int32_t x)8440957b409SSimon J. Gerraty EQ0(int32_t x)
8450957b409SSimon J. Gerraty {
8460957b409SSimon J. Gerraty uint32_t q;
8470957b409SSimon J. Gerraty
8480957b409SSimon J. Gerraty q = (uint32_t)x;
8490957b409SSimon J. Gerraty return ~(q | -q) >> 31;
8500957b409SSimon J. Gerraty }
8510957b409SSimon J. Gerraty
8520957b409SSimon J. Gerraty /*
8530957b409SSimon J. Gerraty * Returns 1 if x > 0, 0 otherwise. Take care that the operand is signed.
8540957b409SSimon J. Gerraty */
8550957b409SSimon J. Gerraty static inline uint32_t
GT0(int32_t x)8560957b409SSimon J. Gerraty GT0(int32_t x)
8570957b409SSimon J. Gerraty {
8580957b409SSimon J. Gerraty /*
8590957b409SSimon J. Gerraty * High bit of -x is 0 if x == 0, but 1 if x > 0.
8600957b409SSimon J. Gerraty */
8610957b409SSimon J. Gerraty uint32_t q;
8620957b409SSimon J. Gerraty
8630957b409SSimon J. Gerraty q = (uint32_t)x;
8640957b409SSimon J. Gerraty return (~q & -q) >> 31;
8650957b409SSimon J. Gerraty }
8660957b409SSimon J. Gerraty
8670957b409SSimon J. Gerraty /*
8680957b409SSimon J. Gerraty * Returns 1 if x >= 0, 0 otherwise. Take care that the operand is signed.
8690957b409SSimon J. Gerraty */
8700957b409SSimon J. Gerraty static inline uint32_t
GE0(int32_t x)8710957b409SSimon J. Gerraty GE0(int32_t x)
8720957b409SSimon J. Gerraty {
8730957b409SSimon J. Gerraty return ~(uint32_t)x >> 31;
8740957b409SSimon J. Gerraty }
8750957b409SSimon J. Gerraty
8760957b409SSimon J. Gerraty /*
8770957b409SSimon J. Gerraty * Returns 1 if x < 0, 0 otherwise. Take care that the operand is signed.
8780957b409SSimon J. Gerraty */
8790957b409SSimon J. Gerraty static inline uint32_t
LT0(int32_t x)8800957b409SSimon J. Gerraty LT0(int32_t x)
8810957b409SSimon J. Gerraty {
8820957b409SSimon J. Gerraty return (uint32_t)x >> 31;
8830957b409SSimon J. Gerraty }
8840957b409SSimon J. Gerraty
8850957b409SSimon J. Gerraty /*
8860957b409SSimon J. Gerraty * Returns 1 if x <= 0, 0 otherwise. Take care that the operand is signed.
8870957b409SSimon J. Gerraty */
8880957b409SSimon J. Gerraty static inline uint32_t
LE0(int32_t x)8890957b409SSimon J. Gerraty LE0(int32_t x)
8900957b409SSimon J. Gerraty {
8910957b409SSimon J. Gerraty uint32_t q;
8920957b409SSimon J. Gerraty
8930957b409SSimon J. Gerraty /*
8940957b409SSimon J. Gerraty * ~-x has its high bit set if and only if -x is nonnegative (as
8950957b409SSimon J. Gerraty * a signed int), i.e. x is in the -(2^31-1) to 0 range. We must
8960957b409SSimon J. Gerraty * do an OR with x itself to account for x = -2^31.
8970957b409SSimon J. Gerraty */
8980957b409SSimon J. Gerraty q = (uint32_t)x;
8990957b409SSimon J. Gerraty return (q | ~-q) >> 31;
9000957b409SSimon J. Gerraty }
9010957b409SSimon J. Gerraty
9020957b409SSimon J. Gerraty /*
9030957b409SSimon J. Gerraty * Conditional copy: src[] is copied into dst[] if and only if ctl is 1.
9040957b409SSimon J. Gerraty * dst[] and src[] may overlap completely (but not partially).
9050957b409SSimon J. Gerraty */
9060957b409SSimon J. Gerraty void br_ccopy(uint32_t ctl, void *dst, const void *src, size_t len);
9070957b409SSimon J. Gerraty
9080957b409SSimon J. Gerraty #define CCOPY br_ccopy
9090957b409SSimon J. Gerraty
9100957b409SSimon J. Gerraty /*
9110957b409SSimon J. Gerraty * Compute the bit length of a 32-bit integer. Returned value is between 0
9120957b409SSimon J. Gerraty * and 32 (inclusive).
9130957b409SSimon J. Gerraty */
9140957b409SSimon J. Gerraty static inline uint32_t
BIT_LENGTH(uint32_t x)9150957b409SSimon J. Gerraty BIT_LENGTH(uint32_t x)
9160957b409SSimon J. Gerraty {
9170957b409SSimon J. Gerraty uint32_t k, c;
9180957b409SSimon J. Gerraty
9190957b409SSimon J. Gerraty k = NEQ(x, 0);
9200957b409SSimon J. Gerraty c = GT(x, 0xFFFF); x = MUX(c, x >> 16, x); k += c << 4;
9210957b409SSimon J. Gerraty c = GT(x, 0x00FF); x = MUX(c, x >> 8, x); k += c << 3;
9220957b409SSimon J. Gerraty c = GT(x, 0x000F); x = MUX(c, x >> 4, x); k += c << 2;
9230957b409SSimon J. Gerraty c = GT(x, 0x0003); x = MUX(c, x >> 2, x); k += c << 1;
9240957b409SSimon J. Gerraty k += GT(x, 0x0001);
9250957b409SSimon J. Gerraty return k;
9260957b409SSimon J. Gerraty }
9270957b409SSimon J. Gerraty
9280957b409SSimon J. Gerraty /*
9290957b409SSimon J. Gerraty * Compute the minimum of x and y.
9300957b409SSimon J. Gerraty */
9310957b409SSimon J. Gerraty static inline uint32_t
MIN(uint32_t x,uint32_t y)9320957b409SSimon J. Gerraty MIN(uint32_t x, uint32_t y)
9330957b409SSimon J. Gerraty {
9340957b409SSimon J. Gerraty return MUX(GT(x, y), y, x);
9350957b409SSimon J. Gerraty }
9360957b409SSimon J. Gerraty
9370957b409SSimon J. Gerraty /*
9380957b409SSimon J. Gerraty * Compute the maximum of x and y.
9390957b409SSimon J. Gerraty */
9400957b409SSimon J. Gerraty static inline uint32_t
MAX(uint32_t x,uint32_t y)9410957b409SSimon J. Gerraty MAX(uint32_t x, uint32_t y)
9420957b409SSimon J. Gerraty {
9430957b409SSimon J. Gerraty return MUX(GT(x, y), x, y);
9440957b409SSimon J. Gerraty }
9450957b409SSimon J. Gerraty
9460957b409SSimon J. Gerraty /*
9470957b409SSimon J. Gerraty * Multiply two 32-bit integers, with a 64-bit result. This default
9480957b409SSimon J. Gerraty * implementation assumes that the basic multiplication operator
9490957b409SSimon J. Gerraty * yields constant-time code.
9500957b409SSimon J. Gerraty */
9510957b409SSimon J. Gerraty #define MUL(x, y) ((uint64_t)(x) * (uint64_t)(y))
9520957b409SSimon J. Gerraty
9530957b409SSimon J. Gerraty #if BR_CT_MUL31
9540957b409SSimon J. Gerraty
9550957b409SSimon J. Gerraty /*
9560957b409SSimon J. Gerraty * Alternate implementation of MUL31, that will be constant-time on some
9570957b409SSimon J. Gerraty * (old) platforms where the default MUL31 is not. Unfortunately, it is
9580957b409SSimon J. Gerraty * also substantially slower, and yields larger code, on more modern
9590957b409SSimon J. Gerraty * platforms, which is why it is deactivated by default.
9600957b409SSimon J. Gerraty *
9610957b409SSimon J. Gerraty * MUL31_lo() must do some extra work because on some platforms, the
9620957b409SSimon J. Gerraty * _signed_ multiplication may return early if the top bits are 1.
9630957b409SSimon J. Gerraty * Simply truncating (casting) the output of MUL31() would not be
9640957b409SSimon J. Gerraty * sufficient, because the compiler may notice that we keep only the low
9650957b409SSimon J. Gerraty * word, and then replace automatically the unsigned multiplication with
9660957b409SSimon J. Gerraty * a signed multiplication opcode.
9670957b409SSimon J. Gerraty */
9680957b409SSimon J. Gerraty #define MUL31(x, y) ((uint64_t)((x) | (uint32_t)0x80000000) \
9690957b409SSimon J. Gerraty * (uint64_t)((y) | (uint32_t)0x80000000) \
9700957b409SSimon J. Gerraty - ((uint64_t)(x) << 31) - ((uint64_t)(y) << 31) \
9710957b409SSimon J. Gerraty - ((uint64_t)1 << 62))
9720957b409SSimon J. Gerraty static inline uint32_t
MUL31_lo(uint32_t x,uint32_t y)9730957b409SSimon J. Gerraty MUL31_lo(uint32_t x, uint32_t y)
9740957b409SSimon J. Gerraty {
9750957b409SSimon J. Gerraty uint32_t xl, xh;
9760957b409SSimon J. Gerraty uint32_t yl, yh;
9770957b409SSimon J. Gerraty
9780957b409SSimon J. Gerraty xl = (x & 0xFFFF) | (uint32_t)0x80000000;
9790957b409SSimon J. Gerraty xh = (x >> 16) | (uint32_t)0x80000000;
9800957b409SSimon J. Gerraty yl = (y & 0xFFFF) | (uint32_t)0x80000000;
9810957b409SSimon J. Gerraty yh = (y >> 16) | (uint32_t)0x80000000;
9820957b409SSimon J. Gerraty return (xl * yl + ((xl * yh + xh * yl) << 16)) & (uint32_t)0x7FFFFFFF;
9830957b409SSimon J. Gerraty }
9840957b409SSimon J. Gerraty
9850957b409SSimon J. Gerraty #else
9860957b409SSimon J. Gerraty
9870957b409SSimon J. Gerraty /*
9880957b409SSimon J. Gerraty * Multiply two 31-bit integers, with a 62-bit result. This default
9890957b409SSimon J. Gerraty * implementation assumes that the basic multiplication operator
9900957b409SSimon J. Gerraty * yields constant-time code.
9910957b409SSimon J. Gerraty * The MUL31_lo() macro returns only the low 31 bits of the product.
9920957b409SSimon J. Gerraty */
9930957b409SSimon J. Gerraty #define MUL31(x, y) ((uint64_t)(x) * (uint64_t)(y))
9940957b409SSimon J. Gerraty #define MUL31_lo(x, y) (((uint32_t)(x) * (uint32_t)(y)) & (uint32_t)0x7FFFFFFF)
9950957b409SSimon J. Gerraty
9960957b409SSimon J. Gerraty #endif
9970957b409SSimon J. Gerraty
9980957b409SSimon J. Gerraty /*
9990957b409SSimon J. Gerraty * Multiply two words together; the sum of the lengths of the two
10000957b409SSimon J. Gerraty * operands must not exceed 31 (for instance, one operand may use 16
10010957b409SSimon J. Gerraty * bits if the other fits on 15). If BR_CT_MUL15 is non-zero, then the
10020957b409SSimon J. Gerraty * macro will contain some extra operations that help in making the
10030957b409SSimon J. Gerraty * operation constant-time on some platforms, where the basic 32-bit
10040957b409SSimon J. Gerraty * multiplication is not constant-time.
10050957b409SSimon J. Gerraty */
10060957b409SSimon J. Gerraty #if BR_CT_MUL15
10070957b409SSimon J. Gerraty #define MUL15(x, y) (((uint32_t)(x) | (uint32_t)0x80000000) \
10080957b409SSimon J. Gerraty * ((uint32_t)(y) | (uint32_t)0x80000000) \
10090957b409SSimon J. Gerraty & (uint32_t)0x7FFFFFFF)
10100957b409SSimon J. Gerraty #else
10110957b409SSimon J. Gerraty #define MUL15(x, y) ((uint32_t)(x) * (uint32_t)(y))
10120957b409SSimon J. Gerraty #endif
10130957b409SSimon J. Gerraty
10140957b409SSimon J. Gerraty /*
10150957b409SSimon J. Gerraty * Arithmetic right shift (sign bit is copied). What happens when
10160957b409SSimon J. Gerraty * right-shifting a negative value is _implementation-defined_, so it
10170957b409SSimon J. Gerraty * does not trigger undefined behaviour, but it is still up to each
10180957b409SSimon J. Gerraty * compiler to define (and document) what it does. Most/all compilers
10190957b409SSimon J. Gerraty * will do an arithmetic shift, the sign bit being used to fill the
10200957b409SSimon J. Gerraty * holes; this is a native operation on the underlying CPU, and it would
10210957b409SSimon J. Gerraty * make little sense for the compiler to do otherwise. GCC explicitly
10220957b409SSimon J. Gerraty * documents that it follows that convention.
10230957b409SSimon J. Gerraty *
10240957b409SSimon J. Gerraty * Still, if BR_NO_ARITH_SHIFT is defined (and non-zero), then an
10250957b409SSimon J. Gerraty * alternate version will be used, that does not rely on such
10260957b409SSimon J. Gerraty * implementation-defined behaviour. Unfortunately, it is also slower
10270957b409SSimon J. Gerraty * and yields bigger code, which is why it is deactivated by default.
10280957b409SSimon J. Gerraty */
10290957b409SSimon J. Gerraty #if BR_NO_ARITH_SHIFT
10300957b409SSimon J. Gerraty #define ARSH(x, n) (((uint32_t)(x) >> (n)) \
10310957b409SSimon J. Gerraty | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
10320957b409SSimon J. Gerraty #else
10330957b409SSimon J. Gerraty #define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
10340957b409SSimon J. Gerraty #endif
10350957b409SSimon J. Gerraty
10360957b409SSimon J. Gerraty /*
10370957b409SSimon J. Gerraty * Constant-time division. The dividend hi:lo is divided by the
10380957b409SSimon J. Gerraty * divisor d; the quotient is returned and the remainder is written
10390957b409SSimon J. Gerraty * in *r. If hi == d, then the quotient does not fit on 32 bits;
10400957b409SSimon J. Gerraty * returned value is thus truncated. If hi > d, returned values are
10410957b409SSimon J. Gerraty * indeterminate.
10420957b409SSimon J. Gerraty */
10430957b409SSimon J. Gerraty uint32_t br_divrem(uint32_t hi, uint32_t lo, uint32_t d, uint32_t *r);
10440957b409SSimon J. Gerraty
10450957b409SSimon J. Gerraty /*
10460957b409SSimon J. Gerraty * Wrapper for br_divrem(); the remainder is returned, and the quotient
10470957b409SSimon J. Gerraty * is discarded.
10480957b409SSimon J. Gerraty */
10490957b409SSimon J. Gerraty static inline uint32_t
br_rem(uint32_t hi,uint32_t lo,uint32_t d)10500957b409SSimon J. Gerraty br_rem(uint32_t hi, uint32_t lo, uint32_t d)
10510957b409SSimon J. Gerraty {
10520957b409SSimon J. Gerraty uint32_t r;
10530957b409SSimon J. Gerraty
10540957b409SSimon J. Gerraty br_divrem(hi, lo, d, &r);
10550957b409SSimon J. Gerraty return r;
10560957b409SSimon J. Gerraty }
10570957b409SSimon J. Gerraty
10580957b409SSimon J. Gerraty /*
10590957b409SSimon J. Gerraty * Wrapper for br_divrem(); the quotient is returned, and the remainder
10600957b409SSimon J. Gerraty * is discarded.
10610957b409SSimon J. Gerraty */
10620957b409SSimon J. Gerraty static inline uint32_t
br_div(uint32_t hi,uint32_t lo,uint32_t d)10630957b409SSimon J. Gerraty br_div(uint32_t hi, uint32_t lo, uint32_t d)
10640957b409SSimon J. Gerraty {
10650957b409SSimon J. Gerraty uint32_t r;
10660957b409SSimon J. Gerraty
10670957b409SSimon J. Gerraty return br_divrem(hi, lo, d, &r);
10680957b409SSimon J. Gerraty }
10690957b409SSimon J. Gerraty
10700957b409SSimon J. Gerraty /* ==================================================================== */
10710957b409SSimon J. Gerraty
10720957b409SSimon J. Gerraty /*
10730957b409SSimon J. Gerraty * Integers 'i32'
10740957b409SSimon J. Gerraty * --------------
10750957b409SSimon J. Gerraty *
10760957b409SSimon J. Gerraty * The 'i32' functions implement computations on big integers using
10770957b409SSimon J. Gerraty * an internal representation as an array of 32-bit integers. For
10780957b409SSimon J. Gerraty * an array x[]:
10790957b409SSimon J. Gerraty * -- x[0] contains the "announced bit length" of the integer
10800957b409SSimon J. Gerraty * -- x[1], x[2]... contain the value in little-endian order (x[1]
10810957b409SSimon J. Gerraty * contains the least significant 32 bits)
10820957b409SSimon J. Gerraty *
10830957b409SSimon J. Gerraty * Multiplications rely on the elementary 32x32->64 multiplication.
10840957b409SSimon J. Gerraty *
10850957b409SSimon J. Gerraty * The announced bit length specifies the number of bits that are
10860957b409SSimon J. Gerraty * significant in the subsequent 32-bit words. Unused bits in the
10870957b409SSimon J. Gerraty * last (most significant) word are set to 0; subsequent words are
10880957b409SSimon J. Gerraty * uninitialized and need not exist at all.
10890957b409SSimon J. Gerraty *
10900957b409SSimon J. Gerraty * The execution time and memory access patterns of all computations
10910957b409SSimon J. Gerraty * depend on the announced bit length, but not on the actual word
10920957b409SSimon J. Gerraty * values. For modular integers, the announced bit length of any integer
10930957b409SSimon J. Gerraty * modulo n is equal to the actual bit length of n; thus, computations
10940957b409SSimon J. Gerraty * on modular integers are "constant-time" (only the modulus length may
10950957b409SSimon J. Gerraty * leak).
10960957b409SSimon J. Gerraty */
10970957b409SSimon J. Gerraty
10980957b409SSimon J. Gerraty /*
10990957b409SSimon J. Gerraty * Compute the actual bit length of an integer. The argument x should
11000957b409SSimon J. Gerraty * point to the first (least significant) value word of the integer.
11010957b409SSimon J. Gerraty * The len 'xlen' contains the number of 32-bit words to access.
11020957b409SSimon J. Gerraty *
11030957b409SSimon J. Gerraty * CT: value or length of x does not leak.
11040957b409SSimon J. Gerraty */
11050957b409SSimon J. Gerraty uint32_t br_i32_bit_length(uint32_t *x, size_t xlen);
11060957b409SSimon J. Gerraty
11070957b409SSimon J. Gerraty /*
11080957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation. The
11090957b409SSimon J. Gerraty * "true" bit length of the integer is computed, but all words of x[]
11100957b409SSimon J. Gerraty * corresponding to the full 'len' bytes of the source are set.
11110957b409SSimon J. Gerraty *
11120957b409SSimon J. Gerraty * CT: value or length of x does not leak.
11130957b409SSimon J. Gerraty */
11140957b409SSimon J. Gerraty void br_i32_decode(uint32_t *x, const void *src, size_t len);
11150957b409SSimon J. Gerraty
11160957b409SSimon J. Gerraty /*
11170957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation. The
11180957b409SSimon J. Gerraty * integer MUST be lower than m[]; the announced bit length written in
11190957b409SSimon J. Gerraty * x[] will be equal to that of m[]. All 'len' bytes from the source are
11200957b409SSimon J. Gerraty * read.
11210957b409SSimon J. Gerraty *
11220957b409SSimon J. Gerraty * Returned value is 1 if the decode value fits within the modulus, 0
11230957b409SSimon J. Gerraty * otherwise. In the latter case, the x[] buffer will be set to 0 (but
11240957b409SSimon J. Gerraty * still with the announced bit length of m[]).
11250957b409SSimon J. Gerraty *
11260957b409SSimon J. Gerraty * CT: value or length of x does not leak. Memory access pattern depends
11270957b409SSimon J. Gerraty * only of 'len' and the announced bit length of m. Whether x fits or
11280957b409SSimon J. Gerraty * not does not leak either.
11290957b409SSimon J. Gerraty */
11300957b409SSimon J. Gerraty uint32_t br_i32_decode_mod(uint32_t *x,
11310957b409SSimon J. Gerraty const void *src, size_t len, const uint32_t *m);
11320957b409SSimon J. Gerraty
11330957b409SSimon J. Gerraty /*
11340957b409SSimon J. Gerraty * Reduce an integer (a[]) modulo another (m[]). The result is written
11350957b409SSimon J. Gerraty * in x[] and its announced bit length is set to be equal to that of m[].
11360957b409SSimon J. Gerraty *
11370957b409SSimon J. Gerraty * x[] MUST be distinct from a[] and m[].
11380957b409SSimon J. Gerraty *
11390957b409SSimon J. Gerraty * CT: only announced bit lengths leak, not values of x, a or m.
11400957b409SSimon J. Gerraty */
11410957b409SSimon J. Gerraty void br_i32_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
11420957b409SSimon J. Gerraty
11430957b409SSimon J. Gerraty /*
11440957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation, and
11450957b409SSimon J. Gerraty * reduce it modulo the provided modulus m[]. The announced bit length
11460957b409SSimon J. Gerraty * of the result is set to be equal to that of the modulus.
11470957b409SSimon J. Gerraty *
11480957b409SSimon J. Gerraty * x[] MUST be distinct from m[].
11490957b409SSimon J. Gerraty */
11500957b409SSimon J. Gerraty void br_i32_decode_reduce(uint32_t *x,
11510957b409SSimon J. Gerraty const void *src, size_t len, const uint32_t *m);
11520957b409SSimon J. Gerraty
11530957b409SSimon J. Gerraty /*
11540957b409SSimon J. Gerraty * Encode an integer into its big-endian unsigned representation. The
11550957b409SSimon J. Gerraty * output length in bytes is provided (parameter 'len'); if the length
11560957b409SSimon J. Gerraty * is too short then the integer is appropriately truncated; if it is
11570957b409SSimon J. Gerraty * too long then the extra bytes are set to 0.
11580957b409SSimon J. Gerraty */
11590957b409SSimon J. Gerraty void br_i32_encode(void *dst, size_t len, const uint32_t *x);
11600957b409SSimon J. Gerraty
11610957b409SSimon J. Gerraty /*
11620957b409SSimon J. Gerraty * Multiply x[] by 2^32 and then add integer z, modulo m[]. This
11630957b409SSimon J. Gerraty * function assumes that x[] and m[] have the same announced bit
11640957b409SSimon J. Gerraty * length, and the announced bit length of m[] matches its true
11650957b409SSimon J. Gerraty * bit length.
11660957b409SSimon J. Gerraty *
11670957b409SSimon J. Gerraty * x[] and m[] MUST be distinct arrays.
11680957b409SSimon J. Gerraty *
11690957b409SSimon J. Gerraty * CT: only the common announced bit length of x and m leaks, not
11700957b409SSimon J. Gerraty * the values of x, z or m.
11710957b409SSimon J. Gerraty */
11720957b409SSimon J. Gerraty void br_i32_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
11730957b409SSimon J. Gerraty
11740957b409SSimon J. Gerraty /*
11750957b409SSimon J. Gerraty * Extract one word from an integer. The offset is counted in bits.
11760957b409SSimon J. Gerraty * The word MUST entirely fit within the word elements corresponding
11770957b409SSimon J. Gerraty * to the announced bit length of a[].
11780957b409SSimon J. Gerraty */
11790957b409SSimon J. Gerraty static inline uint32_t
br_i32_word(const uint32_t * a,uint32_t off)11800957b409SSimon J. Gerraty br_i32_word(const uint32_t *a, uint32_t off)
11810957b409SSimon J. Gerraty {
11820957b409SSimon J. Gerraty size_t u;
11830957b409SSimon J. Gerraty unsigned j;
11840957b409SSimon J. Gerraty
11850957b409SSimon J. Gerraty u = (size_t)(off >> 5) + 1;
11860957b409SSimon J. Gerraty j = (unsigned)off & 31;
11870957b409SSimon J. Gerraty if (j == 0) {
11880957b409SSimon J. Gerraty return a[u];
11890957b409SSimon J. Gerraty } else {
11900957b409SSimon J. Gerraty return (a[u] >> j) | (a[u + 1] << (32 - j));
11910957b409SSimon J. Gerraty }
11920957b409SSimon J. Gerraty }
11930957b409SSimon J. Gerraty
11940957b409SSimon J. Gerraty /*
11950957b409SSimon J. Gerraty * Test whether an integer is zero.
11960957b409SSimon J. Gerraty */
11970957b409SSimon J. Gerraty uint32_t br_i32_iszero(const uint32_t *x);
11980957b409SSimon J. Gerraty
11990957b409SSimon J. Gerraty /*
12000957b409SSimon J. Gerraty * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
12010957b409SSimon J. Gerraty * is unmodified, but the carry is still computed and returned. The
12020957b409SSimon J. Gerraty * arrays a[] and b[] MUST have the same announced bit length.
12030957b409SSimon J. Gerraty *
12040957b409SSimon J. Gerraty * a[] and b[] MAY be the same array, but partial overlap is not allowed.
12050957b409SSimon J. Gerraty */
12060957b409SSimon J. Gerraty uint32_t br_i32_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
12070957b409SSimon J. Gerraty
12080957b409SSimon J. Gerraty /*
12090957b409SSimon J. Gerraty * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
12100957b409SSimon J. Gerraty * then a[] is unmodified, but the carry is still computed and returned.
12110957b409SSimon J. Gerraty * The arrays a[] and b[] MUST have the same announced bit length.
12120957b409SSimon J. Gerraty *
12130957b409SSimon J. Gerraty * a[] and b[] MAY be the same array, but partial overlap is not allowed.
12140957b409SSimon J. Gerraty */
12150957b409SSimon J. Gerraty uint32_t br_i32_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
12160957b409SSimon J. Gerraty
12170957b409SSimon J. Gerraty /*
12180957b409SSimon J. Gerraty * Compute d+a*b, result in d. The initial announced bit length of d[]
12190957b409SSimon J. Gerraty * MUST match that of a[]. The d[] array MUST be large enough to
12200957b409SSimon J. Gerraty * accommodate the full result, plus (possibly) an extra word. The
12210957b409SSimon J. Gerraty * resulting announced bit length of d[] will be the sum of the announced
12220957b409SSimon J. Gerraty * bit lengths of a[] and b[] (therefore, it may be larger than the actual
12230957b409SSimon J. Gerraty * bit length of the numerical result).
12240957b409SSimon J. Gerraty *
12250957b409SSimon J. Gerraty * a[] and b[] may be the same array. d[] must be disjoint from both a[]
12260957b409SSimon J. Gerraty * and b[].
12270957b409SSimon J. Gerraty */
12280957b409SSimon J. Gerraty void br_i32_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
12290957b409SSimon J. Gerraty
12300957b409SSimon J. Gerraty /*
12310957b409SSimon J. Gerraty * Zeroize an integer. The announced bit length is set to the provided
12320957b409SSimon J. Gerraty * value, and the corresponding words are set to 0.
12330957b409SSimon J. Gerraty */
12340957b409SSimon J. Gerraty static inline void
br_i32_zero(uint32_t * x,uint32_t bit_len)12350957b409SSimon J. Gerraty br_i32_zero(uint32_t *x, uint32_t bit_len)
12360957b409SSimon J. Gerraty {
12370957b409SSimon J. Gerraty *x ++ = bit_len;
12380957b409SSimon J. Gerraty memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
12390957b409SSimon J. Gerraty }
12400957b409SSimon J. Gerraty
12410957b409SSimon J. Gerraty /*
12420957b409SSimon J. Gerraty * Compute -(1/x) mod 2^32. If x is even, then this function returns 0.
12430957b409SSimon J. Gerraty */
12440957b409SSimon J. Gerraty uint32_t br_i32_ninv32(uint32_t x);
12450957b409SSimon J. Gerraty
12460957b409SSimon J. Gerraty /*
12470957b409SSimon J. Gerraty * Convert a modular integer to Montgomery representation. The integer x[]
12480957b409SSimon J. Gerraty * MUST be lower than m[], but with the same announced bit length.
12490957b409SSimon J. Gerraty */
12500957b409SSimon J. Gerraty void br_i32_to_monty(uint32_t *x, const uint32_t *m);
12510957b409SSimon J. Gerraty
12520957b409SSimon J. Gerraty /*
12530957b409SSimon J. Gerraty * Convert a modular integer back from Montgomery representation. The
12540957b409SSimon J. Gerraty * integer x[] MUST be lower than m[], but with the same announced bit
12550957b409SSimon J. Gerraty * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
12560957b409SSimon J. Gerraty * the least significant value word of m[] (this works only if m[] is
12570957b409SSimon J. Gerraty * an odd integer).
12580957b409SSimon J. Gerraty */
12590957b409SSimon J. Gerraty void br_i32_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
12600957b409SSimon J. Gerraty
12610957b409SSimon J. Gerraty /*
12620957b409SSimon J. Gerraty * Compute a modular Montgomery multiplication. d[] is filled with the
12630957b409SSimon J. Gerraty * value of x*y/R modulo m[] (where R is the Montgomery factor). The
12640957b409SSimon J. Gerraty * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
12650957b409SSimon J. Gerraty * numerically lower than m[]. x[] and y[] MAY be the same array. The
12660957b409SSimon J. Gerraty * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
12670957b409SSimon J. Gerraty * significant value word of m[] (this works only if m[] is an odd
12680957b409SSimon J. Gerraty * integer).
12690957b409SSimon J. Gerraty */
12700957b409SSimon J. Gerraty void br_i32_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
12710957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i);
12720957b409SSimon J. Gerraty
12730957b409SSimon J. Gerraty /*
12740957b409SSimon J. Gerraty * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
12750957b409SSimon J. Gerraty * (same announced bit length, lower value). m[] MUST be odd. The
12760957b409SSimon J. Gerraty * exponent is in big-endian unsigned notation, over 'elen' bytes. The
12770957b409SSimon J. Gerraty * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
12780957b409SSimon J. Gerraty * significant value word of m[] (this works only if m[] is an odd
12790957b409SSimon J. Gerraty * integer). The t1[] and t2[] parameters must be temporary arrays,
12800957b409SSimon J. Gerraty * each large enough to accommodate an integer with the same size as m[].
12810957b409SSimon J. Gerraty */
12820957b409SSimon J. Gerraty void br_i32_modpow(uint32_t *x, const unsigned char *e, size_t elen,
12830957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
12840957b409SSimon J. Gerraty
12850957b409SSimon J. Gerraty /* ==================================================================== */
12860957b409SSimon J. Gerraty
12870957b409SSimon J. Gerraty /*
12880957b409SSimon J. Gerraty * Integers 'i31'
12890957b409SSimon J. Gerraty * --------------
12900957b409SSimon J. Gerraty *
12910957b409SSimon J. Gerraty * The 'i31' functions implement computations on big integers using
12920957b409SSimon J. Gerraty * an internal representation as an array of 32-bit integers. For
12930957b409SSimon J. Gerraty * an array x[]:
12940957b409SSimon J. Gerraty * -- x[0] encodes the array length and the "announced bit length"
12950957b409SSimon J. Gerraty * of the integer: namely, if the announced bit length is k,
12960957b409SSimon J. Gerraty * then x[0] = ((k / 31) << 5) + (k % 31).
12970957b409SSimon J. Gerraty * -- x[1], x[2]... contain the value in little-endian order, 31
12980957b409SSimon J. Gerraty * bits per word (x[1] contains the least significant 31 bits).
12990957b409SSimon J. Gerraty * The upper bit of each word is 0.
13000957b409SSimon J. Gerraty *
13010957b409SSimon J. Gerraty * Multiplications rely on the elementary 32x32->64 multiplication.
13020957b409SSimon J. Gerraty *
13030957b409SSimon J. Gerraty * The announced bit length specifies the number of bits that are
13040957b409SSimon J. Gerraty * significant in the subsequent 32-bit words. Unused bits in the
13050957b409SSimon J. Gerraty * last (most significant) word are set to 0; subsequent words are
13060957b409SSimon J. Gerraty * uninitialized and need not exist at all.
13070957b409SSimon J. Gerraty *
13080957b409SSimon J. Gerraty * The execution time and memory access patterns of all computations
13090957b409SSimon J. Gerraty * depend on the announced bit length, but not on the actual word
13100957b409SSimon J. Gerraty * values. For modular integers, the announced bit length of any integer
13110957b409SSimon J. Gerraty * modulo n is equal to the actual bit length of n; thus, computations
13120957b409SSimon J. Gerraty * on modular integers are "constant-time" (only the modulus length may
13130957b409SSimon J. Gerraty * leak).
13140957b409SSimon J. Gerraty */
13150957b409SSimon J. Gerraty
13160957b409SSimon J. Gerraty /*
13170957b409SSimon J. Gerraty * Test whether an integer is zero.
13180957b409SSimon J. Gerraty */
13190957b409SSimon J. Gerraty uint32_t br_i31_iszero(const uint32_t *x);
13200957b409SSimon J. Gerraty
13210957b409SSimon J. Gerraty /*
13220957b409SSimon J. Gerraty * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
13230957b409SSimon J. Gerraty * is unmodified, but the carry is still computed and returned. The
13240957b409SSimon J. Gerraty * arrays a[] and b[] MUST have the same announced bit length.
13250957b409SSimon J. Gerraty *
13260957b409SSimon J. Gerraty * a[] and b[] MAY be the same array, but partial overlap is not allowed.
13270957b409SSimon J. Gerraty */
13280957b409SSimon J. Gerraty uint32_t br_i31_add(uint32_t *a, const uint32_t *b, uint32_t ctl);
13290957b409SSimon J. Gerraty
13300957b409SSimon J. Gerraty /*
13310957b409SSimon J. Gerraty * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
13320957b409SSimon J. Gerraty * then a[] is unmodified, but the carry is still computed and returned.
13330957b409SSimon J. Gerraty * The arrays a[] and b[] MUST have the same announced bit length.
13340957b409SSimon J. Gerraty *
13350957b409SSimon J. Gerraty * a[] and b[] MAY be the same array, but partial overlap is not allowed.
13360957b409SSimon J. Gerraty */
13370957b409SSimon J. Gerraty uint32_t br_i31_sub(uint32_t *a, const uint32_t *b, uint32_t ctl);
13380957b409SSimon J. Gerraty
13390957b409SSimon J. Gerraty /*
13400957b409SSimon J. Gerraty * Compute the ENCODED actual bit length of an integer. The argument x
13410957b409SSimon J. Gerraty * should point to the first (least significant) value word of the
13420957b409SSimon J. Gerraty * integer. The len 'xlen' contains the number of 32-bit words to
13430957b409SSimon J. Gerraty * access. The upper bit of each value word MUST be 0.
13440957b409SSimon J. Gerraty * Returned value is ((k / 31) << 5) + (k % 31) if the bit length is k.
13450957b409SSimon J. Gerraty *
13460957b409SSimon J. Gerraty * CT: value or length of x does not leak.
13470957b409SSimon J. Gerraty */
13480957b409SSimon J. Gerraty uint32_t br_i31_bit_length(uint32_t *x, size_t xlen);
13490957b409SSimon J. Gerraty
13500957b409SSimon J. Gerraty /*
13510957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation. The
13520957b409SSimon J. Gerraty * "true" bit length of the integer is computed and set in the encoded
13530957b409SSimon J. Gerraty * announced bit length (x[0]), but all words of x[] corresponding to
13540957b409SSimon J. Gerraty * the full 'len' bytes of the source are set.
13550957b409SSimon J. Gerraty *
13560957b409SSimon J. Gerraty * CT: value or length of x does not leak.
13570957b409SSimon J. Gerraty */
13580957b409SSimon J. Gerraty void br_i31_decode(uint32_t *x, const void *src, size_t len);
13590957b409SSimon J. Gerraty
13600957b409SSimon J. Gerraty /*
13610957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation. The
13620957b409SSimon J. Gerraty * integer MUST be lower than m[]; the (encoded) announced bit length
13630957b409SSimon J. Gerraty * written in x[] will be equal to that of m[]. All 'len' bytes from the
13640957b409SSimon J. Gerraty * source are read.
13650957b409SSimon J. Gerraty *
13660957b409SSimon J. Gerraty * Returned value is 1 if the decode value fits within the modulus, 0
13670957b409SSimon J. Gerraty * otherwise. In the latter case, the x[] buffer will be set to 0 (but
13680957b409SSimon J. Gerraty * still with the announced bit length of m[]).
13690957b409SSimon J. Gerraty *
13700957b409SSimon J. Gerraty * CT: value or length of x does not leak. Memory access pattern depends
13710957b409SSimon J. Gerraty * only of 'len' and the announced bit length of m. Whether x fits or
13720957b409SSimon J. Gerraty * not does not leak either.
13730957b409SSimon J. Gerraty */
13740957b409SSimon J. Gerraty uint32_t br_i31_decode_mod(uint32_t *x,
13750957b409SSimon J. Gerraty const void *src, size_t len, const uint32_t *m);
13760957b409SSimon J. Gerraty
13770957b409SSimon J. Gerraty /*
13780957b409SSimon J. Gerraty * Zeroize an integer. The announced bit length is set to the provided
13790957b409SSimon J. Gerraty * value, and the corresponding words are set to 0. The ENCODED bit length
13800957b409SSimon J. Gerraty * is expected here.
13810957b409SSimon J. Gerraty */
13820957b409SSimon J. Gerraty static inline void
br_i31_zero(uint32_t * x,uint32_t bit_len)13830957b409SSimon J. Gerraty br_i31_zero(uint32_t *x, uint32_t bit_len)
13840957b409SSimon J. Gerraty {
13850957b409SSimon J. Gerraty *x ++ = bit_len;
13860957b409SSimon J. Gerraty memset(x, 0, ((bit_len + 31) >> 5) * sizeof *x);
13870957b409SSimon J. Gerraty }
13880957b409SSimon J. Gerraty
13890957b409SSimon J. Gerraty /*
13900957b409SSimon J. Gerraty * Right-shift an integer. The shift amount must be lower than 31
13910957b409SSimon J. Gerraty * bits.
13920957b409SSimon J. Gerraty */
13930957b409SSimon J. Gerraty void br_i31_rshift(uint32_t *x, int count);
13940957b409SSimon J. Gerraty
13950957b409SSimon J. Gerraty /*
13960957b409SSimon J. Gerraty * Reduce an integer (a[]) modulo another (m[]). The result is written
13970957b409SSimon J. Gerraty * in x[] and its announced bit length is set to be equal to that of m[].
13980957b409SSimon J. Gerraty *
13990957b409SSimon J. Gerraty * x[] MUST be distinct from a[] and m[].
14000957b409SSimon J. Gerraty *
14010957b409SSimon J. Gerraty * CT: only announced bit lengths leak, not values of x, a or m.
14020957b409SSimon J. Gerraty */
14030957b409SSimon J. Gerraty void br_i31_reduce(uint32_t *x, const uint32_t *a, const uint32_t *m);
14040957b409SSimon J. Gerraty
14050957b409SSimon J. Gerraty /*
14060957b409SSimon J. Gerraty * Decode an integer from its big-endian unsigned representation, and
14070957b409SSimon J. Gerraty * reduce it modulo the provided modulus m[]. The announced bit length
14080957b409SSimon J. Gerraty * of the result is set to be equal to that of the modulus.
14090957b409SSimon J. Gerraty *
14100957b409SSimon J. Gerraty * x[] MUST be distinct from m[].
14110957b409SSimon J. Gerraty */
14120957b409SSimon J. Gerraty void br_i31_decode_reduce(uint32_t *x,
14130957b409SSimon J. Gerraty const void *src, size_t len, const uint32_t *m);
14140957b409SSimon J. Gerraty
14150957b409SSimon J. Gerraty /*
14160957b409SSimon J. Gerraty * Multiply x[] by 2^31 and then add integer z, modulo m[]. This
14170957b409SSimon J. Gerraty * function assumes that x[] and m[] have the same announced bit
14180957b409SSimon J. Gerraty * length, the announced bit length of m[] matches its true
14190957b409SSimon J. Gerraty * bit length.
14200957b409SSimon J. Gerraty *
14210957b409SSimon J. Gerraty * x[] and m[] MUST be distinct arrays. z MUST fit in 31 bits (upper
14220957b409SSimon J. Gerraty * bit set to 0).
14230957b409SSimon J. Gerraty *
14240957b409SSimon J. Gerraty * CT: only the common announced bit length of x and m leaks, not
14250957b409SSimon J. Gerraty * the values of x, z or m.
14260957b409SSimon J. Gerraty */
14270957b409SSimon J. Gerraty void br_i31_muladd_small(uint32_t *x, uint32_t z, const uint32_t *m);
14280957b409SSimon J. Gerraty
14290957b409SSimon J. Gerraty /*
14300957b409SSimon J. Gerraty * Encode an integer into its big-endian unsigned representation. The
14310957b409SSimon J. Gerraty * output length in bytes is provided (parameter 'len'); if the length
14320957b409SSimon J. Gerraty * is too short then the integer is appropriately truncated; if it is
14330957b409SSimon J. Gerraty * too long then the extra bytes are set to 0.
14340957b409SSimon J. Gerraty */
14350957b409SSimon J. Gerraty void br_i31_encode(void *dst, size_t len, const uint32_t *x);
14360957b409SSimon J. Gerraty
14370957b409SSimon J. Gerraty /*
14380957b409SSimon J. Gerraty * Compute -(1/x) mod 2^31. If x is even, then this function returns 0.
14390957b409SSimon J. Gerraty */
14400957b409SSimon J. Gerraty uint32_t br_i31_ninv31(uint32_t x);
14410957b409SSimon J. Gerraty
14420957b409SSimon J. Gerraty /*
14430957b409SSimon J. Gerraty * Compute a modular Montgomery multiplication. d[] is filled with the
14440957b409SSimon J. Gerraty * value of x*y/R modulo m[] (where R is the Montgomery factor). The
14450957b409SSimon J. Gerraty * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
14460957b409SSimon J. Gerraty * numerically lower than m[]. x[] and y[] MAY be the same array. The
14470957b409SSimon J. Gerraty * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
14480957b409SSimon J. Gerraty * significant value word of m[] (this works only if m[] is an odd
14490957b409SSimon J. Gerraty * integer).
14500957b409SSimon J. Gerraty */
14510957b409SSimon J. Gerraty void br_i31_montymul(uint32_t *d, const uint32_t *x, const uint32_t *y,
14520957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i);
14530957b409SSimon J. Gerraty
14540957b409SSimon J. Gerraty /*
14550957b409SSimon J. Gerraty * Convert a modular integer to Montgomery representation. The integer x[]
14560957b409SSimon J. Gerraty * MUST be lower than m[], but with the same announced bit length.
14570957b409SSimon J. Gerraty */
14580957b409SSimon J. Gerraty void br_i31_to_monty(uint32_t *x, const uint32_t *m);
14590957b409SSimon J. Gerraty
14600957b409SSimon J. Gerraty /*
14610957b409SSimon J. Gerraty * Convert a modular integer back from Montgomery representation. The
14620957b409SSimon J. Gerraty * integer x[] MUST be lower than m[], but with the same announced bit
14630957b409SSimon J. Gerraty * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
14640957b409SSimon J. Gerraty * the least significant value word of m[] (this works only if m[] is
14650957b409SSimon J. Gerraty * an odd integer).
14660957b409SSimon J. Gerraty */
14670957b409SSimon J. Gerraty void br_i31_from_monty(uint32_t *x, const uint32_t *m, uint32_t m0i);
14680957b409SSimon J. Gerraty
14690957b409SSimon J. Gerraty /*
14700957b409SSimon J. Gerraty * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
14710957b409SSimon J. Gerraty * (same announced bit length, lower value). m[] MUST be odd. The
14720957b409SSimon J. Gerraty * exponent is in big-endian unsigned notation, over 'elen' bytes. The
14730957b409SSimon J. Gerraty * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
14740957b409SSimon J. Gerraty * significant value word of m[] (this works only if m[] is an odd
14750957b409SSimon J. Gerraty * integer). The t1[] and t2[] parameters must be temporary arrays,
14760957b409SSimon J. Gerraty * each large enough to accommodate an integer with the same size as m[].
14770957b409SSimon J. Gerraty */
14780957b409SSimon J. Gerraty void br_i31_modpow(uint32_t *x, const unsigned char *e, size_t elen,
14790957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *t1, uint32_t *t2);
14800957b409SSimon J. Gerraty
14810957b409SSimon J. Gerraty /*
14820957b409SSimon J. Gerraty * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
14830957b409SSimon J. Gerraty * (same announced bit length, lower value). m[] MUST be odd. The
14840957b409SSimon J. Gerraty * exponent is in big-endian unsigned notation, over 'elen' bytes. The
14850957b409SSimon J. Gerraty * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
14860957b409SSimon J. Gerraty * significant value word of m[] (this works only if m[] is an odd
14870957b409SSimon J. Gerraty * integer). The tmp[] array is used for temporaries, and has size
14880957b409SSimon J. Gerraty * 'twlen' words; it must be large enough to accommodate at least two
14890957b409SSimon J. Gerraty * temporary values with the same size as m[] (including the leading
14900957b409SSimon J. Gerraty * "bit length" word). If there is room for more temporaries, then this
14910957b409SSimon J. Gerraty * function may use the extra room for window-based optimisation,
14920957b409SSimon J. Gerraty * resulting in faster computations.
14930957b409SSimon J. Gerraty *
14940957b409SSimon J. Gerraty * Returned value is 1 on success, 0 on error. An error is reported if
14950957b409SSimon J. Gerraty * the provided tmp[] array is too short.
14960957b409SSimon J. Gerraty */
14970957b409SSimon J. Gerraty uint32_t br_i31_modpow_opt(uint32_t *x, const unsigned char *e, size_t elen,
14980957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
14990957b409SSimon J. Gerraty
15000957b409SSimon J. Gerraty /*
15010957b409SSimon J. Gerraty * Compute d+a*b, result in d. The initial announced bit length of d[]
15020957b409SSimon J. Gerraty * MUST match that of a[]. The d[] array MUST be large enough to
15030957b409SSimon J. Gerraty * accommodate the full result, plus (possibly) an extra word. The
15040957b409SSimon J. Gerraty * resulting announced bit length of d[] will be the sum of the announced
15050957b409SSimon J. Gerraty * bit lengths of a[] and b[] (therefore, it may be larger than the actual
15060957b409SSimon J. Gerraty * bit length of the numerical result).
15070957b409SSimon J. Gerraty *
15080957b409SSimon J. Gerraty * a[] and b[] may be the same array. d[] must be disjoint from both a[]
15090957b409SSimon J. Gerraty * and b[].
15100957b409SSimon J. Gerraty */
15110957b409SSimon J. Gerraty void br_i31_mulacc(uint32_t *d, const uint32_t *a, const uint32_t *b);
15120957b409SSimon J. Gerraty
15130957b409SSimon J. Gerraty /*
15140957b409SSimon J. Gerraty * Compute x/y mod m, result in x. Values x and y must be between 0 and
15150957b409SSimon J. Gerraty * m-1, and have the same announced bit length as m. Modulus m must be
15160957b409SSimon J. Gerraty * odd. The "m0i" parameter is equal to -1/m mod 2^31. The array 't'
15170957b409SSimon J. Gerraty * must point to a temporary area that can hold at least three integers
15180957b409SSimon J. Gerraty * of the size of m.
15190957b409SSimon J. Gerraty *
15200957b409SSimon J. Gerraty * m may not overlap x and y. x and y may overlap each other (this can
15210957b409SSimon J. Gerraty * be useful to test whether a value is invertible modulo m). t must be
15220957b409SSimon J. Gerraty * disjoint from all other arrays.
15230957b409SSimon J. Gerraty *
15240957b409SSimon J. Gerraty * Returned value is 1 on success, 0 otherwise. Success is attained if
15250957b409SSimon J. Gerraty * y is invertible modulo m.
15260957b409SSimon J. Gerraty */
15270957b409SSimon J. Gerraty uint32_t br_i31_moddiv(uint32_t *x, const uint32_t *y,
15280957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *t);
15290957b409SSimon J. Gerraty
15300957b409SSimon J. Gerraty /* ==================================================================== */
15310957b409SSimon J. Gerraty
15320957b409SSimon J. Gerraty /*
15330957b409SSimon J. Gerraty * FIXME: document "i15" functions.
15340957b409SSimon J. Gerraty */
15350957b409SSimon J. Gerraty
15360957b409SSimon J. Gerraty static inline void
br_i15_zero(uint16_t * x,uint16_t bit_len)15370957b409SSimon J. Gerraty br_i15_zero(uint16_t *x, uint16_t bit_len)
15380957b409SSimon J. Gerraty {
15390957b409SSimon J. Gerraty *x ++ = bit_len;
15400957b409SSimon J. Gerraty memset(x, 0, ((bit_len + 15) >> 4) * sizeof *x);
15410957b409SSimon J. Gerraty }
15420957b409SSimon J. Gerraty
15430957b409SSimon J. Gerraty uint32_t br_i15_iszero(const uint16_t *x);
15440957b409SSimon J. Gerraty
15450957b409SSimon J. Gerraty uint16_t br_i15_ninv15(uint16_t x);
15460957b409SSimon J. Gerraty
15470957b409SSimon J. Gerraty uint32_t br_i15_add(uint16_t *a, const uint16_t *b, uint32_t ctl);
15480957b409SSimon J. Gerraty
15490957b409SSimon J. Gerraty uint32_t br_i15_sub(uint16_t *a, const uint16_t *b, uint32_t ctl);
15500957b409SSimon J. Gerraty
15510957b409SSimon J. Gerraty void br_i15_muladd_small(uint16_t *x, uint16_t z, const uint16_t *m);
15520957b409SSimon J. Gerraty
15530957b409SSimon J. Gerraty void br_i15_montymul(uint16_t *d, const uint16_t *x, const uint16_t *y,
15540957b409SSimon J. Gerraty const uint16_t *m, uint16_t m0i);
15550957b409SSimon J. Gerraty
15560957b409SSimon J. Gerraty void br_i15_to_monty(uint16_t *x, const uint16_t *m);
15570957b409SSimon J. Gerraty
15580957b409SSimon J. Gerraty void br_i15_modpow(uint16_t *x, const unsigned char *e, size_t elen,
15590957b409SSimon J. Gerraty const uint16_t *m, uint16_t m0i, uint16_t *t1, uint16_t *t2);
15600957b409SSimon J. Gerraty
15610957b409SSimon J. Gerraty uint32_t br_i15_modpow_opt(uint16_t *x, const unsigned char *e, size_t elen,
15620957b409SSimon J. Gerraty const uint16_t *m, uint16_t m0i, uint16_t *tmp, size_t twlen);
15630957b409SSimon J. Gerraty
15640957b409SSimon J. Gerraty void br_i15_encode(void *dst, size_t len, const uint16_t *x);
15650957b409SSimon J. Gerraty
15660957b409SSimon J. Gerraty uint32_t br_i15_decode_mod(uint16_t *x,
15670957b409SSimon J. Gerraty const void *src, size_t len, const uint16_t *m);
15680957b409SSimon J. Gerraty
15690957b409SSimon J. Gerraty void br_i15_rshift(uint16_t *x, int count);
15700957b409SSimon J. Gerraty
15710957b409SSimon J. Gerraty uint32_t br_i15_bit_length(uint16_t *x, size_t xlen);
15720957b409SSimon J. Gerraty
15730957b409SSimon J. Gerraty void br_i15_decode(uint16_t *x, const void *src, size_t len);
15740957b409SSimon J. Gerraty
15750957b409SSimon J. Gerraty void br_i15_from_monty(uint16_t *x, const uint16_t *m, uint16_t m0i);
15760957b409SSimon J. Gerraty
15770957b409SSimon J. Gerraty void br_i15_decode_reduce(uint16_t *x,
15780957b409SSimon J. Gerraty const void *src, size_t len, const uint16_t *m);
15790957b409SSimon J. Gerraty
15800957b409SSimon J. Gerraty void br_i15_reduce(uint16_t *x, const uint16_t *a, const uint16_t *m);
15810957b409SSimon J. Gerraty
15820957b409SSimon J. Gerraty void br_i15_mulacc(uint16_t *d, const uint16_t *a, const uint16_t *b);
15830957b409SSimon J. Gerraty
15840957b409SSimon J. Gerraty uint32_t br_i15_moddiv(uint16_t *x, const uint16_t *y,
15850957b409SSimon J. Gerraty const uint16_t *m, uint16_t m0i, uint16_t *t);
15860957b409SSimon J. Gerraty
15870957b409SSimon J. Gerraty /*
15880957b409SSimon J. Gerraty * Variant of br_i31_modpow_opt() that internally uses 64x64->128
15890957b409SSimon J. Gerraty * multiplications. It expects the same parameters as br_i31_modpow_opt(),
15900957b409SSimon J. Gerraty * except that the temporaries should be 64-bit integers, not 32-bit
15910957b409SSimon J. Gerraty * integers.
15920957b409SSimon J. Gerraty */
15930957b409SSimon J. Gerraty uint32_t br_i62_modpow_opt(uint32_t *x31, const unsigned char *e, size_t elen,
15940957b409SSimon J. Gerraty const uint32_t *m31, uint32_t m0i31, uint64_t *tmp, size_t twlen);
15950957b409SSimon J. Gerraty
15960957b409SSimon J. Gerraty /*
15970957b409SSimon J. Gerraty * Type for a function with the same API as br_i31_modpow_opt() (some
15980957b409SSimon J. Gerraty * implementations of this type may have stricter alignment requirements
15990957b409SSimon J. Gerraty * on the temporaries).
16000957b409SSimon J. Gerraty */
16010957b409SSimon J. Gerraty typedef uint32_t (*br_i31_modpow_opt_type)(uint32_t *x,
16020957b409SSimon J. Gerraty const unsigned char *e, size_t elen,
16030957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
16040957b409SSimon J. Gerraty
16050957b409SSimon J. Gerraty /*
16060957b409SSimon J. Gerraty * Wrapper for br_i62_modpow_opt() that uses the same type as
16070957b409SSimon J. Gerraty * br_i31_modpow_opt(); however, it requires its 'tmp' argument to the
16080957b409SSimon J. Gerraty * 64-bit aligned.
16090957b409SSimon J. Gerraty */
16100957b409SSimon J. Gerraty uint32_t br_i62_modpow_opt_as_i31(uint32_t *x,
16110957b409SSimon J. Gerraty const unsigned char *e, size_t elen,
16120957b409SSimon J. Gerraty const uint32_t *m, uint32_t m0i, uint32_t *tmp, size_t twlen);
16130957b409SSimon J. Gerraty
16140957b409SSimon J. Gerraty /* ==================================================================== */
16150957b409SSimon J. Gerraty
16160957b409SSimon J. Gerraty static inline size_t
br_digest_size(const br_hash_class * digest_class)16170957b409SSimon J. Gerraty br_digest_size(const br_hash_class *digest_class)
16180957b409SSimon J. Gerraty {
16190957b409SSimon J. Gerraty return (size_t)(digest_class->desc >> BR_HASHDESC_OUT_OFF)
16200957b409SSimon J. Gerraty & BR_HASHDESC_OUT_MASK;
16210957b409SSimon J. Gerraty }
16220957b409SSimon J. Gerraty
16230957b409SSimon J. Gerraty /*
16240957b409SSimon J. Gerraty * Get the output size (in bytes) of a hash function.
16250957b409SSimon J. Gerraty */
16260957b409SSimon J. Gerraty size_t br_digest_size_by_ID(int digest_id);
16270957b409SSimon J. Gerraty
16280957b409SSimon J. Gerraty /*
16290957b409SSimon J. Gerraty * Get the OID (encoded OBJECT IDENTIFIER value, without tag and length)
16300957b409SSimon J. Gerraty * for a hash function. If digest_id is not a supported digest identifier
16310957b409SSimon J. Gerraty * (in particular if it is equal to 0, i.e. br_md5sha1_ID), then NULL is
16320957b409SSimon J. Gerraty * returned and *len is set to 0.
16330957b409SSimon J. Gerraty */
16340957b409SSimon J. Gerraty const unsigned char *br_digest_OID(int digest_id, size_t *len);
16350957b409SSimon J. Gerraty
16360957b409SSimon J. Gerraty /* ==================================================================== */
16370957b409SSimon J. Gerraty /*
16380957b409SSimon J. Gerraty * DES support functions.
16390957b409SSimon J. Gerraty */
16400957b409SSimon J. Gerraty
16410957b409SSimon J. Gerraty /*
16420957b409SSimon J. Gerraty * Apply DES Initial Permutation.
16430957b409SSimon J. Gerraty */
16440957b409SSimon J. Gerraty void br_des_do_IP(uint32_t *xl, uint32_t *xr);
16450957b409SSimon J. Gerraty
16460957b409SSimon J. Gerraty /*
16470957b409SSimon J. Gerraty * Apply DES Final Permutation (inverse of IP).
16480957b409SSimon J. Gerraty */
16490957b409SSimon J. Gerraty void br_des_do_invIP(uint32_t *xl, uint32_t *xr);
16500957b409SSimon J. Gerraty
16510957b409SSimon J. Gerraty /*
16520957b409SSimon J. Gerraty * Key schedule unit: for a DES key (8 bytes), compute 16 subkeys. Each
16530957b409SSimon J. Gerraty * subkey is two 28-bit words represented as two 32-bit words; the PC-2
16540957b409SSimon J. Gerraty * bit extration is NOT applied.
16550957b409SSimon J. Gerraty */
16560957b409SSimon J. Gerraty void br_des_keysched_unit(uint32_t *skey, const void *key);
16570957b409SSimon J. Gerraty
16580957b409SSimon J. Gerraty /*
16590957b409SSimon J. Gerraty * Reversal of 16 DES sub-keys (for decryption).
16600957b409SSimon J. Gerraty */
16610957b409SSimon J. Gerraty void br_des_rev_skey(uint32_t *skey);
16620957b409SSimon J. Gerraty
16630957b409SSimon J. Gerraty /*
16640957b409SSimon J. Gerraty * DES/3DES key schedule for 'des_tab' (encryption direction). Returned
16650957b409SSimon J. Gerraty * value is the number of rounds.
16660957b409SSimon J. Gerraty */
16670957b409SSimon J. Gerraty unsigned br_des_tab_keysched(uint32_t *skey, const void *key, size_t key_len);
16680957b409SSimon J. Gerraty
16690957b409SSimon J. Gerraty /*
16700957b409SSimon J. Gerraty * DES/3DES key schedule for 'des_ct' (encryption direction). Returned
16710957b409SSimon J. Gerraty * value is the number of rounds.
16720957b409SSimon J. Gerraty */
16730957b409SSimon J. Gerraty unsigned br_des_ct_keysched(uint32_t *skey, const void *key, size_t key_len);
16740957b409SSimon J. Gerraty
16750957b409SSimon J. Gerraty /*
16760957b409SSimon J. Gerraty * DES/3DES subkey decompression (from the compressed bitsliced subkeys).
16770957b409SSimon J. Gerraty */
16780957b409SSimon J. Gerraty void br_des_ct_skey_expand(uint32_t *sk_exp,
16790957b409SSimon J. Gerraty unsigned num_rounds, const uint32_t *skey);
16800957b409SSimon J. Gerraty
16810957b409SSimon J. Gerraty /*
16820957b409SSimon J. Gerraty * DES/3DES block encryption/decryption ('des_tab').
16830957b409SSimon J. Gerraty */
16840957b409SSimon J. Gerraty void br_des_tab_process_block(unsigned num_rounds,
16850957b409SSimon J. Gerraty const uint32_t *skey, void *block);
16860957b409SSimon J. Gerraty
16870957b409SSimon J. Gerraty /*
16880957b409SSimon J. Gerraty * DES/3DES block encryption/decryption ('des_ct').
16890957b409SSimon J. Gerraty */
16900957b409SSimon J. Gerraty void br_des_ct_process_block(unsigned num_rounds,
16910957b409SSimon J. Gerraty const uint32_t *skey, void *block);
16920957b409SSimon J. Gerraty
16930957b409SSimon J. Gerraty /* ==================================================================== */
16940957b409SSimon J. Gerraty /*
16950957b409SSimon J. Gerraty * AES support functions.
16960957b409SSimon J. Gerraty */
16970957b409SSimon J. Gerraty
16980957b409SSimon J. Gerraty /*
16990957b409SSimon J. Gerraty * The AES S-box (256-byte table).
17000957b409SSimon J. Gerraty */
17010957b409SSimon J. Gerraty extern const unsigned char br_aes_S[];
17020957b409SSimon J. Gerraty
17030957b409SSimon J. Gerraty /*
17040957b409SSimon J. Gerraty * AES key schedule. skey[] is filled with n+1 128-bit subkeys, where n
17050957b409SSimon J. Gerraty * is the number of rounds (10 to 14, depending on key size). The number
17060957b409SSimon J. Gerraty * of rounds is returned. If the key size is invalid (not 16, 24 or 32),
17070957b409SSimon J. Gerraty * then 0 is returned.
17080957b409SSimon J. Gerraty *
17090957b409SSimon J. Gerraty * This implementation uses a 256-byte table and is NOT constant-time.
17100957b409SSimon J. Gerraty */
17110957b409SSimon J. Gerraty unsigned br_aes_keysched(uint32_t *skey, const void *key, size_t key_len);
17120957b409SSimon J. Gerraty
17130957b409SSimon J. Gerraty /*
17140957b409SSimon J. Gerraty * AES key schedule for decryption ('aes_big' implementation).
17150957b409SSimon J. Gerraty */
17160957b409SSimon J. Gerraty unsigned br_aes_big_keysched_inv(uint32_t *skey,
17170957b409SSimon J. Gerraty const void *key, size_t key_len);
17180957b409SSimon J. Gerraty
17190957b409SSimon J. Gerraty /*
17200957b409SSimon J. Gerraty * AES block encryption with the 'aes_big' implementation (fast, but
17210957b409SSimon J. Gerraty * not constant-time). This function encrypts a single block "in place".
17220957b409SSimon J. Gerraty */
17230957b409SSimon J. Gerraty void br_aes_big_encrypt(unsigned num_rounds, const uint32_t *skey, void *data);
17240957b409SSimon J. Gerraty
17250957b409SSimon J. Gerraty /*
17260957b409SSimon J. Gerraty * AES block decryption with the 'aes_big' implementation (fast, but
17270957b409SSimon J. Gerraty * not constant-time). This function decrypts a single block "in place".
17280957b409SSimon J. Gerraty */
17290957b409SSimon J. Gerraty void br_aes_big_decrypt(unsigned num_rounds, const uint32_t *skey, void *data);
17300957b409SSimon J. Gerraty
17310957b409SSimon J. Gerraty /*
17320957b409SSimon J. Gerraty * AES block encryption with the 'aes_small' implementation (small, but
17330957b409SSimon J. Gerraty * slow and not constant-time). This function encrypts a single block
17340957b409SSimon J. Gerraty * "in place".
17350957b409SSimon J. Gerraty */
17360957b409SSimon J. Gerraty void br_aes_small_encrypt(unsigned num_rounds,
17370957b409SSimon J. Gerraty const uint32_t *skey, void *data);
17380957b409SSimon J. Gerraty
17390957b409SSimon J. Gerraty /*
17400957b409SSimon J. Gerraty * AES block decryption with the 'aes_small' implementation (small, but
17410957b409SSimon J. Gerraty * slow and not constant-time). This function decrypts a single block
17420957b409SSimon J. Gerraty * "in place".
17430957b409SSimon J. Gerraty */
17440957b409SSimon J. Gerraty void br_aes_small_decrypt(unsigned num_rounds,
17450957b409SSimon J. Gerraty const uint32_t *skey, void *data);
17460957b409SSimon J. Gerraty
17470957b409SSimon J. Gerraty /*
17480957b409SSimon J. Gerraty * The constant-time implementation is "bitsliced": the 128-bit state is
17490957b409SSimon J. Gerraty * split over eight 32-bit words q* in the following way:
17500957b409SSimon J. Gerraty *
17510957b409SSimon J. Gerraty * -- Input block consists in 16 bytes:
17520957b409SSimon J. Gerraty * a00 a10 a20 a30 a01 a11 a21 a31 a02 a12 a22 a32 a03 a13 a23 a33
17530957b409SSimon J. Gerraty * In the terminology of FIPS 197, this is a 4x4 matrix which is read
17540957b409SSimon J. Gerraty * column by column.
17550957b409SSimon J. Gerraty *
17560957b409SSimon J. Gerraty * -- Each byte is split into eight bits which are distributed over the
17570957b409SSimon J. Gerraty * eight words, at the same rank. Thus, for a byte x at rank k, bit 0
17580957b409SSimon J. Gerraty * (least significant) of x will be at rank k in q0 (if that bit is b,
17590957b409SSimon J. Gerraty * then it contributes "b << k" to the value of q0), bit 1 of x will be
17600957b409SSimon J. Gerraty * at rank k in q1, and so on.
17610957b409SSimon J. Gerraty *
17620957b409SSimon J. Gerraty * -- Ranks given to bits are in "row order" and are either all even, or
17630957b409SSimon J. Gerraty * all odd. Two independent AES states are thus interleaved, one using
17640957b409SSimon J. Gerraty * the even ranks, the other the odd ranks. Row order means:
17650957b409SSimon J. Gerraty * a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33
17660957b409SSimon J. Gerraty *
17670957b409SSimon J. Gerraty * Converting input bytes from two AES blocks to bitslice representation
17680957b409SSimon J. Gerraty * is done in the following way:
17690957b409SSimon J. Gerraty * -- Decode first block into the four words q0 q2 q4 q6, in that order,
17700957b409SSimon J. Gerraty * using little-endian convention.
17710957b409SSimon J. Gerraty * -- Decode second block into the four words q1 q3 q5 q7, in that order,
17720957b409SSimon J. Gerraty * using little-endian convention.
17730957b409SSimon J. Gerraty * -- Call br_aes_ct_ortho().
17740957b409SSimon J. Gerraty *
17750957b409SSimon J. Gerraty * Converting back to bytes is done by using the reverse operations. Note
17760957b409SSimon J. Gerraty * that br_aes_ct_ortho() is its own inverse.
17770957b409SSimon J. Gerraty */
17780957b409SSimon J. Gerraty
17790957b409SSimon J. Gerraty /*
17800957b409SSimon J. Gerraty * Perform bytewise orthogonalization of eight 32-bit words. Bytes
17810957b409SSimon J. Gerraty * of q0..q7 are spread over all words: for a byte x that occurs
17820957b409SSimon J. Gerraty * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
17830957b409SSimon J. Gerraty * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
17840957b409SSimon J. Gerraty *
17850957b409SSimon J. Gerraty * This operation is an involution.
17860957b409SSimon J. Gerraty */
17870957b409SSimon J. Gerraty void br_aes_ct_ortho(uint32_t *q);
17880957b409SSimon J. Gerraty
17890957b409SSimon J. Gerraty /*
17900957b409SSimon J. Gerraty * The AES S-box, as a bitsliced constant-time version. The input array
17910957b409SSimon J. Gerraty * consists in eight 32-bit words; 32 S-box instances are computed in
17920957b409SSimon J. Gerraty * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
17930957b409SSimon J. Gerraty * are spread over the words 0 to 7, at the same rank.
17940957b409SSimon J. Gerraty */
17950957b409SSimon J. Gerraty void br_aes_ct_bitslice_Sbox(uint32_t *q);
17960957b409SSimon J. Gerraty
17970957b409SSimon J. Gerraty /*
17980957b409SSimon J. Gerraty * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
17990957b409SSimon J. Gerraty */
18000957b409SSimon J. Gerraty void br_aes_ct_bitslice_invSbox(uint32_t *q);
18010957b409SSimon J. Gerraty
18020957b409SSimon J. Gerraty /*
18030957b409SSimon J. Gerraty * Compute AES encryption on bitsliced data. Since input is stored on
18040957b409SSimon J. Gerraty * eight 32-bit words, two block encryptions are actually performed
18050957b409SSimon J. Gerraty * in parallel.
18060957b409SSimon J. Gerraty */
18070957b409SSimon J. Gerraty void br_aes_ct_bitslice_encrypt(unsigned num_rounds,
18080957b409SSimon J. Gerraty const uint32_t *skey, uint32_t *q);
18090957b409SSimon J. Gerraty
18100957b409SSimon J. Gerraty /*
18110957b409SSimon J. Gerraty * Compute AES decryption on bitsliced data. Since input is stored on
18120957b409SSimon J. Gerraty * eight 32-bit words, two block decryptions are actually performed
18130957b409SSimon J. Gerraty * in parallel.
18140957b409SSimon J. Gerraty */
18150957b409SSimon J. Gerraty void br_aes_ct_bitslice_decrypt(unsigned num_rounds,
18160957b409SSimon J. Gerraty const uint32_t *skey, uint32_t *q);
18170957b409SSimon J. Gerraty
18180957b409SSimon J. Gerraty /*
18190957b409SSimon J. Gerraty * AES key schedule, constant-time version. skey[] is filled with n+1
18200957b409SSimon J. Gerraty * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
18210957b409SSimon J. Gerraty * on key size). The number of rounds is returned. If the key size is
18220957b409SSimon J. Gerraty * invalid (not 16, 24 or 32), then 0 is returned.
18230957b409SSimon J. Gerraty */
18240957b409SSimon J. Gerraty unsigned br_aes_ct_keysched(uint32_t *comp_skey,
18250957b409SSimon J. Gerraty const void *key, size_t key_len);
18260957b409SSimon J. Gerraty
18270957b409SSimon J. Gerraty /*
18280957b409SSimon J. Gerraty * Expand AES subkeys as produced by br_aes_ct_keysched(), into
18290957b409SSimon J. Gerraty * a larger array suitable for br_aes_ct_bitslice_encrypt() and
18300957b409SSimon J. Gerraty * br_aes_ct_bitslice_decrypt().
18310957b409SSimon J. Gerraty */
18320957b409SSimon J. Gerraty void br_aes_ct_skey_expand(uint32_t *skey,
18330957b409SSimon J. Gerraty unsigned num_rounds, const uint32_t *comp_skey);
18340957b409SSimon J. Gerraty
18350957b409SSimon J. Gerraty /*
18360957b409SSimon J. Gerraty * For the ct64 implementation, the same bitslicing technique is used,
18370957b409SSimon J. Gerraty * but four instances are interleaved. First instance uses bits 0, 4,
18380957b409SSimon J. Gerraty * 8, 12,... of each word; second instance uses bits 1, 5, 9, 13,...
18390957b409SSimon J. Gerraty * and so on.
18400957b409SSimon J. Gerraty */
18410957b409SSimon J. Gerraty
18420957b409SSimon J. Gerraty /*
18430957b409SSimon J. Gerraty * Perform bytewise orthogonalization of eight 64-bit words. Bytes
18440957b409SSimon J. Gerraty * of q0..q7 are spread over all words: for a byte x that occurs
18450957b409SSimon J. Gerraty * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
18460957b409SSimon J. Gerraty * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
18470957b409SSimon J. Gerraty *
18480957b409SSimon J. Gerraty * This operation is an involution.
18490957b409SSimon J. Gerraty */
18500957b409SSimon J. Gerraty void br_aes_ct64_ortho(uint64_t *q);
18510957b409SSimon J. Gerraty
18520957b409SSimon J. Gerraty /*
18530957b409SSimon J. Gerraty * Interleave bytes for an AES input block. If input bytes are
18540957b409SSimon J. Gerraty * denoted 0123456789ABCDEF, and have been decoded with little-endian
18550957b409SSimon J. Gerraty * convention (w[0] contains 0123, with '3' being most significant;
18560957b409SSimon J. Gerraty * w[1] contains 4567, and so on), then output word q0 will be
18570957b409SSimon J. Gerraty * set to 08192A3B (again little-endian convention) and q1 will
18580957b409SSimon J. Gerraty * be set to 4C5D6E7F.
18590957b409SSimon J. Gerraty */
18600957b409SSimon J. Gerraty void br_aes_ct64_interleave_in(uint64_t *q0, uint64_t *q1, const uint32_t *w);
18610957b409SSimon J. Gerraty
18620957b409SSimon J. Gerraty /*
18630957b409SSimon J. Gerraty * Perform the opposite of br_aes_ct64_interleave_in().
18640957b409SSimon J. Gerraty */
18650957b409SSimon J. Gerraty void br_aes_ct64_interleave_out(uint32_t *w, uint64_t q0, uint64_t q1);
18660957b409SSimon J. Gerraty
18670957b409SSimon J. Gerraty /*
18680957b409SSimon J. Gerraty * The AES S-box, as a bitsliced constant-time version. The input array
18690957b409SSimon J. Gerraty * consists in eight 64-bit words; 64 S-box instances are computed in
18700957b409SSimon J. Gerraty * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
18710957b409SSimon J. Gerraty * are spread over the words 0 to 7, at the same rank.
18720957b409SSimon J. Gerraty */
18730957b409SSimon J. Gerraty void br_aes_ct64_bitslice_Sbox(uint64_t *q);
18740957b409SSimon J. Gerraty
18750957b409SSimon J. Gerraty /*
18760957b409SSimon J. Gerraty * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
18770957b409SSimon J. Gerraty */
18780957b409SSimon J. Gerraty void br_aes_ct64_bitslice_invSbox(uint64_t *q);
18790957b409SSimon J. Gerraty
18800957b409SSimon J. Gerraty /*
18810957b409SSimon J. Gerraty * Compute AES encryption on bitsliced data. Since input is stored on
18820957b409SSimon J. Gerraty * eight 64-bit words, four block encryptions are actually performed
18830957b409SSimon J. Gerraty * in parallel.
18840957b409SSimon J. Gerraty */
18850957b409SSimon J. Gerraty void br_aes_ct64_bitslice_encrypt(unsigned num_rounds,
18860957b409SSimon J. Gerraty const uint64_t *skey, uint64_t *q);
18870957b409SSimon J. Gerraty
18880957b409SSimon J. Gerraty /*
18890957b409SSimon J. Gerraty * Compute AES decryption on bitsliced data. Since input is stored on
18900957b409SSimon J. Gerraty * eight 64-bit words, four block decryptions are actually performed
18910957b409SSimon J. Gerraty * in parallel.
18920957b409SSimon J. Gerraty */
18930957b409SSimon J. Gerraty void br_aes_ct64_bitslice_decrypt(unsigned num_rounds,
18940957b409SSimon J. Gerraty const uint64_t *skey, uint64_t *q);
18950957b409SSimon J. Gerraty
18960957b409SSimon J. Gerraty /*
18970957b409SSimon J. Gerraty * AES key schedule, constant-time version. skey[] is filled with n+1
18980957b409SSimon J. Gerraty * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
18990957b409SSimon J. Gerraty * on key size). The number of rounds is returned. If the key size is
19000957b409SSimon J. Gerraty * invalid (not 16, 24 or 32), then 0 is returned.
19010957b409SSimon J. Gerraty */
19020957b409SSimon J. Gerraty unsigned br_aes_ct64_keysched(uint64_t *comp_skey,
19030957b409SSimon J. Gerraty const void *key, size_t key_len);
19040957b409SSimon J. Gerraty
19050957b409SSimon J. Gerraty /*
19060957b409SSimon J. Gerraty * Expand AES subkeys as produced by br_aes_ct64_keysched(), into
19070957b409SSimon J. Gerraty * a larger array suitable for br_aes_ct64_bitslice_encrypt() and
19080957b409SSimon J. Gerraty * br_aes_ct64_bitslice_decrypt().
19090957b409SSimon J. Gerraty */
19100957b409SSimon J. Gerraty void br_aes_ct64_skey_expand(uint64_t *skey,
19110957b409SSimon J. Gerraty unsigned num_rounds, const uint64_t *comp_skey);
19120957b409SSimon J. Gerraty
19130957b409SSimon J. Gerraty /*
19140957b409SSimon J. Gerraty * Test support for AES-NI opcodes.
19150957b409SSimon J. Gerraty */
19160957b409SSimon J. Gerraty int br_aes_x86ni_supported(void);
19170957b409SSimon J. Gerraty
19180957b409SSimon J. Gerraty /*
19190957b409SSimon J. Gerraty * AES key schedule, using x86 AES-NI instructions. This yields the
19200957b409SSimon J. Gerraty * subkeys in the encryption direction. Number of rounds is returned.
19210957b409SSimon J. Gerraty * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
19220957b409SSimon J. Gerraty */
19230957b409SSimon J. Gerraty unsigned br_aes_x86ni_keysched_enc(unsigned char *skni,
19240957b409SSimon J. Gerraty const void *key, size_t len);
19250957b409SSimon J. Gerraty
19260957b409SSimon J. Gerraty /*
19270957b409SSimon J. Gerraty * AES key schedule, using x86 AES-NI instructions. This yields the
19280957b409SSimon J. Gerraty * subkeys in the decryption direction. Number of rounds is returned.
19290957b409SSimon J. Gerraty * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
19300957b409SSimon J. Gerraty */
19310957b409SSimon J. Gerraty unsigned br_aes_x86ni_keysched_dec(unsigned char *skni,
19320957b409SSimon J. Gerraty const void *key, size_t len);
19330957b409SSimon J. Gerraty
19340957b409SSimon J. Gerraty /*
19350957b409SSimon J. Gerraty * Test support for AES POWER8 opcodes.
19360957b409SSimon J. Gerraty */
19370957b409SSimon J. Gerraty int br_aes_pwr8_supported(void);
19380957b409SSimon J. Gerraty
19390957b409SSimon J. Gerraty /*
19400957b409SSimon J. Gerraty * AES key schedule, using POWER8 instructions. This yields the
19410957b409SSimon J. Gerraty * subkeys in the encryption direction. Number of rounds is returned.
19420957b409SSimon J. Gerraty * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
19430957b409SSimon J. Gerraty */
19440957b409SSimon J. Gerraty unsigned br_aes_pwr8_keysched(unsigned char *skni,
19450957b409SSimon J. Gerraty const void *key, size_t len);
19460957b409SSimon J. Gerraty
19470957b409SSimon J. Gerraty /* ==================================================================== */
19480957b409SSimon J. Gerraty /*
19490957b409SSimon J. Gerraty * RSA.
19500957b409SSimon J. Gerraty */
19510957b409SSimon J. Gerraty
19520957b409SSimon J. Gerraty /*
19530957b409SSimon J. Gerraty * Apply proper PKCS#1 v1.5 padding (for signatures). 'hash_oid' is
19540957b409SSimon J. Gerraty * the encoded hash function OID, or NULL.
19550957b409SSimon J. Gerraty */
19560957b409SSimon J. Gerraty uint32_t br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid,
19570957b409SSimon J. Gerraty const unsigned char *hash, size_t hash_len,
19580957b409SSimon J. Gerraty uint32_t n_bitlen, unsigned char *x);
19590957b409SSimon J. Gerraty
19600957b409SSimon J. Gerraty /*
19610957b409SSimon J. Gerraty * Check PKCS#1 v1.5 padding (for signatures). 'hash_oid' is the encoded
19620957b409SSimon J. Gerraty * hash function OID, or NULL. The provided 'sig' value is _after_ the
19630957b409SSimon J. Gerraty * modular exponentiation, i.e. it should be the padded hash. On
19640957b409SSimon J. Gerraty * success, the hashed message is extracted.
19650957b409SSimon J. Gerraty */
19660957b409SSimon J. Gerraty uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig, size_t sig_len,
19670957b409SSimon J. Gerraty const unsigned char *hash_oid, size_t hash_len,
19680957b409SSimon J. Gerraty unsigned char *hash_out);
19690957b409SSimon J. Gerraty
19700957b409SSimon J. Gerraty /*
19710957b409SSimon J. Gerraty * Apply proper PSS padding. The 'x' buffer is output only: it
19720957b409SSimon J. Gerraty * receives the value that is to be exponentiated.
19730957b409SSimon J. Gerraty */
19740957b409SSimon J. Gerraty uint32_t br_rsa_pss_sig_pad(const br_prng_class **rng,
19750957b409SSimon J. Gerraty const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
19760957b409SSimon J. Gerraty const unsigned char *hash, size_t salt_len,
19770957b409SSimon J. Gerraty uint32_t n_bitlen, unsigned char *x);
19780957b409SSimon J. Gerraty
19790957b409SSimon J. Gerraty /*
19800957b409SSimon J. Gerraty * Check PSS padding. The provided value is the one _after_
19810957b409SSimon J. Gerraty * the modular exponentiation; it is modified by this function.
19820957b409SSimon J. Gerraty * This function infers the signature length from the public key
19830957b409SSimon J. Gerraty * size, i.e. it assumes that this has already been verified (as
19840957b409SSimon J. Gerraty * part of the exponentiation).
19850957b409SSimon J. Gerraty */
19860957b409SSimon J. Gerraty uint32_t br_rsa_pss_sig_unpad(
19870957b409SSimon J. Gerraty const br_hash_class *hf_data, const br_hash_class *hf_mgf1,
19880957b409SSimon J. Gerraty const unsigned char *hash, size_t salt_len,
19890957b409SSimon J. Gerraty const br_rsa_public_key *pk, unsigned char *x);
19900957b409SSimon J. Gerraty
19910957b409SSimon J. Gerraty /*
19920957b409SSimon J. Gerraty * Apply OAEP padding. Returned value is the actual padded string length,
19930957b409SSimon J. Gerraty * or zero on error.
19940957b409SSimon J. Gerraty */
19950957b409SSimon J. Gerraty size_t br_rsa_oaep_pad(const br_prng_class **rnd, const br_hash_class *dig,
19960957b409SSimon J. Gerraty const void *label, size_t label_len, const br_rsa_public_key *pk,
19970957b409SSimon J. Gerraty void *dst, size_t dst_nax_len, const void *src, size_t src_len);
19980957b409SSimon J. Gerraty
19990957b409SSimon J. Gerraty /*
20000957b409SSimon J. Gerraty * Unravel and check OAEP padding. If the padding is correct, then 1 is
20010957b409SSimon J. Gerraty * returned, '*len' is adjusted to the length of the message, and the
20020957b409SSimon J. Gerraty * data is moved to the start of the 'data' buffer. If the padding is
20030957b409SSimon J. Gerraty * incorrect, then 0 is returned and '*len' is untouched. Either way,
20040957b409SSimon J. Gerraty * the complete buffer contents are altered.
20050957b409SSimon J. Gerraty */
20060957b409SSimon J. Gerraty uint32_t br_rsa_oaep_unpad(const br_hash_class *dig,
20070957b409SSimon J. Gerraty const void *label, size_t label_len, void *data, size_t *len);
20080957b409SSimon J. Gerraty
20090957b409SSimon J. Gerraty /*
20100957b409SSimon J. Gerraty * Compute MGF1 for a given seed, and XOR the output into the provided
20110957b409SSimon J. Gerraty * buffer.
20120957b409SSimon J. Gerraty */
20130957b409SSimon J. Gerraty void br_mgf1_xor(void *data, size_t len,
20140957b409SSimon J. Gerraty const br_hash_class *dig, const void *seed, size_t seed_len);
20150957b409SSimon J. Gerraty
20160957b409SSimon J. Gerraty /*
20170957b409SSimon J. Gerraty * Inner function for RSA key generation; used by the "i31" and "i62"
20180957b409SSimon J. Gerraty * implementations.
20190957b409SSimon J. Gerraty */
20200957b409SSimon J. Gerraty uint32_t br_rsa_i31_keygen_inner(const br_prng_class **rng,
20210957b409SSimon J. Gerraty br_rsa_private_key *sk, void *kbuf_priv,
20220957b409SSimon J. Gerraty br_rsa_public_key *pk, void *kbuf_pub,
20230957b409SSimon J. Gerraty unsigned size, uint32_t pubexp, br_i31_modpow_opt_type mp31);
20240957b409SSimon J. Gerraty
20250957b409SSimon J. Gerraty /* ==================================================================== */
20260957b409SSimon J. Gerraty /*
20270957b409SSimon J. Gerraty * Elliptic curves.
20280957b409SSimon J. Gerraty */
20290957b409SSimon J. Gerraty
20300957b409SSimon J. Gerraty /*
20310957b409SSimon J. Gerraty * Type for generic EC parameters: curve order (unsigned big-endian
20320957b409SSimon J. Gerraty * encoding) and encoded conventional generator.
20330957b409SSimon J. Gerraty */
20340957b409SSimon J. Gerraty typedef struct {
20350957b409SSimon J. Gerraty int curve;
20360957b409SSimon J. Gerraty const unsigned char *order;
20370957b409SSimon J. Gerraty size_t order_len;
20380957b409SSimon J. Gerraty const unsigned char *generator;
20390957b409SSimon J. Gerraty size_t generator_len;
20400957b409SSimon J. Gerraty } br_ec_curve_def;
20410957b409SSimon J. Gerraty
20420957b409SSimon J. Gerraty extern const br_ec_curve_def br_secp256r1;
20430957b409SSimon J. Gerraty extern const br_ec_curve_def br_secp384r1;
20440957b409SSimon J. Gerraty extern const br_ec_curve_def br_secp521r1;
20450957b409SSimon J. Gerraty
20460957b409SSimon J. Gerraty /*
20470957b409SSimon J. Gerraty * For Curve25519, the advertised "order" really is 2^255-1, since the
20480957b409SSimon J. Gerraty * point multipliction function really works over arbitrary 255-bit
20490957b409SSimon J. Gerraty * scalars. This value is only meant as a hint for ECDH key generation;
20500957b409SSimon J. Gerraty * only ECDSA uses the exact curve order, and ECDSA is not used with
20510957b409SSimon J. Gerraty * that specific curve.
20520957b409SSimon J. Gerraty */
20530957b409SSimon J. Gerraty extern const br_ec_curve_def br_curve25519;
20540957b409SSimon J. Gerraty
20550957b409SSimon J. Gerraty /*
20560957b409SSimon J. Gerraty * Decode some bytes as an i31 integer, with truncation (corresponding
20570957b409SSimon J. Gerraty * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
20580957b409SSimon J. Gerraty * length is provided as last parameter. The resulting value will have
20590957b409SSimon J. Gerraty * this declared bit length, and consists the big-endian unsigned decoding
20600957b409SSimon J. Gerraty * of exactly that many bits in the source (capped at the source length).
20610957b409SSimon J. Gerraty */
20620957b409SSimon J. Gerraty void br_ecdsa_i31_bits2int(uint32_t *x,
20630957b409SSimon J. Gerraty const void *src, size_t len, uint32_t ebitlen);
20640957b409SSimon J. Gerraty
20650957b409SSimon J. Gerraty /*
20660957b409SSimon J. Gerraty * Decode some bytes as an i15 integer, with truncation (corresponding
20670957b409SSimon J. Gerraty * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
20680957b409SSimon J. Gerraty * length is provided as last parameter. The resulting value will have
20690957b409SSimon J. Gerraty * this declared bit length, and consists the big-endian unsigned decoding
20700957b409SSimon J. Gerraty * of exactly that many bits in the source (capped at the source length).
20710957b409SSimon J. Gerraty */
20720957b409SSimon J. Gerraty void br_ecdsa_i15_bits2int(uint16_t *x,
20730957b409SSimon J. Gerraty const void *src, size_t len, uint32_t ebitlen);
20740957b409SSimon J. Gerraty
20750957b409SSimon J. Gerraty /* ==================================================================== */
20760957b409SSimon J. Gerraty /*
20770957b409SSimon J. Gerraty * ASN.1 support functions.
20780957b409SSimon J. Gerraty */
20790957b409SSimon J. Gerraty
20800957b409SSimon J. Gerraty /*
20810957b409SSimon J. Gerraty * A br_asn1_uint structure contains encoding information about an
20820957b409SSimon J. Gerraty * INTEGER nonnegative value: pointer to the integer contents (unsigned
20830957b409SSimon J. Gerraty * big-endian representation), length of the integer contents,
20840957b409SSimon J. Gerraty * and length of the encoded value. The data shall have minimal length:
20850957b409SSimon J. Gerraty * - If the integer value is zero, then 'len' must be zero.
20860957b409SSimon J. Gerraty * - If the integer value is not zero, then data[0] must be non-zero.
20870957b409SSimon J. Gerraty *
20880957b409SSimon J. Gerraty * Under these conditions, 'asn1len' is necessarily equal to either len
20890957b409SSimon J. Gerraty * or len+1.
20900957b409SSimon J. Gerraty */
20910957b409SSimon J. Gerraty typedef struct {
20920957b409SSimon J. Gerraty const unsigned char *data;
20930957b409SSimon J. Gerraty size_t len;
20940957b409SSimon J. Gerraty size_t asn1len;
20950957b409SSimon J. Gerraty } br_asn1_uint;
20960957b409SSimon J. Gerraty
20970957b409SSimon J. Gerraty /*
20980957b409SSimon J. Gerraty * Given an encoded integer (unsigned big-endian, with possible leading
20990957b409SSimon J. Gerraty * bytes of value 0), returned the "prepared INTEGER" structure.
21000957b409SSimon J. Gerraty */
21010957b409SSimon J. Gerraty br_asn1_uint br_asn1_uint_prepare(const void *xdata, size_t xlen);
21020957b409SSimon J. Gerraty
21030957b409SSimon J. Gerraty /*
21040957b409SSimon J. Gerraty * Encode an ASN.1 length. The length of the encoded length is returned.
21050957b409SSimon J. Gerraty * If 'dest' is NULL, then no encoding is performed, but the length of
21060957b409SSimon J. Gerraty * the encoded length is still computed and returned.
21070957b409SSimon J. Gerraty */
21080957b409SSimon J. Gerraty size_t br_asn1_encode_length(void *dest, size_t len);
21090957b409SSimon J. Gerraty
21100957b409SSimon J. Gerraty /*
21110957b409SSimon J. Gerraty * Convenient macro for computing lengths of lengths.
21120957b409SSimon J. Gerraty */
21130957b409SSimon J. Gerraty #define len_of_len(len) br_asn1_encode_length(NULL, len)
21140957b409SSimon J. Gerraty
21150957b409SSimon J. Gerraty /*
21160957b409SSimon J. Gerraty * Encode a (prepared) ASN.1 INTEGER. The encoded length is returned.
21170957b409SSimon J. Gerraty * If 'dest' is NULL, then no encoding is performed, but the length of
21180957b409SSimon J. Gerraty * the encoded integer is still computed and returned.
21190957b409SSimon J. Gerraty */
21200957b409SSimon J. Gerraty size_t br_asn1_encode_uint(void *dest, br_asn1_uint pp);
21210957b409SSimon J. Gerraty
21220957b409SSimon J. Gerraty /*
21230957b409SSimon J. Gerraty * Get the OID that identifies an elliptic curve. Returned value is
21240957b409SSimon J. Gerraty * the DER-encoded OID, with the length (always one byte) but without
21250957b409SSimon J. Gerraty * the tag. Thus, the first byte of the returned buffer contains the
21260957b409SSimon J. Gerraty * number of subsequent bytes in the value. If the curve is not
21270957b409SSimon J. Gerraty * recognised, NULL is returned.
21280957b409SSimon J. Gerraty */
21290957b409SSimon J. Gerraty const unsigned char *br_get_curve_OID(int curve);
21300957b409SSimon J. Gerraty
21310957b409SSimon J. Gerraty /*
21320957b409SSimon J. Gerraty * Inner function for EC private key encoding. This is equivalent to
21330957b409SSimon J. Gerraty * the API function br_encode_ec_raw_der(), except for an extra
21340957b409SSimon J. Gerraty * parameter: if 'include_curve_oid' is zero, then the curve OID is
21350957b409SSimon J. Gerraty * _not_ included in the output blob (this is for PKCS#8 support).
21360957b409SSimon J. Gerraty */
21370957b409SSimon J. Gerraty size_t br_encode_ec_raw_der_inner(void *dest,
21380957b409SSimon J. Gerraty const br_ec_private_key *sk, const br_ec_public_key *pk,
21390957b409SSimon J. Gerraty int include_curve_oid);
21400957b409SSimon J. Gerraty
21410957b409SSimon J. Gerraty /* ==================================================================== */
21420957b409SSimon J. Gerraty /*
21430957b409SSimon J. Gerraty * SSL/TLS support functions.
21440957b409SSimon J. Gerraty */
21450957b409SSimon J. Gerraty
21460957b409SSimon J. Gerraty /*
21470957b409SSimon J. Gerraty * Record types.
21480957b409SSimon J. Gerraty */
21490957b409SSimon J. Gerraty #define BR_SSL_CHANGE_CIPHER_SPEC 20
21500957b409SSimon J. Gerraty #define BR_SSL_ALERT 21
21510957b409SSimon J. Gerraty #define BR_SSL_HANDSHAKE 22
21520957b409SSimon J. Gerraty #define BR_SSL_APPLICATION_DATA 23
21530957b409SSimon J. Gerraty
21540957b409SSimon J. Gerraty /*
21550957b409SSimon J. Gerraty * Handshake message types.
21560957b409SSimon J. Gerraty */
21570957b409SSimon J. Gerraty #define BR_SSL_HELLO_REQUEST 0
21580957b409SSimon J. Gerraty #define BR_SSL_CLIENT_HELLO 1
21590957b409SSimon J. Gerraty #define BR_SSL_SERVER_HELLO 2
21600957b409SSimon J. Gerraty #define BR_SSL_CERTIFICATE 11
21610957b409SSimon J. Gerraty #define BR_SSL_SERVER_KEY_EXCHANGE 12
21620957b409SSimon J. Gerraty #define BR_SSL_CERTIFICATE_REQUEST 13
21630957b409SSimon J. Gerraty #define BR_SSL_SERVER_HELLO_DONE 14
21640957b409SSimon J. Gerraty #define BR_SSL_CERTIFICATE_VERIFY 15
21650957b409SSimon J. Gerraty #define BR_SSL_CLIENT_KEY_EXCHANGE 16
21660957b409SSimon J. Gerraty #define BR_SSL_FINISHED 20
21670957b409SSimon J. Gerraty
21680957b409SSimon J. Gerraty /*
21690957b409SSimon J. Gerraty * Alert levels.
21700957b409SSimon J. Gerraty */
21710957b409SSimon J. Gerraty #define BR_LEVEL_WARNING 1
21720957b409SSimon J. Gerraty #define BR_LEVEL_FATAL 2
21730957b409SSimon J. Gerraty
21740957b409SSimon J. Gerraty /*
21750957b409SSimon J. Gerraty * Low-level I/O state.
21760957b409SSimon J. Gerraty */
21770957b409SSimon J. Gerraty #define BR_IO_FAILED 0
21780957b409SSimon J. Gerraty #define BR_IO_IN 1
21790957b409SSimon J. Gerraty #define BR_IO_OUT 2
21800957b409SSimon J. Gerraty #define BR_IO_INOUT 3
21810957b409SSimon J. Gerraty
21820957b409SSimon J. Gerraty /*
21830957b409SSimon J. Gerraty * Mark a SSL engine as failed. The provided error code is recorded if
21840957b409SSimon J. Gerraty * the engine was not already marked as failed. If 'err' is 0, then the
21850957b409SSimon J. Gerraty * engine is marked as closed (without error).
21860957b409SSimon J. Gerraty */
21870957b409SSimon J. Gerraty void br_ssl_engine_fail(br_ssl_engine_context *cc, int err);
21880957b409SSimon J. Gerraty
21890957b409SSimon J. Gerraty /*
21900957b409SSimon J. Gerraty * Test whether the engine is closed (normally or as a failure).
21910957b409SSimon J. Gerraty */
21920957b409SSimon J. Gerraty static inline int
br_ssl_engine_closed(const br_ssl_engine_context * cc)21930957b409SSimon J. Gerraty br_ssl_engine_closed(const br_ssl_engine_context *cc)
21940957b409SSimon J. Gerraty {
21950957b409SSimon J. Gerraty return cc->iomode == BR_IO_FAILED;
21960957b409SSimon J. Gerraty }
21970957b409SSimon J. Gerraty
21980957b409SSimon J. Gerraty /*
21990957b409SSimon J. Gerraty * Configure a new maximum fragment length. If possible, the maximum
22000957b409SSimon J. Gerraty * length for outgoing records is immediately adjusted (if there are
22010957b409SSimon J. Gerraty * not already too many buffered bytes for that).
22020957b409SSimon J. Gerraty */
22030957b409SSimon J. Gerraty void br_ssl_engine_new_max_frag_len(
22040957b409SSimon J. Gerraty br_ssl_engine_context *rc, unsigned max_frag_len);
22050957b409SSimon J. Gerraty
22060957b409SSimon J. Gerraty /*
22070957b409SSimon J. Gerraty * Test whether the current incoming record has been fully received
22080957b409SSimon J. Gerraty * or not. This functions returns 0 only if a complete record header
22090957b409SSimon J. Gerraty * has been received, but some of the (possibly encrypted) payload
22100957b409SSimon J. Gerraty * has not yet been obtained.
22110957b409SSimon J. Gerraty */
22120957b409SSimon J. Gerraty int br_ssl_engine_recvrec_finished(const br_ssl_engine_context *rc);
22130957b409SSimon J. Gerraty
22140957b409SSimon J. Gerraty /*
22150957b409SSimon J. Gerraty * Flush the current record (if not empty). This is meant to be called
22160957b409SSimon J. Gerraty * from the handshake processor only.
22170957b409SSimon J. Gerraty */
22180957b409SSimon J. Gerraty void br_ssl_engine_flush_record(br_ssl_engine_context *cc);
22190957b409SSimon J. Gerraty
22200957b409SSimon J. Gerraty /*
22210957b409SSimon J. Gerraty * Test whether there is some accumulated payload to send.
22220957b409SSimon J. Gerraty */
22230957b409SSimon J. Gerraty static inline int
br_ssl_engine_has_pld_to_send(const br_ssl_engine_context * rc)22240957b409SSimon J. Gerraty br_ssl_engine_has_pld_to_send(const br_ssl_engine_context *rc)
22250957b409SSimon J. Gerraty {
22260957b409SSimon J. Gerraty return rc->oxa != rc->oxb && rc->oxa != rc->oxc;
22270957b409SSimon J. Gerraty }
22280957b409SSimon J. Gerraty
22290957b409SSimon J. Gerraty /*
22300957b409SSimon J. Gerraty * Initialize RNG in engine. Returned value is 1 on success, 0 on error.
22310957b409SSimon J. Gerraty * This function will try to use the OS-provided RNG, if available. If
22320957b409SSimon J. Gerraty * there is no OS-provided RNG, or if it failed, and no entropy was
22330957b409SSimon J. Gerraty * injected by the caller, then a failure will be reported. On error,
22340957b409SSimon J. Gerraty * the context error code is set.
22350957b409SSimon J. Gerraty */
22360957b409SSimon J. Gerraty int br_ssl_engine_init_rand(br_ssl_engine_context *cc);
22370957b409SSimon J. Gerraty
22380957b409SSimon J. Gerraty /*
22390957b409SSimon J. Gerraty * Reset the handshake-related parts of the engine.
22400957b409SSimon J. Gerraty */
22410957b409SSimon J. Gerraty void br_ssl_engine_hs_reset(br_ssl_engine_context *cc,
22420957b409SSimon J. Gerraty void (*hsinit)(void *), void (*hsrun)(void *));
22430957b409SSimon J. Gerraty
22440957b409SSimon J. Gerraty /*
22450957b409SSimon J. Gerraty * Get the PRF to use for this context, for the provided PRF hash
22460957b409SSimon J. Gerraty * function ID.
22470957b409SSimon J. Gerraty */
22480957b409SSimon J. Gerraty br_tls_prf_impl br_ssl_engine_get_PRF(br_ssl_engine_context *cc, int prf_id);
22490957b409SSimon J. Gerraty
22500957b409SSimon J. Gerraty /*
22510957b409SSimon J. Gerraty * Consume the provided pre-master secret and compute the corresponding
22520957b409SSimon J. Gerraty * master secret. The 'prf_id' is the ID of the hash function to use
22530957b409SSimon J. Gerraty * with the TLS 1.2 PRF (ignored if the version is TLS 1.0 or 1.1).
22540957b409SSimon J. Gerraty */
22550957b409SSimon J. Gerraty void br_ssl_engine_compute_master(br_ssl_engine_context *cc,
22560957b409SSimon J. Gerraty int prf_id, const void *pms, size_t len);
22570957b409SSimon J. Gerraty
22580957b409SSimon J. Gerraty /*
22590957b409SSimon J. Gerraty * Switch to CBC decryption for incoming records.
22600957b409SSimon J. Gerraty * cc the engine context
22610957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
22620957b409SSimon J. Gerraty * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
22630957b409SSimon J. Gerraty * mac_id id of hash function for HMAC
22640957b409SSimon J. Gerraty * bc_impl block cipher implementation (CBC decryption)
22650957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
22660957b409SSimon J. Gerraty */
22670957b409SSimon J. Gerraty void br_ssl_engine_switch_cbc_in(br_ssl_engine_context *cc,
22680957b409SSimon J. Gerraty int is_client, int prf_id, int mac_id,
22690957b409SSimon J. Gerraty const br_block_cbcdec_class *bc_impl, size_t cipher_key_len);
22700957b409SSimon J. Gerraty
22710957b409SSimon J. Gerraty /*
22720957b409SSimon J. Gerraty * Switch to CBC encryption for outgoing records.
22730957b409SSimon J. Gerraty * cc the engine context
22740957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
22750957b409SSimon J. Gerraty * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
22760957b409SSimon J. Gerraty * mac_id id of hash function for HMAC
22770957b409SSimon J. Gerraty * bc_impl block cipher implementation (CBC encryption)
22780957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
22790957b409SSimon J. Gerraty */
22800957b409SSimon J. Gerraty void br_ssl_engine_switch_cbc_out(br_ssl_engine_context *cc,
22810957b409SSimon J. Gerraty int is_client, int prf_id, int mac_id,
22820957b409SSimon J. Gerraty const br_block_cbcenc_class *bc_impl, size_t cipher_key_len);
22830957b409SSimon J. Gerraty
22840957b409SSimon J. Gerraty /*
22850957b409SSimon J. Gerraty * Switch to GCM decryption for incoming records.
22860957b409SSimon J. Gerraty * cc the engine context
22870957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
22880957b409SSimon J. Gerraty * prf_id id of hash function for PRF
22890957b409SSimon J. Gerraty * bc_impl block cipher implementation (CTR)
22900957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
22910957b409SSimon J. Gerraty */
22920957b409SSimon J. Gerraty void br_ssl_engine_switch_gcm_in(br_ssl_engine_context *cc,
22930957b409SSimon J. Gerraty int is_client, int prf_id,
22940957b409SSimon J. Gerraty const br_block_ctr_class *bc_impl, size_t cipher_key_len);
22950957b409SSimon J. Gerraty
22960957b409SSimon J. Gerraty /*
22970957b409SSimon J. Gerraty * Switch to GCM encryption for outgoing records.
22980957b409SSimon J. Gerraty * cc the engine context
22990957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
23000957b409SSimon J. Gerraty * prf_id id of hash function for PRF
23010957b409SSimon J. Gerraty * bc_impl block cipher implementation (CTR)
23020957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
23030957b409SSimon J. Gerraty */
23040957b409SSimon J. Gerraty void br_ssl_engine_switch_gcm_out(br_ssl_engine_context *cc,
23050957b409SSimon J. Gerraty int is_client, int prf_id,
23060957b409SSimon J. Gerraty const br_block_ctr_class *bc_impl, size_t cipher_key_len);
23070957b409SSimon J. Gerraty
23080957b409SSimon J. Gerraty /*
23090957b409SSimon J. Gerraty * Switch to ChaCha20+Poly1305 decryption for incoming records.
23100957b409SSimon J. Gerraty * cc the engine context
23110957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
23120957b409SSimon J. Gerraty * prf_id id of hash function for PRF
23130957b409SSimon J. Gerraty */
23140957b409SSimon J. Gerraty void br_ssl_engine_switch_chapol_in(br_ssl_engine_context *cc,
23150957b409SSimon J. Gerraty int is_client, int prf_id);
23160957b409SSimon J. Gerraty
23170957b409SSimon J. Gerraty /*
23180957b409SSimon J. Gerraty * Switch to ChaCha20+Poly1305 encryption for outgoing records.
23190957b409SSimon J. Gerraty * cc the engine context
23200957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
23210957b409SSimon J. Gerraty * prf_id id of hash function for PRF
23220957b409SSimon J. Gerraty */
23230957b409SSimon J. Gerraty void br_ssl_engine_switch_chapol_out(br_ssl_engine_context *cc,
23240957b409SSimon J. Gerraty int is_client, int prf_id);
23250957b409SSimon J. Gerraty
23260957b409SSimon J. Gerraty /*
23270957b409SSimon J. Gerraty * Switch to CCM decryption for incoming records.
23280957b409SSimon J. Gerraty * cc the engine context
23290957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
23300957b409SSimon J. Gerraty * prf_id id of hash function for PRF
23310957b409SSimon J. Gerraty * bc_impl block cipher implementation (CTR+CBC)
23320957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
23330957b409SSimon J. Gerraty * tag_len tag length (in bytes)
23340957b409SSimon J. Gerraty */
23350957b409SSimon J. Gerraty void br_ssl_engine_switch_ccm_in(br_ssl_engine_context *cc,
23360957b409SSimon J. Gerraty int is_client, int prf_id,
23370957b409SSimon J. Gerraty const br_block_ctrcbc_class *bc_impl,
23380957b409SSimon J. Gerraty size_t cipher_key_len, size_t tag_len);
23390957b409SSimon J. Gerraty
23400957b409SSimon J. Gerraty /*
23410957b409SSimon J. Gerraty * Switch to GCM encryption for outgoing records.
23420957b409SSimon J. Gerraty * cc the engine context
23430957b409SSimon J. Gerraty * is_client non-zero for a client, zero for a server
23440957b409SSimon J. Gerraty * prf_id id of hash function for PRF
23450957b409SSimon J. Gerraty * bc_impl block cipher implementation (CTR+CBC)
23460957b409SSimon J. Gerraty * cipher_key_len block cipher key length (in bytes)
23470957b409SSimon J. Gerraty * tag_len tag length (in bytes)
23480957b409SSimon J. Gerraty */
23490957b409SSimon J. Gerraty void br_ssl_engine_switch_ccm_out(br_ssl_engine_context *cc,
23500957b409SSimon J. Gerraty int is_client, int prf_id,
23510957b409SSimon J. Gerraty const br_block_ctrcbc_class *bc_impl,
23520957b409SSimon J. Gerraty size_t cipher_key_len, size_t tag_len);
23530957b409SSimon J. Gerraty
23540957b409SSimon J. Gerraty /*
23550957b409SSimon J. Gerraty * Calls to T0-generated code.
23560957b409SSimon J. Gerraty */
23570957b409SSimon J. Gerraty void br_ssl_hs_client_init_main(void *ctx);
23580957b409SSimon J. Gerraty void br_ssl_hs_client_run(void *ctx);
23590957b409SSimon J. Gerraty void br_ssl_hs_server_init_main(void *ctx);
23600957b409SSimon J. Gerraty void br_ssl_hs_server_run(void *ctx);
23610957b409SSimon J. Gerraty
23620957b409SSimon J. Gerraty /*
23630957b409SSimon J. Gerraty * Get the hash function to use for signatures, given a bit mask of
23640957b409SSimon J. Gerraty * supported hash functions. This implements a strict choice order
23650957b409SSimon J. Gerraty * (namely SHA-256, SHA-384, SHA-512, SHA-224, SHA-1). If the mask
23660957b409SSimon J. Gerraty * does not document support of any of these hash functions, then this
23670957b409SSimon J. Gerraty * functions returns 0.
23680957b409SSimon J. Gerraty */
23690957b409SSimon J. Gerraty int br_ssl_choose_hash(unsigned bf);
23700957b409SSimon J. Gerraty
23710957b409SSimon J. Gerraty /* ==================================================================== */
23720957b409SSimon J. Gerraty
23730957b409SSimon J. Gerraty /*
23740957b409SSimon J. Gerraty * PowerPC / POWER assembly stuff. The special BR_POWER_ASM_MACROS macro
23750957b409SSimon J. Gerraty * must be defined before including this file; this is done by source
23760957b409SSimon J. Gerraty * files that use some inline assembly for PowerPC / POWER machines.
23770957b409SSimon J. Gerraty */
23780957b409SSimon J. Gerraty
23790957b409SSimon J. Gerraty #if BR_POWER_ASM_MACROS
23800957b409SSimon J. Gerraty
23810957b409SSimon J. Gerraty #define lxvw4x(xt, ra, rb) lxvw4x_(xt, ra, rb)
23820957b409SSimon J. Gerraty #define stxvw4x(xt, ra, rb) stxvw4x_(xt, ra, rb)
23830957b409SSimon J. Gerraty
23840957b409SSimon J. Gerraty #define bdnz(foo) bdnz_(foo)
23850957b409SSimon J. Gerraty #define bdz(foo) bdz_(foo)
23860957b409SSimon J. Gerraty #define beq(foo) beq_(foo)
23870957b409SSimon J. Gerraty
23880957b409SSimon J. Gerraty #define li(rx, value) li_(rx, value)
23890957b409SSimon J. Gerraty #define addi(rx, ra, imm) addi_(rx, ra, imm)
23900957b409SSimon J. Gerraty #define cmpldi(rx, imm) cmpldi_(rx, imm)
23910957b409SSimon J. Gerraty #define mtctr(rx) mtctr_(rx)
23920957b409SSimon J. Gerraty #define vspltb(vrt, vrb, uim) vspltb_(vrt, vrb, uim)
23930957b409SSimon J. Gerraty #define vspltw(vrt, vrb, uim) vspltw_(vrt, vrb, uim)
23940957b409SSimon J. Gerraty #define vspltisb(vrt, imm) vspltisb_(vrt, imm)
23950957b409SSimon J. Gerraty #define vspltisw(vrt, imm) vspltisw_(vrt, imm)
23960957b409SSimon J. Gerraty #define vrlw(vrt, vra, vrb) vrlw_(vrt, vra, vrb)
23970957b409SSimon J. Gerraty #define vsbox(vrt, vra) vsbox_(vrt, vra)
23980957b409SSimon J. Gerraty #define vxor(vrt, vra, vrb) vxor_(vrt, vra, vrb)
23990957b409SSimon J. Gerraty #define vand(vrt, vra, vrb) vand_(vrt, vra, vrb)
24000957b409SSimon J. Gerraty #define vsro(vrt, vra, vrb) vsro_(vrt, vra, vrb)
24010957b409SSimon J. Gerraty #define vsl(vrt, vra, vrb) vsl_(vrt, vra, vrb)
24020957b409SSimon J. Gerraty #define vsldoi(vt, va, vb, sh) vsldoi_(vt, va, vb, sh)
24030957b409SSimon J. Gerraty #define vsr(vrt, vra, vrb) vsr_(vrt, vra, vrb)
24040957b409SSimon J. Gerraty #define vaddcuw(vrt, vra, vrb) vaddcuw_(vrt, vra, vrb)
24050957b409SSimon J. Gerraty #define vadduwm(vrt, vra, vrb) vadduwm_(vrt, vra, vrb)
24060957b409SSimon J. Gerraty #define vsububm(vrt, vra, vrb) vsububm_(vrt, vra, vrb)
24070957b409SSimon J. Gerraty #define vsubuwm(vrt, vra, vrb) vsubuwm_(vrt, vra, vrb)
24080957b409SSimon J. Gerraty #define vsrw(vrt, vra, vrb) vsrw_(vrt, vra, vrb)
24090957b409SSimon J. Gerraty #define vcipher(vt, va, vb) vcipher_(vt, va, vb)
24100957b409SSimon J. Gerraty #define vcipherlast(vt, va, vb) vcipherlast_(vt, va, vb)
24110957b409SSimon J. Gerraty #define vncipher(vt, va, vb) vncipher_(vt, va, vb)
24120957b409SSimon J. Gerraty #define vncipherlast(vt, va, vb) vncipherlast_(vt, va, vb)
24130957b409SSimon J. Gerraty #define vperm(vt, va, vb, vc) vperm_(vt, va, vb, vc)
24140957b409SSimon J. Gerraty #define vpmsumd(vt, va, vb) vpmsumd_(vt, va, vb)
24150957b409SSimon J. Gerraty #define xxpermdi(vt, va, vb, d) xxpermdi_(vt, va, vb, d)
24160957b409SSimon J. Gerraty
24170957b409SSimon J. Gerraty #define lxvw4x_(xt, ra, rb) "\tlxvw4x\t" #xt "," #ra "," #rb "\n"
24180957b409SSimon J. Gerraty #define stxvw4x_(xt, ra, rb) "\tstxvw4x\t" #xt "," #ra "," #rb "\n"
24190957b409SSimon J. Gerraty
24200957b409SSimon J. Gerraty #define label(foo) #foo "%=:\n"
24210957b409SSimon J. Gerraty #define bdnz_(foo) "\tbdnz\t" #foo "%=\n"
24220957b409SSimon J. Gerraty #define bdz_(foo) "\tbdz\t" #foo "%=\n"
24230957b409SSimon J. Gerraty #define beq_(foo) "\tbeq\t" #foo "%=\n"
24240957b409SSimon J. Gerraty
24250957b409SSimon J. Gerraty #define li_(rx, value) "\tli\t" #rx "," #value "\n"
24260957b409SSimon J. Gerraty #define addi_(rx, ra, imm) "\taddi\t" #rx "," #ra "," #imm "\n"
24270957b409SSimon J. Gerraty #define cmpldi_(rx, imm) "\tcmpldi\t" #rx "," #imm "\n"
24280957b409SSimon J. Gerraty #define mtctr_(rx) "\tmtctr\t" #rx "\n"
24290957b409SSimon J. Gerraty #define vspltb_(vrt, vrb, uim) "\tvspltb\t" #vrt "," #vrb "," #uim "\n"
24300957b409SSimon J. Gerraty #define vspltw_(vrt, vrb, uim) "\tvspltw\t" #vrt "," #vrb "," #uim "\n"
24310957b409SSimon J. Gerraty #define vspltisb_(vrt, imm) "\tvspltisb\t" #vrt "," #imm "\n"
24320957b409SSimon J. Gerraty #define vspltisw_(vrt, imm) "\tvspltisw\t" #vrt "," #imm "\n"
24330957b409SSimon J. Gerraty #define vrlw_(vrt, vra, vrb) "\tvrlw\t" #vrt "," #vra "," #vrb "\n"
24340957b409SSimon J. Gerraty #define vsbox_(vrt, vra) "\tvsbox\t" #vrt "," #vra "\n"
24350957b409SSimon J. Gerraty #define vxor_(vrt, vra, vrb) "\tvxor\t" #vrt "," #vra "," #vrb "\n"
24360957b409SSimon J. Gerraty #define vand_(vrt, vra, vrb) "\tvand\t" #vrt "," #vra "," #vrb "\n"
24370957b409SSimon J. Gerraty #define vsro_(vrt, vra, vrb) "\tvsro\t" #vrt "," #vra "," #vrb "\n"
24380957b409SSimon J. Gerraty #define vsl_(vrt, vra, vrb) "\tvsl\t" #vrt "," #vra "," #vrb "\n"
24390957b409SSimon J. Gerraty #define vsldoi_(vt, va, vb, sh) "\tvsldoi\t" #vt "," #va "," #vb "," #sh "\n"
24400957b409SSimon J. Gerraty #define vsr_(vrt, vra, vrb) "\tvsr\t" #vrt "," #vra "," #vrb "\n"
24410957b409SSimon J. Gerraty #define vaddcuw_(vrt, vra, vrb) "\tvaddcuw\t" #vrt "," #vra "," #vrb "\n"
24420957b409SSimon J. Gerraty #define vadduwm_(vrt, vra, vrb) "\tvadduwm\t" #vrt "," #vra "," #vrb "\n"
24430957b409SSimon J. Gerraty #define vsububm_(vrt, vra, vrb) "\tvsububm\t" #vrt "," #vra "," #vrb "\n"
24440957b409SSimon J. Gerraty #define vsubuwm_(vrt, vra, vrb) "\tvsubuwm\t" #vrt "," #vra "," #vrb "\n"
24450957b409SSimon J. Gerraty #define vsrw_(vrt, vra, vrb) "\tvsrw\t" #vrt "," #vra "," #vrb "\n"
24460957b409SSimon J. Gerraty #define vcipher_(vt, va, vb) "\tvcipher\t" #vt "," #va "," #vb "\n"
24470957b409SSimon J. Gerraty #define vcipherlast_(vt, va, vb) "\tvcipherlast\t" #vt "," #va "," #vb "\n"
24480957b409SSimon J. Gerraty #define vncipher_(vt, va, vb) "\tvncipher\t" #vt "," #va "," #vb "\n"
24490957b409SSimon J. Gerraty #define vncipherlast_(vt, va, vb) "\tvncipherlast\t" #vt "," #va "," #vb "\n"
24500957b409SSimon J. Gerraty #define vperm_(vt, va, vb, vc) "\tvperm\t" #vt "," #va "," #vb "," #vc "\n"
24510957b409SSimon J. Gerraty #define vpmsumd_(vt, va, vb) "\tvpmsumd\t" #vt "," #va "," #vb "\n"
24520957b409SSimon J. Gerraty #define xxpermdi_(vt, va, vb, d) "\txxpermdi\t" #vt "," #va "," #vb "," #d "\n"
24530957b409SSimon J. Gerraty
24540957b409SSimon J. Gerraty #endif
24550957b409SSimon J. Gerraty
24560957b409SSimon J. Gerraty /* ==================================================================== */
24570957b409SSimon J. Gerraty /*
24580957b409SSimon J. Gerraty * Special "activate intrinsics" code, needed for some compiler versions.
24590957b409SSimon J. Gerraty * This is defined at the end of this file, so that it won't impact any
24600957b409SSimon J. Gerraty * of the inline functions defined previously; and it is controlled by
24610957b409SSimon J. Gerraty * a specific macro defined in the caller code.
24620957b409SSimon J. Gerraty *
24630957b409SSimon J. Gerraty * Calling code conventions:
24640957b409SSimon J. Gerraty *
24650957b409SSimon J. Gerraty * - Caller must define BR_ENABLE_INTRINSICS before including "inner.h".
24660957b409SSimon J. Gerraty * - Functions that use intrinsics must be enclosed in an "enabled"
24670957b409SSimon J. Gerraty * region (between BR_TARGETS_X86_UP and BR_TARGETS_X86_DOWN).
24680957b409SSimon J. Gerraty * - Functions that use intrinsics must be tagged with the appropriate
24690957b409SSimon J. Gerraty * BR_TARGET().
24700957b409SSimon J. Gerraty */
24710957b409SSimon J. Gerraty
24720957b409SSimon J. Gerraty #if BR_ENABLE_INTRINSICS && (BR_GCC_4_4 || BR_CLANG_3_7 || BR_MSC_2005)
24730957b409SSimon J. Gerraty
24740957b409SSimon J. Gerraty /*
24750957b409SSimon J. Gerraty * x86 intrinsics (both 32-bit and 64-bit).
24760957b409SSimon J. Gerraty */
24770957b409SSimon J. Gerraty #if BR_i386 || BR_amd64
24780957b409SSimon J. Gerraty
24790957b409SSimon J. Gerraty /*
24800957b409SSimon J. Gerraty * On GCC before version 5.0, we need to use the pragma to enable the
24810957b409SSimon J. Gerraty * target options globally, because the 'target' function attribute
24820957b409SSimon J. Gerraty * appears to be unreliable. Before 4.6 we must also avoid the
24830957b409SSimon J. Gerraty * push_options / pop_options mechanism, because it tends to trigger
24840957b409SSimon J. Gerraty * some internal compiler errors.
24850957b409SSimon J. Gerraty */
24860957b409SSimon J. Gerraty #if BR_GCC && !BR_GCC_5_0
24870957b409SSimon J. Gerraty #if BR_GCC_4_6
24880957b409SSimon J. Gerraty #define BR_TARGETS_X86_UP \
24890957b409SSimon J. Gerraty _Pragma("GCC push_options") \
24900957b409SSimon J. Gerraty _Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul,rdrnd\")")
24910957b409SSimon J. Gerraty #define BR_TARGETS_X86_DOWN \
24920957b409SSimon J. Gerraty _Pragma("GCC pop_options")
24930957b409SSimon J. Gerraty #else
24940957b409SSimon J. Gerraty #define BR_TARGETS_X86_UP \
24950957b409SSimon J. Gerraty _Pragma("GCC target(\"sse2,ssse3,sse4.1,aes,pclmul\")")
24960957b409SSimon J. Gerraty #define BR_TARGETS_X86_DOWN
24970957b409SSimon J. Gerraty #endif
24980957b409SSimon J. Gerraty #pragma GCC diagnostic ignored "-Wpsabi"
24990957b409SSimon J. Gerraty #endif
25000957b409SSimon J. Gerraty
25010957b409SSimon J. Gerraty #if BR_CLANG && !BR_CLANG_3_8
25020957b409SSimon J. Gerraty #undef __SSE2__
25030957b409SSimon J. Gerraty #undef __SSE3__
25040957b409SSimon J. Gerraty #undef __SSSE3__
25050957b409SSimon J. Gerraty #undef __SSE4_1__
25060957b409SSimon J. Gerraty #undef __AES__
25070957b409SSimon J. Gerraty #undef __PCLMUL__
25080957b409SSimon J. Gerraty #undef __RDRND__
25090957b409SSimon J. Gerraty #define __SSE2__ 1
25100957b409SSimon J. Gerraty #define __SSE3__ 1
25110957b409SSimon J. Gerraty #define __SSSE3__ 1
25120957b409SSimon J. Gerraty #define __SSE4_1__ 1
25130957b409SSimon J. Gerraty #define __AES__ 1
25140957b409SSimon J. Gerraty #define __PCLMUL__ 1
25150957b409SSimon J. Gerraty #define __RDRND__ 1
25160957b409SSimon J. Gerraty #endif
25170957b409SSimon J. Gerraty
25180957b409SSimon J. Gerraty #ifndef BR_TARGETS_X86_UP
25190957b409SSimon J. Gerraty #define BR_TARGETS_X86_UP
25200957b409SSimon J. Gerraty #endif
25210957b409SSimon J. Gerraty #ifndef BR_TARGETS_X86_DOWN
25220957b409SSimon J. Gerraty #define BR_TARGETS_X86_DOWN
25230957b409SSimon J. Gerraty #endif
25240957b409SSimon J. Gerraty
25250957b409SSimon J. Gerraty #if BR_GCC || BR_CLANG
25260957b409SSimon J. Gerraty BR_TARGETS_X86_UP
25270957b409SSimon J. Gerraty #include <x86intrin.h>
25280957b409SSimon J. Gerraty #include <cpuid.h>
25290957b409SSimon J. Gerraty #define br_bswap32 __builtin_bswap32
25300957b409SSimon J. Gerraty BR_TARGETS_X86_DOWN
25310957b409SSimon J. Gerraty #endif
25320957b409SSimon J. Gerraty
25330957b409SSimon J. Gerraty #if BR_MSC
25340957b409SSimon J. Gerraty #include <stdlib.h>
25350957b409SSimon J. Gerraty #include <intrin.h>
25360957b409SSimon J. Gerraty #include <immintrin.h>
25370957b409SSimon J. Gerraty #define br_bswap32 _byteswap_ulong
25380957b409SSimon J. Gerraty #endif
25390957b409SSimon J. Gerraty
25400957b409SSimon J. Gerraty static inline int
br_cpuid(uint32_t mask_eax,uint32_t mask_ebx,uint32_t mask_ecx,uint32_t mask_edx)25410957b409SSimon J. Gerraty br_cpuid(uint32_t mask_eax, uint32_t mask_ebx,
25420957b409SSimon J. Gerraty uint32_t mask_ecx, uint32_t mask_edx)
25430957b409SSimon J. Gerraty {
25440957b409SSimon J. Gerraty #if BR_GCC || BR_CLANG
25450957b409SSimon J. Gerraty unsigned eax, ebx, ecx, edx;
25460957b409SSimon J. Gerraty
25470957b409SSimon J. Gerraty if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
25480957b409SSimon J. Gerraty if ((eax & mask_eax) == mask_eax
25490957b409SSimon J. Gerraty && (ebx & mask_ebx) == mask_ebx
25500957b409SSimon J. Gerraty && (ecx & mask_ecx) == mask_ecx
25510957b409SSimon J. Gerraty && (edx & mask_edx) == mask_edx)
25520957b409SSimon J. Gerraty {
25530957b409SSimon J. Gerraty return 1;
25540957b409SSimon J. Gerraty }
25550957b409SSimon J. Gerraty }
25560957b409SSimon J. Gerraty #elif BR_MSC
25570957b409SSimon J. Gerraty int info[4];
25580957b409SSimon J. Gerraty
25590957b409SSimon J. Gerraty __cpuid(info, 1);
25600957b409SSimon J. Gerraty if (((uint32_t)info[0] & mask_eax) == mask_eax
25610957b409SSimon J. Gerraty && ((uint32_t)info[1] & mask_ebx) == mask_ebx
25620957b409SSimon J. Gerraty && ((uint32_t)info[2] & mask_ecx) == mask_ecx
25630957b409SSimon J. Gerraty && ((uint32_t)info[3] & mask_edx) == mask_edx)
25640957b409SSimon J. Gerraty {
25650957b409SSimon J. Gerraty return 1;
25660957b409SSimon J. Gerraty }
25670957b409SSimon J. Gerraty #endif
25680957b409SSimon J. Gerraty return 0;
25690957b409SSimon J. Gerraty }
25700957b409SSimon J. Gerraty
25710957b409SSimon J. Gerraty #endif
25720957b409SSimon J. Gerraty
25730957b409SSimon J. Gerraty #endif
25740957b409SSimon J. Gerraty
25750957b409SSimon J. Gerraty /* ==================================================================== */
25760957b409SSimon J. Gerraty
25770957b409SSimon J. Gerraty #endif
2578