Home | History | Annotate | Line # | Download | only in opencrypto
aesxcbcmac.c revision 1.1
      1  1.1  drochner /* $NetBSD: aesxcbcmac.c,v 1.1 2011/05/24 19:10:08 drochner 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.1  drochner __KERNEL_RCSID(0, "$NetBSD: aesxcbcmac.c,v 1.1 2011/05/24 19:10:08 drochner Exp $");
     34  1.1  drochner 
     35  1.1  drochner #include <sys/param.h>
     36  1.1  drochner #include <sys/systm.h>
     37  1.1  drochner #include <crypto/rijndael/rijndael.h>
     38  1.1  drochner 
     39  1.1  drochner #include <opencrypto/aesxcbcmac.h>
     40  1.1  drochner 
     41  1.1  drochner int
     42  1.1  drochner aes_xcbc_mac_init(void *vctx, const u_int8_t *key, u_int16_t keylen)
     43  1.1  drochner {
     44  1.1  drochner 	u_int8_t k1seed[AES_BLOCKSIZE] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
     45  1.1  drochner 	u_int8_t k2seed[AES_BLOCKSIZE] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
     46  1.1  drochner 	u_int8_t k3seed[AES_BLOCKSIZE] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
     47  1.1  drochner 	u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
     48  1.1  drochner 	aesxcbc_ctx *ctx;
     49  1.1  drochner 	u_int8_t k1[AES_BLOCKSIZE];
     50  1.1  drochner 
     51  1.1  drochner 	ctx = (aesxcbc_ctx *)vctx;
     52  1.1  drochner 	memset(ctx, 0, sizeof(aesxcbc_ctx));
     53  1.1  drochner 
     54  1.1  drochner 	if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks, key, keylen * 8)) == 0)
     55  1.1  drochner 		return -1;
     56  1.1  drochner 	rijndaelEncrypt(r_ks, ctx->r_nr, k1seed, k1);
     57  1.1  drochner 	rijndaelEncrypt(r_ks, ctx->r_nr, k2seed, ctx->k2);
     58  1.1  drochner 	rijndaelEncrypt(r_ks, ctx->r_nr, k3seed, ctx->k3);
     59  1.1  drochner 	if (rijndaelKeySetupEnc(ctx->r_k1s, k1, AES_BLOCKSIZE * 8) == 0)
     60  1.1  drochner 		return -1;
     61  1.1  drochner 	if (rijndaelKeySetupEnc(ctx->r_k2s, ctx->k2, AES_BLOCKSIZE * 8) == 0)
     62  1.1  drochner 		return -1;
     63  1.1  drochner 	if (rijndaelKeySetupEnc(ctx->r_k3s, ctx->k3, AES_BLOCKSIZE * 8) == 0)
     64  1.1  drochner 		return -1;
     65  1.1  drochner 
     66  1.1  drochner 	return 0;
     67  1.1  drochner }
     68  1.1  drochner 
     69  1.1  drochner int
     70  1.1  drochner aes_xcbc_mac_loop(void *vctx, const u_int8_t *addr, u_int16_t len)
     71  1.1  drochner {
     72  1.1  drochner 	u_int8_t buf[AES_BLOCKSIZE];
     73  1.1  drochner 	aesxcbc_ctx *ctx;
     74  1.1  drochner 	const u_int8_t *ep;
     75  1.1  drochner 	int i;
     76  1.1  drochner 
     77  1.1  drochner 	ctx = (aesxcbc_ctx *)vctx;
     78  1.1  drochner 	ep = addr + len;
     79  1.1  drochner 
     80  1.1  drochner 	if (ctx->buflen == sizeof(ctx->buf)) {
     81  1.1  drochner 		for (i = 0; i < sizeof(ctx->e); i++)
     82  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
     83  1.1  drochner 		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
     84  1.1  drochner 		ctx->buflen = 0;
     85  1.1  drochner 	}
     86  1.1  drochner 	if (ctx->buflen + len < sizeof(ctx->buf)) {
     87  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr, len);
     88  1.1  drochner 		ctx->buflen += len;
     89  1.1  drochner 		return 0;
     90  1.1  drochner 	}
     91  1.1  drochner 	if (ctx->buflen && ctx->buflen + len > sizeof(ctx->buf)) {
     92  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr,
     93  1.1  drochner 		    sizeof(ctx->buf) - ctx->buflen);
     94  1.1  drochner 		for (i = 0; i < sizeof(ctx->e); i++)
     95  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
     96  1.1  drochner 		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
     97  1.1  drochner 		addr += sizeof(ctx->buf) - ctx->buflen;
     98  1.1  drochner 		ctx->buflen = 0;
     99  1.1  drochner 	}
    100  1.1  drochner 	/* due to the special processing for M[n], "=" case is not included */
    101  1.1  drochner 	while (addr + AES_BLOCKSIZE < ep) {
    102  1.1  drochner 		memcpy(buf, addr, AES_BLOCKSIZE);
    103  1.1  drochner 		for (i = 0; i < sizeof(buf); i++)
    104  1.1  drochner 			buf[i] ^= ctx->e[i];
    105  1.1  drochner 		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, buf, ctx->e);
    106  1.1  drochner 		addr += AES_BLOCKSIZE;
    107  1.1  drochner 	}
    108  1.1  drochner 	if (addr < ep) {
    109  1.1  drochner 		memcpy(ctx->buf + ctx->buflen, addr, ep - addr);
    110  1.1  drochner 		ctx->buflen += ep - addr;
    111  1.1  drochner 	}
    112  1.1  drochner 	return 0;
    113  1.1  drochner }
    114  1.1  drochner 
    115  1.1  drochner void
    116  1.1  drochner aes_xcbc_mac_result(u_int8_t *addr, void *vctx)
    117  1.1  drochner {
    118  1.1  drochner 	u_char digest[AES_BLOCKSIZE];
    119  1.1  drochner 	aesxcbc_ctx *ctx;
    120  1.1  drochner 	int i;
    121  1.1  drochner 
    122  1.1  drochner 	ctx = (aesxcbc_ctx *)vctx;
    123  1.1  drochner 
    124  1.1  drochner 	if (ctx->buflen == sizeof(ctx->buf)) {
    125  1.1  drochner 		for (i = 0; i < sizeof(ctx->buf); i++) {
    126  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
    127  1.1  drochner 			ctx->buf[i] ^= ctx->k2[i];
    128  1.1  drochner 		}
    129  1.1  drochner 		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
    130  1.1  drochner 	} else {
    131  1.1  drochner 		for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
    132  1.1  drochner 			ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
    133  1.1  drochner 		for (i = 0; i < sizeof(ctx->buf); i++) {
    134  1.1  drochner 			ctx->buf[i] ^= ctx->e[i];
    135  1.1  drochner 			ctx->buf[i] ^= ctx->k3[i];
    136  1.1  drochner 		}
    137  1.1  drochner 		rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
    138  1.1  drochner 	}
    139  1.1  drochner 
    140  1.1  drochner 	memcpy(addr, digest, sizeof(digest));
    141  1.1  drochner }
    142