Home | History | Annotate | Line # | Download | only in opencrypto
      1  1.3  riastrad /* $NetBSD: aesxcbcmac.c,v 1.3 2020/06/29 23:34:48 riastradh Exp $ */
      2  1.1  drochner 
      3  1.1  drochner /*
      4  1.1  drochner  * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
      5  1.1  drochner  * All rights reserved.
      6  1.1  drochner  *
      7  1.1  drochner  * Redistribution and use in source and binary forms, with or without
      8  1.1  drochner  * modification, are permitted provided that the following conditions
      9  1.1  drochner  * are met:
     10  1.1  drochner  * 1. Redistributions of source code must retain the above copyright
     11  1.1  drochner  *    notice, this list of conditions and the following disclaimer.
     12  1.1  drochner  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  drochner  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  drochner  *    documentation and/or other materials provided with the distribution.
     15  1.1  drochner  * 3. Neither the name of the project nor the names of its contributors
     16  1.1  drochner  *    may be used to endorse or promote products derived from this software
     17  1.1  drochner  *    without specific prior written permission.
     18  1.1  drochner  *
     19  1.1  drochner  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  drochner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  drochner  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  drochner  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  1.1  drochner  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  drochner  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  drochner  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  drochner  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  drochner  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  drochner  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  drochner  * SUCH DAMAGE.
     30  1.1  drochner  */
     31  1.1  drochner 
     32  1.1  drochner #include <sys/cdefs.h>
     33  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.3 2020/06/29 23:34:48 riastradh Exp $");
     34  1.1  drochner 
     35  1.1  drochner #include <sys/param.h>
     36  1.1  drochner #include <sys/systm.h>
     37  1.3  riastrad 
     38  1.3  riastrad #include <crypto/aes/aes.h>
     39  1.1  drochner 
     40  1.1  drochner #include <opencrypto/aesxcbcmac.h>
     41  1.1  drochner 
     42  1.1  drochner int
     43  1.2  christos aes_xcbc_mac_init(void *vctx, const uint8_t *key, u_int16_t keylen)
     44  1.1  drochner {
     45  1.2  christos 	static const uint8_t k1seed[AES_BLOCKSIZE] =
     46  1.2  christos 	    { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
     47  1.2  christos 	static const uint8_t k2seed[AES_BLOCKSIZE] =
     48  1.2  christos 	    { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
     49  1.2  christos 	static const uint8_t k3seed[AES_BLOCKSIZE] =
     50  1.2  christos 	    { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
     51  1.3  riastrad 	struct aesenc r_ks;
     52  1.1  drochner 	aesxcbc_ctx *ctx;
     53  1.2  christos 	uint8_t k1[AES_BLOCKSIZE];
     54  1.1  drochner 
     55  1.2  christos 	ctx = vctx;
     56  1.2  christos 	memset(ctx, 0, sizeof(*ctx));
     57  1.1  drochner 
     58  1.3  riastrad 	switch (keylen) {
     59  1.3  riastrad 	case 16:
     60  1.3  riastrad 		ctx->r_nr = aes_setenckey128(&r_ks, key);
     61  1.3  riastrad 		break;
     62  1.3  riastrad 	case 24:
     63  1.3  riastrad 		ctx->r_nr = aes_setenckey192(&r_ks, key);
     64  1.3  riastrad 		break;
     65  1.3  riastrad 	case 32:
     66  1.3  riastrad 		ctx->r_nr = aes_setenckey256(&r_ks, key);
     67  1.3  riastrad 		break;
     68  1.3  riastrad 	}
     69  1.3  riastrad 	aes_enc(&r_ks, k1seed, k1, ctx->r_nr);
     70  1.3  riastrad 	aes_enc(&r_ks, k2seed, ctx->k2, ctx->r_nr);
     71  1.3  riastrad 	aes_enc(&r_ks, k3seed, ctx->k3, ctx->r_nr);
     72  1.3  riastrad 	aes_setenckey128(&ctx->r_k1s, k1);
     73  1.3  riastrad 
     74  1.3  riastrad 	explicit_memset(&r_ks, 0, sizeof(r_ks));
     75  1.3  riastrad 	explicit_memset(k1, 0, sizeof(k1));
     76  1.1  drochner 
     77  1.1  drochner 	return 0;
     78  1.1  drochner }
     79  1.1  drochner 
     80  1.1  drochner int
     81  1.2  christos aes_xcbc_mac_loop(void *vctx, const uint8_t *addr, u_int16_t len)
     82  1.1  drochner {
     83  1.2  christos 	uint8_t buf[AES_BLOCKSIZE];
     84  1.1  drochner 	aesxcbc_ctx *ctx;
     85  1.2  christos 	const uint8_t *ep;
     86  1.1  drochner 	int i;
     87  1.1  drochner 
     88  1.2  christos 	ctx = vctx;
     89  1.1  drochner 	ep = addr + len;
     90  1.1  drochner 
     91  1.1  drochner 	if (ctx->buflen == sizeof(ctx->buf)) {
     92  1.1  drochner 		for (i = 0; i < sizeof(ctx->e); i++)
     93  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
     94  1.3  riastrad 		aes_enc(&ctx->r_k1s, ctx->buf, ctx->e, ctx->r_nr);
     95  1.1  drochner 		ctx->buflen = 0;
     96  1.1  drochner 	}
     97  1.1  drochner 	if (ctx->buflen + len < sizeof(ctx->buf)) {
     98  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr, len);
     99  1.1  drochner 		ctx->buflen += len;
    100  1.1  drochner 		return 0;
    101  1.1  drochner 	}
    102  1.1  drochner 	if (ctx->buflen && ctx->buflen + len > sizeof(ctx->buf)) {
    103  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr,
    104  1.1  drochner 		    sizeof(ctx->buf) - ctx->buflen);
    105  1.1  drochner 		for (i = 0; i < sizeof(ctx->e); i++)
    106  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
    107  1.3  riastrad 		aes_enc(&ctx->r_k1s, ctx->buf, ctx->e, ctx->r_nr);
    108  1.1  drochner 		addr += sizeof(ctx->buf) - ctx->buflen;
    109  1.1  drochner 		ctx->buflen = 0;
    110  1.1  drochner 	}
    111  1.1  drochner 	/* due to the special processing for M[n], "=" case is not included */
    112  1.2  christos 	while (ep - addr > AES_BLOCKSIZE) {
    113  1.1  drochner 		memcpy(buf, addr, AES_BLOCKSIZE);
    114  1.1  drochner 		for (i = 0; i < sizeof(buf); i++)
    115  1.1  drochner 			buf[i] ^= ctx->e[i];
    116  1.3  riastrad 		aes_enc(&ctx->r_k1s, buf, ctx->e, ctx->r_nr);
    117  1.1  drochner 		addr += AES_BLOCKSIZE;
    118  1.1  drochner 	}
    119  1.1  drochner 	if (addr < ep) {
    120  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr, ep - addr);
    121  1.1  drochner 		ctx->buflen += ep - addr;
    122  1.1  drochner 	}
    123  1.1  drochner 	return 0;
    124  1.1  drochner }
    125  1.1  drochner 
    126  1.1  drochner void
    127  1.2  christos aes_xcbc_mac_result(uint8_t *addr, void *vctx)
    128  1.1  drochner {
    129  1.2  christos 	uint8_t digest[AES_BLOCKSIZE];
    130  1.1  drochner 	aesxcbc_ctx *ctx;
    131  1.1  drochner 	int i;
    132  1.1  drochner 
    133  1.2  christos 	ctx = vctx;
    134  1.1  drochner 
    135  1.1  drochner 	if (ctx->buflen == sizeof(ctx->buf)) {
    136  1.1  drochner 		for (i = 0; i < sizeof(ctx->buf); i++) {
    137  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
    138  1.1  drochner 			ctx->buf[i] ^= ctx->k2[i];
    139  1.1  drochner 		}
    140  1.3  riastrad 		aes_enc(&ctx->r_k1s, ctx->buf, digest, ctx->r_nr);
    141  1.1  drochner 	} else {
    142  1.1  drochner 		for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
    143  1.1  drochner 			ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
    144  1.1  drochner 		for (i = 0; i < sizeof(ctx->buf); i++) {
    145  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
    146  1.1  drochner 			ctx->buf[i] ^= ctx->k3[i];
    147  1.1  drochner 		}
    148  1.3  riastrad 		aes_enc(&ctx->r_k1s, ctx->buf, digest, ctx->r_nr);
    149  1.1  drochner 	}
    150  1.1  drochner 
    151  1.1  drochner 	memcpy(addr, digest, sizeof(digest));
    152  1.1  drochner }
    153