Home | History | Annotate | Line # | Download | only in x86
aes_sse2_impl.c revision 1.4
      1 /*	$NetBSD: aes_sse2_impl.c,v 1.4 2020/07/25 22:12:57 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(1, "$NetBSD: aes_sse2_impl.c,v 1.4 2020/07/25 22:12:57 riastradh Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/endian.h>
     34 
     35 #include <crypto/aes/aes.h>
     36 #include <crypto/aes/aes_impl.h>
     37 #include <crypto/aes/arch/x86/aes_sse2.h>
     38 
     39 #ifdef _KERNEL
     40 #include <x86/cpu.h>
     41 #include <x86/cpuvar.h>
     42 #include <x86/fpu.h>
     43 #include <x86/specialreg.h>
     44 #else
     45 #include <cpuid.h>
     46 #define	fpu_kern_enter()	((void)0)
     47 #define	fpu_kern_leave()	((void)0)
     48 #endif
     49 
     50 static void
     51 aes_sse2_setenckey_impl(struct aesenc *enc, const uint8_t *key,
     52     uint32_t nrounds)
     53 {
     54 
     55 	fpu_kern_enter();
     56 	aes_sse2_setkey(enc->aese_aes.aes_rk64, key, nrounds);
     57 	fpu_kern_leave();
     58 }
     59 
     60 static void
     61 aes_sse2_setdeckey_impl(struct aesdec *dec, const uint8_t *key,
     62     uint32_t nrounds)
     63 {
     64 
     65 	fpu_kern_enter();
     66 	/*
     67 	 * BearSSL computes InvMixColumns on the fly -- no need for
     68 	 * distinct decryption round keys.
     69 	 */
     70 	aes_sse2_setkey(dec->aesd_aes.aes_rk64, key, nrounds);
     71 	fpu_kern_leave();
     72 }
     73 
     74 static void
     75 aes_sse2_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     76     uint8_t out[static 16], uint32_t nrounds)
     77 {
     78 
     79 	fpu_kern_enter();
     80 	aes_sse2_enc(enc, in, out, nrounds);
     81 	fpu_kern_leave();
     82 }
     83 
     84 static void
     85 aes_sse2_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
     86     uint8_t out[static 16], uint32_t nrounds)
     87 {
     88 
     89 	fpu_kern_enter();
     90 	aes_sse2_dec(dec, in, out, nrounds);
     91 	fpu_kern_leave();
     92 }
     93 
     94 static void
     95 aes_sse2_cbc_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
     96     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
     97     uint32_t nrounds)
     98 {
     99 
    100 	if (nbytes == 0)
    101 		return;
    102 	fpu_kern_enter();
    103 	aes_sse2_cbc_enc(enc, in, out, nbytes, iv, nrounds);
    104 	fpu_kern_leave();
    105 }
    106 
    107 static void
    108 aes_sse2_cbc_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    109     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    110     uint32_t nrounds)
    111 {
    112 
    113 	if (nbytes == 0)
    114 		return;
    115 	fpu_kern_enter();
    116 	aes_sse2_cbc_dec(dec, in, out, nbytes, iv, nrounds);
    117 	fpu_kern_leave();
    118 }
    119 
    120 static void
    121 aes_sse2_xts_enc_impl(const struct aesenc *enc, const uint8_t in[static 16],
    122     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    123     uint32_t nrounds)
    124 {
    125 
    126 	if (nbytes == 0)
    127 		return;
    128 	fpu_kern_enter();
    129 	aes_sse2_xts_enc(enc, in, out, nbytes, tweak, nrounds);
    130 	fpu_kern_leave();
    131 }
    132 
    133 static void
    134 aes_sse2_xts_dec_impl(const struct aesdec *dec, const uint8_t in[static 16],
    135     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    136     uint32_t nrounds)
    137 {
    138 
    139 	if (nbytes == 0)
    140 		return;
    141 	fpu_kern_enter();
    142 	aes_sse2_xts_dec(dec, in, out, nbytes, tweak, nrounds);
    143 	fpu_kern_leave();
    144 }
    145 
    146 static int
    147 aes_sse2_probe(void)
    148 {
    149 	int result = 0;
    150 
    151 	/* Verify that the CPU supports SSE and SSE2.  */
    152 #ifdef _KERNEL
    153 	if (!i386_has_sse)
    154 		return -1;
    155 	if (!i386_has_sse2)
    156 		return -1;
    157 #else
    158 	unsigned eax, ebx, ecx, edx;
    159 	if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx))
    160 		return -1;
    161 	if ((edx & bit_SSE) == 0)
    162 		return -1;
    163 	if ((edx & bit_SSE2) == 0)
    164 		return -1;
    165 #endif
    166 
    167 	fpu_kern_enter();
    168 	result = aes_sse2_selftest();
    169 	fpu_kern_leave();
    170 
    171 	return result;
    172 }
    173 
    174 struct aes_impl aes_sse2_impl = {
    175 	.ai_name = "Intel SSE2 bitsliced",
    176 	.ai_probe = aes_sse2_probe,
    177 	.ai_setenckey = aes_sse2_setenckey_impl,
    178 	.ai_setdeckey = aes_sse2_setdeckey_impl,
    179 	.ai_enc = aes_sse2_enc_impl,
    180 	.ai_dec = aes_sse2_dec_impl,
    181 	.ai_cbc_enc = aes_sse2_cbc_enc_impl,
    182 	.ai_cbc_dec = aes_sse2_cbc_dec_impl,
    183 	.ai_xts_enc = aes_sse2_xts_enc_impl,
    184 	.ai_xts_dec = aes_sse2_xts_dec_impl,
    185 };
    186