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 .globl F(rijndael_rcon)
36 ///--------------------------------------------------------------------------
39 // Magic constants for shuffling.
44 ///--------------------------------------------------------------------------
50 /// The AESNI instructions implement a little-endian version of AES, but
51 /// Catacomb's internal interface presents as big-endian so as to work better
52 /// with things like GCM. We therefore maintain the round keys in
53 /// little-endian form, and have to end-swap blocks in and out.
55 /// For added amusement, the AESNI instructions don't implement the
56 /// larger-block versions of Rijndael, so we have to end-swap the keys if
57 /// we're preparing for one of those.
60 .equ maxrounds, 16 // maximum number of rounds
61 .equ maxblksz, 32 // maximum block size, in bytes
62 .equ kbufsz, maxblksz*(maxrounds + 1) // size of a key-schedule buffer
65 .equ nr, 0 // number of rounds
66 .equ w, nr + 4 // encryption key words
67 .equ wi, w + kbufsz // decryption key words
69 ///--------------------------------------------------------------------------
72 FUNC(rijndael_setup_x86ish_aesni)
75 // Arguments are on the stack. We'll need to stack the caller's
76 // register veriables, but we'll manage.
78 # define CTX ebp // context pointer
79 # define BLKSZ [esp + 24] // block size
81 # define SI esi // source pointer
82 # define DI edi // destination pointer
84 # define KSZ ebx // key size
85 # define KSZo ebx // ... as address offset
86 # define NKW edx // total number of key words
87 # define NKW_NEEDS_REFRESH 1 // ... needs recalculating
88 # define RCON ecx // round constants table
89 # define LIM edx // limit pointer
90 # define LIMn edx // ... as integer offset from base
92 # define NR ecx // number of rounds
93 # define LRK eax // distance to last key
94 # define LRKo eax // ... as address offset
95 # define BLKOFF edx // block size in bytes
96 # define BLKOFFo edx // ... as address offset
98 // Stack the caller's registers.
104 // Set up our own variables.
105 mov CTX, [esp + 20] // context base pointer
106 mov SI, [esp + 28] // key material
107 mov KSZ, [esp + 32] // key size, in words
110 #if CPUFAM_AMD64 && ABI_SYSV
111 // Arguments are in registers. We have plenty, but, to be honest,
112 // the initial register allocation is a bit annoying.
114 # define CTX r8 // context pointer
115 # define BLKSZ r9d // block size
117 # define SI rsi // source pointer
118 # define DI rdi // destination pointer
120 # define KSZ edx // key size
121 # define KSZo rdx // ... as address offset
122 # define NKW r10d // total number of key words
123 # define RCON rdi // round constants table
124 # define LIMn ecx // limit pointer
125 # define LIM rcx // ... as integer offset from base
127 # define NR ecx // number of rounds
128 # define LRK eax // distance to last key
129 # define LRKo rax // ... as address offset
130 # define BLKOFF r9d // block size in bytes
131 # define BLKOFFo r9 // ... as address offset
133 // Move arguments to more useful places.
134 mov CTX, rdi // context base pointer
135 mov BLKSZ, esi // block size in words
136 mov SI, rdx // key material
137 mov KSZ, ecx // key size, in words
140 #if CPUFAM_AMD64 && ABI_WIN
141 // Arguments are in different registers, and they're a little tight.
143 # define CTX r8 // context pointer
144 # define BLKSZ edx // block size
146 # define SI rsi // source pointer
147 # define DI rdi // destination pointer
149 # define KSZ r9d // key size
150 # define KSZo r9 // ... as address offset
151 # define NKW r10d // total number of key words
152 # define RCON rdi // round constants table
153 # define LIMn ecx // limit pointer
154 # define LIM rcx // ... as integer offset from base
156 # define NR ecx // number of rounds
157 # define LRK eax // distance to last key
158 # define LRKo rax // ... as address offset
159 # define BLKOFF edx // block size in bytes
160 # define BLKOFFo rdx // ... as address offset
162 // We'll need the index registers, which belong to the caller in this
167 // Move arguments to more useful places.
168 mov SI, r8 // key material
169 mov CTX, rcx // context base pointer
172 // The initial round key material is taken directly from the input
173 // key, so copy it over.
174 #if CPUFAM_AMD64 && ABI_SYSV
175 // We've been lucky. We already have a copy of the context pointer
176 // in rdi, and the key size in ecx.
184 // Find out other useful things.
185 mov NKW, [CTX + nr] // number of rounds
187 imul NKW, BLKSZ // total key size in words
188 #if !NKW_NEEDS_REFRESH
189 // If we can't keep NKW for later, then we use the same register for
190 // it and LIM, so this move is unnecessary.
193 sub LIMn, KSZ // offset by the key size
195 // Find the round constants.
197 leaext RCON, F(rijndael_rcon), ecx
199 // Prepare for the main loop.
201 mov eax, [SI + 4*KSZo - 4] // most recent key word
202 lea LIM, [SI + 4*LIM] // limit, offset by one key expansion
204 // Main key expansion loop. The first word of each key-length chunk
205 // needs special treatment.
207 // This is rather tedious because the Intel `AESKEYGENASSIST'
208 // instruction is very strangely shaped. Firstly, it wants to
209 // operate on vast SSE registers, even though we're data-blocked from
210 // doing more than operation at a time unless we're doing two key
211 // schedules simultaneously -- and even then we can't do more than
212 // two, because the instruction ignores two of its input words
213 // entirely, and produces two different outputs for each of the other
214 // two. And secondly it insists on taking the magic round constant
215 // as an immediate, so it's kind of annoying if you're not
216 // open-coding the whole thing. It's much easier to leave that as
217 // zero and XOR in the round constant by hand.
219 pshufd xmm0, xmm0, ROTR
220 aeskeygenassist xmm1, xmm0, 0
221 pshufd xmm1, xmm1, ROTL
226 mov [SI + 4*KSZo], eax
231 // The next three words are simple...
233 mov [SI + 4*KSZo], eax
240 mov [SI + 4*KSZo], eax
247 mov [SI + 4*KSZo], eax
252 // Word 4. If the key is /more/ than 6 words long, then we must
253 // apply a substitution here.
259 pshufd xmm0, xmm0, ROTL
260 aeskeygenassist xmm1, xmm0, 0
263 mov [SI + 4*KSZo], eax
272 mov [SI + 4*KSZo], eax
281 mov [SI + 4*KSZo], eax
290 mov [SI + 4*KSZo], eax
295 // Must be done by now.
298 // Next job is to construct the decryption keys. The keys for the
299 // first and last rounds don't need to be mangled, but the remaining
300 // ones do -- and they all need to be reordered too.
302 // The plan of action, then, is to copy the final encryption round's
303 // keys into place first, then to do each of the intermediate rounds
304 // in reverse order, and finally do the first round.
306 // Do all of the heavy lifting with SSE registers. The order we're
307 // doing this in means that it's OK if we read or write too much, and
308 // there's easily enough buffer space for the over-enthusiastic reads
309 // and writes because the context has space for 32-byte blocks, which
310 // is our maximum and an exact fit for two SSE registers.
311 8: mov NR, [CTX + nr] // number of rounds
312 #if NKW_NEEDS_REFRESH
317 // If we retain NKW, then BLKSZ and BLKOFF are the same register
318 // because we won't need the former again.
323 lea SI, [CTX + w + 4*LRKo] // last round's keys
324 shl BLKOFF, 2 // block size (in bytes now)
326 // Copy the last encryption round's keys.
331 movdqu xmm0, [SI + 16]
332 movdqu [DI + 16], xmm0
334 // Update the loop variables and stop if we've finished.
340 // Do another middle round's keys...
346 movdqu xmm0, [SI + 16]
348 movdqu [DI + 16], xmm0
351 // Finally do the first encryption round.
356 movdqu xmm0, [SI + 16]
357 movdqu [DI + 16], xmm0
359 // If the block size is not exactly four words then we must end-swap
360 // everything. We can use fancy SSE toys for this.
364 // Find the byte-reordering table.
366 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
368 #if NKW_NEEDS_REFRESH
369 // Calculate the number of subkey words again. (It's a good job
370 // we've got a fast multiplier.)
376 // End-swap the encryption keys.
380 // And the decryption keys.
391 #if CPUFAM_AMD64 && ABI_WIN
399 // End-swap NKW words starting at SI. The end-swapping table is
400 // already loaded into XMM5; and it's OK to work in 16-byte chunks.
427 ///--------------------------------------------------------------------------
428 /// Encrypting and decrypting blocks.
430 .macro encdec op, aes, koff
431 FUNC(rijndael_\op\()_x86ish_aesni)
433 // Find the magic endianness-swapping table.
435 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
438 // Arguments come in on the stack, and need to be collected. We
439 // don't have a shortage of registers.
450 #if CPUFAM_AMD64 && ABI_SYSV
451 // Arguments come in registers. All is good.
459 #if CPUFAM_AMD64 && ABI_WIN
460 // Arguments come in different registers.
474 // Initial whitening.
479 // Dispatch to the correct code.
518 movdqu xmm1, [K + 16]
522 movdqu xmm1, [K + 32]
526 movdqu xmm1, [K + 48]
530 movdqu xmm1, [K + 64]
534 movdqu xmm1, [K + 80]
538 movdqu xmm1, [K + 96]
542 movdqu xmm1, [K + 112]
546 movdqu xmm1, [K + 128]
550 movdqu xmm1, [K + 144]
551 \aes\()last xmm0, xmm1
553 // Unpermute the ciphertext block and store it.
571 encdec eblk, aesenc, w
572 encdec dblk, aesdec, wi
574 ///--------------------------------------------------------------------------
575 /// Random utilities.
578 // Abort the process because of a programming error. Indirecting
579 // through this point serves several purposes: (a) by CALLing, rather
580 // than branching to, `abort', we can save the return address, which
581 // might at least provide a hint as to what went wrong; (b) we don't
582 // have conditional CALLs (and they'd be big anyway); and (c) we can
583 // write a HLT here as a backstop against `abort' being mad.
584 bogus: callext F(abort)
590 ///--------------------------------------------------------------------------
600 ///----- That's all, folks --------------------------------------------------