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"
33 ///--------------------------------------------------------------------------
34 /// External definitions.
37 .globl F(rijndael_rcon)
39 ///--------------------------------------------------------------------------
42 // Magic constants for shuffling.
47 ///--------------------------------------------------------------------------
53 /// The AESNI instructions implement a little-endian version of AES, but
54 /// Catacomb's internal interface presents as big-endian so as to work better
55 /// with things like GCM. We therefore maintain the round keys in
56 /// little-endian form, and have to end-swap blocks in and out.
58 /// For added amusement, the AESNI instructions don't implement the
59 /// larger-block versions of Rijndael, so we have to end-swap the keys if
60 /// we're preparing for one of those.
63 .equ maxrounds, 16 // maximum number of rounds
64 .equ maxblksz, 32 // maximum block size, in bytes
65 .equ kbufsz, maxblksz*(maxrounds + 1) // size of a key-schedule buffer
68 .equ nr, 0 // number of rounds
69 .equ w, nr + 4 // encryption key words
70 .equ wi, w + kbufsz // decryption key words
72 ///--------------------------------------------------------------------------
75 FUNC(rijndael_setup_x86_aesni)
77 // Initial state. We have four arguments:
78 // [esp + 20] is the context pointer
79 // [esp + 24] is the block size, in 32-bit words (4, 6, or 8)
80 // [esp + 28] points to the key material, unaligned
81 // [esp + 32] is the size of the key, in words
82 // The key size has already been checked for validity, and the number
83 // of rounds has been computed. Our job is only to fill in the `w'
91 // The initial round key material is taken directly from the input
92 // key, so copy it over.
93 mov ebp, [esp + 20] // context base pointer
94 mov ebx, [esp + 32] // key size, in words
100 // Find out other useful things.
101 mov edx, [ebp + nr] // number of rounds
103 imul edx, [esp + 24] // total key size in words
104 sub edx, ebx // offset by the key size
106 // Find the round constants.
108 leaext ecx, rijndael_rcon, ecx
110 // Prepare for the main loop.
112 mov eax, [esi + 4*ebx - 4] // most recent key word
113 lea edx, [esi + 4*edx] // limit, offset by one key expansion
115 // Main key expansion loop. The first word of each key-length chunk
116 // needs special treatment.
118 // This is rather tedious because the Intel `AESKEYGENASSIST'
119 // instruction is very strangely shaped. Firstly, it wants to
120 // operate on vast SSE registers, even though we're data-blocked from
121 // doing more than operation at a time unless we're doing two key
122 // schedules simultaneously -- and even then we can't do more than
123 // two, because the instruction ignores two of its input words
124 // entirely, and produces two different outputs for each of the other
125 // two. And secondly it insists on taking the magic round constant
126 // as an immediate, so it's kind of annoying if you're not
127 // open-coding the whole thing. It's much easier to leave that as
128 // zero and XOR in the round constant by hand.
130 pshufd xmm0, xmm0, ROTR
131 aeskeygenassist xmm1, xmm0, 0
132 pshufd xmm1, xmm1, ROTL
137 mov [esi + 4*ebx], eax
142 // The next three words are simple...
144 mov [esi + 4*ebx], eax
151 mov [esi + 4*ebx], eax
158 mov [esi + 4*ebx], eax
163 // Word 4. If the key is /more/ than 6 words long, then we must
164 // apply a substitution here.
170 pshufd xmm0, xmm0, ROTL
171 aeskeygenassist xmm1, xmm0, 0
174 mov [esi + 4*ebx], eax
183 mov [esi + 4*ebx], eax
192 mov [esi + 4*ebx], eax
201 mov [esi + 4*ebx], eax
206 // Must be done by now.
209 // Next job is to construct the decryption keys. The keys for the
210 // first and last rounds don't need to be mangled, but the remaining
211 // ones do -- and they all need to be reordered too.
213 // The plan of action, then, is to copy the final encryption round's
214 // keys into place first, then to do each of the intermediate rounds
215 // in reverse order, and finally do the first round.
217 // Do all of the heavy lifting with SSE registers. The order we're
218 // doing this in means that it's OK if we read or write too much, and
219 // there's easily enough buffer space for the over-enthusiastic reads
220 // and writes because the context has space for 32-byte blocks, which
221 // is our maximum and an exact fit for two SSE registers.
222 8: mov ecx, [ebp + nr] // number of rounds
223 mov ebx, [esp + 24] // block size (in words)
227 lea esi, [ebp + 4*edx + w] // last round's keys
228 shl ebx, 2 // block size (in bytes now)
230 // Copy the last encryption round's keys.
235 movdqu xmm0, [esi + 16]
236 movdqu [edi + 16], xmm0
238 // Update the loop variables and stop if we've finished.
244 // Do another middle round's keys...
250 movdqu xmm0, [esi + 16]
252 movdqu [edi + 16], xmm0
255 // Finally do the first encryption round.
256 0: movdqu xmm0, [esi]
260 movdqu xmm0, [esi + 16]
261 movdqu [edi + 16], xmm0
263 // If the block size is not exactly four words then we must end-swap
264 // everything. We can use fancy SSE toys for this.
268 // Find the byte-reordering table.
270 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
272 // Calculate the number of subkey words again. (It's a good job
273 // we've got a fast multiplier.)
276 imul ecx, [esp + 24] // total keys in words
278 // End-swap the encryption keys.
283 // And the decryption keys.
297 // End-swap ECX words starting at ESI. The end-swapping table is
298 // already loaded into XMM5; and it's OK to work in 16-byte chunks.
309 ///--------------------------------------------------------------------------
310 /// Encrypting and decrypting blocks.
312 .macro encdec op, aes, koff
313 FUNC(rijndael_\op\()_x86_aesni)
315 // On entry, we have:
316 // [esp + 4] points to the context block
317 // [esp + 8] points to the input data block
318 // [esp + 12] points to the output buffer
320 // Find the magic endianness-swapping table.
322 movdqa xmm5, [INTADDR(endswap_tab, ecx)]
324 // Load the input block and end-swap it. Also, start loading the
330 lea edx, [eax + \koff]
333 // Initial whitening.
338 // Dispatch to the correct code.
353 14: movdqu xmm1, [edx]
358 13: movdqu xmm1, [edx]
363 12: movdqu xmm1, [edx]
368 11: movdqu xmm1, [edx]
373 10: movdqu xmm1, [edx]
377 movdqu xmm1, [edx + 16]
381 movdqu xmm1, [edx + 32]
385 movdqu xmm1, [edx + 48]
389 movdqu xmm1, [edx + 64]
393 movdqu xmm1, [edx + 80]
397 movdqu xmm1, [edx + 96]
401 movdqu xmm1, [edx + 112]
405 movdqu xmm1, [edx + 128]
409 movdqu xmm1, [edx + 144]
410 \aes\()last xmm0, xmm1
412 // Unpermute the ciphertext block and store it.
423 encdec eblk, aesenc, w
424 encdec dblk, aesdec, wi
426 ///--------------------------------------------------------------------------
427 /// Random utilities.
430 // Abort the process because of a programming error. Indirecting
431 // through this point serves several purposes: (a) by CALLing, rather
432 // than branching to, `abort', we can save the return address, which
433 // might at least provide a hint as to what went wrong; (b) we don't
434 // have conditional CALLs (and they'd be big anyway); and (c) we can
435 // write a HLT here as a backstop against `abort' being mad.
436 bogus: callext F(abort)
442 ///--------------------------------------------------------------------------
452 ///----- That's all, folks --------------------------------------------------