| 1 |
/* AArch64 SIMD vector permutation AES for Libgcrypt |
| 2 |
* Copyright (C) 2014-2025 Jussi Kivilinna <[email protected]> |
| 3 |
* |
| 4 |
* This file is part of Libgcrypt. |
| 5 |
* |
| 6 |
* Libgcrypt is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as |
| 8 |
* published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgcrypt is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* |
| 20 |
* The code is based on the public domain library libvpaes version 0.5 |
| 21 |
* available at http://crypto.stanford.edu/vpaes/ and which carries |
| 22 |
* this notice: |
| 23 |
* |
| 24 |
* libvpaes: constant-time SSSE3 AES encryption and decryption. |
| 25 |
* version 0.5 |
| 26 |
* |
| 27 |
* By Mike Hamburg, Stanford University, 2009. Public domain. |
| 28 |
* I wrote essentially all of this code. I did not write the test |
| 29 |
* vectors; they are the NIST known answer tests. I hereby release all |
| 30 |
* the code and documentation here that I wrote into the public domain. |
| 31 |
* |
| 32 |
* This is an implementation of AES following my paper, |
| 33 |
* "Accelerating AES with Vector Permute Instructions" |
| 34 |
* CHES 2009; http://shiftleft.org/papers/vector_aes/ |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <config.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> /* for memcmp() */ |
| 41 |
|
| 42 |
#include "types.h" /* for byte and u32 typedefs */ |
| 43 |
#include "g10lib.h" |
| 44 |
#include "cipher.h" |
| 45 |
#include "bufhelp.h" |
| 46 |
#include "rijndael-internal.h" |
| 47 |
#include "./cipher-internal.h" |
| 48 |
|
| 49 |
|
| 50 |
#ifdef USE_VP_AARCH64 |
| 51 |
|
| 52 |
|
| 53 |
/********************************************************************** |
| 54 |
AT&T x86 asm to intrinsics conversion macros (ARM) |
| 55 |
**********************************************************************/ |
| 56 |
|
| 57 |
#include "simd-common-aarch64.h" |
| 58 |
#include <arm_neon.h> |
| 59 |
|
| 60 |
#define __m128i uint64x2_t |
| 61 |
|
| 62 |
#define pand128(a, o) (o = vandq_u64(o, a)) |
| 63 |
#define pandn128(a, o) (o = vbicq_u64(a, o)) |
| 64 |
#define pxor128(a, o) (o = veorq_u64(o, a)) |
| 65 |
#define paddq128(a, o) (o = vaddq_u64(o, a)) |
| 66 |
#define paddd128(a, o) (o = (__m128i)vaddq_u32((uint32x4_t)o, (uint32x4_t)a)) |
| 67 |
#define paddb128(a, o) (o = (__m128i)vaddq_u8((uint8x16_t)o, (uint8x16_t)a)) |
| 68 |
|
| 69 |
#define psrld128(s, o) (o = (__m128i)vshrq_n_u32((uint32x4_t)o, s)) |
| 70 |
#define psraq128(s, o) (o = (__m128i)vshrq_n_s64((int64x2_t)o, s)) |
| 71 |
#define psrldq128(s, o) ({ uint64x2_t __tmp = { 0, 0 }; \ |
| 72 |
o = (__m128i)vextq_u8((uint8x16_t)o, \ |
| 73 |
(uint8x16_t)__tmp, (s) & 15);}) |
| 74 |
#define pslldq128(s, o) ({ uint64x2_t __tmp = { 0, 0 }; \ |
| 75 |
o = (__m128i)vextq_u8((uint8x16_t)__tmp, \ |
| 76 |
(uint8x16_t)o, (16 - (s)) & 15);}) |
| 77 |
#define psrl_byte_128(s, o) (o = (__m128i)vshrq_n_u8((uint8x16_t)o, s)) |
| 78 |
|
| 79 |
#define pshufb128(m8, o) (o = (__m128i)vqtbl1q_u8((uint8x16_t)o, (uint8x16_t)m8)) |
| 80 |
#define pshufd128(m32, a, o) ({ static const __m128i __tmp1 = PSHUFD_MASK_TO_PSHUFB_MASK(m32); \ |
| 81 |
__m128i __tmp2; \ |
| 82 |
movdqa128(a, o); \ |
| 83 |
movdqa128_memld(&__tmp1, __tmp2); \ |
| 84 |
pshufb128(__tmp2, o); }) |
| 85 |
#define pshufd128_0x93(a, o) (o = (__m128i)vextq_u8((uint8x16_t)a, (uint8x16_t)a, 12)) |
| 86 |
#define pshufd128_0xFF(a, o) (o = (__m128i)vdupq_laneq_u32((uint32x4_t)a, 3)) |
| 87 |
#define pshufd128_0xFE(a, o) pshufd128(0xFE, a, o) |
| 88 |
#define pshufd128_0x4E(a, o) (o = (__m128i)vextq_u8((uint8x16_t)a, (uint8x16_t)a, 8)) |
| 89 |
|
| 90 |
#define palignr128(s, a, o) (o = (__m128i)vextq_u8((uint8x16_t)a, (uint8x16_t)o, s)) |
| 91 |
|
| 92 |
#define movdqa128(a, o) (o = a) |
| 93 |
|
| 94 |
#define movdqa128_memld(a, o) (o = (__m128i)vld1q_u8((const uint8_t *)(a))) |
| 95 |
|
| 96 |
#define pand128_amemld(m, o) pand128((__m128i)vld1q_u8((const uint8_t *)(m)), o) |
| 97 |
#define pxor128_amemld(m, o) pxor128((__m128i)vld1q_u8((const uint8_t *)(m)), o) |
| 98 |
#define paddq128_amemld(m, o) paddq128((__m128i)vld1q_u8((const uint8_t *)(m)), o) |
| 99 |
#define paddd128_amemld(m, o) paddd128((__m128i)vld1q_u8((const uint8_t *)(m)), o) |
| 100 |
#define pshufb128_amemld(m, o) pshufb128((__m128i)vld1q_u8((const uint8_t *)(m)), o) |
| 101 |
|
| 102 |
/* Following operations may have unaligned memory input */ |
| 103 |
#define movdqu128_memld(a, o) (o = (__m128i)vld1q_u8((const uint8_t *)(a))) |
| 104 |
|
| 105 |
/* Following operations may have unaligned memory output */ |
| 106 |
#define movdqu128_memst(a, o) vst1q_u8((uint8_t *)(o), (uint8x16_t)a) |
| 107 |
|
| 108 |
|
| 109 |
#ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE |
| 110 |
# define FUNC_ATTR_OPT __attribute__((optimize("-O2"))) |
| 111 |
#else |
| 112 |
# define FUNC_ATTR_OPT |
| 113 |
#endif |
| 114 |
|
| 115 |
#define SIMD128_OPT_ATTR FUNC_ATTR_OPT |
| 116 |
|
| 117 |
#define FUNC_ENCRYPT _gcry_aes_vp_aarch64_encrypt |
| 118 |
#define FUNC_DECRYPT _gcry_aes_vp_aarch64_decrypt |
| 119 |
#define FUNC_CFB_ENC _gcry_aes_vp_aarch64_cfb_enc |
| 120 |
#define FUNC_CFB_DEC _gcry_aes_vp_aarch64_cfb_dec |
| 121 |
#define FUNC_CBC_ENC _gcry_aes_vp_aarch64_cbc_enc |
| 122 |
#define FUNC_CBC_DEC _gcry_aes_vp_aarch64_cbc_dec |
| 123 |
#define FUNC_CTR_ENC _gcry_aes_vp_aarch64_ctr_enc |
| 124 |
#define FUNC_CTR32LE_ENC _gcry_aes_vp_aarch64_ctr32le_enc |
| 125 |
#define FUNC_OCB_CRYPT _gcry_aes_vp_aarch64_ocb_crypt |
| 126 |
#define FUNC_OCB_AUTH _gcry_aes_vp_aarch64_ocb_auth |
| 127 |
#define FUNC_ECB_CRYPT _gcry_aes_vp_aarch64_ecb_crypt |
| 128 |
#define FUNC_XTS_CRYPT _gcry_aes_vp_aarch64_xts_crypt |
| 129 |
#define FUNC_SETKEY _gcry_aes_vp_aarch64_do_setkey |
| 130 |
#define FUNC_PREPARE_DEC _gcry_aes_vp_aarch64_prepare_decryption |
| 131 |
|
| 132 |
#include "rijndael-vp-simd128.h" |
| 133 |
|
| 134 |
#endif /* USE_VP_AARCH64 */ |