ee4bf9ad3b548670de681b70a7cf7487bbae7ca4
[platform/upstream/nettle.git] / x86_64 / serpent-decrypt.asm
1 C x86_64/serpent-decrypt.asm
2
3 ifelse(<
4    Copyright (C) 2011 Niels Möller
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14
15    or
16
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20
21    or both in parallel, as here.
22
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 >)
32
33 include_src(<x86_64/serpent.m4>)
34
35 C Register usage:
36
37 C Single block serpent state, two copies
38 define(<x0>, <%eax>)
39 define(<x1>, <%ebx>)
40 define(<x2>, <%ebp>)
41 define(<x3>, <%r8d>)
42
43 define(<y0>, <%r9d>)
44 define(<y1>, <%r10d>)
45 define(<y2>, <%r11d>)
46 define(<y3>, <%r12d>)
47
48 C Quadruple block serpent state, two copies
49 define(<X0>, <%xmm0>)
50 define(<X1>, <%xmm1>)
51 define(<X2>, <%xmm2>)
52 define(<X3>, <%xmm3>)
53
54 define(<Y0>, <%xmm4>)
55 define(<Y1>, <%xmm5>)
56 define(<Y2>, <%xmm6>)
57 define(<Y3>, <%xmm7>)
58
59 define(<MINUS1>, <%xmm8>)
60 define(<T0>, <%xmm9>)
61 define(<T1>, <%xmm10>)
62 define(<T2>, <%xmm11>)
63 define(<T3>, <%xmm12>)
64
65 C Arguments
66 define(<CTX>, <%rdi>)
67 define(<N>, <%rsi>)
68 define(<DST>, <%rdx>)
69 define(<SRC>, <%rcx>)
70
71 define(<CNT>, <%r13>)
72 define(<TMP32>, <%r14d>)
73
74 C SBOX macros. Inputs $1 - $4 (destroyed), outputs $5 - $8
75
76 define(<SBOX0I>, <
77         mov     $1, $5
78         xor     $3, $5
79         mov     $1, $7
80         or      $2, $7
81         mov     $3, $6
82         xor     $4, $6
83         xor     $6, $7
84         and     $3, $6
85         or      $2, $3
86         xor     $4, $2
87         or      $1, $6
88         and     $3, $2
89         xor     $2, $6
90         or      $7, $1
91         xor     $6, $1
92         mov     $7, $2
93         and     $1, $2
94         not     $7
95         or      $7, $4
96         xor     $3, $4
97         mov     $1, $8
98         xor     $4, $8
99         or      $4, $2
100         xor     $2, $5
101 >)
102
103 define(<SBOX1I>, <
104         mov     $2, $6
105         or      $4, $6
106         xor     $3, $6
107         mov     $1, $8
108         xor     $2, $8
109         mov     $1, $5
110         or      $6, $5
111         and     $8, $5
112         xor     $5, $2
113         xor     $6, $8
114         and     $4, $2
115         mov     $1, $7
116         and     $3, $7
117         or      $7, $6
118         or      $4, $7
119         xor     $5, $7
120         not     $7
121         xor     $2, $6
122         xor     $6, $5
123         xor     $3, $5
124         or      $7, $1
125         xor     $1, $5
126 >)
127
128 define(<SBOX2I>, <
129         mov     $1, $5
130         xor     $4, $5
131         mov     $3, $7
132         xor     $4, $7
133         mov     $2, $6
134         or      $7, $6
135         xor     $6, $5
136         mov     $4, $6
137         or      $5, $6
138         and     $2, $6
139         not     $4
140         mov     $1, $8
141         or      $3, $8
142         and     $8, $7
143         xor     $7, $6
144         and     $2, $8
145         and     $3, $1
146         or      $4, $1
147         xor     $1, $8
148         and     $8, $3
149         xor     $1, $3
150         mov     $5, $7
151         xor     $6, $7
152         xor     $3, $7
153 >)
154
155 define(<SBOX3I>, <
156         mov     $3, $8
157         or      $4, $8
158         mov     $2, $5
159         and     $8, $5
160         mov     $1, $7
161         or      $4, $7
162         mov     $3, $6
163         xor     $7, $6
164         xor     $6, $5
165         xor     $1, $4
166         xor     $4, $8
167         xor     $2, $7
168         and     $6, $7
169         xor     $4, $7
170         xor     $1, $6
171         or      $5, $4
172         and     $4, $6
173         xor     $2, $6
174         and     $7, $1
175         or      $2, $1
176         xor     $1, $8
177 >)
178
179 define(<SBOX4I>, <
180         mov     $3, $6
181         xor     $4, $6
182         mov     $3, $7
183         or      $4, $7
184         xor     $2, $7
185         or      $4, $2
186         mov     $1, $5
187         xor     $7, $5
188         xor     $7, $4
189         and     $1, $7
190         xor     $7, $6
191         xor     $1, $7
192         or      $3, $7
193         and     $2, $1
194         mov     $1, $8
195         xor     $4, $8
196         not     $1
197         or      $6, $1
198         xor     $1, $5
199         xor     $2, $1
200         xor     $1, $7
201 >)
202
203 define(<SBOX5I>, <
204         mov     $1, $6
205         and     $4, $6
206         mov     $3, $8
207         xor     $6, $8
208         mov     $2, $5
209         and     $8, $5
210         mov     $1, $7
211         xor     $4, $7
212         xor     $2, $4
213         xor     $7, $5
214         and     $1, $3
215         and     $5, $1
216         or      $2, $3
217         xor     $5, $6
218         xor     $3, $6
219         mov     $5, $7
220         or      $6, $7
221         xor     $8, $7
222         xor     $4, $7
223         not     $2
224         or      $1, $2
225         xor     $2, $8
226 >)
227
228 define(<SBOX6I>, <
229         mov     $1, $7
230         xor     $3, $7
231         not     $3
232         mov     $2, $5
233         xor     $4, $5
234         mov     $1, $6
235         or      $3, $6
236         xor     $5, $6
237         mov     $2, $8
238         and     $7, $8
239         or      $4, $8
240         or      $3, $4
241         or      $2, $3
242         and     $1, $3
243         mov     $3, $5
244         xor     $8, $5
245         not     $5
246         and     $7, $8
247         xor     $3, $8
248         xor     $6, $1
249         xor     $1, $8
250         and     $5, $2
251         xor     $2, $7
252         xor     $4, $7
253 >)
254
255 define(<SBOX7I>, <
256         mov     $1, $8
257         and     $2, $8
258         mov     $2, $7
259         xor     $4, $7
260         or      $8, $7
261         mov     $1, $6
262         or      $4, $6
263         and     $3, $6
264         xor     $6, $7
265         or      $3, $8
266         mov     $1, $5
267         or      $2, $5
268         and     $4, $5
269         xor     $5, $8
270         xor     $2, $5
271         mov     $4, $6
272         xor     $8, $6
273         not     $6
274         or      $5, $6
275         xor     $3, $5
276         xor     $1, $6
277         or      $6, $4
278         xor     $4, $5
279 >)
280
281 define(<LTI>, <
282         rol     <$>10, $3
283         rol     <$>27, $1
284         mov     $2, TMP32
285         shl     <$>7, TMP32
286         xor     $4, $3
287         xor     TMP32, $3
288         xor     $2, $1
289         xor     $4, $1
290         rol     <$>25, $4
291         rol     <$>31, $2
292         mov     $1, TMP32
293         shl     <$>3, TMP32
294         xor     $3, $4
295         xor     TMP32, $4
296         xor     $1, $2
297         xor     $3, $2
298         rol     <$>29, $3
299         rol     <$>19, $1
300 >)
301
302 define(<PNOT>, <
303         pxor    MINUS1, $1
304 >)
305
306 define(<WSBOX0I>, <
307         movdqa  $1, $5
308         pxor    $3, $5
309         movdqa  $1, $7
310         por     $2, $7
311         movdqa  $3, $6
312         pxor    $4, $6
313         pxor    $6, $7
314         pand    $3, $6
315         por     $2, $3
316         pxor    $4, $2
317         por     $1, $6
318         pand    $3, $2
319         pxor    $2, $6
320         por     $7, $1
321         pxor    $6, $1
322         movdqa  $7, $2
323         pand    $1, $2
324         PNOT($7)
325         por     $7, $4
326         pxor    $3, $4
327         movdqa  $1, $8
328         pxor    $4, $8
329         por     $4, $2
330         pxor    $2, $5
331 >)
332
333 define(<WSBOX1I>, <
334         movdqa  $2, $6
335         por     $4, $6
336         pxor    $3, $6
337         movdqa  $1, $8
338         pxor    $2, $8
339         movdqa  $1, $5
340         por     $6, $5
341         pand    $8, $5
342         pxor    $5, $2
343         pxor    $6, $8
344         pand    $4, $2
345         movdqa  $1, $7
346         pand    $3, $7
347         por     $7, $6
348         por     $4, $7
349         pxor    $5, $7
350         PNOT($7)
351         pxor    $2, $6
352         pxor    $6, $5
353         pxor    $3, $5
354         por     $7, $1
355         pxor    $1, $5
356 >)
357
358 define(<WSBOX2I>, <
359         movdqa  $1, $5
360         pxor    $4, $5
361         movdqa  $3, $7
362         pxor    $4, $7
363         movdqa  $2, $6
364         por     $7, $6
365         pxor    $6, $5
366         movdqa  $4, $6
367         por     $5, $6
368         pand    $2, $6
369         PNOT($4)
370         movdqa  $1, $8
371         por     $3, $8
372         pand    $8, $7
373         pxor    $7, $6
374         pand    $2, $8
375         pand    $3, $1
376         por     $4, $1
377         pxor    $1, $8
378         pand    $8, $3
379         pxor    $1, $3
380         movdqa  $5, $7
381         pxor    $6, $7
382         pxor    $3, $7
383 >)
384
385 define(<WSBOX3I>, <
386         movdqa  $3, $8
387         por     $4, $8
388         movdqa  $2, $5
389         pand    $8, $5
390         movdqa  $1, $7
391         por     $4, $7
392         movdqa  $3, $6
393         pxor    $7, $6
394         pxor    $6, $5
395         pxor    $1, $4
396         pxor    $4, $8
397         pxor    $2, $7
398         pand    $6, $7
399         pxor    $4, $7
400         pxor    $1, $6
401         por     $5, $4
402         pand    $4, $6
403         pxor    $2, $6
404         pand    $7, $1
405         por     $2, $1
406         pxor    $1, $8
407 >)
408
409 define(<WSBOX4I>, <
410         movdqa  $3, $6
411         pxor    $4, $6
412         movdqa  $3, $7
413         por     $4, $7
414         pxor    $2, $7
415         por     $4, $2
416         movdqa  $1, $5
417         pxor    $7, $5
418         pxor    $7, $4
419         pand    $1, $7
420         pxor    $7, $6
421         pxor    $1, $7
422         por     $3, $7
423         pand    $2, $1
424         movdqa  $1, $8
425         pxor    $4, $8
426         PNOT($1)
427         por     $6, $1
428         pxor    $1, $5
429         pxor    $2, $1
430         pxor    $1, $7
431 >)
432
433 define(<WSBOX5I>, <
434         movdqa  $1, $6
435         pand    $4, $6
436         movdqa  $3, $8
437         pxor    $6, $8
438         movdqa  $2, $5
439         pand    $8, $5
440         movdqa  $1, $7
441         pxor    $4, $7
442         pxor    $2, $4
443         pxor    $7, $5
444         pand    $1, $3
445         pand    $5, $1
446         por     $2, $3
447         pxor    $5, $6
448         pxor    $3, $6
449         movdqa  $5, $7
450         por     $6, $7
451         pxor    $8, $7
452         pxor    $4, $7
453         PNOT($2)
454         por     $1, $2
455         pxor    $2, $8
456 >)
457
458 define(<WSBOX6I>, <
459         movdqa  $1, $7
460         pxor    $3, $7
461         PNOT($3)
462         movdqa  $2, $5
463         pxor    $4, $5
464         movdqa  $1, $6
465         por     $3, $6
466         pxor    $5, $6
467         movdqa  $2, $8
468         pand    $7, $8
469         por     $4, $8
470         por     $3, $4
471         por     $2, $3
472         pand    $1, $3
473         movdqa  $3, $5
474         pxor    $8, $5
475         PNOT($5)
476         pand    $7, $8
477         pxor    $3, $8
478         pxor    $6, $1
479         pxor    $1, $8
480         pand    $5, $2
481         pxor    $2, $7
482         pxor    $4, $7
483 >)
484
485 define(<WSBOX7I>, <
486         movdqa  $1, $8
487         pand    $2, $8
488         movdqa  $2, $7
489         pxor    $4, $7
490         por     $8, $7
491         movdqa  $1, $6
492         por     $4, $6
493         pand    $3, $6
494         pxor    $6, $7
495         por     $3, $8
496         movdqa  $1, $5
497         por     $2, $5
498         pand    $4, $5
499         pxor    $5, $8
500         pxor    $2, $5
501         movdqa  $4, $6
502         pxor    $8, $6
503         PNOT($6)
504         por     $5, $6
505         pxor    $3, $5
506         pxor    $1, $6
507         por     $6, $4
508         pxor    $4, $5
509 >)
510
511 define(<WLTI>, <
512         WROL(10, $3)
513         WROL(27, $1)
514         movdqa  $2, T0
515         pslld   <$>7, T0
516         pxor    $4, $3
517         pxor    T0, $3
518         pxor    $2, $1
519         pxor    $4, $1
520         WROL(25, $4)
521         WROL(31, $2)
522         movdqa  $1, T0
523         pslld   <$>3, T0
524         pxor    $3, $4
525         pxor    T0, $4
526         pxor    $1, $2
527         pxor    $3, $2
528         WROL(29, $3)
529         WROL(19, $1)
530 >)
531
532         .file "serpent-decrypt.asm"
533         
534         C serpent_decrypt(struct serpent_context *ctx, 
535         C                 size_t length, uint8_t *dst,
536         C                 const uint8_t *src)
537         .text
538         ALIGN(16)
539 PROLOGUE(nettle_serpent_decrypt)
540         C save all registers that need to be saved
541         W64_ENTRY(4, 13)
542         push    %rbx
543         push    %rbp
544         push    %r12
545         push    %r13
546         push    %r14
547
548         lea     (SRC, N), SRC
549         lea     (DST, N), DST
550         neg     N
551         jz      .Lend
552
553         cmp     $-64, N
554         ja      .Lblock_loop
555
556         pcmpeqd MINUS1, MINUS1
557
558 .Lwblock_loop:
559         movups  (SRC, N), X0
560         movups  16(SRC, N), X1
561         movups  32(SRC, N), X2
562         movups  48(SRC, N), X3
563
564         WTRANSPOSE(X0,X1,X2,X3)
565
566         mov     $384, CNT
567
568         C FIXME: CNT known, no index register needed
569         WKEYXOR(128, X0,X1,X2,X3)
570
571         jmp     .Lwround_start
572
573         ALIGN(16)
574
575 .Lwround_loop:
576         WLTI(X0,X1,X2,X3)
577 .Lwround_start:
578         WSBOX7I(X0,X1,X2,X3, Y0,Y1,Y2,Y3)
579         WKEYXOR(112, Y0,Y1,Y2,Y3)
580
581         WLTI(Y0,Y1,Y2,Y3)
582         WSBOX6I(Y0,Y1,Y2,Y3, X0,X1,X2,X3)
583         WKEYXOR(96, X0,X1,X2,X3)
584         
585         WLTI(X0,X1,X2,X3)
586         WSBOX5I(X0,X1,X2,X3, Y0,Y1,Y2,Y3)
587         WKEYXOR(80, Y0,Y1,Y2,Y3)
588
589         WLTI(Y0,Y1,Y2,Y3)
590         WSBOX4I(Y0,Y1,Y2,Y3, X0,X1,X2,X3)
591         WKEYXOR(64, X0,X1,X2,X3)
592         
593         WLTI(X0,X1,X2,X3)
594         WSBOX3I(X0,X1,X2,X3, Y0,Y1,Y2,Y3)
595         WKEYXOR(48, Y0,Y1,Y2,Y3)
596
597         WLTI(Y0,Y1,Y2,Y3)
598         WSBOX2I(Y0,Y1,Y2,Y3, X0,X1,X2,X3)
599         WKEYXOR(32, X0,X1,X2,X3)
600         
601         WLTI(X0,X1,X2,X3)
602         WSBOX1I(X0,X1,X2,X3, Y0,Y1,Y2,Y3)
603         WKEYXOR(16, Y0,Y1,Y2,Y3)
604
605         WLTI(Y0,Y1,Y2,Y3)
606         WSBOX0I(Y0,Y1,Y2,Y3, X0,X1,X2,X3)
607         WKEYXOR(, X0,X1,X2,X3)
608
609         sub     $128, CNT
610         jnc     .Lwround_loop
611
612         WTRANSPOSE(X0,X1,X2,X3)
613
614         movups  X0, (DST, N)
615         movups  X1, 16(DST, N)
616         movups  X2, 32(DST, N)
617         movups  X3, 48(DST, N)
618
619         C FIXME: Adjust N, so we can use just jnc without an extra cmp.
620         add     $64, N
621         jz      .Lend
622
623         cmp     $-64, N
624         jbe     .Lwblock_loop
625
626 .Lblock_loop:
627         movl    (SRC, N), x0
628         movl    4(SRC, N), x1
629         movl    8(SRC, N), x2
630         movl    12(SRC, N), x3
631
632         xor     512(CTX), x0
633         xor     516(CTX), x1
634         xor     520(CTX), x2
635         xor     524(CTX), x3
636
637         mov     $384, CNT
638         jmp     .Lround_start
639
640         ALIGN(16)
641 .Lround_loop:
642         LTI(x0,x1,x2,x3)
643 .Lround_start:
644         SBOX7I(x0,x1,x2,x3, y0,y1,y2,y3)
645         xor     112(CTX, CNT), y0
646         xor     116(CTX, CNT), y1
647         xor     120(CTX, CNT), y2
648         xor     124(CTX, CNT), y3
649
650         LTI(y0,y1,y2,y3)
651         SBOX6I(y0,y1,y2,y3, x0,x1,x2,x3)
652         xor      96(CTX, CNT), x0
653         xor     100(CTX, CNT), x1
654         xor     104(CTX, CNT), x2
655         xor     108(CTX, CNT), x3
656
657         LTI(x0,x1,x2,x3)
658         SBOX5I(x0,x1,x2,x3, y0,y1,y2,y3)
659         xor     80(CTX, CNT), y0
660         xor     84(CTX, CNT), y1
661         xor     88(CTX, CNT), y2
662         xor     92(CTX, CNT), y3
663
664         LTI(y0,y1,y2,y3)
665         SBOX4I(y0,y1,y2,y3, x0,x1,x2,x3)
666         xor     64(CTX, CNT), x0
667         xor     68(CTX, CNT), x1
668         xor     72(CTX, CNT), x2
669         xor     76(CTX, CNT), x3
670
671         LTI(x0,x1,x2,x3)
672         SBOX3I(x0,x1,x2,x3, y0,y1,y2,y3)
673         xor     48(CTX, CNT), y0
674         xor     52(CTX, CNT), y1
675         xor     56(CTX, CNT), y2
676         xor     60(CTX, CNT), y3
677
678         LTI(y0,y1,y2,y3)
679         SBOX2I(y0,y1,y2,y3, x0,x1,x2,x3)
680         xor     32(CTX, CNT), x0
681         xor     36(CTX, CNT), x1
682         xor     40(CTX, CNT), x2
683         xor     44(CTX, CNT), x3
684
685         LTI(x0,x1,x2,x3)
686         SBOX1I(x0,x1,x2,x3, y0,y1,y2,y3)
687         xor     16(CTX, CNT), y0
688         xor     20(CTX, CNT), y1
689         xor     24(CTX, CNT), y2
690         xor     28(CTX, CNT), y3
691
692         LTI(y0,y1,y2,y3)
693         SBOX0I(y0,y1,y2,y3, x0,x1,x2,x3)
694         xor       (CTX, CNT), x0
695         xor      4(CTX, CNT), x1
696         xor      8(CTX, CNT), x2
697         xor     12(CTX, CNT), x3
698         sub     $128, CNT
699         jnc     .Lround_loop
700
701         movl    x0, (DST, N)
702         movl    x1, 4(DST, N)
703         movl    x2, 8(DST, N)
704         movl    x3, 12(DST, N)
705         add     $16, N
706         jnc     .Lblock_loop
707         
708 .Lend:
709         pop     %r14
710         pop     %r13
711         pop     %r12
712         pop     %rbp
713         pop     %rbx
714         W64_EXIT(4, 13)
715         ret