| 1 |
/* Rijndael (AES) for GnuPG - PowerPC Vector Crypto AES implementation |
| 2 |
* Copyright (C) 2019 Shawn Landden <[email protected]> |
| 3 |
* Copyright (C) 2019-2020 Jussi Kivilinna <[email protected]> |
| 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 |
* Alternatively, this code may be used in OpenSSL from The OpenSSL Project, |
| 21 |
* and Cryptogams by Andy Polyakov, and if made part of a release of either |
| 22 |
* or both projects, is thereafter dual-licensed under the license said project |
| 23 |
* is released under. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef G10_RIJNDAEL_PPC_COMMON_H |
| 27 |
#define G10_RIJNDAEL_PPC_COMMON_H |
| 28 |
|
| 29 |
#include "simd-common-ppc.h" |
| 30 |
#include <altivec.h> |
| 31 |
|
| 32 |
|
| 33 |
typedef vector unsigned char block; |
| 34 |
typedef vector unsigned int vec_u32; |
| 35 |
|
| 36 |
typedef union |
| 37 |
{ |
| 38 |
u32 data32[4]; |
| 39 |
} __attribute__((packed, aligned(1), may_alias)) u128_t; |
| 40 |
|
| 41 |
|
| 42 |
#define ALWAYS_INLINE inline __attribute__((always_inline)) |
| 43 |
#define NO_INLINE __attribute__((noinline)) |
| 44 |
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function)) |
| 45 |
|
| 46 |
#define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION |
| 47 |
#define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE |
| 48 |
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE |
| 49 |
|
| 50 |
|
| 51 |
#define ALIGNED_LOAD(in_ptr, offs) \ |
| 52 |
(asm_aligned_ld ((offs) * 16, (const void *)(in_ptr))) |
| 53 |
|
| 54 |
#define ALIGNED_STORE(out_ptr, offs, vec) \ |
| 55 |
(asm_aligned_st ((vec), (offs) * 16, (void *)(out_ptr))) |
| 56 |
|
| 57 |
#define VEC_BE_SWAP(vec, bige_const) (asm_be_swap ((vec), (bige_const))) |
| 58 |
|
| 59 |
#define VEC_LOAD_BE(in_ptr, offs, bige_const) \ |
| 60 |
(asm_be_swap (asm_load_be_noswap ((offs) * 16, (const void *)(in_ptr)), \ |
| 61 |
bige_const)) |
| 62 |
|
| 63 |
#define VEC_LOAD_BE_NOSWAP(in_ptr, offs) \ |
| 64 |
(asm_load_be_noswap ((offs) * 16, (const unsigned char *)(in_ptr))) |
| 65 |
|
| 66 |
#define VEC_STORE_BE(out_ptr, offs, vec, bige_const) \ |
| 67 |
(asm_store_be_noswap (asm_be_swap ((vec), (bige_const)), (offs) * 16, \ |
| 68 |
(void *)(out_ptr))) |
| 69 |
|
| 70 |
#define VEC_STORE_BE_NOSWAP(out_ptr, offs, vec) \ |
| 71 |
(asm_store_be_noswap ((vec), (offs) * 16, (void *)(out_ptr))) |
| 72 |
|
| 73 |
|
| 74 |
#define ROUND_KEY_VARIABLES \ |
| 75 |
block rkey0, rkeylast |
| 76 |
|
| 77 |
#define PRELOAD_ROUND_KEYS(nrounds) \ |
| 78 |
do { \ |
| 79 |
rkey0 = ALIGNED_LOAD (rk, 0); \ |
| 80 |
rkeylast = ALIGNED_LOAD (rk, nrounds); \ |
| 81 |
} while (0) |
| 82 |
|
| 83 |
#define AES_ENCRYPT(blk, nrounds) \ |
| 84 |
do { \ |
| 85 |
blk ^= rkey0; \ |
| 86 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 1)); \ |
| 87 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 2)); \ |
| 88 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 3)); \ |
| 89 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 4)); \ |
| 90 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 5)); \ |
| 91 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 6)); \ |
| 92 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 7)); \ |
| 93 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 8)); \ |
| 94 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 9)); \ |
| 95 |
if (nrounds >= 12) \ |
| 96 |
{ \ |
| 97 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 10)); \ |
| 98 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 11)); \ |
| 99 |
if (rounds > 12) \ |
| 100 |
{ \ |
| 101 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 12)); \ |
| 102 |
blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 13)); \ |
| 103 |
} \ |
| 104 |
} \ |
| 105 |
blk = asm_cipherlast_be (blk, rkeylast); \ |
| 106 |
} while (0) |
| 107 |
|
| 108 |
#define AES_DECRYPT(blk, nrounds) \ |
| 109 |
do { \ |
| 110 |
blk ^= rkey0; \ |
| 111 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 1)); \ |
| 112 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 2)); \ |
| 113 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 3)); \ |
| 114 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 4)); \ |
| 115 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 5)); \ |
| 116 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 6)); \ |
| 117 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 7)); \ |
| 118 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 8)); \ |
| 119 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 9)); \ |
| 120 |
if (nrounds >= 12) \ |
| 121 |
{ \ |
| 122 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 10)); \ |
| 123 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 11)); \ |
| 124 |
if (rounds > 12) \ |
| 125 |
{ \ |
| 126 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 12)); \ |
| 127 |
blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 13)); \ |
| 128 |
} \ |
| 129 |
} \ |
| 130 |
blk = asm_ncipherlast_be (blk, rkeylast); \ |
| 131 |
} while (0) |
| 132 |
|
| 133 |
|
| 134 |
#define ROUND_KEY_VARIABLES_ALL \ |
| 135 |
block rkey0, rkey1, rkey2, rkey3, rkey4, rkey5, rkey6, rkey7, rkey8, \ |
| 136 |
rkey9, rkey10, rkey11, rkey12, rkey13, rkeylast |
| 137 |
|
| 138 |
#define PRELOAD_ROUND_KEYS_ALL(nrounds) \ |
| 139 |
do { \ |
| 140 |
static const block preload_zero = { 0 }; \ |
| 141 |
rkey0 = ALIGNED_LOAD (rk, 0); \ |
| 142 |
rkey1 = ALIGNED_LOAD (rk, 1); \ |
| 143 |
rkey2 = ALIGNED_LOAD (rk, 2); \ |
| 144 |
rkey3 = ALIGNED_LOAD (rk, 3); \ |
| 145 |
rkey4 = ALIGNED_LOAD (rk, 4); \ |
| 146 |
rkey5 = ALIGNED_LOAD (rk, 5); \ |
| 147 |
rkey6 = ALIGNED_LOAD (rk, 6); \ |
| 148 |
rkey7 = ALIGNED_LOAD (rk, 7); \ |
| 149 |
rkey8 = ALIGNED_LOAD (rk, 8); \ |
| 150 |
rkey9 = ALIGNED_LOAD (rk, 9); \ |
| 151 |
rkey10 = preload_zero; \ |
| 152 |
rkey11 = preload_zero; \ |
| 153 |
rkey12 = preload_zero; \ |
| 154 |
rkey13 = preload_zero; \ |
| 155 |
if (nrounds >= 12) \ |
| 156 |
{ \ |
| 157 |
rkey10 = ALIGNED_LOAD (rk, 10); \ |
| 158 |
rkey11 = ALIGNED_LOAD (rk, 11); \ |
| 159 |
if (rounds > 12) \ |
| 160 |
{ \ |
| 161 |
rkey12 = ALIGNED_LOAD (rk, 12); \ |
| 162 |
rkey13 = ALIGNED_LOAD (rk, 13); \ |
| 163 |
} \ |
| 164 |
} \ |
| 165 |
rkeylast = ALIGNED_LOAD (rk, nrounds); \ |
| 166 |
} while (0) |
| 167 |
|
| 168 |
|
| 169 |
static ASM_FUNC_ATTR_INLINE block |
| 170 |
asm_aligned_ld(unsigned long offset, const void *ptr) |
| 171 |
{ |
| 172 |
block vec; |
| 173 |
#if __GNUC__ >= 4 |
| 174 |
if (__builtin_constant_p (offset) && offset == 0) |
| 175 |
__asm__ volatile ("lvx %0,0,%1\n\t" |
| 176 |
: "=v" (vec) |
| 177 |
: "r" ((uintptr_t)ptr) |
| 178 |
: "memory"); |
| 179 |
else |
| 180 |
#endif |
| 181 |
__asm__ volatile ("lvx %0,%1,%2\n\t" |
| 182 |
: "=v" (vec) |
| 183 |
: "r" (offset), "r" ((uintptr_t)ptr) |
| 184 |
: "memory", "r0"); |
| 185 |
return vec; |
| 186 |
} |
| 187 |
|
| 188 |
static ASM_FUNC_ATTR_INLINE void |
| 189 |
asm_aligned_st(block vec, unsigned long offset, void *ptr) |
| 190 |
{ |
| 191 |
#if __GNUC__ >= 4 |
| 192 |
if (__builtin_constant_p (offset) && offset == 0) |
| 193 |
__asm__ volatile ("stvx %0,0,%1\n\t" |
| 194 |
: |
| 195 |
: "v" (vec), "r" ((uintptr_t)ptr) |
| 196 |
: "memory"); |
| 197 |
else |
| 198 |
#endif |
| 199 |
__asm__ volatile ("stvx %0,%1,%2\n\t" |
| 200 |
: |
| 201 |
: "v" (vec), "r" (offset), "r" ((uintptr_t)ptr) |
| 202 |
: "memory", "r0"); |
| 203 |
} |
| 204 |
|
| 205 |
static ASM_FUNC_ATTR_INLINE block |
| 206 |
asm_vperm1(block vec, block mask) |
| 207 |
{ |
| 208 |
block o; |
| 209 |
__asm__ volatile ("vperm %0,%1,%1,%2\n\t" |
| 210 |
: "=v" (o) |
| 211 |
: "v" (vec), "v" (mask)); |
| 212 |
return o; |
| 213 |
} |
| 214 |
|
| 215 |
static ASM_FUNC_ATTR_INLINE block |
| 216 |
asm_add_uint128(block a, block b) |
| 217 |
{ |
| 218 |
block res; |
| 219 |
__asm__ volatile ("vadduqm %0,%1,%2\n\t" |
| 220 |
: "=v" (res) |
| 221 |
: "v" (a), "v" (b)); |
| 222 |
return res; |
| 223 |
} |
| 224 |
|
| 225 |
static ASM_FUNC_ATTR_INLINE block |
| 226 |
asm_add_uint64(block a, block b) |
| 227 |
{ |
| 228 |
block res; |
| 229 |
__asm__ volatile ("vaddudm %0,%1,%2\n\t" |
| 230 |
: "=v" (res) |
| 231 |
: "v" (a), "v" (b)); |
| 232 |
return res; |
| 233 |
} |
| 234 |
|
| 235 |
static ASM_FUNC_ATTR_INLINE block |
| 236 |
asm_sra_int64(block a, block b) |
| 237 |
{ |
| 238 |
block res; |
| 239 |
__asm__ volatile ("vsrad %0,%1,%2\n\t" |
| 240 |
: "=v" (res) |
| 241 |
: "v" (a), "v" (b)); |
| 242 |
return res; |
| 243 |
} |
| 244 |
|
| 245 |
static block |
| 246 |
asm_swap_uint64_halfs(block a) |
| 247 |
{ |
| 248 |
block res; |
| 249 |
__asm__ volatile ("xxswapd %x0, %x1" |
| 250 |
: "=wa" (res) |
| 251 |
: "wa" (a)); |
| 252 |
return res; |
| 253 |
} |
| 254 |
|
| 255 |
static ASM_FUNC_ATTR_INLINE block |
| 256 |
asm_xor(block a, block b) |
| 257 |
{ |
| 258 |
block res; |
| 259 |
__asm__ volatile ("vxor %0,%1,%2\n\t" |
| 260 |
: "=v" (res) |
| 261 |
: "v" (a), "v" (b)); |
| 262 |
return res; |
| 263 |
} |
| 264 |
|
| 265 |
static ASM_FUNC_ATTR_INLINE block |
| 266 |
asm_sbox_be(block b) |
| 267 |
{ |
| 268 |
block o; |
| 269 |
__asm__ volatile ("vsbox %0, %1\n\t" |
| 270 |
: "=v" (o) |
| 271 |
: "v" (b)); |
| 272 |
return o; |
| 273 |
} |
| 274 |
|
| 275 |
static ASM_FUNC_ATTR_INLINE block |
| 276 |
asm_cipher_be(block b, block rk) |
| 277 |
{ |
| 278 |
block o; |
| 279 |
__asm__ volatile ("vcipher %0, %1, %2\n\t" |
| 280 |
: "=v" (o) |
| 281 |
: "v" (b), "v" (rk)); |
| 282 |
return o; |
| 283 |
} |
| 284 |
|
| 285 |
static ASM_FUNC_ATTR_INLINE block |
| 286 |
asm_cipherlast_be(block b, block rk) |
| 287 |
{ |
| 288 |
block o; |
| 289 |
__asm__ volatile ("vcipherlast %0, %1, %2\n\t" |
| 290 |
: "=v" (o) |
| 291 |
: "v" (b), "v" (rk)); |
| 292 |
return o; |
| 293 |
} |
| 294 |
|
| 295 |
static ASM_FUNC_ATTR_INLINE block |
| 296 |
asm_ncipher_be(block b, block rk) |
| 297 |
{ |
| 298 |
block o; |
| 299 |
__asm__ volatile ("vncipher %0, %1, %2\n\t" |
| 300 |
: "=v" (o) |
| 301 |
: "v" (b), "v" (rk)); |
| 302 |
return o; |
| 303 |
} |
| 304 |
|
| 305 |
static ASM_FUNC_ATTR_INLINE block |
| 306 |
asm_ncipherlast_be(block b, block rk) |
| 307 |
{ |
| 308 |
block o; |
| 309 |
__asm__ volatile ("vncipherlast %0, %1, %2\n\t" |
| 310 |
: "=v" (o) |
| 311 |
: "v" (b), "v" (rk)); |
| 312 |
return o; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
/* Make a decryption key from an encryption key. */ |
| 317 |
static ASM_FUNC_ATTR_INLINE void |
| 318 |
internal_aes_ppc_prepare_decryption (RIJNDAEL_context *ctx) |
| 319 |
{ |
| 320 |
u128_t *ekey = (u128_t *)(void *)ctx->keyschenc; |
| 321 |
u128_t *dkey = (u128_t *)(void *)ctx->keyschdec; |
| 322 |
int rounds = ctx->rounds; |
| 323 |
int rr; |
| 324 |
int r; |
| 325 |
|
| 326 |
r = 0; |
| 327 |
rr = rounds; |
| 328 |
for (r = 0, rr = rounds; r <= rounds; r++, rr--) |
| 329 |
{ |
| 330 |
ALIGNED_STORE (dkey, r, ALIGNED_LOAD (ekey, rr)); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
#endif /* G10_RIJNDAEL_PPC_COMMON_H */ |