Debian Codegraph @

cipher/rijndael-ssse3-amd64.c

raw
1 /* SSSE3 vector permutation AES for Libgcrypt
2 * Copyright (C) 2014-2017 Jussi Kivilinna <[email protected]>
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 *
20 * The code is based on the public domain library libvpaes version 0.5
21 * available at http://crypto.stanford.edu/vpaes/ and which carries
22 * this notice:
23 *
24 * libvpaes: constant-time SSSE3 AES encryption and decryption.
25 * version 0.5
26 *
27 * By Mike Hamburg, Stanford University, 2009. Public domain.
28 * I wrote essentially all of this code. I did not write the test
29 * vectors; they are the NIST known answer tests. I hereby release all
30 * the code and documentation here that I wrote into the public domain.
31 *
32 * This is an implementation of AES following my paper,
33 * "Accelerating AES with Vector Permute Instructions"
34 * CHES 2009; http://shiftleft.org/papers/vector_aes/
35 */
36
37 #include <config.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h> /* for memcmp() */
41
42 #include "types.h" /* for byte and u32 typedefs */
43 #include "g10lib.h"
44 #include "cipher.h"
45 #include "bufhelp.h"
46 #include "rijndael-internal.h"
47 #include "./cipher-internal.h"
48
49
50 #ifdef USE_SSSE3
51
52
53 #if _GCRY_GCC_VERSION >= 40400 /* 4.4 */
54 /* Prevent compiler from issuing SSE instructions between asm blocks. */
55 # pragma GCC target("no-sse")
56 #endif
57 #if __clang__
58 # pragma clang attribute push (__attribute__((target("no-sse"))), apply_to = function)
59 #endif
60
61
62 #define ALWAYS_INLINE inline __attribute__((always_inline))
63 #define NO_INSTRUMENT_FUNCTION __attribute__((no_instrument_function))
64
65 #define ASM_FUNC_ATTR NO_INSTRUMENT_FUNCTION
66 #define ASM_FUNC_ATTR_INLINE ASM_FUNC_ATTR ALWAYS_INLINE
67
68
69 /* Copy of ocb_get_l needed here as GCC is unable to inline ocb_get_l
70 because of 'pragma target'. */
71 static ASM_FUNC_ATTR_INLINE const unsigned char *
72 aes_ocb_get_l (gcry_cipher_hd_t c, u64 n)
73 {
74 unsigned long ntz;
75
76 /* Assumes that N != 0. */
77 asm ("rep;bsfl %k[low], %k[ntz]\n\t"
78 : [ntz] "=r" (ntz)
79 : [low] "r" ((unsigned long)n)
80 : "cc");
81
82 return c->u_mode.ocb.L[ntz];
83 }
84
85
86 /* Assembly functions in rijndael-ssse3-amd64-asm.S. Note that these
87 have custom calling convention (additional XMM parameters). */
88 extern void _gcry_aes_ssse3_enc_preload(void);
89 extern void _gcry_aes_ssse3_dec_preload(void);
90 extern void _gcry_aes_ssse3_schedule_core(const void *key, u64 keybits,
91 void *buffer, u64 decrypt,
92 u64 rotoffs);
93 extern void _gcry_aes_ssse3_encrypt_core(const void *key, u64 nrounds);
94 extern void _gcry_aes_ssse3_decrypt_core(const void *key, u64 nrounds);
95
96
97
98 /* Two macros to be called prior and after the use of SSSE3
99 instructions. There should be no external function calls between
100 the use of these macros. There purpose is to make sure that the
101 SSE registers are cleared and won't reveal any information about
102 the key or the data. */
103 #ifdef HAVE_COMPATIBLE_GCC_WIN64_PLATFORM_AS
104 # define SSSE3_STATE_SIZE (16 * 10)
105 /* XMM6-XMM15 are callee-saved registers on WIN64. */
106 # define vpaes_ssse3_prepare() \
107 asm volatile ("movdqu %%xmm6, 0*16(%0)\n\t" \
108 "movdqu %%xmm7, 1*16(%0)\n\t" \
109 "movdqu %%xmm8, 2*16(%0)\n\t" \
110 "movdqu %%xmm9, 3*16(%0)\n\t" \
111 "movdqu %%xmm10, 4*16(%0)\n\t" \
112 "movdqu %%xmm11, 5*16(%0)\n\t" \
113 "movdqu %%xmm12, 6*16(%0)\n\t" \
114 "movdqu %%xmm13, 7*16(%0)\n\t" \
115 "movdqu %%xmm14, 8*16(%0)\n\t" \
116 "movdqu %%xmm15, 9*16(%0)\n\t" \
117 : \
118 : "r" (ssse3_state) \
119 : "memory" )
120 # define vpaes_ssse3_cleanup() \
121 asm volatile ("pxor %%xmm0, %%xmm0 \n\t" \
122 "pxor %%xmm1, %%xmm1 \n\t" \
123 "pxor %%xmm2, %%xmm2 \n\t" \
124 "pxor %%xmm3, %%xmm3 \n\t" \
125 "pxor %%xmm4, %%xmm4 \n\t" \
126 "pxor %%xmm5, %%xmm5 \n\t" \
127 "movdqu 0*16(%0), %%xmm6 \n\t" \
128 "movdqu 1*16(%0), %%xmm7 \n\t" \
129 "movdqu 2*16(%0), %%xmm8 \n\t" \
130 "movdqu 3*16(%0), %%xmm9 \n\t" \
131 "movdqu 4*16(%0), %%xmm10 \n\t" \
132 "movdqu 5*16(%0), %%xmm11 \n\t" \
133 "movdqu 6*16(%0), %%xmm12 \n\t" \
134 "movdqu 7*16(%0), %%xmm13 \n\t" \
135 "movdqu 8*16(%0), %%xmm14 \n\t" \
136 "movdqu 9*16(%0), %%xmm15 \n\t" \
137 : \
138 : "r" (ssse3_state) \
139 : "memory" )
140 #else
141 # define SSSE3_STATE_SIZE 1
142 # define vpaes_ssse3_prepare() (void)ssse3_state
143 # define vpaes_ssse3_cleanup() \
144 asm volatile ("pxor %%xmm0, %%xmm0 \n\t" \
145 "pxor %%xmm1, %%xmm1 \n\t" \
146 "pxor %%xmm2, %%xmm2 \n\t" \
147 "pxor %%xmm3, %%xmm3 \n\t" \
148 "pxor %%xmm4, %%xmm4 \n\t" \
149 "pxor %%xmm5, %%xmm5 \n\t" \
150 "pxor %%xmm6, %%xmm6 \n\t" \
151 "pxor %%xmm7, %%xmm7 \n\t" \
152 "pxor %%xmm8, %%xmm8 \n\t" \
153 ::: "memory" )
154 #endif
155
156 #define vpaes_ssse3_prepare_enc() \
157 vpaes_ssse3_prepare(); \
158 _gcry_aes_ssse3_enc_preload();
159
160 #define vpaes_ssse3_prepare_dec() \
161 vpaes_ssse3_prepare(); \
162 _gcry_aes_ssse3_dec_preload();
163
164
165 void ASM_FUNC_ATTR
166 _gcry_aes_ssse3_do_setkey (RIJNDAEL_context *ctx, const byte *key)
167 {
168 unsigned int keybits = (ctx->rounds - 10) * 32 + 128;
169 byte ssse3_state[SSSE3_STATE_SIZE];
170
171 vpaes_ssse3_prepare();
172
173 _gcry_aes_ssse3_schedule_core(key, keybits, &ctx->keyschenc32[0][0], 0, 48);
174
175 /* Save key for setting up decryption. */
176 if (keybits > 192)
177 asm volatile ("movdqu (%[src]), %%xmm0\n\t"
178 "movdqu 16(%[src]), %%xmm1\n\t"
179 "movdqu %%xmm0, (%[dst])\n\t"
180 "movdqu %%xmm1, 16(%[dst])\n\t"
181 : /* No output */
182 : [dst] "r" (&ctx->keyschdec32[0][0]), [src] "r" (key)
183 : "memory" );
184 else if (keybits == 192)
185 asm volatile ("movdqu (%[src]), %%xmm0\n\t"
186 "movq 16(%[src]), %%xmm1\n\t"
187 "movdqu %%xmm0, (%[dst])\n\t"
188 "movq %%xmm1, 16(%[dst])\n\t"
189 : /* No output */
190 : [dst] "r" (&ctx->keyschdec32[0][0]), [src] "r" (key)
191 : "memory" );
192 else
193 asm volatile ("movdqu (%[src]), %%xmm0\n\t"
194 "movdqu %%xmm0, (%[dst])\n\t"
195 : /* No output */
196 : [dst] "r" (&ctx->keyschdec32[0][0]), [src] "r" (key)
197 : "memory" );
198
199 vpaes_ssse3_cleanup();
200 }
201
202
203 /* Make a decryption key from an encryption key. */
204 static ASM_FUNC_ATTR_INLINE void
205 do_ssse3_prepare_decryption (RIJNDAEL_context *ctx,
206 byte ssse3_state[SSSE3_STATE_SIZE])
207 {
208 unsigned int keybits = (ctx->rounds - 10) * 32 + 128;
209
210 vpaes_ssse3_prepare();
211
212 _gcry_aes_ssse3_schedule_core(&ctx->keyschdec32[0][0], keybits,
213 &ctx->keyschdec32[ctx->rounds][0], 1,
214 (keybits == 192) ? 0 : 32);
215
216 vpaes_ssse3_cleanup();
217 }
218
219 void ASM_FUNC_ATTR
220 _gcry_aes_ssse3_prepare_decryption (RIJNDAEL_context *ctx)
221 {
222 byte ssse3_state[SSSE3_STATE_SIZE];
223
224 do_ssse3_prepare_decryption(ctx, ssse3_state);
225 }
226
227
228 /* Encrypt one block using the Intel SSSE3 instructions. Block is input
229 * and output through SSE register xmm0. */
230 static ASM_FUNC_ATTR_INLINE void
231 do_vpaes_ssse3_enc (const RIJNDAEL_context *ctx, unsigned int nrounds)
232 {
233 _gcry_aes_ssse3_encrypt_core(ctx->keyschenc32, nrounds);
234 }
235
236
237 /* Decrypt one block using the Intel SSSE3 instructions. Block is input
238 * and output through SSE register xmm0. */
239 static ASM_FUNC_ATTR_INLINE void
240 do_vpaes_ssse3_dec (const RIJNDAEL_context *ctx, unsigned int nrounds)
241 {
242 _gcry_aes_ssse3_decrypt_core(ctx->keyschdec32, nrounds);
243 }
244
245
246 unsigned int ASM_FUNC_ATTR
247 _gcry_aes_ssse3_encrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
248 const unsigned char *src)
249 {
250 unsigned int nrounds = ctx->rounds;
251 byte ssse3_state[SSSE3_STATE_SIZE];
252
253 vpaes_ssse3_prepare_enc ();
254 asm volatile ("movdqu %[src], %%xmm0\n\t"
255 :
256 : [src] "m" (*src)
257 : "memory" );
258 do_vpaes_ssse3_enc (ctx, nrounds);
259 asm volatile ("movdqu %%xmm0, %[dst]\n\t"
260 : [dst] "=m" (*dst)
261 :
262 : "memory" );
263 vpaes_ssse3_cleanup ();
264 return 0;
265 }
266
267
268 void ASM_FUNC_ATTR
269 _gcry_aes_ssse3_cfb_enc (RIJNDAEL_context *ctx, unsigned char *iv,
270 unsigned char *outbuf, const unsigned char *inbuf,
271 size_t nblocks)
272 {
273 unsigned int nrounds = ctx->rounds;
274 byte ssse3_state[SSSE3_STATE_SIZE];
275
276 vpaes_ssse3_prepare_enc ();
277
278 asm volatile ("movdqu %[iv], %%xmm0\n\t"
279 : /* No output */
280 : [iv] "m" (*iv)
281 : "memory" );
282
283 for ( ;nblocks; nblocks-- )
284 {
285 do_vpaes_ssse3_enc (ctx, nrounds);
286
287 asm volatile ("movdqu %[inbuf], %%xmm1\n\t"
288 "pxor %%xmm1, %%xmm0\n\t"
289 "movdqu %%xmm0, %[outbuf]\n\t"
290 : [outbuf] "=m" (*outbuf)
291 : [inbuf] "m" (*inbuf)
292 : "memory" );
293
294 outbuf += BLOCKSIZE;
295 inbuf += BLOCKSIZE;
296 }
297
298 asm volatile ("movdqu %%xmm0, %[iv]\n\t"
299 : [iv] "=m" (*iv)
300 :
301 : "memory" );
302
303 vpaes_ssse3_cleanup ();
304 }
305
306
307 void ASM_FUNC_ATTR
308 _gcry_aes_ssse3_cbc_enc (RIJNDAEL_context *ctx, unsigned char *iv,
309 unsigned char *outbuf, const unsigned char *inbuf,
310 size_t nblocks, int cbc_mac)
311 {
312 unsigned int nrounds = ctx->rounds;
313 byte ssse3_state[SSSE3_STATE_SIZE];
314
315 vpaes_ssse3_prepare_enc ();
316
317 asm volatile ("movdqu %[iv], %%xmm7\n\t"
318 : /* No output */
319 : [iv] "m" (*iv)
320 : "memory" );
321
322 for ( ;nblocks; nblocks-- )
323 {
324 asm volatile ("movdqu %[inbuf], %%xmm0\n\t"
325 "pxor %%xmm7, %%xmm0\n\t"
326 : /* No output */
327 : [inbuf] "m" (*inbuf)
328 : "memory" );
329
330 do_vpaes_ssse3_enc (ctx, nrounds);
331
332 asm volatile ("movdqa %%xmm0, %%xmm7\n\t"
333 "movdqu %%xmm0, %[outbuf]\n\t"
334 : [outbuf] "=m" (*outbuf)
335 :
336 : "memory" );
337
338 inbuf += BLOCKSIZE;
339 if (!cbc_mac)
340 outbuf += BLOCKSIZE;
341 }
342
343 asm volatile ("movdqu %%xmm7, %[iv]\n\t"
344 : [iv] "=m" (*iv)
345 :
346 : "memory" );
347
348 vpaes_ssse3_cleanup ();
349 }
350
351
352 void ASM_FUNC_ATTR
353 _gcry_aes_ssse3_ctr_enc (RIJNDAEL_context *ctx, unsigned char *ctr,
354 unsigned char *outbuf, const unsigned char *inbuf,
355 size_t nblocks)
356 {
357 static const unsigned char be_mask[16] __attribute__ ((aligned (16))) =
358 { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
359 unsigned int nrounds = ctx->rounds;
360 byte ssse3_state[SSSE3_STATE_SIZE];
361 u64 ctrlow;
362
363 vpaes_ssse3_prepare_enc ();
364
365 asm volatile ("movdqa %[mask], %%xmm6\n\t" /* Preload mask */
366 "movdqa (%[ctr]), %%xmm7\n\t" /* Preload CTR */
367 "movq 8(%[ctr]), %q[ctrlow]\n\t"
368 "bswapq %q[ctrlow]\n\t"
369 : [ctrlow] "=r" (ctrlow)
370 : [mask] "m" (*be_mask),
371 [ctr] "r" (ctr)
372 : "memory", "cc");
373
374 for ( ;nblocks; nblocks-- )
375 {
376 asm volatile ("movdqa %%xmm7, %%xmm0\n\t" /* xmm0 := CTR (xmm7) */
377 "pcmpeqd %%xmm1, %%xmm1\n\t"
378 "psrldq $8, %%xmm1\n\t" /* xmm1 = -1 */
379
380 "pshufb %%xmm6, %%xmm7\n\t"
381 "psubq %%xmm1, %%xmm7\n\t" /* xmm7++ (big endian) */
382
383 /* detect if 64-bit carry handling is needed */
384 "incq %q[ctrlow]\n\t"
385 "jnz .Lno_carry%=\n\t"
386
387 "pslldq $8, %%xmm1\n\t" /* move lower 64-bit to high */
388 "psubq %%xmm1, %%xmm7\n\t" /* add carry to upper 64bits */
389
390 ".Lno_carry%=:\n\t"
391
392 "pshufb %%xmm6, %%xmm7\n\t"
393 : [ctrlow] "+r" (ctrlow)
394 :
395 : "cc", "memory");
396
397 do_vpaes_ssse3_enc (ctx, nrounds);
398
399 asm volatile ("movdqu %[src], %%xmm1\n\t" /* xmm1 := input */
400 "pxor %%xmm1, %%xmm0\n\t" /* EncCTR ^= input */
401 "movdqu %%xmm0, %[dst]" /* Store EncCTR. */
402 : [dst] "=m" (*outbuf)
403 : [src] "m" (*inbuf)
404 : "memory");
405
406 outbuf += BLOCKSIZE;
407 inbuf += BLOCKSIZE;
408 }
409
410 asm volatile ("movdqu %%xmm7, %[ctr]\n\t" /* Update CTR (mem). */
411 : [ctr] "=m" (*ctr)
412 :
413 : "memory" );
414
415 vpaes_ssse3_cleanup ();
416 }
417
418
419 unsigned int ASM_FUNC_ATTR
420 _gcry_aes_ssse3_decrypt (const RIJNDAEL_context *ctx, unsigned char *dst,
421 const unsigned char *src)
422 {
423 unsigned int nrounds = ctx->rounds;
424 byte ssse3_state[SSSE3_STATE_SIZE];
425
426 vpaes_ssse3_prepare_dec ();
427 asm volatile ("movdqu %[src], %%xmm0\n\t"
428 :
429 : [src] "m" (*src)
430 : "memory" );
431 do_vpaes_ssse3_dec (ctx, nrounds);
432 asm volatile ("movdqu %%xmm0, %[dst]\n\t"
433 : [dst] "=m" (*dst)
434 :
435 : "memory" );
436 vpaes_ssse3_cleanup ();
437 return 0;
438 }
439
440
441 void ASM_FUNC_ATTR
442 _gcry_aes_ssse3_cfb_dec (RIJNDAEL_context *ctx, unsigned char *iv,
443 unsigned char *outbuf, const unsigned char *inbuf,
444 size_t nblocks)
445 {
446 unsigned int nrounds = ctx->rounds;
447 byte ssse3_state[SSSE3_STATE_SIZE];
448
449 vpaes_ssse3_prepare_enc ();
450
451 asm volatile ("movdqu %[iv], %%xmm0\n\t"
452 : /* No output */
453 : [iv] "m" (*iv)
454 : "memory" );
455
456 for ( ;nblocks; nblocks-- )
457 {
458 do_vpaes_ssse3_enc (ctx, nrounds);
459
460 asm volatile ("movdqa %%xmm0, %%xmm6\n\t"
461 "movdqu %[inbuf], %%xmm0\n\t"
462 "pxor %%xmm0, %%xmm6\n\t"
463 "movdqu %%xmm6, %[outbuf]\n\t"
464 : [outbuf] "=m" (*outbuf)
465 : [inbuf] "m" (*inbuf)
466 : "memory" );
467
468 outbuf += BLOCKSIZE;
469 inbuf += BLOCKSIZE;
470 }
471
472 asm volatile ("movdqu %%xmm0, %[iv]\n\t"
473 : [iv] "=m" (*iv)
474 :
475 : "memory" );
476
477 vpaes_ssse3_cleanup ();
478 }
479
480
481 void ASM_FUNC_ATTR
482 _gcry_aes_ssse3_cbc_dec (RIJNDAEL_context *ctx, unsigned char *iv,
483 unsigned char *outbuf, const unsigned char *inbuf,
484 size_t nblocks)
485 {
486 unsigned int nrounds = ctx->rounds;
487 byte ssse3_state[SSSE3_STATE_SIZE];
488
489 if ( !ctx->decryption_prepared )
490 {
491 do_ssse3_prepare_decryption ( ctx, ssse3_state );
492 ctx->decryption_prepared = 1;
493 }
494
495 vpaes_ssse3_prepare_dec ();
496
497 asm volatile ("movdqu %[iv], %%xmm7\n\t" /* use xmm7 as fast IV storage */
498 : /* No output */
499 : [iv] "m" (*iv)
500 : "memory");
501
502 for ( ;nblocks; nblocks-- )
503 {
504 asm volatile ("movdqu %[inbuf], %%xmm0\n\t"
505 "movdqa %%xmm0, %%xmm6\n\t" /* use xmm6 as savebuf */
506 : /* No output */
507 : [inbuf] "m" (*inbuf)
508 : "memory");
509
510 do_vpaes_ssse3_dec (ctx, nrounds);
511
512 asm volatile ("pxor %%xmm7, %%xmm0\n\t" /* xor IV with output */
513 "movdqu %%xmm0, %[outbuf]\n\t"
514 "movdqu %%xmm6, %%xmm7\n\t" /* store savebuf as new IV */
515 : [outbuf] "=m" (*outbuf)
516 :
517 : "memory");
518
519 outbuf += BLOCKSIZE;
520 inbuf += BLOCKSIZE;
521 }
522
523 asm volatile ("movdqu %%xmm7, %[iv]\n\t" /* store IV */
524 : /* No output */
525 : [iv] "m" (*iv)
526 : "memory");
527
528 vpaes_ssse3_cleanup ();
529 }
530
531
532 static void ASM_FUNC_ATTR
533 ssse3_ocb_enc (gcry_cipher_hd_t c, void *outbuf_arg,
534 const void *inbuf_arg, size_t nblocks)
535 {
536 RIJNDAEL_context *ctx = (void *)&c->context.c;
537 unsigned char *outbuf = outbuf_arg;
538 const unsigned char *inbuf = inbuf_arg;
539 u64 n = c->u_mode.ocb.data_nblocks;
540 unsigned int nrounds = ctx->rounds;
541 byte ssse3_state[SSSE3_STATE_SIZE];
542
543 vpaes_ssse3_prepare_enc ();
544
545 /* Preload Offset and Checksum */
546 asm volatile ("movdqu %[iv], %%xmm7\n\t"
547 "movdqu %[ctr], %%xmm6\n\t"
548 : /* No output */
549 : [iv] "m" (*c->u_iv.iv),
550 [ctr] "m" (*c->u_ctr.ctr)
551 : "memory" );
552
553 for ( ;nblocks; nblocks-- )
554 {
555 const unsigned char *l;
556
557 l = aes_ocb_get_l(c, ++n);
558
559 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
560 /* Checksum_i = Checksum_{i-1} xor P_i */
561 /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
562 asm volatile ("movdqu %[l], %%xmm1\n\t"
563 "movdqu %[inbuf], %%xmm0\n\t"
564 "pxor %%xmm1, %%xmm7\n\t"
565 "pxor %%xmm0, %%xmm6\n\t"
566 "pxor %%xmm7, %%xmm0\n\t"
567 :
568 : [l] "m" (*l),
569 [inbuf] "m" (*inbuf)
570 : "memory" );
571
572 do_vpaes_ssse3_enc (ctx, nrounds);
573
574 asm volatile ("pxor %%xmm7, %%xmm0\n\t"
575 "movdqu %%xmm0, %[outbuf]\n\t"
576 : [outbuf] "=m" (*outbuf)
577 :
578 : "memory" );
579
580 inbuf += BLOCKSIZE;
581 outbuf += BLOCKSIZE;
582 }
583
584 c->u_mode.ocb.data_nblocks = n;
585 asm volatile ("movdqu %%xmm7, %[iv]\n\t"
586 "movdqu %%xmm6, %[ctr]\n\t"
587 : [iv] "=m" (*c->u_iv.iv),
588 [ctr] "=m" (*c->u_ctr.ctr)
589 :
590 : "memory" );
591
592 vpaes_ssse3_cleanup ();
593 }
594
595 static void ASM_FUNC_ATTR
596 ssse3_ocb_dec (gcry_cipher_hd_t c, void *outbuf_arg,
597 const void *inbuf_arg, size_t nblocks)
598 {
599 RIJNDAEL_context *ctx = (void *)&c->context.c;
600 unsigned char *outbuf = outbuf_arg;
601 const unsigned char *inbuf = inbuf_arg;
602 u64 n = c->u_mode.ocb.data_nblocks;
603 unsigned int nrounds = ctx->rounds;
604 byte ssse3_state[SSSE3_STATE_SIZE];
605
606 if ( !ctx->decryption_prepared )
607 {
608 do_ssse3_prepare_decryption ( ctx, ssse3_state );
609 ctx->decryption_prepared = 1;
610 }
611
612 vpaes_ssse3_prepare_dec ();
613
614 /* Preload Offset and Checksum */
615 asm volatile ("movdqu %[iv], %%xmm7\n\t"
616 "movdqu %[ctr], %%xmm6\n\t"
617 : /* No output */
618 : [iv] "m" (*c->u_iv.iv),
619 [ctr] "m" (*c->u_ctr.ctr)
620 : "memory" );
621
622 for ( ;nblocks; nblocks-- )
623 {
624 const unsigned char *l;
625
626 l = aes_ocb_get_l(c, ++n);
627
628 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
629 /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
630 /* Checksum_i = Checksum_{i-1} xor P_i */
631 asm volatile ("movdqu %[l], %%xmm1\n\t"
632 "movdqu %[inbuf], %%xmm0\n\t"
633 "pxor %%xmm1, %%xmm7\n\t"
634 "pxor %%xmm7, %%xmm0\n\t"
635 :
636 : [l] "m" (*l),
637 [inbuf] "m" (*inbuf)
638 : "memory" );
639
640 do_vpaes_ssse3_dec (ctx, nrounds);
641
642 asm volatile ("pxor %%xmm7, %%xmm0\n\t"
643 "pxor %%xmm0, %%xmm6\n\t"
644 "movdqu %%xmm0, %[outbuf]\n\t"
645 : [outbuf] "=m" (*outbuf)
646 :
647 : "memory" );
648
649 inbuf += BLOCKSIZE;
650 outbuf += BLOCKSIZE;
651 }
652
653 c->u_mode.ocb.data_nblocks = n;
654 asm volatile ("movdqu %%xmm7, %[iv]\n\t"
655 "movdqu %%xmm6, %[ctr]\n\t"
656 : [iv] "=m" (*c->u_iv.iv),
657 [ctr] "=m" (*c->u_ctr.ctr)
658 :
659 : "memory" );
660
661 vpaes_ssse3_cleanup ();
662 }
663
664
665 size_t ASM_FUNC_ATTR
666 _gcry_aes_ssse3_ocb_crypt(gcry_cipher_hd_t c, void *outbuf_arg,
667 const void *inbuf_arg, size_t nblocks, int encrypt)
668 {
669 if (encrypt)
670 ssse3_ocb_enc(c, outbuf_arg, inbuf_arg, nblocks);
671 else
672 ssse3_ocb_dec(c, outbuf_arg, inbuf_arg, nblocks);
673
674 return 0;
675 }
676
677
678 size_t ASM_FUNC_ATTR
679 _gcry_aes_ssse3_ocb_auth (gcry_cipher_hd_t c, const void *abuf_arg,
680 size_t nblocks)
681 {
682 RIJNDAEL_context *ctx = (void *)&c->context.c;
683 const unsigned char *abuf = abuf_arg;
684 u64 n = c->u_mode.ocb.aad_nblocks;
685 unsigned int nrounds = ctx->rounds;
686 byte ssse3_state[SSSE3_STATE_SIZE];
687
688 vpaes_ssse3_prepare_enc ();
689
690 /* Preload Offset and Sum */
691 asm volatile ("movdqu %[iv], %%xmm7\n\t"
692 "movdqu %[ctr], %%xmm6\n\t"
693 : /* No output */
694 : [iv] "m" (*c->u_mode.ocb.aad_offset),
695 [ctr] "m" (*c->u_mode.ocb.aad_sum)
696 : "memory" );
697
698 for ( ;nblocks; nblocks-- )
699 {
700 const unsigned char *l;
701
702 l = aes_ocb_get_l(c, ++n);
703
704 /* Offset_i = Offset_{i-1} xor L_{ntz(i)} */
705 /* Sum_i = Sum_{i-1} xor ENCIPHER(K, A_i xor Offset_i) */
706 asm volatile ("movdqu %[l], %%xmm1\n\t"
707 "movdqu %[abuf], %%xmm0\n\t"
708 "pxor %%xmm1, %%xmm7\n\t"
709 "pxor %%xmm7, %%xmm0\n\t"
710 :
711 : [l] "m" (*l),
712 [abuf] "m" (*abuf)
713 : "memory" );
714
715 do_vpaes_ssse3_enc (ctx, nrounds);
716
717 asm volatile ("pxor %%xmm0, %%xmm6\n\t"
718 :
719 :
720 : "memory" );
721
722 abuf += BLOCKSIZE;
723 }
724
725 c->u_mode.ocb.aad_nblocks = n;
726 asm volatile ("movdqu %%xmm7, %[iv]\n\t"
727 "movdqu %%xmm6, %[ctr]\n\t"
728 : [iv] "=m" (*c->u_mode.ocb.aad_offset),
729 [ctr] "=m" (*c->u_mode.ocb.aad_sum)
730 :
731 : "memory" );
732
733 vpaes_ssse3_cleanup ();
734
735 return 0;
736 }
737
738 #if __clang__
739 # pragma clang attribute pop
740 #endif
741
742 #endif /* USE_SSSE3 */