1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Serpent Cipher 4-way parallel algorithm (i586/SSE2)
5 * Copyright (C) 2011 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
7 * Based on crypto/serpent.c by
8 * Copyright (C) 2002 Dag Arne Osvik <osvik@ii.uib.no>
9 * 2003 Herbert Valerio Riedel <hvr@gnu.org>
12 #include <linux/linkage.h>
14 .file "serpent-sse2-i586-asm_32.S"
22 /**********************************************************************
24 **********************************************************************/
38 #define get_key(i, j, t) \
39 movd (4*(i)+(j))*4(CTX), t; \
42 #define K(x0, x1, x2, x3, x4, i) \
52 #define LK(x0, x1, x2, x3, x4, i) \
55 psrld $(32 - 13), x4; \
60 psrld $(32 - 3), x4; \
65 psrld $(32 - 1), x4; \
73 psrld $(32 - 7), x4; \
87 psrld $(32 - 5), x4; \
91 psrld $(32 - 22), x4; \
98 #define KL(x0, x1, x2, x3, x4, i) \
99 K(x0, x1, x2, x3, x4, i); \
102 pslld $(32 - 5), x4; \
106 pslld $(32 - 22), x4; \
116 pslld $(32 - 1), x4; \
120 pslld $(32 - 7), x4; \
128 pslld $(32 - 13), x4; \
134 pslld $(32 - 3), x4; \
137 #define S0(x0, x1, x2, x3, x4) \
156 #define S1(x0, x1, x2, x3, x4) \
176 #define S2(x0, x1, x2, x3, x4) \
195 #define S3(x0, x1, x2, x3, x4) \
216 #define S4(x0, x1, x2, x3, x4) \
235 #define S5(x0, x1, x2, x3, x4) \
254 #define S6(x0, x1, x2, x3, x4) \
273 #define S7(x0, x1, x2, x3, x4) \
294 #define SI0(x0, x1, x2, x3, x4) \
313 #define SI1(x0, x1, x2, x3, x4) \
332 #define SI2(x0, x1, x2, x3, x4) \
351 #define SI3(x0, x1, x2, x3, x4) \
371 #define SI4(x0, x1, x2, x3, x4) \
391 #define SI5(x0, x1, x2, x3, x4) \
413 #define SI6(x0, x1, x2, x3, x4) \
432 #define SI7(x0, x1, x2, x3, x4) \
453 #define transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
468 #define read_blocks(in, x0, x1, x2, x3, t0, t1, t2) \
469 movdqu (0*4*4)(in), x0; \
470 movdqu (1*4*4)(in), x1; \
471 movdqu (2*4*4)(in), x2; \
472 movdqu (3*4*4)(in), x3; \
474 transpose_4x4(x0, x1, x2, x3, t0, t1, t2)
476 #define write_blocks(out, x0, x1, x2, x3, t0, t1, t2) \
477 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
479 movdqu x0, (0*4*4)(out); \
480 movdqu x1, (1*4*4)(out); \
481 movdqu x2, (2*4*4)(out); \
482 movdqu x3, (3*4*4)(out);
484 #define xor_blocks(out, x0, x1, x2, x3, t0, t1, t2) \
485 transpose_4x4(x0, x1, x2, x3, t0, t1, t2) \
487 movdqu (0*4*4)(out), t0; \
489 movdqu x0, (0*4*4)(out); \
490 movdqu (1*4*4)(out), t0; \
492 movdqu x1, (1*4*4)(out); \
493 movdqu (2*4*4)(out), t0; \
495 movdqu x2, (2*4*4)(out); \
496 movdqu (3*4*4)(out), t0; \
498 movdqu x3, (3*4*4)(out);
500 SYM_FUNC_START(__serpent_enc_blk_4way)
502 * arg_ctx(%esp): ctx, CTX
505 * arg_xor(%esp): bool, if true: xor output
510 movl arg_ctx(%esp), CTX;
512 movl arg_src(%esp), %eax;
513 read_blocks(%eax, RA, RB, RC, RD, RT0, RT1, RE);
515 K(RA, RB, RC, RD, RE, 0);
516 S0(RA, RB, RC, RD, RE); LK(RC, RB, RD, RA, RE, 1);
517 S1(RC, RB, RD, RA, RE); LK(RE, RD, RA, RC, RB, 2);
518 S2(RE, RD, RA, RC, RB); LK(RB, RD, RE, RC, RA, 3);
519 S3(RB, RD, RE, RC, RA); LK(RC, RA, RD, RB, RE, 4);
520 S4(RC, RA, RD, RB, RE); LK(RA, RD, RB, RE, RC, 5);
521 S5(RA, RD, RB, RE, RC); LK(RC, RA, RD, RE, RB, 6);
522 S6(RC, RA, RD, RE, RB); LK(RD, RB, RA, RE, RC, 7);
523 S7(RD, RB, RA, RE, RC); LK(RC, RA, RE, RD, RB, 8);
524 S0(RC, RA, RE, RD, RB); LK(RE, RA, RD, RC, RB, 9);
525 S1(RE, RA, RD, RC, RB); LK(RB, RD, RC, RE, RA, 10);
526 S2(RB, RD, RC, RE, RA); LK(RA, RD, RB, RE, RC, 11);
527 S3(RA, RD, RB, RE, RC); LK(RE, RC, RD, RA, RB, 12);
528 S4(RE, RC, RD, RA, RB); LK(RC, RD, RA, RB, RE, 13);
529 S5(RC, RD, RA, RB, RE); LK(RE, RC, RD, RB, RA, 14);
530 S6(RE, RC, RD, RB, RA); LK(RD, RA, RC, RB, RE, 15);
531 S7(RD, RA, RC, RB, RE); LK(RE, RC, RB, RD, RA, 16);
532 S0(RE, RC, RB, RD, RA); LK(RB, RC, RD, RE, RA, 17);
533 S1(RB, RC, RD, RE, RA); LK(RA, RD, RE, RB, RC, 18);
534 S2(RA, RD, RE, RB, RC); LK(RC, RD, RA, RB, RE, 19);
535 S3(RC, RD, RA, RB, RE); LK(RB, RE, RD, RC, RA, 20);
536 S4(RB, RE, RD, RC, RA); LK(RE, RD, RC, RA, RB, 21);
537 S5(RE, RD, RC, RA, RB); LK(RB, RE, RD, RA, RC, 22);
538 S6(RB, RE, RD, RA, RC); LK(RD, RC, RE, RA, RB, 23);
539 S7(RD, RC, RE, RA, RB); LK(RB, RE, RA, RD, RC, 24);
540 S0(RB, RE, RA, RD, RC); LK(RA, RE, RD, RB, RC, 25);
541 S1(RA, RE, RD, RB, RC); LK(RC, RD, RB, RA, RE, 26);
542 S2(RC, RD, RB, RA, RE); LK(RE, RD, RC, RA, RB, 27);
543 S3(RE, RD, RC, RA, RB); LK(RA, RB, RD, RE, RC, 28);
544 S4(RA, RB, RD, RE, RC); LK(RB, RD, RE, RC, RA, 29);
545 S5(RB, RD, RE, RC, RA); LK(RA, RB, RD, RC, RE, 30);
546 S6(RA, RB, RD, RC, RE); LK(RD, RE, RB, RC, RA, 31);
547 S7(RD, RE, RB, RC, RA); K(RA, RB, RC, RD, RE, 32);
549 movl arg_dst(%esp), %eax;
551 cmpb $0, arg_xor(%esp);
554 write_blocks(%eax, RA, RB, RC, RD, RT0, RT1, RE);
559 xor_blocks(%eax, RA, RB, RC, RD, RT0, RT1, RE);
562 SYM_FUNC_END(__serpent_enc_blk_4way)
564 SYM_FUNC_START(serpent_dec_blk_4way)
566 * arg_ctx(%esp): ctx, CTX
573 movl arg_ctx(%esp), CTX;
575 movl arg_src(%esp), %eax;
576 read_blocks(%eax, RA, RB, RC, RD, RT0, RT1, RE);
578 K(RA, RB, RC, RD, RE, 32);
579 SI7(RA, RB, RC, RD, RE); KL(RB, RD, RA, RE, RC, 31);
580 SI6(RB, RD, RA, RE, RC); KL(RA, RC, RE, RB, RD, 30);
581 SI5(RA, RC, RE, RB, RD); KL(RC, RD, RA, RE, RB, 29);
582 SI4(RC, RD, RA, RE, RB); KL(RC, RA, RB, RE, RD, 28);
583 SI3(RC, RA, RB, RE, RD); KL(RB, RC, RD, RE, RA, 27);
584 SI2(RB, RC, RD, RE, RA); KL(RC, RA, RE, RD, RB, 26);
585 SI1(RC, RA, RE, RD, RB); KL(RB, RA, RE, RD, RC, 25);
586 SI0(RB, RA, RE, RD, RC); KL(RE, RC, RA, RB, RD, 24);
587 SI7(RE, RC, RA, RB, RD); KL(RC, RB, RE, RD, RA, 23);
588 SI6(RC, RB, RE, RD, RA); KL(RE, RA, RD, RC, RB, 22);
589 SI5(RE, RA, RD, RC, RB); KL(RA, RB, RE, RD, RC, 21);
590 SI4(RA, RB, RE, RD, RC); KL(RA, RE, RC, RD, RB, 20);
591 SI3(RA, RE, RC, RD, RB); KL(RC, RA, RB, RD, RE, 19);
592 SI2(RC, RA, RB, RD, RE); KL(RA, RE, RD, RB, RC, 18);
593 SI1(RA, RE, RD, RB, RC); KL(RC, RE, RD, RB, RA, 17);
594 SI0(RC, RE, RD, RB, RA); KL(RD, RA, RE, RC, RB, 16);
595 SI7(RD, RA, RE, RC, RB); KL(RA, RC, RD, RB, RE, 15);
596 SI6(RA, RC, RD, RB, RE); KL(RD, RE, RB, RA, RC, 14);
597 SI5(RD, RE, RB, RA, RC); KL(RE, RC, RD, RB, RA, 13);
598 SI4(RE, RC, RD, RB, RA); KL(RE, RD, RA, RB, RC, 12);
599 SI3(RE, RD, RA, RB, RC); KL(RA, RE, RC, RB, RD, 11);
600 SI2(RA, RE, RC, RB, RD); KL(RE, RD, RB, RC, RA, 10);
601 SI1(RE, RD, RB, RC, RA); KL(RA, RD, RB, RC, RE, 9);
602 SI0(RA, RD, RB, RC, RE); KL(RB, RE, RD, RA, RC, 8);
603 SI7(RB, RE, RD, RA, RC); KL(RE, RA, RB, RC, RD, 7);
604 SI6(RE, RA, RB, RC, RD); KL(RB, RD, RC, RE, RA, 6);
605 SI5(RB, RD, RC, RE, RA); KL(RD, RA, RB, RC, RE, 5);
606 SI4(RD, RA, RB, RC, RE); KL(RD, RB, RE, RC, RA, 4);
607 SI3(RD, RB, RE, RC, RA); KL(RE, RD, RA, RC, RB, 3);
608 SI2(RE, RD, RA, RC, RB); KL(RD, RB, RC, RA, RE, 2);
609 SI1(RD, RB, RC, RA, RE); KL(RE, RB, RC, RA, RD, 1);
610 SI0(RE, RB, RC, RA, RD); K(RC, RD, RB, RE, RA, 0);
612 movl arg_dst(%esp), %eax;
613 write_blocks(%eax, RC, RD, RB, RE, RT0, RT1, RA);
616 SYM_FUNC_END(serpent_dec_blk_4way)