1 /* $KAME: ah_aesxcbcmac.c,v 1.6 2003/07/22 02:30:54 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/socket.h>
37 #include <sys/queue.h>
38 #include <sys/malloc.h>
39 #include <sys/syslog.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
47 #include <netinet6/ipsec.h>
48 #include <netinet6/ah.h>
49 #include <netinet6/ah_aesxcbcmac.h>
51 #include <netkey/key.h>
53 #include <crypto/rijndael/rijndael.h>
55 #include <net/net_osdep.h>
57 #define AES_BLOCKSIZE 16
60 u_int8_t e[AES_BLOCKSIZE];
61 u_int8_t buf[AES_BLOCKSIZE];
63 u_int32_t r_k1s[(RIJNDAEL_MAXNR+1)*4];
64 u_int32_t r_k2s[(RIJNDAEL_MAXNR+1)*4];
65 u_int32_t r_k3s[(RIJNDAEL_MAXNR+1)*4];
66 int r_nr; /* key-length-dependent number of rounds */
67 u_int8_t k2[AES_BLOCKSIZE];
68 u_int8_t k3[AES_BLOCKSIZE];
72 ah_aes_xcbc_mac_init(state, sav)
73 struct ah_algorithm_state *state;
76 u_int8_t k1seed[AES_BLOCKSIZE] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
77 u_int8_t k2seed[AES_BLOCKSIZE] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
78 u_int8_t k3seed[AES_BLOCKSIZE] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
79 u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
81 u_int8_t k1[AES_BLOCKSIZE];
84 panic("ah_aes_xcbc_mac_init: what?");
87 state->foo = (void *)malloc(sizeof(aesxcbc_ctx), M_TEMP, M_NOWAIT);
90 bzero(state->foo, sizeof(aesxcbc_ctx));
92 ctx = (aesxcbc_ctx *)state->foo;
94 if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks,
95 (char *)_KEYBUF(sav->key_auth), AES_BLOCKSIZE * 8)) == 0)
97 rijndaelEncrypt(r_ks, ctx->r_nr, k1seed, k1);
98 rijndaelEncrypt(r_ks, ctx->r_nr, k2seed, ctx->k2);
99 rijndaelEncrypt(r_ks, ctx->r_nr, k3seed, ctx->k3);
100 if (rijndaelKeySetupEnc(ctx->r_k1s, k1, AES_BLOCKSIZE * 8) == 0)
102 if (rijndaelKeySetupEnc(ctx->r_k2s, ctx->k2, AES_BLOCKSIZE * 8) == 0)
104 if (rijndaelKeySetupEnc(ctx->r_k3s, ctx->k3, AES_BLOCKSIZE * 8) == 0)
111 ah_aes_xcbc_mac_loop(state, addr, len)
112 struct ah_algorithm_state *state;
116 u_int8_t buf[AES_BLOCKSIZE];
121 if (!state || !state->foo)
122 panic("ah_aes_xcbc_mac_loop: what?");
124 ctx = (aesxcbc_ctx *)state->foo;
127 if (ctx->buflen == sizeof(ctx->buf)) {
128 for (i = 0; i < sizeof(ctx->e); i++)
129 ctx->buf[i] ^= ctx->e[i];
130 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
133 if (ctx->buflen + len < sizeof(ctx->buf)) {
134 bcopy(addr, ctx->buf + ctx->buflen, len);
138 if (ctx->buflen && ctx->buflen + len > sizeof(ctx->buf)) {
139 bcopy(addr, ctx->buf + ctx->buflen,
140 sizeof(ctx->buf) - ctx->buflen);
141 for (i = 0; i < sizeof(ctx->e); i++)
142 ctx->buf[i] ^= ctx->e[i];
143 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
144 addr += sizeof(ctx->buf) - ctx->buflen;
147 /* due to the special processing for M[n], "=" case is not included */
148 while (addr + AES_BLOCKSIZE < ep) {
149 bcopy(addr, buf, AES_BLOCKSIZE);
150 for (i = 0; i < sizeof(buf); i++)
152 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, buf, ctx->e);
153 addr += AES_BLOCKSIZE;
156 bcopy(addr, ctx->buf + ctx->buflen, ep - addr);
157 ctx->buflen += ep - addr;
162 ah_aes_xcbc_mac_result(state, addr, l)
163 struct ah_algorithm_state *state;
167 u_char digest[AES_BLOCKSIZE];
171 ctx = (aesxcbc_ctx *)state->foo;
173 if (ctx->buflen == sizeof(ctx->buf)) {
174 for (i = 0; i < sizeof(ctx->buf); i++) {
175 ctx->buf[i] ^= ctx->e[i];
176 ctx->buf[i] ^= ctx->k2[i];
178 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
180 for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
181 ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
182 for (i = 0; i < sizeof(ctx->buf); i++) {
183 ctx->buf[i] ^= ctx->e[i];
184 ctx->buf[i] ^= ctx->k3[i];
186 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
189 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
191 free(state->foo, M_TEMP);