Debian Codegraph @

cipher/rijndael-ppc.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, 2022 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
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=pwr8"))) FUNC_ATTR_OPT
45 #elif defined(HAVE_GCC_ATTRIBUTE_PPC_TARGET)
46 # define PPC_OPT_ATTR __attribute__((target("cpu=power8"))) FUNC_ATTR_OPT
47 #else
48 # define PPC_OPT_ATTR FUNC_ATTR_OPT
49 #endif
50
51
52 #ifndef WORDS_BIGENDIAN
53 static const block vec_bswap32_const_neg =
54 { ~3, ~2, ~1, ~0, ~7, ~6, ~5, ~4, ~11, ~10, ~9, ~8, ~15, ~14, ~13, ~12 };
55 #endif
56
57
58 static ASM_FUNC_ATTR_INLINE block
59 asm_load_be_const(void)
60 {
61 #ifndef WORDS_BIGENDIAN
62 return ALIGNED_LOAD (&vec_bswap32_const_neg, 0);
63 #else
64 static const block vec_dummy = { 0 };
65 return vec_dummy;
66 #endif
67 }
68
69 static ASM_FUNC_ATTR_INLINE block
70 asm_be_swap(block vec, block be_bswap_const)
71 {
72 (void)be_bswap_const;
73 #ifndef WORDS_BIGENDIAN
74 return asm_vperm1 (vec, be_bswap_const);
75 #else
76 return vec;
77 #endif
78 }
79
80 static ASM_FUNC_ATTR_INLINE block
81 asm_load_be_noswap(unsigned long offset, const void *ptr)
82 {
83 block vec;
84 #if __GNUC__ >= 4
85 if (__builtin_constant_p (offset) && offset == 0)
86 __asm__ volatile ("lxvw4x %x0,0,%1\n\t"
87 : "=wa" (vec)
88 : "r" ((uintptr_t)ptr)
89 : "memory");
90 else
91 #endif
92 __asm__ volatile ("lxvw4x %x0,%1,%2\n\t"
93 : "=wa" (vec)
94 : "r" (offset), "r" ((uintptr_t)ptr)
95 : "memory", "r0");
96 /* NOTE: vec needs to be be-swapped using 'asm_be_swap' by caller */
97 return vec;
98 }
99
100 static ASM_FUNC_ATTR_INLINE void
101 asm_store_be_noswap(block vec, unsigned long offset, void *ptr)
102 {
103 /* NOTE: vec be-swapped using 'asm_be_swap' by caller */
104 #if __GNUC__ >= 4
105 if (__builtin_constant_p (offset) && offset == 0)
106 __asm__ volatile ("stxvw4x %x0,0,%1\n\t"
107 :
108 : "wa" (vec), "r" ((uintptr_t)ptr)
109 : "memory");
110 else
111 #endif
112 __asm__ volatile ("stxvw4x %x0,%1,%2\n\t"
113 :
114 : "wa" (vec), "r" (offset), "r" ((uintptr_t)ptr)
115 : "memory", "r0");
116 }
117
118
119 static ASM_FUNC_ATTR_INLINE unsigned int
120 keysched_idx(unsigned int in)
121 {
122 #ifdef WORDS_BIGENDIAN
123 return in;
124 #else
125 return (in & ~3U) | (3U - (in & 3U));
126 #endif
127 }
128
129
130 static ASM_FUNC_ATTR_INLINE vec_u32
131 bcast_u32_to_vec(u32 x)
132 {
133 vec_u32 v = { x, x, x, x };
134 return v;
135 }
136
137
138 static ASM_FUNC_ATTR_INLINE u32
139 u32_from_vec(vec_u32 x)
140 {
141 #ifdef WORDS_BIGENDIAN
142 return x[1];
143 #else
144 return x[2];
145 #endif
146 }
147
148
149 void PPC_OPT_ATTR
150 _gcry_aes_ppc8_setkey (RIJNDAEL_context *ctx, const byte *key)
151 {
152 static const vec_u32 rotate24 = { 24, 24, 24, 24 };
153 static const vec_u32 rcon_const = { 0x1b, 0x1b, 0x1b, 0x1b };
154 vec_u32 tk_vu32[MAXKC];
155 unsigned int rounds = ctx->rounds;
156 unsigned int KC = rounds - 6;
157 u32 *W_u32 = ctx->keyschenc32b;
158 unsigned int i, j;
159 vec_u32 tk_prev;
160 vec_u32 rcon = { 1, 1, 1, 1 };
161
162 for (i = 0; i < KC; i += 2)
163 {
164 unsigned int idx0 = keysched_idx(i + 0);
165 unsigned int idx1 = keysched_idx(i + 1);
166 tk_vu32[i + 0] = bcast_u32_to_vec(buf_get_le32(key + i * 4 + 0));
167 tk_vu32[i + 1] = bcast_u32_to_vec(buf_get_le32(key + i * 4 + 4));
168 W_u32[idx0] = u32_from_vec(vec_revb(tk_vu32[i + 0]));
169 W_u32[idx1] = u32_from_vec(vec_revb(tk_vu32[i + 1]));
170 }
171
172 for (i = KC, j = KC, tk_prev = tk_vu32[KC - 1];
173 i < 4 * (rounds + 1);
174 i += 2, j += 2)
175 {
176 unsigned int idx0 = keysched_idx(i + 0);
177 unsigned int idx1 = keysched_idx(i + 1);
178 vec_u32 temp0 = tk_prev;
179 vec_u32 temp1;
180
181 if (j == KC)
182 {
183 j = 0;
184 temp0 = (vec_u32)(asm_sbox_be((block)vec_rl(temp0, rotate24))) ^ rcon;
185 rcon = (vec_u32)(((block)rcon << 1)
186 ^ (-((block)rcon >> 7) & (block)rcon_const));
187 }
188 else if (KC == 8 && j == 4)
189 {
190 temp0 = (vec_u32)asm_sbox_be((block)temp0);
191 }
192
193 temp1 = tk_vu32[j + 0];
194
195 tk_vu32[j + 0] = temp0 ^ temp1;
196 tk_vu32[j + 1] ^= temp0 ^ temp1;
197 tk_prev = tk_vu32[j + 1];
198
199 W_u32[idx0] = u32_from_vec(vec_revb(tk_vu32[j + 0]));
200 W_u32[idx1] = u32_from_vec(vec_revb(tk_vu32[j + 1]));
201 }
202
203 wipememory(tk_vu32, sizeof(tk_vu32));
204
205 clear_vec_regs();
206 }
207
208
209 void PPC_OPT_ATTR
210 _gcry_aes_ppc8_prepare_decryption (RIJNDAEL_context *ctx)
211 {
212 internal_aes_ppc_prepare_decryption (ctx);
213
214 clear_vec_regs();
215 }
216
217
218 #define GCRY_AES_PPC8 1
219 #define ENCRYPT_BLOCK_FUNC _gcry_aes_ppc8_encrypt
220 #define DECRYPT_BLOCK_FUNC _gcry_aes_ppc8_decrypt
221 #define ECB_CRYPT_FUNC _gcry_aes_ppc8_ecb_crypt
222 #define CFB_ENC_FUNC _gcry_aes_ppc8_cfb_enc
223 #define CFB_DEC_FUNC _gcry_aes_ppc8_cfb_dec
224 #define CBC_ENC_FUNC _gcry_aes_ppc8_cbc_enc
225 #define CBC_DEC_FUNC _gcry_aes_ppc8_cbc_dec
226 #define CTR_ENC_FUNC _gcry_aes_ppc8_ctr_enc
227 #define OCB_CRYPT_FUNC _gcry_aes_ppc8_ocb_crypt
228 #define OCB_AUTH_FUNC _gcry_aes_ppc8_ocb_auth
229 #define XTS_CRYPT_FUNC _gcry_aes_ppc8_xts_crypt
230 #define CTR32LE_ENC_FUNC _gcry_aes_ppc8_ctr32le_enc
231
232 #include <rijndael-ppc-functions.h>
233
234 #endif /* USE_PPC_CRYPTO */