Debian Codegraph @

cipher/rijndael-ppc-functions.h

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-2023 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 unsigned int PPC_OPT_ATTR
27 ENCRYPT_BLOCK_FUNC (const RIJNDAEL_context *ctx, unsigned char *out,
28 const unsigned char *in)
29 {
30 const block bige_const = asm_load_be_const();
31 const u128_t *rk = (u128_t *)&ctx->keyschenc;
32 int rounds = ctx->rounds;
33 ROUND_KEY_VARIABLES;
34 block b;
35
36 b = VEC_LOAD_BE (in, 0, bige_const);
37
38 PRELOAD_ROUND_KEYS (rounds);
39
40 AES_ENCRYPT (b, rounds);
41 VEC_STORE_BE (out, 0, b, bige_const);
42
43 clear_vec_regs();
44
45 return 0; /* does not use stack */
46 }
47
48
49 unsigned int PPC_OPT_ATTR
50 DECRYPT_BLOCK_FUNC (const RIJNDAEL_context *ctx, unsigned char *out,
51 const unsigned char *in)
52 {
53 const block bige_const = asm_load_be_const();
54 const u128_t *rk = (u128_t *)&ctx->keyschdec;
55 int rounds = ctx->rounds;
56 ROUND_KEY_VARIABLES;
57 block b;
58
59 b = VEC_LOAD_BE (in, 0, bige_const);
60
61 PRELOAD_ROUND_KEYS (rounds);
62
63 AES_DECRYPT (b, rounds);
64 VEC_STORE_BE (out, 0, b, bige_const);
65
66 clear_vec_regs();
67
68 return 0; /* does not use stack */
69 }
70
71
72 void PPC_OPT_ATTR
73 CFB_ENC_FUNC (void *context, unsigned char *iv_arg, void *outbuf_arg,
74 const void *inbuf_arg, size_t nblocks)
75 {
76 const block bige_const = asm_load_be_const();
77 RIJNDAEL_context *ctx = context;
78 const u128_t *rk = (u128_t *)&ctx->keyschenc;
79 const u128_t *in = (const u128_t *)inbuf_arg;
80 u128_t *out = (u128_t *)outbuf_arg;
81 int rounds = ctx->rounds;
82 ROUND_KEY_VARIABLES_ALL;
83 block key0_xor_keylast;
84 block iv, outiv;
85
86 iv = VEC_LOAD_BE (iv_arg, 0, bige_const);
87 outiv = iv;
88
89 PRELOAD_ROUND_KEYS_ALL (rounds);
90 key0_xor_keylast = rkey0 ^ rkeylast;
91 iv ^= rkey0;
92
93 for (; nblocks; nblocks--)
94 {
95 rkeylast = key0_xor_keylast ^ VEC_LOAD_BE (in++, 0, bige_const);
96
97 iv = asm_cipher_be (iv, rkey1);
98 iv = asm_cipher_be (iv, rkey2);
99 iv = asm_cipher_be (iv, rkey3);
100 iv = asm_cipher_be (iv, rkey4);
101 iv = asm_cipher_be (iv, rkey5);
102 iv = asm_cipher_be (iv, rkey6);
103 iv = asm_cipher_be (iv, rkey7);
104 iv = asm_cipher_be (iv, rkey8);
105 iv = asm_cipher_be (iv, rkey9);
106 if (rounds >= 12)
107 {
108 iv = asm_cipher_be (iv, rkey10);
109 iv = asm_cipher_be (iv, rkey11);
110 if (rounds > 12)
111 {
112 iv = asm_cipher_be (iv, rkey12);
113 iv = asm_cipher_be (iv, rkey13);
114 }
115 }
116 iv = asm_cipherlast_be (iv, rkeylast);
117
118 outiv = rkey0 ^ iv;
119 VEC_STORE_BE (out++, 0, outiv, bige_const);
120 }
121
122 VEC_STORE_BE (iv_arg, 0, outiv, bige_const);
123
124 clear_vec_regs();
125 }
126
127
128 void PPC_OPT_ATTR
129 ECB_CRYPT_FUNC (void *context, void *outbuf_arg, const void *inbuf_arg,
130 size_t nblocks, int encrypt)
131 {
132 const block bige_const = asm_load_be_const();
133 RIJNDAEL_context *ctx = context;
134 const u128_t *rk = encrypt ? (u128_t *)&ctx->keyschenc
135 : (u128_t *)&ctx->keyschdec;
136 const u128_t *in = (const u128_t *)inbuf_arg;
137 u128_t *out = (u128_t *)outbuf_arg;
138 int rounds = ctx->rounds;
139 ROUND_KEY_VARIABLES;
140 block b0, b1, b2, b3, b4, b5, b6, b7;
141 block rkey;
142
143 if (!encrypt && !ctx->decryption_prepared)
144 {
145 internal_aes_ppc_prepare_decryption (ctx);
146 ctx->decryption_prepared = 1;
147 }
148
149 PRELOAD_ROUND_KEYS (rounds);
150
151 for (; nblocks >= 8; nblocks -= 8)
152 {
153 b0 = VEC_LOAD_BE (in, 0, bige_const);
154 b1 = VEC_LOAD_BE (in, 1, bige_const);
155 b2 = VEC_LOAD_BE (in, 2, bige_const);
156 b3 = VEC_LOAD_BE (in, 3, bige_const);
157 b0 = asm_xor (rkey0, b0);
158 b1 = asm_xor (rkey0, b1);
159 b4 = VEC_LOAD_BE (in, 4, bige_const);
160 b5 = VEC_LOAD_BE (in, 5, bige_const);
161 b2 = asm_xor (rkey0, b2);
162 b3 = asm_xor (rkey0, b3);
163 b6 = VEC_LOAD_BE (in, 6, bige_const);
164 b7 = VEC_LOAD_BE (in, 7, bige_const);
165 in += 8;
166 b4 = asm_xor (rkey0, b4);
167 b5 = asm_xor (rkey0, b5);
168 b6 = asm_xor (rkey0, b6);
169 b7 = asm_xor (rkey0, b7);
170
171 if (encrypt)
172 {
173 #define DO_ROUND(r) \
174 rkey = ALIGNED_LOAD (rk, r); \
175 b0 = asm_cipher_be (b0, rkey); \
176 b1 = asm_cipher_be (b1, rkey); \
177 b2 = asm_cipher_be (b2, rkey); \
178 b3 = asm_cipher_be (b3, rkey); \
179 b4 = asm_cipher_be (b4, rkey); \
180 b5 = asm_cipher_be (b5, rkey); \
181 b6 = asm_cipher_be (b6, rkey); \
182 b7 = asm_cipher_be (b7, rkey);
183
184 DO_ROUND(1);
185 DO_ROUND(2);
186 DO_ROUND(3);
187 DO_ROUND(4);
188 DO_ROUND(5);
189 DO_ROUND(6);
190 DO_ROUND(7);
191 DO_ROUND(8);
192 DO_ROUND(9);
193 if (rounds >= 12)
194 {
195 DO_ROUND(10);
196 DO_ROUND(11);
197 if (rounds > 12)
198 {
199 DO_ROUND(12);
200 DO_ROUND(13);
201 }
202 }
203
204 #undef DO_ROUND
205
206 b0 = asm_cipherlast_be (b0, rkeylast);
207 b1 = asm_cipherlast_be (b1, rkeylast);
208 b2 = asm_cipherlast_be (b2, rkeylast);
209 b3 = asm_cipherlast_be (b3, rkeylast);
210 b4 = asm_cipherlast_be (b4, rkeylast);
211 b5 = asm_cipherlast_be (b5, rkeylast);
212 b6 = asm_cipherlast_be (b6, rkeylast);
213 b7 = asm_cipherlast_be (b7, rkeylast);
214 }
215 else
216 {
217 #define DO_ROUND(r) \
218 rkey = ALIGNED_LOAD (rk, r); \
219 b0 = asm_ncipher_be (b0, rkey); \
220 b1 = asm_ncipher_be (b1, rkey); \
221 b2 = asm_ncipher_be (b2, rkey); \
222 b3 = asm_ncipher_be (b3, rkey); \
223 b4 = asm_ncipher_be (b4, rkey); \
224 b5 = asm_ncipher_be (b5, rkey); \
225 b6 = asm_ncipher_be (b6, rkey); \
226 b7 = asm_ncipher_be (b7, rkey);
227
228 DO_ROUND(1);
229 DO_ROUND(2);
230 DO_ROUND(3);
231 DO_ROUND(4);
232 DO_ROUND(5);
233 DO_ROUND(6);
234 DO_ROUND(7);
235 DO_ROUND(8);
236 DO_ROUND(9);
237 if (rounds >= 12)
238 {
239 DO_ROUND(10);
240 DO_ROUND(11);
241 if (rounds > 12)
242 {
243 DO_ROUND(12);
244 DO_ROUND(13);
245 }
246 }
247
248 #undef DO_ROUND
249
250 b0 = asm_ncipherlast_be (b0, rkeylast);
251 b1 = asm_ncipherlast_be (b1, rkeylast);
252 b2 = asm_ncipherlast_be (b2, rkeylast);
253 b3 = asm_ncipherlast_be (b3, rkeylast);
254 b4 = asm_ncipherlast_be (b4, rkeylast);
255 b5 = asm_ncipherlast_be (b5, rkeylast);
256 b6 = asm_ncipherlast_be (b6, rkeylast);
257 b7 = asm_ncipherlast_be (b7, rkeylast);
258 }
259
260 VEC_STORE_BE (out, 0, b0, bige_const);
261 VEC_STORE_BE (out, 1, b1, bige_const);
262 VEC_STORE_BE (out, 2, b2, bige_const);
263 VEC_STORE_BE (out, 3, b3, bige_const);
264 VEC_STORE_BE (out, 4, b4, bige_const);
265 VEC_STORE_BE (out, 5, b5, bige_const);
266 VEC_STORE_BE (out, 6, b6, bige_const);
267 VEC_STORE_BE (out, 7, b7, bige_const);
268 out += 8;
269 }
270
271 if (nblocks >= 4)
272 {
273 b0 = VEC_LOAD_BE (in, 0, bige_const);
274 b1 = VEC_LOAD_BE (in, 1, bige_const);
275 b2 = VEC_LOAD_BE (in, 2, bige_const);
276 b3 = VEC_LOAD_BE (in, 3, bige_const);
277
278 b0 = asm_xor (rkey0, b0);
279 b1 = asm_xor (rkey0, b1);
280 b2 = asm_xor (rkey0, b2);
281 b3 = asm_xor (rkey0, b3);
282
283 if (encrypt)
284 {
285 #define DO_ROUND(r) \
286 rkey = ALIGNED_LOAD (rk, r); \
287 b0 = asm_cipher_be (b0, rkey); \
288 b1 = asm_cipher_be (b1, rkey); \
289 b2 = asm_cipher_be (b2, rkey); \
290 b3 = asm_cipher_be (b3, rkey);
291
292 DO_ROUND(1);
293 DO_ROUND(2);
294 DO_ROUND(3);
295 DO_ROUND(4);
296 DO_ROUND(5);
297 DO_ROUND(6);
298 DO_ROUND(7);
299 DO_ROUND(8);
300 DO_ROUND(9);
301 if (rounds >= 12)
302 {
303 DO_ROUND(10);
304 DO_ROUND(11);
305 if (rounds > 12)
306 {
307 DO_ROUND(12);
308 DO_ROUND(13);
309 }
310 }
311 #undef DO_ROUND
312
313 b0 = asm_cipherlast_be (b0, rkeylast);
314 b1 = asm_cipherlast_be (b1, rkeylast);
315 b2 = asm_cipherlast_be (b2, rkeylast);
316 b3 = asm_cipherlast_be (b3, rkeylast);
317 }
318 else
319 {
320 #define DO_ROUND(r) \
321 rkey = ALIGNED_LOAD (rk, r); \
322 b0 = asm_ncipher_be (b0, rkey); \
323 b1 = asm_ncipher_be (b1, rkey); \
324 b2 = asm_ncipher_be (b2, rkey); \
325 b3 = asm_ncipher_be (b3, rkey);
326
327 DO_ROUND(1);
328 DO_ROUND(2);
329 DO_ROUND(3);
330 DO_ROUND(4);
331 DO_ROUND(5);
332 DO_ROUND(6);
333 DO_ROUND(7);
334 DO_ROUND(8);
335 DO_ROUND(9);
336 if (rounds >= 12)
337 {
338 DO_ROUND(10);
339 DO_ROUND(11);
340 if (rounds > 12)
341 {
342 DO_ROUND(12);
343 DO_ROUND(13);
344 }
345 }
346 #undef DO_ROUND
347
348 b0 = asm_ncipherlast_be (b0, rkeylast);
349 b1 = asm_ncipherlast_be (b1, rkeylast);
350 b2 = asm_ncipherlast_be (b2, rkeylast);
351 b3 = asm_ncipherlast_be (b3, rkeylast);
352 }
353
354 VEC_STORE_BE (out, 0, b0, bige_const);
355 VEC_STORE_BE (out, 1, b1, bige_const);
356 VEC_STORE_BE (out, 2, b2, bige_const);
357 VEC_STORE_BE (out, 3, b3, bige_const);
358
359 in += 4;
360 out += 4;
361 nblocks -= 4;
362 }
363
364 for (; nblocks; nblocks--)
365 {
366 b0 = VEC_LOAD_BE (in, 0, bige_const);
367
368 if (encrypt)
369 {
370 AES_ENCRYPT (b0, rounds);
371 }
372 else
373 {
374 AES_DECRYPT (b0, rounds);
375 }
376
377 VEC_STORE_BE (out, 0, b0, bige_const);
378
379 out++;
380 in++;
381 }
382
383 clear_vec_regs();
384 }
385
386
387 void PPC_OPT_ATTR
388 CFB_DEC_FUNC (void *context, unsigned char *iv_arg, void *outbuf_arg,
389 const void *inbuf_arg, size_t nblocks)
390 {
391 const block bige_const = asm_load_be_const();
392 RIJNDAEL_context *ctx = context;
393 const u128_t *rk = (u128_t *)&ctx->keyschenc;
394 const u128_t *in = (const u128_t *)inbuf_arg;
395 u128_t *out = (u128_t *)outbuf_arg;
396 int rounds = ctx->rounds;
397 ROUND_KEY_VARIABLES;
398 block rkeylast_orig;
399 block iv, b, bin;
400 block in0, in1, in2, in3, in4, in5, in6, in7;
401 block b0, b1, b2, b3, b4, b5, b6, b7;
402 block rkey;
403
404 iv = VEC_LOAD_BE (iv_arg, 0, bige_const);
405
406 PRELOAD_ROUND_KEYS (rounds);
407 rkeylast_orig = rkeylast;
408
409 for (; nblocks >= 8; nblocks -= 8)
410 {
411 in0 = iv;
412 in1 = VEC_LOAD_BE_NOSWAP (in, 0);
413 in2 = VEC_LOAD_BE_NOSWAP (in, 1);
414 in3 = VEC_LOAD_BE_NOSWAP (in, 2);
415 in4 = VEC_LOAD_BE_NOSWAP (in, 3);
416 in1 = VEC_BE_SWAP (in1, bige_const);
417 in2 = VEC_BE_SWAP (in2, bige_const);
418 in5 = VEC_LOAD_BE_NOSWAP (in, 4);
419 in6 = VEC_LOAD_BE_NOSWAP (in, 5);
420 in3 = VEC_BE_SWAP (in3, bige_const);
421 in4 = VEC_BE_SWAP (in4, bige_const);
422 in7 = VEC_LOAD_BE_NOSWAP (in, 6);
423 iv = VEC_LOAD_BE_NOSWAP (in, 7);
424 in += 8;
425 in5 = VEC_BE_SWAP (in5, bige_const);
426 in6 = VEC_BE_SWAP (in6, bige_const);
427 b0 = asm_xor (rkey0, in0);
428 b1 = asm_xor (rkey0, in1);
429 in7 = VEC_BE_SWAP (in7, bige_const);
430 iv = VEC_BE_SWAP (iv, bige_const);
431 b2 = asm_xor (rkey0, in2);
432 b3 = asm_xor (rkey0, in3);
433 b4 = asm_xor (rkey0, in4);
434 b5 = asm_xor (rkey0, in5);
435 b6 = asm_xor (rkey0, in6);
436 b7 = asm_xor (rkey0, in7);
437
438 #define DO_ROUND(r) \
439 rkey = ALIGNED_LOAD (rk, r); \
440 b0 = asm_cipher_be (b0, rkey); \
441 b1 = asm_cipher_be (b1, rkey); \
442 b2 = asm_cipher_be (b2, rkey); \
443 b3 = asm_cipher_be (b3, rkey); \
444 b4 = asm_cipher_be (b4, rkey); \
445 b5 = asm_cipher_be (b5, rkey); \
446 b6 = asm_cipher_be (b6, rkey); \
447 b7 = asm_cipher_be (b7, rkey);
448
449 DO_ROUND(1);
450 DO_ROUND(2);
451 DO_ROUND(3);
452 DO_ROUND(4);
453 DO_ROUND(5);
454 DO_ROUND(6);
455 DO_ROUND(7);
456 DO_ROUND(8);
457 DO_ROUND(9);
458 if (rounds >= 12)
459 {
460 DO_ROUND(10);
461 DO_ROUND(11);
462 if (rounds > 12)
463 {
464 DO_ROUND(12);
465 DO_ROUND(13);
466 }
467 }
468
469 #undef DO_ROUND
470
471 in1 = asm_xor (rkeylast, in1);
472 in2 = asm_xor (rkeylast, in2);
473 in3 = asm_xor (rkeylast, in3);
474 in4 = asm_xor (rkeylast, in4);
475 b0 = asm_cipherlast_be (b0, in1);
476 b1 = asm_cipherlast_be (b1, in2);
477 in5 = asm_xor (rkeylast, in5);
478 in6 = asm_xor (rkeylast, in6);
479 b2 = asm_cipherlast_be (b2, in3);
480 b3 = asm_cipherlast_be (b3, in4);
481 in7 = asm_xor (rkeylast, in7);
482 in0 = asm_xor (rkeylast, iv);
483 b0 = VEC_BE_SWAP (b0, bige_const);
484 b1 = VEC_BE_SWAP (b1, bige_const);
485 b4 = asm_cipherlast_be (b4, in5);
486 b5 = asm_cipherlast_be (b5, in6);
487 b2 = VEC_BE_SWAP (b2, bige_const);
488 b3 = VEC_BE_SWAP (b3, bige_const);
489 b6 = asm_cipherlast_be (b6, in7);
490 b7 = asm_cipherlast_be (b7, in0);
491 b4 = VEC_BE_SWAP (b4, bige_const);
492 b5 = VEC_BE_SWAP (b5, bige_const);
493 b6 = VEC_BE_SWAP (b6, bige_const);
494 b7 = VEC_BE_SWAP (b7, bige_const);
495 VEC_STORE_BE_NOSWAP (out, 0, b0);
496 VEC_STORE_BE_NOSWAP (out, 1, b1);
497 VEC_STORE_BE_NOSWAP (out, 2, b2);
498 VEC_STORE_BE_NOSWAP (out, 3, b3);
499 VEC_STORE_BE_NOSWAP (out, 4, b4);
500 VEC_STORE_BE_NOSWAP (out, 5, b5);
501 VEC_STORE_BE_NOSWAP (out, 6, b6);
502 VEC_STORE_BE_NOSWAP (out, 7, b7);
503 out += 8;
504 }
505
506 if (nblocks >= 4)
507 {
508 in0 = iv;
509 in1 = VEC_LOAD_BE (in, 0, bige_const);
510 in2 = VEC_LOAD_BE (in, 1, bige_const);
511 in3 = VEC_LOAD_BE (in, 2, bige_const);
512 iv = VEC_LOAD_BE (in, 3, bige_const);
513
514 b0 = asm_xor (rkey0, in0);
515 b1 = asm_xor (rkey0, in1);
516 b2 = asm_xor (rkey0, in2);
517 b3 = asm_xor (rkey0, in3);
518
519 #define DO_ROUND(r) \
520 rkey = ALIGNED_LOAD (rk, r); \
521 b0 = asm_cipher_be (b0, rkey); \
522 b1 = asm_cipher_be (b1, rkey); \
523 b2 = asm_cipher_be (b2, rkey); \
524 b3 = asm_cipher_be (b3, rkey);
525
526 DO_ROUND(1);
527 DO_ROUND(2);
528 DO_ROUND(3);
529 DO_ROUND(4);
530 DO_ROUND(5);
531 DO_ROUND(6);
532 DO_ROUND(7);
533 DO_ROUND(8);
534 DO_ROUND(9);
535 if (rounds >= 12)
536 {
537 DO_ROUND(10);
538 DO_ROUND(11);
539 if (rounds > 12)
540 {
541 DO_ROUND(12);
542 DO_ROUND(13);
543 }
544 }
545
546 #undef DO_ROUND
547
548 in1 = asm_xor (rkeylast, in1);
549 in2 = asm_xor (rkeylast, in2);
550 in3 = asm_xor (rkeylast, in3);
551 in0 = asm_xor (rkeylast, iv);
552 b0 = asm_cipherlast_be (b0, in1);
553 b1 = asm_cipherlast_be (b1, in2);
554 b2 = asm_cipherlast_be (b2, in3);
555 b3 = asm_cipherlast_be (b3, in0);
556 VEC_STORE_BE (out, 0, b0, bige_const);
557 VEC_STORE_BE (out, 1, b1, bige_const);
558 VEC_STORE_BE (out, 2, b2, bige_const);
559 VEC_STORE_BE (out, 3, b3, bige_const);
560
561 in += 4;
562 out += 4;
563 nblocks -= 4;
564 }
565
566 for (; nblocks; nblocks--)
567 {
568 bin = VEC_LOAD_BE (in, 0, bige_const);
569 rkeylast = rkeylast_orig ^ bin;
570 b = iv;
571 iv = bin;
572
573 AES_ENCRYPT (b, rounds);
574
575 VEC_STORE_BE (out, 0, b, bige_const);
576
577 out++;
578 in++;
579 }
580
581 VEC_STORE_BE (iv_arg, 0, iv, bige_const);
582
583 clear_vec_regs();
584 }
585
586
587 void PPC_OPT_ATTR
588 CBC_ENC_FUNC (void *context, unsigned char *iv_arg, void *outbuf_arg,
589 const void *inbuf_arg, size_t nblocks, int cbc_mac)
590 {
591 const block bige_const = asm_load_be_const();
592 RIJNDAEL_context *ctx = context;
593 const u128_t *rk = (u128_t *)&ctx->keyschenc;
594 const u128_t *in = (const u128_t *)inbuf_arg;
595 byte *out = (byte *)outbuf_arg;
596 int rounds = ctx->rounds;
597 ROUND_KEY_VARIABLES_ALL;
598 block iv, key0_xor_keylast, nextiv, outiv;
599 unsigned int outadd = -(!cbc_mac) & 16;
600
601 if (nblocks == 0) /* CMAC may call with nblocks 0. */
602 return;
603
604 iv = VEC_LOAD_BE (iv_arg, 0, bige_const);
605
606 PRELOAD_ROUND_KEYS_ALL (rounds);
607 key0_xor_keylast = rkey0 ^ rkeylast;
608
609 nextiv = VEC_LOAD_BE (in++, 0, bige_const);
610 iv ^= rkey0 ^ nextiv;
611
612 do
613 {
614 if (--nblocks)
615 {
616 nextiv = key0_xor_keylast ^ VEC_LOAD_BE (in++, 0, bige_const);
617 }
618
619 iv = asm_cipher_be (iv, rkey1);
620 iv = asm_cipher_be (iv, rkey2);
621 iv = asm_cipher_be (iv, rkey3);
622 iv = asm_cipher_be (iv, rkey4);
623 iv = asm_cipher_be (iv, rkey5);
624 iv = asm_cipher_be (iv, rkey6);
625 iv = asm_cipher_be (iv, rkey7);
626 iv = asm_cipher_be (iv, rkey8);
627 iv = asm_cipher_be (iv, rkey9);
628 if (rounds >= 12)
629 {
630 iv = asm_cipher_be (iv, rkey10);
631 iv = asm_cipher_be (iv, rkey11);
632 if (rounds > 12)
633 {
634 iv = asm_cipher_be (iv, rkey12);
635 iv = asm_cipher_be (iv, rkey13);
636 }
637 }
638 outiv = iv;
639 /* Proper order for following instructions is important for best
640 * performance on POWER8: the output path vcipherlast needs to be
641 * last one. */
642 __asm__ volatile ("vcipherlast %0, %0, %2\n\t"
643 "vcipherlast %1, %1, %3\n\t"
644 : "+v" (iv), "+v" (outiv)
645 : "v" (nextiv), "v" (rkeylast));
646
647 VEC_STORE_BE ((u128_t *)out, 0, outiv, bige_const);
648 out += outadd;
649 }
650 while (nblocks);
651
652 VEC_STORE_BE (iv_arg, 0, outiv, bige_const);
653
654 clear_vec_regs();
655 }
656
657
658 void PPC_OPT_ATTR
659 CBC_DEC_FUNC (void *context, unsigned char *iv_arg, void *outbuf_arg,
660 const void *inbuf_arg, size_t nblocks)
661 {
662 const block bige_const = asm_load_be_const();
663 RIJNDAEL_context *ctx = context;
664 const u128_t *rk = (u128_t *)&ctx->keyschdec;
665 const u128_t *in = (const u128_t *)inbuf_arg;
666 u128_t *out = (u128_t *)outbuf_arg;
667 int rounds = ctx->rounds;
668 ROUND_KEY_VARIABLES;
669 block rkeylast_orig;
670 block in0, in1, in2, in3, in4, in5, in6, in7;
671 block b0, b1, b2, b3, b4, b5, b6, b7;
672 block rkey;
673 block iv, b;
674
675 if (!ctx->decryption_prepared)
676 {
677 internal_aes_ppc_prepare_decryption (ctx);
678 ctx->decryption_prepared = 1;
679 }
680
681 iv = VEC_LOAD_BE (iv_arg, 0, bige_const);
682
683 PRELOAD_ROUND_KEYS (rounds);
684 rkeylast_orig = rkeylast;
685
686 for (; nblocks >= 8; nblocks -= 8)
687 {
688 in0 = VEC_LOAD_BE_NOSWAP (in, 0);
689 in1 = VEC_LOAD_BE_NOSWAP (in, 1);
690 in2 = VEC_LOAD_BE_NOSWAP (in, 2);
691 in3 = VEC_LOAD_BE_NOSWAP (in, 3);
692 in0 = VEC_BE_SWAP (in0, bige_const);
693 in1 = VEC_BE_SWAP (in1, bige_const);
694 in4 = VEC_LOAD_BE_NOSWAP (in, 4);
695 in5 = VEC_LOAD_BE_NOSWAP (in, 5);
696 in2 = VEC_BE_SWAP (in2, bige_const);
697 in3 = VEC_BE_SWAP (in3, bige_const);
698 in6 = VEC_LOAD_BE_NOSWAP (in, 6);
699 in7 = VEC_LOAD_BE_NOSWAP (in, 7);
700 in += 8;
701 b0 = asm_xor (rkey0, in0);
702 b1 = asm_xor (rkey0, in1);
703 in4 = VEC_BE_SWAP (in4, bige_const);
704 in5 = VEC_BE_SWAP (in5, bige_const);
705 b2 = asm_xor (rkey0, in2);
706 b3 = asm_xor (rkey0, in3);
707 in6 = VEC_BE_SWAP (in6, bige_const);
708 in7 = VEC_BE_SWAP (in7, bige_const);
709 b4 = asm_xor (rkey0, in4);
710 b5 = asm_xor (rkey0, in5);
711 b6 = asm_xor (rkey0, in6);
712 b7 = asm_xor (rkey0, in7);
713
714 #define DO_ROUND(r) \
715 rkey = ALIGNED_LOAD (rk, r); \
716 b0 = asm_ncipher_be (b0, rkey); \
717 b1 = asm_ncipher_be (b1, rkey); \
718 b2 = asm_ncipher_be (b2, rkey); \
719 b3 = asm_ncipher_be (b3, rkey); \
720 b4 = asm_ncipher_be (b4, rkey); \
721 b5 = asm_ncipher_be (b5, rkey); \
722 b6 = asm_ncipher_be (b6, rkey); \
723 b7 = asm_ncipher_be (b7, rkey);
724
725 DO_ROUND(1);
726 DO_ROUND(2);
727 DO_ROUND(3);
728 DO_ROUND(4);
729 DO_ROUND(5);
730 DO_ROUND(6);
731 DO_ROUND(7);
732 DO_ROUND(8);
733 DO_ROUND(9);
734 if (rounds >= 12)
735 {
736 DO_ROUND(10);
737 DO_ROUND(11);
738 if (rounds > 12)
739 {
740 DO_ROUND(12);
741 DO_ROUND(13);
742 }
743 }
744
745 #undef DO_ROUND
746
747 iv = asm_xor (rkeylast, iv);
748 in0 = asm_xor (rkeylast, in0);
749 in1 = asm_xor (rkeylast, in1);
750 in2 = asm_xor (rkeylast, in2);
751 b0 = asm_ncipherlast_be (b0, iv);
752 iv = in7;
753 b1 = asm_ncipherlast_be (b1, in0);
754 in3 = asm_xor (rkeylast, in3);
755 in4 = asm_xor (rkeylast, in4);
756 b2 = asm_ncipherlast_be (b2, in1);
757 b3 = asm_ncipherlast_be (b3, in2);
758 in5 = asm_xor (rkeylast, in5);
759 in6 = asm_xor (rkeylast, in6);
760 b0 = VEC_BE_SWAP (b0, bige_const);
761 b1 = VEC_BE_SWAP (b1, bige_const);
762 b4 = asm_ncipherlast_be (b4, in3);
763 b5 = asm_ncipherlast_be (b5, in4);
764 b2 = VEC_BE_SWAP (b2, bige_const);
765 b3 = VEC_BE_SWAP (b3, bige_const);
766 b6 = asm_ncipherlast_be (b6, in5);
767 b7 = asm_ncipherlast_be (b7, in6);
768 b4 = VEC_BE_SWAP (b4, bige_const);
769 b5 = VEC_BE_SWAP (b5, bige_const);
770 b6 = VEC_BE_SWAP (b6, bige_const);
771 b7 = VEC_BE_SWAP (b7, bige_const);
772 VEC_STORE_BE_NOSWAP (out, 0, b0);
773 VEC_STORE_BE_NOSWAP (out, 1, b1);
774 VEC_STORE_BE_NOSWAP (out, 2, b2);
775 VEC_STORE_BE_NOSWAP (out, 3, b3);
776 VEC_STORE_BE_NOSWAP (out, 4, b4);
777 VEC_STORE_BE_NOSWAP (out, 5, b5);
778 VEC_STORE_BE_NOSWAP (out, 6, b6);
779 VEC_STORE_BE_NOSWAP (out, 7, b7);
780 out += 8;
781 }
782
783 if (nblocks >= 4)
784 {
785 in0 = VEC_LOAD_BE (in, 0, bige_const);
786 in1 = VEC_LOAD_BE (in, 1, bige_const);
787 in2 = VEC_LOAD_BE (in, 2, bige_const);
788 in3 = VEC_LOAD_BE (in, 3, bige_const);
789
790 b0 = asm_xor (rkey0, in0);
791 b1 = asm_xor (rkey0, in1);
792 b2 = asm_xor (rkey0, in2);
793 b3 = asm_xor (rkey0, in3);
794
795 #define DO_ROUND(r) \
796 rkey = ALIGNED_LOAD (rk, r); \
797 b0 = asm_ncipher_be (b0, rkey); \
798 b1 = asm_ncipher_be (b1, rkey); \
799 b2 = asm_ncipher_be (b2, rkey); \
800 b3 = asm_ncipher_be (b3, rkey);
801
802 DO_ROUND(1);
803 DO_ROUND(2);
804 DO_ROUND(3);
805 DO_ROUND(4);
806 DO_ROUND(5);
807 DO_ROUND(6);
808 DO_ROUND(7);
809 DO_ROUND(8);
810 DO_ROUND(9);
811 if (rounds >= 12)
812 {
813 DO_ROUND(10);
814 DO_ROUND(11);
815 if (rounds > 12)
816 {
817 DO_ROUND(12);
818 DO_ROUND(13);
819 }
820 }
821
822 #undef DO_ROUND
823
824 iv = asm_xor (rkeylast, iv);
825 in0 = asm_xor (rkeylast, in0);
826 in1 = asm_xor (rkeylast, in1);
827 in2 = asm_xor (rkeylast, in2);
828
829 b0 = asm_ncipherlast_be (b0, iv);
830 iv = in3;
831 b1 = asm_ncipherlast_be (b1, in0);
832 b2 = asm_ncipherlast_be (b2, in1);
833 b3 = asm_ncipherlast_be (b3, in2);
834
835 VEC_STORE_BE (out, 0, b0, bige_const);
836 VEC_STORE_BE (out, 1, b1, bige_const);
837 VEC_STORE_BE (out, 2, b2, bige_const);
838 VEC_STORE_BE (out, 3, b3, bige_const);
839
840 in += 4;
841 out += 4;
842 nblocks -= 4;
843 }
844
845 for (; nblocks; nblocks--)
846 {
847 rkeylast = rkeylast_orig ^ iv;
848
849 iv = VEC_LOAD_BE (in, 0, bige_const);
850 b = iv;
851 AES_DECRYPT (b, rounds);
852
853 VEC_STORE_BE (out, 0, b, bige_const);
854
855 in++;
856 out++;
857 }
858
859 VEC_STORE_BE (iv_arg, 0, iv, bige_const);
860
861 clear_vec_regs();
862 }
863
864
865 void PPC_OPT_ATTR
866 CTR_ENC_FUNC (void *context, unsigned char *ctr_arg, void *outbuf_arg,
867 const void *inbuf_arg, size_t nblocks)
868 {
869 static const unsigned char vec_one_const[16] =
870 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
871 const block bige_const = asm_load_be_const();
872 RIJNDAEL_context *ctx = context;
873 const u128_t *rk = (u128_t *)&ctx->keyschenc;
874 const u128_t *in = (const u128_t *)inbuf_arg;
875 u128_t *out = (u128_t *)outbuf_arg;
876 int rounds = ctx->rounds;
877 ROUND_KEY_VARIABLES;
878 block rkeylast_orig;
879 block ctr, b, one;
880
881 ctr = VEC_LOAD_BE (ctr_arg, 0, bige_const);
882 one = VEC_LOAD_BE (&vec_one_const, 0, bige_const);
883
884 PRELOAD_ROUND_KEYS (rounds);
885 rkeylast_orig = rkeylast;
886
887 if (nblocks >= 4)
888 {
889 block in0, in1, in2, in3, in4, in5, in6, in7;
890 block b0, b1, b2, b3, b4, b5, b6, b7;
891 block two, three, four;
892 block rkey;
893
894 two = asm_add_uint128 (one, one);
895 three = asm_add_uint128 (two, one);
896 four = asm_add_uint128 (two, two);
897
898 for (; nblocks >= 8; nblocks -= 8)
899 {
900 b1 = asm_add_uint128 (ctr, one);
901 b2 = asm_add_uint128 (ctr, two);
902 b3 = asm_add_uint128 (ctr, three);
903 b4 = asm_add_uint128 (ctr, four);
904 b5 = asm_add_uint128 (b1, four);
905 b6 = asm_add_uint128 (b2, four);
906 b7 = asm_add_uint128 (b3, four);
907 b0 = asm_xor (rkey0, ctr);
908 rkey = ALIGNED_LOAD (rk, 1);
909 ctr = asm_add_uint128 (b4, four);
910 b1 = asm_xor (rkey0, b1);
911 b2 = asm_xor (rkey0, b2);
912 b3 = asm_xor (rkey0, b3);
913 b0 = asm_cipher_be (b0, rkey);
914 b1 = asm_cipher_be (b1, rkey);
915 b2 = asm_cipher_be (b2, rkey);
916 b3 = asm_cipher_be (b3, rkey);
917 b4 = asm_xor (rkey0, b4);
918 b5 = asm_xor (rkey0, b5);
919 b6 = asm_xor (rkey0, b6);
920 b7 = asm_xor (rkey0, b7);
921 b4 = asm_cipher_be (b4, rkey);
922 b5 = asm_cipher_be (b5, rkey);
923 b6 = asm_cipher_be (b6, rkey);
924 b7 = asm_cipher_be (b7, rkey);
925
926 #define DO_ROUND(r) \
927 rkey = ALIGNED_LOAD (rk, r); \
928 b0 = asm_cipher_be (b0, rkey); \
929 b1 = asm_cipher_be (b1, rkey); \
930 b2 = asm_cipher_be (b2, rkey); \
931 b3 = asm_cipher_be (b3, rkey); \
932 b4 = asm_cipher_be (b4, rkey); \
933 b5 = asm_cipher_be (b5, rkey); \
934 b6 = asm_cipher_be (b6, rkey); \
935 b7 = asm_cipher_be (b7, rkey);
936
937 in0 = VEC_LOAD_BE_NOSWAP (in, 0);
938 DO_ROUND(2);
939 in1 = VEC_LOAD_BE_NOSWAP (in, 1);
940 DO_ROUND(3);
941 in2 = VEC_LOAD_BE_NOSWAP (in, 2);
942 DO_ROUND(4);
943 in3 = VEC_LOAD_BE_NOSWAP (in, 3);
944 DO_ROUND(5);
945 in4 = VEC_LOAD_BE_NOSWAP (in, 4);
946 DO_ROUND(6);
947 in5 = VEC_LOAD_BE_NOSWAP (in, 5);
948 DO_ROUND(7);
949 in6 = VEC_LOAD_BE_NOSWAP (in, 6);
950 DO_ROUND(8);
951 in7 = VEC_LOAD_BE_NOSWAP (in, 7);
952 in += 8;
953 DO_ROUND(9);
954
955 if (rounds >= 12)
956 {
957 DO_ROUND(10);
958 DO_ROUND(11);
959 if (rounds > 12)
960 {
961 DO_ROUND(12);
962 DO_ROUND(13);
963 }
964 }
965
966 #undef DO_ROUND
967
968 in0 = VEC_BE_SWAP (in0, bige_const);
969 in1 = VEC_BE_SWAP (in1, bige_const);
970 in2 = VEC_BE_SWAP (in2, bige_const);
971 in3 = VEC_BE_SWAP (in3, bige_const);
972 in4 = VEC_BE_SWAP (in4, bige_const);
973 in5 = VEC_BE_SWAP (in5, bige_const);
974 in6 = VEC_BE_SWAP (in6, bige_const);
975 in7 = VEC_BE_SWAP (in7, bige_const);
976
977 in0 = asm_xor (rkeylast, in0);
978 in1 = asm_xor (rkeylast, in1);
979 in2 = asm_xor (rkeylast, in2);
980 in3 = asm_xor (rkeylast, in3);
981 b0 = asm_cipherlast_be (b0, in0);
982 b1 = asm_cipherlast_be (b1, in1);
983 in4 = asm_xor (rkeylast, in4);
984 in5 = asm_xor (rkeylast, in5);
985 b2 = asm_cipherlast_be (b2, in2);
986 b3 = asm_cipherlast_be (b3, in3);
987 in6 = asm_xor (rkeylast, in6);
988 in7 = asm_xor (rkeylast, in7);
989 b4 = asm_cipherlast_be (b4, in4);
990 b5 = asm_cipherlast_be (b5, in5);
991 b6 = asm_cipherlast_be (b6, in6);
992 b7 = asm_cipherlast_be (b7, in7);
993
994 b0 = VEC_BE_SWAP (b0, bige_const);
995 b1 = VEC_BE_SWAP (b1, bige_const);
996 b2 = VEC_BE_SWAP (b2, bige_const);
997 b3 = VEC_BE_SWAP (b3, bige_const);
998 b4 = VEC_BE_SWAP (b4, bige_const);
999 b5 = VEC_BE_SWAP (b5, bige_const);
1000 b6 = VEC_BE_SWAP (b6, bige_const);
1001 b7 = VEC_BE_SWAP (b7, bige_const);
1002 VEC_STORE_BE_NOSWAP (out, 0, b0);
1003 VEC_STORE_BE_NOSWAP (out, 1, b1);
1004 VEC_STORE_BE_NOSWAP (out, 2, b2);
1005 VEC_STORE_BE_NOSWAP (out, 3, b3);
1006 VEC_STORE_BE_NOSWAP (out, 4, b4);
1007 VEC_STORE_BE_NOSWAP (out, 5, b5);
1008 VEC_STORE_BE_NOSWAP (out, 6, b6);
1009 VEC_STORE_BE_NOSWAP (out, 7, b7);
1010 out += 8;
1011 }
1012
1013 if (nblocks >= 4)
1014 {
1015 b1 = asm_add_uint128 (ctr, one);
1016 b2 = asm_add_uint128 (ctr, two);
1017 b3 = asm_add_uint128 (ctr, three);
1018 b0 = asm_xor (rkey0, ctr);
1019 ctr = asm_add_uint128 (ctr, four);
1020 b1 = asm_xor (rkey0, b1);
1021 b2 = asm_xor (rkey0, b2);
1022 b3 = asm_xor (rkey0, b3);
1023
1024 #define DO_ROUND(r) \
1025 rkey = ALIGNED_LOAD (rk, r); \
1026 b0 = asm_cipher_be (b0, rkey); \
1027 b1 = asm_cipher_be (b1, rkey); \
1028 b2 = asm_cipher_be (b2, rkey); \
1029 b3 = asm_cipher_be (b3, rkey);
1030
1031 DO_ROUND(1);
1032 DO_ROUND(2);
1033 DO_ROUND(3);
1034 DO_ROUND(4);
1035 DO_ROUND(5);
1036 DO_ROUND(6);
1037 DO_ROUND(7);
1038 DO_ROUND(8);
1039
1040 in0 = VEC_LOAD_BE (in, 0, bige_const);
1041 in1 = VEC_LOAD_BE (in, 1, bige_const);
1042 in2 = VEC_LOAD_BE (in, 2, bige_const);
1043 in3 = VEC_LOAD_BE (in, 3, bige_const);
1044
1045 DO_ROUND(9);
1046 if (rounds >= 12)
1047 {
1048 DO_ROUND(10);
1049 DO_ROUND(11);
1050 if (rounds > 12)
1051 {
1052 DO_ROUND(12);
1053 DO_ROUND(13);
1054 }
1055 }
1056
1057 #undef DO_ROUND
1058
1059 in0 = asm_xor (rkeylast, in0);
1060 in1 = asm_xor (rkeylast, in1);
1061 in2 = asm_xor (rkeylast, in2);
1062 in3 = asm_xor (rkeylast, in3);
1063
1064 b0 = asm_cipherlast_be (b0, in0);
1065 b1 = asm_cipherlast_be (b1, in1);
1066 b2 = asm_cipherlast_be (b2, in2);
1067 b3 = asm_cipherlast_be (b3, in3);
1068
1069 VEC_STORE_BE (out, 0, b0, bige_const);
1070 VEC_STORE_BE (out, 1, b1, bige_const);
1071 VEC_STORE_BE (out, 2, b2, bige_const);
1072 VEC_STORE_BE (out, 3, b3, bige_const);
1073
1074 in += 4;
1075 out += 4;
1076 nblocks -= 4;
1077 }
1078 }
1079
1080 for (; nblocks; nblocks--)
1081 {
1082 b = ctr;
1083 ctr = asm_add_uint128 (ctr, one);
1084 rkeylast = rkeylast_orig ^ VEC_LOAD_BE (in, 0, bige_const);
1085
1086 AES_ENCRYPT (b, rounds);
1087
1088 VEC_STORE_BE (out, 0, b, bige_const);
1089
1090 out++;
1091 in++;
1092 }
1093
1094 VEC_STORE_BE (ctr_arg, 0, ctr, bige_const);
1095
1096 clear_vec_regs();
1097 }
1098
1099
1100 size_t PPC_OPT_ATTR
1101 OCB_CRYPT_FUNC (gcry_cipher_hd_t c, void *outbuf_arg, const void *inbuf_arg,
1102 size_t nblocks, int encrypt)
1103 {
1104 const block bige_const = asm_load_be_const();
1105 RIJNDAEL_context *ctx = (void *)&c->context.c;
1106 const u128_t *in = (const u128_t *)inbuf_arg;
1107 u128_t *out = (u128_t *)outbuf_arg;
1108 int rounds = ctx->rounds;
1109 u64 data_nblocks = c->u_mode.ocb.data_nblocks;
1110 block l0, l1, l2, l;
1111 block b0, b1, b2, b3, b4, b5, b6, b7, b;
1112 block iv0, iv1, iv2, iv3, iv4, iv5, iv6, iv7;
1113 block rkey, rkeylf;
1114 block ctr, iv;
1115 ROUND_KEY_VARIABLES;
1116
1117 iv = VEC_LOAD_BE (c->u_iv.iv, 0, bige_const);
1118 ctr = VEC_LOAD_BE (c->u_ctr.ctr, 0, bige_const);
1119
1120 l0 = VEC_LOAD_BE (c->u_mode.ocb.L[0], 0, bige_const);
1121 l1 = VEC_LOAD_BE (c->u_mode.ocb.L[1], 0, bige_const);
1122 l2 = VEC_LOAD_BE (c->u_mode.ocb.L[2], 0, bige_const);
1123
1124 if (encrypt)
1125 {
1126 const u128_t *rk = (u128_t *)&ctx->keyschenc;
1127
1128 PRELOAD_ROUND_KEYS (rounds);
1129
1130 for (; nblocks >= 8 && data_nblocks % 8; nblocks--)
1131 {
1132 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1133 b = VEC_LOAD_BE (in, 0, bige_const);
1134
1135 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1136 iv ^= l;
1137 /* Checksum_i = Checksum_{i-1} xor P_i */
1138 ctr ^= b;
1139 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1140 b ^= iv;
1141 AES_ENCRYPT (b, rounds);
1142 b ^= iv;
1143
1144 VEC_STORE_BE (out, 0, b, bige_const);
1145
1146 in += 1;
1147 out += 1;
1148 }
1149
1150 for (; nblocks >= 8; nblocks -= 8)
1151 {
1152 b0 = VEC_LOAD_BE_NOSWAP (in, 0);
1153 b1 = VEC_LOAD_BE_NOSWAP (in, 1);
1154 b2 = VEC_LOAD_BE_NOSWAP (in, 2);
1155 b3 = VEC_LOAD_BE_NOSWAP (in, 3);
1156 b4 = VEC_LOAD_BE_NOSWAP (in, 4);
1157 b5 = VEC_LOAD_BE_NOSWAP (in, 5);
1158 b6 = VEC_LOAD_BE_NOSWAP (in, 6);
1159 b7 = VEC_LOAD_BE_NOSWAP (in, 7);
1160 in += 8;
1161 l = VEC_LOAD_BE_NOSWAP (ocb_get_l (c, data_nblocks += 8), 0);
1162 b0 = VEC_BE_SWAP(b0, bige_const);
1163 b1 = VEC_BE_SWAP(b1, bige_const);
1164 b2 = VEC_BE_SWAP(b2, bige_const);
1165 b3 = VEC_BE_SWAP(b3, bige_const);
1166 b4 = VEC_BE_SWAP(b4, bige_const);
1167 b5 = VEC_BE_SWAP(b5, bige_const);
1168 b6 = VEC_BE_SWAP(b6, bige_const);
1169 b7 = VEC_BE_SWAP(b7, bige_const);
1170 l = VEC_BE_SWAP(l, bige_const);
1171
1172 ctr ^= b0 ^ b1 ^ b2 ^ b3 ^ b4 ^ b5 ^ b6 ^ b7;
1173
1174 iv ^= rkey0;
1175
1176 iv0 = iv ^ l0;
1177 iv1 = iv ^ l0 ^ l1;
1178 iv2 = iv ^ l1;
1179 iv3 = iv ^ l1 ^ l2;
1180 iv4 = iv ^ l1 ^ l2 ^ l0;
1181 iv5 = iv ^ l2 ^ l0;
1182 iv6 = iv ^ l2;
1183 iv7 = iv ^ l2 ^ l;
1184
1185 b0 ^= iv0;
1186 b1 ^= iv1;
1187 b2 ^= iv2;
1188 b3 ^= iv3;
1189 b4 ^= iv4;
1190 b5 ^= iv5;
1191 b6 ^= iv6;
1192 b7 ^= iv7;
1193 iv = iv7 ^ rkey0;
1194
1195 #define DO_ROUND(r) \
1196 rkey = ALIGNED_LOAD (rk, r); \
1197 b0 = asm_cipher_be (b0, rkey); \
1198 b1 = asm_cipher_be (b1, rkey); \
1199 b2 = asm_cipher_be (b2, rkey); \
1200 b3 = asm_cipher_be (b3, rkey); \
1201 b4 = asm_cipher_be (b4, rkey); \
1202 b5 = asm_cipher_be (b5, rkey); \
1203 b6 = asm_cipher_be (b6, rkey); \
1204 b7 = asm_cipher_be (b7, rkey);
1205
1206 DO_ROUND(1);
1207 DO_ROUND(2);
1208 DO_ROUND(3);
1209 DO_ROUND(4);
1210 DO_ROUND(5);
1211 DO_ROUND(6);
1212 DO_ROUND(7);
1213
1214 rkeylf = asm_xor (rkeylast, rkey0);
1215
1216 DO_ROUND(8);
1217
1218 iv0 = asm_xor (rkeylf, iv0);
1219 iv1 = asm_xor (rkeylf, iv1);
1220 iv2 = asm_xor (rkeylf, iv2);
1221 iv3 = asm_xor (rkeylf, iv3);
1222 iv4 = asm_xor (rkeylf, iv4);
1223 iv5 = asm_xor (rkeylf, iv5);
1224 iv6 = asm_xor (rkeylf, iv6);
1225 iv7 = asm_xor (rkeylf, iv7);
1226
1227 DO_ROUND(9);
1228 if (rounds >= 12)
1229 {
1230 DO_ROUND(10);
1231 DO_ROUND(11);
1232 if (rounds > 12)
1233 {
1234 DO_ROUND(12);
1235 DO_ROUND(13);
1236 }
1237 }
1238
1239 #undef DO_ROUND
1240
1241 b0 = asm_cipherlast_be (b0, iv0);
1242 b1 = asm_cipherlast_be (b1, iv1);
1243 b2 = asm_cipherlast_be (b2, iv2);
1244 b3 = asm_cipherlast_be (b3, iv3);
1245 b4 = asm_cipherlast_be (b4, iv4);
1246 b5 = asm_cipherlast_be (b5, iv5);
1247 b6 = asm_cipherlast_be (b6, iv6);
1248 b7 = asm_cipherlast_be (b7, iv7);
1249
1250 b0 = VEC_BE_SWAP (b0, bige_const);
1251 b1 = VEC_BE_SWAP (b1, bige_const);
1252 b2 = VEC_BE_SWAP (b2, bige_const);
1253 b3 = VEC_BE_SWAP (b3, bige_const);
1254 b4 = VEC_BE_SWAP (b4, bige_const);
1255 b5 = VEC_BE_SWAP (b5, bige_const);
1256 b6 = VEC_BE_SWAP (b6, bige_const);
1257 b7 = VEC_BE_SWAP (b7, bige_const);
1258 VEC_STORE_BE_NOSWAP (out, 0, b0);
1259 VEC_STORE_BE_NOSWAP (out, 1, b1);
1260 VEC_STORE_BE_NOSWAP (out, 2, b2);
1261 VEC_STORE_BE_NOSWAP (out, 3, b3);
1262 VEC_STORE_BE_NOSWAP (out, 4, b4);
1263 VEC_STORE_BE_NOSWAP (out, 5, b5);
1264 VEC_STORE_BE_NOSWAP (out, 6, b6);
1265 VEC_STORE_BE_NOSWAP (out, 7, b7);
1266 out += 8;
1267 }
1268
1269 if (nblocks >= 4 && (data_nblocks % 4) == 0)
1270 {
1271 b0 = VEC_LOAD_BE (in, 0, bige_const);
1272 b1 = VEC_LOAD_BE (in, 1, bige_const);
1273 b2 = VEC_LOAD_BE (in, 2, bige_const);
1274 b3 = VEC_LOAD_BE (in, 3, bige_const);
1275
1276 l = VEC_LOAD_BE (ocb_get_l (c, data_nblocks += 4), 0, bige_const);
1277
1278 ctr ^= b0 ^ b1 ^ b2 ^ b3;
1279
1280 iv ^= rkey0;
1281
1282 iv0 = iv ^ l0;
1283 iv1 = iv ^ l0 ^ l1;
1284 iv2 = iv ^ l1;
1285 iv3 = iv ^ l1 ^ l;
1286
1287 b0 ^= iv0;
1288 b1 ^= iv1;
1289 b2 ^= iv2;
1290 b3 ^= iv3;
1291 iv = iv3 ^ rkey0;
1292
1293 #define DO_ROUND(r) \
1294 rkey = ALIGNED_LOAD (rk, r); \
1295 b0 = asm_cipher_be (b0, rkey); \
1296 b1 = asm_cipher_be (b1, rkey); \
1297 b2 = asm_cipher_be (b2, rkey); \
1298 b3 = asm_cipher_be (b3, rkey);
1299
1300 DO_ROUND(1);
1301 DO_ROUND(2);
1302 DO_ROUND(3);
1303 DO_ROUND(4);
1304 DO_ROUND(5);
1305 DO_ROUND(6);
1306 DO_ROUND(7);
1307 DO_ROUND(8);
1308 DO_ROUND(9);
1309 if (rounds >= 12)
1310 {
1311 DO_ROUND(10);
1312 DO_ROUND(11);
1313 if (rounds > 12)
1314 {
1315 DO_ROUND(12);
1316 DO_ROUND(13);
1317 }
1318 }
1319
1320 #undef DO_ROUND
1321
1322 rkey = rkeylast ^ rkey0;
1323 b0 = asm_cipherlast_be (b0, rkey ^ iv0);
1324 b1 = asm_cipherlast_be (b1, rkey ^ iv1);
1325 b2 = asm_cipherlast_be (b2, rkey ^ iv2);
1326 b3 = asm_cipherlast_be (b3, rkey ^ iv3);
1327
1328 VEC_STORE_BE (out, 0, b0, bige_const);
1329 VEC_STORE_BE (out, 1, b1, bige_const);
1330 VEC_STORE_BE (out, 2, b2, bige_const);
1331 VEC_STORE_BE (out, 3, b3, bige_const);
1332
1333 in += 4;
1334 out += 4;
1335 nblocks -= 4;
1336 }
1337
1338 for (; nblocks; nblocks--)
1339 {
1340 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1341 b = VEC_LOAD_BE (in, 0, bige_const);
1342
1343 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1344 iv ^= l;
1345 /* Checksum_i = Checksum_{i-1} xor P_i */
1346 ctr ^= b;
1347 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
1348 b ^= iv;
1349 AES_ENCRYPT (b, rounds);
1350 b ^= iv;
1351
1352 VEC_STORE_BE (out, 0, b, bige_const);
1353
1354 in += 1;
1355 out += 1;
1356 }
1357 }
1358 else
1359 {
1360 const u128_t *rk = (u128_t *)&ctx->keyschdec;
1361
1362 if (!ctx->decryption_prepared)
1363 {
1364 internal_aes_ppc_prepare_decryption (ctx);
1365 ctx->decryption_prepared = 1;
1366 }
1367
1368 PRELOAD_ROUND_KEYS (rounds);
1369
1370 for (; nblocks >= 8 && data_nblocks % 8; nblocks--)
1371 {
1372 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1373 b = VEC_LOAD_BE (in, 0, bige_const);
1374
1375 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1376 iv ^= l;
1377 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
1378 b ^= iv;
1379 AES_DECRYPT (b, rounds);
1380 b ^= iv;
1381 /* Checksum_i = Checksum_{i-1} xor P_i */
1382 ctr ^= b;
1383
1384 VEC_STORE_BE (out, 0, b, bige_const);
1385
1386 in += 1;
1387 out += 1;
1388 }
1389
1390 for (; nblocks >= 8; nblocks -= 8)
1391 {
1392 b0 = VEC_LOAD_BE_NOSWAP (in, 0);
1393 b1 = VEC_LOAD_BE_NOSWAP (in, 1);
1394 b2 = VEC_LOAD_BE_NOSWAP (in, 2);
1395 b3 = VEC_LOAD_BE_NOSWAP (in, 3);
1396 b4 = VEC_LOAD_BE_NOSWAP (in, 4);
1397 b5 = VEC_LOAD_BE_NOSWAP (in, 5);
1398 b6 = VEC_LOAD_BE_NOSWAP (in, 6);
1399 b7 = VEC_LOAD_BE_NOSWAP (in, 7);
1400 in += 8;
1401 l = VEC_LOAD_BE_NOSWAP (ocb_get_l (c, data_nblocks += 8), 0);
1402 b0 = VEC_BE_SWAP(b0, bige_const);
1403 b1 = VEC_BE_SWAP(b1, bige_const);
1404 b2 = VEC_BE_SWAP(b2, bige_const);
1405 b3 = VEC_BE_SWAP(b3, bige_const);
1406 b4 = VEC_BE_SWAP(b4, bige_const);
1407 b5 = VEC_BE_SWAP(b5, bige_const);
1408 b6 = VEC_BE_SWAP(b6, bige_const);
1409 b7 = VEC_BE_SWAP(b7, bige_const);
1410 l = VEC_BE_SWAP(l, bige_const);
1411
1412 iv ^= rkey0;
1413
1414 iv0 = iv ^ l0;
1415 iv1 = iv ^ l0 ^ l1;
1416 iv2 = iv ^ l1;
1417 iv3 = iv ^ l1 ^ l2;
1418 iv4 = iv ^ l1 ^ l2 ^ l0;
1419 iv5 = iv ^ l2 ^ l0;
1420 iv6 = iv ^ l2;
1421 iv7 = iv ^ l2 ^ l;
1422
1423 b0 ^= iv0;
1424 b1 ^= iv1;
1425 b2 ^= iv2;
1426 b3 ^= iv3;
1427 b4 ^= iv4;
1428 b5 ^= iv5;
1429 b6 ^= iv6;
1430 b7 ^= iv7;
1431 iv = iv7 ^ rkey0;
1432
1433 #define DO_ROUND(r) \
1434 rkey = ALIGNED_LOAD (rk, r); \
1435 b0 = asm_ncipher_be (b0, rkey); \
1436 b1 = asm_ncipher_be (b1, rkey); \
1437 b2 = asm_ncipher_be (b2, rkey); \
1438 b3 = asm_ncipher_be (b3, rkey); \
1439 b4 = asm_ncipher_be (b4, rkey); \
1440 b5 = asm_ncipher_be (b5, rkey); \
1441 b6 = asm_ncipher_be (b6, rkey); \
1442 b7 = asm_ncipher_be (b7, rkey);
1443
1444 DO_ROUND(1);
1445 DO_ROUND(2);
1446 DO_ROUND(3);
1447 DO_ROUND(4);
1448 DO_ROUND(5);
1449 DO_ROUND(6);
1450 DO_ROUND(7);
1451
1452 rkeylf = asm_xor (rkeylast, rkey0);
1453
1454 DO_ROUND(8);
1455
1456 iv0 = asm_xor (rkeylf, iv0);
1457 iv1 = asm_xor (rkeylf, iv1);
1458 iv2 = asm_xor (rkeylf, iv2);
1459 iv3 = asm_xor (rkeylf, iv3);
1460 iv4 = asm_xor (rkeylf, iv4);
1461 iv5 = asm_xor (rkeylf, iv5);
1462 iv6 = asm_xor (rkeylf, iv6);
1463 iv7 = asm_xor (rkeylf, iv7);
1464
1465 DO_ROUND(9);
1466 if (rounds >= 12)
1467 {
1468 DO_ROUND(10);
1469 DO_ROUND(11);
1470 if (rounds > 12)
1471 {
1472 DO_ROUND(12);
1473 DO_ROUND(13);
1474 }
1475 }
1476
1477 #undef DO_ROUND
1478
1479 b0 = asm_ncipherlast_be (b0, iv0);
1480 b1 = asm_ncipherlast_be (b1, iv1);
1481 b2 = asm_ncipherlast_be (b2, iv2);
1482 b3 = asm_ncipherlast_be (b3, iv3);
1483 b4 = asm_ncipherlast_be (b4, iv4);
1484 b5 = asm_ncipherlast_be (b5, iv5);
1485 b6 = asm_ncipherlast_be (b6, iv6);
1486 b7 = asm_ncipherlast_be (b7, iv7);
1487
1488 ctr ^= b0 ^ b1 ^ b2 ^ b3 ^ b4 ^ b5 ^ b6 ^ b7;
1489
1490 b0 = VEC_BE_SWAP (b0, bige_const);
1491 b1 = VEC_BE_SWAP (b1, bige_const);
1492 b2 = VEC_BE_SWAP (b2, bige_const);
1493 b3 = VEC_BE_SWAP (b3, bige_const);
1494 b4 = VEC_BE_SWAP (b4, bige_const);
1495 b5 = VEC_BE_SWAP (b5, bige_const);
1496 b6 = VEC_BE_SWAP (b6, bige_const);
1497 b7 = VEC_BE_SWAP (b7, bige_const);
1498 VEC_STORE_BE_NOSWAP (out, 0, b0);
1499 VEC_STORE_BE_NOSWAP (out, 1, b1);
1500 VEC_STORE_BE_NOSWAP (out, 2, b2);
1501 VEC_STORE_BE_NOSWAP (out, 3, b3);
1502 VEC_STORE_BE_NOSWAP (out, 4, b4);
1503 VEC_STORE_BE_NOSWAP (out, 5, b5);
1504 VEC_STORE_BE_NOSWAP (out, 6, b6);
1505 VEC_STORE_BE_NOSWAP (out, 7, b7);
1506 out += 8;
1507 }
1508
1509 if (nblocks >= 4 && (data_nblocks % 4) == 0)
1510 {
1511 b0 = VEC_LOAD_BE (in, 0, bige_const);
1512 b1 = VEC_LOAD_BE (in, 1, bige_const);
1513 b2 = VEC_LOAD_BE (in, 2, bige_const);
1514 b3 = VEC_LOAD_BE (in, 3, bige_const);
1515
1516 l = VEC_LOAD_BE (ocb_get_l (c, data_nblocks += 4), 0, bige_const);
1517
1518 iv ^= rkey0;
1519
1520 iv0 = iv ^ l0;
1521 iv1 = iv ^ l0 ^ l1;
1522 iv2 = iv ^ l1;
1523 iv3 = iv ^ l1 ^ l;
1524
1525 b0 ^= iv0;
1526 b1 ^= iv1;
1527 b2 ^= iv2;
1528 b3 ^= iv3;
1529 iv = iv3 ^ rkey0;
1530
1531 #define DO_ROUND(r) \
1532 rkey = ALIGNED_LOAD (rk, r); \
1533 b0 = asm_ncipher_be (b0, rkey); \
1534 b1 = asm_ncipher_be (b1, rkey); \
1535 b2 = asm_ncipher_be (b2, rkey); \
1536 b3 = asm_ncipher_be (b3, rkey);
1537
1538 DO_ROUND(1);
1539 DO_ROUND(2);
1540 DO_ROUND(3);
1541 DO_ROUND(4);
1542 DO_ROUND(5);
1543 DO_ROUND(6);
1544 DO_ROUND(7);
1545 DO_ROUND(8);
1546 DO_ROUND(9);
1547 if (rounds >= 12)
1548 {
1549 DO_ROUND(10);
1550 DO_ROUND(11);
1551 if (rounds > 12)
1552 {
1553 DO_ROUND(12);
1554 DO_ROUND(13);
1555 }
1556 }
1557
1558 #undef DO_ROUND
1559
1560 rkey = rkeylast ^ rkey0;
1561 b0 = asm_ncipherlast_be (b0, rkey ^ iv0);
1562 b1 = asm_ncipherlast_be (b1, rkey ^ iv1);
1563 b2 = asm_ncipherlast_be (b2, rkey ^ iv2);
1564 b3 = asm_ncipherlast_be (b3, rkey ^ iv3);
1565
1566 VEC_STORE_BE (out, 0, b0, bige_const);
1567 VEC_STORE_BE (out, 1, b1, bige_const);
1568 VEC_STORE_BE (out, 2, b2, bige_const);
1569 VEC_STORE_BE (out, 3, b3, bige_const);
1570
1571 ctr ^= b0 ^ b1 ^ b2 ^ b3;
1572
1573 in += 4;
1574 out += 4;
1575 nblocks -= 4;
1576 }
1577
1578 for (; nblocks; nblocks--)
1579 {
1580 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1581 b = VEC_LOAD_BE (in, 0, bige_const);
1582
1583 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1584 iv ^= l;
1585 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
1586 b ^= iv;
1587 AES_DECRYPT (b, rounds);
1588 b ^= iv;
1589 /* Checksum_i = Checksum_{i-1} xor P_i */
1590 ctr ^= b;
1591
1592 VEC_STORE_BE (out, 0, b, bige_const);
1593
1594 in += 1;
1595 out += 1;
1596 }
1597 }
1598
1599 VEC_STORE_BE (c->u_iv.iv, 0, iv, bige_const);
1600 VEC_STORE_BE (c->u_ctr.ctr, 0, ctr, bige_const);
1601 c->u_mode.ocb.data_nblocks = data_nblocks;
1602
1603 clear_vec_regs();
1604
1605 return 0;
1606 }
1607
1608
1609 size_t PPC_OPT_ATTR
1610 OCB_AUTH_FUNC (gcry_cipher_hd_t c, void *abuf_arg, size_t nblocks)
1611 {
1612 const block bige_const = asm_load_be_const();
1613 RIJNDAEL_context *ctx = (void *)&c->context.c;
1614 const u128_t *rk = (u128_t *)&ctx->keyschenc;
1615 const u128_t *abuf = (const u128_t *)abuf_arg;
1616 int rounds = ctx->rounds;
1617 u64 data_nblocks = c->u_mode.ocb.aad_nblocks;
1618 block l0, l1, l2, l;
1619 block b0, b1, b2, b3, b4, b5, b6, b7, b;
1620 block iv0, iv1, iv2, iv3, iv4, iv5, iv6, iv7;
1621 block rkey, frkey;
1622 block ctr, iv;
1623 ROUND_KEY_VARIABLES;
1624
1625 iv = VEC_LOAD_BE (c->u_mode.ocb.aad_offset, 0, bige_const);
1626 ctr = VEC_LOAD_BE (c->u_mode.ocb.aad_sum, 0, bige_const);
1627
1628 l0 = VEC_LOAD_BE (c->u_mode.ocb.L[0], 0, bige_const);
1629 l1 = VEC_LOAD_BE (c->u_mode.ocb.L[1], 0, bige_const);
1630 l2 = VEC_LOAD_BE (c->u_mode.ocb.L[2], 0, bige_const);
1631
1632 PRELOAD_ROUND_KEYS (rounds);
1633
1634 for (; nblocks >= 8 && data_nblocks % 8; nblocks--)
1635 {
1636 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1637 b = VEC_LOAD_BE (abuf, 0, bige_const);
1638
1639 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1640 iv ^= l;
1641 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1642 b ^= iv;
1643 AES_ENCRYPT (b, rounds);
1644 ctr ^= b;
1645
1646 abuf += 1;
1647 }
1648
1649 for (; nblocks >= 8; nblocks -= 8)
1650 {
1651 b0 = VEC_LOAD_BE (abuf, 0, bige_const);
1652 b1 = VEC_LOAD_BE (abuf, 1, bige_const);
1653 b2 = VEC_LOAD_BE (abuf, 2, bige_const);
1654 b3 = VEC_LOAD_BE (abuf, 3, bige_const);
1655 b4 = VEC_LOAD_BE (abuf, 4, bige_const);
1656 b5 = VEC_LOAD_BE (abuf, 5, bige_const);
1657 b6 = VEC_LOAD_BE (abuf, 6, bige_const);
1658 b7 = VEC_LOAD_BE (abuf, 7, bige_const);
1659
1660 l = VEC_LOAD_BE (ocb_get_l (c, data_nblocks += 8), 0, bige_const);
1661
1662 frkey = rkey0;
1663 iv ^= frkey;
1664
1665 iv0 = iv ^ l0;
1666 iv1 = iv ^ l0 ^ l1;
1667 iv2 = iv ^ l1;
1668 iv3 = iv ^ l1 ^ l2;
1669 iv4 = iv ^ l1 ^ l2 ^ l0;
1670 iv5 = iv ^ l2 ^ l0;
1671 iv6 = iv ^ l2;
1672 iv7 = iv ^ l2 ^ l;
1673
1674 b0 ^= iv0;
1675 b1 ^= iv1;
1676 b2 ^= iv2;
1677 b3 ^= iv3;
1678 b4 ^= iv4;
1679 b5 ^= iv5;
1680 b6 ^= iv6;
1681 b7 ^= iv7;
1682 iv = iv7 ^ frkey;
1683
1684 #define DO_ROUND(r) \
1685 rkey = ALIGNED_LOAD (rk, r); \
1686 b0 = asm_cipher_be (b0, rkey); \
1687 b1 = asm_cipher_be (b1, rkey); \
1688 b2 = asm_cipher_be (b2, rkey); \
1689 b3 = asm_cipher_be (b3, rkey); \
1690 b4 = asm_cipher_be (b4, rkey); \
1691 b5 = asm_cipher_be (b5, rkey); \
1692 b6 = asm_cipher_be (b6, rkey); \
1693 b7 = asm_cipher_be (b7, rkey);
1694
1695 DO_ROUND(1);
1696 DO_ROUND(2);
1697 DO_ROUND(3);
1698 DO_ROUND(4);
1699 DO_ROUND(5);
1700 DO_ROUND(6);
1701 DO_ROUND(7);
1702 DO_ROUND(8);
1703 DO_ROUND(9);
1704 if (rounds >= 12)
1705 {
1706 DO_ROUND(10);
1707 DO_ROUND(11);
1708 if (rounds > 12)
1709 {
1710 DO_ROUND(12);
1711 DO_ROUND(13);
1712 }
1713 }
1714
1715 #undef DO_ROUND
1716
1717 rkey = rkeylast;
1718 b0 = asm_cipherlast_be (b0, rkey);
1719 b1 = asm_cipherlast_be (b1, rkey);
1720 b2 = asm_cipherlast_be (b2, rkey);
1721 b3 = asm_cipherlast_be (b3, rkey);
1722 b4 = asm_cipherlast_be (b4, rkey);
1723 b5 = asm_cipherlast_be (b5, rkey);
1724 b6 = asm_cipherlast_be (b6, rkey);
1725 b7 = asm_cipherlast_be (b7, rkey);
1726
1727 ctr ^= b0 ^ b1 ^ b2 ^ b3 ^ b4 ^ b5 ^ b6 ^ b7;
1728
1729 abuf += 8;
1730 }
1731
1732 if (nblocks >= 4 && (data_nblocks % 4) == 0)
1733 {
1734 b0 = VEC_LOAD_BE (abuf, 0, bige_const);
1735 b1 = VEC_LOAD_BE (abuf, 1, bige_const);
1736 b2 = VEC_LOAD_BE (abuf, 2, bige_const);
1737 b3 = VEC_LOAD_BE (abuf, 3, bige_const);
1738
1739 l = VEC_LOAD_BE (ocb_get_l (c, data_nblocks += 4), 0, bige_const);
1740
1741 frkey = rkey0;
1742 iv ^= frkey;
1743
1744 iv0 = iv ^ l0;
1745 iv1 = iv ^ l0 ^ l1;
1746 iv2 = iv ^ l1;
1747 iv3 = iv ^ l1 ^ l;
1748
1749 b0 ^= iv0;
1750 b1 ^= iv1;
1751 b2 ^= iv2;
1752 b3 ^= iv3;
1753 iv = iv3 ^ frkey;
1754
1755 #define DO_ROUND(r) \
1756 rkey = ALIGNED_LOAD (rk, r); \
1757 b0 = asm_cipher_be (b0, rkey); \
1758 b1 = asm_cipher_be (b1, rkey); \
1759 b2 = asm_cipher_be (b2, rkey); \
1760 b3 = asm_cipher_be (b3, rkey);
1761
1762 DO_ROUND(1);
1763 DO_ROUND(2);
1764 DO_ROUND(3);
1765 DO_ROUND(4);
1766 DO_ROUND(5);
1767 DO_ROUND(6);
1768 DO_ROUND(7);
1769 DO_ROUND(8);
1770 DO_ROUND(9);
1771 if (rounds >= 12)
1772 {
1773 DO_ROUND(10);
1774 DO_ROUND(11);
1775 if (rounds > 12)
1776 {
1777 DO_ROUND(12);
1778 DO_ROUND(13);
1779 }
1780 }
1781
1782 #undef DO_ROUND
1783
1784 rkey = rkeylast;
1785 b0 = asm_cipherlast_be (b0, rkey);
1786 b1 = asm_cipherlast_be (b1, rkey);
1787 b2 = asm_cipherlast_be (b2, rkey);
1788 b3 = asm_cipherlast_be (b3, rkey);
1789
1790 ctr ^= b0 ^ b1 ^ b2 ^ b3;
1791
1792 abuf += 4;
1793 nblocks -= 4;
1794 }
1795
1796 for (; nblocks; nblocks--)
1797 {
1798 l = VEC_LOAD_BE (ocb_get_l (c, ++data_nblocks), 0, bige_const);
1799 b = VEC_LOAD_BE (abuf, 0, bige_const);
1800
1801 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
1802 iv ^= l;
1803 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
1804 b ^= iv;
1805 AES_ENCRYPT (b, rounds);
1806 ctr ^= b;
1807
1808 abuf += 1;
1809 }
1810
1811 VEC_STORE_BE (c->u_mode.ocb.aad_offset, 0, iv, bige_const);
1812 VEC_STORE_BE (c->u_mode.ocb.aad_sum, 0, ctr, bige_const);
1813 c->u_mode.ocb.aad_nblocks = data_nblocks;
1814
1815 clear_vec_regs();
1816
1817 return 0;
1818 }
1819
1820
1821 void PPC_OPT_ATTR
1822 XTS_CRYPT_FUNC (void *context, unsigned char *tweak_arg, void *outbuf_arg,
1823 const void *inbuf_arg, size_t nblocks, int encrypt)
1824 {
1825 #ifdef WORDS_BIGENDIAN
1826 static const block vec_bswap128_const =
1827 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
1828 #else
1829 static const block vec_bswap128_const =
1830 { ~15, ~14, ~13, ~12, ~11, ~10, ~9, ~8, ~7, ~6, ~5, ~4, ~3, ~2, ~1, ~0 };
1831 #endif
1832 static const unsigned char vec_tweak_const[16] =
1833 { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0x87 };
1834 static const vector unsigned long long vec_shift63_const =
1835 { 63, 63 };
1836 const block bige_const = asm_load_be_const();
1837 RIJNDAEL_context *ctx = context;
1838 const u128_t *in = (const u128_t *)inbuf_arg;
1839 u128_t *out = (u128_t *)outbuf_arg;
1840 int rounds = ctx->rounds;
1841 block tweak;
1842 block b0, b1, b2, b3, b4, b5, b6, b7, b, rkey, rkeylf;
1843 block tweak0, tweak1, tweak2, tweak3, tweak4, tweak5, tweak6, tweak7;
1844 block tweak_const, bswap128_const, shift63_const;
1845 ROUND_KEY_VARIABLES;
1846
1847 tweak_const = VEC_LOAD_BE (&vec_tweak_const, 0, bige_const);
1848 bswap128_const = ALIGNED_LOAD (&vec_bswap128_const, 0);
1849 shift63_const = ALIGNED_LOAD (&vec_shift63_const, 0);
1850
1851 tweak = VEC_LOAD_BE (tweak_arg, 0, bige_const);
1852 tweak = asm_vperm1 (tweak, bswap128_const);
1853
1854 #define GEN_TWEAK(tout, tin) /* Generate next tweak. */ \
1855 do { \
1856 block tmp1, tmp2; \
1857 tmp1 = asm_swap_uint64_halfs(tin); \
1858 tmp2 = asm_add_uint64(tin, tin); \
1859 tmp1 = asm_sra_int64(tmp1, shift63_const) & tweak_const; \
1860 tout = asm_xor(tmp1, tmp2); \
1861 } while (0)
1862
1863 if (encrypt)
1864 {
1865 const u128_t *rk = (u128_t *)&ctx->keyschenc;
1866
1867 PRELOAD_ROUND_KEYS (rounds);
1868
1869 for (; nblocks >= 8; nblocks -= 8)
1870 {
1871 b0 = VEC_LOAD_BE_NOSWAP (in, 0);
1872 b1 = VEC_LOAD_BE_NOSWAP (in, 1);
1873 b2 = VEC_LOAD_BE_NOSWAP (in, 2);
1874 b3 = VEC_LOAD_BE_NOSWAP (in, 3);
1875 tweak0 = tweak;
1876 GEN_TWEAK (tweak1, tweak0);
1877 tweak0 = asm_vperm1 (tweak0, bswap128_const);
1878 b4 = VEC_LOAD_BE_NOSWAP (in, 4);
1879 b5 = VEC_LOAD_BE_NOSWAP (in, 5);
1880 GEN_TWEAK (tweak2, tweak1);
1881 tweak1 = asm_vperm1 (tweak1, bswap128_const);
1882 b6 = VEC_LOAD_BE_NOSWAP (in, 6);
1883 b7 = VEC_LOAD_BE_NOSWAP (in, 7);
1884 in += 8;
1885
1886 b0 = VEC_BE_SWAP(b0, bige_const);
1887 b1 = VEC_BE_SWAP(b1, bige_const);
1888 GEN_TWEAK (tweak3, tweak2);
1889 tweak2 = asm_vperm1 (tweak2, bswap128_const);
1890 GEN_TWEAK (tweak4, tweak3);
1891 tweak3 = asm_vperm1 (tweak3, bswap128_const);
1892 b2 = VEC_BE_SWAP(b2, bige_const);
1893 b3 = VEC_BE_SWAP(b3, bige_const);
1894 GEN_TWEAK (tweak5, tweak4);
1895 tweak4 = asm_vperm1 (tweak4, bswap128_const);
1896 GEN_TWEAK (tweak6, tweak5);
1897 tweak5 = asm_vperm1 (tweak5, bswap128_const);
1898 b4 = VEC_BE_SWAP(b4, bige_const);
1899 b5 = VEC_BE_SWAP(b5, bige_const);
1900 GEN_TWEAK (tweak7, tweak6);
1901 tweak6 = asm_vperm1 (tweak6, bswap128_const);
1902 GEN_TWEAK (tweak, tweak7);
1903 tweak7 = asm_vperm1 (tweak7, bswap128_const);
1904 b6 = VEC_BE_SWAP(b6, bige_const);
1905 b7 = VEC_BE_SWAP(b7, bige_const);
1906
1907 tweak0 = asm_xor (tweak0, rkey0);
1908 tweak1 = asm_xor (tweak1, rkey0);
1909 tweak2 = asm_xor (tweak2, rkey0);
1910 tweak3 = asm_xor (tweak3, rkey0);
1911 tweak4 = asm_xor (tweak4, rkey0);
1912 tweak5 = asm_xor (tweak5, rkey0);
1913 tweak6 = asm_xor (tweak6, rkey0);
1914 tweak7 = asm_xor (tweak7, rkey0);
1915
1916 b0 = asm_xor (b0, tweak0);
1917 b1 = asm_xor (b1, tweak1);
1918 b2 = asm_xor (b2, tweak2);
1919 b3 = asm_xor (b3, tweak3);
1920 b4 = asm_xor (b4, tweak4);
1921 b5 = asm_xor (b5, tweak5);
1922 b6 = asm_xor (b6, tweak6);
1923 b7 = asm_xor (b7, tweak7);
1924
1925 #define DO_ROUND(r) \
1926 rkey = ALIGNED_LOAD (rk, r); \
1927 b0 = asm_cipher_be (b0, rkey); \
1928 b1 = asm_cipher_be (b1, rkey); \
1929 b2 = asm_cipher_be (b2, rkey); \
1930 b3 = asm_cipher_be (b3, rkey); \
1931 b4 = asm_cipher_be (b4, rkey); \
1932 b5 = asm_cipher_be (b5, rkey); \
1933 b6 = asm_cipher_be (b6, rkey); \
1934 b7 = asm_cipher_be (b7, rkey);
1935
1936 DO_ROUND(1);
1937 DO_ROUND(2);
1938 DO_ROUND(3);
1939 DO_ROUND(4);
1940 DO_ROUND(5);
1941 DO_ROUND(6);
1942 DO_ROUND(7);
1943
1944 rkeylf = asm_xor (rkeylast, rkey0);
1945
1946 DO_ROUND(8);
1947
1948 tweak0 = asm_xor (tweak0, rkeylf);
1949 tweak1 = asm_xor (tweak1, rkeylf);
1950 tweak2 = asm_xor (tweak2, rkeylf);
1951 tweak3 = asm_xor (tweak3, rkeylf);
1952 tweak4 = asm_xor (tweak4, rkeylf);
1953 tweak5 = asm_xor (tweak5, rkeylf);
1954 tweak6 = asm_xor (tweak6, rkeylf);
1955 tweak7 = asm_xor (tweak7, rkeylf);
1956
1957 DO_ROUND(9);
1958 if (rounds >= 12)
1959 {
1960 DO_ROUND(10);
1961 DO_ROUND(11);
1962 if (rounds > 12)
1963 {
1964 DO_ROUND(12);
1965 DO_ROUND(13);
1966 }
1967 }
1968
1969 #undef DO_ROUND
1970
1971 b0 = asm_cipherlast_be (b0, tweak0);
1972 b1 = asm_cipherlast_be (b1, tweak1);
1973 b2 = asm_cipherlast_be (b2, tweak2);
1974 b3 = asm_cipherlast_be (b3, tweak3);
1975 b0 = VEC_BE_SWAP (b0, bige_const);
1976 b1 = VEC_BE_SWAP (b1, bige_const);
1977 b4 = asm_cipherlast_be (b4, tweak4);
1978 b5 = asm_cipherlast_be (b5, tweak5);
1979 b2 = VEC_BE_SWAP (b2, bige_const);
1980 b3 = VEC_BE_SWAP (b3, bige_const);
1981 b6 = asm_cipherlast_be (b6, tweak6);
1982 b7 = asm_cipherlast_be (b7, tweak7);
1983 VEC_STORE_BE_NOSWAP (out, 0, b0);
1984 VEC_STORE_BE_NOSWAP (out, 1, b1);
1985 b4 = VEC_BE_SWAP (b4, bige_const);
1986 b5 = VEC_BE_SWAP (b5, bige_const);
1987 VEC_STORE_BE_NOSWAP (out, 2, b2);
1988 VEC_STORE_BE_NOSWAP (out, 3, b3);
1989 b6 = VEC_BE_SWAP (b6, bige_const);
1990 b7 = VEC_BE_SWAP (b7, bige_const);
1991 VEC_STORE_BE_NOSWAP (out, 4, b4);
1992 VEC_STORE_BE_NOSWAP (out, 5, b5);
1993 VEC_STORE_BE_NOSWAP (out, 6, b6);
1994 VEC_STORE_BE_NOSWAP (out, 7, b7);
1995 out += 8;
1996 }
1997
1998 if (nblocks >= 4)
1999 {
2000 tweak0 = tweak;
2001 GEN_TWEAK (tweak1, tweak0);
2002 GEN_TWEAK (tweak2, tweak1);
2003 GEN_TWEAK (tweak3, tweak2);
2004 GEN_TWEAK (tweak, tweak3);
2005
2006 b0 = VEC_LOAD_BE (in, 0, bige_const);
2007 b1 = VEC_LOAD_BE (in, 1, bige_const);
2008 b2 = VEC_LOAD_BE (in, 2, bige_const);
2009 b3 = VEC_LOAD_BE (in, 3, bige_const);
2010
2011 tweak0 = asm_vperm1 (tweak0, bswap128_const);
2012 tweak1 = asm_vperm1 (tweak1, bswap128_const);
2013 tweak2 = asm_vperm1 (tweak2, bswap128_const);
2014 tweak3 = asm_vperm1 (tweak3, bswap128_const);
2015
2016 b0 ^= tweak0 ^ rkey0;
2017 b1 ^= tweak1 ^ rkey0;
2018 b2 ^= tweak2 ^ rkey0;
2019 b3 ^= tweak3 ^ rkey0;
2020
2021 #define DO_ROUND(r) \
2022 rkey = ALIGNED_LOAD (rk, r); \
2023 b0 = asm_cipher_be (b0, rkey); \
2024 b1 = asm_cipher_be (b1, rkey); \
2025 b2 = asm_cipher_be (b2, rkey); \
2026 b3 = asm_cipher_be (b3, rkey);
2027
2028 DO_ROUND(1);
2029 DO_ROUND(2);
2030 DO_ROUND(3);
2031 DO_ROUND(4);
2032 DO_ROUND(5);
2033 DO_ROUND(6);
2034 DO_ROUND(7);
2035 DO_ROUND(8);
2036 DO_ROUND(9);
2037 if (rounds >= 12)
2038 {
2039 DO_ROUND(10);
2040 DO_ROUND(11);
2041 if (rounds > 12)
2042 {
2043 DO_ROUND(12);
2044 DO_ROUND(13);
2045 }
2046 }
2047
2048 #undef DO_ROUND
2049
2050 rkey = rkeylast;
2051 b0 = asm_cipherlast_be (b0, rkey ^ tweak0);
2052 b1 = asm_cipherlast_be (b1, rkey ^ tweak1);
2053 b2 = asm_cipherlast_be (b2, rkey ^ tweak2);
2054 b3 = asm_cipherlast_be (b3, rkey ^ tweak3);
2055
2056 VEC_STORE_BE (out, 0, b0, bige_const);
2057 VEC_STORE_BE (out, 1, b1, bige_const);
2058 VEC_STORE_BE (out, 2, b2, bige_const);
2059 VEC_STORE_BE (out, 3, b3, bige_const);
2060
2061 in += 4;
2062 out += 4;
2063 nblocks -= 4;
2064 }
2065
2066 for (; nblocks; nblocks--)
2067 {
2068 tweak0 = asm_vperm1 (tweak, bswap128_const);
2069
2070 /* Xor-Encrypt/Decrypt-Xor block. */
2071 b = VEC_LOAD_BE (in, 0, bige_const) ^ tweak0;
2072
2073 /* Generate next tweak. */
2074 GEN_TWEAK (tweak, tweak);
2075
2076 AES_ENCRYPT (b, rounds);
2077
2078 b ^= tweak0;
2079 VEC_STORE_BE (out, 0, b, bige_const);
2080
2081 in++;
2082 out++;
2083 }
2084 }
2085 else
2086 {
2087 const u128_t *rk = (u128_t *)&ctx->keyschdec;
2088
2089 if (!ctx->decryption_prepared)
2090 {
2091 internal_aes_ppc_prepare_decryption (ctx);
2092 ctx->decryption_prepared = 1;
2093 }
2094
2095 PRELOAD_ROUND_KEYS (rounds);
2096
2097 for (; nblocks >= 8; nblocks -= 8)
2098 {
2099 b0 = VEC_LOAD_BE_NOSWAP (in, 0);
2100 b1 = VEC_LOAD_BE_NOSWAP (in, 1);
2101 b2 = VEC_LOAD_BE_NOSWAP (in, 2);
2102 b3 = VEC_LOAD_BE_NOSWAP (in, 3);
2103 tweak0 = tweak;
2104 GEN_TWEAK (tweak1, tweak0);
2105 tweak0 = asm_vperm1 (tweak0, bswap128_const);
2106 b4 = VEC_LOAD_BE_NOSWAP (in, 4);
2107 b5 = VEC_LOAD_BE_NOSWAP (in, 5);
2108 GEN_TWEAK (tweak2, tweak1);
2109 tweak1 = asm_vperm1 (tweak1, bswap128_const);
2110 b6 = VEC_LOAD_BE_NOSWAP (in, 6);
2111 b7 = VEC_LOAD_BE_NOSWAP (in, 7);
2112 in += 8;
2113
2114 b0 = VEC_BE_SWAP(b0, bige_const);
2115 b1 = VEC_BE_SWAP(b1, bige_const);
2116 GEN_TWEAK (tweak3, tweak2);
2117 tweak2 = asm_vperm1 (tweak2, bswap128_const);
2118 GEN_TWEAK (tweak4, tweak3);
2119 tweak3 = asm_vperm1 (tweak3, bswap128_const);
2120 b2 = VEC_BE_SWAP(b2, bige_const);
2121 b3 = VEC_BE_SWAP(b3, bige_const);
2122 GEN_TWEAK (tweak5, tweak4);
2123 tweak4 = asm_vperm1 (tweak4, bswap128_const);
2124 GEN_TWEAK (tweak6, tweak5);
2125 tweak5 = asm_vperm1 (tweak5, bswap128_const);
2126 b4 = VEC_BE_SWAP(b4, bige_const);
2127 b5 = VEC_BE_SWAP(b5, bige_const);
2128 GEN_TWEAK (tweak7, tweak6);
2129 tweak6 = asm_vperm1 (tweak6, bswap128_const);
2130 GEN_TWEAK (tweak, tweak7);
2131 tweak7 = asm_vperm1 (tweak7, bswap128_const);
2132 b6 = VEC_BE_SWAP(b6, bige_const);
2133 b7 = VEC_BE_SWAP(b7, bige_const);
2134
2135 tweak0 = asm_xor (tweak0, rkey0);
2136 tweak1 = asm_xor (tweak1, rkey0);
2137 tweak2 = asm_xor (tweak2, rkey0);
2138 tweak3 = asm_xor (tweak3, rkey0);
2139 tweak4 = asm_xor (tweak4, rkey0);
2140 tweak5 = asm_xor (tweak5, rkey0);
2141 tweak6 = asm_xor (tweak6, rkey0);
2142 tweak7 = asm_xor (tweak7, rkey0);
2143
2144 b0 = asm_xor (b0, tweak0);
2145 b1 = asm_xor (b1, tweak1);
2146 b2 = asm_xor (b2, tweak2);
2147 b3 = asm_xor (b3, tweak3);
2148 b4 = asm_xor (b4, tweak4);
2149 b5 = asm_xor (b5, tweak5);
2150 b6 = asm_xor (b6, tweak6);
2151 b7 = asm_xor (b7, tweak7);
2152
2153 #define DO_ROUND(r) \
2154 rkey = ALIGNED_LOAD (rk, r); \
2155 b0 = asm_ncipher_be (b0, rkey); \
2156 b1 = asm_ncipher_be (b1, rkey); \
2157 b2 = asm_ncipher_be (b2, rkey); \
2158 b3 = asm_ncipher_be (b3, rkey); \
2159 b4 = asm_ncipher_be (b4, rkey); \
2160 b5 = asm_ncipher_be (b5, rkey); \
2161 b6 = asm_ncipher_be (b6, rkey); \
2162 b7 = asm_ncipher_be (b7, rkey);
2163
2164 DO_ROUND(1);
2165 DO_ROUND(2);
2166 DO_ROUND(3);
2167 DO_ROUND(4);
2168 DO_ROUND(5);
2169 DO_ROUND(6);
2170 DO_ROUND(7);
2171
2172 rkeylf = asm_xor (rkeylast, rkey0);
2173
2174 DO_ROUND(8);
2175
2176 tweak0 = asm_xor (tweak0, rkeylf);
2177 tweak1 = asm_xor (tweak1, rkeylf);
2178 tweak2 = asm_xor (tweak2, rkeylf);
2179 tweak3 = asm_xor (tweak3, rkeylf);
2180 tweak4 = asm_xor (tweak4, rkeylf);
2181 tweak5 = asm_xor (tweak5, rkeylf);
2182 tweak6 = asm_xor (tweak6, rkeylf);
2183 tweak7 = asm_xor (tweak7, rkeylf);
2184
2185 DO_ROUND(9);
2186 if (rounds >= 12)
2187 {
2188 DO_ROUND(10);
2189 DO_ROUND(11);
2190 if (rounds > 12)
2191 {
2192 DO_ROUND(12);
2193 DO_ROUND(13);
2194 }
2195 }
2196
2197 #undef DO_ROUND
2198
2199 b0 = asm_ncipherlast_be (b0, tweak0);
2200 b1 = asm_ncipherlast_be (b1, tweak1);
2201 b2 = asm_ncipherlast_be (b2, tweak2);
2202 b3 = asm_ncipherlast_be (b3, tweak3);
2203 b0 = VEC_BE_SWAP (b0, bige_const);
2204 b1 = VEC_BE_SWAP (b1, bige_const);
2205 b4 = asm_ncipherlast_be (b4, tweak4);
2206 b5 = asm_ncipherlast_be (b5, tweak5);
2207 b2 = VEC_BE_SWAP (b2, bige_const);
2208 b3 = VEC_BE_SWAP (b3, bige_const);
2209 b6 = asm_ncipherlast_be (b6, tweak6);
2210 b7 = asm_ncipherlast_be (b7, tweak7);
2211 VEC_STORE_BE_NOSWAP (out, 0, b0);
2212 VEC_STORE_BE_NOSWAP (out, 1, b1);
2213 b4 = VEC_BE_SWAP (b4, bige_const);
2214 b5 = VEC_BE_SWAP (b5, bige_const);
2215 VEC_STORE_BE_NOSWAP (out, 2, b2);
2216 VEC_STORE_BE_NOSWAP (out, 3, b3);
2217 b6 = VEC_BE_SWAP (b6, bige_const);
2218 b7 = VEC_BE_SWAP (b7, bige_const);
2219 VEC_STORE_BE_NOSWAP (out, 4, b4);
2220 VEC_STORE_BE_NOSWAP (out, 5, b5);
2221 VEC_STORE_BE_NOSWAP (out, 6, b6);
2222 VEC_STORE_BE_NOSWAP (out, 7, b7);
2223 out += 8;
2224 }
2225
2226 if (nblocks >= 4)
2227 {
2228 tweak0 = tweak;
2229 GEN_TWEAK (tweak1, tweak0);
2230 GEN_TWEAK (tweak2, tweak1);
2231 GEN_TWEAK (tweak3, tweak2);
2232 GEN_TWEAK (tweak, tweak3);
2233
2234 b0 = VEC_LOAD_BE (in, 0, bige_const);
2235 b1 = VEC_LOAD_BE (in, 1, bige_const);
2236 b2 = VEC_LOAD_BE (in, 2, bige_const);
2237 b3 = VEC_LOAD_BE (in, 3, bige_const);
2238
2239 tweak0 = asm_vperm1 (tweak0, bswap128_const);
2240 tweak1 = asm_vperm1 (tweak1, bswap128_const);
2241 tweak2 = asm_vperm1 (tweak2, bswap128_const);
2242 tweak3 = asm_vperm1 (tweak3, bswap128_const);
2243
2244 b0 ^= tweak0 ^ rkey0;
2245 b1 ^= tweak1 ^ rkey0;
2246 b2 ^= tweak2 ^ rkey0;
2247 b3 ^= tweak3 ^ rkey0;
2248
2249 #define DO_ROUND(r) \
2250 rkey = ALIGNED_LOAD (rk, r); \
2251 b0 = asm_ncipher_be (b0, rkey); \
2252 b1 = asm_ncipher_be (b1, rkey); \
2253 b2 = asm_ncipher_be (b2, rkey); \
2254 b3 = asm_ncipher_be (b3, rkey);
2255
2256 DO_ROUND(1);
2257 DO_ROUND(2);
2258 DO_ROUND(3);
2259 DO_ROUND(4);
2260 DO_ROUND(5);
2261 DO_ROUND(6);
2262 DO_ROUND(7);
2263 DO_ROUND(8);
2264 DO_ROUND(9);
2265 if (rounds >= 12)
2266 {
2267 DO_ROUND(10);
2268 DO_ROUND(11);
2269 if (rounds > 12)
2270 {
2271 DO_ROUND(12);
2272 DO_ROUND(13);
2273 }
2274 }
2275
2276 #undef DO_ROUND
2277
2278 rkey = rkeylast;
2279 b0 = asm_ncipherlast_be (b0, rkey ^ tweak0);
2280 b1 = asm_ncipherlast_be (b1, rkey ^ tweak1);
2281 b2 = asm_ncipherlast_be (b2, rkey ^ tweak2);
2282 b3 = asm_ncipherlast_be (b3, rkey ^ tweak3);
2283
2284 VEC_STORE_BE (out, 0, b0, bige_const);
2285 VEC_STORE_BE (out, 1, b1, bige_const);
2286 VEC_STORE_BE (out, 2, b2, bige_const);
2287 VEC_STORE_BE (out, 3, b3, bige_const);
2288
2289 in += 4;
2290 out += 4;
2291 nblocks -= 4;
2292 }
2293
2294 for (; nblocks; nblocks--)
2295 {
2296 tweak0 = asm_vperm1 (tweak, bswap128_const);
2297
2298 /* Xor-Encrypt/Decrypt-Xor block. */
2299 b = VEC_LOAD_BE (in, 0, bige_const) ^ tweak0;
2300
2301 /* Generate next tweak. */
2302 GEN_TWEAK (tweak, tweak);
2303
2304 AES_DECRYPT (b, rounds);
2305
2306 b ^= tweak0;
2307 VEC_STORE_BE (out, 0, b, bige_const);
2308
2309 in++;
2310 out++;
2311 }
2312 }
2313
2314 tweak = asm_vperm1 (tweak, bswap128_const);
2315 VEC_STORE_BE (tweak_arg, 0, tweak, bige_const);
2316
2317 #undef GEN_TWEAK
2318
2319 clear_vec_regs();
2320 }
2321
2322
2323 void PPC_OPT_ATTR
2324 CTR32LE_ENC_FUNC(void *context, unsigned char *ctr_arg, void *outbuf_arg,
2325 const void *inbuf_arg, size_t nblocks)
2326 {
2327 #ifndef WORDS_BIGENDIAN
2328 static const vec_u32 vec_u32_one = { 1, 0, 0, 0 };
2329 #else
2330 static const vec_u32 vec_u32_one = { 0, 0, 0, 1 };
2331 #endif
2332 const block bige_const = asm_load_be_const();
2333 RIJNDAEL_context *ctx = context;
2334 const u128_t *rk = (u128_t *)&ctx->keyschenc;
2335 const u128_t *in = (const u128_t *)inbuf_arg;
2336 u128_t *out = (u128_t *)outbuf_arg;
2337 int rounds = ctx->rounds;
2338 ROUND_KEY_VARIABLES;
2339 block rkeylast_orig;
2340 block b;
2341 vec_u32 ctr, one;
2342
2343 ctr = (vec_u32)vec_reve (VEC_LOAD_BE (ctr_arg, 0, bige_const));
2344 one = vec_u32_one;
2345
2346 PRELOAD_ROUND_KEYS (rounds);
2347 rkeylast_orig = rkeylast;
2348
2349 #define VEC_ADD_CTRLE32(ctrv_u32, addv_u32) \
2350 vec_reve((block)((ctrv_u32) + (addv_u32)))
2351
2352 if (nblocks >= 4)
2353 {
2354 block in0, in1, in2, in3, in4, in5, in6, in7;
2355 block b0, b1, b2, b3, b4, b5, b6, b7;
2356 vec_u32 two, three, four, five, six, seven, eight;
2357 block rkey;
2358
2359 two = one + one;
2360 three = two + one;
2361 four = two + two;
2362 five = three + two;
2363 six = three + three;
2364 seven = four + three;
2365 eight = four + four;
2366
2367 for (; nblocks >= 8; nblocks -= 8)
2368 {
2369 b1 = VEC_ADD_CTRLE32 (ctr, one);
2370 b2 = VEC_ADD_CTRLE32 (ctr, two);
2371 b3 = VEC_ADD_CTRLE32 (ctr, three);
2372 b4 = VEC_ADD_CTRLE32 (ctr, four);
2373 b5 = VEC_ADD_CTRLE32 (ctr, five);
2374 b6 = VEC_ADD_CTRLE32 (ctr, six);
2375 b7 = VEC_ADD_CTRLE32 (ctr, seven);
2376 b0 = asm_xor (rkey0, vec_reve((block)ctr));
2377 rkey = ALIGNED_LOAD (rk, 1);
2378 ctr = ctr + eight;
2379 b1 = asm_xor (rkey0, b1);
2380 b2 = asm_xor (rkey0, b2);
2381 b3 = asm_xor (rkey0, b3);
2382 b0 = asm_cipher_be (b0, rkey);
2383 b1 = asm_cipher_be (b1, rkey);
2384 b2 = asm_cipher_be (b2, rkey);
2385 b3 = asm_cipher_be (b3, rkey);
2386 b4 = asm_xor (rkey0, b4);
2387 b5 = asm_xor (rkey0, b5);
2388 b6 = asm_xor (rkey0, b6);
2389 b7 = asm_xor (rkey0, b7);
2390 b4 = asm_cipher_be (b4, rkey);
2391 b5 = asm_cipher_be (b5, rkey);
2392 b6 = asm_cipher_be (b6, rkey);
2393 b7 = asm_cipher_be (b7, rkey);
2394
2395 #define DO_ROUND(r) \
2396 rkey = ALIGNED_LOAD (rk, r); \
2397 b0 = asm_cipher_be (b0, rkey); \
2398 b1 = asm_cipher_be (b1, rkey); \
2399 b2 = asm_cipher_be (b2, rkey); \
2400 b3 = asm_cipher_be (b3, rkey); \
2401 b4 = asm_cipher_be (b4, rkey); \
2402 b5 = asm_cipher_be (b5, rkey); \
2403 b6 = asm_cipher_be (b6, rkey); \
2404 b7 = asm_cipher_be (b7, rkey);
2405
2406 in0 = VEC_LOAD_BE_NOSWAP (in, 0);
2407 DO_ROUND(2);
2408 in1 = VEC_LOAD_BE_NOSWAP (in, 1);
2409 DO_ROUND(3);
2410 in2 = VEC_LOAD_BE_NOSWAP (in, 2);
2411 DO_ROUND(4);
2412 in3 = VEC_LOAD_BE_NOSWAP (in, 3);
2413 DO_ROUND(5);
2414 in4 = VEC_LOAD_BE_NOSWAP (in, 4);
2415 DO_ROUND(6);
2416 in5 = VEC_LOAD_BE_NOSWAP (in, 5);
2417 DO_ROUND(7);
2418 in6 = VEC_LOAD_BE_NOSWAP (in, 6);
2419 DO_ROUND(8);
2420 in7 = VEC_LOAD_BE_NOSWAP (in, 7);
2421 in += 8;
2422 DO_ROUND(9);
2423
2424 if (rounds >= 12)
2425 {
2426 DO_ROUND(10);
2427 DO_ROUND(11);
2428 if (rounds > 12)
2429 {
2430 DO_ROUND(12);
2431 DO_ROUND(13);
2432 }
2433 }
2434
2435 #undef DO_ROUND
2436
2437 in0 = VEC_BE_SWAP (in0, bige_const);
2438 in1 = VEC_BE_SWAP (in1, bige_const);
2439 in2 = VEC_BE_SWAP (in2, bige_const);
2440 in3 = VEC_BE_SWAP (in3, bige_const);
2441 in4 = VEC_BE_SWAP (in4, bige_const);
2442 in5 = VEC_BE_SWAP (in5, bige_const);
2443 in6 = VEC_BE_SWAP (in6, bige_const);
2444 in7 = VEC_BE_SWAP (in7, bige_const);
2445
2446 in0 = asm_xor (rkeylast, in0);
2447 in1 = asm_xor (rkeylast, in1);
2448 in2 = asm_xor (rkeylast, in2);
2449 in3 = asm_xor (rkeylast, in3);
2450 b0 = asm_cipherlast_be (b0, in0);
2451 b1 = asm_cipherlast_be (b1, in1);
2452 in4 = asm_xor (rkeylast, in4);
2453 in5 = asm_xor (rkeylast, in5);
2454 b2 = asm_cipherlast_be (b2, in2);
2455 b3 = asm_cipherlast_be (b3, in3);
2456 in6 = asm_xor (rkeylast, in6);
2457 in7 = asm_xor (rkeylast, in7);
2458 b4 = asm_cipherlast_be (b4, in4);
2459 b5 = asm_cipherlast_be (b5, in5);
2460 b6 = asm_cipherlast_be (b6, in6);
2461 b7 = asm_cipherlast_be (b7, in7);
2462
2463 b0 = VEC_BE_SWAP (b0, bige_const);
2464 b1 = VEC_BE_SWAP (b1, bige_const);
2465 b2 = VEC_BE_SWAP (b2, bige_const);
2466 b3 = VEC_BE_SWAP (b3, bige_const);
2467 b4 = VEC_BE_SWAP (b4, bige_const);
2468 b5 = VEC_BE_SWAP (b5, bige_const);
2469 b6 = VEC_BE_SWAP (b6, bige_const);
2470 b7 = VEC_BE_SWAP (b7, bige_const);
2471 VEC_STORE_BE_NOSWAP (out, 0, b0);
2472 VEC_STORE_BE_NOSWAP (out, 1, b1);
2473 VEC_STORE_BE_NOSWAP (out, 2, b2);
2474 VEC_STORE_BE_NOSWAP (out, 3, b3);
2475 VEC_STORE_BE_NOSWAP (out, 4, b4);
2476 VEC_STORE_BE_NOSWAP (out, 5, b5);
2477 VEC_STORE_BE_NOSWAP (out, 6, b6);
2478 VEC_STORE_BE_NOSWAP (out, 7, b7);
2479 out += 8;
2480 }
2481
2482 if (nblocks >= 4)
2483 {
2484 b1 = VEC_ADD_CTRLE32 (ctr, one);
2485 b2 = VEC_ADD_CTRLE32 (ctr, two);
2486 b3 = VEC_ADD_CTRLE32 (ctr, three);
2487 b0 = asm_xor (rkey0, vec_reve((block)ctr));
2488 ctr = ctr + four;
2489 b1 = asm_xor (rkey0, b1);
2490 b2 = asm_xor (rkey0, b2);
2491 b3 = asm_xor (rkey0, b3);
2492
2493 #define DO_ROUND(r) \
2494 rkey = ALIGNED_LOAD (rk, r); \
2495 b0 = asm_cipher_be (b0, rkey); \
2496 b1 = asm_cipher_be (b1, rkey); \
2497 b2 = asm_cipher_be (b2, rkey); \
2498 b3 = asm_cipher_be (b3, rkey);
2499
2500 DO_ROUND(1);
2501 DO_ROUND(2);
2502 DO_ROUND(3);
2503 DO_ROUND(4);
2504 DO_ROUND(5);
2505 DO_ROUND(6);
2506 DO_ROUND(7);
2507 DO_ROUND(8);
2508
2509 in0 = VEC_LOAD_BE (in, 0, bige_const);
2510 in1 = VEC_LOAD_BE (in, 1, bige_const);
2511 in2 = VEC_LOAD_BE (in, 2, bige_const);
2512 in3 = VEC_LOAD_BE (in, 3, bige_const);
2513
2514 DO_ROUND(9);
2515 if (rounds >= 12)
2516 {
2517 DO_ROUND(10);
2518 DO_ROUND(11);
2519 if (rounds > 12)
2520 {
2521 DO_ROUND(12);
2522 DO_ROUND(13);
2523 }
2524 }
2525
2526 #undef DO_ROUND
2527
2528 in0 = asm_xor (rkeylast, in0);
2529 in1 = asm_xor (rkeylast, in1);
2530 in2 = asm_xor (rkeylast, in2);
2531 in3 = asm_xor (rkeylast, in3);
2532
2533 b0 = asm_cipherlast_be (b0, in0);
2534 b1 = asm_cipherlast_be (b1, in1);
2535 b2 = asm_cipherlast_be (b2, in2);
2536 b3 = asm_cipherlast_be (b3, in3);
2537
2538 VEC_STORE_BE (out, 0, b0, bige_const);
2539 VEC_STORE_BE (out, 1, b1, bige_const);
2540 VEC_STORE_BE (out, 2, b2, bige_const);
2541 VEC_STORE_BE (out, 3, b3, bige_const);
2542
2543 in += 4;
2544 out += 4;
2545 nblocks -= 4;
2546 }
2547 }
2548
2549 for (; nblocks; nblocks--)
2550 {
2551 b = vec_reve((block)ctr);
2552 ctr = ctr + one;
2553 rkeylast = rkeylast_orig ^ VEC_LOAD_BE (in, 0, bige_const);
2554
2555 AES_ENCRYPT (b, rounds);
2556
2557 VEC_STORE_BE (out, 0, b, bige_const);
2558
2559 out++;
2560 in++;
2561 }
2562
2563 #undef VEC_ADD_CTRLE32
2564
2565 VEC_STORE_BE (ctr_arg, 0, vec_reve((block)ctr), bige_const);
2566
2567 clear_vec_regs();
2568 }