Imported Upstream version 2.4
[platform/upstream/nettle.git] / x86_64 / aes.m4
1 dnl LREG(reg) gives the 8-bit register corresponding to the given 32-bit register.
2 define(<LREG>,<ifelse(
3         $1, %eax, %al,
4         $1, %ebx, %bl,
5         $1, %ecx, %cl,
6         $1, %edx, %dl,
7         $1, %esi, %sil,
8         $1, %edi, %dil,
9         $1, %ebp, %bpl,
10         $1, %esp, %spl,
11         $1, %r8d, %r8b,
12         $1, %r9d, %r9b,
13         $1, %r10d, %r10b,
14         $1, %r11d, %r11b,
15         $1, %r12d, %r12b,
16         $1, %r13d, %r13b,
17         $1, %r14d, %r14b,
18         $1, %r15d, %r15b)>)dnl
19
20 define(<HREG>,<ifelse(
21         $1, %eax, %ah,
22         $1, %ebx, %bh,
23         $1, %ecx, %ch,
24         $1, %edx, %dh,
25         error)>)
26
27 define(<XREG>,<ifelse(
28         $1, %rax, %eax,
29         $1, %rbx, %ebx,
30         $1, %rcx, %ecx,
31         $1, %rdx, %edx,
32         $1, %rsi, %esi,
33         $1, %rdi, %edi,
34         $1, %rbp, %ebp,
35         $1, %rsp, %esp,
36         $1, %r8, %r8d,
37         $1, %r9, %r9d,
38         $1, %r10,%r10d,
39         $1, %r11,%r11d,
40         $1, %r12,%r12d,
41         $1, %r13,%r13d,
42         $1, %r14,%r14d,
43         $1, %r15,%r15d)>)dnl
44
45 dnl AES_LOAD(a, b, c, d, src, key)
46 dnl Loads the next block of data from src, and add the subkey pointed
47 dnl to by key.
48 dnl Note that x86 allows unaligned accesses.
49 dnl Would it be preferable to interleave the loads and stores?
50 define(<AES_LOAD>, <
51         movl    ($5),$1
52         movl    4($5),$2
53         movl    8($5),$3
54         movl    12($5),$4
55         
56         xorl    ($6),$1
57         xorl    4($6),$2
58         xorl    8($6),$3
59         xorl    12($6),$4>)dnl
60
61 dnl AES_STORE(a, b, c, d, key, dst)
62 dnl Adds the subkey to a, b, c, d,
63 dnl and stores the result in the area pointed to by dst.
64 dnl Note that x86 allows unaligned accesses.
65 dnl Would it be preferable to interleave the loads and stores?
66 define(<AES_STORE>, <
67         xorl    ($5),$1
68         xorl    4($5),$2
69         xorl    8($5),$3
70         xorl    12($5),$4
71
72         movl    $1,($6)
73         movl    $2,4($6)
74         movl    $3,8($6)
75         movl    $4,12($6)>)dnl
76
77 dnl AES_ROUND(table,a,b,c,d,out,ptr)
78 dnl Computes one word of the AES round. Leaves result in $6.
79 define(<AES_ROUND>, <
80         movzb   LREG($2), $7
81         movl    AES_TABLE0 ($1, $7, 4),$6
82         movzb   HREG($3), XREG($7)
83         xorl    AES_TABLE1 ($1, $7, 4),$6
84         movl    $4,XREG($7)
85         shr     <$>16,$7
86         and     <$>0xff,$7
87         xorl    AES_TABLE2 ($1, $7, 4),$6
88         movl    $5,XREG($7)
89         shr     <$>24,$7
90         xorl    AES_TABLE3 ($1, $7, 4),$6>)dnl
91
92 dnl AES_FINAL_ROUND(a, b, c, d, table, out, tmp)
93 dnl Computes one word of the final round. Leaves result in $6. Also
94 dnl performs the first substitution step, on the least significant
95 dnl byte, and rotates 8 bits.
96 define(<AES_FINAL_ROUND>, <
97         movzb   LREG($1),$7
98         movzbl  ($5, $7), $6
99         movl    $2,XREG($7)
100         andl    <$>0x0000ff00,XREG($7)
101         orl     XREG($7), $6
102         movl    $3,XREG($7)
103         andl    <$>0x00ff0000,XREG($7)
104         orl     XREG($7), $6
105         movl    $4,XREG($7)
106         andl    <$>0xff000000,XREG($7)
107         orl     XREG($7), $6
108         roll    <$>8, $6>)dnl
109
110 dnl AES_SUBST_BYTE(A, B, C, D, table, tmp)
111 dnl Substitutes the least significant byte of
112 dnl each of eax, ebx, ecx and edx, and also rotates
113 dnl the words one byte to the left.
114 dnl Uses that AES_SBOX == 0
115 define(<AES_SUBST_BYTE>, <
116         movzb   LREG($1),$6
117         movb    ($5, $6),LREG($1)
118         roll    <$>8,$1
119
120         movzb  LREG($2),$6
121         movb    ($5, $6),LREG($2)
122         roll    <$>8,$2
123
124         movzb  LREG($3),$6
125         movb    ($5, $6),LREG($3)
126         roll    <$>8,$3
127
128         movzb  LREG($4),$6
129         movb    ($5, $6),LREG($4)
130         roll    <$>8,$4>)dnl