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>
81 #include <libkern/crypto/md5.h>
82 #include <libkern/crypto/sha1.h>
83 #include <crypto/sha2/sha2.h>
85 #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_none_mature(struct secasvar
*);
92 static int ah_none_init(struct ah_algorithm_state
*, struct secasvar
*);
93 static void ah_none_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
94 static void ah_none_result(struct ah_algorithm_state
*, caddr_t
);
95 static int ah_keyed_md5_mature(struct secasvar
*);
96 static int ah_keyed_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
97 static void ah_keyed_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
98 static void ah_keyed_md5_result(struct ah_algorithm_state
*, caddr_t
);
99 static int ah_keyed_sha1_mature(struct secasvar
*);
100 static int ah_keyed_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
101 static void ah_keyed_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
102 static void ah_keyed_sha1_result(struct ah_algorithm_state
*, caddr_t
);
103 static int ah_hmac_md5_mature(struct secasvar
*);
104 static int ah_hmac_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
105 static void ah_hmac_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
106 static void ah_hmac_md5_result(struct ah_algorithm_state
*, caddr_t
);
107 static int ah_hmac_sha1_mature(struct secasvar
*);
108 static int ah_hmac_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
109 static void ah_hmac_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
110 static void ah_hmac_sha1_result(struct ah_algorithm_state
*, caddr_t
);
112 static int ah_hmac_sha2_256_mature(struct secasvar
*);
113 static int ah_hmac_sha2_256_init(struct ah_algorithm_state
*,
115 static void ah_hmac_sha2_256_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
116 static void ah_hmac_sha2_256_result(struct ah_algorithm_state
*, caddr_t
);
117 static int ah_hmac_sha2_384_mature(struct secasvar
*);
118 static int ah_hmac_sha2_384_init(struct ah_algorithm_state
*,
120 static void ah_hmac_sha2_384_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
121 static void ah_hmac_sha2_384_result(struct ah_algorithm_state
*, caddr_t
);
122 static int ah_hmac_sha2_512_mature(struct secasvar
*);
123 static int ah_hmac_sha2_512_init(struct ah_algorithm_state
*,
125 static void ah_hmac_sha2_512_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
126 static void ah_hmac_sha2_512_result(struct ah_algorithm_state
*, caddr_t
);
127 #endif /* ALLCRYPTO */
129 static void ah_update_mbuf(struct mbuf
*, int, int,
130 const struct ah_algorithm
*, struct ah_algorithm_state
*);
132 const struct ah_algorithm
*
133 ah_algorithm_lookup(idx
)
136 /* checksum algorithms */
137 static struct ah_algorithm hmac_md5
=
138 { ah_sumsiz_1216
, ah_hmac_md5_mature
, 128, 128, "hmac-md5",
139 ah_hmac_md5_init
, ah_hmac_md5_loop
,
140 ah_hmac_md5_result
, };
141 static struct ah_algorithm keyed_md5
=
142 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
143 ah_keyed_md5_init
, ah_keyed_md5_loop
,
144 ah_keyed_md5_result
, };
145 static struct ah_algorithm hmac_sha1
=
146 { ah_sumsiz_1216
, ah_hmac_sha1_mature
, 160, 160, "hmac-sha1",
147 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
148 ah_hmac_sha1_result
, };
149 static struct ah_algorithm keyed_sha1
=
150 { ah_sumsiz_1216
, ah_keyed_sha1_mature
, 160, 160, "keyed-sha1",
151 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
152 ah_keyed_sha1_result
, };
153 static struct ah_algorithm ah_none
=
154 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
155 ah_none_init
, ah_none_loop
, ah_none_result
, };
157 static struct ah_algorithm hmac_sha2_256
=
158 { ah_sumsiz_1216
, ah_hmac_sha2_256_mature
, 256, 256,
160 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
161 ah_hmac_sha2_256_result
, };
162 static struct ah_algorithm hmac_sha2_384
=
163 { ah_sumsiz_1216
, ah_hmac_sha2_384_mature
, 384, 384,
165 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
166 ah_hmac_sha2_384_result
, };
167 static struct ah_algorithm hmac_sha2_512
=
168 { ah_sumsiz_1216
, ah_hmac_sha2_512_mature
, 512, 512,
170 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
171 ah_hmac_sha2_512_result
, };
172 #endif /* ALLCRYPTO */
175 case SADB_AALG_MD5HMAC
:
177 case SADB_AALG_SHA1HMAC
:
179 case SADB_X_AALG_MD5
:
181 case SADB_X_AALG_SHA
:
183 case SADB_X_AALG_NULL
:
186 case SADB_X_AALG_SHA2_256
:
187 return &hmac_sha2_256
;
188 case SADB_X_AALG_SHA2_384
:
189 return &hmac_sha2_384
;
190 case SADB_X_AALG_SHA2_512
:
191 return &hmac_sha2_512
;
192 #endif /* ALLCRYPTO */
201 struct secasvar
*sav
;
205 if (sav
->flags
& SADB_X_EXT_OLD
)
213 struct secasvar
*sav
;
222 struct secasvar
*sav
;
224 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
226 "ah_none_mature: protocol and algorithm mismatch.\n"));
234 struct ah_algorithm_state
*state
,
235 __unused
struct secasvar
*sav
)
243 __unused
struct ah_algorithm_state
*state
,
244 __unused caddr_t addr
,
251 __unused
struct ah_algorithm_state
*state
,
252 __unused caddr_t addr
)
258 __unused
struct secasvar
*sav
)
260 /* anything is okay */
265 ah_keyed_md5_init(state
, sav
)
266 struct ah_algorithm_state
*state
;
267 struct secasvar
*sav
;
274 panic("ah_keyed_md5_init: what?");
277 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
278 if (state
->foo
== NULL
)
281 MD5Init((MD5_CTX
*)state
->foo
);
283 MD5Update((MD5_CTX
*)state
->foo
,
284 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
285 (u_int
)_KEYLEN(state
->sav
->key_auth
));
289 * We cannot simply use md5_pad() since the function
290 * won't update the total length.
292 if (_KEYLEN(state
->sav
->key_auth
) < 56)
293 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
295 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
296 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
300 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
303 bzero(buf
, sizeof(buf
));
304 while (sizeof(buf
) < padlen
) {
305 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
306 padlen
-= sizeof(buf
);
309 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
312 buf
[0] = (keybitlen
>> 0) & 0xff;
313 buf
[1] = (keybitlen
>> 8) & 0xff;
314 buf
[2] = (keybitlen
>> 16) & 0xff;
315 buf
[3] = (keybitlen
>> 24) & 0xff;
316 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
323 ah_keyed_md5_loop(state
, addr
, len
)
324 struct ah_algorithm_state
*state
;
329 panic("ah_keyed_md5_loop: what?");
331 MD5Update((MD5_CTX
*)state
->foo
, addr
, len
);
335 ah_keyed_md5_result(state
, addr
)
336 struct ah_algorithm_state
*state
;
342 panic("ah_keyed_md5_result: what?");
345 MD5Update((MD5_CTX
*)state
->foo
,
346 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
347 (u_int
)_KEYLEN(state
->sav
->key_auth
));
349 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
350 FREE(state
->foo
, M_TEMP
);
351 bcopy(&digest
[0], (void *)addr
, sizeof(digest
));
355 ah_keyed_sha1_mature(sav
)
356 struct secasvar
*sav
;
358 const struct ah_algorithm
*algo
;
360 if (!sav
->key_auth
) {
361 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
365 algo
= ah_algorithm_lookup(sav
->alg_auth
);
367 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
371 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
372 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
374 "ah_keyed_sha1_mature: invalid key length %d.\n",
375 sav
->key_auth
->sadb_key_bits
));
383 ah_keyed_sha1_init(state
, sav
)
384 struct ah_algorithm_state
*state
;
385 struct secasvar
*sav
;
393 panic("ah_keyed_sha1_init: what?");
396 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_NOWAIT
);
400 ctxt
= (SHA1_CTX
*)state
->foo
;
404 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
405 (u_int
)_KEYLEN(state
->sav
->key_auth
));
410 if (_KEYLEN(state
->sav
->key_auth
) < 56)
411 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
413 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
414 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
418 SHA1Update(ctxt
, &buf
[0], 1);
421 bzero(buf
, sizeof(buf
));
422 while (sizeof(buf
) < padlen
) {
423 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
424 padlen
-= sizeof(buf
);
427 SHA1Update(ctxt
, &buf
[0], padlen
);
430 buf
[0] = (keybitlen
>> 0) & 0xff;
431 buf
[1] = (keybitlen
>> 8) & 0xff;
432 buf
[2] = (keybitlen
>> 16) & 0xff;
433 buf
[3] = (keybitlen
>> 24) & 0xff;
434 SHA1Update(ctxt
, buf
, 8);
441 ah_keyed_sha1_loop(state
, addr
, len
)
442 struct ah_algorithm_state
*state
;
448 if (!state
|| !state
->foo
)
449 panic("ah_keyed_sha1_loop: what?");
450 ctxt
= (SHA1_CTX
*)state
->foo
;
452 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
456 ah_keyed_sha1_result(state
, addr
)
457 struct ah_algorithm_state
*state
;
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((caddr_t
)&digest
[0], ctxt
);
472 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
474 FREE(state
->foo
, M_TEMP
);
478 ah_hmac_md5_mature(sav
)
479 struct secasvar
*sav
;
481 const struct ah_algorithm
*algo
;
483 if (!sav
->key_auth
) {
484 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
488 algo
= ah_algorithm_lookup(sav
->alg_auth
);
490 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
494 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
495 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
497 "ah_hmac_md5_mature: invalid key length %d.\n",
498 sav
->key_auth
->sadb_key_bits
));
506 ah_hmac_md5_init(state
, sav
)
507 struct ah_algorithm_state
*state
;
508 struct secasvar
*sav
;
519 panic("ah_hmac_md5_init: what?");
522 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
526 ipad
= (u_char
*)state
->foo
;
527 opad
= (u_char
*)(ipad
+ 64);
528 ctxt
= (MD5_CTX
*)(opad
+ 64);
530 /* compress the key if necessery */
531 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
533 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
534 _KEYLEN(state
->sav
->key_auth
));
535 MD5Final(&tk
[0], ctxt
);
539 key
= _KEYBUF(state
->sav
->key_auth
);
540 keylen
= _KEYLEN(state
->sav
->key_auth
);
545 bcopy(key
, ipad
, keylen
);
546 bcopy(key
, opad
, keylen
);
547 for (i
= 0; i
< 64; i
++) {
553 MD5Update(ctxt
, ipad
, 64);
559 ah_hmac_md5_loop(state
, addr
, len
)
560 struct ah_algorithm_state
*state
;
566 if (!state
|| !state
->foo
)
567 panic("ah_hmac_md5_loop: what?");
568 ctxt
= (MD5_CTX
*)(((caddr_t
)state
->foo
) + 128);
569 MD5Update(ctxt
, addr
, len
);
573 ah_hmac_md5_result(state
, addr
)
574 struct ah_algorithm_state
*state
;
582 if (!state
|| !state
->foo
)
583 panic("ah_hmac_md5_result: what?");
585 ipad
= (u_char
*)state
->foo
;
586 opad
= (u_char
*)(ipad
+ 64);
587 ctxt
= (MD5_CTX
*)(opad
+ 64);
589 MD5Final(&digest
[0], ctxt
);
592 MD5Update(ctxt
, opad
, 64);
593 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
594 MD5Final(&digest
[0], ctxt
);
596 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
598 FREE(state
->foo
, M_TEMP
);
602 ah_hmac_sha1_mature(sav
)
603 struct secasvar
*sav
;
605 const struct ah_algorithm
*algo
;
607 if (!sav
->key_auth
) {
608 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
612 algo
= ah_algorithm_lookup(sav
->alg_auth
);
614 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
618 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
619 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
621 "ah_hmac_sha1_mature: invalid key length %d.\n",
622 sav
->key_auth
->sadb_key_bits
));
630 ah_hmac_sha1_init(state
, sav
)
631 struct ah_algorithm_state
*state
;
632 struct secasvar
*sav
;
637 u_char tk
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
643 panic("ah_hmac_sha1_init: what?");
646 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
651 ipad
= (u_char
*)state
->foo
;
652 opad
= (u_char
*)(ipad
+ 64);
653 ctxt
= (SHA1_CTX
*)(opad
+ 64);
655 /* compress the key if necessery */
656 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
658 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
659 _KEYLEN(state
->sav
->key_auth
));
660 SHA1Final(&tk
[0], ctxt
);
662 keylen
= SHA1_RESULTLEN
;
664 key
= _KEYBUF(state
->sav
->key_auth
);
665 keylen
= _KEYLEN(state
->sav
->key_auth
);
670 bcopy(key
, ipad
, keylen
);
671 bcopy(key
, opad
, keylen
);
672 for (i
= 0; i
< 64; i
++) {
678 SHA1Update(ctxt
, ipad
, 64);
684 ah_hmac_sha1_loop(state
, addr
, len
)
685 struct ah_algorithm_state
*state
;
691 if (!state
|| !state
->foo
)
692 panic("ah_hmac_sha1_loop: what?");
694 ctxt
= (SHA1_CTX
*)(((u_char
*)state
->foo
) + 128);
695 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
699 ah_hmac_sha1_result(state
, addr
)
700 struct ah_algorithm_state
*state
;
703 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
708 if (!state
|| !state
->foo
)
709 panic("ah_hmac_sha1_result: what?");
711 ipad
= (u_char
*)state
->foo
;
712 opad
= (u_char
*)(ipad
+ 64);
713 ctxt
= (SHA1_CTX
*)(opad
+ 64);
715 SHA1Final((caddr_t
)&digest
[0], ctxt
);
718 SHA1Update(ctxt
, opad
, 64);
719 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
720 SHA1Final((caddr_t
)&digest
[0], ctxt
);
722 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
724 FREE(state
->foo
, M_TEMP
);
729 ah_hmac_sha2_256_mature(sav
)
730 struct secasvar
*sav
;
732 const struct ah_algorithm
*algo
;
734 if (!sav
->key_auth
) {
736 "ah_hmac_sha2_256_mature: no key is given.\n"));
740 algo
= ah_algorithm_lookup(sav
->alg_auth
);
743 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
747 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
748 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
750 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
751 sav
->key_auth
->sadb_key_bits
));
759 ah_hmac_sha2_256_init(state
, sav
)
760 struct ah_algorithm_state
*state
;
761 struct secasvar
*sav
;
766 u_char tk
[SHA256_DIGEST_LENGTH
];
772 panic("ah_hmac_sha2_256_init: what?");
775 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
780 ipad
= (u_char
*)state
->foo
;
781 opad
= (u_char
*)(ipad
+ 64);
782 ctxt
= (SHA256_CTX
*)(opad
+ 64);
784 /* compress the key if necessery */
785 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
786 bzero(tk
, sizeof(tk
));
787 bzero(ctxt
, sizeof(*ctxt
));
789 SHA256_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
790 _KEYLEN(state
->sav
->key_auth
));
791 SHA256_Final(&tk
[0], ctxt
);
793 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
795 key
= _KEYBUF(state
->sav
->key_auth
);
796 keylen
= _KEYLEN(state
->sav
->key_auth
);
801 bcopy(key
, ipad
, keylen
);
802 bcopy(key
, opad
, keylen
);
803 for (i
= 0; i
< 64; i
++) {
808 bzero(ctxt
, sizeof(*ctxt
));
810 SHA256_Update(ctxt
, ipad
, 64);
816 ah_hmac_sha2_256_loop(state
, addr
, len
)
817 struct ah_algorithm_state
*state
;
823 if (!state
|| !state
->foo
)
824 panic("ah_hmac_sha2_256_loop: what?");
826 ctxt
= (SHA256_CTX
*)(((u_char
*)state
->foo
) + 128);
827 SHA256_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
831 ah_hmac_sha2_256_result(state
, addr
)
832 struct ah_algorithm_state
*state
;
835 u_char digest
[SHA256_DIGEST_LENGTH
];
840 if (!state
|| !state
->foo
)
841 panic("ah_hmac_sha2_256_result: what?");
843 ipad
= (u_char
*)state
->foo
;
844 opad
= (u_char
*)(ipad
+ 64);
845 ctxt
= (SHA256_CTX
*)(opad
+ 64);
847 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
849 bzero(ctxt
, sizeof(*ctxt
));
851 SHA256_Update(ctxt
, opad
, 64);
852 SHA256_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
853 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
855 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
857 FREE(state
->foo
, M_TEMP
);
861 ah_hmac_sha2_384_mature(sav
)
862 struct secasvar
*sav
;
864 const struct ah_algorithm
*algo
;
866 if (!sav
->key_auth
) {
868 "ah_hmac_sha2_384_mature: no key is given.\n"));
872 algo
= ah_algorithm_lookup(sav
->alg_auth
);
875 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
879 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
880 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
882 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
883 sav
->key_auth
->sadb_key_bits
));
891 ah_hmac_sha2_384_init(state
, sav
)
892 struct ah_algorithm_state
*state
;
893 struct secasvar
*sav
;
898 u_char tk
[SHA384_DIGEST_LENGTH
];
904 panic("ah_hmac_sha2_384_init: what?");
907 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA384_CTX
),
911 bzero(state
->foo
, 64 + 64 + sizeof(SHA384_CTX
));
913 ipad
= (u_char
*)state
->foo
;
914 opad
= (u_char
*)(ipad
+ 64);
915 ctxt
= (SHA384_CTX
*)(opad
+ 64);
917 /* compress the key if necessery */
918 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
919 bzero(tk
, sizeof(tk
));
920 bzero(ctxt
, sizeof(*ctxt
));
922 SHA384_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
923 _KEYLEN(state
->sav
->key_auth
));
924 SHA384_Final(&tk
[0], ctxt
);
926 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
928 key
= _KEYBUF(state
->sav
->key_auth
);
929 keylen
= _KEYLEN(state
->sav
->key_auth
);
934 bcopy(key
, ipad
, keylen
);
935 bcopy(key
, opad
, keylen
);
936 for (i
= 0; i
< 64; i
++) {
941 bzero(ctxt
, sizeof(*ctxt
));
943 SHA384_Update(ctxt
, ipad
, 64);
949 ah_hmac_sha2_384_loop(state
, addr
, len
)
950 struct ah_algorithm_state
*state
;
956 if (!state
|| !state
->foo
)
957 panic("ah_hmac_sha2_384_loop: what?");
959 ctxt
= (SHA384_CTX
*)(((u_char
*)state
->foo
) + 128);
960 SHA384_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
964 ah_hmac_sha2_384_result(state
, addr
)
965 struct ah_algorithm_state
*state
;
968 u_char digest
[SHA384_DIGEST_LENGTH
];
973 if (!state
|| !state
->foo
)
974 panic("ah_hmac_sha2_384_result: what?");
976 ipad
= (u_char
*)state
->foo
;
977 opad
= (u_char
*)(ipad
+ 64);
978 ctxt
= (SHA384_CTX
*)(opad
+ 64);
980 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
982 bzero(ctxt
, sizeof(*ctxt
));
984 SHA384_Update(ctxt
, opad
, 64);
985 SHA384_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
986 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
988 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
990 FREE(state
->foo
, M_TEMP
);
994 ah_hmac_sha2_512_mature(sav
)
995 struct secasvar
*sav
;
997 const struct ah_algorithm
*algo
;
999 if (!sav
->key_auth
) {
1001 "ah_hmac_sha2_512_mature: no key is given.\n"));
1005 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1008 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1012 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1013 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1015 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1016 sav
->key_auth
->sadb_key_bits
));
1024 ah_hmac_sha2_512_init(state
, sav
)
1025 struct ah_algorithm_state
*state
;
1026 struct secasvar
*sav
;
1031 u_char tk
[SHA512_DIGEST_LENGTH
];
1037 panic("ah_hmac_sha2_512_init: what?");
1040 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA512_CTX
),
1044 bzero(state
->foo
, 64 + 64 + sizeof(SHA512_CTX
));
1046 ipad
= (u_char
*)state
->foo
;
1047 opad
= (u_char
*)(ipad
+ 64);
1048 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1050 /* compress the key if necessery */
1051 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
1052 bzero(tk
, sizeof(tk
));
1053 bzero(ctxt
, sizeof(*ctxt
));
1055 SHA512_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
1056 _KEYLEN(state
->sav
->key_auth
));
1057 SHA512_Final(&tk
[0], ctxt
);
1059 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
1061 key
= _KEYBUF(state
->sav
->key_auth
);
1062 keylen
= _KEYLEN(state
->sav
->key_auth
);
1067 bcopy(key
, ipad
, keylen
);
1068 bcopy(key
, opad
, keylen
);
1069 for (i
= 0; i
< 64; i
++) {
1074 bzero(ctxt
, sizeof(*ctxt
));
1076 SHA512_Update(ctxt
, ipad
, 64);
1082 ah_hmac_sha2_512_loop(state
, addr
, len
)
1083 struct ah_algorithm_state
*state
;
1089 if (!state
|| !state
->foo
)
1090 panic("ah_hmac_sha2_512_loop: what?");
1092 ctxt
= (SHA512_CTX
*)(((u_char
*)state
->foo
) + 128);
1093 SHA512_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
1097 ah_hmac_sha2_512_result(state
, addr
)
1098 struct ah_algorithm_state
*state
;
1101 u_char digest
[SHA512_DIGEST_LENGTH
];
1106 if (!state
|| !state
->foo
)
1107 panic("ah_hmac_sha2_512_result: what?");
1109 ipad
= (u_char
*)state
->foo
;
1110 opad
= (u_char
*)(ipad
+ 64);
1111 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1113 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1115 bzero(ctxt
, sizeof(*ctxt
));
1117 SHA512_Update(ctxt
, opad
, 64);
1118 SHA512_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
1119 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1121 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
1123 FREE(state
->foo
, M_TEMP
);
1125 #endif /* ALLCRYPTO */
1127 /*------------------------------------------------------------*/
1130 * go generate the checksum.
1133 ah_update_mbuf(m
, off
, len
, algo
, algos
)
1137 const struct ah_algorithm
*algo
;
1138 struct ah_algorithm_state
*algos
;
1143 /* easy case first */
1144 if (off
+ len
<= m
->m_len
) {
1145 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1149 for (n
= m
; n
; n
= n
->m_next
) {
1157 panic("ah_update_mbuf: wrong offset specified");
1159 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1162 if (n
->m_len
- off
< len
)
1163 tlen
= n
->m_len
- off
;
1167 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1176 * Go generate the checksum. This function won't modify the mbuf chain
1179 * NOTE: the function does not free mbuf on failure.
1180 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1183 ah4_calccksum(m
, ahdat
, len
, algo
, sav
)
1187 const struct ah_algorithm
*algo
;
1188 struct secasvar
*sav
;
1192 size_t advancewidth
;
1193 struct ah_algorithm_state algos
;
1194 u_char sumbuf
[AH_MAXSUMSIZE
];
1197 struct mbuf
*n
= NULL
;
1199 if ((m
->m_flags
& M_PKTHDR
) == 0)
1203 hdrtype
= -1; /*dummy, it is called IPPROTO_IP*/
1207 error
= (algo
->init
)(&algos
, sav
);
1211 advancewidth
= 0; /*safety*/
1216 case -1: /*first one only*/
1219 * copy ip hdr, modify to fit the AH checksum rule,
1220 * then take a checksum.
1225 m_copydata(m
, off
, sizeof(iphdr
), (caddr_t
)&iphdr
);
1227 hlen
= IP_VHL_HL(iphdr
.ip_vhl
) << 2;
1229 hlen
= iphdr
.ip_hl
<< 2;
1232 iphdr
.ip_sum
= htons(0);
1233 if (ip4_ah_cleartos
)
1235 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1236 (algo
->update
)(&algos
, (caddr_t
)&iphdr
, sizeof(struct ip
));
1238 if (hlen
!= sizeof(struct ip
)) {
1242 if (hlen
> MCLBYTES
) {
1246 MGET(n
, M_DONTWAIT
, MT_DATA
);
1247 if (n
&& hlen
> MLEN
) {
1248 MCLGET(n
, M_DONTWAIT
);
1249 if ((n
->m_flags
& M_EXT
) == 0) {
1258 m_copydata(m
, off
, hlen
, mtod(n
, caddr_t
));
1261 * IP options processing.
1262 * See RFC2402 appendix A.
1264 p
= mtod(n
, u_char
*);
1265 i
= sizeof(struct ip
);
1267 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1268 ipseclog((LOG_ERR
, "ah4_calccksum: "
1269 "invalid IP option\n"));
1273 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1274 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1275 i
+ IPOPT_OLEN
< hlen
)
1279 "ah4_calccksum: invalid IP option "
1281 p
[i
+ IPOPT_OPTVAL
]));
1287 switch (p
[i
+ IPOPT_OPTVAL
]) {
1293 case IPOPT_SECURITY
: /* 0x82 */
1294 case 0x85: /* Extended security */
1295 case 0x86: /* Commercial security */
1296 case 0x94: /* Router alert */
1297 case 0x95: /* RFC1770 */
1298 l
= p
[i
+ IPOPT_OLEN
];
1304 l
= p
[i
+ IPOPT_OLEN
];
1310 if (l
< 1 || hlen
- i
< l
) {
1313 "ah4_calccksum: invalid IP option "
1314 "(type=%02x len=%02x)\n",
1315 p
[i
+ IPOPT_OPTVAL
],
1316 p
[i
+ IPOPT_OLEN
]));
1322 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
)
1326 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1327 (algo
->update
)(&algos
, p
, hlen
- sizeof(struct ip
));
1333 hdrtype
= (iphdr
.ip_p
) & 0xff;
1334 advancewidth
= hlen
;
1345 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1346 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1348 : sizeof(struct newah
);
1349 siz
= (*algo
->sumsiz
)(sav
);
1350 totlen
= (ah
.ah_len
+ 2) << 2;
1353 * special treatment is necessary for the first one, not others
1356 if (totlen
> m
->m_pkthdr
.len
- off
||
1357 totlen
> MCLBYTES
) {
1361 MGET(n
, M_DONTWAIT
, MT_DATA
);
1362 if (n
&& totlen
> MLEN
) {
1363 MCLGET(n
, M_DONTWAIT
);
1364 if ((n
->m_flags
& M_EXT
) == 0) {
1373 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1375 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1376 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1380 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1383 hdrtype
= ah
.ah_nxt
;
1384 advancewidth
= totlen
;
1389 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1390 advancewidth
= m
->m_pkthdr
.len
- off
;
1394 off
+= advancewidth
;
1395 if (off
< m
->m_pkthdr
.len
)
1398 if (len
< (*algo
->sumsiz
)(sav
)) {
1403 (algo
->result
)(&algos
, &sumbuf
[0]);
1404 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1419 * Go generate the checksum. This function won't modify the mbuf chain
1422 * NOTE: the function does not free mbuf on failure.
1423 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1426 ah6_calccksum(m
, ahdat
, len
, algo
, sav
)
1430 const struct ah_algorithm
*algo
;
1431 struct secasvar
*sav
;
1435 struct mbuf
*n
= NULL
;
1438 struct ah_algorithm_state algos
;
1439 u_char sumbuf
[AH_MAXSUMSIZE
];
1441 if ((m
->m_flags
& M_PKTHDR
) == 0)
1444 error
= (algo
->init
)(&algos
, sav
);
1449 proto
= IPPROTO_IPV6
;
1454 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1456 newoff
= m
->m_pkthdr
.len
;
1457 else if (newoff
<= off
) {
1465 * special treatment is necessary for the first one, not others
1468 struct ip6_hdr ip6copy
;
1470 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1475 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1477 ip6copy
.ip6_flow
= 0;
1478 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1479 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1480 ip6copy
.ip6_hlim
= 0;
1481 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
))
1482 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1483 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
))
1484 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1485 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1486 sizeof(struct ip6_hdr
));
1488 newoff
= m
->m_pkthdr
.len
;
1489 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1499 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1501 : sizeof(struct newah
);
1502 siz
= (*algo
->sumsiz
)(sav
);
1505 * special treatment is necessary for the first one, not others
1508 if (newoff
- off
> MCLBYTES
) {
1512 MGET(n
, M_DONTWAIT
, MT_DATA
);
1513 if (n
&& newoff
- off
> MLEN
) {
1514 MCLGET(n
, M_DONTWAIT
);
1515 if ((n
->m_flags
& M_EXT
) == 0) {
1524 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1525 n
->m_len
= newoff
- off
;
1526 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1527 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1531 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1536 case IPPROTO_HOPOPTS
:
1537 case IPPROTO_DSTOPTS
:
1539 struct ip6_ext
*ip6e
;
1541 u_int8_t
*p
, *optend
, *optp
;
1543 if (newoff
- off
> MCLBYTES
) {
1547 MGET(n
, M_DONTWAIT
, MT_DATA
);
1548 if (n
&& newoff
- off
> MLEN
) {
1549 MCLGET(n
, M_DONTWAIT
);
1550 if ((n
->m_flags
& M_EXT
) == 0) {
1559 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1560 n
->m_len
= newoff
- off
;
1562 ip6e
= mtod(n
, struct ip6_ext
*);
1563 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1564 if (newoff
- off
< hdrlen
) {
1570 p
= mtod(n
, u_int8_t
*);
1571 optend
= p
+ hdrlen
;
1574 * ICV calculation for the options header including all
1575 * options. This part is a little tricky since there are
1576 * two type of options; mutable and immutable. We try to
1577 * null-out mutable ones here.
1580 while (optp
< optend
) {
1581 if (optp
[0] == IP6OPT_PAD1
)
1584 if (optp
+ 2 > optend
) {
1590 optlen
= optp
[1] + 2;
1592 if (optp
[0] & IP6OPT_MUTABLE
)
1593 bzero(optp
+ 2, optlen
- 2);
1599 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1605 case IPPROTO_ROUTING
:
1607 * For an input packet, we can just calculate `as is'.
1608 * For an output packet, we assume ip6_output have already
1609 * made packet how it will be received at the final
1615 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1619 if (newoff
< m
->m_pkthdr
.len
) {
1625 if (len
< (*algo
->sumsiz
)(sav
)) {
1630 (algo
->result
)(&algos
, &sumbuf
[0]);
1631 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));