]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/ah_aesxcbcmac.c
This commit was generated by cvs2svn to compensate for changes in r169185,
[FreeBSD/FreeBSD.git] / sys / netinet6 / ah_aesxcbcmac.c
1 /*      $KAME: ah_aesxcbcmac.c,v 1.6 2003/07/22 02:30:54 itojun Exp $   */
2
3 /*-
4  * Copyright (C) 1995, 1996, 1997, 1998 and 2003 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33
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>
40 #include <sys/mbuf.h>
41
42 #include <net/if.h>
43 #include <net/route.h>
44
45 #include <netinet/in.h>
46
47 #include <netinet6/ipsec.h>
48 #include <netinet6/ah.h>
49 #include <netinet6/ah_aesxcbcmac.h>
50
51 #include <netkey/key.h>
52
53 #include <crypto/rijndael/rijndael.h>
54
55 #define AES_BLOCKSIZE   16
56
57 typedef struct {
58         u_int8_t        e[AES_BLOCKSIZE];
59         u_int8_t        buf[AES_BLOCKSIZE];
60         size_t          buflen;
61         u_int32_t       r_k1s[(RIJNDAEL_MAXNR+1)*4];
62         u_int32_t       r_k2s[(RIJNDAEL_MAXNR+1)*4];
63         u_int32_t       r_k3s[(RIJNDAEL_MAXNR+1)*4];
64         int             r_nr; /* key-length-dependent number of rounds */
65         u_int8_t        k2[AES_BLOCKSIZE];
66         u_int8_t        k3[AES_BLOCKSIZE];
67 } aesxcbc_ctx;
68
69 int
70 ah_aes_xcbc_mac_init(state, sav)
71         struct ah_algorithm_state *state;
72         struct secasvar *sav;
73 {
74         u_int8_t k1seed[AES_BLOCKSIZE] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 };
75         u_int8_t k2seed[AES_BLOCKSIZE] = { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 };
76         u_int8_t k3seed[AES_BLOCKSIZE] = { 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3 };
77         u_int32_t r_ks[(RIJNDAEL_MAXNR+1)*4];
78         aesxcbc_ctx *ctx;
79         u_int8_t k1[AES_BLOCKSIZE];
80
81         if (!state)
82                 panic("ah_aes_xcbc_mac_init: what?");
83
84         state->sav = sav;
85         state->foo = (void *)malloc(sizeof(aesxcbc_ctx), M_TEMP, M_NOWAIT);
86         if (!state->foo)
87                 return ENOBUFS;
88         bzero(state->foo, sizeof(aesxcbc_ctx));
89
90         ctx = (aesxcbc_ctx *)state->foo;
91
92         if ((ctx->r_nr = rijndaelKeySetupEnc(r_ks,
93             (char *)_KEYBUF(sav->key_auth), AES_BLOCKSIZE * 8)) == 0)
94                 return -1;
95         rijndaelEncrypt(r_ks, ctx->r_nr, k1seed, k1);
96         rijndaelEncrypt(r_ks, ctx->r_nr, k2seed, ctx->k2);
97         rijndaelEncrypt(r_ks, ctx->r_nr, k3seed, ctx->k3);
98         if (rijndaelKeySetupEnc(ctx->r_k1s, k1, AES_BLOCKSIZE * 8) == 0)
99                 return -1;
100         if (rijndaelKeySetupEnc(ctx->r_k2s, ctx->k2, AES_BLOCKSIZE * 8) == 0)
101                 return -1;
102         if (rijndaelKeySetupEnc(ctx->r_k3s, ctx->k3, AES_BLOCKSIZE * 8) == 0)
103                 return -1;
104
105         return 0;
106 }
107
108 void
109 ah_aes_xcbc_mac_loop(state, addr, len)
110         struct ah_algorithm_state *state;
111         u_int8_t *addr;
112         size_t len;
113 {
114         u_int8_t buf[AES_BLOCKSIZE];
115         aesxcbc_ctx *ctx;
116         u_int8_t *ep;
117         int i;
118
119         if (!state || !state->foo)
120                 panic("ah_aes_xcbc_mac_loop: what?");
121
122         ctx = (aesxcbc_ctx *)state->foo;
123         ep = addr + len;
124
125         if (ctx->buflen == sizeof(ctx->buf)) {
126                 for (i = 0; i < sizeof(ctx->e); i++)
127                         ctx->buf[i] ^= ctx->e[i];
128                 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
129                 ctx->buflen = 0;
130         }
131         if (ctx->buflen + len < sizeof(ctx->buf)) {
132                 bcopy(addr, ctx->buf + ctx->buflen, len);
133                 ctx->buflen += len;
134                 return;
135         }
136         if (ctx->buflen && ctx->buflen + len > sizeof(ctx->buf)) {
137                 bcopy(addr, ctx->buf + ctx->buflen,
138                     sizeof(ctx->buf) - ctx->buflen);
139                 for (i = 0; i < sizeof(ctx->e); i++)
140                         ctx->buf[i] ^= ctx->e[i];
141                 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, ctx->e);
142                 addr += sizeof(ctx->buf) - ctx->buflen;
143                 ctx->buflen = 0;
144         }
145         /* due to the special processing for M[n], "=" case is not included */
146         while (addr + AES_BLOCKSIZE < ep) {
147                 bcopy(addr, buf, AES_BLOCKSIZE);
148                 for (i = 0; i < sizeof(buf); i++)
149                         buf[i] ^= ctx->e[i];
150                 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, buf, ctx->e);
151                 addr += AES_BLOCKSIZE;
152         }
153         if (addr < ep) {
154                 bcopy(addr, ctx->buf + ctx->buflen, ep - addr);
155                 ctx->buflen += ep - addr;
156         }
157 }
158
159 void
160 ah_aes_xcbc_mac_result(state, addr, l)
161         struct ah_algorithm_state *state;
162         u_int8_t *addr;
163         size_t l;
164 {
165         u_char digest[AES_BLOCKSIZE];
166         aesxcbc_ctx *ctx;
167         int i;
168
169         ctx = (aesxcbc_ctx *)state->foo;
170
171         if (ctx->buflen == sizeof(ctx->buf)) {
172                 for (i = 0; i < sizeof(ctx->buf); i++) {
173                         ctx->buf[i] ^= ctx->e[i];
174                         ctx->buf[i] ^= ctx->k2[i];
175                 }
176                 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
177         } else {
178                 for (i = ctx->buflen; i < sizeof(ctx->buf); i++)
179                         ctx->buf[i] = (i == ctx->buflen) ? 0x80 : 0x00;
180                 for (i = 0; i < sizeof(ctx->buf); i++) {
181                         ctx->buf[i] ^= ctx->e[i];
182                         ctx->buf[i] ^= ctx->k3[i];
183                 }
184                 rijndaelEncrypt(ctx->r_k1s, ctx->r_nr, ctx->buf, digest);
185         }
186
187         bcopy(digest, addr, sizeof(digest) > l ? l : sizeof(digest));
188
189         free(state->foo, M_TEMP);
190 }