| 1 |
/* Rijndael (AES) for GnuPG |
| 2 |
* Copyright (C) 2000, 2001, 2002, 2003, 2007, |
| 3 |
* 2008, 2011, 2012 Free Software Foundation, Inc. |
| 4 |
* |
| 5 |
* This file is part of Libgcrypt. |
| 6 |
* |
| 7 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2.1 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef G10_RIJNDAEL_INTERNAL_H |
| 22 |
#define G10_RIJNDAEL_INTERNAL_H |
| 23 |
|
| 24 |
#include "types.h" /* for byte and u32 typedefs */ |
| 25 |
|
| 26 |
|
| 27 |
#define MAXKC (256/32) |
| 28 |
#define MAXROUNDS 14 |
| 29 |
#define BLOCKSIZE (128/8) |
| 30 |
|
| 31 |
|
| 32 |
/* Helper macro to force alignment to 16 or 64 bytes. */ |
| 33 |
#ifdef HAVE_GCC_ATTRIBUTE_ALIGNED |
| 34 |
# define ATTR_ALIGNED_16 __attribute__ ((aligned (16))) |
| 35 |
# define ATTR_ALIGNED_64 __attribute__ ((aligned (64))) |
| 36 |
#else |
| 37 |
# define ATTR_ALIGNED_16 |
| 38 |
# define ATTR_ALIGNED_64 |
| 39 |
#endif |
| 40 |
|
| 41 |
|
| 42 |
/* USE_AMD64_ASM indicates whether to use AMD64 assembly code. */ |
| 43 |
#undef USE_AMD64_ASM |
| 44 |
#if defined(__x86_64__) && (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 45 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) |
| 46 |
# define USE_AMD64_ASM 1 |
| 47 |
#endif |
| 48 |
|
| 49 |
/* USE_SSSE3 indicates whether to use SSSE3 code. */ |
| 50 |
#if defined(__x86_64__) && defined(HAVE_GCC_INLINE_ASM_SSSE3) && \ |
| 51 |
(defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 52 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) |
| 53 |
# define USE_SSSE3 1 |
| 54 |
#endif |
| 55 |
|
| 56 |
/* USE_ARM_ASM indicates whether to use ARM assembly code. */ |
| 57 |
#undef USE_ARM_ASM |
| 58 |
#if defined(__ARMEL__) |
| 59 |
# ifdef HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS |
| 60 |
# define USE_ARM_ASM 1 |
| 61 |
# endif |
| 62 |
#endif |
| 63 |
#if defined(__AARCH64EL__) |
| 64 |
# ifdef HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS |
| 65 |
# define USE_ARM_ASM 1 |
| 66 |
# endif |
| 67 |
#endif |
| 68 |
|
| 69 |
/* USE_PADLOCK indicates whether to compile the padlock specific |
| 70 |
code. */ |
| 71 |
#undef USE_PADLOCK |
| 72 |
#ifdef ENABLE_PADLOCK_SUPPORT |
| 73 |
# ifdef HAVE_GCC_ATTRIBUTE_ALIGNED |
| 74 |
# if (defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__) |
| 75 |
# define USE_PADLOCK 1 |
| 76 |
# endif |
| 77 |
# endif |
| 78 |
#endif /* ENABLE_PADLOCK_SUPPORT */ |
| 79 |
|
| 80 |
/* USE_AESNI inidicates whether to compile with Intel AES-NI code. We |
| 81 |
need the vector-size attribute which seems to be available since |
| 82 |
gcc 3. However, to be on the safe side we require at least gcc 4. */ |
| 83 |
#undef USE_AESNI |
| 84 |
#ifdef ENABLE_AESNI_SUPPORT |
| 85 |
# if ((defined (__i386__) && SIZEOF_UNSIGNED_LONG == 4) || defined(__x86_64__)) |
| 86 |
# if __GNUC__ >= 4 |
| 87 |
# define USE_AESNI 1 |
| 88 |
# endif |
| 89 |
# endif |
| 90 |
#endif /* ENABLE_AESNI_SUPPORT */ |
| 91 |
|
| 92 |
/* USE_VAES inidicates whether to compile with AMD64 VAES/AVX2 code. */ |
| 93 |
#undef USE_VAES |
| 94 |
#if (defined(HAVE_COMPATIBLE_GCC_AMD64_PLATFORM_AS) || \ |
| 95 |
defined(HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS)) && \ |
| 96 |
defined(__x86_64__) && defined(ENABLE_AVX2_SUPPORT) && \ |
| 97 |
defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL) && \ |
| 98 |
defined(USE_AESNI) |
| 99 |
# define USE_VAES 1 |
| 100 |
#endif |
| 101 |
|
| 102 |
/* USE_VAES inidicates whether to compile with AMD64 VAES/AVX512 code. */ |
| 103 |
#undef USE_VAES_AVX512 |
| 104 |
#if defined(USE_VAES) && defined(ENABLE_AVX512_SUPPORT) |
| 105 |
# define USE_VAES_AVX512 1 |
| 106 |
#endif |
| 107 |
|
| 108 |
/* USE_VAES_I386 inidicates whether to compile with i386 VAES code. */ |
| 109 |
#undef USE_VAES_I386 |
| 110 |
#if (defined(HAVE_COMPATIBLE_GCC_I386_PLATFORM_AS) || \ |
| 111 |
defined(HAVE_COMPATIBLE_GCC_WIN32_PLATFORM_AS)) && \ |
| 112 |
defined(__i386__) && defined(ENABLE_AVX2_SUPPORT) && \ |
| 113 |
defined(HAVE_GCC_INLINE_ASM_VAES_VPCLMUL) && \ |
| 114 |
defined(USE_AESNI) |
| 115 |
# define USE_VAES_I386 1 |
| 116 |
#endif |
| 117 |
|
| 118 |
/* USE_ARM_CE indicates whether to enable ARMv8 Crypto Extension assembly |
| 119 |
* code. */ |
| 120 |
#undef USE_ARM_CE |
| 121 |
#ifdef ENABLE_ARM_CRYPTO_SUPPORT |
| 122 |
# if defined(HAVE_ARM_ARCH_V6) && defined(__ARMEL__) \ |
| 123 |
&& defined(HAVE_COMPATIBLE_GCC_ARM_PLATFORM_AS) \ |
| 124 |
&& defined(HAVE_GCC_INLINE_ASM_AARCH32_CRYPTO) |
| 125 |
# define USE_ARM_CE 1 |
| 126 |
# elif defined(__AARCH64EL__) \ |
| 127 |
&& defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) \ |
| 128 |
&& defined(HAVE_GCC_INLINE_ASM_AARCH64_CRYPTO) |
| 129 |
# define USE_ARM_CE 1 |
| 130 |
# endif |
| 131 |
#endif /* ENABLE_ARM_CRYPTO_SUPPORT */ |
| 132 |
|
| 133 |
/* USE_RISCV_V_CRYPTO indicates whether to enable RISC-V vector cryptography |
| 134 |
* extension code. */ |
| 135 |
#undef USE_RISCV_V_CRYPTO |
| 136 |
#if defined (__riscv) && \ |
| 137 |
defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) && \ |
| 138 |
defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_CRYPTO_INTRINSICS) |
| 139 |
# define USE_RISCV_V_CRYPTO 1 |
| 140 |
#endif |
| 141 |
|
| 142 |
/* USE_VP_AARCH64 indicates whether to enable vector permute AArch64 SIMD code. */ |
| 143 |
#undef USE_VP_AARCH64 |
| 144 |
#if defined(__AARCH64EL__) && defined(HAVE_COMPATIBLE_GCC_AARCH64_PLATFORM_AS) |
| 145 |
# define USE_VP_AARCH64 1 |
| 146 |
#endif |
| 147 |
|
| 148 |
/* USE_VP_RISCV indicates whether to enable vector permute RISC-V code. */ |
| 149 |
#undef USE_VP_RISCV |
| 150 |
#if defined (__riscv) && defined(HAVE_COMPATIBLE_CC_RISCV_VECTOR_INTRINSICS) |
| 151 |
# define USE_VP_RISCV 1 |
| 152 |
#endif |
| 153 |
|
| 154 |
/* USE_PPC_CRYPTO indicates whether to enable PowerPC vector crypto |
| 155 |
* accelerated code. USE_PPC_CRYPTO_WITH_PPC9LE indicates whether to |
| 156 |
* enable POWER9 optimized variant. */ |
| 157 |
#undef USE_PPC_CRYPTO |
| 158 |
#undef USE_PPC_CRYPTO_WITH_PPC9LE |
| 159 |
#ifdef ENABLE_PPC_CRYPTO_SUPPORT |
| 160 |
# if defined(HAVE_COMPATIBLE_CC_PPC_ALTIVEC) && \ |
| 161 |
defined(HAVE_GCC_INLINE_ASM_PPC_ALTIVEC) |
| 162 |
# if __GNUC__ >= 4 |
| 163 |
# define USE_PPC_CRYPTO 1 |
| 164 |
# if !defined(WORDS_BIGENDIAN) && defined(HAVE_GCC_INLINE_ASM_PPC_ARCH_3_00) |
| 165 |
# define USE_PPC_CRYPTO_WITH_PPC9LE 1 |
| 166 |
# endif |
| 167 |
# endif |
| 168 |
# endif |
| 169 |
#endif /* ENABLE_PPC_CRYPTO_SUPPORT */ |
| 170 |
|
| 171 |
/* USE_S390X_CRYPTO indicates whether to enable zSeries code. */ |
| 172 |
#undef USE_S390X_CRYPTO |
| 173 |
#if defined(HAVE_GCC_INLINE_ASM_S390X) |
| 174 |
# define USE_S390X_CRYPTO 1 |
| 175 |
#endif /* USE_S390X_CRYPTO */ |
| 176 |
|
| 177 |
struct RIJNDAEL_context_s; |
| 178 |
|
| 179 |
typedef unsigned int (*rijndael_cryptfn_t)(const struct RIJNDAEL_context_s *ctx, |
| 180 |
unsigned char *bx, |
| 181 |
const unsigned char *ax); |
| 182 |
typedef void (*rijndael_prefetchfn_t)(void); |
| 183 |
typedef void (*rijndael_prepare_decfn_t)(struct RIJNDAEL_context_s *ctx); |
| 184 |
|
| 185 |
/* Our context object. */ |
| 186 |
typedef struct RIJNDAEL_context_s |
| 187 |
{ |
| 188 |
/* The first fields are the keyschedule arrays. This is so that |
| 189 |
they are aligned on a 16 byte boundary if using gcc. This |
| 190 |
alignment is required for the AES-NI code and a good idea in any |
| 191 |
case. The alignment is guaranteed due to the way cipher.c |
| 192 |
allocates the space for the context. The PROPERLY_ALIGNED_TYPE |
| 193 |
hack is used to force a minimal alignment if not using gcc of if |
| 194 |
the alignment requirement is higher that 16 bytes. */ |
| 195 |
union |
| 196 |
{ |
| 197 |
PROPERLY_ALIGNED_TYPE dummy; |
| 198 |
byte keyschedule[MAXROUNDS+1][4][4]; |
| 199 |
u32 keyschedule32[MAXROUNDS+1][4]; |
| 200 |
u32 keyschedule32b[(MAXROUNDS+1)*4]; |
| 201 |
#ifdef USE_PADLOCK |
| 202 |
/* The key as passed to the padlock engine. It is only used if |
| 203 |
the padlock engine is used (USE_PADLOCK, below). */ |
| 204 |
unsigned char padlock_key[16] __attribute__ ((aligned (16))); |
| 205 |
#endif /*USE_PADLOCK*/ |
| 206 |
} u1; |
| 207 |
union |
| 208 |
{ |
| 209 |
PROPERLY_ALIGNED_TYPE dummy; |
| 210 |
byte keyschedule[MAXROUNDS+1][4][4]; |
| 211 |
u32 keyschedule32[MAXROUNDS+1][4]; |
| 212 |
} u2; |
| 213 |
int rounds; /* Key-length-dependent number of rounds. */ |
| 214 |
unsigned int decryption_prepared:1; /* The decryption key schedule is available. */ |
| 215 |
#ifdef USE_AESNI |
| 216 |
unsigned int use_avx:1; /* AVX shall be used by AES-NI implementation. */ |
| 217 |
unsigned int use_avx2:1; /* AVX2 shall be used by AES-NI implementation. */ |
| 218 |
#endif /*USE_AESNI*/ |
| 219 |
#ifdef USE_VAES_AVX512 |
| 220 |
unsigned int use_vaes_avx512:1; /* AVX512 shall be used by VAES implementation. */ |
| 221 |
#endif /*USE_VAES_AVX512*/ |
| 222 |
#ifdef USE_S390X_CRYPTO |
| 223 |
byte km_func; |
| 224 |
byte km_func_xts; |
| 225 |
byte kmc_func; |
| 226 |
byte kmac_func; |
| 227 |
byte kmf_func; |
| 228 |
byte kmo_func; |
| 229 |
byte kma_func; |
| 230 |
#endif /*USE_S390X_CRYPTO*/ |
| 231 |
rijndael_cryptfn_t encrypt_fn; |
| 232 |
rijndael_cryptfn_t decrypt_fn; |
| 233 |
rijndael_prefetchfn_t prefetch_enc_fn; |
| 234 |
rijndael_prefetchfn_t prefetch_dec_fn; |
| 235 |
rijndael_prepare_decfn_t prepare_decryption; |
| 236 |
} RIJNDAEL_context ATTR_ALIGNED_16; |
| 237 |
|
| 238 |
/* Macros defining alias for the keyschedules. */ |
| 239 |
#define keyschenc u1.keyschedule |
| 240 |
#define keyschenc32 u1.keyschedule32 |
| 241 |
#define keyschenc32b u1.keyschedule32b |
| 242 |
#define keyschdec u2.keyschedule |
| 243 |
#define keyschdec32 u2.keyschedule32 |
| 244 |
#define padlockkey u1.padlock_key |
| 245 |
|
| 246 |
#endif /* G10_RIJNDAEL_INTERNAL_H */ |