Home | History | Annotate | Line # | Download | only in x86
aes_sse2_impl.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: aes_sse2_impl.c,v 1.1 2020/06/29 23:47:54 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
      8  1.1  riastrad  * modification, are permitted provided that the following conditions
      9  1.1  riastrad  * are met:
     10  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     11  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     12  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  riastrad  */
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.1  riastrad __KERNEL_RCSID(1, "$NetBSD: aes_sse2_impl.c,v 1.1 2020/06/29 23:47:54 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/types.h>
     33  1.1  riastrad #include <sys/endian.h>
     34  1.1  riastrad #include <sys/systm.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include <crypto/aes/aes.h>
     37  1.1  riastrad #include <crypto/aes/arch/x86/aes_sse2.h>
     38  1.1  riastrad 
     39  1.1  riastrad #include <x86/cpu.h>
     40  1.1  riastrad #include <x86/cpuvar.h>
     41  1.1  riastrad #include <x86/fpu.h>
     42  1.1  riastrad #include <x86/specialreg.h>
     43  1.1  riastrad 
     44  1.1  riastrad #include "aes_sse2_impl.h"
     45  1.1  riastrad 
     46  1.1  riastrad static void
     47  1.1  riastrad aes_sse2_setkey(uint64_t rk[static 30], const void *key, uint32_t nrounds)
     48  1.1  riastrad {
     49  1.1  riastrad 	size_t key_len;
     50  1.1  riastrad 
     51  1.1  riastrad 	switch (nrounds) {
     52  1.1  riastrad 	case 10:
     53  1.1  riastrad 		key_len = 16;
     54  1.1  riastrad 		break;
     55  1.1  riastrad 	case 12:
     56  1.1  riastrad 		key_len = 24;
     57  1.1  riastrad 		break;
     58  1.1  riastrad 	case 14:
     59  1.1  riastrad 		key_len = 32;
     60  1.1  riastrad 		break;
     61  1.1  riastrad 	default:
     62  1.1  riastrad 		panic("invalid AES nrounds: %u", nrounds);
     63  1.1  riastrad 	}
     64  1.1  riastrad 
     65  1.1  riastrad 	fpu_kern_enter();
     66  1.1  riastrad 	aes_sse2_keysched(rk, key, key_len);
     67  1.1  riastrad 	fpu_kern_leave();
     68  1.1  riastrad }
     69  1.1  riastrad 
     70  1.1  riastrad static void
     71  1.1  riastrad aes_sse2_setenckey(struct aesenc *enc, const uint8_t *key, uint32_t nrounds)
     72  1.1  riastrad {
     73  1.1  riastrad 
     74  1.1  riastrad 	aes_sse2_setkey(enc->aese_aes.aes_rk64, key, nrounds);
     75  1.1  riastrad }
     76  1.1  riastrad 
     77  1.1  riastrad static void
     78  1.1  riastrad aes_sse2_setdeckey(struct aesdec *dec, const uint8_t *key, uint32_t nrounds)
     79  1.1  riastrad {
     80  1.1  riastrad 
     81  1.1  riastrad 	/*
     82  1.1  riastrad 	 * BearSSL computes InvMixColumns on the fly -- no need for
     83  1.1  riastrad 	 * distinct decryption round keys.
     84  1.1  riastrad 	 */
     85  1.1  riastrad 	aes_sse2_setkey(dec->aesd_aes.aes_rk64, key, nrounds);
     86  1.1  riastrad }
     87  1.1  riastrad 
     88  1.1  riastrad static void
     89  1.1  riastrad aes_sse2_enc(const struct aesenc *enc, const uint8_t in[static 16],
     90  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
     91  1.1  riastrad {
     92  1.1  riastrad 	uint64_t sk_exp[120];
     93  1.1  riastrad 	__m128i q[4];
     94  1.1  riastrad 
     95  1.1  riastrad 	fpu_kern_enter();
     96  1.1  riastrad 
     97  1.1  riastrad 	/* Expand round keys for bitslicing.  */
     98  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk64);
     99  1.1  riastrad 
    100  1.1  riastrad 	/* Load input block interleaved with garbage blocks.  */
    101  1.1  riastrad 	q[0] = aes_sse2_interleave_in(_mm_loadu_epi8(in));
    102  1.1  riastrad 	q[1] = q[2] = q[3] = _mm_setzero_si128();
    103  1.1  riastrad 
    104  1.1  riastrad 	/* Transform to bitslice, decrypt, transform from bitslice.  */
    105  1.1  riastrad 	aes_sse2_ortho(q);
    106  1.1  riastrad 	aes_sse2_bitslice_encrypt(nrounds, sk_exp, q);
    107  1.1  riastrad 	aes_sse2_ortho(q);
    108  1.1  riastrad 
    109  1.1  riastrad 	/* Store output block.  */
    110  1.1  riastrad 	_mm_storeu_epi8(out, aes_sse2_interleave_out(q[0]));
    111  1.1  riastrad 
    112  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    113  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    114  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    115  1.1  riastrad 
    116  1.1  riastrad 	fpu_kern_leave();
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad static void
    120  1.1  riastrad aes_sse2_dec(const struct aesdec *dec, const uint8_t in[static 16],
    121  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
    122  1.1  riastrad {
    123  1.1  riastrad 	uint64_t sk_exp[120];
    124  1.1  riastrad 	__m128i q[4];
    125  1.1  riastrad 
    126  1.1  riastrad 	fpu_kern_enter();
    127  1.1  riastrad 
    128  1.1  riastrad 	/* Expand round keys for bitslicing.  */
    129  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk64);
    130  1.1  riastrad 
    131  1.1  riastrad 	/* Load input block interleaved with garbage blocks.  */
    132  1.1  riastrad 	q[0] = aes_sse2_interleave_in(_mm_loadu_epi8(in));
    133  1.1  riastrad 	q[1] = q[2] = q[3] = _mm_setzero_si128();
    134  1.1  riastrad 
    135  1.1  riastrad 	/* Transform to bitslice, decrypt, transform from bitslice.  */
    136  1.1  riastrad 	aes_sse2_ortho(q);
    137  1.1  riastrad 	aes_sse2_bitslice_decrypt(nrounds, sk_exp, q);
    138  1.1  riastrad 	aes_sse2_ortho(q);
    139  1.1  riastrad 
    140  1.1  riastrad 	/* Store output block.  */
    141  1.1  riastrad 	_mm_storeu_epi8(out, aes_sse2_interleave_out(q[0]));
    142  1.1  riastrad 
    143  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    144  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    145  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    146  1.1  riastrad 
    147  1.1  riastrad 	fpu_kern_leave();
    148  1.1  riastrad }
    149  1.1  riastrad 
    150  1.1  riastrad static void
    151  1.1  riastrad aes_sse2_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
    152  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    153  1.1  riastrad     uint32_t nrounds)
    154  1.1  riastrad {
    155  1.1  riastrad 	uint64_t sk_exp[120];
    156  1.1  riastrad 	__m128i q[4];
    157  1.1  riastrad 	__m128i cv;
    158  1.1  riastrad 
    159  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    160  1.1  riastrad 
    161  1.1  riastrad 	/* Skip if there's nothing to do.  */
    162  1.1  riastrad 	if (nbytes == 0)
    163  1.1  riastrad 		return;
    164  1.1  riastrad 
    165  1.1  riastrad 	fpu_kern_enter();
    166  1.1  riastrad 
    167  1.1  riastrad 	/* Expand round keys for bitslicing.  */
    168  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk64);
    169  1.1  riastrad 
    170  1.1  riastrad 	/* Load the IV.  */
    171  1.1  riastrad 	cv = _mm_loadu_epi8(iv);
    172  1.1  riastrad 
    173  1.1  riastrad 	for (; nbytes; nbytes -= 16, in += 16, out += 16) {
    174  1.1  riastrad 		/* Load input block and apply CV.  */
    175  1.1  riastrad 		q[0] = aes_sse2_interleave_in(cv ^ _mm_loadu_epi8(in));
    176  1.1  riastrad 
    177  1.1  riastrad 		/* Transform to bitslice, encrypt, transform from bitslice.  */
    178  1.1  riastrad 		aes_sse2_ortho(q);
    179  1.1  riastrad 		aes_sse2_bitslice_encrypt(nrounds, sk_exp, q);
    180  1.1  riastrad 		aes_sse2_ortho(q);
    181  1.1  riastrad 
    182  1.1  riastrad 		/* Remember ciphertext as CV and store output block.  */
    183  1.1  riastrad 		cv = aes_sse2_interleave_out(q[0]);
    184  1.1  riastrad 		_mm_storeu_epi8(out, cv);
    185  1.1  riastrad 	}
    186  1.1  riastrad 
    187  1.1  riastrad 	/* Store updated IV.  */
    188  1.1  riastrad 	_mm_storeu_epi8(iv, cv);
    189  1.1  riastrad 
    190  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    191  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    192  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    193  1.1  riastrad 
    194  1.1  riastrad 	fpu_kern_leave();
    195  1.1  riastrad }
    196  1.1  riastrad 
    197  1.1  riastrad static void
    198  1.1  riastrad aes_sse2_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
    199  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t ivp[static 16],
    200  1.1  riastrad     uint32_t nrounds)
    201  1.1  riastrad {
    202  1.1  riastrad 	uint64_t sk_exp[120];
    203  1.1  riastrad 	__m128i q[4];
    204  1.1  riastrad 	__m128i cv, iv, w;
    205  1.1  riastrad 
    206  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    207  1.1  riastrad 
    208  1.1  riastrad 	/* Skip if there's nothing to do.  */
    209  1.1  riastrad 	if (nbytes == 0)
    210  1.1  riastrad 		return;
    211  1.1  riastrad 
    212  1.1  riastrad 	fpu_kern_enter();
    213  1.1  riastrad 
    214  1.1  riastrad 	/* Expand round keys for bitslicing.  */
    215  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk64);
    216  1.1  riastrad 
    217  1.1  riastrad 	/* Load the IV.  */
    218  1.1  riastrad 	iv = _mm_loadu_epi8(ivp);
    219  1.1  riastrad 
    220  1.1  riastrad 	/* Load the last cipher block.  */
    221  1.1  riastrad 	cv = _mm_loadu_epi8(in + nbytes - 16);
    222  1.1  riastrad 
    223  1.1  riastrad 	/* Store the updated IV.  */
    224  1.1  riastrad 	_mm_storeu_epi8(ivp, cv);
    225  1.1  riastrad 
    226  1.1  riastrad 	/* Process the last blocks if not an even multiple of four.  */
    227  1.1  riastrad 	if (nbytes % (4*16)) {
    228  1.1  riastrad 		unsigned n = (nbytes/16) % 4;
    229  1.1  riastrad 
    230  1.1  riastrad 		KASSERT(n > 0);
    231  1.1  riastrad 		KASSERT(n < 4);
    232  1.1  riastrad 
    233  1.1  riastrad 		q[1] = q[2] = q[3] = _mm_setzero_si128();
    234  1.1  riastrad 		q[n - 1] = aes_sse2_interleave_in(cv);
    235  1.1  riastrad 		switch (nbytes % 64) {
    236  1.1  riastrad 		case 48:
    237  1.1  riastrad 			w = _mm_loadu_epi8(in + nbytes - 32);
    238  1.1  riastrad 			q[1] = aes_sse2_interleave_in(w);
    239  1.1  riastrad 			/*FALLTHROUGH*/
    240  1.1  riastrad 		case 32:
    241  1.1  riastrad 			w = _mm_loadu_epi8(in + nbytes - 48);
    242  1.1  riastrad 			q[0] = aes_sse2_interleave_in(w);
    243  1.1  riastrad 			/*FALLTHROUGH*/
    244  1.1  riastrad 		case 16:
    245  1.1  riastrad 			break;
    246  1.1  riastrad 		}
    247  1.1  riastrad 
    248  1.1  riastrad 		/* Decrypt.  */
    249  1.1  riastrad 		aes_sse2_ortho(q);
    250  1.1  riastrad 		aes_sse2_bitslice_decrypt(nrounds, sk_exp, q);
    251  1.1  riastrad 		aes_sse2_ortho(q);
    252  1.1  riastrad 
    253  1.1  riastrad 		do {
    254  1.1  riastrad 			n--;
    255  1.1  riastrad 			w = aes_sse2_interleave_out(q[n]);
    256  1.1  riastrad 			if ((nbytes -= 16) == 0)
    257  1.1  riastrad 				goto out;
    258  1.1  riastrad 			cv = _mm_loadu_epi8(in + nbytes - 16);
    259  1.1  riastrad 			_mm_storeu_epi8(out + nbytes, w ^ cv);
    260  1.1  riastrad 		} while (n);
    261  1.1  riastrad 	}
    262  1.1  riastrad 
    263  1.1  riastrad 	for (;;) {
    264  1.1  riastrad 		KASSERT(nbytes >= 64);
    265  1.1  riastrad 		nbytes -= 64;
    266  1.1  riastrad 
    267  1.1  riastrad 		/*
    268  1.1  riastrad 		 * 1. Set up upper cipher block from cv.
    269  1.1  riastrad 		 * 2. Load lower cipher block into cv and set it up.
    270  1.1  riastrad 		 * 3. Decrypt.
    271  1.1  riastrad 		 */
    272  1.1  riastrad 		q[3] = aes_sse2_interleave_in(cv);
    273  1.1  riastrad 
    274  1.1  riastrad 		w = _mm_loadu_epi8(in + nbytes + 4*8);
    275  1.1  riastrad 		q[2] = aes_sse2_interleave_in(w);
    276  1.1  riastrad 
    277  1.1  riastrad 		w = _mm_loadu_epi8(in + nbytes + 4*4);
    278  1.1  riastrad 		q[1] = aes_sse2_interleave_in(w);
    279  1.1  riastrad 
    280  1.1  riastrad 		w = _mm_loadu_epi8(in + nbytes + 4*0);
    281  1.1  riastrad 		q[0] = aes_sse2_interleave_in(w);
    282  1.1  riastrad 
    283  1.1  riastrad 		aes_sse2_ortho(q);
    284  1.1  riastrad 		aes_sse2_bitslice_decrypt(nrounds, sk_exp, q);
    285  1.1  riastrad 		aes_sse2_ortho(q);
    286  1.1  riastrad 
    287  1.1  riastrad 		/* Store the upper output block.  */
    288  1.1  riastrad 		w = aes_sse2_interleave_out(q[3]);
    289  1.1  riastrad 		cv = _mm_loadu_epi8(in + nbytes + 4*8);
    290  1.1  riastrad 		_mm_storeu_epi8(out + nbytes + 4*12, w ^ cv);
    291  1.1  riastrad 
    292  1.1  riastrad 		/* Store the middle output blocks.  */
    293  1.1  riastrad 		w = aes_sse2_interleave_out(q[2]);
    294  1.1  riastrad 		cv = _mm_loadu_epi8(in + nbytes + 4*4);
    295  1.1  riastrad 		_mm_storeu_epi8(out + nbytes + 4*8, w ^ cv);
    296  1.1  riastrad 
    297  1.1  riastrad 		w = aes_sse2_interleave_out(q[1]);
    298  1.1  riastrad 		cv = _mm_loadu_epi8(in + nbytes + 4*0);
    299  1.1  riastrad 		_mm_storeu_epi8(out + nbytes + 4*4, w ^ cv);
    300  1.1  riastrad 
    301  1.1  riastrad 		/*
    302  1.1  riastrad 		 * Get the first output block, but don't load the CV
    303  1.1  riastrad 		 * yet -- it might be the previous ciphertext block, or
    304  1.1  riastrad 		 * it might be the IV.
    305  1.1  riastrad 		 */
    306  1.1  riastrad 		w = aes_sse2_interleave_out(q[0]);
    307  1.1  riastrad 
    308  1.1  riastrad 		/* Stop if we've reached the first output block.  */
    309  1.1  riastrad 		if (nbytes == 0)
    310  1.1  riastrad 			goto out;
    311  1.1  riastrad 
    312  1.1  riastrad 		/*
    313  1.1  riastrad 		 * Load the preceding cipher block, and apply it as the
    314  1.1  riastrad 		 * chaining value to this one.
    315  1.1  riastrad 		 */
    316  1.1  riastrad 		cv = _mm_loadu_epi8(in + nbytes - 16);
    317  1.1  riastrad 		_mm_storeu_epi8(out + nbytes, w ^ cv);
    318  1.1  riastrad 	}
    319  1.1  riastrad 
    320  1.1  riastrad out:	/* Store the first output block.  */
    321  1.1  riastrad 	_mm_storeu_epi8(out, w ^ iv);
    322  1.1  riastrad 
    323  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    324  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    325  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    326  1.1  riastrad 
    327  1.1  riastrad 	fpu_kern_leave();
    328  1.1  riastrad }
    329  1.1  riastrad 
    330  1.1  riastrad static inline __m128i
    331  1.1  riastrad aes_sse2_xts_update(__m128i t)
    332  1.1  riastrad {
    333  1.1  riastrad 	const __m128i one = _mm_set_epi64x(1, 1);
    334  1.1  riastrad 	__m128i s, m, c;
    335  1.1  riastrad 
    336  1.1  riastrad 	s = _mm_srli_epi64(t, 63);	/* 1 if high bit set else 0 */
    337  1.1  riastrad 	m = _mm_sub_epi64(s, one);	/* 0 if high bit set else -1 */
    338  1.1  riastrad 	m = _mm_shuffle_epi32(m, 0x4e);	/* swap halves */
    339  1.1  riastrad 	c = _mm_set_epi64x(1, 0x87);	/* carry */
    340  1.1  riastrad 
    341  1.1  riastrad 	return _mm_slli_epi64(t, 1) ^ (c & ~m);
    342  1.1  riastrad }
    343  1.1  riastrad 
    344  1.1  riastrad static int
    345  1.1  riastrad aes_sse2_xts_update_selftest(void)
    346  1.1  riastrad {
    347  1.1  riastrad 	static const struct {
    348  1.1  riastrad 		uint32_t in[4], out[4];
    349  1.1  riastrad 	} cases[] = {
    350  1.1  riastrad 		[0] = { {1}, {2} },
    351  1.1  riastrad 		[1] = { {0x80000000U,0,0,0}, {0,1,0,0} },
    352  1.1  riastrad 		[2] = { {0,0x80000000U,0,0}, {0,0,1,0} },
    353  1.1  riastrad 		[3] = { {0,0,0x80000000U,0}, {0,0,0,1} },
    354  1.1  riastrad 		[4] = { {0,0,0,0x80000000U}, {0x87,0,0,0} },
    355  1.1  riastrad 		[5] = { {0,0x80000000U,0,0x80000000U}, {0x87,0,1,0} },
    356  1.1  riastrad 	};
    357  1.1  riastrad 	unsigned i;
    358  1.1  riastrad 	uint32_t t[4];
    359  1.1  riastrad 	int result = 0;
    360  1.1  riastrad 
    361  1.1  riastrad 	for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
    362  1.1  riastrad 		t[0] = cases[i].in[0];
    363  1.1  riastrad 		t[1] = cases[i].in[1];
    364  1.1  riastrad 		t[2] = cases[i].in[2];
    365  1.1  riastrad 		t[3] = cases[i].in[3];
    366  1.1  riastrad 		_mm_storeu_epi8(t, aes_sse2_xts_update(_mm_loadu_epi8(t)));
    367  1.1  riastrad 		if (t[0] != cases[i].out[0] ||
    368  1.1  riastrad 		    t[1] != cases[i].out[1] ||
    369  1.1  riastrad 		    t[2] != cases[i].out[2] ||
    370  1.1  riastrad 		    t[3] != cases[i].out[3]) {
    371  1.1  riastrad 			printf("%s %u:"
    372  1.1  riastrad 			    " %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32"\n",
    373  1.1  riastrad 			    __func__, i, t[0], t[1], t[2], t[3]);
    374  1.1  riastrad 			result = -1;
    375  1.1  riastrad 		}
    376  1.1  riastrad 	}
    377  1.1  riastrad 
    378  1.1  riastrad 	return result;
    379  1.1  riastrad }
    380  1.1  riastrad 
    381  1.1  riastrad static void
    382  1.1  riastrad aes_sse2_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
    383  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    384  1.1  riastrad     uint32_t nrounds)
    385  1.1  riastrad {
    386  1.1  riastrad 	uint64_t sk_exp[120];
    387  1.1  riastrad 	__m128i q[4];
    388  1.1  riastrad 	__m128i w;
    389  1.1  riastrad 	__m128i t[5];
    390  1.1  riastrad 	unsigned i;
    391  1.1  riastrad 
    392  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    393  1.1  riastrad 
    394  1.1  riastrad 	/* Skip if there's nothing to do.  */
    395  1.1  riastrad 	if (nbytes == 0)
    396  1.1  riastrad 		return;
    397  1.1  riastrad 
    398  1.1  riastrad 	fpu_kern_enter();
    399  1.1  riastrad 
    400  1.1  riastrad 	/* Expand round keys for bitslicing.  */
    401  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, enc->aese_aes.aes_rk64);
    402  1.1  riastrad 
    403  1.1  riastrad 	/* Load tweak.  */
    404  1.1  riastrad 	t[0] = _mm_loadu_epi8(tweak);
    405  1.1  riastrad 
    406  1.1  riastrad 	/* Handle the first block separately if odd number.  */
    407  1.1  riastrad 	if (nbytes % (4*16)) {
    408  1.1  riastrad 		/* Load up the tweaked inputs.  */
    409  1.1  riastrad 		for (i = 0; i < (nbytes/16) % 4; i++) {
    410  1.1  riastrad 			w = _mm_loadu_epi8(in + 16*i) ^ t[i];
    411  1.1  riastrad 			q[i] = aes_sse2_interleave_in(w);
    412  1.1  riastrad 			t[i + 1] = aes_sse2_xts_update(t[i]);
    413  1.1  riastrad 		}
    414  1.1  riastrad 		for (; i < 4; i++)
    415  1.1  riastrad 			q[i] = _mm_setzero_si128();
    416  1.1  riastrad 
    417  1.1  riastrad 		/* Encrypt up to four blocks.  */
    418  1.1  riastrad 		aes_sse2_ortho(q);
    419  1.1  riastrad 		aes_sse2_bitslice_encrypt(nrounds, sk_exp, q);
    420  1.1  riastrad 		aes_sse2_ortho(q);
    421  1.1  riastrad 
    422  1.1  riastrad 		/* Store the tweaked outputs.  */
    423  1.1  riastrad 		for (i = 0; i < (nbytes/16) % 4; i++) {
    424  1.1  riastrad 			w = aes_sse2_interleave_out(q[i]);
    425  1.1  riastrad 			_mm_storeu_epi8(out + 16*i, w ^ t[i]);
    426  1.1  riastrad 		}
    427  1.1  riastrad 
    428  1.1  riastrad 		/* Advance to the next block.  */
    429  1.1  riastrad 		t[0] = t[i];
    430  1.1  riastrad 		in += nbytes % (4*16);
    431  1.1  riastrad 		out += nbytes % (4*16);
    432  1.1  riastrad 		nbytes -= nbytes % (4*16);
    433  1.1  riastrad 		if (nbytes == 0)
    434  1.1  riastrad 			goto out;
    435  1.1  riastrad 	}
    436  1.1  riastrad 
    437  1.1  riastrad 	do {
    438  1.1  riastrad 		KASSERT(nbytes % 64 == 0);
    439  1.1  riastrad 		KASSERT(nbytes >= 64);
    440  1.1  riastrad 
    441  1.1  riastrad 		/* Load up the tweaked inputs.  */
    442  1.1  riastrad 		for (i = 0; i < 4; i++) {
    443  1.1  riastrad 			w = _mm_loadu_epi8(in + 16*i) ^ t[i];
    444  1.1  riastrad 			q[i] = aes_sse2_interleave_in(w);
    445  1.1  riastrad 			t[i + 1] = aes_sse2_xts_update(t[i]);
    446  1.1  riastrad 		}
    447  1.1  riastrad 
    448  1.1  riastrad 		/* Encrypt four blocks.  */
    449  1.1  riastrad 		aes_sse2_ortho(q);
    450  1.1  riastrad 		aes_sse2_bitslice_encrypt(nrounds, sk_exp, q);
    451  1.1  riastrad 		aes_sse2_ortho(q);
    452  1.1  riastrad 
    453  1.1  riastrad 		/* Store the tweaked outputs.  */
    454  1.1  riastrad 		for (i = 0; i < 4; i++) {
    455  1.1  riastrad 			w = aes_sse2_interleave_out(q[i]);
    456  1.1  riastrad 			_mm_storeu_epi8(out + 16*i, w ^ t[i]);
    457  1.1  riastrad 		}
    458  1.1  riastrad 
    459  1.1  riastrad 		/* Advance to the next block.  */
    460  1.1  riastrad 		t[0] = t[4];
    461  1.1  riastrad 		in += 64;
    462  1.1  riastrad 		out += 64;
    463  1.1  riastrad 		nbytes -= 64;
    464  1.1  riastrad 	} while (nbytes);
    465  1.1  riastrad 
    466  1.1  riastrad out:	/* Store the updated tweak.  */
    467  1.1  riastrad 	_mm_storeu_epi8(tweak, t[0]);
    468  1.1  riastrad 
    469  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    470  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    471  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    472  1.1  riastrad 	explicit_memset(t, 0, sizeof t);
    473  1.1  riastrad 
    474  1.1  riastrad 	fpu_kern_leave();
    475  1.1  riastrad }
    476  1.1  riastrad 
    477  1.1  riastrad static void
    478  1.1  riastrad aes_sse2_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
    479  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    480  1.1  riastrad     uint32_t nrounds)
    481  1.1  riastrad {
    482  1.1  riastrad 	uint64_t sk_exp[120];
    483  1.1  riastrad 	__m128i q[4];
    484  1.1  riastrad 	__m128i w;
    485  1.1  riastrad 	__m128i t[5];
    486  1.1  riastrad 	unsigned i;
    487  1.1  riastrad 
    488  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    489  1.1  riastrad 
    490  1.1  riastrad 	/* Skip if there's nothing to do.  */
    491  1.1  riastrad 	if (nbytes == 0)
    492  1.1  riastrad 		return;
    493  1.1  riastrad 
    494  1.1  riastrad 	fpu_kern_enter();
    495  1.1  riastrad 
    496  1.1  riastrad 	/* Expand round keys for bitslicing.  */
    497  1.1  riastrad 	aes_sse2_skey_expand(sk_exp, nrounds, dec->aesd_aes.aes_rk64);
    498  1.1  riastrad 
    499  1.1  riastrad 	/* Load tweak.  */
    500  1.1  riastrad 	t[0] = _mm_loadu_epi8(tweak);
    501  1.1  riastrad 
    502  1.1  riastrad 	/* Handle the first block separately if odd number.  */
    503  1.1  riastrad 	if (nbytes % (4*16)) {
    504  1.1  riastrad 		/* Load up the tweaked inputs.  */
    505  1.1  riastrad 		for (i = 0; i < (nbytes/16) % 4; i++) {
    506  1.1  riastrad 			w = _mm_loadu_epi8(in + 16*i) ^ t[i];
    507  1.1  riastrad 			q[i] = aes_sse2_interleave_in(w);
    508  1.1  riastrad 			t[i + 1] = aes_sse2_xts_update(t[i]);
    509  1.1  riastrad 		}
    510  1.1  riastrad 		for (; i < 4; i++)
    511  1.1  riastrad 			q[i] = _mm_setzero_si128();
    512  1.1  riastrad 
    513  1.1  riastrad 		/* Decrypt up to four blocks.  */
    514  1.1  riastrad 		aes_sse2_ortho(q);
    515  1.1  riastrad 		aes_sse2_bitslice_decrypt(nrounds, sk_exp, q);
    516  1.1  riastrad 		aes_sse2_ortho(q);
    517  1.1  riastrad 
    518  1.1  riastrad 		/* Store the tweaked outputs.  */
    519  1.1  riastrad 		for (i = 0; i < (nbytes/16) % 4; i++) {
    520  1.1  riastrad 			w = aes_sse2_interleave_out(q[i]);
    521  1.1  riastrad 			_mm_storeu_epi8(out + 16*i, w ^ t[i]);
    522  1.1  riastrad 		}
    523  1.1  riastrad 
    524  1.1  riastrad 		/* Advance to the next block.  */
    525  1.1  riastrad 		t[0] = t[i];
    526  1.1  riastrad 		in += nbytes % (4*16);
    527  1.1  riastrad 		out += nbytes % (4*16);
    528  1.1  riastrad 		nbytes -= nbytes % (4*16);
    529  1.1  riastrad 		if (nbytes == 0)
    530  1.1  riastrad 			goto out;
    531  1.1  riastrad 	}
    532  1.1  riastrad 
    533  1.1  riastrad 	do {
    534  1.1  riastrad 		KASSERT(nbytes % 64 == 0);
    535  1.1  riastrad 		KASSERT(nbytes >= 64);
    536  1.1  riastrad 
    537  1.1  riastrad 		/* Load up the tweaked inputs.  */
    538  1.1  riastrad 		for (i = 0; i < 4; i++) {
    539  1.1  riastrad 			w = _mm_loadu_epi8(in + 16*i) ^ t[i];
    540  1.1  riastrad 			q[i] = aes_sse2_interleave_in(w);
    541  1.1  riastrad 			t[i + 1] = aes_sse2_xts_update(t[i]);
    542  1.1  riastrad 		}
    543  1.1  riastrad 
    544  1.1  riastrad 		/* Decrypt four blocks.  */
    545  1.1  riastrad 		aes_sse2_ortho(q);
    546  1.1  riastrad 		aes_sse2_bitslice_decrypt(nrounds, sk_exp, q);
    547  1.1  riastrad 		aes_sse2_ortho(q);
    548  1.1  riastrad 
    549  1.1  riastrad 		/* Store the tweaked outputs.  */
    550  1.1  riastrad 		for (i = 0; i < 4; i++) {
    551  1.1  riastrad 			w = aes_sse2_interleave_out(q[i]);
    552  1.1  riastrad 			_mm_storeu_epi8(out + 16*i, w ^ t[i]);
    553  1.1  riastrad 		}
    554  1.1  riastrad 
    555  1.1  riastrad 		/* Advance to the next block.  */
    556  1.1  riastrad 		t[0] = t[4];
    557  1.1  riastrad 		in += 64;
    558  1.1  riastrad 		out += 64;
    559  1.1  riastrad 		nbytes -= 64;
    560  1.1  riastrad 	} while (nbytes);
    561  1.1  riastrad 
    562  1.1  riastrad out:	/* Store the updated tweak.  */
    563  1.1  riastrad 	_mm_storeu_epi8(tweak, t[0]);
    564  1.1  riastrad 
    565  1.1  riastrad 	/* Paranoia: Zero temporary buffers.  */
    566  1.1  riastrad 	explicit_memset(sk_exp, 0, sizeof sk_exp);
    567  1.1  riastrad 	explicit_memset(q, 0, sizeof q);
    568  1.1  riastrad 	explicit_memset(t, 0, sizeof t);
    569  1.1  riastrad 
    570  1.1  riastrad 	fpu_kern_leave();
    571  1.1  riastrad }
    572  1.1  riastrad 
    573  1.1  riastrad static int
    574  1.1  riastrad aes_sse2_probe(void)
    575  1.1  riastrad {
    576  1.1  riastrad 	int result = 0;
    577  1.1  riastrad 
    578  1.1  riastrad 	/* Verify that the CPU supports SSE and SSE2.  */
    579  1.1  riastrad 	if (!i386_has_sse)
    580  1.1  riastrad 		return -1;
    581  1.1  riastrad 	if (!i386_has_sse2)
    582  1.1  riastrad 		return -1;
    583  1.1  riastrad 
    584  1.1  riastrad 	fpu_kern_enter();
    585  1.1  riastrad 
    586  1.1  riastrad 	if (aes_sse2_xts_update_selftest())
    587  1.1  riastrad 		result = -1;
    588  1.1  riastrad 
    589  1.1  riastrad 	fpu_kern_leave();
    590  1.1  riastrad 
    591  1.1  riastrad 	/* XXX test aes_sse2_bitslice_decrypt */
    592  1.1  riastrad 	/* XXX test aes_sse2_bitslice_encrypt */
    593  1.1  riastrad 	/* XXX test aes_sse2_keysched */
    594  1.1  riastrad 	/* XXX test aes_sse2_ortho */
    595  1.1  riastrad 	/* XXX test aes_sse2_skey_expand */
    596  1.1  riastrad 
    597  1.1  riastrad 	return result;
    598  1.1  riastrad }
    599  1.1  riastrad 
    600  1.1  riastrad struct aes_impl aes_sse2_impl = {
    601  1.1  riastrad 	.ai_name = "Intel SSE2 bitsliced",
    602  1.1  riastrad 	.ai_probe = aes_sse2_probe,
    603  1.1  riastrad 	.ai_setenckey = aes_sse2_setenckey,
    604  1.1  riastrad 	.ai_setdeckey = aes_sse2_setdeckey,
    605  1.1  riastrad 	.ai_enc = aes_sse2_enc,
    606  1.1  riastrad 	.ai_dec = aes_sse2_dec,
    607  1.1  riastrad 	.ai_cbc_enc = aes_sse2_cbc_enc,
    608  1.1  riastrad 	.ai_cbc_dec = aes_sse2_cbc_dec,
    609  1.1  riastrad 	.ai_xts_enc = aes_sse2_xts_enc,
    610  1.1  riastrad 	.ai_xts_dec = aes_sse2_xts_dec,
    611  1.1  riastrad };
    612