2 * Copyright (c) 2008-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/ah_core.c,v 1.2.2.4 2001/07/03 11:01:49 ume Exp $ */
30 /* $KAME: ah_core.c,v 1.44 2001/03/12 11:24:39 itojun Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * RFC1826/2402 authentication header.
65 /* TODO: have shared routines for hmac-* algorithms */
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/malloc.h>
71 #include <sys/domain.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
77 #include <sys/syslog.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_var.h>
87 #include <netinet/ip6.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet/icmp6.h>
91 #include <netinet6/ipsec.h>
92 #include <netinet6/ipsec6.h>
93 #include <netinet6/ah.h>
94 #include <netinet6/ah6.h>
96 #include <netinet6/esp.h>
97 #include <netinet6/esp6.h>
99 #include <net/pfkeyv2.h>
100 #include <netkey/keydb.h>
101 #include <libkern/crypto/md5.h>
102 #include <libkern/crypto/sha1.h>
103 #include <libkern/crypto/sha2.h>
105 #include <net/net_osdep.h>
109 static int ah_sumsiz_1216(struct secasvar
*);
110 static int ah_sumsiz_zero(struct secasvar
*);
111 static int ah_none_mature(struct secasvar
*);
112 static int ah_none_init(struct ah_algorithm_state
*, struct secasvar
*);
113 static void ah_none_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
114 static void ah_none_result(struct ah_algorithm_state
*, caddr_t
, size_t);
115 static int ah_keyed_md5_mature(struct secasvar
*);
116 static int ah_keyed_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
117 static void ah_keyed_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
118 static void ah_keyed_md5_result(struct ah_algorithm_state
*, caddr_t
, size_t);
119 static int ah_keyed_sha1_mature(struct secasvar
*);
120 static int ah_keyed_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
121 static void ah_keyed_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
122 static void ah_keyed_sha1_result(struct ah_algorithm_state
*, caddr_t
, size_t);
123 static int ah_hmac_md5_mature(struct secasvar
*);
124 static int ah_hmac_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
125 static void ah_hmac_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
126 static void ah_hmac_md5_result(struct ah_algorithm_state
*, caddr_t
, size_t);
127 static int ah_hmac_sha1_mature(struct secasvar
*);
128 static int ah_hmac_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
129 static void ah_hmac_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
130 static void ah_hmac_sha1_result(struct ah_algorithm_state
*, caddr_t
, size_t);
132 static int ah_sumsiz_sha2_256(struct secasvar
*);
133 static int ah_hmac_sha2_256_mature(struct secasvar
*);
134 static int ah_hmac_sha2_256_init(struct ah_algorithm_state
*,
136 static void ah_hmac_sha2_256_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
137 static void ah_hmac_sha2_256_result(struct ah_algorithm_state
*, caddr_t
, size_t);
138 static int ah_sumsiz_sha2_384(struct secasvar
*);
139 static int ah_hmac_sha2_384_mature(struct secasvar
*);
140 static int ah_hmac_sha2_384_init(struct ah_algorithm_state
*,
142 static void ah_hmac_sha2_384_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
143 static void ah_hmac_sha2_384_result(struct ah_algorithm_state
*, caddr_t
, size_t);
144 static int ah_sumsiz_sha2_512(struct secasvar
*);
145 static int ah_hmac_sha2_512_mature(struct secasvar
*);
146 static int ah_hmac_sha2_512_init(struct ah_algorithm_state
*,
148 static void ah_hmac_sha2_512_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
149 static void ah_hmac_sha2_512_result(struct ah_algorithm_state
*, caddr_t
, size_t);
150 #endif /* AH_ALL_CRYPTO */
152 static void ah_update_mbuf(struct mbuf
*, int, int,
153 const struct ah_algorithm
*, struct ah_algorithm_state
*);
155 const struct ah_algorithm
*
156 ah_algorithm_lookup(int idx
)
158 /* checksum algorithms */
159 static const struct ah_algorithm hmac_md5
=
160 { ah_sumsiz_1216
, ah_hmac_md5_mature
, 128, 128, "hmac-md5",
161 ah_hmac_md5_init
, ah_hmac_md5_loop
,
162 ah_hmac_md5_result
, };
163 static const struct ah_algorithm keyed_md5
=
164 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
165 ah_keyed_md5_init
, ah_keyed_md5_loop
,
166 ah_keyed_md5_result
, };
167 static const struct ah_algorithm hmac_sha1
=
168 { ah_sumsiz_1216
, ah_hmac_sha1_mature
, 160, 160, "hmac-sha1",
169 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
170 ah_hmac_sha1_result
, };
171 static const struct ah_algorithm keyed_sha1
=
172 { ah_sumsiz_1216
, ah_keyed_sha1_mature
, 160, 160, "keyed-sha1",
173 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
174 ah_keyed_sha1_result
, };
175 static const struct ah_algorithm ah_none
=
176 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
177 ah_none_init
, ah_none_loop
, ah_none_result
, };
179 static const struct ah_algorithm hmac_sha2_256
=
180 { ah_sumsiz_sha2_256
, ah_hmac_sha2_256_mature
, 256, 256,
182 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
183 ah_hmac_sha2_256_result
, };
184 static const struct ah_algorithm hmac_sha2_384
=
185 { ah_sumsiz_sha2_384
, ah_hmac_sha2_384_mature
, 384, 384,
187 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
188 ah_hmac_sha2_384_result
, };
189 static const struct ah_algorithm hmac_sha2_512
=
190 { ah_sumsiz_sha2_512
, ah_hmac_sha2_512_mature
, 512, 512,
192 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
193 ah_hmac_sha2_512_result
, };
194 #endif /* AH_ALL_CRYPTO */
197 case SADB_AALG_MD5HMAC
:
199 case SADB_AALG_SHA1HMAC
:
201 case SADB_X_AALG_MD5
:
203 case SADB_X_AALG_SHA
:
205 case SADB_X_AALG_NULL
:
208 case SADB_X_AALG_SHA2_256
:
209 return &hmac_sha2_256
;
210 case SADB_X_AALG_SHA2_384
:
211 return &hmac_sha2_384
;
212 case SADB_X_AALG_SHA2_512
:
213 return &hmac_sha2_512
;
214 #endif /* AH_ALL_CRYPTO */
222 ah_sumsiz_1216(struct secasvar
*sav
)
227 if (sav
->flags
& SADB_X_EXT_OLD
) {
235 ah_sumsiz_zero(struct secasvar
*sav
)
244 ah_none_mature(struct secasvar
*sav
)
246 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
248 "ah_none_mature: protocol and algorithm mismatch.\n"));
256 struct ah_algorithm_state
*state
,
257 __unused
struct secasvar
*sav
)
265 __unused
struct ah_algorithm_state
*state
,
266 __unused caddr_t addr
,
273 __unused
struct ah_algorithm_state
*state
,
274 __unused caddr_t addr
,
281 __unused
struct secasvar
*sav
)
283 /* anything is okay */
288 ah_keyed_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
291 u_int8_t buf
[32] __attribute__((aligned(4)));
295 panic("ah_keyed_md5_init: what?");
299 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
300 if (state
->foo
== NULL
) {
304 MD5Init((MD5_CTX
*)state
->foo
);
306 MD5Update((MD5_CTX
*)state
->foo
,
307 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
308 (u_int
)_KEYLEN(state
->sav
->key_auth
));
312 * We cannot simply use md5_pad() since the function
313 * won't update the total length.
315 if (_KEYLEN(state
->sav
->key_auth
) < 56) {
316 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
318 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
320 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
324 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
327 bzero(buf
, sizeof(buf
));
328 while (sizeof(buf
) < padlen
) {
329 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
330 padlen
-= sizeof(buf
);
333 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
336 buf
[0] = (keybitlen
>> 0) & 0xff;
337 buf
[1] = (keybitlen
>> 8) & 0xff;
338 buf
[2] = (keybitlen
>> 16) & 0xff;
339 buf
[3] = (keybitlen
>> 24) & 0xff;
340 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
347 ah_keyed_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
350 panic("ah_keyed_md5_loop: what?");
353 VERIFY(len
<= UINT_MAX
);
354 MD5Update((MD5_CTX
*)state
->foo
, addr
, (uint
)len
);
358 ah_keyed_md5_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
360 u_char digest
[16] __attribute__((aligned(4)));
363 panic("ah_keyed_md5_result: what?");
367 MD5Update((MD5_CTX
*)state
->foo
,
368 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
369 (u_int
)_KEYLEN(state
->sav
->key_auth
));
371 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
372 FREE(state
->foo
, M_TEMP
);
373 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
377 ah_keyed_sha1_mature(struct secasvar
*sav
)
379 const struct ah_algorithm
*algo
;
381 if (!sav
->key_auth
) {
382 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
386 algo
= ah_algorithm_lookup(sav
->alg_auth
);
388 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
392 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
393 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
395 "ah_keyed_sha1_mature: invalid key length %d.\n",
396 sav
->key_auth
->sadb_key_bits
));
404 ah_keyed_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
409 u_int8_t buf
[32] __attribute__((aligned(4)));
412 panic("ah_keyed_sha1_init: what?");
416 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_NOWAIT
);
421 ctxt
= (SHA1_CTX
*)state
->foo
;
425 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
426 (u_int
)_KEYLEN(state
->sav
->key_auth
));
431 if (_KEYLEN(state
->sav
->key_auth
) < 56) {
432 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
434 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
436 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
440 SHA1Update(ctxt
, &buf
[0], 1);
443 bzero(buf
, sizeof(buf
));
444 while (sizeof(buf
) < padlen
) {
445 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
446 padlen
-= sizeof(buf
);
449 SHA1Update(ctxt
, &buf
[0], padlen
);
452 buf
[0] = (keybitlen
>> 0) & 0xff;
453 buf
[1] = (keybitlen
>> 8) & 0xff;
454 buf
[2] = (keybitlen
>> 16) & 0xff;
455 buf
[3] = (keybitlen
>> 24) & 0xff;
456 SHA1Update(ctxt
, buf
, 8);
463 ah_keyed_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
467 if (!state
|| !state
->foo
) {
468 panic("ah_keyed_sha1_loop: what?");
470 ctxt
= (SHA1_CTX
*)state
->foo
;
472 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
476 ah_keyed_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
478 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
481 if (!state
|| !state
->foo
) {
482 panic("ah_keyed_sha1_result: what?");
484 ctxt
= (SHA1_CTX
*)state
->foo
;
487 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
488 (u_int
)_KEYLEN(state
->sav
->key_auth
));
490 SHA1Final((caddr_t
)&digest
[0], ctxt
);
491 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
493 FREE(state
->foo
, M_TEMP
);
497 ah_hmac_md5_mature(struct secasvar
*sav
)
499 const struct ah_algorithm
*algo
;
501 if (!sav
->key_auth
) {
502 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
506 algo
= ah_algorithm_lookup(sav
->alg_auth
);
508 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
512 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
513 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
515 "ah_hmac_md5_mature: invalid key length %d.\n",
516 sav
->key_auth
->sadb_key_bits
));
524 ah_hmac_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
528 u_char tk
[16] __attribute__((aligned(4)));
535 panic("ah_hmac_md5_init: what?");
539 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
544 ipad
= (u_char
*)state
->foo
;
545 opad
= (u_char
*)(ipad
+ 64);
546 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
548 /* compress the key if necessery */
549 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
551 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
552 _KEYLEN(state
->sav
->key_auth
));
553 MD5Final(&tk
[0], ctxt
);
557 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
558 keylen
= _KEYLEN(state
->sav
->key_auth
);
563 bcopy(key
, ipad
, keylen
);
564 bcopy(key
, opad
, keylen
);
565 for (i
= 0; i
< 64; i
++) {
571 MD5Update(ctxt
, ipad
, 64);
577 ah_hmac_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
581 if (!state
|| !state
->foo
) {
582 panic("ah_hmac_md5_loop: what?");
584 ctxt
= (MD5_CTX
*)(void *)(((caddr_t
)state
->foo
) + 128);
585 VERIFY(len
<= UINT_MAX
);
586 MD5Update(ctxt
, addr
, (uint
)len
);
590 ah_hmac_md5_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
592 u_char digest
[16] __attribute__((aligned(4)));
597 if (!state
|| !state
->foo
) {
598 panic("ah_hmac_md5_result: what?");
601 ipad
= (u_char
*)state
->foo
;
602 opad
= (u_char
*)(ipad
+ 64);
603 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
605 MD5Final(&digest
[0], ctxt
);
608 MD5Update(ctxt
, opad
, 64);
609 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
610 MD5Final(&digest
[0], ctxt
);
612 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
614 FREE(state
->foo
, M_TEMP
);
618 ah_hmac_sha1_mature(struct secasvar
*sav
)
620 const struct ah_algorithm
*algo
;
622 if (!sav
->key_auth
) {
623 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
627 algo
= ah_algorithm_lookup(sav
->alg_auth
);
629 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
633 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
634 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
636 "ah_hmac_sha1_mature: invalid key length %d.\n",
637 sav
->key_auth
->sadb_key_bits
));
645 ah_hmac_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
650 u_char tk
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
656 panic("ah_hmac_sha1_init: what?");
660 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
666 ipad
= (u_char
*)state
->foo
;
667 opad
= (u_char
*)(ipad
+ 64);
668 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
670 /* compress the key if necessery */
671 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
673 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
674 _KEYLEN(state
->sav
->key_auth
));
675 SHA1Final(&tk
[0], ctxt
);
677 keylen
= SHA1_RESULTLEN
;
679 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
680 keylen
= _KEYLEN(state
->sav
->key_auth
);
685 bcopy(key
, ipad
, keylen
);
686 bcopy(key
, opad
, keylen
);
687 for (i
= 0; i
< 64; i
++) {
693 SHA1Update(ctxt
, ipad
, 64);
699 ah_hmac_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
703 if (!state
|| !state
->foo
) {
704 panic("ah_hmac_sha1_loop: what?");
707 ctxt
= (SHA1_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
708 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
712 ah_hmac_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
714 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
719 if (!state
|| !state
->foo
) {
720 panic("ah_hmac_sha1_result: what?");
723 ipad
= (u_char
*)state
->foo
;
724 opad
= (u_char
*)(ipad
+ 64);
725 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
727 SHA1Final((caddr_t
)&digest
[0], ctxt
);
730 SHA1Update(ctxt
, opad
, 64);
731 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
732 SHA1Final((caddr_t
)&digest
[0], ctxt
);
734 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
736 FREE(state
->foo
, M_TEMP
);
741 ah_sumsiz_sha2_256(struct secasvar
*sav
)
746 // return half the output size (in bytes), as per rfc 4868
747 return 16; // 256/(8*2)
751 ah_hmac_sha2_256_mature(struct secasvar
*sav
)
753 const struct ah_algorithm
*algo
;
755 if (!sav
->key_auth
) {
757 "ah_hmac_sha2_256_mature: no key is given.\n"));
761 algo
= ah_algorithm_lookup(sav
->alg_auth
);
764 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
768 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
769 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
771 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
772 sav
->key_auth
->sadb_key_bits
));
780 ah_hmac_sha2_256_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
785 u_char tk
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
791 panic("ah_hmac_sha2_256_init: what?");
795 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
801 ipad
= (u_char
*)state
->foo
;
802 opad
= (u_char
*)(ipad
+ 64);
803 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
805 /* compress the key if necessery */
806 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
807 bzero(tk
, sizeof(tk
));
808 bzero(ctxt
, sizeof(*ctxt
));
810 SHA256_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
811 _KEYLEN(state
->sav
->key_auth
));
812 SHA256_Final(&tk
[0], ctxt
);
814 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
816 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
817 keylen
= _KEYLEN(state
->sav
->key_auth
);
822 bcopy(key
, ipad
, keylen
);
823 bcopy(key
, opad
, keylen
);
824 for (i
= 0; i
< 64; i
++) {
829 bzero(ctxt
, sizeof(*ctxt
));
831 SHA256_Update(ctxt
, ipad
, 64);
837 ah_hmac_sha2_256_loop(struct ah_algorithm_state
*state
,
843 if (!state
|| !state
->foo
) {
844 panic("ah_hmac_sha2_256_loop: what?");
847 ctxt
= (SHA256_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
848 SHA256_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
852 ah_hmac_sha2_256_result(struct ah_algorithm_state
*state
,
856 u_char digest
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
861 if (!state
|| !state
->foo
) {
862 panic("ah_hmac_sha2_256_result: what?");
865 ipad
= (u_char
*)state
->foo
;
866 opad
= (u_char
*)(ipad
+ 64);
867 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
869 SHA256_Final((u_int8_t
*)digest
, ctxt
);
872 SHA256_Update(ctxt
, opad
, 64);
873 SHA256_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
874 SHA256_Final((u_int8_t
*)digest
, ctxt
);
876 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
878 FREE(state
->foo
, M_TEMP
);
882 ah_sumsiz_sha2_384(struct secasvar
*sav
)
887 // return half the output size (in bytes), as per rfc 4868
888 return 24; // 384/(8*2)
892 ah_hmac_sha2_384_mature(struct secasvar
*sav
)
894 const struct ah_algorithm
*algo
;
896 if (!sav
->key_auth
) {
898 "ah_hmac_sha2_384_mature: no key is given.\n"));
902 algo
= ah_algorithm_lookup(sav
->alg_auth
);
905 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
909 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
910 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
912 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
913 sav
->key_auth
->sadb_key_bits
));
921 ah_hmac_sha2_384_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
926 u_char tk
[SHA384_DIGEST_LENGTH
] __attribute__((aligned(4)));
932 panic("ah_hmac_sha2_384_init: what?");
936 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA384_CTX
),
937 M_TEMP
, M_NOWAIT
| M_ZERO
);
942 ipad
= (u_char
*)state
->foo
;
943 opad
= (u_char
*)(ipad
+ 128);
944 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
946 /* compress the key if necessery */
947 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
948 bzero(tk
, sizeof(tk
));
949 bzero(ctxt
, sizeof(*ctxt
));
951 SHA384_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
952 _KEYLEN(state
->sav
->key_auth
));
953 SHA384_Final(&tk
[0], ctxt
);
955 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
957 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
958 keylen
= _KEYLEN(state
->sav
->key_auth
);
963 bcopy(key
, ipad
, keylen
);
964 bcopy(key
, opad
, keylen
);
965 for (i
= 0; i
< 128; i
++) {
970 bzero(ctxt
, sizeof(*ctxt
));
972 SHA384_Update(ctxt
, ipad
, 128);
978 ah_hmac_sha2_384_loop(struct ah_algorithm_state
*state
,
984 if (!state
|| !state
->foo
) {
985 panic("ah_hmac_sha2_384_loop: what?");
988 ctxt
= (SHA384_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
989 SHA384_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
993 ah_hmac_sha2_384_result(struct ah_algorithm_state
*state
,
997 u_char digest
[SHA384_DIGEST_LENGTH
];
1002 if (!state
|| !state
->foo
) {
1003 panic("ah_hmac_sha2_384_result: what?");
1006 ipad
= (u_char
*)state
->foo
;
1007 opad
= (u_char
*)(ipad
+ 128);
1008 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
1010 SHA384_Final((u_int8_t
*)digest
, ctxt
);
1013 SHA384_Update(ctxt
, opad
, 128);
1014 SHA384_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
1015 SHA384_Final((u_int8_t
*)digest
, ctxt
);
1017 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
1019 FREE(state
->foo
, M_TEMP
);
1023 ah_sumsiz_sha2_512(struct secasvar
*sav
)
1028 // return half the output size (in bytes), as per rfc 4868
1029 return 32; // 512/(8*2)
1033 ah_hmac_sha2_512_mature(struct secasvar
*sav
)
1035 const struct ah_algorithm
*algo
;
1037 if (!sav
->key_auth
) {
1039 "ah_hmac_sha2_512_mature: no key is given.\n"));
1043 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1046 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1050 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1051 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1053 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1054 sav
->key_auth
->sadb_key_bits
));
1062 ah_hmac_sha2_512_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
1067 u_char tk
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1073 panic("ah_hmac_sha2_512_init: what?");
1077 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA512_CTX
),
1078 M_TEMP
, M_NOWAIT
| M_ZERO
);
1083 ipad
= (u_char
*)state
->foo
;
1084 opad
= (u_char
*)(ipad
+ 128);
1085 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1087 /* compress the key if necessery */
1088 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
1089 bzero(tk
, sizeof(tk
));
1090 bzero(ctxt
, sizeof(*ctxt
));
1092 SHA512_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
1093 _KEYLEN(state
->sav
->key_auth
));
1094 SHA512_Final(&tk
[0], ctxt
);
1096 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
1098 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
1099 keylen
= _KEYLEN(state
->sav
->key_auth
);
1104 bcopy(key
, ipad
, keylen
);
1105 bcopy(key
, opad
, keylen
);
1106 for (i
= 0; i
< 128; i
++) {
1111 bzero(ctxt
, sizeof(*ctxt
));
1113 SHA512_Update(ctxt
, ipad
, 128);
1119 ah_hmac_sha2_512_loop(struct ah_algorithm_state
*state
,
1125 if (!state
|| !state
->foo
) {
1126 panic("ah_hmac_sha2_512_loop: what?");
1129 ctxt
= (SHA512_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
1130 SHA512_Update(ctxt
, (const u_int8_t
*) addr
, (size_t)len
);
1134 ah_hmac_sha2_512_result(struct ah_algorithm_state
*state
,
1138 u_char digest
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1143 if (!state
|| !state
->foo
) {
1144 panic("ah_hmac_sha2_512_result: what?");
1147 ipad
= (u_char
*)state
->foo
;
1148 opad
= (u_char
*)(ipad
+ 128);
1149 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1151 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1154 SHA512_Update(ctxt
, opad
, 128);
1155 SHA512_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
1156 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1158 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
1160 FREE(state
->foo
, M_TEMP
);
1162 #endif /* AH_ALL_CRYPTO */
1164 /*------------------------------------------------------------*/
1167 * go generate the checksum.
1170 ah_update_mbuf(struct mbuf
*m
, int off
, int len
,
1171 const struct ah_algorithm
*algo
,
1172 struct ah_algorithm_state
*algos
)
1177 /* easy case first */
1178 if (off
+ len
<= m
->m_len
) {
1179 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1183 for (n
= m
; n
; n
= n
->m_next
) {
1184 if (off
< n
->m_len
) {
1192 panic("ah_update_mbuf: wrong offset specified");
1195 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1196 if (n
->m_len
== 0) {
1199 if (n
->m_len
- off
< len
) {
1200 tlen
= n
->m_len
- off
;
1205 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1214 * Go generate the checksum. This function won't modify the mbuf chain
1217 * NOTE: the function does not free mbuf on failure.
1218 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1221 ah4_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1222 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1226 size_t advancewidth
;
1227 struct ah_algorithm_state algos
;
1228 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1231 struct mbuf
*n
= NULL
;
1233 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1238 hdrtype
= -1; /*dummy, it is called IPPROTO_IP*/
1242 error
= (algo
->init
)(&algos
, sav
);
1247 advancewidth
= 0; /*safety*/
1252 case -1: /*first one only*/
1255 * copy ip hdr, modify to fit the AH checksum rule,
1256 * then take a checksum.
1261 m_copydata(m
, off
, sizeof(iphdr
), (caddr_t
)&iphdr
);
1263 hlen
= IP_VHL_HL(iphdr
.ip_vhl
) << 2;
1265 hlen
= iphdr
.ip_hl
<< 2;
1268 iphdr
.ip_sum
= htons(0);
1269 if (ip4_ah_cleartos
) {
1272 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1273 (algo
->update
)(&algos
, (caddr_t
)&iphdr
, sizeof(struct ip
));
1275 if (hlen
!= sizeof(struct ip
)) {
1279 if (hlen
> MCLBYTES
) {
1283 MGET(n
, M_DONTWAIT
, MT_DATA
);
1284 if (n
&& hlen
> MLEN
) {
1285 MCLGET(n
, M_DONTWAIT
);
1286 if ((n
->m_flags
& M_EXT
) == 0) {
1295 VERIFY(hlen
<= INT_MAX
);
1296 m_copydata(m
, off
, (int)hlen
, mtod(n
, caddr_t
));
1299 * IP options processing.
1300 * See RFC2402 appendix A.
1302 p
= mtod(n
, u_char
*);
1303 i
= sizeof(struct ip
);
1305 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1306 ipseclog((LOG_ERR
, "ah4_calccksum: "
1307 "invalid IP option\n"));
1311 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1312 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1313 i
+ IPOPT_OLEN
< hlen
) {
1317 "ah4_calccksum: invalid IP option "
1319 p
[i
+ IPOPT_OPTVAL
]));
1325 switch (p
[i
+ IPOPT_OPTVAL
]) {
1331 case IPOPT_SECURITY
: /* 0x82 */
1332 case 0x85: /* Extended security */
1333 case 0x86: /* Commercial security */
1334 case 0x94: /* Router alert */
1335 case 0x95: /* RFC1770 */
1336 l
= p
[i
+ IPOPT_OLEN
];
1343 l
= p
[i
+ IPOPT_OLEN
];
1350 if (l
< 1 || hlen
- i
< l
) {
1353 "ah4_calccksum: invalid IP option "
1354 "(type=%02x len=%02x)\n",
1355 p
[i
+ IPOPT_OPTVAL
],
1356 p
[i
+ IPOPT_OLEN
]));
1363 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
) {
1369 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1370 (algo
->update
)(&algos
, (caddr_t
)p
, hlen
- sizeof(struct ip
));
1376 hdrtype
= (iphdr
.ip_p
) & 0xff;
1377 advancewidth
= hlen
;
1388 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1389 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1391 : sizeof(struct newah
);
1392 siz
= (*algo
->sumsiz
)(sav
);
1393 totlen
= (ah
.ah_len
+ 2) << 2;
1396 * special treatment is necessary for the first one, not others
1399 if (totlen
> m
->m_pkthdr
.len
- off
||
1400 totlen
> MCLBYTES
) {
1404 MGET(n
, M_DONTWAIT
, MT_DATA
);
1405 if (n
&& totlen
> MLEN
) {
1406 MCLGET(n
, M_DONTWAIT
);
1407 if ((n
->m_flags
& M_EXT
) == 0) {
1416 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1418 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1419 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1423 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1427 hdrtype
= ah
.ah_nxt
;
1428 advancewidth
= totlen
;
1433 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1434 advancewidth
= m
->m_pkthdr
.len
- off
;
1438 off
+= advancewidth
;
1439 if (off
< m
->m_pkthdr
.len
) {
1443 if (len
< (*algo
->sumsiz
)(sav
)) {
1448 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1449 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1465 * Go generate the checksum. This function won't modify the mbuf chain
1468 * NOTE: the function does not free mbuf on failure.
1469 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1472 ah6_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1473 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1477 struct mbuf
*n
= NULL
;
1480 struct ah_algorithm_state algos
;
1481 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1483 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1487 error
= (algo
->init
)(&algos
, sav
);
1493 proto
= IPPROTO_IPV6
;
1498 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1500 newoff
= m
->m_pkthdr
.len
;
1501 } else if (newoff
<= off
) {
1504 } else if (m
->m_pkthdr
.len
< newoff
) {
1512 * special treatment is necessary for the first one, not others
1515 struct ip6_hdr ip6copy
;
1517 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1522 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1524 ip6copy
.ip6_flow
= 0;
1525 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1526 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1527 ip6copy
.ip6_hlim
= 0;
1528 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
)) {
1529 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1531 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
)) {
1532 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1534 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1535 sizeof(struct ip6_hdr
));
1537 newoff
= m
->m_pkthdr
.len
;
1538 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1548 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1550 : sizeof(struct newah
);
1551 siz
= (*algo
->sumsiz
)(sav
);
1554 * special treatment is necessary for the first one, not others
1557 if (newoff
- off
> MCLBYTES
) {
1561 MGET(n
, M_DONTWAIT
, MT_DATA
);
1562 if (n
&& newoff
- off
> MLEN
) {
1563 MCLGET(n
, M_DONTWAIT
);
1564 if ((n
->m_flags
& M_EXT
) == 0) {
1573 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1574 n
->m_len
= newoff
- off
;
1575 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1576 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1580 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1586 case IPPROTO_HOPOPTS
:
1587 case IPPROTO_DSTOPTS
:
1589 struct ip6_ext
*ip6e
;
1591 u_int8_t
*p
, *optend
, *optp
;
1593 if (newoff
- off
> MCLBYTES
) {
1597 MGET(n
, M_DONTWAIT
, MT_DATA
);
1598 if (n
&& newoff
- off
> MLEN
) {
1599 MCLGET(n
, M_DONTWAIT
);
1600 if ((n
->m_flags
& M_EXT
) == 0) {
1609 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1610 n
->m_len
= newoff
- off
;
1612 ip6e
= mtod(n
, struct ip6_ext
*);
1613 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1614 if (newoff
- off
< hdrlen
) {
1620 p
= mtod(n
, u_int8_t
*);
1621 optend
= p
+ hdrlen
;
1624 * ICV calculation for the options header including all
1625 * options. This part is a little tricky since there are
1626 * two type of options; mutable and immutable. We try to
1627 * null-out mutable ones here.
1630 while (optp
< optend
) {
1631 if (optp
[0] == IP6OPT_PAD1
) {
1634 if (optp
+ 2 > optend
) {
1640 optlen
= optp
[1] + 2;
1641 if (optp
+ optlen
> optend
) {
1648 if (optp
[0] & IP6OPT_MUTABLE
) {
1649 bzero(optp
+ 2, optlen
- 2);
1656 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1662 case IPPROTO_ROUTING
:
1664 * For an input packet, we can just calculate `as is'.
1665 * For an output packet, we assume ip6_output have already
1666 * made packet how it will be received at the final
1672 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1676 if (newoff
< m
->m_pkthdr
.len
) {
1682 if (len
< (*algo
->sumsiz
)(sav
)) {
1687 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1688 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));