Debian Codegraph @

cipher/rijndael-ppc9le.c

raw
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 #include <config.h>
27
28 #include "rijndael-internal.h"
29 #include "cipher-internal.h"
30 #include "bufhelp.h"
31
32 #ifdef USE_PPC_CRYPTO_WITH_PPC9LE
33
34 #include "rijndael-ppc-common.h"
35
36
37 #ifdef HAVE_GCC_ATTRIBUTE_OPTIMIZE
38 # define FUNC_ATTR_OPT __attribute__((optimize("-O2")))
39 #else
40 # define FUNC_ATTR_OPT
41 #endif
42
43 #if defined(__clang__) && defined(HAVE_CLANG_ATTRIBUTE_PPC_TARGET)
44 # define PPC_OPT_ATTR __attribute__((target("arch=pwr9"))) FUNC_ATTR_OPT
45 #elif defined(HAVE_GCC_ATTRIBUTE_PPC_TARGET)
46 # define PPC_OPT_ATTR __attribute__((target("cpu=power9"))) FUNC_ATTR_OPT
47 #else
48 # define PPC_OPT_ATTR FUNC_ATTR_OPT
49 #endif
50
51
52 static ASM_FUNC_ATTR_INLINE block
53 asm_load_be_const(void)
54 {
55 static const block vec_dummy = { 0 };
56 return vec_dummy;
57 }
58
59 static ASM_FUNC_ATTR_INLINE block
60 asm_be_swap(block vec, block be_bswap_const)
61 {
62 (void)be_bswap_const;
63 return vec;
64 }
65
66 static ASM_FUNC_ATTR_INLINE block
67 asm_load_be_noswap(unsigned long offset, const void *ptr)
68 {
69 block vec;
70 #if __GNUC__ >= 4
71 if (__builtin_constant_p (offset) && offset == 0)
72 __asm__ volatile ("lxvb16x %x0,0,%1\n\t"
73 : "=wa" (vec)
74 : "r" ((uintptr_t)ptr)
75 : "memory");
76 else
77 #endif
78 __asm__ volatile ("lxvb16x %x0,%1,%2\n\t"
79 : "=wa" (vec)
80 : "r" (offset), "r" ((uintptr_t)ptr)
81 : "memory", "r0");
82 return vec;
83 }
84
85 static ASM_FUNC_ATTR_INLINE void
86 asm_store_be_noswap(block vec, unsigned long offset, void *ptr)
87 {
88 #if __GNUC__ >= 4
89 if (__builtin_constant_p (offset) && offset == 0)
90 __asm__ volatile ("stxvb16x %x0,0,%1\n\t"
91 :
92 : "wa" (vec), "r" ((uintptr_t)ptr)
93 : "memory");
94 else
95 #endif
96 __asm__ volatile ("stxvb16x %x0,%1,%2\n\t"
97 :
98 : "wa" (vec), "r" (offset), "r" ((uintptr_t)ptr)
99 : "memory", "r0");
100 }
101
102
103 #define GCRY_AES_PPC9LE 1
104 #define ENCRYPT_BLOCK_FUNC _gcry_aes_ppc9le_encrypt
105 #define DECRYPT_BLOCK_FUNC _gcry_aes_ppc9le_decrypt
106 #define ECB_CRYPT_FUNC _gcry_aes_ppc9le_ecb_crypt
107 #define CFB_ENC_FUNC _gcry_aes_ppc9le_cfb_enc
108 #define CFB_DEC_FUNC _gcry_aes_ppc9le_cfb_dec
109 #define CBC_ENC_FUNC _gcry_aes_ppc9le_cbc_enc
110 #define CBC_DEC_FUNC _gcry_aes_ppc9le_cbc_dec
111 #define CTR_ENC_FUNC _gcry_aes_ppc9le_ctr_enc
112 #define OCB_CRYPT_FUNC _gcry_aes_ppc9le_ocb_crypt
113 #define OCB_AUTH_FUNC _gcry_aes_ppc9le_ocb_auth
114 #define XTS_CRYPT_FUNC _gcry_aes_ppc9le_xts_crypt
115 #define CTR32LE_ENC_FUNC _gcry_aes_ppc9le_ctr32le_enc
116
117 #include <rijndael-ppc-functions.h>
118
119 #endif /* USE_PPC_CRYPTO */