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>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet/icmp6.h>
93 #include <netinet6/ipsec.h>
95 #include <netinet6/ipsec6.h>
97 #include <netinet6/ah.h>
99 #include <netinet6/ah6.h>
102 #include <netinet6/esp.h>
104 #include <netinet6/esp6.h>
107 #include <net/pfkeyv2.h>
108 #include <netkey/keydb.h>
109 #include <libkern/crypto/md5.h>
110 #include <libkern/crypto/sha1.h>
111 #include <libkern/crypto/sha2.h>
113 #include <net/net_osdep.h>
117 static int ah_sumsiz_1216(struct secasvar
*);
118 static int ah_sumsiz_zero(struct secasvar
*);
119 static int ah_none_mature(struct secasvar
*);
120 static int ah_none_init(struct ah_algorithm_state
*, struct secasvar
*);
121 static void ah_none_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
122 static void ah_none_result(struct ah_algorithm_state
*, caddr_t
, size_t);
123 static int ah_keyed_md5_mature(struct secasvar
*);
124 static int ah_keyed_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
125 static void ah_keyed_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
126 static void ah_keyed_md5_result(struct ah_algorithm_state
*, caddr_t
, size_t);
127 static int ah_keyed_sha1_mature(struct secasvar
*);
128 static int ah_keyed_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
129 static void ah_keyed_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
130 static void ah_keyed_sha1_result(struct ah_algorithm_state
*, caddr_t
, size_t);
131 static int ah_hmac_md5_mature(struct secasvar
*);
132 static int ah_hmac_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
133 static void ah_hmac_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
134 static void ah_hmac_md5_result(struct ah_algorithm_state
*, caddr_t
, size_t);
135 static int ah_hmac_sha1_mature(struct secasvar
*);
136 static int ah_hmac_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
137 static void ah_hmac_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
138 static void ah_hmac_sha1_result(struct ah_algorithm_state
*, caddr_t
, size_t);
140 static int ah_sumsiz_sha2_256(struct secasvar
*);
141 static int ah_hmac_sha2_256_mature(struct secasvar
*);
142 static int ah_hmac_sha2_256_init(struct ah_algorithm_state
*,
144 static void ah_hmac_sha2_256_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
145 static void ah_hmac_sha2_256_result(struct ah_algorithm_state
*, caddr_t
, size_t);
146 static int ah_sumsiz_sha2_384(struct secasvar
*);
147 static int ah_hmac_sha2_384_mature(struct secasvar
*);
148 static int ah_hmac_sha2_384_init(struct ah_algorithm_state
*,
150 static void ah_hmac_sha2_384_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
151 static void ah_hmac_sha2_384_result(struct ah_algorithm_state
*, caddr_t
, size_t);
152 static int ah_sumsiz_sha2_512(struct secasvar
*);
153 static int ah_hmac_sha2_512_mature(struct secasvar
*);
154 static int ah_hmac_sha2_512_init(struct ah_algorithm_state
*,
156 static void ah_hmac_sha2_512_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
157 static void ah_hmac_sha2_512_result(struct ah_algorithm_state
*, caddr_t
, size_t);
158 #endif /* AH_ALL_CRYPTO */
160 static void ah_update_mbuf(struct mbuf
*, int, int,
161 const struct ah_algorithm
*, struct ah_algorithm_state
*);
163 const struct ah_algorithm
*
164 ah_algorithm_lookup(int idx
)
166 /* checksum algorithms */
167 static const struct ah_algorithm hmac_md5
=
168 { ah_sumsiz_1216
, ah_hmac_md5_mature
, 128, 128, "hmac-md5",
169 ah_hmac_md5_init
, ah_hmac_md5_loop
,
170 ah_hmac_md5_result
, };
171 static const struct ah_algorithm keyed_md5
=
172 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
173 ah_keyed_md5_init
, ah_keyed_md5_loop
,
174 ah_keyed_md5_result
, };
175 static const struct ah_algorithm hmac_sha1
=
176 { ah_sumsiz_1216
, ah_hmac_sha1_mature
, 160, 160, "hmac-sha1",
177 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
178 ah_hmac_sha1_result
, };
179 static const struct ah_algorithm keyed_sha1
=
180 { ah_sumsiz_1216
, ah_keyed_sha1_mature
, 160, 160, "keyed-sha1",
181 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
182 ah_keyed_sha1_result
, };
183 static const struct ah_algorithm ah_none
=
184 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
185 ah_none_init
, ah_none_loop
, ah_none_result
, };
187 static const struct ah_algorithm hmac_sha2_256
=
188 { ah_sumsiz_sha2_256
, ah_hmac_sha2_256_mature
, 256, 256,
190 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
191 ah_hmac_sha2_256_result
, };
192 static const struct ah_algorithm hmac_sha2_384
=
193 { ah_sumsiz_sha2_384
, ah_hmac_sha2_384_mature
, 384, 384,
195 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
196 ah_hmac_sha2_384_result
, };
197 static const struct ah_algorithm hmac_sha2_512
=
198 { ah_sumsiz_sha2_512
, ah_hmac_sha2_512_mature
, 512, 512,
200 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
201 ah_hmac_sha2_512_result
, };
202 #endif /* AH_ALL_CRYPTO */
205 case SADB_AALG_MD5HMAC
:
207 case SADB_AALG_SHA1HMAC
:
209 case SADB_X_AALG_MD5
:
211 case SADB_X_AALG_SHA
:
213 case SADB_X_AALG_NULL
:
216 case SADB_X_AALG_SHA2_256
:
217 return &hmac_sha2_256
;
218 case SADB_X_AALG_SHA2_384
:
219 return &hmac_sha2_384
;
220 case SADB_X_AALG_SHA2_512
:
221 return &hmac_sha2_512
;
222 #endif /* AH_ALL_CRYPTO */
230 ah_sumsiz_1216(struct secasvar
*sav
)
235 if (sav
->flags
& SADB_X_EXT_OLD
) {
243 ah_sumsiz_zero(struct secasvar
*sav
)
252 ah_none_mature(struct secasvar
*sav
)
254 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
256 "ah_none_mature: protocol and algorithm mismatch.\n"));
264 struct ah_algorithm_state
*state
,
265 __unused
struct secasvar
*sav
)
273 __unused
struct ah_algorithm_state
*state
,
274 __unused caddr_t addr
,
281 __unused
struct ah_algorithm_state
*state
,
282 __unused caddr_t addr
,
289 __unused
struct secasvar
*sav
)
291 /* anything is okay */
296 ah_keyed_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
300 u_int8_t buf
[32] __attribute__((aligned(4)));
303 panic("ah_keyed_md5_init: what?");
307 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
308 if (state
->foo
== NULL
) {
312 MD5Init((MD5_CTX
*)state
->foo
);
314 MD5Update((MD5_CTX
*)state
->foo
,
315 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
316 (u_int
)_KEYLEN(state
->sav
->key_auth
));
320 * We cannot simply use md5_pad() since the function
321 * won't update the total length.
323 if (_KEYLEN(state
->sav
->key_auth
) < 56) {
324 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
326 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
328 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
332 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
335 bzero(buf
, sizeof(buf
));
336 while (sizeof(buf
) < padlen
) {
337 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
338 padlen
-= sizeof(buf
);
341 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
344 buf
[0] = (keybitlen
>> 0) & 0xff;
345 buf
[1] = (keybitlen
>> 8) & 0xff;
346 buf
[2] = (keybitlen
>> 16) & 0xff;
347 buf
[3] = (keybitlen
>> 24) & 0xff;
348 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
355 ah_keyed_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
358 panic("ah_keyed_md5_loop: what?");
361 MD5Update((MD5_CTX
*)state
->foo
, addr
, len
);
365 ah_keyed_md5_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
367 u_char digest
[16] __attribute__((aligned(4)));
370 panic("ah_keyed_md5_result: what?");
374 MD5Update((MD5_CTX
*)state
->foo
,
375 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
376 (u_int
)_KEYLEN(state
->sav
->key_auth
));
378 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
379 FREE(state
->foo
, M_TEMP
);
380 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
384 ah_keyed_sha1_mature(struct secasvar
*sav
)
386 const struct ah_algorithm
*algo
;
388 if (!sav
->key_auth
) {
389 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
393 algo
= ah_algorithm_lookup(sav
->alg_auth
);
395 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
399 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
400 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
402 "ah_keyed_sha1_mature: invalid key length %d.\n",
403 sav
->key_auth
->sadb_key_bits
));
411 ah_keyed_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
416 u_int8_t buf
[32] __attribute__((aligned(4)));
419 panic("ah_keyed_sha1_init: what?");
423 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_NOWAIT
);
428 ctxt
= (SHA1_CTX
*)state
->foo
;
432 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
433 (u_int
)_KEYLEN(state
->sav
->key_auth
));
438 if (_KEYLEN(state
->sav
->key_auth
) < 56) {
439 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
441 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
443 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
447 SHA1Update(ctxt
, &buf
[0], 1);
450 bzero(buf
, sizeof(buf
));
451 while (sizeof(buf
) < padlen
) {
452 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
453 padlen
-= sizeof(buf
);
456 SHA1Update(ctxt
, &buf
[0], padlen
);
459 buf
[0] = (keybitlen
>> 0) & 0xff;
460 buf
[1] = (keybitlen
>> 8) & 0xff;
461 buf
[2] = (keybitlen
>> 16) & 0xff;
462 buf
[3] = (keybitlen
>> 24) & 0xff;
463 SHA1Update(ctxt
, buf
, 8);
470 ah_keyed_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
474 if (!state
|| !state
->foo
) {
475 panic("ah_keyed_sha1_loop: what?");
477 ctxt
= (SHA1_CTX
*)state
->foo
;
479 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
483 ah_keyed_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
485 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
488 if (!state
|| !state
->foo
) {
489 panic("ah_keyed_sha1_result: what?");
491 ctxt
= (SHA1_CTX
*)state
->foo
;
494 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
495 (u_int
)_KEYLEN(state
->sav
->key_auth
));
497 SHA1Final((caddr_t
)&digest
[0], ctxt
);
498 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
500 FREE(state
->foo
, M_TEMP
);
504 ah_hmac_md5_mature(struct secasvar
*sav
)
506 const struct ah_algorithm
*algo
;
508 if (!sav
->key_auth
) {
509 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
513 algo
= ah_algorithm_lookup(sav
->alg_auth
);
515 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
519 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
520 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
522 "ah_hmac_md5_mature: invalid key length %d.\n",
523 sav
->key_auth
->sadb_key_bits
));
531 ah_hmac_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
535 u_char tk
[16] __attribute__((aligned(4)));
542 panic("ah_hmac_md5_init: what?");
546 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
551 ipad
= (u_char
*)state
->foo
;
552 opad
= (u_char
*)(ipad
+ 64);
553 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
555 /* compress the key if necessery */
556 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
558 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
559 _KEYLEN(state
->sav
->key_auth
));
560 MD5Final(&tk
[0], ctxt
);
564 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
565 keylen
= _KEYLEN(state
->sav
->key_auth
);
570 bcopy(key
, ipad
, keylen
);
571 bcopy(key
, opad
, keylen
);
572 for (i
= 0; i
< 64; i
++) {
578 MD5Update(ctxt
, ipad
, 64);
584 ah_hmac_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
588 if (!state
|| !state
->foo
) {
589 panic("ah_hmac_md5_loop: what?");
591 ctxt
= (MD5_CTX
*)(void *)(((caddr_t
)state
->foo
) + 128);
592 MD5Update(ctxt
, addr
, len
);
596 ah_hmac_md5_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
598 u_char digest
[16] __attribute__((aligned(4)));
603 if (!state
|| !state
->foo
) {
604 panic("ah_hmac_md5_result: what?");
607 ipad
= (u_char
*)state
->foo
;
608 opad
= (u_char
*)(ipad
+ 64);
609 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
611 MD5Final(&digest
[0], ctxt
);
614 MD5Update(ctxt
, opad
, 64);
615 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
616 MD5Final(&digest
[0], ctxt
);
618 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
620 FREE(state
->foo
, M_TEMP
);
624 ah_hmac_sha1_mature(struct secasvar
*sav
)
626 const struct ah_algorithm
*algo
;
628 if (!sav
->key_auth
) {
629 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
633 algo
= ah_algorithm_lookup(sav
->alg_auth
);
635 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
639 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
640 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
642 "ah_hmac_sha1_mature: invalid key length %d.\n",
643 sav
->key_auth
->sadb_key_bits
));
651 ah_hmac_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
656 u_char tk
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
662 panic("ah_hmac_sha1_init: what?");
666 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
672 ipad
= (u_char
*)state
->foo
;
673 opad
= (u_char
*)(ipad
+ 64);
674 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
676 /* compress the key if necessery */
677 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
679 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
680 _KEYLEN(state
->sav
->key_auth
));
681 SHA1Final(&tk
[0], ctxt
);
683 keylen
= SHA1_RESULTLEN
;
685 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
686 keylen
= _KEYLEN(state
->sav
->key_auth
);
691 bcopy(key
, ipad
, keylen
);
692 bcopy(key
, opad
, keylen
);
693 for (i
= 0; i
< 64; i
++) {
699 SHA1Update(ctxt
, ipad
, 64);
705 ah_hmac_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
709 if (!state
|| !state
->foo
) {
710 panic("ah_hmac_sha1_loop: what?");
713 ctxt
= (SHA1_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
714 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
718 ah_hmac_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
720 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
725 if (!state
|| !state
->foo
) {
726 panic("ah_hmac_sha1_result: what?");
729 ipad
= (u_char
*)state
->foo
;
730 opad
= (u_char
*)(ipad
+ 64);
731 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
733 SHA1Final((caddr_t
)&digest
[0], ctxt
);
736 SHA1Update(ctxt
, opad
, 64);
737 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
738 SHA1Final((caddr_t
)&digest
[0], ctxt
);
740 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
742 FREE(state
->foo
, M_TEMP
);
747 ah_sumsiz_sha2_256(struct secasvar
*sav
)
752 // return half the output size (in bytes), as per rfc 4868
753 return 16; // 256/(8*2)
757 ah_hmac_sha2_256_mature(struct secasvar
*sav
)
759 const struct ah_algorithm
*algo
;
761 if (!sav
->key_auth
) {
763 "ah_hmac_sha2_256_mature: no key is given.\n"));
767 algo
= ah_algorithm_lookup(sav
->alg_auth
);
770 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
774 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
775 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
777 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
778 sav
->key_auth
->sadb_key_bits
));
786 ah_hmac_sha2_256_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
791 u_char tk
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
797 panic("ah_hmac_sha2_256_init: what?");
801 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
807 ipad
= (u_char
*)state
->foo
;
808 opad
= (u_char
*)(ipad
+ 64);
809 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
811 /* compress the key if necessery */
812 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
813 bzero(tk
, sizeof(tk
));
814 bzero(ctxt
, sizeof(*ctxt
));
816 SHA256_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
817 _KEYLEN(state
->sav
->key_auth
));
818 SHA256_Final(&tk
[0], ctxt
);
820 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
822 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
823 keylen
= _KEYLEN(state
->sav
->key_auth
);
828 bcopy(key
, ipad
, keylen
);
829 bcopy(key
, opad
, keylen
);
830 for (i
= 0; i
< 64; i
++) {
835 bzero(ctxt
, sizeof(*ctxt
));
837 SHA256_Update(ctxt
, ipad
, 64);
843 ah_hmac_sha2_256_loop(struct ah_algorithm_state
*state
,
849 if (!state
|| !state
->foo
) {
850 panic("ah_hmac_sha2_256_loop: what?");
853 ctxt
= (SHA256_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
854 SHA256_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
858 ah_hmac_sha2_256_result(struct ah_algorithm_state
*state
,
862 u_char digest
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
867 if (!state
|| !state
->foo
) {
868 panic("ah_hmac_sha2_256_result: what?");
871 ipad
= (u_char
*)state
->foo
;
872 opad
= (u_char
*)(ipad
+ 64);
873 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
875 SHA256_Final((u_int8_t
*)digest
, ctxt
);
878 SHA256_Update(ctxt
, opad
, 64);
879 SHA256_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
880 SHA256_Final((u_int8_t
*)digest
, ctxt
);
882 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
884 FREE(state
->foo
, M_TEMP
);
888 ah_sumsiz_sha2_384(struct secasvar
*sav
)
893 // return half the output size (in bytes), as per rfc 4868
894 return 24; // 384/(8*2)
898 ah_hmac_sha2_384_mature(struct secasvar
*sav
)
900 const struct ah_algorithm
*algo
;
902 if (!sav
->key_auth
) {
904 "ah_hmac_sha2_384_mature: no key is given.\n"));
908 algo
= ah_algorithm_lookup(sav
->alg_auth
);
911 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
915 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
916 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
918 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
919 sav
->key_auth
->sadb_key_bits
));
927 ah_hmac_sha2_384_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
932 u_char tk
[SHA384_DIGEST_LENGTH
] __attribute__((aligned(4)));
938 panic("ah_hmac_sha2_384_init: what?");
942 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA384_CTX
),
943 M_TEMP
, M_NOWAIT
| M_ZERO
);
948 ipad
= (u_char
*)state
->foo
;
949 opad
= (u_char
*)(ipad
+ 128);
950 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
952 /* compress the key if necessery */
953 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
954 bzero(tk
, sizeof(tk
));
955 bzero(ctxt
, sizeof(*ctxt
));
957 SHA384_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
958 _KEYLEN(state
->sav
->key_auth
));
959 SHA384_Final(&tk
[0], ctxt
);
961 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
963 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
964 keylen
= _KEYLEN(state
->sav
->key_auth
);
969 bcopy(key
, ipad
, keylen
);
970 bcopy(key
, opad
, keylen
);
971 for (i
= 0; i
< 128; i
++) {
976 bzero(ctxt
, sizeof(*ctxt
));
978 SHA384_Update(ctxt
, ipad
, 128);
984 ah_hmac_sha2_384_loop(struct ah_algorithm_state
*state
,
990 if (!state
|| !state
->foo
) {
991 panic("ah_hmac_sha2_384_loop: what?");
994 ctxt
= (SHA384_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
995 SHA384_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
999 ah_hmac_sha2_384_result(struct ah_algorithm_state
*state
,
1003 u_char digest
[SHA384_DIGEST_LENGTH
];
1008 if (!state
|| !state
->foo
) {
1009 panic("ah_hmac_sha2_384_result: what?");
1012 ipad
= (u_char
*)state
->foo
;
1013 opad
= (u_char
*)(ipad
+ 128);
1014 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
1016 SHA384_Final((u_int8_t
*)digest
, ctxt
);
1019 SHA384_Update(ctxt
, opad
, 128);
1020 SHA384_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
1021 SHA384_Final((u_int8_t
*)digest
, ctxt
);
1023 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
1025 FREE(state
->foo
, M_TEMP
);
1029 ah_sumsiz_sha2_512(struct secasvar
*sav
)
1034 // return half the output size (in bytes), as per rfc 4868
1035 return 32; // 512/(8*2)
1039 ah_hmac_sha2_512_mature(struct secasvar
*sav
)
1041 const struct ah_algorithm
*algo
;
1043 if (!sav
->key_auth
) {
1045 "ah_hmac_sha2_512_mature: no key is given.\n"));
1049 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1052 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1056 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1057 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1059 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1060 sav
->key_auth
->sadb_key_bits
));
1068 ah_hmac_sha2_512_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
1073 u_char tk
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1079 panic("ah_hmac_sha2_512_init: what?");
1083 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA512_CTX
),
1084 M_TEMP
, M_NOWAIT
| M_ZERO
);
1089 ipad
= (u_char
*)state
->foo
;
1090 opad
= (u_char
*)(ipad
+ 128);
1091 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1093 /* compress the key if necessery */
1094 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
1095 bzero(tk
, sizeof(tk
));
1096 bzero(ctxt
, sizeof(*ctxt
));
1098 SHA512_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
1099 _KEYLEN(state
->sav
->key_auth
));
1100 SHA512_Final(&tk
[0], ctxt
);
1102 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
1104 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
1105 keylen
= _KEYLEN(state
->sav
->key_auth
);
1110 bcopy(key
, ipad
, keylen
);
1111 bcopy(key
, opad
, keylen
);
1112 for (i
= 0; i
< 128; i
++) {
1117 bzero(ctxt
, sizeof(*ctxt
));
1119 SHA512_Update(ctxt
, ipad
, 128);
1125 ah_hmac_sha2_512_loop(struct ah_algorithm_state
*state
,
1131 if (!state
|| !state
->foo
) {
1132 panic("ah_hmac_sha2_512_loop: what?");
1135 ctxt
= (SHA512_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
1136 SHA512_Update(ctxt
, (const u_int8_t
*) addr
, (size_t)len
);
1140 ah_hmac_sha2_512_result(struct ah_algorithm_state
*state
,
1144 u_char digest
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1149 if (!state
|| !state
->foo
) {
1150 panic("ah_hmac_sha2_512_result: what?");
1153 ipad
= (u_char
*)state
->foo
;
1154 opad
= (u_char
*)(ipad
+ 128);
1155 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1157 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1160 SHA512_Update(ctxt
, opad
, 128);
1161 SHA512_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
1162 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1164 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
1166 FREE(state
->foo
, M_TEMP
);
1168 #endif /* AH_ALL_CRYPTO */
1170 /*------------------------------------------------------------*/
1173 * go generate the checksum.
1176 ah_update_mbuf(struct mbuf
*m
, int off
, int len
,
1177 const struct ah_algorithm
*algo
,
1178 struct ah_algorithm_state
*algos
)
1183 /* easy case first */
1184 if (off
+ len
<= m
->m_len
) {
1185 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1189 for (n
= m
; n
; n
= n
->m_next
) {
1190 if (off
< n
->m_len
) {
1198 panic("ah_update_mbuf: wrong offset specified");
1201 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1202 if (n
->m_len
== 0) {
1205 if (n
->m_len
- off
< len
) {
1206 tlen
= n
->m_len
- off
;
1211 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1220 * Go generate the checksum. This function won't modify the mbuf chain
1223 * NOTE: the function does not free mbuf on failure.
1224 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1227 ah4_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1228 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1232 size_t advancewidth
;
1233 struct ah_algorithm_state algos
;
1234 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1237 struct mbuf
*n
= NULL
;
1239 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1244 hdrtype
= -1; /*dummy, it is called IPPROTO_IP*/
1248 error
= (algo
->init
)(&algos
, sav
);
1253 advancewidth
= 0; /*safety*/
1258 case -1: /*first one only*/
1261 * copy ip hdr, modify to fit the AH checksum rule,
1262 * then take a checksum.
1267 m_copydata(m
, off
, sizeof(iphdr
), (caddr_t
)&iphdr
);
1269 hlen
= IP_VHL_HL(iphdr
.ip_vhl
) << 2;
1271 hlen
= iphdr
.ip_hl
<< 2;
1274 iphdr
.ip_sum
= htons(0);
1275 if (ip4_ah_cleartos
) {
1278 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1279 (algo
->update
)(&algos
, (caddr_t
)&iphdr
, sizeof(struct ip
));
1281 if (hlen
!= sizeof(struct ip
)) {
1285 if (hlen
> MCLBYTES
) {
1289 MGET(n
, M_DONTWAIT
, MT_DATA
);
1290 if (n
&& hlen
> MLEN
) {
1291 MCLGET(n
, M_DONTWAIT
);
1292 if ((n
->m_flags
& M_EXT
) == 0) {
1301 m_copydata(m
, off
, hlen
, mtod(n
, caddr_t
));
1304 * IP options processing.
1305 * See RFC2402 appendix A.
1307 p
= mtod(n
, u_char
*);
1308 i
= sizeof(struct ip
);
1310 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1311 ipseclog((LOG_ERR
, "ah4_calccksum: "
1312 "invalid IP option\n"));
1316 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1317 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1318 i
+ IPOPT_OLEN
< hlen
) {
1322 "ah4_calccksum: invalid IP option "
1324 p
[i
+ IPOPT_OPTVAL
]));
1330 switch (p
[i
+ IPOPT_OPTVAL
]) {
1336 case IPOPT_SECURITY
: /* 0x82 */
1337 case 0x85: /* Extended security */
1338 case 0x86: /* Commercial security */
1339 case 0x94: /* Router alert */
1340 case 0x95: /* RFC1770 */
1341 l
= p
[i
+ IPOPT_OLEN
];
1348 l
= p
[i
+ IPOPT_OLEN
];
1355 if (l
< 1 || hlen
- i
< l
) {
1358 "ah4_calccksum: invalid IP option "
1359 "(type=%02x len=%02x)\n",
1360 p
[i
+ IPOPT_OPTVAL
],
1361 p
[i
+ IPOPT_OLEN
]));
1368 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
) {
1374 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1375 (algo
->update
)(&algos
, (caddr_t
)p
, hlen
- sizeof(struct ip
));
1381 hdrtype
= (iphdr
.ip_p
) & 0xff;
1382 advancewidth
= hlen
;
1393 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1394 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1396 : sizeof(struct newah
);
1397 siz
= (*algo
->sumsiz
)(sav
);
1398 totlen
= (ah
.ah_len
+ 2) << 2;
1401 * special treatment is necessary for the first one, not others
1404 if (totlen
> m
->m_pkthdr
.len
- off
||
1405 totlen
> MCLBYTES
) {
1409 MGET(n
, M_DONTWAIT
, MT_DATA
);
1410 if (n
&& totlen
> MLEN
) {
1411 MCLGET(n
, M_DONTWAIT
);
1412 if ((n
->m_flags
& M_EXT
) == 0) {
1421 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1423 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1424 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1428 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1432 hdrtype
= ah
.ah_nxt
;
1433 advancewidth
= totlen
;
1438 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1439 advancewidth
= m
->m_pkthdr
.len
- off
;
1443 off
+= advancewidth
;
1444 if (off
< m
->m_pkthdr
.len
) {
1448 if (len
< (*algo
->sumsiz
)(sav
)) {
1453 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1454 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1471 * Go generate the checksum. This function won't modify the mbuf chain
1474 * NOTE: the function does not free mbuf on failure.
1475 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1478 ah6_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1479 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1483 struct mbuf
*n
= NULL
;
1486 struct ah_algorithm_state algos
;
1487 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1489 if ((m
->m_flags
& M_PKTHDR
) == 0) {
1493 error
= (algo
->init
)(&algos
, sav
);
1499 proto
= IPPROTO_IPV6
;
1504 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1506 newoff
= m
->m_pkthdr
.len
;
1507 } else if (newoff
<= off
) {
1510 } else if (m
->m_pkthdr
.len
< newoff
) {
1518 * special treatment is necessary for the first one, not others
1521 struct ip6_hdr ip6copy
;
1523 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1528 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1530 ip6copy
.ip6_flow
= 0;
1531 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1532 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1533 ip6copy
.ip6_hlim
= 0;
1534 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
)) {
1535 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1537 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
)) {
1538 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1540 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1541 sizeof(struct ip6_hdr
));
1543 newoff
= m
->m_pkthdr
.len
;
1544 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1554 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1556 : sizeof(struct newah
);
1557 siz
= (*algo
->sumsiz
)(sav
);
1560 * special treatment is necessary for the first one, not others
1563 if (newoff
- off
> MCLBYTES
) {
1567 MGET(n
, M_DONTWAIT
, MT_DATA
);
1568 if (n
&& newoff
- off
> MLEN
) {
1569 MCLGET(n
, M_DONTWAIT
);
1570 if ((n
->m_flags
& M_EXT
) == 0) {
1579 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1580 n
->m_len
= newoff
- off
;
1581 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1582 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1586 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1592 case IPPROTO_HOPOPTS
:
1593 case IPPROTO_DSTOPTS
:
1595 struct ip6_ext
*ip6e
;
1597 u_int8_t
*p
, *optend
, *optp
;
1599 if (newoff
- off
> MCLBYTES
) {
1603 MGET(n
, M_DONTWAIT
, MT_DATA
);
1604 if (n
&& newoff
- off
> MLEN
) {
1605 MCLGET(n
, M_DONTWAIT
);
1606 if ((n
->m_flags
& M_EXT
) == 0) {
1615 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1616 n
->m_len
= newoff
- off
;
1618 ip6e
= mtod(n
, struct ip6_ext
*);
1619 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1620 if (newoff
- off
< hdrlen
) {
1626 p
= mtod(n
, u_int8_t
*);
1627 optend
= p
+ hdrlen
;
1630 * ICV calculation for the options header including all
1631 * options. This part is a little tricky since there are
1632 * two type of options; mutable and immutable. We try to
1633 * null-out mutable ones here.
1636 while (optp
< optend
) {
1637 if (optp
[0] == IP6OPT_PAD1
) {
1640 if (optp
+ 2 > optend
) {
1646 optlen
= optp
[1] + 2;
1647 if (optp
+ optlen
> optend
) {
1654 if (optp
[0] & IP6OPT_MUTABLE
) {
1655 bzero(optp
+ 2, optlen
- 2);
1662 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1668 case IPPROTO_ROUTING
:
1670 * For an input packet, we can just calculate `as is'.
1671 * For an output packet, we assume ip6_output have already
1672 * made packet how it will be received at the final
1678 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1682 if (newoff
< m
->m_pkthdr
.len
) {
1688 if (len
< (*algo
->sumsiz
)(sav
)) {
1693 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1694 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));