Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/modes/asm/aes-gcm-ppc.pl

raw
1 #! /usr/bin/env perl
2 # Copyright 2014-2026 The OpenSSL Project Authors. All Rights Reserved.
3 # Copyright 2025- IBM Corp. All rights reserved
4 #
5 # Licensed under the Apache License 2.0 (the "License"). You may not use
6 # this file except in compliance with the License. You can obtain a copy
7 # in the file LICENSE in the source distribution or at
8 # https://www.openssl.org/source/license.html
9 #
10 #===================================================================================
11 # Accelerated AES-GCM stitched implementation for ppc64le.
12 #
13 # Written by Danny Tsen <[email protected]>
14 #
15 # GHASH is based on the Karatsuba multiplication method.
16 #
17 # Xi xor X1
18 #
19 # X1 * H^4 + X2 * H^3 + x3 * H^2 + X4 * H =
20 # (X1.h * H4.h + xX.l * H4.l + X1 * H4) +
21 # (X2.h * H3.h + X2.l * H3.l + X2 * H3) +
22 # (X3.h * H2.h + X3.l * H2.l + X3 * H2) +
23 # (X4.h * H.h + X4.l * H.l + X4 * H)
24 #
25 # Xi = v0
26 # H Poly = v2
27 # Hash keys = v3 - v14
28 # ( H.l, H, H.h)
29 # ( H^2.l, H^2, H^2.h)
30 # ( H^3.l, H^3, H^3.h)
31 # ( H^4.l, H^4, H^4.h)
32 #
33 # v30 is IV
34 # v31 - counter 1
35 #
36 # AES used,
37 # vs0 - round key 0
38 # v15, v16, v17, v18, v19, v20, v21, v22 for 8 blocks (encrypted)
39 #
40 # This implementation uses stitched AES-GCM approach to improve overall performance.
41 # AES is implemented with 8x blocks and GHASH is using 2 4x blocks.
42 #
43 # ===================================================================================
44 #
45 use strict;
46 use warnings;
47
48 # $output is the last argument if it looks like a file (it has an extension)
49 # $flavour is the first argument if it doesn't look like a file
50 my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
51 my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
52
53 $output and open STDOUT,">$output";
54
55 my $code.=<<___;
56 .machine "any"
57 .text
58
59 .macro SAVE_REGS
60 mflr 0
61 std 0, 16(1)
62 stdu 1,-512(1)
63
64 std 14, 112(1)
65 std 15, 120(1)
66 std 16, 128(1)
67 std 17, 136(1)
68 std 18, 144(1)
69 std 19, 152(1)
70 std 20, 160(1)
71 std 21, 168(1)
72 std 22, 176(1)
73 std 23, 184(1)
74 std 24, 192(1)
75
76 stxv 32+20, 256(1)
77 stxv 32+21, 256+16(1)
78 stxv 32+22, 256+32(1)
79 stxv 32+23, 256+48(1)
80 stxv 32+24, 256+64(1)
81 stxv 32+25, 256+80(1)
82 stxv 32+26, 256+96(1)
83 stxv 32+27, 256+112(1)
84 stxv 32+28, 256+128(1)
85 stxv 32+29, 256+144(1)
86 stxv 32+30, 256+160(1)
87 stxv 32+31, 256+176(1)
88 .endm # SAVE_REGS
89
90 .macro RESTORE_REGS
91 lxv 32+20, 256(1)
92 lxv 32+21, 256+16(1)
93 lxv 32+22, 256+32(1)
94 lxv 32+23, 256+48(1)
95 lxv 32+24, 256+64(1)
96 lxv 32+25, 256+80(1)
97 lxv 32+26, 256+96(1)
98 lxv 32+27, 256+112(1)
99 lxv 32+28, 256+128(1)
100 lxv 32+29, 256+144(1)
101 lxv 32+30, 256+160(1)
102 lxv 32+31, 256+176(1)
103
104 ld 14, 112(1)
105 ld 15, 120(1)
106 ld 16, 128(1)
107 ld 17, 136(1)
108 ld 18, 144(1)
109 ld 19, 152(1)
110 ld 20, 160(1)
111 ld 21, 168(1)
112 ld 22, 176(1)
113 ld 23, 184(1)
114 ld 24, 192(1)
115
116 addi 1, 1, 512
117 ld 0, 16(1)
118 mtlr 0
119 .endm # RESTORE_REGS
120
121 # 4x loops
122 .macro AES_CIPHER_4x r
123 vcipher 15, 15, \\r
124 vcipher 16, 16, \\r
125 vcipher 17, 17, \\r
126 vcipher 18, 18, \\r
127 .endm
128
129 # 8x loops
130 .macro AES_CIPHER_8x r
131 vcipher 15, 15, \\r
132 vcipher 16, 16, \\r
133 vcipher 17, 17, \\r
134 vcipher 18, 18, \\r
135 vcipher 19, 19, \\r
136 vcipher 20, 20, \\r
137 vcipher 21, 21, \\r
138 vcipher 22, 22, \\r
139 .endm
140
141 .macro LOOP_8AES_STATE
142 AES_CIPHER_8x 23
143 AES_CIPHER_8x 24
144 AES_CIPHER_8x 25
145 AES_CIPHER_8x 26
146 AES_CIPHER_8x 27
147 AES_CIPHER_8x 28
148 AES_CIPHER_8x 29
149 AES_CIPHER_8x 1
150 .endm
151
152 #
153 # PPC_GFMUL128_8x: Compute hash values of 8 blocks based on Karatsuba method.
154 #
155 # S1 should xor with the previous digest
156 #
157 # Xi = v0
158 # H Poly = v2
159 # Hash keys = v3 - v14
160 # vs10: vpermxor vector
161 # Scratch: v23 - v29
162 #
163 .macro PPC_GFMUL128_8x
164
165 vpmsumd 23, 12, 15 # H4.L * X.L
166 vpmsumd 24, 9, 16
167 vpmsumd 25, 6, 17
168 vpmsumd 26, 3, 18
169
170 vxor 23, 23, 24
171 vxor 23, 23, 25
172 vxor 23, 23, 26 # L
173
174 vpmsumd 27, 13, 15 # H4.L * X.H + H4.H * X.L
175 vpmsumd 28, 10, 16 # H3.L * X1.H + H3.H * X1.L
176 vpmsumd 25, 7, 17
177 vpmsumd 26, 4, 18
178
179 vxor 24, 27, 28
180 vxor 24, 24, 25
181 vxor 24, 24, 26 # M
182
183 vpmsumd 26, 14, 15 # H4.H * X.H
184 vpmsumd 27, 11, 16
185 vpmsumd 28, 8, 17
186 vpmsumd 29, 5, 18
187
188 vxor 26, 26, 27
189 vxor 26, 26, 28
190 vxor 26, 26, 29
191
192 # sum hash and reduction with H Poly
193 vpmsumd 28, 23, 2 # reduction
194
195 vxor 1, 1, 1
196 vsldoi 25, 24, 1, 8 # mL
197 vsldoi 1, 1, 24, 8 # mH
198 vxor 23, 23, 25 # mL + L
199
200 # This performs swap and xor like,
201 # vsldoi 23, 23, 23, 8 # swap
202 # vxor 23, 23, 28
203 xxlor 32+29, 10, 10
204 vpermxor 23, 23, 28, 29
205
206 vxor 24, 26, 1 # H
207
208 # sum hash and reduction with H Poly
209 #
210 # vsldoi 25, 23, 23, 8 # swap
211 # vpmsumd 23, 23, 2
212 # vxor 27, 25, 24
213 #
214 vpermxor 27, 23, 24, 29
215 vpmsumd 23, 23, 2
216 vxor 0, 23, 27 # Digest of 4 blocks
217
218 vxor 19, 19, 0
219
220 # Compute digest for the next 4 blocks
221 vpmsumd 24, 9, 20
222 vpmsumd 25, 6, 21
223 vpmsumd 26, 3, 22
224 vpmsumd 23, 12, 19 # H4.L * X.L
225
226 vxor 23, 23, 24
227 vxor 23, 23, 25
228 vxor 23, 23, 26 # L
229
230 vpmsumd 27, 13, 19 # H4.L * X.H + H4.H * X.L
231 vpmsumd 28, 10, 20 # H3.L * X1.H + H3.H * X1.L
232 vpmsumd 25, 7, 21
233 vpmsumd 26, 4, 22
234
235 vxor 24, 27, 28
236 vxor 24, 24, 25
237 vxor 24, 24, 26 # M
238
239 vpmsumd 26, 14, 19 # H4.H * X.H
240 vpmsumd 27, 11, 20
241 vpmsumd 28, 8, 21
242 vpmsumd 29, 5, 22
243
244 vxor 26, 26, 27
245 vxor 26, 26, 28
246 vxor 26, 26, 29
247
248 # sum hash and reduction with H Poly
249 vpmsumd 28, 23, 2 # reduction
250
251 vxor 1, 1, 1
252 vsldoi 25, 24, 1, 8 # mL
253 vsldoi 1, 1, 24, 8 # mH
254 vxor 23, 23, 25 # mL + L
255
256 # This performs swap and xor like,
257 # vsldoi 23, 23, 23, 8 # swap
258 # vxor 23, 23, 28
259 xxlor 32+29, 10, 10
260 vpermxor 23, 23, 28, 29
261
262 vxor 24, 26, 1 # H
263
264 # sum hash and reduction with H Poly
265 #
266 # vsldoi 25, 23, 23, 8 # swap
267 # vpmsumd 23, 23, 2
268 # vxor 27, 25, 24
269 #
270 vpermxor 27, 23, 24, 29
271 vpmsumd 23, 23, 2
272 vxor 0, 23, 27 # Digest of 8 blocks
273 .endm
274
275 #
276 # Compute update single ghash
277 # vs10: vpermxor vector
278 # scratch: v1, v22..v27
279 #
280 .macro PPC_GHASH1x H S1
281
282 vxor 1, 1, 1
283
284 vpmsumd 22, 3, \\S1 # L
285 vpmsumd 23, 4, \\S1 # M
286 vpmsumd 24, 5, \\S1 # H
287
288 vpmsumd 27, 22, 2 # reduction
289
290 vsldoi 25, 23, 1, 8 # mL
291 vsldoi 26, 1, 23, 8 # mH
292 vxor 22, 22, 25 # LL + LL
293 vxor 24, 24, 26 # HH + HH
294
295 xxlor 32+25, 10, 10
296 vpermxor 22, 22, 27, 25
297
298 # vsldoi 23, 22, 22, 8 # swap
299 # vpmsumd 22, 22, 2 # reduction
300 # vxor 23, 23, 24
301 vpermxor 23, 22, 24, 25
302 vpmsumd 22, 22, 2 # reduction
303
304 vxor \\H, 22, 23
305 .endm
306
307 #
308 # LOAD_HASH_TABLE
309 # Xi = v0
310 # H Poly = v2
311 # Hash keys = v3 - v14
312 #
313 .macro LOAD_HASH_TABLE
314 # Load Xi
315 lxvb16x 32, 0, 8 # load Xi
316
317 vxor 1, 1, 1
318
319 li 10, 32
320 lxvd2x 2+32, 10, 8 # H Poli
321
322 # load Hash - h^4, h^3, h^2, h
323 li 10, 64
324 lxvd2x 4+32, 10, 8 # H
325 vsldoi 3, 1, 4, 8 # l
326 vsldoi 5, 4, 1, 8 # h
327 li 10, 112
328 lxvd2x 7+32, 10, 8 # H^2
329 vsldoi 6, 1, 7, 8 # l
330 vsldoi 8, 7, 1, 8 # h
331 li 10, 160
332 lxvd2x 10+32, 10, 8 # H^3
333 vsldoi 9, 1, 10, 8 # l
334 vsldoi 11, 10, 1, 8 # h
335 li 10, 208
336 lxvd2x 13+32, 10, 8 # H^4
337 vsldoi 12, 1, 13, 8 # l
338 vsldoi 14, 13, 1, 8 # h
339 .endm
340
341 .macro PROCESS_8X_AES_STATES
342 vcipherlast 15, 15, 1
343 vcipherlast 16, 16, 1
344 vcipherlast 17, 17, 1
345 vcipherlast 18, 18, 1
346 vcipherlast 19, 19, 1
347 vcipherlast 20, 20, 1
348 vcipherlast 21, 21, 1
349 vcipherlast 22, 22, 1
350
351 lxvb16x 32+23, 0, 14 # load block
352 lxvb16x 32+24, 15, 14 # load block
353 lxvb16x 32+25, 16, 14 # load block
354 lxvb16x 32+26, 17, 14 # load block
355 lxvb16x 32+27, 18, 14 # load block
356 lxvb16x 32+28, 19, 14 # load block
357 lxvb16x 32+29, 20, 14 # load block
358 lxvb16x 32+30, 21, 14 # load block
359 addi 14, 14, 128
360
361 vxor 15, 15, 23
362 vxor 16, 16, 24
363 vxor 17, 17, 25
364 vxor 18, 18, 26
365 vxor 19, 19, 27
366 vxor 20, 20, 28
367 vxor 21, 21, 29
368 vxor 22, 22, 30
369
370 stxvb16x 47, 0, 9 # store output
371 stxvb16x 48, 15, 9 # store output
372 stxvb16x 49, 16, 9 # store output
373 stxvb16x 50, 17, 9 # store output
374 stxvb16x 51, 18, 9 # store output
375 stxvb16x 52, 19, 9 # store output
376 stxvb16x 53, 20, 9 # store output
377 stxvb16x 54, 21, 9 # store output
378 addi 9, 9, 128
379 .endm
380
381 .macro COMPUTE_STATES
382 xxlor 32+15, 9, 9 # last state
383 vadduwm 15, 15, 31 # state + counter
384 vadduwm 16, 15, 31
385 vadduwm 17, 16, 31
386 vadduwm 18, 17, 31
387 vadduwm 19, 18, 31
388 vadduwm 20, 19, 31
389 vadduwm 21, 20, 31
390 vadduwm 22, 21, 31
391 xxlor 9, 32+22, 32+22 # save last state
392
393 xxlxor 32+15, 32+15, 0 # IV + round key - add round key 0
394 xxlxor 32+16, 32+16, 0
395 xxlxor 32+17, 32+17, 0
396 xxlxor 32+18, 32+18, 0
397 xxlxor 32+19, 32+19, 0
398 xxlxor 32+20, 32+20, 0
399 xxlxor 32+21, 32+21, 0
400 xxlxor 32+22, 32+22, 0
401 .endm
402
403 ################################################################################
404 # Compute AES and ghash one block at a time.
405 # r23: AES rounds
406 # v30: current IV
407 # vs0: roundkey 0
408 #
409 ################################################################################
410 .align 4
411 aes_gcm_crypt_1x:
412
413 cmpdi 5, 16
414 bge __More_1x
415 blr
416 __More_1x:
417 li 10, 16
418 divdu 12, 5, 10
419
420 xxlxor 32+15, 32+30, 0
421
422 # Pre-load 8 AES rounds to scratch vectors.
423 lxv 32+16, 16(6) # round key 1
424 lxv 32+17, 32(6) # round key 2
425 lxv 32+18, 48(6) # round key 3
426 lxv 32+19, 64(6) # round key 4
427 lxv 32+20, 80(6) # round key 5
428 lxv 32+21, 96(6) # round key 6
429 lxv 32+28, 112(6) # round key 7
430 lxv 32+29, 128(6) # round key 8
431
432 lwz 23, 240(6) # n rounds
433 addi 22, 23, -9 # remaining AES rounds
434
435 cmpdi 12, 0
436 bgt __Loop_1x
437 blr
438
439 __Loop_1x:
440 mtctr 22
441 addi 10, 6, 144
442 vcipher 15, 15, 16
443 vcipher 15, 15, 17
444 vcipher 15, 15, 18
445 vcipher 15, 15, 19
446 vcipher 15, 15, 20
447 vcipher 15, 15, 21
448 vcipher 15, 15, 28
449 vcipher 15, 15, 29
450
451 __Loop_aes_1state:
452 lxv 32+1, 0(10)
453 vcipher 15, 15, 1
454 addi 10, 10, 16
455 bdnz __Loop_aes_1state
456 lxv 32+1, 0(10) # last round key
457 lxvb16x 11, 0, 14 # load input block
458 vcipherlast 15, 15, 1
459
460 xxlxor 32+15, 32+15, 11
461 stxvb16x 32+15, 0, 9 # store output
462 addi 14, 14, 16
463 addi 9, 9, 16
464
465 cmpdi 24, 0 # decrypt?
466 bne __Encrypt_1x
467 xxlor 15+32, 11, 11
468 __Encrypt_1x:
469 vxor 15, 15, 0
470 PPC_GHASH1x 0, 15
471
472 addi 5, 5, -16
473 addi 11, 11, 16
474
475 vadduwm 30, 30, 31 # IV + counter
476 xxlxor 32+15, 32+30, 0
477 addi 12, 12, -1
478 cmpdi 12, 0
479 bgt __Loop_1x
480
481 stxvb16x 32+0, 0, 8 # update Xi
482 blr
483 .size aes_gcm_crypt_1x,.-aes_gcm_crypt_1x
484
485 ################################################################################
486 # Process a normal partial block when we come here.
487 # Compute partial mask, Load and store partial block to stack.
488 # Compute AES state.
489 # Compute ghash.
490 #
491 ################################################################################
492 .align 4
493 __Process_partial:
494
495 # create partial mask
496 vspltisb 16, -1
497 li 12, 16
498 sub 12, 12, 5
499 sldi 12, 12, 3
500 mtvsrdd 32+17, 0, 12
501 vslo 16, 16, 17 # partial block mask
502
503 lxvb16x 11, 0, 14 # load partial block
504 xxland 11, 11, 32+16
505
506 # AES crypt partial
507 xxlxor 32+15, 32+30, 0
508 lwz 23, 240(6) # n rounds
509 addi 22, 23, -1 # loop - 1
510 mtctr 22
511 addi 10, 6, 16
512
513 __Loop_aes_pstate:
514 lxv 32+1, 0(10)
515 vcipher 15, 15, 1
516 addi 10, 10, 16
517 bdnz __Loop_aes_pstate
518 lxv 32+1, 0(10) # last round key
519 vcipherlast 15, 15, 1
520
521 xxlxor 32+15, 32+15, 11
522 vand 15, 15, 16
523
524 # AES crypt output v15
525 # Write partial
526 li 10, 224
527 stxvb16x 15+32, 10, 1 # write v15 to stack
528 addi 10, 1, 223
529 addi 12, 9, -1
530 mtctr 5 # partial block len
531 __Write_partial:
532 lbzu 22, 1(10)
533 stbu 22, 1(12)
534 bdnz __Write_partial
535
536 cmpdi 24, 0 # decrypt?
537 bne __Encrypt_partial
538 xxlor 32+15, 11, 11 # decrypt using the input block
539 __Encrypt_partial:
540 vxor 15, 15, 0 # ^ previous hash
541 PPC_GHASH1x 0, 15
542 li 5, 0 # done last byte
543 stxvb16x 32+0, 0, 8 # Update X1
544 blr
545 .size __Process_partial,.-__Process_partial
546
547 ################################################################################
548 # ppc_aes_gcm_encrypt (const void *inp, void *out, size_t len,
549 # const char *rk, unsigned char iv[16], void *Xip);
550 #
551 # r3 - inp
552 # r4 - out
553 # r5 - len
554 # r6 - AES round keys
555 # r7 - iv
556 # r8 - Xi, HPoli, hash keys
557 #
558 # rounds is at offset 240 in rk
559 # Xi is at 0 in gcm_table (Xip).
560 #
561 ################################################################################
562 .global ppc_aes_gcm_encrypt
563 .align 5
564 ppc_aes_gcm_encrypt:
565
566 SAVE_REGS
567 LOAD_HASH_TABLE
568
569 # initialize ICB: GHASH( IV ), IV - r7
570 lxvb16x 30+32, 0, 7 # load IV - v30
571
572 mr 14, 3
573 mr 9, 4
574
575 # counter 1
576 vxor 31, 31, 31
577 vspltisb 22, 1
578 vsldoi 31, 31, 22,1 # counter 1
579
580 addis 11, 2, permx\@toc\@ha
581 addi 11, 11, permx\@toc\@l
582 lxv 10, 0(11) # vs10: vpermxor vector
583 li 11, 0
584
585 lxv 0, 0(6) # round key 0
586
587 #
588 # Process different blocks
589 #
590 cmpdi 5, 128
591 blt __Process_more_enc
592
593 # load 9 round keys
594 lxv 32+23, 16(6) # round key 1
595 lxv 32+24, 32(6) # round key 2
596 lxv 32+25, 48(6) # round key 3
597 lxv 32+26, 64(6) # round key 4
598 lxv 32+27, 80(6) # round key 5
599 lxv 32+28, 96(6) # round key 6
600 lxv 32+29, 112(6) # round key 7
601 lxv 32+1, 128(6) # round key 8
602
603 # load rounds - 10 (128), 12 (192), 14 (256)
604 lwz 23, 240(6) # n rounds
605
606 __Process_encrypt:
607 #
608 # Process 8x AES/GCM blocks
609 #
610 __Process_8x_enc:
611 # 8x blocks
612 li 10, 128
613 divdu 12, 5, 10 # n 128 bytes-blocks
614
615 addi 12, 12, -1 # loop - 1
616
617 vmr 15, 30 # first state: IV
618 vadduwm 16, 15, 31 # state + counter
619 vadduwm 17, 16, 31
620 vadduwm 18, 17, 31
621 vadduwm 19, 18, 31
622 vadduwm 20, 19, 31
623 vadduwm 21, 20, 31
624 vadduwm 22, 21, 31
625 xxlor 9, 32+22, 32+22 # save last state
626
627 # vxor state, state, w # addroundkey
628 xxlxor 32+15, 32+15, 0 # IV + round key - add round key 0
629 xxlxor 32+16, 32+16, 0
630 xxlxor 32+17, 32+17, 0
631 xxlxor 32+18, 32+18, 0
632 xxlxor 32+19, 32+19, 0
633 xxlxor 32+20, 32+20, 0
634 xxlxor 32+21, 32+21, 0
635 xxlxor 32+22, 32+22, 0
636
637 li 15, 16
638 li 16, 32
639 li 17, 48
640 li 18, 64
641 li 19, 80
642 li 20, 96
643 li 21, 112
644
645 #
646 # Pre-compute first 8 AES state and leave 1/3/5 more rounds
647 # for the loop.
648 #
649 addi 22, 23, -9 # process 8 keys
650 mtctr 22 # AES key loop
651 addi 10, 6, 144
652
653 LOOP_8AES_STATE # process 8 AES keys
654
655 __PreLoop_aes_state:
656 lxv 32+1, 0(10) # round key
657 AES_CIPHER_8x 1
658 addi 10, 10, 16
659 bdnz __PreLoop_aes_state
660 lxv 32+1, 0(10) # last round key (v1)
661
662 cmpdi 12, 0 # Only one loop (8 block)
663 beq __Finish_ghash
664
665 #
666 # Loop 8x blocks and compute ghash
667 #
668 __Loop_8x_block_enc:
669 PROCESS_8X_AES_STATES
670
671 # Compute ghash here
672 vxor 15, 15, 0
673 PPC_GFMUL128_8x
674
675 COMPUTE_STATES
676
677 addi 5, 5, -128
678 addi 11, 11, 128
679
680 lxv 32+23, 16(6) # round key 1
681 lxv 32+24, 32(6) # round key 2
682 lxv 32+25, 48(6) # round key 3
683 lxv 32+26, 64(6) # round key 4
684 lxv 32+27, 80(6) # round key 5
685 lxv 32+28, 96(6) # round key 6
686 lxv 32+29, 112(6) # round key 7
687 lxv 32+1, 128(6) # round key 8
688
689 # Compute first 8 AES state and leave 1/3/5 more rounds
690 # for the loop.
691 LOOP_8AES_STATE # process 8 AES keys
692 mtctr 22 # AES key loop
693 addi 10, 6, 144
694
695 __LastLoop_aes_state:
696 lxv 32+1, 0(10) # round key
697 AES_CIPHER_8x 1
698 addi 10, 10, 16
699 bdnz __LastLoop_aes_state
700
701 lxv 32+1, 0(10) # last round key (v1)
702
703 addi 12, 12, -1
704 cmpdi 12, 0
705 bne __Loop_8x_block_enc
706
707 #
708 # Remainng blocks
709 #
710 __Finish_ghash:
711 PROCESS_8X_AES_STATES
712
713 # Compute ghash here
714 vxor 15, 15, 0
715 PPC_GFMUL128_8x
716
717 # Update IV and Xi
718 xxlor 30+32, 9, 9 # last ctr
719 vadduwm 30, 30, 31 # increase ctr
720 stxvb16x 32+0, 0, 8 # update Xi
721
722 addi 5, 5, -128
723 addi 11, 11, 128
724
725 #
726 # Done 8x blocks
727 #
728
729 cmpdi 5, 0
730 beq aes_gcm_out
731
732 __Process_more_enc:
733 li 24, 1 # encrypt
734 bl aes_gcm_crypt_1x
735 cmpdi 5, 0
736 beq aes_gcm_out
737
738 bl __Process_partial
739 b aes_gcm_out
740
741 .size ppc_aes_gcm_encrypt,.-ppc_aes_gcm_encrypt
742
743 ################################################################################
744 # ppc_aes_gcm_decrypt (const void *inp, void *out, size_t len,
745 # const char *rk, unsigned char iv[16], void *Xip);
746 # 8x Decrypt
747 #
748 ################################################################################
749 .global ppc_aes_gcm_decrypt
750 .align 5
751 ppc_aes_gcm_decrypt:
752
753 SAVE_REGS
754 LOAD_HASH_TABLE
755
756 # initialize ICB: GHASH( IV ), IV - r7
757 lxvb16x 30+32, 0, 7 # load IV - v30
758
759 mr 14, 3
760 mr 9, 4
761
762 # counter 1
763 vxor 31, 31, 31
764 vspltisb 22, 1
765 vsldoi 31, 31, 22,1 # counter 1
766
767 addis 11, 2, permx\@toc\@ha
768 addi 11, 11, permx\@toc\@l
769 lxv 10, 0(11) # vs10: vpermxor vector
770 li 11, 0
771
772 lxv 0, 0(6) # round key 0
773
774 #
775 # Process different blocks
776 #
777 cmpdi 5, 128
778 blt __Process_more_dec
779
780 # load 9 round keys
781 lxv 32+23, 16(6) # round key 1
782 lxv 32+24, 32(6) # round key 2
783 lxv 32+25, 48(6) # round key 3
784 lxv 32+26, 64(6) # round key 4
785 lxv 32+27, 80(6) # round key 5
786 lxv 32+28, 96(6) # round key 6
787 lxv 32+29, 112(6) # round key 7
788 lxv 32+1, 128(6) # round key 8
789
790 # load rounds - 10 (128), 12 (192), 14 (256)
791 lwz 23, 240(6) # n rounds
792
793 __Process_decrypt:
794 #
795 # Process 8x AES/GCM blocks
796 #
797 __Process_8x_dec:
798 # 8x blocks
799 li 10, 128
800 divdu 12, 5, 10 # n 128 bytes-blocks
801
802 addi 12, 12, -1 # loop - 1
803
804 vmr 15, 30 # first state: IV
805 vadduwm 16, 15, 31 # state + counter
806 vadduwm 17, 16, 31
807 vadduwm 18, 17, 31
808 vadduwm 19, 18, 31
809 vadduwm 20, 19, 31
810 vadduwm 21, 20, 31
811 vadduwm 22, 21, 31
812 xxlor 9, 32+22, 32+22 # save last state
813
814 # vxor state, state, w # addroundkey
815 xxlxor 32+15, 32+15, 0 # IV + round key - add round key 0
816 xxlxor 32+16, 32+16, 0
817 xxlxor 32+17, 32+17, 0
818 xxlxor 32+18, 32+18, 0
819 xxlxor 32+19, 32+19, 0
820 xxlxor 32+20, 32+20, 0
821 xxlxor 32+21, 32+21, 0
822 xxlxor 32+22, 32+22, 0
823
824 li 15, 16
825 li 16, 32
826 li 17, 48
827 li 18, 64
828 li 19, 80
829 li 20, 96
830 li 21, 112
831
832 #
833 # Pre-compute first 8 AES state and leave 1/3/5 more rounds
834 # for the loop.
835 #
836 addi 22, 23, -9 # process 8 keys
837 mtctr 22 # AES key loop
838 addi 10, 6, 144
839
840 LOOP_8AES_STATE # process 8 AES keys
841
842 __PreLoop_aes_state_dec:
843 lxv 32+1, 0(10) # round key
844 AES_CIPHER_8x 1
845 addi 10, 10, 16
846 bdnz __PreLoop_aes_state_dec
847 lxv 32+1, 0(10) # last round key (v1)
848
849 cmpdi 12, 0 # Only one loop (8 block)
850 beq __Finish_ghash_dec
851
852 #
853 # Loop 8x blocks and compute ghash
854 #
855 __Loop_8x_block_dec:
856 vcipherlast 15, 15, 1
857 vcipherlast 16, 16, 1
858 vcipherlast 17, 17, 1
859 vcipherlast 18, 18, 1
860 vcipherlast 19, 19, 1
861 vcipherlast 20, 20, 1
862 vcipherlast 21, 21, 1
863 vcipherlast 22, 22, 1
864
865 lxvb16x 32+23, 0, 14 # load block
866 lxvb16x 32+24, 15, 14 # load block
867 lxvb16x 32+25, 16, 14 # load block
868 lxvb16x 32+26, 17, 14 # load block
869 lxvb16x 32+27, 18, 14 # load block
870 lxvb16x 32+28, 19, 14 # load block
871 lxvb16x 32+29, 20, 14 # load block
872 lxvb16x 32+30, 21, 14 # load block
873 addi 14, 14, 128
874
875 vxor 15, 15, 23
876 vxor 16, 16, 24
877 vxor 17, 17, 25
878 vxor 18, 18, 26
879 vxor 19, 19, 27
880 vxor 20, 20, 28
881 vxor 21, 21, 29
882 vxor 22, 22, 30
883
884 stxvb16x 47, 0, 9 # store output
885 stxvb16x 48, 15, 9 # store output
886 stxvb16x 49, 16, 9 # store output
887 stxvb16x 50, 17, 9 # store output
888 stxvb16x 51, 18, 9 # store output
889 stxvb16x 52, 19, 9 # store output
890 stxvb16x 53, 20, 9 # store output
891 stxvb16x 54, 21, 9 # store output
892
893 addi 9, 9, 128
894
895 vmr 15, 23
896 vmr 16, 24
897 vmr 17, 25
898 vmr 18, 26
899 vmr 19, 27
900 vmr 20, 28
901 vmr 21, 29
902 vmr 22, 30
903
904 # ghash here
905 vxor 15, 15, 0
906 PPC_GFMUL128_8x
907
908 xxlor 32+15, 9, 9 # last state
909 vadduwm 15, 15, 31 # state + counter
910 vadduwm 16, 15, 31
911 vadduwm 17, 16, 31
912 vadduwm 18, 17, 31
913 vadduwm 19, 18, 31
914 vadduwm 20, 19, 31
915 vadduwm 21, 20, 31
916 vadduwm 22, 21, 31
917 xxlor 9, 32+22, 32+22 # save last state
918
919 xxlor 32+27, 0, 0 # restore roundkey 0
920 vxor 15, 15, 27 # IV + round key - add round key 0
921 vxor 16, 16, 27
922 vxor 17, 17, 27
923 vxor 18, 18, 27
924 vxor 19, 19, 27
925 vxor 20, 20, 27
926 vxor 21, 21, 27
927 vxor 22, 22, 27
928
929 addi 5, 5, -128
930 addi 11, 11, 128
931
932 lxv 32+23, 16(6) # round key 1
933 lxv 32+24, 32(6) # round key 2
934 lxv 32+25, 48(6) # round key 3
935 lxv 32+26, 64(6) # round key 4
936 lxv 32+27, 80(6) # round key 5
937 lxv 32+28, 96(6) # round key 6
938 lxv 32+29, 112(6) # round key 7
939 lxv 32+1, 128(6) # round key 8
940
941 LOOP_8AES_STATE # process 8 AES keys
942 mtctr 22 # AES key loop
943 addi 10, 6, 144
944 __LastLoop_aes_state_dec:
945 lxv 32+1, 0(10) # round key
946 AES_CIPHER_8x 1
947 addi 10, 10, 16
948 bdnz __LastLoop_aes_state_dec
949 lxv 32+1, 0(10) # last round key (v1)
950
951 addi 12, 12, -1
952 cmpdi 12, 0
953 bne __Loop_8x_block_dec
954
955 __Finish_ghash_dec:
956 vcipherlast 15, 15, 1
957 vcipherlast 16, 16, 1
958 vcipherlast 17, 17, 1
959 vcipherlast 18, 18, 1
960 vcipherlast 19, 19, 1
961 vcipherlast 20, 20, 1
962 vcipherlast 21, 21, 1
963 vcipherlast 22, 22, 1
964
965 lxvb16x 32+23, 0, 14 # load block
966 lxvb16x 32+24, 15, 14 # load block
967 lxvb16x 32+25, 16, 14 # load block
968 lxvb16x 32+26, 17, 14 # load block
969 lxvb16x 32+27, 18, 14 # load block
970 lxvb16x 32+28, 19, 14 # load block
971 lxvb16x 32+29, 20, 14 # load block
972 lxvb16x 32+30, 21, 14 # load block
973 addi 14, 14, 128
974
975 vxor 15, 15, 23
976 vxor 16, 16, 24
977 vxor 17, 17, 25
978 vxor 18, 18, 26
979 vxor 19, 19, 27
980 vxor 20, 20, 28
981 vxor 21, 21, 29
982 vxor 22, 22, 30
983
984 stxvb16x 47, 0, 9 # store output
985 stxvb16x 48, 15, 9 # store output
986 stxvb16x 49, 16, 9 # store output
987 stxvb16x 50, 17, 9 # store output
988 stxvb16x 51, 18, 9 # store output
989 stxvb16x 52, 19, 9 # store output
990 stxvb16x 53, 20, 9 # store output
991 stxvb16x 54, 21, 9 # store output
992 addi 9, 9, 128
993
994 vxor 15, 23, 0
995 vmr 16, 24
996 vmr 17, 25
997 vmr 18, 26
998 vmr 19, 27
999 vmr 20, 28
1000 vmr 21, 29
1001 vmr 22, 30
1002
1003 #vxor 15, 15, 0
1004 PPC_GFMUL128_8x
1005
1006 xxlor 30+32, 9, 9 # last ctr
1007 vadduwm 30, 30, 31 # increase ctr
1008 stxvb16x 32+0, 0, 8 # update Xi
1009
1010 addi 5, 5, -128
1011 addi 11, 11, 128
1012
1013 #
1014 # Done 8x blocks
1015 #
1016
1017 cmpdi 5, 0
1018 beq aes_gcm_out
1019
1020 __Process_more_dec:
1021 li 24, 0 # decrypt
1022 bl aes_gcm_crypt_1x
1023 cmpdi 5, 0
1024 beq aes_gcm_out
1025
1026 bl __Process_partial
1027 b aes_gcm_out
1028 .size ppc_aes_gcm_decrypt,.-ppc_aes_gcm_decrypt
1029
1030 aes_gcm_out:
1031
1032 mr 3, 11 # return count
1033
1034 RESTORE_REGS
1035 blr
1036 .size aes_gcm_out,.-aes_gcm_out
1037
1038 .rodata
1039 .align 4
1040 # for vector permute and xor
1041 permx:
1042 .long 0x4c5d6e7f, 0x08192a3b, 0xc4d5e6f7, 0x8091a2b3
1043 ___
1044
1045 print $code;
1046 close STDOUT or die "error closing STDOUT: $!";