]> git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/aes/i386/AES.s
xnu-1699.26.8.tar.gz
[apple/xnu.git] / bsd / crypto / aes / i386 / AES.s
1 /* AES.s -- Core AES routines for Intel processors.
2
3 Written by Eric Postpischil, January 30, 2008.
4 */
5
6
7 /* We build these AES routines as a single module because the routines refer
8 to labels in Data.s and it is easier and faster to refer to them as local
9 labels. In my implementations of AES for CommonCrypto, both i386 and
10 x86_64 use position-independent code. For this in-kernel implementation,
11 i386 has been converted to absolute addressing, but x86_64 still uses PIC.
12
13 A local label can be referred to with position-independent assembler
14 expressions such as "label-base(register)", where <base> is a local label
15 whose address has been loaded into <register>. (On i386, this is typically
16 done with the idiom of a call to the next instruction and a pop of that
17 return address into a register.) Without local labels, the references must
18 be done using spaces for addresses of "lazy symbols" that are filled in by
19 the dynamic loader and loaded by the code that wants the address.
20
21 So the various routines in other files are assembled here via #include
22 directives.
23 */
24 #include "Data.s"
25
26
27 #define TableSize (256*4)
28 /* Each of the arrays defined in Data.s except for the round constants
29 in _AESRcon is composed of four tables of 256 entries of four bytes
30 each. TableSize is the number of bytes in one of those four tables.
31 */
32
33
34 // Include constants describing the AES context structures.
35 #include "Context.h"
36
37
38 /* Define a macro to select a value based on architecture. This reduces
39 some of the architecture conditionalization later in the source.
40 */
41 #if defined __i386__
42 #define Arch(i386, x86_64) i386
43 #elif defined __x86_64__
44 #define Arch(i386, x86_64) x86_64
45 #endif
46
47
48 // Define an instruction for moving pointers.
49 #define movp Arch(movd, movd)
50 // Latter argument should be "movq", but the assembler uses "movd".
51
52
53 /* Rename the general registers. This makes it easier to keep track of them
54 and provides names for the "whole register" that are uniform between i386
55 and x86_64.
56 */
57 #if defined __i386__
58 #define r0 %eax // Available for any use.
59 #define r1 %ecx // Available for any use, some special purposes (loop).
60 #define r2 %edx // Available for any use.
61 #define r3 %ebx // Must be preserved by called routine.
62 #define r4 %esp // Stack pointer.
63 #define r5 %ebp // Frame pointer, must preserve, no bare indirect.
64 #define r6 %esi // Must be preserved by called routine.
65 #define r7 %edi // Must be preserved by called routine.
66 #elif defined __x86_64__
67 #define r0 %rax // Available for any use.
68 #define r1 %rcx // Available for any use.
69 #define r2 %rdx // Available for any use.
70 #define r3 %rbx // Must be preserved by called routine.
71 #define r4 %rsp // Stack pointer.
72 #define r5 %rbp // Frame pointer. Must be preserved by called routine.
73 #define r6 %rsi // Available for any use.
74 #define r7 %rdi // Available for any use.
75 #define r8 %r8 // Available for any use.
76 #define r9 %r9 // Available for any use.
77 #define r10 %r10 // Available for any use.
78 #define r11 %r11 // Available for any use.
79 #define r12 %r12 // Must be preserved by called routine.
80 #define r13 %r13 // Must be preserved by called routine.
81 #define r14 %r14 // Must be preserved by called routine.
82 #define r15 %r15 // Must be preserved by called routine.
83 #else
84 #error "Unknown architecture."
85 #endif
86
87 // Define names for parts of registers.
88
89 #define r0d %eax // Low 32 bits of r0.
90 #define r1d %ecx // Low 32 bits of r1.
91 #define r2d %edx // Low 32 bits of r2.
92 #define r3d %ebx // Low 32 bits of r3.
93 #define r5d %ebp // Low 32 bits of r5.
94 #define r6d %esi // Low 32 bits of r6.
95 #define r7d %edi // Low 32 bits of r7.
96 #define r8d %r8d // Low 32 bits of r8.
97 #define r9d %r9d // Low 32 bits of r9.
98 #define r11d %r11d // Low 32 bits of r11.
99
100 #define r0l %al // Low byte of r0.
101 #define r1l %cl // Low byte of r1.
102 #define r2l %dl // Low byte of r2.
103 #define r3l %bl // Low byte of r3.
104 #define r5l %bpl // Low byte of r5.
105
106 #define r0h %ah // Second lowest byte of r0.
107 #define r1h %ch // Second lowest byte of r1.
108 #define r2h %dh // Second lowest byte of r2.
109 #define r3h %bh // Second lowest byte of r3.
110
111
112 .text
113
114
115 // Define encryption routine, _AESEncryptWithExpandedKey
116 #define Select 0
117 #include "EncryptDecrypt.s"
118 #undef Select
119
120
121 // Define decryption routine, _AESDecryptWithExpandedKey
122 #define Select 1
123 #include "EncryptDecrypt.s"
124 #undef Select
125
126 // Define encryption routine, _AESEncryptWithExpandedKey
127 #define Select 2
128 #include "EncryptDecrypt.s"
129 #undef Select
130
131
132 // Define decryption routine, _AESDecryptWithExpandedKey
133 #define Select 3
134 #include "EncryptDecrypt.s"
135 #undef Select
136
137
138 // Define key expansion routine for encryption, _AESExpandKeyForEncryption.
139 #include "ExpandKeyForEncryption.s"
140
141
142 // Define key expansion for decryption routine, _AESExpandKeyForDecryption.
143 #include "ExpandKeyForDecryption.s"