1 /* $NetBSD: ah_core.c,v 1.47 2009/03/18 17:06:52 cegger Exp $ */
2 /* $KAME: ah_core.c,v 1.57 2003/07/25 09:33:36 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * RFC1826/2402 authentication header.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ah_core.c,v 1.47 2009/03/18 17:06:52 cegger Exp $");
41 #include "opt_ipsec.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/domain.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/errno.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
57 #include <net/route.h>
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #include <netinet/in_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
67 #include <netinet/icmp6.h>
68 #include <netinet6/scope6_var.h>
71 #include <netinet6/ipsec.h>
72 #include <netinet6/ah.h>
73 #include <netinet6/ah_aesxcbcmac.h>
75 #include <netinet6/esp.h>
77 #include <net/pfkeyv2.h>
78 #include <netkey/keydb.h>
80 #define MD5_RESULTLEN 16
82 #define SHA1_RESULTLEN 20
84 #include <sys/rmd160.h>
85 #define RIPEMD160_RESULTLEN 20
87 #include <net/net_osdep.h>
89 static int ah_sumsiz_1216(struct secasvar
*);
90 static int ah_sumsiz_zero(struct secasvar
*);
91 static int ah_common_mature(struct secasvar
*);
92 static int ah_none_mature(struct secasvar
*);
93 static int ah_none_init(struct ah_algorithm_state
*, struct secasvar
*);
94 static void ah_none_loop(struct ah_algorithm_state
*, u_int8_t
*, size_t);
95 static void ah_none_result(struct ah_algorithm_state
*,
97 static int ah_keyed_md5_mature(struct secasvar
*);
98 static int ah_keyed_md5_init(struct ah_algorithm_state
*,
100 static void ah_keyed_md5_loop(struct ah_algorithm_state
*, u_int8_t
*,
102 static void ah_keyed_md5_result(struct ah_algorithm_state
*,
104 static int ah_keyed_sha1_init(struct ah_algorithm_state
*,
106 static void ah_keyed_sha1_loop(struct ah_algorithm_state
*, u_int8_t
*,
108 static void ah_keyed_sha1_result(struct ah_algorithm_state
*, u_int8_t
*,
110 static int ah_hmac_md5_init(struct ah_algorithm_state
*,
112 static void ah_hmac_md5_loop(struct ah_algorithm_state
*, u_int8_t
*,
114 static void ah_hmac_md5_result(struct ah_algorithm_state
*,
116 static int ah_hmac_sha1_init(struct ah_algorithm_state
*,
118 static void ah_hmac_sha1_loop(struct ah_algorithm_state
*, u_int8_t
*,
120 static void ah_hmac_sha1_result(struct ah_algorithm_state
*,
122 static int ah_hmac_sha2_256_init(struct ah_algorithm_state
*,
124 static void ah_hmac_sha2_256_loop(struct ah_algorithm_state
*, u_int8_t
*,
126 static void ah_hmac_sha2_256_result(struct ah_algorithm_state
*,
128 static int ah_hmac_sha2_384_init(struct ah_algorithm_state
*,
130 static void ah_hmac_sha2_384_loop(struct ah_algorithm_state
*, u_int8_t
*,
132 static void ah_hmac_sha2_384_result(struct ah_algorithm_state
*,
134 static int ah_hmac_sha2_512_init(struct ah_algorithm_state
*,
136 static void ah_hmac_sha2_512_loop(struct ah_algorithm_state
*, u_int8_t
*,
138 static void ah_hmac_sha2_512_result(struct ah_algorithm_state
*,
140 static int ah_hmac_ripemd160_init(struct ah_algorithm_state
*,
142 static void ah_hmac_ripemd160_loop(struct ah_algorithm_state
*, u_int8_t
*,
144 static void ah_hmac_ripemd160_result(struct ah_algorithm_state
*,
147 static void ah_update_mbuf(struct mbuf
*, int, int,
148 const struct ah_algorithm
*, struct ah_algorithm_state
*);
150 /* checksum algorithms */
151 static const struct ah_algorithm ah_algorithms
[] = {
152 { ah_sumsiz_1216
, ah_common_mature
, 128, 128, "hmac-md5",
153 ah_hmac_md5_init
, ah_hmac_md5_loop
,
154 ah_hmac_md5_result
, },
155 { ah_sumsiz_1216
, ah_common_mature
, 160, 160, "hmac-sha1",
156 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
157 ah_hmac_sha1_result
, },
158 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
159 ah_keyed_md5_init
, ah_keyed_md5_loop
,
160 ah_keyed_md5_result
, },
161 { ah_sumsiz_1216
, ah_common_mature
, 160, 160, "keyed-sha1",
162 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
163 ah_keyed_sha1_result
, },
164 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
165 ah_none_init
, ah_none_loop
, ah_none_result
, },
166 { ah_sumsiz_1216
, ah_common_mature
, 256, 256,
168 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
169 ah_hmac_sha2_256_result
, },
170 { ah_sumsiz_1216
, ah_common_mature
, 384, 384,
172 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
173 ah_hmac_sha2_384_result
, },
174 { ah_sumsiz_1216
, ah_common_mature
, 512, 512,
176 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
177 ah_hmac_sha2_512_result
, },
178 { ah_sumsiz_1216
, ah_common_mature
, 160, 160,
180 ah_hmac_ripemd160_init
, ah_hmac_ripemd160_loop
,
181 ah_hmac_ripemd160_result
, },
182 { ah_sumsiz_1216
, ah_common_mature
, 128, 128,
184 ah_aes_xcbc_mac_init
, ah_aes_xcbc_mac_loop
,
185 ah_aes_xcbc_mac_result
, },
188 const struct ah_algorithm
*
189 ah_algorithm_lookup(int idx
)
193 case SADB_AALG_MD5HMAC
:
194 return &ah_algorithms
[0];
195 case SADB_AALG_SHA1HMAC
:
196 return &ah_algorithms
[1];
197 case SADB_X_AALG_MD5
:
198 return &ah_algorithms
[2];
199 case SADB_X_AALG_SHA
:
200 return &ah_algorithms
[3];
201 case SADB_X_AALG_NULL
:
202 return &ah_algorithms
[4];
203 case SADB_X_AALG_SHA2_256
:
204 return &ah_algorithms
[5];
205 case SADB_X_AALG_SHA2_384
:
206 return &ah_algorithms
[6];
207 case SADB_X_AALG_SHA2_512
:
208 return &ah_algorithms
[7];
209 case SADB_X_AALG_RIPEMD160HMAC
:
210 return &ah_algorithms
[8];
211 case SADB_X_AALG_AES_XCBC_MAC
:
212 return &ah_algorithms
[9];
220 ah_sumsiz_1216(struct secasvar
*sav
)
223 panic("ah_sumsiz_1216: null pointer is passed");
224 if (sav
->flags
& SADB_X_EXT_OLD
)
231 ah_sumsiz_zero(struct secasvar
*sav
)
234 panic("ah_sumsiz_zero: null pointer is passed");
239 ah_common_mature(struct secasvar
*sav
)
241 const struct ah_algorithm
*algo
;
243 if (!sav
->key_auth
) {
244 ipseclog((LOG_ERR
, "ah_common_mature: no key is given.\n"));
248 algo
= ah_algorithm_lookup(sav
->alg_auth
);
250 ipseclog((LOG_ERR
, "ah_common_mature: unsupported algorithm.\n"));
254 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
255 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
257 "ah_common_mature: invalid key length %d for %s.\n",
258 sav
->key_auth
->sadb_key_bits
, algo
->name
));
266 ah_none_mature(struct secasvar
*sav
)
268 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
270 "ah_none_mature: protocol and algorithm mismatch.\n"));
277 ah_none_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
284 ah_none_loop(struct ah_algorithm_state
*state
,
285 u_int8_t
*addr
, size_t len
)
290 ah_none_result(struct ah_algorithm_state
*state
,
291 u_int8_t
*addr
, size_t l
)
296 ah_keyed_md5_mature(struct secasvar
*sav
)
298 /* anything is okay */
303 ah_keyed_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
310 panic("ah_keyed_md5_init: what?");
313 state
->foo
= (void *)malloc(sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
314 if (state
->foo
== NULL
)
317 MD5Init((MD5_CTX
*)state
->foo
);
319 MD5Update((MD5_CTX
*)state
->foo
,
320 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
321 (u_int
)_KEYLEN(state
->sav
->key_auth
));
325 * We cannot simply use md5_pad() since the function
326 * won't update the total length.
328 if (_KEYLEN(state
->sav
->key_auth
) < 56)
329 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
331 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
332 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
336 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
339 memset(buf
, 0, sizeof(buf
));
340 while (sizeof(buf
) < padlen
) {
341 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
342 padlen
-= sizeof(buf
);
345 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
348 buf
[0] = (keybitlen
>> 0) & 0xff;
349 buf
[1] = (keybitlen
>> 8) & 0xff;
350 buf
[2] = (keybitlen
>> 16) & 0xff;
351 buf
[3] = (keybitlen
>> 24) & 0xff;
352 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
359 ah_keyed_md5_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
363 panic("ah_keyed_md5_loop: what?");
365 MD5Update((MD5_CTX
*)state
->foo
, addr
, len
);
369 ah_keyed_md5_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
372 u_char digest
[MD5_RESULTLEN
];
375 panic("ah_keyed_md5_result: what?");
378 MD5Update((MD5_CTX
*)state
->foo
,
379 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
380 (u_int
)_KEYLEN(state
->sav
->key_auth
));
382 MD5Final(digest
, (MD5_CTX
*)state
->foo
);
383 free(state
->foo
, M_TEMP
);
384 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
388 ah_keyed_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
396 panic("ah_keyed_sha1_init: what?");
399 state
->foo
= (void *)malloc(sizeof(SHA1_CTX
), M_TEMP
, M_NOWAIT
);
403 ctxt
= (SHA1_CTX
*)state
->foo
;
407 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
408 (u_int
)_KEYLEN(state
->sav
->key_auth
));
413 if (_KEYLEN(state
->sav
->key_auth
) < 56)
414 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
416 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
417 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
421 SHA1Update(ctxt
, &buf
[0], 1);
424 memset(buf
, 0, sizeof(buf
));
425 while (sizeof(buf
) < padlen
) {
426 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
427 padlen
-= sizeof(buf
);
430 SHA1Update(ctxt
, &buf
[0], padlen
);
433 buf
[0] = (keybitlen
>> 0) & 0xff;
434 buf
[1] = (keybitlen
>> 8) & 0xff;
435 buf
[2] = (keybitlen
>> 16) & 0xff;
436 buf
[3] = (keybitlen
>> 24) & 0xff;
437 SHA1Update(ctxt
, buf
, 8);
444 ah_keyed_sha1_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
449 if (!state
|| !state
->foo
)
450 panic("ah_keyed_sha1_loop: what?");
451 ctxt
= (SHA1_CTX
*)state
->foo
;
453 SHA1Update(ctxt
, (u_int8_t
*)addr
, (size_t)len
);
457 ah_keyed_sha1_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
460 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
463 if (!state
|| !state
->foo
)
464 panic("ah_keyed_sha1_result: what?");
465 ctxt
= (SHA1_CTX
*)state
->foo
;
468 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
469 (u_int
)_KEYLEN(state
->sav
->key_auth
));
471 SHA1Final((u_int8_t
*)digest
, ctxt
);
472 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
474 free(state
->foo
, M_TEMP
);
478 ah_hmac_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
482 u_char tk
[MD5_RESULTLEN
];
489 panic("ah_hmac_md5_init: what?");
492 state
->foo
= (void *)malloc(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
496 ipad
= (u_char
*)state
->foo
;
497 opad
= (u_char
*)(ipad
+ 64);
498 ctxt
= (MD5_CTX
*)(opad
+ 64);
500 /* compress the key if necessery */
501 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
503 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
504 _KEYLEN(state
->sav
->key_auth
));
505 MD5Final(&tk
[0], ctxt
);
509 key
= _KEYBUF(state
->sav
->key_auth
);
510 keylen
= _KEYLEN(state
->sav
->key_auth
);
515 memcpy(ipad
, key
, keylen
);
516 memcpy(opad
, key
, keylen
);
517 for (i
= 0; i
< 64; i
++) {
523 MD5Update(ctxt
, ipad
, 64);
529 ah_hmac_md5_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
534 if (!state
|| !state
->foo
)
535 panic("ah_hmac_md5_loop: what?");
536 ctxt
= (MD5_CTX
*)(((u_int8_t
*)state
->foo
) + 128);
537 MD5Update(ctxt
, addr
, len
);
541 ah_hmac_md5_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
544 u_char digest
[MD5_RESULTLEN
];
549 if (!state
|| !state
->foo
)
550 panic("ah_hmac_md5_result: what?");
552 ipad
= (u_char
*)state
->foo
;
553 opad
= (u_char
*)(ipad
+ 64);
554 ctxt
= (MD5_CTX
*)(opad
+ 64);
556 MD5Final(digest
, ctxt
);
559 MD5Update(ctxt
, opad
, 64);
560 MD5Update(ctxt
, digest
, sizeof(digest
));
561 MD5Final(digest
, ctxt
);
563 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
565 free(state
->foo
, M_TEMP
);
569 ah_hmac_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
574 u_char tk
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
580 panic("ah_hmac_sha1_init: what?");
583 state
->foo
= (void *)malloc(64 + 64 + sizeof(SHA1_CTX
),
588 ipad
= (u_char
*)state
->foo
;
589 opad
= (u_char
*)(ipad
+ 64);
590 ctxt
= (SHA1_CTX
*)(opad
+ 64);
592 /* compress the key if necessery */
593 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
595 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
596 _KEYLEN(state
->sav
->key_auth
));
597 SHA1Final(&tk
[0], ctxt
);
599 keylen
= SHA1_RESULTLEN
;
601 key
= _KEYBUF(state
->sav
->key_auth
);
602 keylen
= _KEYLEN(state
->sav
->key_auth
);
607 memcpy(ipad
, key
, keylen
);
608 memcpy(opad
, key
, keylen
);
609 for (i
= 0; i
< 64; i
++) {
615 SHA1Update(ctxt
, ipad
, 64);
621 ah_hmac_sha1_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
626 if (!state
|| !state
->foo
)
627 panic("ah_hmac_sha1_loop: what?");
629 ctxt
= (SHA1_CTX
*)(((u_char
*)state
->foo
) + 128);
630 SHA1Update(ctxt
, (u_int8_t
*)addr
, (size_t)len
);
634 ah_hmac_sha1_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
637 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
642 if (!state
|| !state
->foo
)
643 panic("ah_hmac_sha1_result: what?");
645 ipad
= (u_char
*)state
->foo
;
646 opad
= (u_char
*)(ipad
+ 64);
647 ctxt
= (SHA1_CTX
*)(opad
+ 64);
649 SHA1Final((u_int8_t
*)digest
, ctxt
);
652 SHA1Update(ctxt
, opad
, 64);
653 SHA1Update(ctxt
, (u_int8_t
*)digest
, sizeof(digest
));
654 SHA1Final((u_int8_t
*)digest
, ctxt
);
656 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
658 free(state
->foo
, M_TEMP
);
662 ah_hmac_sha2_256_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
667 u_char tk
[SHA256_DIGEST_LENGTH
];
673 panic("ah_hmac_sha2_256_init: what?");
676 state
->foo
= (void *)malloc(64 + 64 + sizeof(SHA256_CTX
),
681 ipad
= (u_char
*)state
->foo
;
682 opad
= (u_char
*)(ipad
+ 64);
683 ctxt
= (SHA256_CTX
*)(opad
+ 64);
685 /* compress the key if necessery */
686 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
687 memset(tk
, 0, sizeof(tk
));
689 SHA256_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
690 _KEYLEN(state
->sav
->key_auth
));
691 SHA256_Final(&tk
[0], ctxt
);
693 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
695 key
= _KEYBUF(state
->sav
->key_auth
);
696 keylen
= _KEYLEN(state
->sav
->key_auth
);
701 memcpy(ipad
, key
, keylen
);
702 memcpy(opad
, key
, keylen
);
703 for (i
= 0; i
< 64; i
++) {
709 SHA256_Update(ctxt
, ipad
, 64);
715 ah_hmac_sha2_256_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
720 if (!state
|| !state
->foo
)
721 panic("ah_hmac_sha2_256_loop: what?");
723 ctxt
= (SHA256_CTX
*)(((u_char
*)state
->foo
) + 128);
724 SHA256_Update(ctxt
, (void *)addr
, (size_t)len
);
728 ah_hmac_sha2_256_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
731 u_char digest
[SHA256_DIGEST_LENGTH
];
736 if (!state
|| !state
->foo
)
737 panic("ah_hmac_sha2_256_result: what?");
739 ipad
= (u_char
*)state
->foo
;
740 opad
= (u_char
*)(ipad
+ 64);
741 ctxt
= (SHA256_CTX
*)(opad
+ 64);
743 SHA256_Final((void *)digest
, ctxt
);
746 SHA256_Update(ctxt
, opad
, 64);
747 SHA256_Update(ctxt
, (void *)digest
, sizeof(digest
));
748 SHA256_Final((void *)digest
, ctxt
);
750 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
752 free(state
->foo
, M_TEMP
);
756 ah_hmac_sha2_384_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
761 u_char tk
[SHA384_DIGEST_LENGTH
];
767 panic("ah_hmac_sha2_384_init: what?");
770 state
->foo
= (void *)malloc(64 + 64 + sizeof(SHA384_CTX
),
774 memset(state
->foo
, 0, 64 + 64 + sizeof(SHA384_CTX
));
776 ipad
= (u_char
*)state
->foo
;
777 opad
= (u_char
*)(ipad
+ 64);
778 ctxt
= (SHA384_CTX
*)(opad
+ 64);
780 /* compress the key if necessery */
781 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
782 memset(tk
, 0, sizeof(tk
));
784 SHA384_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
785 _KEYLEN(state
->sav
->key_auth
));
786 SHA384_Final(&tk
[0], ctxt
);
788 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
790 key
= _KEYBUF(state
->sav
->key_auth
);
791 keylen
= _KEYLEN(state
->sav
->key_auth
);
796 memcpy(ipad
, key
, keylen
);
797 memcpy(opad
, key
, keylen
);
798 for (i
= 0; i
< 64; i
++) {
804 SHA384_Update(ctxt
, ipad
, 64);
810 ah_hmac_sha2_384_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
815 if (!state
|| !state
->foo
)
816 panic("ah_hmac_sha2_384_loop: what?");
818 ctxt
= (SHA384_CTX
*)(((u_char
*)state
->foo
) + 128);
819 SHA384_Update(ctxt
, (void *)addr
, (size_t)len
);
823 ah_hmac_sha2_384_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
826 u_char digest
[SHA384_DIGEST_LENGTH
];
831 if (!state
|| !state
->foo
)
832 panic("ah_hmac_sha2_384_result: what?");
834 ipad
= (u_char
*)state
->foo
;
835 opad
= (u_char
*)(ipad
+ 64);
836 ctxt
= (SHA384_CTX
*)(opad
+ 64);
838 SHA384_Final((void *)digest
, ctxt
);
841 SHA384_Update(ctxt
, opad
, 64);
842 SHA384_Update(ctxt
, (void *)digest
, sizeof(digest
));
843 SHA384_Final((void *)digest
, ctxt
);
845 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
847 free(state
->foo
, M_TEMP
);
851 ah_hmac_sha2_512_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
856 u_char tk
[SHA512_DIGEST_LENGTH
];
862 panic("ah_hmac_sha2_512_init: what?");
865 state
->foo
= (void *)malloc(64 + 64 + sizeof(SHA512_CTX
),
869 memset(state
->foo
, 0, 64 + 64 + sizeof(SHA512_CTX
));
871 ipad
= (u_char
*)state
->foo
;
872 opad
= (u_char
*)(ipad
+ 64);
873 ctxt
= (SHA512_CTX
*)(opad
+ 64);
875 /* compress the key if necessery */
876 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
877 memset(tk
, 0, sizeof(tk
));
879 SHA512_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
880 _KEYLEN(state
->sav
->key_auth
));
881 SHA512_Final(&tk
[0], ctxt
);
883 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
885 key
= _KEYBUF(state
->sav
->key_auth
);
886 keylen
= _KEYLEN(state
->sav
->key_auth
);
891 memcpy(ipad
, key
, keylen
);
892 memcpy(opad
, key
, keylen
);
893 for (i
= 0; i
< 64; i
++) {
899 SHA512_Update(ctxt
, ipad
, 64);
905 ah_hmac_sha2_512_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
910 if (!state
|| !state
->foo
)
911 panic("ah_hmac_sha2_512_loop: what?");
913 ctxt
= (SHA512_CTX
*)(((u_char
*)state
->foo
) + 128);
914 SHA512_Update(ctxt
, (void *)addr
, (size_t)len
);
918 ah_hmac_sha2_512_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
921 u_char digest
[SHA512_DIGEST_LENGTH
];
926 if (!state
|| !state
->foo
)
927 panic("ah_hmac_sha2_512_result: what?");
929 ipad
= (u_char
*)state
->foo
;
930 opad
= (u_char
*)(ipad
+ 64);
931 ctxt
= (SHA512_CTX
*)(opad
+ 64);
933 SHA512_Final((void *)digest
, ctxt
);
936 SHA512_Update(ctxt
, opad
, 64);
937 SHA512_Update(ctxt
, (void *)digest
, sizeof(digest
));
938 SHA512_Final((void *)digest
, ctxt
);
940 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
942 free(state
->foo
, M_TEMP
);
946 ah_hmac_ripemd160_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
951 u_char tk
[RIPEMD160_RESULTLEN
];
957 panic("ah_hmac_ripemd160_init: what?");
960 state
->foo
= (void *)malloc(64 + 64 + sizeof(RMD160_CTX
),
964 memset(state
->foo
, 0, 64 + 64 + sizeof(RMD160_CTX
));
966 ipad
= (u_char
*)state
->foo
;
967 opad
= (u_char
*)(ipad
+ 64);
968 ctxt
= (RMD160_CTX
*)(opad
+ 64);
970 /* compress the key if necessery */
971 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
972 memset(tk
, 0, sizeof(tk
));
974 RMD160Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
975 _KEYLEN(state
->sav
->key_auth
));
976 RMD160Final(&tk
[0], ctxt
);
978 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
980 key
= _KEYBUF(state
->sav
->key_auth
);
981 keylen
= _KEYLEN(state
->sav
->key_auth
);
986 memcpy(ipad
, key
, keylen
);
987 memcpy(opad
, key
, keylen
);
988 for (i
= 0; i
< 64; i
++) {
994 RMD160Update(ctxt
, ipad
, 64);
1000 ah_hmac_ripemd160_loop(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
1005 if (!state
|| !state
->foo
)
1006 panic("ah_hmac_ripemd160_loop: what?");
1008 ctxt
= (RMD160_CTX
*)(((u_char
*)state
->foo
) + 128);
1009 RMD160Update(ctxt
, (void *)addr
, (size_t)len
);
1013 ah_hmac_ripemd160_result(struct ah_algorithm_state
*state
, u_int8_t
*addr
,
1016 u_char digest
[RIPEMD160_RESULTLEN
];
1021 if (!state
|| !state
->foo
)
1022 panic("ah_hmac_ripemd160_result: what?");
1024 ipad
= (u_char
*)state
->foo
;
1025 opad
= (u_char
*)(ipad
+ 64);
1026 ctxt
= (RMD160_CTX
*)(opad
+ 64);
1028 RMD160Final((void *)digest
, ctxt
);
1031 RMD160Update(ctxt
, opad
, 64);
1032 RMD160Update(ctxt
, (void *)digest
, sizeof(digest
));
1033 RMD160Final((void *)digest
, ctxt
);
1035 memcpy(addr
, digest
, sizeof(digest
) > l
? l
: sizeof(digest
));
1037 free(state
->foo
, M_TEMP
);
1040 /*------------------------------------------------------------*/
1043 * go generate the checksum.
1046 ah_update_mbuf(struct mbuf
*m
, int off
, int len
,
1047 const struct ah_algorithm
*algo
, struct ah_algorithm_state
*algos
)
1052 /* easy case first */
1053 if (off
+ len
<= m
->m_len
) {
1054 (algo
->update
)(algos
, mtod(m
, u_int8_t
*) + off
, len
);
1058 for (n
= m
; n
; n
= n
->m_next
) {
1066 panic("ah_update_mbuf: wrong offset specified");
1068 for (/* nothing */; n
&& len
> 0; n
= n
->m_next
) {
1071 if (n
->m_len
- off
< len
)
1072 tlen
= n
->m_len
- off
;
1076 (algo
->update
)(algos
, mtod(n
, u_int8_t
*) + off
, tlen
);
1085 * Go generate the checksum. This function won't modify the mbuf chain
1088 * NOTE: the function does not free mbuf on failure.
1089 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1092 ah4_calccksum(struct mbuf
*m
, u_int8_t
*ahdat
, size_t len
,
1093 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1097 size_t advancewidth
;
1098 struct ah_algorithm_state algos
;
1099 u_char sumbuf
[AH_MAXSUMSIZE
];
1102 struct mbuf
*n
= NULL
;
1104 if ((m
->m_flags
& M_PKTHDR
) == 0)
1108 hdrtype
= -1; /* dummy, it is called IPPROTO_IP */
1112 error
= (algo
->init
)(&algos
, sav
);
1116 advancewidth
= 0; /* safety */
1121 case -1: /* first one only */
1124 * copy ip hdr, modify to fit the AH checksum rule,
1125 * then take a checksum.
1130 m_copydata(m
, off
, sizeof(iphdr
), (void *)&iphdr
);
1131 hlen
= iphdr
.ip_hl
<< 2;
1133 iphdr
.ip_sum
= htons(0);
1134 if (ip4_ah_cleartos
)
1136 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1137 (algo
->update
)(&algos
, (u_int8_t
*)&iphdr
, sizeof(struct ip
));
1139 if (hlen
!= sizeof(struct ip
)) {
1143 if (hlen
> MCLBYTES
) {
1147 MGET(n
, M_DONTWAIT
, MT_DATA
);
1148 if (n
&& hlen
> MLEN
) {
1149 MCLGET(n
, M_DONTWAIT
);
1150 if ((n
->m_flags
& M_EXT
) == 0) {
1159 m_copydata(m
, off
, hlen
, mtod(n
, void *));
1162 * IP options processing.
1163 * See RFC2402 appendix A.
1165 p
= mtod(n
, u_char
*);
1166 i
= sizeof(struct ip
);
1168 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1169 ipseclog((LOG_ERR
, "ah4_calccksum: "
1170 "invalid IP option\n"));
1174 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1175 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1176 i
+ IPOPT_OLEN
< hlen
)
1180 "ah4_calccksum: invalid IP option "
1182 p
[i
+ IPOPT_OPTVAL
]));
1188 switch (p
[i
+ IPOPT_OPTVAL
]) {
1194 case IPOPT_SECURITY
: /* 0x82 */
1195 case 0x85: /* Extended security */
1196 case 0x86: /* Commercial security */
1197 case 0x94: /* Router alert */
1198 case 0x95: /* RFC1770 */
1199 l
= p
[i
+ IPOPT_OLEN
];
1205 l
= p
[i
+ IPOPT_OLEN
];
1211 if (l
< 1 || hlen
- i
< l
) {
1214 "ah4_calccksum: invalid IP option "
1215 "(type=%02x len=%02x)\n",
1216 p
[i
+ IPOPT_OPTVAL
],
1217 p
[i
+ IPOPT_OLEN
]));
1222 memset(p
+ i
, 0, l
);
1223 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
)
1227 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1228 (algo
->update
)(&algos
, p
, hlen
- sizeof(struct ip
));
1234 hdrtype
= (iphdr
.ip_p
) & 0xff;
1235 advancewidth
= hlen
;
1246 m_copydata(m
, off
, sizeof(ah
), (void *)&ah
);
1247 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1249 : sizeof(struct newah
);
1250 siz
= (*algo
->sumsiz
)(sav
);
1251 totlen
= (ah
.ah_len
+ 2) << 2;
1254 * special treatment is necessary for the first one, not others
1257 if (totlen
> m
->m_pkthdr
.len
- off
||
1258 totlen
> MCLBYTES
) {
1262 MGET(n
, M_DONTWAIT
, MT_DATA
);
1263 if (n
&& totlen
> MLEN
) {
1264 MCLGET(n
, M_DONTWAIT
);
1265 if ((n
->m_flags
& M_EXT
) == 0) {
1274 m_copydata(m
, off
, totlen
, mtod(n
, void *));
1276 memset(mtod(n
, u_int8_t
*) + hdrsiz
, 0, siz
);
1277 (algo
->update
)(&algos
, mtod(n
, u_int8_t
*), n
->m_len
);
1281 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1284 hdrtype
= ah
.ah_nxt
;
1285 advancewidth
= totlen
;
1290 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1291 advancewidth
= m
->m_pkthdr
.len
- off
;
1295 off
+= advancewidth
;
1296 if (off
< m
->m_pkthdr
.len
)
1299 if (len
< (*algo
->sumsiz
)(sav
)) {
1304 (algo
->result
)(&algos
, sumbuf
, sizeof(sumbuf
));
1305 memcpy(ahdat
, &sumbuf
[0], (*algo
->sumsiz
)(sav
));
1320 * Go generate the checksum. This function won't modify the mbuf chain
1323 * NOTE: the function does not free mbuf on failure.
1324 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1327 ah6_calccksum(struct mbuf
*m
, u_int8_t
*ahdat
, size_t len
,
1328 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1332 struct mbuf
*n
= NULL
;
1335 struct ah_algorithm_state algos
;
1336 u_char sumbuf
[AH_MAXSUMSIZE
];
1338 if ((m
->m_flags
& M_PKTHDR
) == 0)
1341 error
= (algo
->init
)(&algos
, sav
);
1346 proto
= IPPROTO_IPV6
;
1351 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1353 newoff
= m
->m_pkthdr
.len
;
1354 else if (newoff
<= off
) {
1362 * special treatment is necessary for the first one, not others
1365 struct ip6_hdr ip6copy
;
1367 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1372 m_copydata(m
, off
, newoff
- off
, (void *)&ip6copy
);
1374 ip6copy
.ip6_flow
= 0;
1375 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1376 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1377 ip6copy
.ip6_hlim
= 0;
1378 in6_clearscope(&ip6copy
.ip6_src
); /* XXX */
1379 in6_clearscope(&ip6copy
.ip6_dst
); /* XXX */
1380 (algo
->update
)(&algos
, (u_int8_t
*)&ip6copy
,
1381 sizeof(struct ip6_hdr
));
1383 newoff
= m
->m_pkthdr
.len
;
1384 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1394 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1396 : sizeof(struct newah
);
1397 siz
= (*algo
->sumsiz
)(sav
);
1400 * special treatment is necessary for the first one, not others
1403 if (newoff
- off
> MCLBYTES
) {
1407 MGET(n
, M_DONTWAIT
, MT_DATA
);
1408 if (n
&& newoff
- off
> MLEN
) {
1409 MCLGET(n
, M_DONTWAIT
);
1410 if ((n
->m_flags
& M_EXT
) == 0) {
1419 m_copydata(m
, off
, newoff
- off
, mtod(n
, void *));
1420 n
->m_len
= newoff
- off
;
1421 memset(mtod(n
, u_int8_t
*) + hdrsiz
, 0, siz
);
1422 (algo
->update
)(&algos
, mtod(n
, u_int8_t
*), n
->m_len
);
1426 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1431 case IPPROTO_HOPOPTS
:
1432 case IPPROTO_DSTOPTS
:
1434 struct ip6_ext
*ip6e
;
1436 u_int8_t
*p
, *optend
, *optp
;
1438 if (newoff
- off
> MCLBYTES
) {
1442 MGET(n
, M_DONTWAIT
, MT_DATA
);
1443 if (n
&& newoff
- off
> MLEN
) {
1444 MCLGET(n
, M_DONTWAIT
);
1445 if ((n
->m_flags
& M_EXT
) == 0) {
1454 m_copydata(m
, off
, newoff
- off
, mtod(n
, void *));
1455 n
->m_len
= newoff
- off
;
1457 ip6e
= mtod(n
, struct ip6_ext
*);
1458 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1459 if (newoff
- off
< hdrlen
) {
1465 p
= mtod(n
, u_int8_t
*);
1466 optend
= p
+ hdrlen
;
1469 * ICV calculation for the options header including all
1470 * options. This part is a little tricky since there are
1471 * two type of options; mutable and immutable. We try to
1472 * null-out mutable ones here.
1475 while (optp
< optend
) {
1476 if (optp
[0] == IP6OPT_PAD1
)
1479 if (optp
+ 2 > optend
) {
1485 optlen
= optp
[1] + 2;
1488 if (optp
+ optlen
> optend
) {
1495 if (optp
[0] & IP6OPT_MUTABLE
)
1496 memset(optp
+ 2, 0, optlen
- 2);
1501 (algo
->update
)(&algos
, mtod(n
, u_int8_t
*), n
->m_len
);
1507 case IPPROTO_ROUTING
:
1509 * For an input packet, we can just calculate `as is'.
1510 * For an output packet, we assume ip6_output have already
1511 * made packet how it will be received at the final
1517 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1521 if (newoff
< m
->m_pkthdr
.len
) {
1527 if (len
< (*algo
->sumsiz
)(sav
)) {
1532 (algo
->result
)(&algos
, sumbuf
, sizeof(sumbuf
));
1533 memcpy(ahdat
, &sumbuf
[0], (*algo
->sumsiz
)(sav
));