1 /* $FreeBSD: src/sys/netinet6/ah_core.c,v 1.2.2.4 2001/07/03 11:01:49 ume Exp $ */
2 /* $KAME: ah_core.c,v 1.44 2001/03/12 11:24:39 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 /* TODO: have shared routines for hmac-* algorithms */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet/icmp6.h>
65 #include <netinet6/ipsec.h>
67 #include <netinet6/ipsec6.h>
69 #include <netinet6/ah.h>
71 #include <netinet6/ah6.h>
74 #include <netinet6/esp.h>
76 #include <netinet6/esp6.h>
79 #include <net/pfkeyv2.h>
80 #include <netkey/keydb.h>
82 #include <crypto/sha1.h>
83 #include <crypto/sha2/sha2.h>
85 #include <net/net_osdep.h>
89 static int ah_sumsiz_1216
__P((struct secasvar
*));
90 static int ah_sumsiz_zero
__P((struct secasvar
*));
91 static int ah_none_mature
__P((struct secasvar
*));
92 static int ah_none_init
__P((struct ah_algorithm_state
*, struct secasvar
*));
93 static void ah_none_loop
__P((struct ah_algorithm_state
*, caddr_t
, size_t));
94 static void ah_none_result
__P((struct ah_algorithm_state
*, caddr_t
));
95 static int ah_keyed_md5_mature
__P((struct secasvar
*));
96 static int ah_keyed_md5_init
__P((struct ah_algorithm_state
*,
98 static void ah_keyed_md5_loop
__P((struct ah_algorithm_state
*, caddr_t
,
100 static void ah_keyed_md5_result
__P((struct ah_algorithm_state
*, caddr_t
));
101 static int ah_keyed_sha1_mature
__P((struct secasvar
*));
102 static int ah_keyed_sha1_init
__P((struct ah_algorithm_state
*,
104 static void ah_keyed_sha1_loop
__P((struct ah_algorithm_state
*, caddr_t
,
106 static void ah_keyed_sha1_result
__P((struct ah_algorithm_state
*, caddr_t
));
107 static int ah_hmac_md5_mature
__P((struct secasvar
*));
108 static int ah_hmac_md5_init
__P((struct ah_algorithm_state
*,
110 static void ah_hmac_md5_loop
__P((struct ah_algorithm_state
*, caddr_t
,
112 static void ah_hmac_md5_result
__P((struct ah_algorithm_state
*, caddr_t
));
113 static int ah_hmac_sha1_mature
__P((struct secasvar
*));
114 static int ah_hmac_sha1_init
__P((struct ah_algorithm_state
*,
116 static void ah_hmac_sha1_loop
__P((struct ah_algorithm_state
*, caddr_t
,
118 static void ah_hmac_sha1_result
__P((struct ah_algorithm_state
*, caddr_t
));
119 static int ah_hmac_sha2_256_mature
__P((struct secasvar
*));
120 static int ah_hmac_sha2_256_init
__P((struct ah_algorithm_state
*,
122 static void ah_hmac_sha2_256_loop
__P((struct ah_algorithm_state
*, caddr_t
,
124 static void ah_hmac_sha2_256_result
__P((struct ah_algorithm_state
*, caddr_t
));
125 static int ah_hmac_sha2_384_mature
__P((struct secasvar
*));
126 static int ah_hmac_sha2_384_init
__P((struct ah_algorithm_state
*,
128 static void ah_hmac_sha2_384_loop
__P((struct ah_algorithm_state
*, caddr_t
,
130 static void ah_hmac_sha2_384_result
__P((struct ah_algorithm_state
*, caddr_t
));
131 static int ah_hmac_sha2_512_mature
__P((struct secasvar
*));
132 static int ah_hmac_sha2_512_init
__P((struct ah_algorithm_state
*,
134 static void ah_hmac_sha2_512_loop
__P((struct ah_algorithm_state
*, caddr_t
,
136 static void ah_hmac_sha2_512_result
__P((struct ah_algorithm_state
*, caddr_t
));
138 static void ah_update_mbuf
__P((struct mbuf
*, int, int,
139 const struct ah_algorithm
*, struct ah_algorithm_state
*));
141 const struct ah_algorithm
*
142 ah_algorithm_lookup(idx
)
145 /* checksum algorithms */
146 static struct ah_algorithm ah_algorithms
[] = {
147 { ah_sumsiz_1216
, ah_hmac_md5_mature
, 128, 128, "hmac-md5",
148 ah_hmac_md5_init
, ah_hmac_md5_loop
,
149 ah_hmac_md5_result
, },
150 { ah_sumsiz_1216
, ah_hmac_sha1_mature
, 160, 160, "hmac-sha1",
151 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
152 ah_hmac_sha1_result
, },
153 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
154 ah_keyed_md5_init
, ah_keyed_md5_loop
,
155 ah_keyed_md5_result
, },
156 { ah_sumsiz_1216
, ah_keyed_sha1_mature
, 160, 160, "keyed-sha1",
157 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
158 ah_keyed_sha1_result
, },
159 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
160 ah_none_init
, ah_none_loop
, ah_none_result
, },
161 { ah_sumsiz_1216
, ah_hmac_sha2_256_mature
, 256, 256,
163 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
164 ah_hmac_sha2_256_result
, },
165 { ah_sumsiz_1216
, ah_hmac_sha2_384_mature
, 384, 384,
167 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
168 ah_hmac_sha2_384_result
, },
169 { ah_sumsiz_1216
, ah_hmac_sha2_512_mature
, 512, 512,
171 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
172 ah_hmac_sha2_512_result
, },
176 case SADB_AALG_MD5HMAC
:
177 return &ah_algorithms
[0];
178 case SADB_AALG_SHA1HMAC
:
179 return &ah_algorithms
[1];
180 case SADB_X_AALG_MD5
:
181 return &ah_algorithms
[2];
182 case SADB_X_AALG_SHA
:
183 return &ah_algorithms
[3];
184 case SADB_X_AALG_NULL
:
185 return &ah_algorithms
[4];
186 case SADB_X_AALG_SHA2_256
:
187 return &ah_algorithms
[5];
188 case SADB_X_AALG_SHA2_384
:
189 return &ah_algorithms
[6];
190 case SADB_X_AALG_SHA2_512
:
191 return &ah_algorithms
[7];
200 struct secasvar
*sav
;
204 if (sav
->flags
& SADB_X_EXT_OLD
)
212 struct secasvar
*sav
;
221 struct secasvar
*sav
;
223 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
225 "ah_none_mature: protocol and algorithm mismatch.\n"));
232 ah_none_init(state
, sav
)
233 struct ah_algorithm_state
*state
;
234 struct secasvar
*sav
;
241 ah_none_loop(state
, addr
, len
)
242 struct ah_algorithm_state
*state
;
249 ah_none_result(state
, addr
)
250 struct ah_algorithm_state
*state
;
256 ah_keyed_md5_mature(sav
)
257 struct secasvar
*sav
;
259 /* anything is okay */
264 ah_keyed_md5_init(state
, sav
)
265 struct ah_algorithm_state
*state
;
266 struct secasvar
*sav
;
273 panic("ah_keyed_md5_init: what?");
276 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_WAITOK
);
277 if (state
->foo
== NULL
)
280 MD5Init((MD5_CTX
*)state
->foo
);
282 MD5Update((MD5_CTX
*)state
->foo
,
283 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
284 (u_int
)_KEYLEN(state
->sav
->key_auth
));
288 * We cannot simply use md5_pad() since the function
289 * won't update the total length.
291 if (_KEYLEN(state
->sav
->key_auth
) < 56)
292 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
294 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
295 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
299 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
302 bzero(buf
, sizeof(buf
));
303 while (sizeof(buf
) < padlen
) {
304 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
305 padlen
-= sizeof(buf
);
308 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
311 buf
[0] = (keybitlen
>> 0) & 0xff;
312 buf
[1] = (keybitlen
>> 8) & 0xff;
313 buf
[2] = (keybitlen
>> 16) & 0xff;
314 buf
[3] = (keybitlen
>> 24) & 0xff;
315 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
322 ah_keyed_md5_loop(state
, addr
, len
)
323 struct ah_algorithm_state
*state
;
328 panic("ah_keyed_md5_loop: what?");
330 MD5Update((MD5_CTX
*)state
->foo
, addr
, len
);
334 ah_keyed_md5_result(state
, addr
)
335 struct ah_algorithm_state
*state
;
341 panic("ah_keyed_md5_result: what?");
344 MD5Update((MD5_CTX
*)state
->foo
,
345 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
346 (u_int
)_KEYLEN(state
->sav
->key_auth
));
348 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
349 FREE(state
->foo
, M_TEMP
);
350 bcopy(&digest
[0], (void *)addr
, sizeof(digest
));
354 ah_keyed_sha1_mature(sav
)
355 struct secasvar
*sav
;
357 const struct ah_algorithm
*algo
;
359 if (!sav
->key_auth
) {
360 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
364 algo
= ah_algorithm_lookup(sav
->alg_auth
);
366 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
370 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
371 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
373 "ah_keyed_sha1_mature: invalid key length %d.\n",
374 sav
->key_auth
->sadb_key_bits
));
382 ah_keyed_sha1_init(state
, sav
)
383 struct ah_algorithm_state
*state
;
384 struct secasvar
*sav
;
392 panic("ah_keyed_sha1_init: what?");
395 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_WAITOK
);
399 ctxt
= (SHA1_CTX
*)state
->foo
;
403 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
404 (u_int
)_KEYLEN(state
->sav
->key_auth
));
409 if (_KEYLEN(state
->sav
->key_auth
) < 56)
410 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
412 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
413 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
417 SHA1Update(ctxt
, &buf
[0], 1);
420 bzero(buf
, sizeof(buf
));
421 while (sizeof(buf
) < padlen
) {
422 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
423 padlen
-= sizeof(buf
);
426 SHA1Update(ctxt
, &buf
[0], padlen
);
429 buf
[0] = (keybitlen
>> 0) & 0xff;
430 buf
[1] = (keybitlen
>> 8) & 0xff;
431 buf
[2] = (keybitlen
>> 16) & 0xff;
432 buf
[3] = (keybitlen
>> 24) & 0xff;
433 SHA1Update(ctxt
, buf
, 8);
440 ah_keyed_sha1_loop(state
, addr
, len
)
441 struct ah_algorithm_state
*state
;
447 if (!state
|| !state
->foo
)
448 panic("ah_keyed_sha1_loop: what?");
449 ctxt
= (SHA1_CTX
*)state
->foo
;
451 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
455 ah_keyed_sha1_result(state
, addr
)
456 struct ah_algorithm_state
*state
;
459 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
462 if (!state
|| !state
->foo
)
463 panic("ah_keyed_sha1_result: what?");
464 ctxt
= (SHA1_CTX
*)state
->foo
;
467 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
468 (u_int
)_KEYLEN(state
->sav
->key_auth
));
470 SHA1Final((caddr_t
)&digest
[0], ctxt
);
471 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
473 FREE(state
->foo
, M_TEMP
);
477 ah_hmac_md5_mature(sav
)
478 struct secasvar
*sav
;
480 const struct ah_algorithm
*algo
;
482 if (!sav
->key_auth
) {
483 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
487 algo
= ah_algorithm_lookup(sav
->alg_auth
);
489 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
493 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
494 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
496 "ah_hmac_md5_mature: invalid key length %d.\n",
497 sav
->key_auth
->sadb_key_bits
));
505 ah_hmac_md5_init(state
, sav
)
506 struct ah_algorithm_state
*state
;
507 struct secasvar
*sav
;
518 panic("ah_hmac_md5_init: what?");
521 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_WAITOK
);
525 ipad
= (u_char
*)state
->foo
;
526 opad
= (u_char
*)(ipad
+ 64);
527 ctxt
= (MD5_CTX
*)(opad
+ 64);
529 /* compress the key if necessery */
530 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
532 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
533 _KEYLEN(state
->sav
->key_auth
));
534 MD5Final(&tk
[0], ctxt
);
538 key
= _KEYBUF(state
->sav
->key_auth
);
539 keylen
= _KEYLEN(state
->sav
->key_auth
);
544 bcopy(key
, ipad
, keylen
);
545 bcopy(key
, opad
, keylen
);
546 for (i
= 0; i
< 64; i
++) {
552 MD5Update(ctxt
, ipad
, 64);
558 ah_hmac_md5_loop(state
, addr
, len
)
559 struct ah_algorithm_state
*state
;
565 if (!state
|| !state
->foo
)
566 panic("ah_hmac_md5_loop: what?");
567 ctxt
= (MD5_CTX
*)(((caddr_t
)state
->foo
) + 128);
568 MD5Update(ctxt
, addr
, len
);
572 ah_hmac_md5_result(state
, addr
)
573 struct ah_algorithm_state
*state
;
581 if (!state
|| !state
->foo
)
582 panic("ah_hmac_md5_result: what?");
584 ipad
= (u_char
*)state
->foo
;
585 opad
= (u_char
*)(ipad
+ 64);
586 ctxt
= (MD5_CTX
*)(opad
+ 64);
588 MD5Final(&digest
[0], ctxt
);
591 MD5Update(ctxt
, opad
, 64);
592 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
593 MD5Final(&digest
[0], ctxt
);
595 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
597 FREE(state
->foo
, M_TEMP
);
601 ah_hmac_sha1_mature(sav
)
602 struct secasvar
*sav
;
604 const struct ah_algorithm
*algo
;
606 if (!sav
->key_auth
) {
607 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
611 algo
= ah_algorithm_lookup(sav
->alg_auth
);
613 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
617 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
618 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
620 "ah_hmac_sha1_mature: invalid key length %d.\n",
621 sav
->key_auth
->sadb_key_bits
));
629 ah_hmac_sha1_init(state
, sav
)
630 struct ah_algorithm_state
*state
;
631 struct secasvar
*sav
;
636 u_char tk
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
642 panic("ah_hmac_sha1_init: what?");
645 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
650 ipad
= (u_char
*)state
->foo
;
651 opad
= (u_char
*)(ipad
+ 64);
652 ctxt
= (SHA1_CTX
*)(opad
+ 64);
654 /* compress the key if necessery */
655 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
657 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
658 _KEYLEN(state
->sav
->key_auth
));
659 SHA1Final(&tk
[0], ctxt
);
661 keylen
= SHA1_RESULTLEN
;
663 key
= _KEYBUF(state
->sav
->key_auth
);
664 keylen
= _KEYLEN(state
->sav
->key_auth
);
669 bcopy(key
, ipad
, keylen
);
670 bcopy(key
, opad
, keylen
);
671 for (i
= 0; i
< 64; i
++) {
677 SHA1Update(ctxt
, ipad
, 64);
683 ah_hmac_sha1_loop(state
, addr
, len
)
684 struct ah_algorithm_state
*state
;
690 if (!state
|| !state
->foo
)
691 panic("ah_hmac_sha1_loop: what?");
693 ctxt
= (SHA1_CTX
*)(((u_char
*)state
->foo
) + 128);
694 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
698 ah_hmac_sha1_result(state
, addr
)
699 struct ah_algorithm_state
*state
;
702 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
707 if (!state
|| !state
->foo
)
708 panic("ah_hmac_sha1_result: what?");
710 ipad
= (u_char
*)state
->foo
;
711 opad
= (u_char
*)(ipad
+ 64);
712 ctxt
= (SHA1_CTX
*)(opad
+ 64);
714 SHA1Final((caddr_t
)&digest
[0], ctxt
);
717 SHA1Update(ctxt
, opad
, 64);
718 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
719 SHA1Final((caddr_t
)&digest
[0], ctxt
);
721 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
723 FREE(state
->foo
, M_TEMP
);
727 ah_hmac_sha2_256_mature(sav
)
728 struct secasvar
*sav
;
730 const struct ah_algorithm
*algo
;
732 if (!sav
->key_auth
) {
734 "ah_hmac_sha2_256_mature: no key is given.\n"));
738 algo
= ah_algorithm_lookup(sav
->alg_auth
);
741 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
745 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
746 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
748 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
749 sav
->key_auth
->sadb_key_bits
));
757 ah_hmac_sha2_256_init(state
, sav
)
758 struct ah_algorithm_state
*state
;
759 struct secasvar
*sav
;
764 u_char tk
[SHA256_DIGEST_LENGTH
];
770 panic("ah_hmac_sha2_256_init: what?");
773 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
778 ipad
= (u_char
*)state
->foo
;
779 opad
= (u_char
*)(ipad
+ 64);
780 ctxt
= (SHA256_CTX
*)(opad
+ 64);
782 /* compress the key if necessery */
783 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
784 bzero(tk
, sizeof(tk
));
785 bzero(ctxt
, sizeof(*ctxt
));
787 SHA256_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
788 _KEYLEN(state
->sav
->key_auth
));
789 SHA256_Final(&tk
[0], ctxt
);
791 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
793 key
= _KEYBUF(state
->sav
->key_auth
);
794 keylen
= _KEYLEN(state
->sav
->key_auth
);
799 bcopy(key
, ipad
, keylen
);
800 bcopy(key
, opad
, keylen
);
801 for (i
= 0; i
< 64; i
++) {
806 bzero(ctxt
, sizeof(*ctxt
));
808 SHA256_Update(ctxt
, ipad
, 64);
814 ah_hmac_sha2_256_loop(state
, addr
, len
)
815 struct ah_algorithm_state
*state
;
821 if (!state
|| !state
->foo
)
822 panic("ah_hmac_sha2_256_loop: what?");
824 ctxt
= (SHA256_CTX
*)(((u_char
*)state
->foo
) + 128);
825 SHA256_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
829 ah_hmac_sha2_256_result(state
, addr
)
830 struct ah_algorithm_state
*state
;
833 u_char digest
[SHA256_DIGEST_LENGTH
];
838 if (!state
|| !state
->foo
)
839 panic("ah_hmac_sha2_256_result: what?");
841 ipad
= (u_char
*)state
->foo
;
842 opad
= (u_char
*)(ipad
+ 64);
843 ctxt
= (SHA256_CTX
*)(opad
+ 64);
845 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
847 bzero(ctxt
, sizeof(*ctxt
));
849 SHA256_Update(ctxt
, opad
, 64);
850 SHA256_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
851 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
853 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
855 FREE(state
->foo
, M_TEMP
);
859 ah_hmac_sha2_384_mature(sav
)
860 struct secasvar
*sav
;
862 const struct ah_algorithm
*algo
;
864 if (!sav
->key_auth
) {
866 "ah_hmac_sha2_384_mature: no key is given.\n"));
870 algo
= ah_algorithm_lookup(sav
->alg_auth
);
873 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
877 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
878 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
880 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
881 sav
->key_auth
->sadb_key_bits
));
889 ah_hmac_sha2_384_init(state
, sav
)
890 struct ah_algorithm_state
*state
;
891 struct secasvar
*sav
;
896 u_char tk
[SHA384_DIGEST_LENGTH
];
902 panic("ah_hmac_sha2_384_init: what?");
905 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA384_CTX
),
909 bzero(state
->foo
, 64 + 64 + sizeof(SHA384_CTX
));
911 ipad
= (u_char
*)state
->foo
;
912 opad
= (u_char
*)(ipad
+ 64);
913 ctxt
= (SHA384_CTX
*)(opad
+ 64);
915 /* compress the key if necessery */
916 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
917 bzero(tk
, sizeof(tk
));
918 bzero(ctxt
, sizeof(*ctxt
));
920 SHA384_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
921 _KEYLEN(state
->sav
->key_auth
));
922 SHA384_Final(&tk
[0], ctxt
);
924 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
926 key
= _KEYBUF(state
->sav
->key_auth
);
927 keylen
= _KEYLEN(state
->sav
->key_auth
);
932 bcopy(key
, ipad
, keylen
);
933 bcopy(key
, opad
, keylen
);
934 for (i
= 0; i
< 64; i
++) {
939 bzero(ctxt
, sizeof(*ctxt
));
941 SHA384_Update(ctxt
, ipad
, 64);
947 ah_hmac_sha2_384_loop(state
, addr
, len
)
948 struct ah_algorithm_state
*state
;
954 if (!state
|| !state
->foo
)
955 panic("ah_hmac_sha2_384_loop: what?");
957 ctxt
= (SHA384_CTX
*)(((u_char
*)state
->foo
) + 128);
958 SHA384_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
962 ah_hmac_sha2_384_result(state
, addr
)
963 struct ah_algorithm_state
*state
;
966 u_char digest
[SHA384_DIGEST_LENGTH
];
971 if (!state
|| !state
->foo
)
972 panic("ah_hmac_sha2_384_result: what?");
974 ipad
= (u_char
*)state
->foo
;
975 opad
= (u_char
*)(ipad
+ 64);
976 ctxt
= (SHA384_CTX
*)(opad
+ 64);
978 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
980 bzero(ctxt
, sizeof(*ctxt
));
982 SHA384_Update(ctxt
, opad
, 64);
983 SHA384_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
984 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
986 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
988 FREE(state
->foo
, M_TEMP
);
992 ah_hmac_sha2_512_mature(sav
)
993 struct secasvar
*sav
;
995 const struct ah_algorithm
*algo
;
997 if (!sav
->key_auth
) {
999 "ah_hmac_sha2_512_mature: no key is given.\n"));
1003 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1006 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1010 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1011 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1013 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1014 sav
->key_auth
->sadb_key_bits
));
1022 ah_hmac_sha2_512_init(state
, sav
)
1023 struct ah_algorithm_state
*state
;
1024 struct secasvar
*sav
;
1029 u_char tk
[SHA512_DIGEST_LENGTH
];
1035 panic("ah_hmac_sha2_512_init: what?");
1038 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA512_CTX
),
1042 bzero(state
->foo
, 64 + 64 + sizeof(SHA512_CTX
));
1044 ipad
= (u_char
*)state
->foo
;
1045 opad
= (u_char
*)(ipad
+ 64);
1046 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1048 /* compress the key if necessery */
1049 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
1050 bzero(tk
, sizeof(tk
));
1051 bzero(ctxt
, sizeof(*ctxt
));
1053 SHA512_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
1054 _KEYLEN(state
->sav
->key_auth
));
1055 SHA512_Final(&tk
[0], ctxt
);
1057 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
1059 key
= _KEYBUF(state
->sav
->key_auth
);
1060 keylen
= _KEYLEN(state
->sav
->key_auth
);
1065 bcopy(key
, ipad
, keylen
);
1066 bcopy(key
, opad
, keylen
);
1067 for (i
= 0; i
< 64; i
++) {
1072 bzero(ctxt
, sizeof(*ctxt
));
1074 SHA512_Update(ctxt
, ipad
, 64);
1080 ah_hmac_sha2_512_loop(state
, addr
, len
)
1081 struct ah_algorithm_state
*state
;
1087 if (!state
|| !state
->foo
)
1088 panic("ah_hmac_sha2_512_loop: what?");
1090 ctxt
= (SHA512_CTX
*)(((u_char
*)state
->foo
) + 128);
1091 SHA512_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
1095 ah_hmac_sha2_512_result(state
, addr
)
1096 struct ah_algorithm_state
*state
;
1099 u_char digest
[SHA512_DIGEST_LENGTH
];
1104 if (!state
|| !state
->foo
)
1105 panic("ah_hmac_sha2_512_result: what?");
1107 ipad
= (u_char
*)state
->foo
;
1108 opad
= (u_char
*)(ipad
+ 64);
1109 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1111 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1113 bzero(ctxt
, sizeof(*ctxt
));
1115 SHA512_Update(ctxt
, opad
, 64);
1116 SHA512_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
1117 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1119 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
1121 FREE(state
->foo
, M_TEMP
);
1124 /*------------------------------------------------------------*/
1127 * go generate the checksum.
1130 ah_update_mbuf(m
, off
, len
, algo
, algos
)
1134 const struct ah_algorithm
*algo
;
1135 struct ah_algorithm_state
*algos
;
1140 /* easy case first */
1141 if (off
+ len
<= m
->m_len
) {
1142 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1146 for (n
= m
; n
; n
= n
->m_next
) {
1154 panic("ah_update_mbuf: wrong offset specified");
1156 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1159 if (n
->m_len
- off
< len
)
1160 tlen
= n
->m_len
- off
;
1164 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1173 * Go generate the checksum. This function won't modify the mbuf chain
1176 * NOTE: the function does not free mbuf on failure.
1177 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1180 ah4_calccksum(m
, ahdat
, len
, algo
, sav
)
1184 const struct ah_algorithm
*algo
;
1185 struct secasvar
*sav
;
1189 size_t advancewidth
;
1190 struct ah_algorithm_state algos
;
1191 u_char sumbuf
[AH_MAXSUMSIZE
];
1194 struct mbuf
*n
= NULL
;
1196 if ((m
->m_flags
& M_PKTHDR
) == 0)
1200 hdrtype
= -1; /*dummy, it is called IPPROTO_IP*/
1204 error
= (algo
->init
)(&algos
, sav
);
1208 advancewidth
= 0; /*safety*/
1213 case -1: /*first one only*/
1216 * copy ip hdr, modify to fit the AH checksum rule,
1217 * then take a checksum.
1222 m_copydata(m
, off
, sizeof(iphdr
), (caddr_t
)&iphdr
);
1224 hlen
= IP_VHL_HL(iphdr
.ip_vhl
) << 2;
1226 hlen
= iphdr
.ip_hl
<< 2;
1229 iphdr
.ip_sum
= htons(0);
1230 if (ip4_ah_cleartos
)
1232 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1233 (algo
->update
)(&algos
, (caddr_t
)&iphdr
, sizeof(struct ip
));
1235 if (hlen
!= sizeof(struct ip
)) {
1239 if (hlen
> MCLBYTES
) {
1243 MGET(n
, M_DONTWAIT
, MT_DATA
);
1244 if (n
&& hlen
> MLEN
) {
1245 MCLGET(n
, M_DONTWAIT
);
1246 if ((n
->m_flags
& M_EXT
) == 0) {
1255 m_copydata(m
, off
, hlen
, mtod(n
, caddr_t
));
1258 * IP options processing.
1259 * See RFC2402 appendix A.
1261 p
= mtod(n
, u_char
*);
1262 i
= sizeof(struct ip
);
1264 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1265 ipseclog((LOG_ERR
, "ah4_calccksum: "
1266 "invalid IP option\n"));
1270 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1271 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1272 i
+ IPOPT_OLEN
< hlen
)
1276 "ah4_calccksum: invalid IP option "
1278 p
[i
+ IPOPT_OPTVAL
]));
1284 switch (p
[i
+ IPOPT_OPTVAL
]) {
1290 case IPOPT_SECURITY
: /* 0x82 */
1291 case 0x85: /* Extended security */
1292 case 0x86: /* Commercial security */
1293 case 0x94: /* Router alert */
1294 case 0x95: /* RFC1770 */
1295 l
= p
[i
+ IPOPT_OLEN
];
1301 l
= p
[i
+ IPOPT_OLEN
];
1307 if (l
< 1 || hlen
- i
< l
) {
1310 "ah4_calccksum: invalid IP option "
1311 "(type=%02x len=%02x)\n",
1312 p
[i
+ IPOPT_OPTVAL
],
1313 p
[i
+ IPOPT_OLEN
]));
1319 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
)
1323 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1324 (algo
->update
)(&algos
, p
, hlen
- sizeof(struct ip
));
1330 hdrtype
= (iphdr
.ip_p
) & 0xff;
1331 advancewidth
= hlen
;
1342 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1343 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1345 : sizeof(struct newah
);
1346 siz
= (*algo
->sumsiz
)(sav
);
1347 totlen
= (ah
.ah_len
+ 2) << 2;
1350 * special treatment is necessary for the first one, not others
1353 if (totlen
> m
->m_pkthdr
.len
- off
||
1354 totlen
> MCLBYTES
) {
1358 MGET(n
, M_DONTWAIT
, MT_DATA
);
1359 if (n
&& totlen
> MLEN
) {
1360 MCLGET(n
, M_DONTWAIT
);
1361 if ((n
->m_flags
& M_EXT
) == 0) {
1370 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1372 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1373 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1377 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1380 hdrtype
= ah
.ah_nxt
;
1381 advancewidth
= totlen
;
1386 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1387 advancewidth
= m
->m_pkthdr
.len
- off
;
1391 off
+= advancewidth
;
1392 if (off
< m
->m_pkthdr
.len
)
1395 if (len
< (*algo
->sumsiz
)(sav
)) {
1400 (algo
->result
)(&algos
, &sumbuf
[0]);
1401 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1416 * Go generate the checksum. This function won't modify the mbuf chain
1419 * NOTE: the function does not free mbuf on failure.
1420 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1423 ah6_calccksum(m
, ahdat
, len
, algo
, sav
)
1427 const struct ah_algorithm
*algo
;
1428 struct secasvar
*sav
;
1432 struct mbuf
*n
= NULL
;
1435 struct ah_algorithm_state algos
;
1436 u_char sumbuf
[AH_MAXSUMSIZE
];
1438 if ((m
->m_flags
& M_PKTHDR
) == 0)
1441 error
= (algo
->init
)(&algos
, sav
);
1446 proto
= IPPROTO_IPV6
;
1451 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1453 newoff
= m
->m_pkthdr
.len
;
1454 else if (newoff
<= off
) {
1462 * special treatment is necessary for the first one, not others
1465 struct ip6_hdr ip6copy
;
1467 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1472 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1474 ip6copy
.ip6_flow
= 0;
1475 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1476 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1477 ip6copy
.ip6_hlim
= 0;
1478 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
))
1479 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1480 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
))
1481 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1482 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1483 sizeof(struct ip6_hdr
));
1485 newoff
= m
->m_pkthdr
.len
;
1486 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1496 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1498 : sizeof(struct newah
);
1499 siz
= (*algo
->sumsiz
)(sav
);
1502 * special treatment is necessary for the first one, not others
1505 if (newoff
- off
> MCLBYTES
) {
1509 MGET(n
, M_DONTWAIT
, MT_DATA
);
1510 if (n
&& newoff
- off
> MLEN
) {
1511 MCLGET(n
, M_DONTWAIT
);
1512 if ((n
->m_flags
& M_EXT
) == 0) {
1521 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1522 n
->m_len
= newoff
- off
;
1523 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1524 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1528 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1533 case IPPROTO_HOPOPTS
:
1534 case IPPROTO_DSTOPTS
:
1536 struct ip6_ext
*ip6e
;
1538 u_int8_t
*p
, *optend
, *optp
;
1540 if (newoff
- off
> MCLBYTES
) {
1544 MGET(n
, M_DONTWAIT
, MT_DATA
);
1545 if (n
&& newoff
- off
> MLEN
) {
1546 MCLGET(n
, M_DONTWAIT
);
1547 if ((n
->m_flags
& M_EXT
) == 0) {
1556 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1557 n
->m_len
= newoff
- off
;
1559 ip6e
= mtod(n
, struct ip6_ext
*);
1560 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1561 if (newoff
- off
< hdrlen
) {
1567 p
= mtod(n
, u_int8_t
*);
1568 optend
= p
+ hdrlen
;
1571 * ICV calculation for the options header including all
1572 * options. This part is a little tricky since there are
1573 * two type of options; mutable and immutable. We try to
1574 * null-out mutable ones here.
1577 while (optp
< optend
) {
1578 if (optp
[0] == IP6OPT_PAD1
)
1581 if (optp
+ 2 > optend
) {
1587 optlen
= optp
[1] + 2;
1589 if (optp
[0] & IP6OPT_MUTABLE
)
1590 bzero(optp
+ 2, optlen
- 2);
1596 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1602 case IPPROTO_ROUTING
:
1604 * For an input packet, we can just calculate `as is'.
1605 * For an output packet, we assume ip6_output have already
1606 * made packet how it will be received at the final
1612 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1616 if (newoff
< m
->m_pkthdr
.len
) {
1622 if (len
< (*algo
->sumsiz
)(sav
)) {
1627 (algo
->result
)(&algos
, &sumbuf
[0]);
1628 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));