summaryrefslogtreecommitdiff
path: root/cipher/rijndael-ppc-common.h
blob: fc8ee52694a3f974a7a06de596ea32072a499c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Rijndael (AES) for GnuPG - PowerPC Vector Crypto AES implementation
 * Copyright (C) 2019 Shawn Landden <shawn@git.icu>
 * Copyright (C) 2019-2020 Jussi Kivilinna <jussi.kivilinna@iki.fi>
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Alternatively, this code may be used in OpenSSL from The OpenSSL Project,
 * and Cryptogams by Andy Polyakov, and if made part of a release of either
 * or both projects, is thereafter dual-licensed under the license said project
 * is released under.
 */

#ifndef G10_RIJNDAEL_PPC_COMMON_H
#define G10_RIJNDAEL_PPC_COMMON_H

#include <altivec.h>


typedef vector unsigned char block;
typedef vector unsigned int vec_u32;

typedef union
{
  u32 data32[4];
} __attribute__((packed, aligned(1), may_alias)) u128_t;


#define ALWAYS_INLINE inline __attribute__((always_inline))
#define NO_INLINE __attribute__((noinline))
#define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))

#define ASM_FUNC_ATTR          NO_INSTRUMENT_FUNCTION
#define ASM_FUNC_ATTR_INLINE   ASM_FUNC_ATTR ALWAYS_INLINE
#define ASM_FUNC_ATTR_NOINLINE ASM_FUNC_ATTR NO_INLINE


#define ALIGNED_LOAD(in_ptr, offs) \
  (asm_aligned_ld ((offs) * 16, (const void *)(in_ptr)))

#define ALIGNED_STORE(out_ptr, offs, vec) \
  (asm_aligned_st ((vec), (offs) * 16, (void *)(out_ptr)))

#define VEC_BE_SWAP(vec, bige_const) (asm_be_swap ((vec), (bige_const)))

#define VEC_LOAD_BE(in_ptr, offs, bige_const) \
  (asm_be_swap (asm_load_be_noswap ((offs) * 16, (const void *)(in_ptr)), \
		bige_const))

#define VEC_LOAD_BE_NOSWAP(in_ptr, offs) \
  (asm_load_be_noswap ((offs) * 16, (const unsigned char *)(in_ptr)))

#define VEC_STORE_BE(out_ptr, offs, vec, bige_const) \
  (asm_store_be_noswap (asm_be_swap ((vec), (bige_const)), (offs) * 16, \
		        (void *)(out_ptr)))

#define VEC_STORE_BE_NOSWAP(out_ptr, offs, vec) \
  (asm_store_be_noswap ((vec), (offs) * 16, (void *)(out_ptr)))


#define ROUND_KEY_VARIABLES \
  block rkey0, rkeylast

#define PRELOAD_ROUND_KEYS(nrounds) \
  do { \
    rkey0 = ALIGNED_LOAD (rk, 0); \
    rkeylast = ALIGNED_LOAD (rk, nrounds); \
  } while (0)

#define AES_ENCRYPT(blk, nrounds) \
  do { \
    blk ^= rkey0; \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 1)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 2)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 3)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 4)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 5)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 6)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 7)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 8)); \
    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 9)); \
    if (nrounds >= 12) \
      { \
	blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 10)); \
	blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 11)); \
	if (rounds > 12) \
	  { \
	    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 12)); \
	    blk = asm_cipher_be (blk, ALIGNED_LOAD (rk, 13)); \
	  } \
      } \
    blk = asm_cipherlast_be (blk, rkeylast); \
  } while (0)

#define AES_DECRYPT(blk, nrounds) \
  do { \
    blk ^= rkey0; \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 1)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 2)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 3)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 4)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 5)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 6)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 7)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 8)); \
    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 9)); \
    if (nrounds >= 12) \
      { \
	blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 10)); \
	blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 11)); \
	if (rounds > 12) \
	  { \
	    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 12)); \
	    blk = asm_ncipher_be (blk, ALIGNED_LOAD (rk, 13)); \
	  } \
      } \
    blk = asm_ncipherlast_be (blk, rkeylast); \
  } while (0)


#define ROUND_KEY_VARIABLES_ALL \
  block rkey0, rkey1, rkey2, rkey3, rkey4, rkey5, rkey6, rkey7, rkey8, \
        rkey9, rkey10, rkey11, rkey12, rkey13, rkeylast

#define PRELOAD_ROUND_KEYS_ALL(nrounds) \
  do { \
    rkey0 = ALIGNED_LOAD (rk, 0); \
    rkey1 = ALIGNED_LOAD (rk, 1); \
    rkey2 = ALIGNED_LOAD (rk, 2); \
    rkey3 = ALIGNED_LOAD (rk, 3); \
    rkey4 = ALIGNED_LOAD (rk, 4); \
    rkey5 = ALIGNED_LOAD (rk, 5); \
    rkey6 = ALIGNED_LOAD (rk, 6); \
    rkey7 = ALIGNED_LOAD (rk, 7); \
    rkey8 = ALIGNED_LOAD (rk, 8); \
    rkey9 = ALIGNED_LOAD (rk, 9); \
    if (nrounds >= 12) \
      { \
	rkey10 = ALIGNED_LOAD (rk, 10); \
	rkey11 = ALIGNED_LOAD (rk, 11); \
	if (rounds > 12) \
	  { \
	    rkey12 = ALIGNED_LOAD (rk, 12); \
	    rkey13 = ALIGNED_LOAD (rk, 13); \
	  } \
      } \
    rkeylast = ALIGNED_LOAD (rk, nrounds); \
  } while (0)


static ASM_FUNC_ATTR_INLINE block
asm_aligned_ld(unsigned long offset, const void *ptr)
{
  block vec;
#if __GNUC__ >= 4
  if (__builtin_constant_p (offset) && offset == 0)
    __asm__ volatile ("lvx %0,0,%1\n\t"
		      : "=v" (vec)
		      : "r" ((uintptr_t)ptr)
		      : "memory");
  else
#endif
    __asm__ volatile ("lvx %0,%1,%2\n\t"
		      : "=v" (vec)
		      : "r" (offset), "r" ((uintptr_t)ptr)
		      : "memory", "r0");
  return vec;
}

static ASM_FUNC_ATTR_INLINE void
asm_aligned_st(block vec, unsigned long offset, void *ptr)
{
#if __GNUC__ >= 4
  if (__builtin_constant_p (offset) && offset == 0)
    __asm__ volatile ("stvx %0,0,%1\n\t"
		      :
		      : "v" (vec), "r" ((uintptr_t)ptr)
		      : "memory");
  else
#endif
    __asm__ volatile ("stvx %0,%1,%2\n\t"
		      :
		      : "v" (vec), "r" (offset), "r" ((uintptr_t)ptr)
		      : "memory", "r0");
}

static ASM_FUNC_ATTR_INLINE block
asm_vperm1(block vec, block mask)
{
  block o;
  __asm__ volatile ("vperm %0,%1,%1,%2\n\t"
		    : "=v" (o)
		    : "v" (vec), "v" (mask));
  return o;
}

static ASM_FUNC_ATTR_INLINE block
asm_add_uint128(block a, block b)
{
  block res;
  __asm__ volatile ("vadduqm %0,%1,%2\n\t"
		    : "=v" (res)
		    : "v" (a), "v" (b));
  return res;
}

static ASM_FUNC_ATTR_INLINE block
asm_add_uint64(block a, block b)
{
  block res;
  __asm__ volatile ("vaddudm %0,%1,%2\n\t"
		    : "=v" (res)
		    : "v" (a), "v" (b));
  return res;
}

static ASM_FUNC_ATTR_INLINE block
asm_sra_int64(block a, block b)
{
  block res;
  __asm__ volatile ("vsrad %0,%1,%2\n\t"
		    : "=v" (res)
		    : "v" (a), "v" (b));
  return res;
}

static block
asm_swap_uint64_halfs(block a)
{
  block res;
  __asm__ volatile ("xxswapd %x0, %x1"
		    : "=wa" (res)
		    : "wa" (a));
  return res;
}

static ASM_FUNC_ATTR_INLINE block
asm_xor(block a, block b)
{
  block res;
  __asm__ volatile ("vxor %0,%1,%2\n\t"
		    : "=v" (res)
		    : "v" (a), "v" (b));
  return res;
}

static ASM_FUNC_ATTR_INLINE block
asm_sbox_be(block b)
{
  block o;
  __asm__ volatile ("vsbox %0, %1\n\t"
		    : "=v" (o)
		    : "v" (b));
  return o;
}

static ASM_FUNC_ATTR_INLINE block
asm_cipher_be(block b, block rk)
{
  block o;
  __asm__ volatile ("vcipher %0, %1, %2\n\t"
		    : "=v" (o)
		    : "v" (b), "v" (rk));
  return o;
}

static ASM_FUNC_ATTR_INLINE block
asm_cipherlast_be(block b, block rk)
{
  block o;
  __asm__ volatile ("vcipherlast %0, %1, %2\n\t"
		    : "=v" (o)
		    : "v" (b), "v" (rk));
  return o;
}

static ASM_FUNC_ATTR_INLINE block
asm_ncipher_be(block b, block rk)
{
  block o;
  __asm__ volatile ("vncipher %0, %1, %2\n\t"
		    : "=v" (o)
		    : "v" (b), "v" (rk));
  return o;
}

static ASM_FUNC_ATTR_INLINE block
asm_ncipherlast_be(block b, block rk)
{
  block o;
  __asm__ volatile ("vncipherlast %0, %1, %2\n\t"
		    : "=v" (o)
		    : "v" (b), "v" (rk));
  return o;
}


/* Make a decryption key from an encryption key. */
static ASM_FUNC_ATTR_INLINE void
internal_aes_ppc_prepare_decryption (RIJNDAEL_context *ctx)
{
  u128_t *ekey = (u128_t *)(void *)ctx->keyschenc;
  u128_t *dkey = (u128_t *)(void *)ctx->keyschdec;
  int rounds = ctx->rounds;
  int rr;
  int r;

  r = 0;
  rr = rounds;
  for (r = 0, rr = rounds; r <= rounds; r++, rr--)
    {
      ALIGNED_STORE (dkey, r, ALIGNED_LOAD (ekey, rr));
    }
}

#endif /* G10_RIJNDAEL_PPC_COMMON_H */