xref: /aosp_15_r20/external/boringssl/src/crypto/hrss/internal.h (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2018, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #ifndef OPENSSL_HEADER_HRSS_INTERNAL_H
16 #define OPENSSL_HEADER_HRSS_INTERNAL_H
17 
18 #include <openssl/base.h>
19 #include "../internal.h"
20 
21 #if defined(__cplusplus)
22 extern "C" {
23 #endif
24 
25 
26 #define N 701
27 #define BITS_PER_WORD (sizeof(crypto_word_t) * 8)
28 #define WORDS_PER_POLY ((N + BITS_PER_WORD - 1) / BITS_PER_WORD)
29 #define BITS_IN_LAST_WORD (N % BITS_PER_WORD)
30 
31 struct poly2 {
32   crypto_word_t v[WORDS_PER_POLY];
33 };
34 
35 struct poly3 {
36   struct poly2 s, a;
37 };
38 
39 OPENSSL_EXPORT void HRSS_poly3_mul(struct poly3 *out, const struct poly3 *x,
40                                    const struct poly3 *y);
41 OPENSSL_EXPORT void HRSS_poly3_invert(struct poly3 *out,
42                                       const struct poly3 *in);
43 
44 // On x86-64, we can use the AVX2 code from [HRSS]. (The authors have given
45 // explicit permission for this and signed a CLA.) However it's 57KB of object
46 // code, so it's not used if |OPENSSL_SMALL| is defined.
47 #if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_SMALL) && \
48     defined(OPENSSL_X86_64) && defined(OPENSSL_LINUX)
49 #define POLY_RQ_MUL_ASM
50 // POLY_MUL_RQ_SCRATCH_SPACE is the number of bytes of scratch space needed
51 // by the assembly function poly_Rq_mul.
52 #define POLY_MUL_RQ_SCRATCH_SPACE (6144 + 6144 + 12288 + 512 + 9408 + 32)
53 
54 // poly_Rq_mul is defined in assembly. Inputs and outputs must be 16-byte-
55 // aligned.
56 extern void poly_Rq_mul(
57     uint16_t r[N + 3], const uint16_t a[N + 3], const uint16_t b[N + 3],
58     // The following should be `scratch[POLY_MUL_RQ_SCRATCH_SPACE]` but
59     // GCC 11.1 has a bug with unions that breaks that.
60     uint8_t scratch[]);
61 #endif
62 
63 
64 #if defined(__cplusplus)
65 }  // extern "C"
66 #endif
67 
68 #endif  // !OPENSSL_HEADER_HRSS_INTERNAL_H
69