1 /// -*- mode: asm; asm-comment-char: ?/ -*-
3 /// AESNI-based implementation of Rijndael
5 /// (c) 2015 Straylight/Edgeware
8 ///----- Licensing notice ---------------------------------------------------
10 /// This file is part of Catacomb.
12 /// Catacomb is free software; you can redistribute it and/or modify
13 /// it under the terms of the GNU Library General Public License as
14 /// published by the Free Software Foundation; either version 2 of the
15 /// License, or (at your option) any later version.
17 /// Catacomb is distributed in the hope that it will be useful,
18 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 /// GNU Library General Public License for more details.
22 /// You should have received a copy of the GNU Library General Public
23 /// License along with Catacomb; if not, write to the Free
24 /// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 /// MA 02111-1307, USA.
27 ///--------------------------------------------------------------------------
28 /// External definitions.
31 #include "asm-common.h"
34 .extern F(rijndael_rcon)
36 ///--------------------------------------------------------------------------
42 /// The AESNI instructions implement a little-endian version of AES, but
43 /// Catacomb's internal interface presents as big-endian so as to work better
44 /// with things like GCM. We therefore maintain the round keys in
45 /// little-endian form, and have to end-swap blocks in and out.
47 /// For added amusement, the AESNI instructions don't implement the
48 /// larger-block versions of Rijndael, so we have to end-swap the keys if
49 /// we're preparing for one of those.
52 .equ maxrounds, 16 // maximum number of rounds
53 .equ maxblksz, 32 // maximum block size, in bytes
54 .equ kbufsz, maxblksz*(maxrounds + 1) // size of a key-schedule buffer
57 .equ nr, 0 // number of rounds
58 .equ w, nr + 4 // encryption key words
59 .equ wi, w + kbufsz // decryption key words
61 ///--------------------------------------------------------------------------
64 FUNC(rijndael_setup_x86ish_aesni)
70 // Arguments are on the stack. We'll need to stack the caller's
71 // register veriables, but we'll manage.
73 # define CTX ebp // context pointer
74 # define BLKSZ [esp + 24] // block size
76 # define KSZ ebx // key size
77 # define NKW edx // total number of key words
78 # define NKW_NEEDS_REFRESH 1 // ... needs recalculating
79 # define RCON ecx // round constants table
80 # define LIM edx // limit pointer
81 # define CYIX edi // index in shift-register cycle
83 # define NR ecx // number of rounds
84 # define LRK eax // distance to last key
85 # define BLKOFF edx // block size in bytes
87 // Stack the caller's registers.
93 // Set up our own variables.
94 mov CTX, [esp + 20] // context base pointer
95 mov SI, [esp + 28] // key material
96 mov KSZ, [esp + 32] // key size, in words
99 #if CPUFAM_AMD64 && ABI_SYSV
100 // Arguments are in registers. We have plenty, but, to be honest,
101 // the initial register allocation is a bit annoying.
103 # define CTX r8 // context pointer
104 # define BLKSZ r9d // block size
106 # define KSZ edx // key size
107 # define NKW r10d // total number of key words
108 # define RCON rdi // round constants table
109 # define LIM rcx // limit pointer
110 # define CYIX r11d // index in shift-register cycle
112 # define NR ecx // number of rounds
113 # define LRK eax // distance to last key
114 # define BLKOFF r9d // block size in bytes
116 // Move arguments to more useful places.
117 mov CTX, rdi // context base pointer
118 mov BLKSZ, esi // block size in words
119 mov SI, rdx // key material
120 mov KSZ, ecx // key size, in words
123 #if CPUFAM_AMD64 && ABI_WIN
124 // Arguments are in different registers, and they're a little tight.
126 # define CTX r8 // context pointer
127 # define BLKSZ edx // block size
129 # define KSZ r9d // key size
130 # define NKW r10d // total number of key words
131 # define RCON rdi // round constants table
132 # define LIM rcx // limit pointer
133 # define CYIX r11d // index in shift-register cycle
135 # define NR ecx // number of rounds
136 # define LRK eax // distance to last key
137 # define BLKOFF edx // block size in bytes
139 // We'll need the index registers, which belong to the caller in this
144 // Move arguments to more useful places.
145 mov rsi, r8 // key material
146 mov CTX, rcx // context base pointer
151 // The initial round key material is taken directly from the input
152 // key, so copy it over.
153 #if CPUFAM_AMD64 && ABI_SYSV
154 // We've been lucky. We already have a copy of the context pointer
155 // in rdi, and the key size in ecx.
163 // Find out other useful things.
164 mov NKW, [CTX + nr] // number of rounds
166 imul NKW, BLKSZ // total key size in words
167 #if !NKW_NEEDS_REFRESH
168 // If we can't keep NKW for later, then we use the same register for
169 // it and LIM, so this move is unnecessary.
172 sub DWORD(LIM), KSZ // offset by the key size
174 // Find the round constants.
176 leaext RCON, F(rijndael_rcon), WHOLE(c)
178 // Prepare for the main loop.
180 mov eax, [SI + 4*WHOLE(KSZ) - 4] // most recent key word
181 lea LIM, [SI + 4*LIM] // limit, offset by one key expansion
182 xor CYIX, CYIX // start of new cycle
184 // Main key expansion loop. The first word of each key-length chunk
185 // needs special treatment.
187 // This is rather tedious because the Intel `AESKEYGENASSIST'
188 // instruction is very strangely shaped. Firstly, it wants to
189 // operate on vast SSE registers, even though we're data-blocked from
190 // doing more than operation at a time unless we're doing two key
191 // schedules simultaneously -- and even then we can't do more than
192 // two, because the instruction ignores two of its input words
193 // entirely, and produces two different outputs for each of the other
194 // two. And secondly it insists on taking the magic round constant
195 // as an immediate, so it's kind of annoying if you're not
196 // open-coding the whole thing. It's much easier to leave that as
197 // zero and XOR in the round constant by hand.
198 0: cmp CYIX, 0 // first word of the cycle?
200 cmp CYIX, 4 // fourth word of the cycle?
202 cmp KSZ, 7 // and a large key?
205 // Fourth word of the cycle, and seven or eight words of key. Do a
206 // byte substitution.
208 pshufd xmm0, xmm0, SHUF(2, 1, 0, 3)
209 aeskeygenassist xmm1, xmm0, 0
213 // First word of the cycle. This is the complicated piece.
215 pshufd xmm0, xmm0, SHUF(0, 3, 2, 1)
216 aeskeygenassist xmm1, xmm0, 0
217 pshufd xmm1, xmm1, SHUF(2, 1, 0, 3)
222 // Common tail. Mix in the corresponding word from the previous
223 // cycle and prepare for the next loop.
225 mov [SI + 4*WHOLE(KSZ)], eax
235 // Next job is to construct the decryption keys. The keys for the
236 // first and last rounds don't need to be mangled, but the remaining
237 // ones do -- and they all need to be reordered too.
239 // The plan of action, then, is to copy the final encryption round's
240 // keys into place first, then to do each of the intermediate rounds
241 // in reverse order, and finally do the first round.
243 // Do all of the heavy lifting with SSE registers. The order we're
244 // doing this in means that it's OK if we read or write too much, and
245 // there's easily enough buffer space for the over-enthusiastic reads
246 // and writes because the context has space for 32-byte blocks, which
247 // is our maximum and an exact fit for two SSE registers.
248 9: mov NR, [CTX + nr] // number of rounds
249 #if NKW_NEEDS_REFRESH
254 // If we retain NKW, then BLKSZ and BLKOFF are the same register
255 // because we won't need the former again.
260 lea SI, [CTX + w + 4*WHOLE(LRK)] // last round's keys
261 shl BLKOFF, 2 // block size (in bytes now)
263 // Copy the last encryption round's keys.
268 movdqu xmm0, [SI + 16]
269 movdqu [DI + 16], xmm0
271 // Update the loop variables and stop if we've finished.
272 0: add DI, WHOLE(BLKOFF)
273 sub SI, WHOLE(BLKOFF)
277 // Do another middle round's keys...
283 movdqu xmm0, [SI + 16]
285 movdqu [DI + 16], xmm0
288 // Finally do the first encryption round.
293 movdqu xmm0, [SI + 16]
294 movdqu [DI + 16], xmm0
296 // If the block size is not exactly four words then we must end-swap
297 // everything. We can use fancy SSE toys for this.
301 // Find the byte-reordering table.
303 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
305 #if NKW_NEEDS_REFRESH
306 // Calculate the number of subkey words again. (It's a good job
307 // we've got a fast multiplier.)
313 // End-swap the encryption keys.
317 // And the decryption keys.
328 #if CPUFAM_AMD64 && ABI_WIN
336 INTFUNC(endswap_block)
337 // End-swap NKW words starting at SI. The end-swapping table is
338 // already loaded into XMM5; and it's OK to work in 16-byte chunks.
364 ///--------------------------------------------------------------------------
365 /// Encrypting and decrypting blocks.
367 .macro encdec op, aes, koff
368 FUNC(rijndael_\op\()_x86ish_aesni)
371 // Arguments come in on the stack, and need to be collected. We
372 // don't have a shortage of registers.
383 #if CPUFAM_AMD64 && ABI_SYSV
384 // Arguments come in registers. All is good.
392 #if CPUFAM_AMD64 && ABI_WIN
393 // Arguments come in different registers.
403 // Find the magic endianness-swapping table.
405 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
413 // Initial whitening.
421 // Dispatch to the correct code.
460 movdqu xmm1, [K + 16]
464 movdqu xmm1, [K + 32]
468 movdqu xmm1, [K + 48]
472 movdqu xmm1, [K + 64]
476 movdqu xmm1, [K + 80]
480 movdqu xmm1, [K + 96]
484 movdqu xmm1, [K + 112]
488 movdqu xmm1, [K + 128]
492 movdqu xmm1, [K + 144]
493 \aes\()last xmm0, xmm1
495 // Unpermute the ciphertext block and store it.
510 encdec eblk, aesenc, w
511 encdec dblk, aesdec, wi
513 ///--------------------------------------------------------------------------
514 /// Random utilities.
517 // Abort the process because of a programming error. Indirecting
518 // through this point serves several purposes: (a) by CALLing, rather
519 // than branching to, `abort', we can save the return address, which
520 // might at least provide a hint as to what went wrong; (b) we don't
521 // have conditional CALLs (and they'd be big anyway); and (c) we can
522 // write a HLT here as a backstop against `abort' being mad.
531 ///--------------------------------------------------------------------------
543 ///----- That's all, folks --------------------------------------------------