Home | History | Annotate | Line # | Download | only in x86
aes_ssse3_subr.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: aes_ssse3_subr.c,v 1.1 2020/06/29 23:51:35 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_ssse3_subr.c,v 1.1 2020/06/29 23:51:35 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/systm.h>
     33  1.1  riastrad 
     34  1.1  riastrad #include <lib/libkern/libkern.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include "aes_ssse3_impl.h"
     37  1.1  riastrad 
     38  1.1  riastrad static inline __m128i
     39  1.1  riastrad loadblock(const void *in)
     40  1.1  riastrad {
     41  1.1  riastrad 	return _mm_loadu_epi8(in);
     42  1.1  riastrad }
     43  1.1  riastrad 
     44  1.1  riastrad static inline void
     45  1.1  riastrad storeblock(void *out, __m128i block)
     46  1.1  riastrad {
     47  1.1  riastrad 	_mm_storeu_epi8(out, block);
     48  1.1  riastrad }
     49  1.1  riastrad 
     50  1.1  riastrad void
     51  1.1  riastrad aes_ssse3_enc(const struct aesenc *enc, const uint8_t in[static 16],
     52  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
     53  1.1  riastrad {
     54  1.1  riastrad 	__m128i block;
     55  1.1  riastrad 
     56  1.1  riastrad 	block = loadblock(in);
     57  1.1  riastrad 	block = aes_ssse3_enc1(enc, block, nrounds);
     58  1.1  riastrad 	storeblock(out, block);
     59  1.1  riastrad }
     60  1.1  riastrad 
     61  1.1  riastrad void
     62  1.1  riastrad aes_ssse3_dec(const struct aesdec *dec, const uint8_t in[static 16],
     63  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
     64  1.1  riastrad {
     65  1.1  riastrad 	__m128i block;
     66  1.1  riastrad 
     67  1.1  riastrad 	block = loadblock(in);
     68  1.1  riastrad 	block = aes_ssse3_dec1(dec, block, nrounds);
     69  1.1  riastrad 	storeblock(out, block);
     70  1.1  riastrad }
     71  1.1  riastrad 
     72  1.1  riastrad void
     73  1.1  riastrad aes_ssse3_cbc_enc(const struct aesenc *enc, const uint8_t in[static 16],
     74  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     75  1.1  riastrad     uint32_t nrounds)
     76  1.1  riastrad {
     77  1.1  riastrad 	__m128i cv;
     78  1.1  riastrad 
     79  1.1  riastrad 	KASSERT(nbytes);
     80  1.1  riastrad 
     81  1.1  riastrad 	cv = loadblock(iv);
     82  1.1  riastrad 	for (; nbytes; nbytes -= 16, in += 16, out += 16) {
     83  1.1  riastrad 		cv ^= loadblock(in);
     84  1.1  riastrad 		cv = aes_ssse3_enc1(enc, cv, nrounds);
     85  1.1  riastrad 		storeblock(out, cv);
     86  1.1  riastrad 	}
     87  1.1  riastrad 	storeblock(iv, cv);
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad void
     91  1.1  riastrad aes_ssse3_cbc_dec(const struct aesdec *dec, const uint8_t in[static 16],
     92  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     93  1.1  riastrad     uint32_t nrounds)
     94  1.1  riastrad {
     95  1.1  riastrad 	__m128i iv0, cv, b;
     96  1.1  riastrad 
     97  1.1  riastrad 	KASSERT(nbytes);
     98  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
     99  1.1  riastrad 
    100  1.1  riastrad 	iv0 = loadblock(iv);
    101  1.1  riastrad 	cv = loadblock(in + nbytes - 16);
    102  1.1  riastrad 	storeblock(iv, cv);
    103  1.1  riastrad 
    104  1.1  riastrad 	for (;;) {
    105  1.1  riastrad 		b = aes_ssse3_dec1(dec, cv, nrounds);
    106  1.1  riastrad 		if ((nbytes -= 16) == 0)
    107  1.1  riastrad 			break;
    108  1.1  riastrad 		cv = loadblock(in + nbytes - 16);
    109  1.1  riastrad 		storeblock(out + nbytes, b ^ cv);
    110  1.1  riastrad 	}
    111  1.1  riastrad 	storeblock(out, b ^ iv0);
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad static inline __m128i
    115  1.1  riastrad aes_ssse3_xts_update(__m128i t)
    116  1.1  riastrad {
    117  1.1  riastrad 	const __m128i one = _mm_set_epi64x(1, 1);
    118  1.1  riastrad 	__m128i s, m, c;
    119  1.1  riastrad 
    120  1.1  riastrad 	s = _mm_srli_epi64(t, 63);	/* 1 if high bit set else 0 */
    121  1.1  riastrad 	m = _mm_sub_epi64(s, one);	/* 0 if high bit set else -1 */
    122  1.1  riastrad 	m = _mm_shuffle_epi32(m, 0x4e);	/* swap halves */
    123  1.1  riastrad 	c = _mm_set_epi64x(1, 0x87);	/* carry */
    124  1.1  riastrad 
    125  1.1  riastrad 	return _mm_slli_epi64(t, 1) ^ (c & ~m);
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad static int
    129  1.1  riastrad aes_ssse3_xts_update_selftest(void)
    130  1.1  riastrad {
    131  1.1  riastrad 	static const struct {
    132  1.1  riastrad 		uint32_t in[4], out[4];
    133  1.1  riastrad 	} cases[] = {
    134  1.1  riastrad 		[0] = { {1}, {2} },
    135  1.1  riastrad 		[1] = { {0x80000000U,0,0,0}, {0,1,0,0} },
    136  1.1  riastrad 		[2] = { {0,0x80000000U,0,0}, {0,0,1,0} },
    137  1.1  riastrad 		[3] = { {0,0,0x80000000U,0}, {0,0,0,1} },
    138  1.1  riastrad 		[4] = { {0,0,0,0x80000000U}, {0x87,0,0,0} },
    139  1.1  riastrad 		[5] = { {0,0x80000000U,0,0x80000000U}, {0x87,0,1,0} },
    140  1.1  riastrad 	};
    141  1.1  riastrad 	unsigned i;
    142  1.1  riastrad 	uint32_t t[4];
    143  1.1  riastrad 	int result = 0;
    144  1.1  riastrad 
    145  1.1  riastrad 	for (i = 0; i < sizeof(cases)/sizeof(cases[0]); i++) {
    146  1.1  riastrad 		t[0] = cases[i].in[0];
    147  1.1  riastrad 		t[1] = cases[i].in[1];
    148  1.1  riastrad 		t[2] = cases[i].in[2];
    149  1.1  riastrad 		t[3] = cases[i].in[3];
    150  1.1  riastrad 		storeblock(t, aes_ssse3_xts_update(loadblock(t)));
    151  1.1  riastrad 		if (t[0] != cases[i].out[0] ||
    152  1.1  riastrad 		    t[1] != cases[i].out[1] ||
    153  1.1  riastrad 		    t[2] != cases[i].out[2] ||
    154  1.1  riastrad 		    t[3] != cases[i].out[3]) {
    155  1.1  riastrad 			printf("%s %u:"
    156  1.1  riastrad 			    " %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32"\n",
    157  1.1  riastrad 			    __func__, i, t[0], t[1], t[2], t[3]);
    158  1.1  riastrad 			result = -1;
    159  1.1  riastrad 		}
    160  1.1  riastrad 	}
    161  1.1  riastrad 
    162  1.1  riastrad 	return result;
    163  1.1  riastrad }
    164  1.1  riastrad 
    165  1.1  riastrad void
    166  1.1  riastrad aes_ssse3_xts_enc(const struct aesenc *enc, const uint8_t in[static 16],
    167  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    168  1.1  riastrad     uint32_t nrounds)
    169  1.1  riastrad {
    170  1.1  riastrad 	__m128i t, b;
    171  1.1  riastrad 
    172  1.1  riastrad 	KASSERT(nbytes);
    173  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    174  1.1  riastrad 
    175  1.1  riastrad 	t = loadblock(tweak);
    176  1.1  riastrad 	for (; nbytes; nbytes -= 16, in += 16, out += 16) {
    177  1.1  riastrad 		b = t ^ loadblock(in);
    178  1.1  riastrad 		b = aes_ssse3_enc1(enc, b, nrounds);
    179  1.1  riastrad 		storeblock(out, t ^ b);
    180  1.1  riastrad 		t = aes_ssse3_xts_update(t);
    181  1.1  riastrad 	}
    182  1.1  riastrad 	storeblock(tweak, t);
    183  1.1  riastrad }
    184  1.1  riastrad 
    185  1.1  riastrad void
    186  1.1  riastrad aes_ssse3_xts_dec(const struct aesdec *dec, const uint8_t in[static 16],
    187  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    188  1.1  riastrad     uint32_t nrounds)
    189  1.1  riastrad {
    190  1.1  riastrad 	__m128i t, b;
    191  1.1  riastrad 
    192  1.1  riastrad 	KASSERT(nbytes);
    193  1.1  riastrad 	KASSERT(nbytes % 16 == 0);
    194  1.1  riastrad 
    195  1.1  riastrad 	t = loadblock(tweak);
    196  1.1  riastrad 	for (; nbytes; nbytes -= 16, in += 16, out += 16) {
    197  1.1  riastrad 		b = t ^ loadblock(in);
    198  1.1  riastrad 		b = aes_ssse3_dec1(dec, b, nrounds);
    199  1.1  riastrad 		storeblock(out, t ^ b);
    200  1.1  riastrad 		t = aes_ssse3_xts_update(t);
    201  1.1  riastrad 	}
    202  1.1  riastrad 	storeblock(tweak, t);
    203  1.1  riastrad }
    204  1.1  riastrad 
    205  1.1  riastrad int
    206  1.1  riastrad aes_ssse3_selftest(void)
    207  1.1  riastrad {
    208  1.1  riastrad 
    209  1.1  riastrad 	if (aes_ssse3_xts_update_selftest())
    210  1.1  riastrad 		return -1;
    211  1.1  riastrad 
    212  1.1  riastrad 	return 0;
    213  1.1  riastrad }
    214