Home | History | Annotate | Line # | Download | only in x86
aes_sse2_impl.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: aes_sse2_impl.c,v 1.3 2020/06/30 20:32:11 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.3  riastrad __KERNEL_RCSID(1, "$NetBSD: aes_sse2_impl.c,v 1.3 2020/06/30 20:32:11 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 
     35  1.1  riastrad #include <crypto/aes/aes.h>
     36  1.1  riastrad #include <crypto/aes/arch/x86/aes_sse2.h>
     37  1.1  riastrad 
     38  1.3  riastrad #ifdef _KERNEL
     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.3  riastrad #else
     44  1.3  riastrad #include <cpuid.h>
     45  1.3  riastrad #define	fpu_kern_enter()	((void)0)
     46  1.3  riastrad #define	fpu_kern_leave()	((void)0)
     47  1.3  riastrad #endif
     48  1.1  riastrad 
     49  1.1  riastrad static void
     50  1.2  riastrad aes_sse2_setenckey_impl(struct aesenc *enc, const uint8_t *key,
     51  1.2  riastrad     uint32_t nrounds)
     52  1.1  riastrad {
     53  1.1  riastrad 
     54  1.1  riastrad 	fpu_kern_enter();
     55  1.2  riastrad 	aes_sse2_setkey(enc->aese_aes.aes_rk64, key, nrounds);
     56  1.1  riastrad 	fpu_kern_leave();
     57  1.1  riastrad }
     58  1.1  riastrad 
     59  1.1  riastrad static void
     60  1.2  riastrad aes_sse2_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
     61  1.2  riastrad     uint32_t nrounds)
     62  1.1  riastrad {
     63  1.1  riastrad 
     64  1.2  riastrad 	fpu_kern_enter();
     65  1.1  riastrad 	/*
     66  1.1  riastrad 	 * BearSSL computes InvMixColumns on the fly -- no need for
     67  1.1  riastrad 	 * distinct decryption round keys.
     68  1.1  riastrad 	 */
     69  1.1  riastrad 	aes_sse2_setkey(dec->aesd_aes.aes_rk64, key, nrounds);
     70  1.2  riastrad 	fpu_kern_leave();
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad static void
     74  1.2  riastrad aes_sse2_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     75  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
     76  1.1  riastrad {
     77  1.1  riastrad 
     78  1.1  riastrad 	fpu_kern_enter();
     79  1.2  riastrad 	aes_sse2_enc(enc, in, out, nrounds);
     80  1.1  riastrad 	fpu_kern_leave();
     81  1.1  riastrad }
     82  1.1  riastrad 
     83  1.1  riastrad static void
     84  1.2  riastrad aes_sse2_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
     85  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
     86  1.1  riastrad {
     87  1.1  riastrad 
     88  1.1  riastrad 	fpu_kern_enter();
     89  1.2  riastrad 	aes_sse2_dec(dec, in, out, nrounds);
     90  1.1  riastrad 	fpu_kern_leave();
     91  1.1  riastrad }
     92  1.1  riastrad 
     93  1.1  riastrad static void
     94  1.2  riastrad aes_sse2_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     95  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     96  1.1  riastrad     uint32_t nrounds)
     97  1.1  riastrad {
     98  1.1  riastrad 
     99  1.1  riastrad 	if (nbytes == 0)
    100  1.1  riastrad 		return;
    101  1.1  riastrad 	fpu_kern_enter();
    102  1.2  riastrad 	aes_sse2_cbc_enc(enc, in, out, nbytes, iv, nrounds);
    103  1.1  riastrad 	fpu_kern_leave();
    104  1.1  riastrad }
    105  1.1  riastrad 
    106  1.1  riastrad static void
    107  1.2  riastrad aes_sse2_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    108  1.2  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    109  1.1  riastrad     uint32_t nrounds)
    110  1.1  riastrad {
    111  1.1  riastrad 
    112  1.1  riastrad 	if (nbytes == 0)
    113  1.1  riastrad 		return;
    114  1.1  riastrad 	fpu_kern_enter();
    115  1.2  riastrad 	aes_sse2_cbc_dec(dec, in, out, nbytes, iv, nrounds);
    116  1.1  riastrad 	fpu_kern_leave();
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad static void
    120  1.2  riastrad aes_sse2_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    121  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    122  1.1  riastrad     uint32_t nrounds)
    123  1.1  riastrad {
    124  1.1  riastrad 
    125  1.1  riastrad 	if (nbytes == 0)
    126  1.1  riastrad 		return;
    127  1.1  riastrad 	fpu_kern_enter();
    128  1.2  riastrad 	aes_sse2_xts_enc(enc, in, out, nbytes, tweak, nrounds);
    129  1.1  riastrad 	fpu_kern_leave();
    130  1.1  riastrad }
    131  1.1  riastrad 
    132  1.1  riastrad static void
    133  1.2  riastrad aes_sse2_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    134  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    135  1.1  riastrad     uint32_t nrounds)
    136  1.1  riastrad {
    137  1.1  riastrad 
    138  1.1  riastrad 	if (nbytes == 0)
    139  1.1  riastrad 		return;
    140  1.1  riastrad 	fpu_kern_enter();
    141  1.2  riastrad 	aes_sse2_xts_dec(dec, in, out, nbytes, tweak, nrounds);
    142  1.1  riastrad 	fpu_kern_leave();
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.1  riastrad static int
    146  1.1  riastrad aes_sse2_probe(void)
    147  1.1  riastrad {
    148  1.1  riastrad 	int result = 0;
    149  1.1  riastrad 
    150  1.1  riastrad 	/* Verify that the CPU supports SSE and SSE2.  */
    151  1.3  riastrad #ifdef _KERNEL
    152  1.1  riastrad 	if (!i386_has_sse)
    153  1.1  riastrad 		return -1;
    154  1.1  riastrad 	if (!i386_has_sse2)
    155  1.1  riastrad 		return -1;
    156  1.3  riastrad #else
    157  1.3  riastrad 	unsigned eax, ebx, ecx, edx;
    158  1.3  riastrad 	if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
    159  1.3  riastrad 		return -1;
    160  1.3  riastrad 	if ((edx & bit_SSE) == 0)
    161  1.3  riastrad 		return -1;
    162  1.3  riastrad 	if ((edx & bit_SSE2) == 0)
    163  1.3  riastrad 		return -1;
    164  1.3  riastrad #endif
    165  1.1  riastrad 
    166  1.1  riastrad 	fpu_kern_enter();
    167  1.2  riastrad 	result = aes_sse2_selftest();
    168  1.1  riastrad 	fpu_kern_leave();
    169  1.1  riastrad 
    170  1.1  riastrad 	return result;
    171  1.1  riastrad }
    172  1.1  riastrad 
    173  1.1  riastrad struct aes_impl aes_sse2_impl = {
    174  1.1  riastrad 	.ai_name = "Intel SSE2 bitsliced",
    175  1.1  riastrad 	.ai_probe = aes_sse2_probe,
    176  1.2  riastrad 	.ai_setenckey = aes_sse2_setenckey_impl,
    177  1.2  riastrad 	.ai_setdeckey = aes_sse2_setdeckey_impl,
    178  1.2  riastrad 	.ai_enc = aes_sse2_enc_impl,
    179  1.2  riastrad 	.ai_dec = aes_sse2_dec_impl,
    180  1.2  riastrad 	.ai_cbc_enc = aes_sse2_cbc_enc_impl,
    181  1.2  riastrad 	.ai_cbc_dec = aes_sse2_cbc_dec_impl,
    182  1.2  riastrad 	.ai_xts_enc = aes_sse2_xts_enc_impl,
    183  1.2  riastrad 	.ai_xts_dec = aes_sse2_xts_dec_impl,
    184  1.1  riastrad };
    185