2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * On MSVC, disable the warning about applying unary minus on an
36 * unsigned type: it is standard, we do it all the time, and for
40 #pragma warning( disable : 4146 )
44 * Maximum size for a RSA modulus (in bits). Allocated stack buffers
45 * depend on that size, so this value should be kept small. Currently,
46 * 2048-bit RSA keys offer adequate security, and should still do so for
47 * the next few decades; however, a number of widespread PKI have
48 * already set their root keys to RSA-4096, so we should be able to
51 * This value MUST be a multiple of 64.
53 #define BR_MAX_RSA_SIZE 4096
56 * Maximum size for a RSA factor (in bits). This is for RSA private-key
57 * operations. Default is to support factors up to a bit more than half
58 * the maximum modulus size.
60 * This value MUST be a multiple of 32.
62 #define BR_MAX_RSA_FACTOR ((BR_MAX_RSA_SIZE + 64) >> 1)
65 * Maximum size for an EC curve (modulus or order), in bits. Size of
66 * stack buffers depends on that parameter. This size MUST be a multiple
67 * of 8 (so that decoding an integer with that many bytes does not
70 #define BR_MAX_EC_SIZE 528
73 * Some macros to recognize the current architecture. Right now, we are
74 * interested into automatically recognizing architecture with efficient
75 * 64-bit types so that we may automatically use implementations that
76 * use 64-bit registers in that case. Future versions may detect, e.g.,
77 * availability of SSE2 intrinsics.
79 * If 'unsigned long' is a 64-bit type, then we assume that 64-bit types
80 * are efficient. Otherwise, we rely on macros that depend on compiler,
81 * OS and architecture. In any case, failure to detect the architecture
82 * as 64-bit means that the 32-bit code will be used, and that code
83 * works also on 64-bit architectures (the 64-bit code may simply be
86 * The test on 'unsigned long' should already catch most cases, the one
87 * notable exception being Windows code where 'unsigned long' is kept to
88 * 32-bit for compatbility with all the legacy code that liberally uses
89 * the 'DWORD' type for 32-bit values.
91 * Macro names are taken from: http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
94 #if ((ULONG_MAX >> 31) >> 31) == 3
96 #elif defined(__ia64) || defined(__itanium__) || defined(_M_IA64)
98 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) \
99 || defined(__64BIT__) || defined(_LP64) || defined(__LP64__)
101 #elif defined(__sparc64__)
103 #elif defined(__x86_64__) || defined(_M_X64)
109 * Set BR_LOMUL on platforms where it makes sense.
112 #if BR_ARMEL_CORTEX_GCC
118 * Determine whether x86 AES instructions are understood by the compiler.
122 #if (__i386__ || __x86_64__) \
123 && ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) \
124 || (__clang_major__ > 3 \
125 || (__clang_major__ == 3 && __clang_minor__ >= 7)))
126 #define BR_AES_X86NI 1
127 #elif (_M_IX86 || _M_X64) && (_MSC_VER >= 1700)
128 #define BR_AES_X86NI 1
133 * If we use x86 AES instruction, determine the compiler brand.
136 #ifndef BR_AES_X86NI_GCC
138 #define BR_AES_X86NI_GCC 1
141 #ifndef BR_AES_X86NI_MSC
143 #define BR_AES_X86NI_MSC 1
149 * A macro to tag a function with a "target" attribute (for GCC and Clang).
152 #define BR_TARGET(x) __attribute__((target(x)))
158 * POWER8 crypto support. We rely on compiler macros for the
159 * architecture, since we do not have a reliable, simple way to detect
160 * the required support at runtime (we could try running an opcode, and
161 * trapping the exception or signal on illegal instruction, but this
162 * induces some non-trivial OS dependencies that we would prefer to
163 * avoid if possible).
166 #if __GNUC__ && ((_ARCH_PWR8 || _ARCH_PPC) && __CRYPTO__)
172 * Detect endinanness on POWER8.
175 #if defined BR_POWER8_LE
178 #define BR_POWER8_BE 0
180 #define BR_POWER8_BE 1
182 #elif defined BR_POWER8_BE
185 #define BR_POWER8_LE 0
187 #define BR_POWER8_LE 1
190 #if __LITTLE_ENDIAN__
191 #define BR_POWER8_LE 1
192 #define BR_POWER8_BE 0
194 #define BR_POWER8_LE 0
195 #define BR_POWER8_BE 1
200 /* ==================================================================== */
202 * Encoding/decoding functions.
204 * 32-bit and 64-bit decoding, both little-endian and big-endian, is
205 * implemented with the inline functions below. These functions are
206 * generic: they don't depend on the architecture natural endianness,
207 * and they can handle unaligned accesses. Optimized versions for some
208 * specific architectures may be implemented at a later time.
212 br_enc16le(void *dst
, unsigned x
)
217 buf
[0] = (unsigned char)x
;
218 buf
[1] = (unsigned char)(x
>> 8);
222 br_enc16be(void *dst
, unsigned x
)
227 buf
[0] = (unsigned char)(x
>> 8);
228 buf
[1] = (unsigned char)x
;
231 static inline unsigned
232 br_dec16le(const void *src
)
234 const unsigned char *buf
;
237 return (unsigned)buf
[0] | ((unsigned)buf
[1] << 8);
240 static inline unsigned
241 br_dec16be(const void *src
)
243 const unsigned char *buf
;
246 return ((unsigned)buf
[0] << 8) | (unsigned)buf
[1];
250 br_enc32le(void *dst
, uint32_t x
)
255 buf
[0] = (unsigned char)x
;
256 buf
[1] = (unsigned char)(x
>> 8);
257 buf
[2] = (unsigned char)(x
>> 16);
258 buf
[3] = (unsigned char)(x
>> 24);
262 br_enc32be(void *dst
, uint32_t x
)
267 buf
[0] = (unsigned char)(x
>> 24);
268 buf
[1] = (unsigned char)(x
>> 16);
269 buf
[2] = (unsigned char)(x
>> 8);
270 buf
[3] = (unsigned char)x
;
273 static inline uint32_t
274 br_dec32le(const void *src
)
276 const unsigned char *buf
;
279 return (uint32_t)buf
[0]
280 | ((uint32_t)buf
[1] << 8)
281 | ((uint32_t)buf
[2] << 16)
282 | ((uint32_t)buf
[3] << 24);
285 static inline uint32_t
286 br_dec32be(const void *src
)
288 const unsigned char *buf
;
291 return ((uint32_t)buf
[0] << 24)
292 | ((uint32_t)buf
[1] << 16)
293 | ((uint32_t)buf
[2] << 8)
298 br_enc64le(void *dst
, uint64_t x
)
303 br_enc32le(buf
, (uint32_t)x
);
304 br_enc32le(buf
+ 4, (uint32_t)(x
>> 32));
308 br_enc64be(void *dst
, uint64_t x
)
313 br_enc32be(buf
, (uint32_t)(x
>> 32));
314 br_enc32be(buf
+ 4, (uint32_t)x
);
317 static inline uint64_t
318 br_dec64le(const void *src
)
320 const unsigned char *buf
;
323 return (uint64_t)br_dec32le(buf
)
324 | ((uint64_t)br_dec32le(buf
+ 4) << 32);
327 static inline uint64_t
328 br_dec64be(const void *src
)
330 const unsigned char *buf
;
333 return ((uint64_t)br_dec32be(buf
) << 32)
334 | (uint64_t)br_dec32be(buf
+ 4);
338 * Range decoding and encoding (for several successive values).
340 void br_range_dec16le(uint16_t *v
, size_t num
, const void *src
);
341 void br_range_dec16be(uint16_t *v
, size_t num
, const void *src
);
342 void br_range_enc16le(void *dst
, const uint16_t *v
, size_t num
);
343 void br_range_enc16be(void *dst
, const uint16_t *v
, size_t num
);
345 void br_range_dec32le(uint32_t *v
, size_t num
, const void *src
);
346 void br_range_dec32be(uint32_t *v
, size_t num
, const void *src
);
347 void br_range_enc32le(void *dst
, const uint32_t *v
, size_t num
);
348 void br_range_enc32be(void *dst
, const uint32_t *v
, size_t num
);
350 void br_range_dec64le(uint64_t *v
, size_t num
, const void *src
);
351 void br_range_dec64be(uint64_t *v
, size_t num
, const void *src
);
352 void br_range_enc64le(void *dst
, const uint64_t *v
, size_t num
);
353 void br_range_enc64be(void *dst
, const uint64_t *v
, size_t num
);
356 * Byte-swap a 32-bit integer.
358 static inline uint32_t
359 br_swap32(uint32_t x
)
361 x
= ((x
& (uint32_t)0x00FF00FF) << 8)
362 | ((x
>> 8) & (uint32_t)0x00FF00FF);
363 return (x
<< 16) | (x
>> 16);
366 /* ==================================================================== */
368 * Support code for hash functions.
372 * IV for MD5, SHA-1, SHA-224 and SHA-256.
374 extern const uint32_t br_md5_IV
[];
375 extern const uint32_t br_sha1_IV
[];
376 extern const uint32_t br_sha224_IV
[];
377 extern const uint32_t br_sha256_IV
[];
380 * Round functions for MD5, SHA-1, SHA-224 and SHA-256 (SHA-224 and
381 * SHA-256 use the same round function).
383 void br_md5_round(const unsigned char *buf
, uint32_t *val
);
384 void br_sha1_round(const unsigned char *buf
, uint32_t *val
);
385 void br_sha2small_round(const unsigned char *buf
, uint32_t *val
);
388 * The core function for the TLS PRF. It computes
389 * P_hash(secret, label + seed), and XORs the result into the dst buffer.
391 void br_tls_phash(void *dst
, size_t len
,
392 const br_hash_class
*dig
,
393 const void *secret
, size_t secret_len
,
394 const char *label
, const void *seed
, size_t seed_len
);
397 * Copy all configured hash implementations from a multihash context
401 br_multihash_copyimpl(br_multihash_context
*dst
,
402 const br_multihash_context
*src
)
404 memcpy((void *)dst
->impl
, src
->impl
, sizeof src
->impl
);
407 /* ==================================================================== */
409 * Constant-time primitives. These functions manipulate 32-bit values in
410 * order to provide constant-time comparisons and multiplexers.
412 * Boolean values (the "ctl" bits) MUST have value 0 or 1.
414 * Implementation notes:
415 * =====================
417 * The uintN_t types are unsigned and with width exactly N bits; the C
418 * standard guarantees that computations are performed modulo 2^N, and
419 * there can be no overflow. Negation (unary '-') works on unsigned types
422 * The intN_t types are guaranteed to have width exactly N bits, with no
423 * padding bit, and using two's complement representation. Casting
424 * intN_t to uintN_t really is conversion modulo 2^N. Beware that intN_t
425 * types, being signed, trigger implementation-defined behaviour on
426 * overflow (including raising some signal): with GCC, while modular
427 * arithmetics are usually applied, the optimizer may assume that
428 * overflows don't occur (unless the -fwrapv command-line option is
429 * added); Clang has the additional -ftrapv option to explicitly trap on
430 * integer overflow or underflow.
436 static inline uint32_t
443 * Multiplexer: returns x if ctl == 1, y if ctl == 0.
445 static inline uint32_t
446 MUX(uint32_t ctl
, uint32_t x
, uint32_t y
)
448 return y
^ (-ctl
& (x
^ y
));
452 * Equality check: returns 1 if x == y, 0 otherwise.
454 static inline uint32_t
455 EQ(uint32_t x
, uint32_t y
)
460 return NOT((q
| -q
) >> 31);
464 * Inequality check: returns 1 if x != y, 0 otherwise.
466 static inline uint32_t
467 NEQ(uint32_t x
, uint32_t y
)
472 return (q
| -q
) >> 31;
476 * Comparison: returns 1 if x > y, 0 otherwise.
478 static inline uint32_t
479 GT(uint32_t x
, uint32_t y
)
482 * If both x < 2^31 and x < 2^31, then y-x will have its high
483 * bit set if x > y, cleared otherwise.
485 * If either x >= 2^31 or y >= 2^31 (but not both), then the
486 * result is the high bit of x.
488 * If both x >= 2^31 and y >= 2^31, then we can virtually
489 * subtract 2^31 from both, and we are back to the first case.
490 * Since (y-2^31)-(x-2^31) = y-x, the subtraction is already
496 return (z
^ ((x
^ y
) & (x
^ z
))) >> 31;
500 * Other comparisons (greater-or-equal, lower-than, lower-or-equal).
502 #define GE(x, y) NOT(GT(y, x))
503 #define LT(x, y) GT(y, x)
504 #define LE(x, y) NOT(GT(x, y))
507 * General comparison: returned value is -1, 0 or 1, depending on
508 * whether x is lower than, equal to, or greater than y.
510 static inline int32_t
511 CMP(uint32_t x
, uint32_t y
)
513 return (int32_t)GT(x
, y
) | -(int32_t)GT(y
, x
);
517 * Returns 1 if x == 0, 0 otherwise. Take care that the operand is signed.
519 static inline uint32_t
525 return ~(q
| -q
) >> 31;
529 * Returns 1 if x > 0, 0 otherwise. Take care that the operand is signed.
531 static inline uint32_t
535 * High bit of -x is 0 if x == 0, but 1 if x > 0.
540 return (~q
& -q
) >> 31;
544 * Returns 1 if x >= 0, 0 otherwise. Take care that the operand is signed.
546 static inline uint32_t
549 return ~(uint32_t)x
>> 31;
553 * Returns 1 if x < 0, 0 otherwise. Take care that the operand is signed.
555 static inline uint32_t
558 return (uint32_t)x
>> 31;
562 * Returns 1 if x <= 0, 0 otherwise. Take care that the operand is signed.
564 static inline uint32_t
570 * ~-x has its high bit set if and only if -x is nonnegative (as
571 * a signed int), i.e. x is in the -(2^31-1) to 0 range. We must
572 * do an OR with x itself to account for x = -2^31.
575 return (q
| ~-q
) >> 31;
579 * Conditional copy: src[] is copied into dst[] if and only if ctl is 1.
580 * dst[] and src[] may overlap completely (but not partially).
582 void br_ccopy(uint32_t ctl
, void *dst
, const void *src
, size_t len
);
584 #define CCOPY br_ccopy
587 * Compute the bit length of a 32-bit integer. Returned value is between 0
588 * and 32 (inclusive).
590 static inline uint32_t
591 BIT_LENGTH(uint32_t x
)
596 c
= GT(x
, 0xFFFF); x
= MUX(c
, x
>> 16, x
); k
+= c
<< 4;
597 c
= GT(x
, 0x00FF); x
= MUX(c
, x
>> 8, x
); k
+= c
<< 3;
598 c
= GT(x
, 0x000F); x
= MUX(c
, x
>> 4, x
); k
+= c
<< 2;
599 c
= GT(x
, 0x0003); x
= MUX(c
, x
>> 2, x
); k
+= c
<< 1;
605 * Compute the minimum of x and y.
607 static inline uint32_t
608 MIN(uint32_t x
, uint32_t y
)
610 return MUX(GT(x
, y
), y
, x
);
614 * Compute the maximum of x and y.
616 static inline uint32_t
617 MAX(uint32_t x
, uint32_t y
)
619 return MUX(GT(x
, y
), x
, y
);
623 * Multiply two 32-bit integers, with a 64-bit result. This default
624 * implementation assumes that the basic multiplication operator
625 * yields constant-time code.
627 #define MUL(x, y) ((uint64_t)(x) * (uint64_t)(y))
632 * Alternate implementation of MUL31, that will be constant-time on some
633 * (old) platforms where the default MUL31 is not. Unfortunately, it is
634 * also substantially slower, and yields larger code, on more modern
635 * platforms, which is why it is deactivated by default.
637 * MUL31_lo() must do some extra work because on some platforms, the
638 * _signed_ multiplication may return early if the top bits are 1.
639 * Simply truncating (casting) the output of MUL31() would not be
640 * sufficient, because the compiler may notice that we keep only the low
641 * word, and then replace automatically the unsigned multiplication with
642 * a signed multiplication opcode.
644 #define MUL31(x, y) ((uint64_t)((x) | (uint32_t)0x80000000) \
645 * (uint64_t)((y) | (uint32_t)0x80000000) \
646 - ((uint64_t)(x) << 31) - ((uint64_t)(y) << 31) \
647 - ((uint64_t)1 << 62))
648 static inline uint32_t
649 MUL31_lo(uint32_t x
, uint32_t y
)
654 xl
= (x
& 0xFFFF) | (uint32_t)0x80000000;
655 xh
= (x
>> 16) | (uint32_t)0x80000000;
656 yl
= (y
& 0xFFFF) | (uint32_t)0x80000000;
657 yh
= (y
>> 16) | (uint32_t)0x80000000;
658 return (xl
* yl
+ ((xl
* yh
+ xh
* yl
) << 16)) & (uint32_t)0x7FFFFFFF;
664 * Multiply two 31-bit integers, with a 62-bit result. This default
665 * implementation assumes that the basic multiplication operator
666 * yields constant-time code.
667 * The MUL31_lo() macro returns only the low 31 bits of the product.
669 #define MUL31(x, y) ((uint64_t)(x) * (uint64_t)(y))
670 #define MUL31_lo(x, y) (((uint32_t)(x) * (uint32_t)(y)) & (uint32_t)0x7FFFFFFF)
675 * Multiply two words together; the sum of the lengths of the two
676 * operands must not exceed 31 (for instance, one operand may use 16
677 * bits if the other fits on 15). If BR_CT_MUL15 is non-zero, then the
678 * macro will contain some extra operations that help in making the
679 * operation constant-time on some platforms, where the basic 32-bit
680 * multiplication is not constant-time.
683 #define MUL15(x, y) (((uint32_t)(x) | (uint32_t)0x80000000) \
684 * ((uint32_t)(y) | (uint32_t)0x80000000) \
685 & (uint32_t)0x7FFFFFFF)
687 #define MUL15(x, y) ((uint32_t)(x) * (uint32_t)(y))
691 * Arithmetic right shift (sign bit is copied). What happens when
692 * right-shifting a negative value is _implementation-defined_, so it
693 * does not trigger undefined behaviour, but it is still up to each
694 * compiler to define (and document) what it does. Most/all compilers
695 * will do an arithmetic shift, the sign bit being used to fill the
696 * holes; this is a native operation on the underlying CPU, and it would
697 * make little sense for the compiler to do otherwise. GCC explicitly
698 * documents that it follows that convention.
700 * Still, if BR_NO_ARITH_SHIFT is defined (and non-zero), then an
701 * alternate version will be used, that does not rely on such
702 * implementation-defined behaviour. Unfortunately, it is also slower
703 * and yields bigger code, which is why it is deactivated by default.
705 #if BR_NO_ARITH_SHIFT
706 #define ARSH(x, n) (((uint32_t)(x) >> (n)) \
707 | ((-((uint32_t)(x) >> 31)) << (32 - (n))))
709 #define ARSH(x, n) ((*(int32_t *)&(x)) >> (n))
713 * Constant-time division. The dividend hi:lo is divided by the
714 * divisor d; the quotient is returned and the remainder is written
715 * in *r. If hi == d, then the quotient does not fit on 32 bits;
716 * returned value is thus truncated. If hi > d, returned values are
719 uint32_t br_divrem(uint32_t hi
, uint32_t lo
, uint32_t d
, uint32_t *r
);
722 * Wrapper for br_divrem(); the remainder is returned, and the quotient
725 static inline uint32_t
726 br_rem(uint32_t hi
, uint32_t lo
, uint32_t d
)
730 br_divrem(hi
, lo
, d
, &r
);
735 * Wrapper for br_divrem(); the quotient is returned, and the remainder
738 static inline uint32_t
739 br_div(uint32_t hi
, uint32_t lo
, uint32_t d
)
743 return br_divrem(hi
, lo
, d
, &r
);
746 /* ==================================================================== */
752 * The 'i32' functions implement computations on big integers using
753 * an internal representation as an array of 32-bit integers. For
755 * -- x[0] contains the "announced bit length" of the integer
756 * -- x[1], x[2]... contain the value in little-endian order (x[1]
757 * contains the least significant 32 bits)
759 * Multiplications rely on the elementary 32x32->64 multiplication.
761 * The announced bit length specifies the number of bits that are
762 * significant in the subsequent 32-bit words. Unused bits in the
763 * last (most significant) word are set to 0; subsequent words are
764 * uninitialized and need not exist at all.
766 * The execution time and memory access patterns of all computations
767 * depend on the announced bit length, but not on the actual word
768 * values. For modular integers, the announced bit length of any integer
769 * modulo n is equal to the actual bit length of n; thus, computations
770 * on modular integers are "constant-time" (only the modulus length may
775 * Compute the actual bit length of an integer. The argument x should
776 * point to the first (least significant) value word of the integer.
777 * The len 'xlen' contains the number of 32-bit words to access.
779 * CT: value or length of x does not leak.
781 uint32_t br_i32_bit_length(uint32_t *x
, size_t xlen
);
784 * Decode an integer from its big-endian unsigned representation. The
785 * "true" bit length of the integer is computed, but all words of x[]
786 * corresponding to the full 'len' bytes of the source are set.
788 * CT: value or length of x does not leak.
790 void br_i32_decode(uint32_t *x
, const void *src
, size_t len
);
793 * Decode an integer from its big-endian unsigned representation. The
794 * integer MUST be lower than m[]; the announced bit length written in
795 * x[] will be equal to that of m[]. All 'len' bytes from the source are
798 * Returned value is 1 if the decode value fits within the modulus, 0
799 * otherwise. In the latter case, the x[] buffer will be set to 0 (but
800 * still with the announced bit length of m[]).
802 * CT: value or length of x does not leak. Memory access pattern depends
803 * only of 'len' and the announced bit length of m. Whether x fits or
804 * not does not leak either.
806 uint32_t br_i32_decode_mod(uint32_t *x
,
807 const void *src
, size_t len
, const uint32_t *m
);
810 * Reduce an integer (a[]) modulo another (m[]). The result is written
811 * in x[] and its announced bit length is set to be equal to that of m[].
813 * x[] MUST be distinct from a[] and m[].
815 * CT: only announced bit lengths leak, not values of x, a or m.
817 void br_i32_reduce(uint32_t *x
, const uint32_t *a
, const uint32_t *m
);
820 * Decode an integer from its big-endian unsigned representation, and
821 * reduce it modulo the provided modulus m[]. The announced bit length
822 * of the result is set to be equal to that of the modulus.
824 * x[] MUST be distinct from m[].
826 void br_i32_decode_reduce(uint32_t *x
,
827 const void *src
, size_t len
, const uint32_t *m
);
830 * Encode an integer into its big-endian unsigned representation. The
831 * output length in bytes is provided (parameter 'len'); if the length
832 * is too short then the integer is appropriately truncated; if it is
833 * too long then the extra bytes are set to 0.
835 void br_i32_encode(void *dst
, size_t len
, const uint32_t *x
);
838 * Multiply x[] by 2^32 and then add integer z, modulo m[]. This
839 * function assumes that x[] and m[] have the same announced bit
840 * length, and the announced bit length of m[] matches its true
843 * x[] and m[] MUST be distinct arrays.
845 * CT: only the common announced bit length of x and m leaks, not
846 * the values of x, z or m.
848 void br_i32_muladd_small(uint32_t *x
, uint32_t z
, const uint32_t *m
);
851 * Extract one word from an integer. The offset is counted in bits.
852 * The word MUST entirely fit within the word elements corresponding
853 * to the announced bit length of a[].
855 static inline uint32_t
856 br_i32_word(const uint32_t *a
, uint32_t off
)
861 u
= (size_t)(off
>> 5) + 1;
862 j
= (unsigned)off
& 31;
866 return (a
[u
] >> j
) | (a
[u
+ 1] << (32 - j
));
871 * Test whether an integer is zero.
873 uint32_t br_i32_iszero(const uint32_t *x
);
876 * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
877 * is unmodified, but the carry is still computed and returned. The
878 * arrays a[] and b[] MUST have the same announced bit length.
880 * a[] and b[] MAY be the same array, but partial overlap is not allowed.
882 uint32_t br_i32_add(uint32_t *a
, const uint32_t *b
, uint32_t ctl
);
885 * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
886 * then a[] is unmodified, but the carry is still computed and returned.
887 * The arrays a[] and b[] MUST have the same announced bit length.
889 * a[] and b[] MAY be the same array, but partial overlap is not allowed.
891 uint32_t br_i32_sub(uint32_t *a
, const uint32_t *b
, uint32_t ctl
);
894 * Compute d+a*b, result in d. The initial announced bit length of d[]
895 * MUST match that of a[]. The d[] array MUST be large enough to
896 * accommodate the full result, plus (possibly) an extra word. The
897 * resulting announced bit length of d[] will be the sum of the announced
898 * bit lengths of a[] and b[] (therefore, it may be larger than the actual
899 * bit length of the numerical result).
901 * a[] and b[] may be the same array. d[] must be disjoint from both a[]
904 void br_i32_mulacc(uint32_t *d
, const uint32_t *a
, const uint32_t *b
);
907 * Zeroize an integer. The announced bit length is set to the provided
908 * value, and the corresponding words are set to 0.
911 br_i32_zero(uint32_t *x
, uint32_t bit_len
)
914 memset(x
, 0, ((bit_len
+ 31) >> 5) * sizeof *x
);
918 * Compute -(1/x) mod 2^32. If x is even, then this function returns 0.
920 uint32_t br_i32_ninv32(uint32_t x
);
923 * Convert a modular integer to Montgomery representation. The integer x[]
924 * MUST be lower than m[], but with the same announced bit length.
926 void br_i32_to_monty(uint32_t *x
, const uint32_t *m
);
929 * Convert a modular integer back from Montgomery representation. The
930 * integer x[] MUST be lower than m[], but with the same announced bit
931 * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
932 * the least significant value word of m[] (this works only if m[] is
935 void br_i32_from_monty(uint32_t *x
, const uint32_t *m
, uint32_t m0i
);
938 * Compute a modular Montgomery multiplication. d[] is filled with the
939 * value of x*y/R modulo m[] (where R is the Montgomery factor). The
940 * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
941 * numerically lower than m[]. x[] and y[] MAY be the same array. The
942 * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
943 * significant value word of m[] (this works only if m[] is an odd
946 void br_i32_montymul(uint32_t *d
, const uint32_t *x
, const uint32_t *y
,
947 const uint32_t *m
, uint32_t m0i
);
950 * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
951 * (same announced bit length, lower value). m[] MUST be odd. The
952 * exponent is in big-endian unsigned notation, over 'elen' bytes. The
953 * "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is the least
954 * significant value word of m[] (this works only if m[] is an odd
955 * integer). The t1[] and t2[] parameters must be temporary arrays,
956 * each large enough to accommodate an integer with the same size as m[].
958 void br_i32_modpow(uint32_t *x
, const unsigned char *e
, size_t elen
,
959 const uint32_t *m
, uint32_t m0i
, uint32_t *t1
, uint32_t *t2
);
961 /* ==================================================================== */
967 * The 'i31' functions implement computations on big integers using
968 * an internal representation as an array of 32-bit integers. For
970 * -- x[0] encodes the array length and the "announced bit length"
971 * of the integer: namely, if the announced bit length is k,
972 * then x[0] = ((k / 31) << 5) + (k % 31).
973 * -- x[1], x[2]... contain the value in little-endian order, 31
974 * bits per word (x[1] contains the least significant 31 bits).
975 * The upper bit of each word is 0.
977 * Multiplications rely on the elementary 32x32->64 multiplication.
979 * The announced bit length specifies the number of bits that are
980 * significant in the subsequent 32-bit words. Unused bits in the
981 * last (most significant) word are set to 0; subsequent words are
982 * uninitialized and need not exist at all.
984 * The execution time and memory access patterns of all computations
985 * depend on the announced bit length, but not on the actual word
986 * values. For modular integers, the announced bit length of any integer
987 * modulo n is equal to the actual bit length of n; thus, computations
988 * on modular integers are "constant-time" (only the modulus length may
993 * Test whether an integer is zero.
995 uint32_t br_i31_iszero(const uint32_t *x
);
998 * Add b[] to a[] and return the carry (0 or 1). If ctl is 0, then a[]
999 * is unmodified, but the carry is still computed and returned. The
1000 * arrays a[] and b[] MUST have the same announced bit length.
1002 * a[] and b[] MAY be the same array, but partial overlap is not allowed.
1004 uint32_t br_i31_add(uint32_t *a
, const uint32_t *b
, uint32_t ctl
);
1007 * Subtract b[] from a[] and return the carry (0 or 1). If ctl is 0,
1008 * then a[] is unmodified, but the carry is still computed and returned.
1009 * The arrays a[] and b[] MUST have the same announced bit length.
1011 * a[] and b[] MAY be the same array, but partial overlap is not allowed.
1013 uint32_t br_i31_sub(uint32_t *a
, const uint32_t *b
, uint32_t ctl
);
1016 * Compute the ENCODED actual bit length of an integer. The argument x
1017 * should point to the first (least significant) value word of the
1018 * integer. The len 'xlen' contains the number of 32-bit words to
1019 * access. The upper bit of each value word MUST be 0.
1020 * Returned value is ((k / 31) << 5) + (k % 31) if the bit length is k.
1022 * CT: value or length of x does not leak.
1024 uint32_t br_i31_bit_length(uint32_t *x
, size_t xlen
);
1027 * Decode an integer from its big-endian unsigned representation. The
1028 * "true" bit length of the integer is computed and set in the encoded
1029 * announced bit length (x[0]), but all words of x[] corresponding to
1030 * the full 'len' bytes of the source are set.
1032 * CT: value or length of x does not leak.
1034 void br_i31_decode(uint32_t *x
, const void *src
, size_t len
);
1037 * Decode an integer from its big-endian unsigned representation. The
1038 * integer MUST be lower than m[]; the (encoded) announced bit length
1039 * written in x[] will be equal to that of m[]. All 'len' bytes from the
1042 * Returned value is 1 if the decode value fits within the modulus, 0
1043 * otherwise. In the latter case, the x[] buffer will be set to 0 (but
1044 * still with the announced bit length of m[]).
1046 * CT: value or length of x does not leak. Memory access pattern depends
1047 * only of 'len' and the announced bit length of m. Whether x fits or
1048 * not does not leak either.
1050 uint32_t br_i31_decode_mod(uint32_t *x
,
1051 const void *src
, size_t len
, const uint32_t *m
);
1054 * Zeroize an integer. The announced bit length is set to the provided
1055 * value, and the corresponding words are set to 0. The ENCODED bit length
1059 br_i31_zero(uint32_t *x
, uint32_t bit_len
)
1062 memset(x
, 0, ((bit_len
+ 31) >> 5) * sizeof *x
);
1066 * Right-shift an integer. The shift amount must be lower than 31
1069 void br_i31_rshift(uint32_t *x
, int count
);
1072 * Reduce an integer (a[]) modulo another (m[]). The result is written
1073 * in x[] and its announced bit length is set to be equal to that of m[].
1075 * x[] MUST be distinct from a[] and m[].
1077 * CT: only announced bit lengths leak, not values of x, a or m.
1079 void br_i31_reduce(uint32_t *x
, const uint32_t *a
, const uint32_t *m
);
1082 * Decode an integer from its big-endian unsigned representation, and
1083 * reduce it modulo the provided modulus m[]. The announced bit length
1084 * of the result is set to be equal to that of the modulus.
1086 * x[] MUST be distinct from m[].
1088 void br_i31_decode_reduce(uint32_t *x
,
1089 const void *src
, size_t len
, const uint32_t *m
);
1092 * Multiply x[] by 2^31 and then add integer z, modulo m[]. This
1093 * function assumes that x[] and m[] have the same announced bit
1094 * length, the announced bit length of m[] matches its true
1097 * x[] and m[] MUST be distinct arrays. z MUST fit in 31 bits (upper
1100 * CT: only the common announced bit length of x and m leaks, not
1101 * the values of x, z or m.
1103 void br_i31_muladd_small(uint32_t *x
, uint32_t z
, const uint32_t *m
);
1106 * Encode an integer into its big-endian unsigned representation. The
1107 * output length in bytes is provided (parameter 'len'); if the length
1108 * is too short then the integer is appropriately truncated; if it is
1109 * too long then the extra bytes are set to 0.
1111 void br_i31_encode(void *dst
, size_t len
, const uint32_t *x
);
1114 * Compute -(1/x) mod 2^31. If x is even, then this function returns 0.
1116 uint32_t br_i31_ninv31(uint32_t x
);
1119 * Compute a modular Montgomery multiplication. d[] is filled with the
1120 * value of x*y/R modulo m[] (where R is the Montgomery factor). The
1121 * array d[] MUST be distinct from x[], y[] and m[]. x[] and y[] MUST be
1122 * numerically lower than m[]. x[] and y[] MAY be the same array. The
1123 * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
1124 * significant value word of m[] (this works only if m[] is an odd
1127 void br_i31_montymul(uint32_t *d
, const uint32_t *x
, const uint32_t *y
,
1128 const uint32_t *m
, uint32_t m0i
);
1131 * Convert a modular integer to Montgomery representation. The integer x[]
1132 * MUST be lower than m[], but with the same announced bit length.
1134 void br_i31_to_monty(uint32_t *x
, const uint32_t *m
);
1137 * Convert a modular integer back from Montgomery representation. The
1138 * integer x[] MUST be lower than m[], but with the same announced bit
1139 * length. The "m0i" parameter is equal to -(1/m0) mod 2^32, where m0 is
1140 * the least significant value word of m[] (this works only if m[] is
1143 void br_i31_from_monty(uint32_t *x
, const uint32_t *m
, uint32_t m0i
);
1146 * Compute a modular exponentiation. x[] MUST be an integer modulo m[]
1147 * (same announced bit length, lower value). m[] MUST be odd. The
1148 * exponent is in big-endian unsigned notation, over 'elen' bytes. The
1149 * "m0i" parameter is equal to -(1/m0) mod 2^31, where m0 is the least
1150 * significant value word of m[] (this works only if m[] is an odd
1151 * integer). The t1[] and t2[] parameters must be temporary arrays,
1152 * each large enough to accommodate an integer with the same size as m[].
1154 void br_i31_modpow(uint32_t *x
, const unsigned char *e
, size_t elen
,
1155 const uint32_t *m
, uint32_t m0i
, uint32_t *t1
, uint32_t *t2
);
1158 * Compute d+a*b, result in d. The initial announced bit length of d[]
1159 * MUST match that of a[]. The d[] array MUST be large enough to
1160 * accommodate the full result, plus (possibly) an extra word. The
1161 * resulting announced bit length of d[] will be the sum of the announced
1162 * bit lengths of a[] and b[] (therefore, it may be larger than the actual
1163 * bit length of the numerical result).
1165 * a[] and b[] may be the same array. d[] must be disjoint from both a[]
1168 void br_i31_mulacc(uint32_t *d
, const uint32_t *a
, const uint32_t *b
);
1170 /* ==================================================================== */
1173 * FIXME: document "i15" functions.
1177 br_i15_zero(uint16_t *x
, uint16_t bit_len
)
1180 memset(x
, 0, ((bit_len
+ 15) >> 4) * sizeof *x
);
1183 uint32_t br_i15_iszero(const uint16_t *x
);
1185 uint16_t br_i15_ninv15(uint16_t x
);
1187 uint32_t br_i15_add(uint16_t *a
, const uint16_t *b
, uint32_t ctl
);
1189 uint32_t br_i15_sub(uint16_t *a
, const uint16_t *b
, uint32_t ctl
);
1191 void br_i15_muladd_small(uint16_t *x
, uint16_t z
, const uint16_t *m
);
1193 void br_i15_montymul(uint16_t *d
, const uint16_t *x
, const uint16_t *y
,
1194 const uint16_t *m
, uint16_t m0i
);
1196 void br_i15_to_monty(uint16_t *x
, const uint16_t *m
);
1198 void br_i15_modpow(uint16_t *x
, const unsigned char *e
, size_t elen
,
1199 const uint16_t *m
, uint16_t m0i
, uint16_t *t1
, uint16_t *t2
);
1201 uint32_t br_i15_modpow_opt(uint16_t *x
, const unsigned char *e
, size_t elen
,
1202 const uint16_t *m
, uint16_t m0i
, uint16_t *tmp
, size_t twlen
);
1204 void br_i15_encode(void *dst
, size_t len
, const uint16_t *x
);
1206 uint32_t br_i15_decode_mod(uint16_t *x
,
1207 const void *src
, size_t len
, const uint16_t *m
);
1209 void br_i15_rshift(uint16_t *x
, int count
);
1211 uint32_t br_i15_bit_length(uint16_t *x
, size_t xlen
);
1213 void br_i15_decode(uint16_t *x
, const void *src
, size_t len
);
1215 void br_i15_from_monty(uint16_t *x
, const uint16_t *m
, uint16_t m0i
);
1217 void br_i15_decode_reduce(uint16_t *x
,
1218 const void *src
, size_t len
, const uint16_t *m
);
1220 void br_i15_reduce(uint16_t *x
, const uint16_t *a
, const uint16_t *m
);
1222 void br_i15_mulacc(uint16_t *d
, const uint16_t *a
, const uint16_t *b
);
1224 /* ==================================================================== */
1226 static inline size_t
1227 br_digest_size(const br_hash_class
*digest_class
)
1229 return (size_t)(digest_class
->desc
>> BR_HASHDESC_OUT_OFF
)
1230 & BR_HASHDESC_OUT_MASK
;
1234 * Get the output size (in bytes) of a hash function.
1236 size_t br_digest_size_by_ID(int digest_id
);
1239 * Get the OID (encoded OBJECT IDENTIFIER value, without tag and length)
1240 * for a hash function. If digest_id is not a supported digest identifier
1241 * (in particular if it is equal to 0, i.e. br_md5sha1_ID), then NULL is
1242 * returned and *len is set to 0.
1244 const unsigned char *br_digest_OID(int digest_id
, size_t *len
);
1246 /* ==================================================================== */
1248 * DES support functions.
1252 * Apply DES Initial Permutation.
1254 void br_des_do_IP(uint32_t *xl
, uint32_t *xr
);
1257 * Apply DES Final Permutation (inverse of IP).
1259 void br_des_do_invIP(uint32_t *xl
, uint32_t *xr
);
1262 * Key schedule unit: for a DES key (8 bytes), compute 16 subkeys. Each
1263 * subkey is two 28-bit words represented as two 32-bit words; the PC-2
1264 * bit extration is NOT applied.
1266 void br_des_keysched_unit(uint32_t *skey
, const void *key
);
1269 * Reversal of 16 DES sub-keys (for decryption).
1271 void br_des_rev_skey(uint32_t *skey
);
1274 * DES/3DES key schedule for 'des_tab' (encryption direction). Returned
1275 * value is the number of rounds.
1277 unsigned br_des_tab_keysched(uint32_t *skey
, const void *key
, size_t key_len
);
1280 * DES/3DES key schedule for 'des_ct' (encryption direction). Returned
1281 * value is the number of rounds.
1283 unsigned br_des_ct_keysched(uint32_t *skey
, const void *key
, size_t key_len
);
1286 * DES/3DES subkey decompression (from the compressed bitsliced subkeys).
1288 void br_des_ct_skey_expand(uint32_t *sk_exp
,
1289 unsigned num_rounds
, const uint32_t *skey
);
1292 * DES/3DES block encryption/decryption ('des_tab').
1294 void br_des_tab_process_block(unsigned num_rounds
,
1295 const uint32_t *skey
, void *block
);
1298 * DES/3DES block encryption/decryption ('des_ct').
1300 void br_des_ct_process_block(unsigned num_rounds
,
1301 const uint32_t *skey
, void *block
);
1303 /* ==================================================================== */
1305 * AES support functions.
1309 * The AES S-box (256-byte table).
1311 extern const unsigned char br_aes_S
[];
1314 * AES key schedule. skey[] is filled with n+1 128-bit subkeys, where n
1315 * is the number of rounds (10 to 14, depending on key size). The number
1316 * of rounds is returned. If the key size is invalid (not 16, 24 or 32),
1317 * then 0 is returned.
1319 * This implementation uses a 256-byte table and is NOT constant-time.
1321 unsigned br_aes_keysched(uint32_t *skey
, const void *key
, size_t key_len
);
1324 * AES key schedule for decryption ('aes_big' implementation).
1326 unsigned br_aes_big_keysched_inv(uint32_t *skey
,
1327 const void *key
, size_t key_len
);
1330 * AES block encryption with the 'aes_big' implementation (fast, but
1331 * not constant-time). This function encrypts a single block "in place".
1333 void br_aes_big_encrypt(unsigned num_rounds
, const uint32_t *skey
, void *data
);
1336 * AES block decryption with the 'aes_big' implementation (fast, but
1337 * not constant-time). This function decrypts a single block "in place".
1339 void br_aes_big_decrypt(unsigned num_rounds
, const uint32_t *skey
, void *data
);
1342 * AES block encryption with the 'aes_small' implementation (small, but
1343 * slow and not constant-time). This function encrypts a single block
1346 void br_aes_small_encrypt(unsigned num_rounds
,
1347 const uint32_t *skey
, void *data
);
1350 * AES block decryption with the 'aes_small' implementation (small, but
1351 * slow and not constant-time). This function decrypts a single block
1354 void br_aes_small_decrypt(unsigned num_rounds
,
1355 const uint32_t *skey
, void *data
);
1358 * The constant-time implementation is "bitsliced": the 128-bit state is
1359 * split over eight 32-bit words q* in the following way:
1361 * -- Input block consists in 16 bytes:
1362 * a00 a10 a20 a30 a01 a11 a21 a31 a02 a12 a22 a32 a03 a13 a23 a33
1363 * In the terminology of FIPS 197, this is a 4x4 matrix which is read
1366 * -- Each byte is split into eight bits which are distributed over the
1367 * eight words, at the same rank. Thus, for a byte x at rank k, bit 0
1368 * (least significant) of x will be at rank k in q0 (if that bit is b,
1369 * then it contributes "b << k" to the value of q0), bit 1 of x will be
1370 * at rank k in q1, and so on.
1372 * -- Ranks given to bits are in "row order" and are either all even, or
1373 * all odd. Two independent AES states are thus interleaved, one using
1374 * the even ranks, the other the odd ranks. Row order means:
1375 * a00 a01 a02 a03 a10 a11 a12 a13 a20 a21 a22 a23 a30 a31 a32 a33
1377 * Converting input bytes from two AES blocks to bitslice representation
1378 * is done in the following way:
1379 * -- Decode first block into the four words q0 q2 q4 q6, in that order,
1380 * using little-endian convention.
1381 * -- Decode second block into the four words q1 q3 q5 q7, in that order,
1382 * using little-endian convention.
1383 * -- Call br_aes_ct_ortho().
1385 * Converting back to bytes is done by using the reverse operations. Note
1386 * that br_aes_ct_ortho() is its own inverse.
1390 * Perform bytewise orthogonalization of eight 32-bit words. Bytes
1391 * of q0..q7 are spread over all words: for a byte x that occurs
1392 * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
1393 * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
1395 * This operation is an involution.
1397 void br_aes_ct_ortho(uint32_t *q
);
1400 * The AES S-box, as a bitsliced constant-time version. The input array
1401 * consists in eight 32-bit words; 32 S-box instances are computed in
1402 * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
1403 * are spread over the words 0 to 7, at the same rank.
1405 void br_aes_ct_bitslice_Sbox(uint32_t *q
);
1408 * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
1410 void br_aes_ct_bitslice_invSbox(uint32_t *q
);
1413 * Compute AES encryption on bitsliced data. Since input is stored on
1414 * eight 32-bit words, two block encryptions are actually performed
1417 void br_aes_ct_bitslice_encrypt(unsigned num_rounds
,
1418 const uint32_t *skey
, uint32_t *q
);
1421 * Compute AES decryption on bitsliced data. Since input is stored on
1422 * eight 32-bit words, two block decryptions are actually performed
1425 void br_aes_ct_bitslice_decrypt(unsigned num_rounds
,
1426 const uint32_t *skey
, uint32_t *q
);
1429 * AES key schedule, constant-time version. skey[] is filled with n+1
1430 * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
1431 * on key size). The number of rounds is returned. If the key size is
1432 * invalid (not 16, 24 or 32), then 0 is returned.
1434 unsigned br_aes_ct_keysched(uint32_t *comp_skey
,
1435 const void *key
, size_t key_len
);
1438 * Expand AES subkeys as produced by br_aes_ct_keysched(), into
1439 * a larger array suitable for br_aes_ct_bitslice_encrypt() and
1440 * br_aes_ct_bitslice_decrypt().
1442 void br_aes_ct_skey_expand(uint32_t *skey
,
1443 unsigned num_rounds
, const uint32_t *comp_skey
);
1446 * For the ct64 implementation, the same bitslicing technique is used,
1447 * but four instances are interleaved. First instance uses bits 0, 4,
1448 * 8, 12,... of each word; second instance uses bits 1, 5, 9, 13,...
1453 * Perform bytewise orthogonalization of eight 64-bit words. Bytes
1454 * of q0..q7 are spread over all words: for a byte x that occurs
1455 * at rank i in q[j] (byte x uses bits 8*i to 8*i+7 in q[j]), the bit
1456 * of rank k in x (0 <= k <= 7) goes to q[k] at rank 8*i+j.
1458 * This operation is an involution.
1460 void br_aes_ct64_ortho(uint64_t *q
);
1463 * Interleave bytes for an AES input block. If input bytes are
1464 * denoted 0123456789ABCDEF, and have been decoded with little-endian
1465 * convention (w[0] contains 0123, with '3' being most significant;
1466 * w[1] contains 4567, and so on), then output word q0 will be
1467 * set to 08192A3B (again little-endian convention) and q1 will
1468 * be set to 4C5D6E7F.
1470 void br_aes_ct64_interleave_in(uint64_t *q0
, uint64_t *q1
, const uint32_t *w
);
1473 * Perform the opposite of br_aes_ct64_interleave_in().
1475 void br_aes_ct64_interleave_out(uint32_t *w
, uint64_t q0
, uint64_t q1
);
1478 * The AES S-box, as a bitsliced constant-time version. The input array
1479 * consists in eight 64-bit words; 64 S-box instances are computed in
1480 * parallel. Bits 0 to 7 of each S-box input (bit 0 is least significant)
1481 * are spread over the words 0 to 7, at the same rank.
1483 void br_aes_ct64_bitslice_Sbox(uint64_t *q
);
1486 * Like br_aes_bitslice_Sbox(), but for the inverse S-box.
1488 void br_aes_ct64_bitslice_invSbox(uint64_t *q
);
1491 * Compute AES encryption on bitsliced data. Since input is stored on
1492 * eight 64-bit words, four block encryptions are actually performed
1495 void br_aes_ct64_bitslice_encrypt(unsigned num_rounds
,
1496 const uint64_t *skey
, uint64_t *q
);
1499 * Compute AES decryption on bitsliced data. Since input is stored on
1500 * eight 64-bit words, four block decryptions are actually performed
1503 void br_aes_ct64_bitslice_decrypt(unsigned num_rounds
,
1504 const uint64_t *skey
, uint64_t *q
);
1507 * AES key schedule, constant-time version. skey[] is filled with n+1
1508 * 128-bit subkeys, where n is the number of rounds (10 to 14, depending
1509 * on key size). The number of rounds is returned. If the key size is
1510 * invalid (not 16, 24 or 32), then 0 is returned.
1512 unsigned br_aes_ct64_keysched(uint64_t *comp_skey
,
1513 const void *key
, size_t key_len
);
1516 * Expand AES subkeys as produced by br_aes_ct64_keysched(), into
1517 * a larger array suitable for br_aes_ct64_bitslice_encrypt() and
1518 * br_aes_ct64_bitslice_decrypt().
1520 void br_aes_ct64_skey_expand(uint64_t *skey
,
1521 unsigned num_rounds
, const uint64_t *comp_skey
);
1524 * Test support for AES-NI opcodes.
1526 int br_aes_x86ni_supported(void);
1529 * AES key schedule, using x86 AES-NI instructions. This yields the
1530 * subkeys in the encryption direction. Number of rounds is returned.
1531 * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
1533 unsigned br_aes_x86ni_keysched_enc(unsigned char *skni
,
1534 const void *key
, size_t len
);
1537 * AES key schedule, using x86 AES-NI instructions. This yields the
1538 * subkeys in the decryption direction. Number of rounds is returned.
1539 * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
1541 unsigned br_aes_x86ni_keysched_dec(unsigned char *skni
,
1542 const void *key
, size_t len
);
1545 * Test support for AES POWER8 opcodes.
1547 int br_aes_pwr8_supported(void);
1550 * AES key schedule, using POWER8 instructions. This yields the
1551 * subkeys in the encryption direction. Number of rounds is returned.
1552 * Key size MUST be 16, 24 or 32 bytes; otherwise, 0 is returned.
1554 unsigned br_aes_pwr8_keysched(unsigned char *skni
,
1555 const void *key
, size_t len
);
1557 /* ==================================================================== */
1563 * Apply proper PKCS#1 v1.5 padding (for signatures). 'hash_oid' is
1564 * the encoded hash function OID, or NULL.
1566 uint32_t br_rsa_pkcs1_sig_pad(const unsigned char *hash_oid
,
1567 const unsigned char *hash
, size_t hash_len
,
1568 uint32_t n_bitlen
, unsigned char *x
);
1571 * Check PKCS#1 v1.5 padding (for signatures). 'hash_oid' is the encoded
1572 * hash function OID, or NULL. The provided 'sig' value is _after_ the
1573 * modular exponentiation, i.e. it should be the padded hash. On
1574 * success, the hashed message is extracted.
1576 uint32_t br_rsa_pkcs1_sig_unpad(const unsigned char *sig
, size_t sig_len
,
1577 const unsigned char *hash_oid
, size_t hash_len
,
1578 unsigned char *hash_out
);
1580 /* ==================================================================== */
1586 * Type for generic EC parameters: curve order (unsigned big-endian
1587 * encoding) and encoded conventional generator.
1591 const unsigned char *order
;
1593 const unsigned char *generator
;
1594 size_t generator_len
;
1597 extern const br_ec_curve_def br_secp256r1
;
1598 extern const br_ec_curve_def br_secp384r1
;
1599 extern const br_ec_curve_def br_secp521r1
;
1602 * For Curve25519, the advertised "order" really is 2^255-1, since the
1603 * point multipliction function really works over arbitrary 255-bit
1604 * scalars. This value is only meant as a hint for ECDH key generation;
1605 * only ECDSA uses the exact curve order, and ECDSA is not used with
1606 * that specific curve.
1608 extern const br_ec_curve_def br_curve25519
;
1611 * Decode some bytes as an i31 integer, with truncation (corresponding
1612 * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
1613 * length is provided as last parameter. The resulting value will have
1614 * this declared bit length, and consists the big-endian unsigned decoding
1615 * of exactly that many bits in the source (capped at the source length).
1617 void br_ecdsa_i31_bits2int(uint32_t *x
,
1618 const void *src
, size_t len
, uint32_t ebitlen
);
1621 * Decode some bytes as an i15 integer, with truncation (corresponding
1622 * to the 'bits2int' operation in RFC 6979). The target ENCODED bit
1623 * length is provided as last parameter. The resulting value will have
1624 * this declared bit length, and consists the big-endian unsigned decoding
1625 * of exactly that many bits in the source (capped at the source length).
1627 void br_ecdsa_i15_bits2int(uint16_t *x
,
1628 const void *src
, size_t len
, uint32_t ebitlen
);
1630 /* ==================================================================== */
1632 * SSL/TLS support functions.
1638 #define BR_SSL_CHANGE_CIPHER_SPEC 20
1639 #define BR_SSL_ALERT 21
1640 #define BR_SSL_HANDSHAKE 22
1641 #define BR_SSL_APPLICATION_DATA 23
1644 * Handshake message types.
1646 #define BR_SSL_HELLO_REQUEST 0
1647 #define BR_SSL_CLIENT_HELLO 1
1648 #define BR_SSL_SERVER_HELLO 2
1649 #define BR_SSL_CERTIFICATE 11
1650 #define BR_SSL_SERVER_KEY_EXCHANGE 12
1651 #define BR_SSL_CERTIFICATE_REQUEST 13
1652 #define BR_SSL_SERVER_HELLO_DONE 14
1653 #define BR_SSL_CERTIFICATE_VERIFY 15
1654 #define BR_SSL_CLIENT_KEY_EXCHANGE 16
1655 #define BR_SSL_FINISHED 20
1660 #define BR_LEVEL_WARNING 1
1661 #define BR_LEVEL_FATAL 2
1664 * Low-level I/O state.
1666 #define BR_IO_FAILED 0
1669 #define BR_IO_INOUT 3
1672 * Mark a SSL engine as failed. The provided error code is recorded if
1673 * the engine was not already marked as failed. If 'err' is 0, then the
1674 * engine is marked as closed (without error).
1676 void br_ssl_engine_fail(br_ssl_engine_context
*cc
, int err
);
1679 * Test whether the engine is closed (normally or as a failure).
1682 br_ssl_engine_closed(const br_ssl_engine_context
*cc
)
1684 return cc
->iomode
== BR_IO_FAILED
;
1688 * Configure a new maximum fragment length. If possible, the maximum
1689 * length for outgoing records is immediately adjusted (if there are
1690 * not already too many buffered bytes for that).
1692 void br_ssl_engine_new_max_frag_len(
1693 br_ssl_engine_context
*rc
, unsigned max_frag_len
);
1696 * Test whether the current incoming record has been fully received
1697 * or not. This functions returns 0 only if a complete record header
1698 * has been received, but some of the (possibly encrypted) payload
1699 * has not yet been obtained.
1701 int br_ssl_engine_recvrec_finished(const br_ssl_engine_context
*rc
);
1704 * Flush the current record (if not empty). This is meant to be called
1705 * from the handshake processor only.
1707 void br_ssl_engine_flush_record(br_ssl_engine_context
*cc
);
1710 * Test whether there is some accumulated payload to send.
1713 br_ssl_engine_has_pld_to_send(const br_ssl_engine_context
*rc
)
1715 return rc
->oxa
!= rc
->oxb
&& rc
->oxa
!= rc
->oxc
;
1719 * Initialize RNG in engine. Returned value is 1 on success, 0 on error.
1720 * This function will try to use the OS-provided RNG, if available. If
1721 * there is no OS-provided RNG, or if it failed, and no entropy was
1722 * injected by the caller, then a failure will be reported. On error,
1723 * the context error code is set.
1725 int br_ssl_engine_init_rand(br_ssl_engine_context
*cc
);
1728 * Reset the handshake-related parts of the engine.
1730 void br_ssl_engine_hs_reset(br_ssl_engine_context
*cc
,
1731 void (*hsinit
)(void *), void (*hsrun
)(void *));
1734 * Get the PRF to use for this context, for the provided PRF hash
1737 br_tls_prf_impl
br_ssl_engine_get_PRF(br_ssl_engine_context
*cc
, int prf_id
);
1740 * Consume the provided pre-master secret and compute the corresponding
1741 * master secret. The 'prf_id' is the ID of the hash function to use
1742 * with the TLS 1.2 PRF (ignored if the version is TLS 1.0 or 1.1).
1744 void br_ssl_engine_compute_master(br_ssl_engine_context
*cc
,
1745 int prf_id
, const void *pms
, size_t len
);
1748 * Switch to CBC decryption for incoming records.
1749 * cc the engine context
1750 * is_client non-zero for a client, zero for a server
1751 * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
1752 * mac_id id of hash function for HMAC
1753 * bc_impl block cipher implementation (CBC decryption)
1754 * cipher_key_len block cipher key length (in bytes)
1756 void br_ssl_engine_switch_cbc_in(br_ssl_engine_context
*cc
,
1757 int is_client
, int prf_id
, int mac_id
,
1758 const br_block_cbcdec_class
*bc_impl
, size_t cipher_key_len
);
1761 * Switch to CBC encryption for outgoing records.
1762 * cc the engine context
1763 * is_client non-zero for a client, zero for a server
1764 * prf_id id of hash function for PRF (ignored if not TLS 1.2+)
1765 * mac_id id of hash function for HMAC
1766 * bc_impl block cipher implementation (CBC encryption)
1767 * cipher_key_len block cipher key length (in bytes)
1769 void br_ssl_engine_switch_cbc_out(br_ssl_engine_context
*cc
,
1770 int is_client
, int prf_id
, int mac_id
,
1771 const br_block_cbcenc_class
*bc_impl
, size_t cipher_key_len
);
1774 * Switch to GCM decryption for incoming records.
1775 * cc the engine context
1776 * is_client non-zero for a client, zero for a server
1777 * prf_id id of hash function for PRF
1778 * bc_impl block cipher implementation (CTR)
1779 * cipher_key_len block cipher key length (in bytes)
1781 void br_ssl_engine_switch_gcm_in(br_ssl_engine_context
*cc
,
1782 int is_client
, int prf_id
,
1783 const br_block_ctr_class
*bc_impl
, size_t cipher_key_len
);
1786 * Switch to GCM encryption for outgoing records.
1787 * cc the engine context
1788 * is_client non-zero for a client, zero for a server
1789 * prf_id id of hash function for PRF
1790 * bc_impl block cipher implementation (CTR)
1791 * cipher_key_len block cipher key length (in bytes)
1793 void br_ssl_engine_switch_gcm_out(br_ssl_engine_context
*cc
,
1794 int is_client
, int prf_id
,
1795 const br_block_ctr_class
*bc_impl
, size_t cipher_key_len
);
1798 * Switch to ChaCha20+Poly1305 decryption for incoming records.
1799 * cc the engine context
1800 * is_client non-zero for a client, zero for a server
1801 * prf_id id of hash function for PRF
1803 void br_ssl_engine_switch_chapol_in(br_ssl_engine_context
*cc
,
1804 int is_client
, int prf_id
);
1807 * Switch to ChaCha20+Poly1305 encryption for outgoing records.
1808 * cc the engine context
1809 * is_client non-zero for a client, zero for a server
1810 * prf_id id of hash function for PRF
1812 void br_ssl_engine_switch_chapol_out(br_ssl_engine_context
*cc
,
1813 int is_client
, int prf_id
);
1816 * Calls to T0-generated code.
1818 void br_ssl_hs_client_init_main(void *ctx
);
1819 void br_ssl_hs_client_run(void *ctx
);
1820 void br_ssl_hs_server_init_main(void *ctx
);
1821 void br_ssl_hs_server_run(void *ctx
);
1824 * Get the hash function to use for signatures, given a bit mask of
1825 * supported hash functions. This implements a strict choice order
1826 * (namely SHA-256, SHA-384, SHA-512, SHA-224, SHA-1). If the mask
1827 * does not document support of any of these hash functions, then this
1828 * functions returns 0.
1830 int br_ssl_choose_hash(unsigned bf
);
1832 /* ==================================================================== */
1835 * PowerPC / POWER assembly stuff. The special BR_POWER_ASM_MACROS macro
1836 * must be defined before including this file; this is done by source
1837 * files that use some inline assembly for PowerPC / POWER machines.
1840 #if BR_POWER_ASM_MACROS
1842 #define lxvw4x(xt, ra, rb) lxvw4x_(xt, ra, rb)
1843 #define stxvw4x(xt, ra, rb) stxvw4x_(xt, ra, rb)
1845 #define bdnz(foo) bdnz_(foo)
1846 #define beq(foo) beq_(foo)
1848 #define li(rx, value) li_(rx, value)
1849 #define addi(rx, ra, imm) addi_(rx, ra, imm)
1850 #define cmpldi(rx, imm) cmpldi_(rx, imm)
1851 #define mtctr(rx) mtctr_(rx)
1852 #define vspltb(vrt, vrb, uim) vspltb_(vrt, vrb, uim)
1853 #define vspltw(vrt, vrb, uim) vspltw_(vrt, vrb, uim)
1854 #define vspltisb(vrt, imm) vspltisb_(vrt, imm)
1855 #define vspltisw(vrt, imm) vspltisw_(vrt, imm)
1856 #define vrlw(vrt, vra, vrb) vrlw_(vrt, vra, vrb)
1857 #define vsbox(vrt, vra) vsbox_(vrt, vra)
1858 #define vxor(vrt, vra, vrb) vxor_(vrt, vra, vrb)
1859 #define vand(vrt, vra, vrb) vand_(vrt, vra, vrb)
1860 #define vsro(vrt, vra, vrb) vsro_(vrt, vra, vrb)
1861 #define vsl(vrt, vra, vrb) vsl_(vrt, vra, vrb)
1862 #define vsldoi(vt, va, vb, sh) vsldoi_(vt, va, vb, sh)
1863 #define vsr(vrt, vra, vrb) vsr_(vrt, vra, vrb)
1864 #define vadduwm(vrt, vra, vrb) vadduwm_(vrt, vra, vrb)
1865 #define vsububm(vrt, vra, vrb) vsububm_(vrt, vra, vrb)
1866 #define vsubuwm(vrt, vra, vrb) vsubuwm_(vrt, vra, vrb)
1867 #define vsrw(vrt, vra, vrb) vsrw_(vrt, vra, vrb)
1868 #define vcipher(vt, va, vb) vcipher_(vt, va, vb)
1869 #define vcipherlast(vt, va, vb) vcipherlast_(vt, va, vb)
1870 #define vncipher(vt, va, vb) vncipher_(vt, va, vb)
1871 #define vncipherlast(vt, va, vb) vncipherlast_(vt, va, vb)
1872 #define vperm(vt, va, vb, vc) vperm_(vt, va, vb, vc)
1873 #define vpmsumd(vt, va, vb) vpmsumd_(vt, va, vb)
1874 #define xxpermdi(vt, va, vb, d) xxpermdi_(vt, va, vb, d)
1876 #define lxvw4x_(xt, ra, rb) "\tlxvw4x\t" #xt "," #ra "," #rb "\n"
1877 #define stxvw4x_(xt, ra, rb) "\tstxvw4x\t" #xt "," #ra "," #rb "\n"
1879 #define label(foo) #foo "%=:\n"
1880 #define bdnz_(foo) "\tbdnz\t" #foo "%=\n"
1881 #define beq_(foo) "\tbeq\t" #foo "%=\n"
1883 #define li_(rx, value) "\tli\t" #rx "," #value "\n"
1884 #define addi_(rx, ra, imm) "\taddi\t" #rx "," #ra "," #imm "\n"
1885 #define cmpldi_(rx, imm) "\tcmpldi\t" #rx "," #imm "\n"
1886 #define mtctr_(rx) "\tmtctr\t" #rx "\n"
1887 #define vspltb_(vrt, vrb, uim) "\tvspltb\t" #vrt "," #vrb "," #uim "\n"
1888 #define vspltw_(vrt, vrb, uim) "\tvspltw\t" #vrt "," #vrb "," #uim "\n"
1889 #define vspltisb_(vrt, imm) "\tvspltisb\t" #vrt "," #imm "\n"
1890 #define vspltisw_(vrt, imm) "\tvspltisw\t" #vrt "," #imm "\n"
1891 #define vrlw_(vrt, vra, vrb) "\tvrlw\t" #vrt "," #vra "," #vrb "\n"
1892 #define vsbox_(vrt, vra) "\tvsbox\t" #vrt "," #vra "\n"
1893 #define vxor_(vrt, vra, vrb) "\tvxor\t" #vrt "," #vra "," #vrb "\n"
1894 #define vand_(vrt, vra, vrb) "\tvand\t" #vrt "," #vra "," #vrb "\n"
1895 #define vsro_(vrt, vra, vrb) "\tvsro\t" #vrt "," #vra "," #vrb "\n"
1896 #define vsl_(vrt, vra, vrb) "\tvsl\t" #vrt "," #vra "," #vrb "\n"
1897 #define vsldoi_(vt, va, vb, sh) "\tvsldoi\t" #vt "," #va "," #vb "," #sh "\n"
1898 #define vsr_(vrt, vra, vrb) "\tvsr\t" #vrt "," #vra "," #vrb "\n"
1899 #define vadduwm_(vrt, vra, vrb) "\tvadduwm\t" #vrt "," #vra "," #vrb "\n"
1900 #define vsububm_(vrt, vra, vrb) "\tvsububm\t" #vrt "," #vra "," #vrb "\n"
1901 #define vsubuwm_(vrt, vra, vrb) "\tvsubuwm\t" #vrt "," #vra "," #vrb "\n"
1902 #define vsrw_(vrt, vra, vrb) "\tvsrw\t" #vrt "," #vra "," #vrb "\n"
1903 #define vcipher_(vt, va, vb) "\tvcipher\t" #vt "," #va "," #vb "\n"
1904 #define vcipherlast_(vt, va, vb) "\tvcipherlast\t" #vt "," #va "," #vb "\n"
1905 #define vncipher_(vt, va, vb) "\tvncipher\t" #vt "," #va "," #vb "\n"
1906 #define vncipherlast_(vt, va, vb) "\tvncipherlast\t" #vt "," #va "," #vb "\n"
1907 #define vperm_(vt, va, vb, vc) "\tvperm\t" #vt "," #va "," #vb "," #vc "\n"
1908 #define vpmsumd_(vt, va, vb) "\tvpmsumd\t" #vt "," #va "," #vb "\n"
1909 #define xxpermdi_(vt, va, vb, d) "\txxpermdi\t" #vt "," #va "," #vb "," #d "\n"
1913 /* ==================================================================== */