1 /* $NetBSD: ah_aesxcbcmac.c,v 1.4 2007/05/23 17:14:59 christos Exp $ */
2 /* $KAME: ah_aesxcbcmac.c,v 1.7 2004/06/02 05:53:14 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: ah_aesxcbcmac.c,v 1.4 2007/05/23 17:14:59 christos Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/socket.h>
39 #include <sys/queue.h>
40 #include <sys/syslog.h>
41 #include <sys/mbuf.h>
42
43 #include <net/if.h>
44 #include <net/route.h>
45
46 #include <netinet/in.h>
47
48 #include <netinet6/ipsec.h>
49 #include <netinet6/ah.h>
50 #include <netinet6/ah_aesxcbcmac.h>
51
52 #include <netkey/key.h>
53
54 #include <crypto/rijndael/rijndael.h>
55
56 #include <net/net_osdep.h>
57
58 #define AES_BLOCKSIZE 16
59
60 typedef struct {
61 u_int8_t e[AES_BLOCKSIZE];
62 u_int8_t buf[AES_BLOCKSIZE];
63 size_t buflen;
64 u_int32_t r_k1s[(RIJNDAEL_MAXNR+1)*4];
65 u_int32_t r_k2s[(RIJNDAEL_MAXNR+1)*4];
66 u_int32_t r_k3s[(RIJNDAEL_MAXNR+1)*4];
67 int r_nr; /* key-length-dependent number of rounds */
68 u_int8_t k2[AES_BLOCKSIZE];
69 u_int8_t k3[AES_BLOCKSIZE];
70 } aesxcbc_ctx;
71
72 int
73 ah_aes_xcbc_mac_init(struct ah_algorithm_state *state, struct secasvar * sav)
74 {
75 u_int8_t k1seed[AES_BLOCKSIZE] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
76 u_int8_t k2seed[AES_BLOCKSIZE] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
77 u_int8_t k3seed[AES_BLOCKSIZE] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
78 u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
79 aesxcbc_ctx *ctx;
80 u_int8_t k1[AES_BLOCKSIZE];
81
82 if (!state)
83 panic("ah_aes_xcbc_mac_init: what?");
84
85 state->sav = sav;
86 state->foo = (void *)malloc(sizeof(aesxcbc_ctx), M_TEMP, M_NOWAIT);
87 if (!state->foo)
88 return ENOBUFS;
89 bzero(state->foo, sizeof(aesxcbc_ctx));
90
91 ctx = (aesxcbc_ctx *)state->foo;
92
93 if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks,
94 (char *)_KEYBUF(sav->key_auth), AES_BLOCKSIZE * 8)) == 0)
95 return -1;
96 rijndaelEncrypt(r_ks, ctx->r_nr, k1seed, k1);
97 rijndaelEncrypt(r_ks, ctx->r_nr, k2seed, ctx->k2);
98 rijndaelEncrypt(r_ks, ctx->r_nr, k3seed, ctx->k3);
99 if (rijndaelKeySetupEnc(ctx->r_k1s, k1, AES_BLOCKSIZE * 8) == 0)
100 return -1;
101 if (rijndaelKeySetupEnc(ctx->r_k2s, ctx->k2, AES_BLOCKSIZE * 8) == 0)
102 return -1;
103 if (rijndaelKeySetupEnc(ctx->r_k3s, ctx->k3, AES_BLOCKSIZE * 8) == 0)
104 return -1;
105
106 return 0;
107 }
108
109 void
110 ah_aes_xcbc_mac_loop(struct ah_algorithm_state *state, u_int8_t *addr,
111 size_t len)
112 {
113 u_int8_t buf[AES_BLOCKSIZE];
114 aesxcbc_ctx *ctx;
115 u_int8_t *ep;
116 int i;
117
118 if (!state || !state->foo)
119 panic("ah_aes_xcbc_mac_loop: what?");
120
121 ctx = (aesxcbc_ctx *)state->foo;
122 ep = addr + len;
123
124 if (ctx->buflen == sizeof(ctx->buf)) {
125 for (i = 0; i < sizeof(ctx->e); i++)
126 ctx->buf[i] ^= ctx->e[i];
127 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
128 ctx->buflen = 0;
129 }
130 if (ctx->buflen + len < sizeof(ctx->buf)) {
131 bcopy(addr, ctx->buf + ctx->buflen, len);
132 ctx->buflen += len;
133 return;
134 }
135 if (ctx->buflen && ctx->buflen + len > sizeof(ctx->buf)) {
136 bcopy(addr, ctx->buf + ctx->buflen,
137 sizeof(ctx->buf) - ctx->buflen);
138 for (i = 0; i < sizeof(ctx->e); i++)
139 ctx->buf[i] ^= ctx->e[i];
140 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
141 addr += sizeof(ctx->buf) - ctx->buflen;
142 ctx->buflen = 0;
143 }
144 /* due to the special processing for M[n], "=" case is not included */
145 while (addr + AES_BLOCKSIZE < ep) {
146 bcopy(addr, buf, AES_BLOCKSIZE);
147 for (i = 0; i < sizeof(buf); i++)
148 buf[i] ^= ctx->e[i];
149 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, buf, ctx->e);
150 addr += AES_BLOCKSIZE;
151 }
152 if (addr < ep) {
153 bcopy(addr, ctx->buf + ctx->buflen, ep - addr);
154 ctx->buflen += ep - addr;
155 }
156 }
157
158 void
159 ah_aes_xcbc_mac_result(struct ah_algorithm_state *state, u_int8_t *addr,
160 size_t l)
161 {
162 u_char digest[AES_BLOCKSIZE];
163 aesxcbc_ctx *ctx;
164 int i;
165
166 ctx = (aesxcbc_ctx *)state->foo;
167
168 if (ctx->buflen == sizeof(ctx->buf)) {
169 for (i = 0; i < sizeof(ctx->buf); i++) {
170 ctx->buf[i] ^= ctx->e[i];
171 ctx->buf[i] ^= ctx->k2[i];
172 }
173 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
174 } else {
175 for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
176 ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
177 for (i = 0; i < sizeof(ctx->buf); i++) {
178 ctx->buf[i] ^= ctx->e[i];
179 ctx->buf[i] ^= ctx->k3[i];
180 }
181 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
182 }
183
184 bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
185
186 free(state->foo, M_TEMP);
187 }
Cache object: c719e561f2cf2f8ca1392d85bf62efa6
|