Home | History | Annotate | Line # | Download | only in aes
aes_impl.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: aes_impl.c,v 1.1 2020/06/29 23:27:52 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_impl.c,v 1.1 2020/06/29 23:27:52 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/types.h>
     33  1.1  riastrad #include <sys/kernel.h>
     34  1.1  riastrad #include <sys/module.h>
     35  1.1  riastrad #include <sys/once.h>
     36  1.1  riastrad #include <sys/systm.h>
     37  1.1  riastrad 
     38  1.1  riastrad #include <crypto/aes/aes.h>
     39  1.1  riastrad #include <crypto/aes/aes_bear.h> /* default implementation */
     40  1.1  riastrad 
     41  1.1  riastrad static const struct aes_impl	*aes_md_impl	__read_mostly;
     42  1.1  riastrad static const struct aes_impl	*aes_impl	__read_mostly;
     43  1.1  riastrad 
     44  1.1  riastrad /*
     45  1.1  riastrad  * The timing of AES implementation selection is finicky:
     46  1.1  riastrad  *
     47  1.1  riastrad  *	1. It has to be done _after_ cpu_attach for implementations,
     48  1.1  riastrad  *	   such as AES-NI, that rely on fpu initialization done by
     49  1.1  riastrad  *	   fpu_attach.
     50  1.1  riastrad  *
     51  1.1  riastrad  *	2. It has to be done _before_ the cgd self-tests or anything
     52  1.1  riastrad  *	   else that might call AES.
     53  1.1  riastrad  *
     54  1.1  riastrad  * For the moment, doing it in module init works.  However, if a
     55  1.1  riastrad  * driver-class module depended on the aes module, that would break.
     56  1.1  riastrad  */
     57  1.1  riastrad 
     58  1.1  riastrad static int
     59  1.1  riastrad aes_select(void)
     60  1.1  riastrad {
     61  1.1  riastrad 
     62  1.1  riastrad 	KASSERT(aes_impl == NULL);
     63  1.1  riastrad 
     64  1.1  riastrad 	if (aes_md_impl) {
     65  1.1  riastrad 		if (aes_selftest(aes_md_impl))
     66  1.1  riastrad 			aprint_error("aes: self-test failed: %s\n",
     67  1.1  riastrad 			    aes_md_impl->ai_name);
     68  1.1  riastrad 		else
     69  1.1  riastrad 			aes_impl = aes_md_impl;
     70  1.1  riastrad 	}
     71  1.1  riastrad 	if (aes_impl == NULL) {
     72  1.1  riastrad 		if (aes_selftest(&aes_bear_impl))
     73  1.1  riastrad 			aprint_error("aes: self-test failed: %s\n",
     74  1.1  riastrad 			    aes_bear_impl.ai_name);
     75  1.1  riastrad 		else
     76  1.1  riastrad 			aes_impl = &aes_bear_impl;
     77  1.1  riastrad 	}
     78  1.1  riastrad 	if (aes_impl == NULL)
     79  1.1  riastrad 		panic("AES self-tests failed");
     80  1.1  riastrad 
     81  1.1  riastrad 	aprint_normal("aes: %s\n", aes_impl->ai_name);
     82  1.1  riastrad 	return 0;
     83  1.1  riastrad }
     84  1.1  riastrad 
     85  1.1  riastrad MODULE(MODULE_CLASS_MISC, aes, NULL);
     86  1.1  riastrad 
     87  1.1  riastrad static int
     88  1.1  riastrad aes_modcmd(modcmd_t cmd, void *opaque)
     89  1.1  riastrad {
     90  1.1  riastrad 
     91  1.1  riastrad 	switch (cmd) {
     92  1.1  riastrad 	case MODULE_CMD_INIT:
     93  1.1  riastrad 		return aes_select();
     94  1.1  riastrad 	case MODULE_CMD_FINI:
     95  1.1  riastrad 		return 0;
     96  1.1  riastrad 	default:
     97  1.1  riastrad 		return ENOTTY;
     98  1.1  riastrad 	}
     99  1.1  riastrad }
    100  1.1  riastrad 
    101  1.1  riastrad static void
    102  1.1  riastrad aes_guarantee_selected(void)
    103  1.1  riastrad {
    104  1.1  riastrad #if 0
    105  1.1  riastrad 	static once_t once;
    106  1.1  riastrad 	int error;
    107  1.1  riastrad 
    108  1.1  riastrad 	error = RUN_ONCE(&once, aes_select);
    109  1.1  riastrad 	KASSERT(error == 0);
    110  1.1  riastrad #endif
    111  1.1  riastrad }
    112  1.1  riastrad 
    113  1.1  riastrad void
    114  1.1  riastrad aes_md_init(const struct aes_impl *impl)
    115  1.1  riastrad {
    116  1.1  riastrad 
    117  1.1  riastrad 	KASSERT(cold);
    118  1.1  riastrad 	KASSERTMSG(aes_impl == NULL,
    119  1.1  riastrad 	    "AES implementation `%s' already chosen, can't offer `%s'",
    120  1.1  riastrad 	    aes_impl->ai_name, impl->ai_name);
    121  1.1  riastrad 	KASSERTMSG(aes_md_impl == NULL,
    122  1.1  riastrad 	    "AES implementation `%s' already offered, can't offer `%s'",
    123  1.1  riastrad 	    aes_md_impl->ai_name, impl->ai_name);
    124  1.1  riastrad 
    125  1.1  riastrad 	aes_md_impl = impl;
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad static void
    129  1.1  riastrad aes_setenckey(struct aesenc *enc, const uint8_t key[static 16],
    130  1.1  riastrad     uint32_t nrounds)
    131  1.1  riastrad {
    132  1.1  riastrad 
    133  1.1  riastrad 	aes_guarantee_selected();
    134  1.1  riastrad 	aes_impl->ai_setenckey(enc, key, nrounds);
    135  1.1  riastrad }
    136  1.1  riastrad 
    137  1.1  riastrad uint32_t
    138  1.1  riastrad aes_setenckey128(struct aesenc *enc, const uint8_t key[static 16])
    139  1.1  riastrad {
    140  1.1  riastrad 	uint32_t nrounds = AES_128_NROUNDS;
    141  1.1  riastrad 
    142  1.1  riastrad 	aes_setenckey(enc, key, nrounds);
    143  1.1  riastrad 	return nrounds;
    144  1.1  riastrad }
    145  1.1  riastrad 
    146  1.1  riastrad uint32_t
    147  1.1  riastrad aes_setenckey192(struct aesenc *enc, const uint8_t key[static 24])
    148  1.1  riastrad {
    149  1.1  riastrad 	uint32_t nrounds = AES_192_NROUNDS;
    150  1.1  riastrad 
    151  1.1  riastrad 	aes_setenckey(enc, key, nrounds);
    152  1.1  riastrad 	return nrounds;
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad uint32_t
    156  1.1  riastrad aes_setenckey256(struct aesenc *enc, const uint8_t key[static 32])
    157  1.1  riastrad {
    158  1.1  riastrad 	uint32_t nrounds = AES_256_NROUNDS;
    159  1.1  riastrad 
    160  1.1  riastrad 	aes_setenckey(enc, key, nrounds);
    161  1.1  riastrad 	return nrounds;
    162  1.1  riastrad }
    163  1.1  riastrad 
    164  1.1  riastrad static void
    165  1.1  riastrad aes_setdeckey(struct aesdec *dec, const uint8_t key[static 16],
    166  1.1  riastrad     uint32_t nrounds)
    167  1.1  riastrad {
    168  1.1  riastrad 
    169  1.1  riastrad 	aes_guarantee_selected();
    170  1.1  riastrad 	aes_impl->ai_setdeckey(dec, key, nrounds);
    171  1.1  riastrad }
    172  1.1  riastrad 
    173  1.1  riastrad uint32_t
    174  1.1  riastrad aes_setdeckey128(struct aesdec *dec, const uint8_t key[static 16])
    175  1.1  riastrad {
    176  1.1  riastrad 	uint32_t nrounds = AES_128_NROUNDS;
    177  1.1  riastrad 
    178  1.1  riastrad 	aes_setdeckey(dec, key, nrounds);
    179  1.1  riastrad 	return nrounds;
    180  1.1  riastrad }
    181  1.1  riastrad 
    182  1.1  riastrad uint32_t
    183  1.1  riastrad aes_setdeckey192(struct aesdec *dec, const uint8_t key[static 24])
    184  1.1  riastrad {
    185  1.1  riastrad 	uint32_t nrounds = AES_192_NROUNDS;
    186  1.1  riastrad 
    187  1.1  riastrad 	aes_setdeckey(dec, key, nrounds);
    188  1.1  riastrad 	return nrounds;
    189  1.1  riastrad }
    190  1.1  riastrad 
    191  1.1  riastrad uint32_t
    192  1.1  riastrad aes_setdeckey256(struct aesdec *dec, const uint8_t key[static 32])
    193  1.1  riastrad {
    194  1.1  riastrad 	uint32_t nrounds = AES_256_NROUNDS;
    195  1.1  riastrad 
    196  1.1  riastrad 	aes_setdeckey(dec, key, nrounds);
    197  1.1  riastrad 	return nrounds;
    198  1.1  riastrad }
    199  1.1  riastrad 
    200  1.1  riastrad void
    201  1.1  riastrad aes_enc(const struct aesenc *enc, const uint8_t in[static 16],
    202  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
    203  1.1  riastrad {
    204  1.1  riastrad 
    205  1.1  riastrad 	aes_guarantee_selected();
    206  1.1  riastrad 	aes_impl->ai_enc(enc, in, out, nrounds);
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.1  riastrad void
    210  1.1  riastrad aes_dec(const struct aesdec *dec, const uint8_t in[static 16],
    211  1.1  riastrad     uint8_t out[static 16], uint32_t nrounds)
    212  1.1  riastrad {
    213  1.1  riastrad 
    214  1.1  riastrad 	aes_guarantee_selected();
    215  1.1  riastrad 	aes_impl->ai_dec(dec, in, out, nrounds);
    216  1.1  riastrad }
    217  1.1  riastrad 
    218  1.1  riastrad void
    219  1.1  riastrad aes_cbc_enc(struct aesenc *enc, const uint8_t in[static 16],
    220  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    221  1.1  riastrad     uint32_t nrounds)
    222  1.1  riastrad {
    223  1.1  riastrad 
    224  1.1  riastrad 	aes_guarantee_selected();
    225  1.1  riastrad 	aes_impl->ai_cbc_enc(enc, in, out, nbytes, iv, nrounds);
    226  1.1  riastrad }
    227  1.1  riastrad 
    228  1.1  riastrad void
    229  1.1  riastrad aes_cbc_dec(struct aesdec *dec, const uint8_t in[static 16],
    230  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t iv[static 16],
    231  1.1  riastrad     uint32_t nrounds)
    232  1.1  riastrad {
    233  1.1  riastrad 
    234  1.1  riastrad 	aes_guarantee_selected();
    235  1.1  riastrad 	aes_impl->ai_cbc_dec(dec, in, out, nbytes, iv, nrounds);
    236  1.1  riastrad }
    237  1.1  riastrad 
    238  1.1  riastrad void
    239  1.1  riastrad aes_xts_enc(struct aesenc *enc, const uint8_t in[static 16],
    240  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    241  1.1  riastrad     uint32_t nrounds)
    242  1.1  riastrad {
    243  1.1  riastrad 
    244  1.1  riastrad 	aes_guarantee_selected();
    245  1.1  riastrad 	aes_impl->ai_xts_enc(enc, in, out, nbytes, tweak, nrounds);
    246  1.1  riastrad }
    247  1.1  riastrad 
    248  1.1  riastrad void
    249  1.1  riastrad aes_xts_dec(struct aesdec *dec, const uint8_t in[static 16],
    250  1.1  riastrad     uint8_t out[static 16], size_t nbytes, uint8_t tweak[static 16],
    251  1.1  riastrad     uint32_t nrounds)
    252  1.1  riastrad {
    253  1.1  riastrad 
    254  1.1  riastrad 	aes_guarantee_selected();
    255  1.1  riastrad 	aes_impl->ai_xts_dec(dec, in, out, nbytes, tweak, nrounds);
    256  1.1  riastrad }
    257