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 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 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 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 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 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 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 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 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
)
234 if (sav
->flags
& SADB_X_EXT_OLD
)
241 ah_sumsiz_zero(struct secasvar
*sav
)
249 ah_none_mature(struct secasvar
*sav
)
251 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
253 "ah_none_mature: protocol and algorithm mismatch.\n"));
261 struct ah_algorithm_state
*state
,
262 __unused
struct secasvar
*sav
)
270 __unused
struct ah_algorithm_state
*state
,
271 __unused caddr_t addr
,
278 __unused
struct ah_algorithm_state
*state
,
279 __unused caddr_t addr
,
286 __unused
struct secasvar
*sav
)
288 /* anything is okay */
293 ah_keyed_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
297 u_int8_t buf
[32] __attribute__((aligned(4)));
300 panic("ah_keyed_md5_init: what?");
303 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
304 if (state
->foo
== NULL
)
307 MD5Init((MD5_CTX
*)state
->foo
);
309 MD5Update((MD5_CTX
*)state
->foo
,
310 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
311 (u_int
)_KEYLEN(state
->sav
->key_auth
));
315 * We cannot simply use md5_pad() since the function
316 * won't update the total length.
318 if (_KEYLEN(state
->sav
->key_auth
) < 56)
319 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
321 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
322 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
326 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
329 bzero(buf
, sizeof(buf
));
330 while (sizeof(buf
) < padlen
) {
331 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
332 padlen
-= sizeof(buf
);
335 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
338 buf
[0] = (keybitlen
>> 0) & 0xff;
339 buf
[1] = (keybitlen
>> 8) & 0xff;
340 buf
[2] = (keybitlen
>> 16) & 0xff;
341 buf
[3] = (keybitlen
>> 24) & 0xff;
342 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
349 ah_keyed_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
352 panic("ah_keyed_md5_loop: what?");
354 MD5Update((MD5_CTX
*)state
->foo
, addr
, 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?");
366 MD5Update((MD5_CTX
*)state
->foo
,
367 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
368 (u_int
)_KEYLEN(state
->sav
->key_auth
));
370 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
371 FREE(state
->foo
, M_TEMP
);
372 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
376 ah_keyed_sha1_mature(struct secasvar
*sav
)
378 const struct ah_algorithm
*algo
;
380 if (!sav
->key_auth
) {
381 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
385 algo
= ah_algorithm_lookup(sav
->alg_auth
);
387 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
391 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
392 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
394 "ah_keyed_sha1_mature: invalid key length %d.\n",
395 sav
->key_auth
->sadb_key_bits
));
403 ah_keyed_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
408 u_int8_t buf
[32] __attribute__((aligned(4)));
411 panic("ah_keyed_sha1_init: what?");
414 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_NOWAIT
);
418 ctxt
= (SHA1_CTX
*)state
->foo
;
422 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
423 (u_int
)_KEYLEN(state
->sav
->key_auth
));
428 if (_KEYLEN(state
->sav
->key_auth
) < 56)
429 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
431 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
432 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
436 SHA1Update(ctxt
, &buf
[0], 1);
439 bzero(buf
, sizeof(buf
));
440 while (sizeof(buf
) < padlen
) {
441 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
442 padlen
-= sizeof(buf
);
445 SHA1Update(ctxt
, &buf
[0], padlen
);
448 buf
[0] = (keybitlen
>> 0) & 0xff;
449 buf
[1] = (keybitlen
>> 8) & 0xff;
450 buf
[2] = (keybitlen
>> 16) & 0xff;
451 buf
[3] = (keybitlen
>> 24) & 0xff;
452 SHA1Update(ctxt
, buf
, 8);
459 ah_keyed_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
463 if (!state
|| !state
->foo
)
464 panic("ah_keyed_sha1_loop: what?");
465 ctxt
= (SHA1_CTX
*)state
->foo
;
467 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
471 ah_keyed_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
473 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
476 if (!state
|| !state
->foo
)
477 panic("ah_keyed_sha1_result: what?");
478 ctxt
= (SHA1_CTX
*)state
->foo
;
481 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
482 (u_int
)_KEYLEN(state
->sav
->key_auth
));
484 SHA1Final((caddr_t
)&digest
[0], ctxt
);
485 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
487 FREE(state
->foo
, M_TEMP
);
491 ah_hmac_md5_mature(struct secasvar
*sav
)
493 const struct ah_algorithm
*algo
;
495 if (!sav
->key_auth
) {
496 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
500 algo
= ah_algorithm_lookup(sav
->alg_auth
);
502 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
506 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
507 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
509 "ah_hmac_md5_mature: invalid key length %d.\n",
510 sav
->key_auth
->sadb_key_bits
));
518 ah_hmac_md5_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
522 u_char tk
[16] __attribute__((aligned(4)));
529 panic("ah_hmac_md5_init: what?");
532 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_NOWAIT
);
536 ipad
= (u_char
*)state
->foo
;
537 opad
= (u_char
*)(ipad
+ 64);
538 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
540 /* compress the key if necessery */
541 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
543 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
544 _KEYLEN(state
->sav
->key_auth
));
545 MD5Final(&tk
[0], ctxt
);
549 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
550 keylen
= _KEYLEN(state
->sav
->key_auth
);
555 bcopy(key
, ipad
, keylen
);
556 bcopy(key
, opad
, keylen
);
557 for (i
= 0; i
< 64; i
++) {
563 MD5Update(ctxt
, ipad
, 64);
569 ah_hmac_md5_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
573 if (!state
|| !state
->foo
)
574 panic("ah_hmac_md5_loop: what?");
575 ctxt
= (MD5_CTX
*)(void *)(((caddr_t
)state
->foo
) + 128);
576 MD5Update(ctxt
, addr
, len
);
580 ah_hmac_md5_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
582 u_char digest
[16] __attribute__((aligned(4)));
587 if (!state
|| !state
->foo
)
588 panic("ah_hmac_md5_result: what?");
590 ipad
= (u_char
*)state
->foo
;
591 opad
= (u_char
*)(ipad
+ 64);
592 ctxt
= (MD5_CTX
*)(void *)(opad
+ 64);
594 MD5Final(&digest
[0], ctxt
);
597 MD5Update(ctxt
, opad
, 64);
598 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
599 MD5Final(&digest
[0], ctxt
);
601 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
603 FREE(state
->foo
, M_TEMP
);
607 ah_hmac_sha1_mature(struct secasvar
*sav
)
609 const struct ah_algorithm
*algo
;
611 if (!sav
->key_auth
) {
612 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
616 algo
= ah_algorithm_lookup(sav
->alg_auth
);
618 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
622 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
623 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
625 "ah_hmac_sha1_mature: invalid key length %d.\n",
626 sav
->key_auth
->sadb_key_bits
));
634 ah_hmac_sha1_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
639 u_char tk
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
645 panic("ah_hmac_sha1_init: what?");
648 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
653 ipad
= (u_char
*)state
->foo
;
654 opad
= (u_char
*)(ipad
+ 64);
655 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
657 /* compress the key if necessery */
658 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
660 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
661 _KEYLEN(state
->sav
->key_auth
));
662 SHA1Final(&tk
[0], ctxt
);
664 keylen
= SHA1_RESULTLEN
;
666 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
667 keylen
= _KEYLEN(state
->sav
->key_auth
);
672 bcopy(key
, ipad
, keylen
);
673 bcopy(key
, opad
, keylen
);
674 for (i
= 0; i
< 64; i
++) {
680 SHA1Update(ctxt
, ipad
, 64);
686 ah_hmac_sha1_loop(struct ah_algorithm_state
*state
, caddr_t addr
, size_t len
)
690 if (!state
|| !state
->foo
)
691 panic("ah_hmac_sha1_loop: what?");
693 ctxt
= (SHA1_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
694 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
698 ah_hmac_sha1_result(struct ah_algorithm_state
*state
, caddr_t addr
, size_t l
)
700 u_char digest
[SHA1_RESULTLEN
] __attribute__((aligned(4))); /* SHA-1 generates 160 bits */
705 if (!state
|| !state
->foo
)
706 panic("ah_hmac_sha1_result: what?");
708 ipad
= (u_char
*)state
->foo
;
709 opad
= (u_char
*)(ipad
+ 64);
710 ctxt
= (SHA1_CTX
*)(void *)(opad
+ 64);
712 SHA1Final((caddr_t
)&digest
[0], ctxt
);
715 SHA1Update(ctxt
, opad
, 64);
716 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
717 SHA1Final((caddr_t
)&digest
[0], ctxt
);
719 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
721 FREE(state
->foo
, M_TEMP
);
726 ah_sumsiz_sha2_256(struct secasvar
*sav
)
730 // return half the output size (in bytes), as per rfc 4868
731 return 16; // 256/(8*2)
735 ah_hmac_sha2_256_mature(struct secasvar
*sav
)
737 const struct ah_algorithm
*algo
;
739 if (!sav
->key_auth
) {
741 "ah_hmac_sha2_256_mature: no key is given.\n"));
745 algo
= ah_algorithm_lookup(sav
->alg_auth
);
748 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
752 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
753 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
755 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
756 sav
->key_auth
->sadb_key_bits
));
764 ah_hmac_sha2_256_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
769 u_char tk
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
775 panic("ah_hmac_sha2_256_init: what?");
778 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
783 ipad
= (u_char
*)state
->foo
;
784 opad
= (u_char
*)(ipad
+ 64);
785 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
787 /* compress the key if necessery */
788 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
789 bzero(tk
, sizeof(tk
));
790 bzero(ctxt
, sizeof(*ctxt
));
792 SHA256_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
793 _KEYLEN(state
->sav
->key_auth
));
794 SHA256_Final(&tk
[0], ctxt
);
796 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
798 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
799 keylen
= _KEYLEN(state
->sav
->key_auth
);
804 bcopy(key
, ipad
, keylen
);
805 bcopy(key
, opad
, keylen
);
806 for (i
= 0; i
< 64; i
++) {
811 bzero(ctxt
, sizeof(*ctxt
));
813 SHA256_Update(ctxt
, ipad
, 64);
819 ah_hmac_sha2_256_loop(struct ah_algorithm_state
*state
,
825 if (!state
|| !state
->foo
)
826 panic("ah_hmac_sha2_256_loop: what?");
828 ctxt
= (SHA256_CTX
*)(void *)(((u_char
*)state
->foo
) + 128);
829 SHA256_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
833 ah_hmac_sha2_256_result(struct ah_algorithm_state
*state
,
837 u_char digest
[SHA256_DIGEST_LENGTH
] __attribute__((aligned(4)));
842 if (!state
|| !state
->foo
)
843 panic("ah_hmac_sha2_256_result: what?");
845 ipad
= (u_char
*)state
->foo
;
846 opad
= (u_char
*)(ipad
+ 64);
847 ctxt
= (SHA256_CTX
*)(void *)(opad
+ 64);
849 SHA256_Final((u_int8_t
*)digest
, ctxt
);
852 SHA256_Update(ctxt
, opad
, 64);
853 SHA256_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
854 SHA256_Final((u_int8_t
*)digest
, ctxt
);
856 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
858 FREE(state
->foo
, M_TEMP
);
862 ah_sumsiz_sha2_384(struct secasvar
*sav
)
866 // return half the output size (in bytes), as per rfc 4868
867 return 24; // 384/(8*2)
871 ah_hmac_sha2_384_mature(struct secasvar
*sav
)
873 const struct ah_algorithm
*algo
;
875 if (!sav
->key_auth
) {
877 "ah_hmac_sha2_384_mature: no key is given.\n"));
881 algo
= ah_algorithm_lookup(sav
->alg_auth
);
884 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
888 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
889 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
891 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
892 sav
->key_auth
->sadb_key_bits
));
900 ah_hmac_sha2_384_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
905 u_char tk
[SHA384_DIGEST_LENGTH
] __attribute__((aligned(4)));
911 panic("ah_hmac_sha2_384_init: what?");
914 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA384_CTX
),
915 M_TEMP
, M_NOWAIT
| M_ZERO
);
919 ipad
= (u_char
*)state
->foo
;
920 opad
= (u_char
*)(ipad
+ 128);
921 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
923 /* compress the key if necessery */
924 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
925 bzero(tk
, sizeof(tk
));
926 bzero(ctxt
, sizeof(*ctxt
));
928 SHA384_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
929 _KEYLEN(state
->sav
->key_auth
));
930 SHA384_Final(&tk
[0], ctxt
);
932 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
934 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
935 keylen
= _KEYLEN(state
->sav
->key_auth
);
940 bcopy(key
, ipad
, keylen
);
941 bcopy(key
, opad
, keylen
);
942 for (i
= 0; i
< 128; i
++) {
947 bzero(ctxt
, sizeof(*ctxt
));
949 SHA384_Update(ctxt
, ipad
, 128);
955 ah_hmac_sha2_384_loop(struct ah_algorithm_state
*state
,
961 if (!state
|| !state
->foo
)
962 panic("ah_hmac_sha2_384_loop: what?");
964 ctxt
= (SHA384_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
965 SHA384_Update(ctxt
, (const u_int8_t
*)addr
, (size_t)len
);
969 ah_hmac_sha2_384_result(struct ah_algorithm_state
*state
,
973 u_char digest
[SHA384_DIGEST_LENGTH
];
978 if (!state
|| !state
->foo
)
979 panic("ah_hmac_sha2_384_result: what?");
981 ipad
= (u_char
*)state
->foo
;
982 opad
= (u_char
*)(ipad
+ 128);
983 ctxt
= (SHA384_CTX
*)(void *)(opad
+ 128);
985 SHA384_Final((u_int8_t
*)digest
, ctxt
);
988 SHA384_Update(ctxt
, opad
, 128);
989 SHA384_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
990 SHA384_Final((u_int8_t
*)digest
, ctxt
);
992 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
994 FREE(state
->foo
, M_TEMP
);
998 ah_sumsiz_sha2_512(struct secasvar
*sav
)
1002 // return half the output size (in bytes), as per rfc 4868
1003 return 32; // 512/(8*2)
1007 ah_hmac_sha2_512_mature(struct secasvar
*sav
)
1009 const struct ah_algorithm
*algo
;
1011 if (!sav
->key_auth
) {
1013 "ah_hmac_sha2_512_mature: no key is given.\n"));
1017 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1020 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
1024 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1025 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1027 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1028 sav
->key_auth
->sadb_key_bits
));
1036 ah_hmac_sha2_512_init(struct ah_algorithm_state
*state
, struct secasvar
*sav
)
1041 u_char tk
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1047 panic("ah_hmac_sha2_512_init: what?");
1050 state
->foo
= (void *)_MALLOC(128 + 128 + sizeof(SHA512_CTX
),
1051 M_TEMP
, M_NOWAIT
| M_ZERO
);
1055 ipad
= (u_char
*)state
->foo
;
1056 opad
= (u_char
*)(ipad
+ 128);
1057 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1059 /* compress the key if necessery */
1060 if (128 < _KEYLEN(state
->sav
->key_auth
)) {
1061 bzero(tk
, sizeof(tk
));
1062 bzero(ctxt
, sizeof(*ctxt
));
1064 SHA512_Update(ctxt
, (const u_int8_t
*) _KEYBUF(state
->sav
->key_auth
),
1065 _KEYLEN(state
->sav
->key_auth
));
1066 SHA512_Final(&tk
[0], ctxt
);
1068 keylen
= sizeof(tk
) < 128 ? sizeof(tk
) : 128;
1070 key
= (u_char
*) _KEYBUF(state
->sav
->key_auth
);
1071 keylen
= _KEYLEN(state
->sav
->key_auth
);
1076 bcopy(key
, ipad
, keylen
);
1077 bcopy(key
, opad
, keylen
);
1078 for (i
= 0; i
< 128; i
++) {
1083 bzero(ctxt
, sizeof(*ctxt
));
1085 SHA512_Update(ctxt
, ipad
, 128);
1091 ah_hmac_sha2_512_loop(struct ah_algorithm_state
*state
,
1097 if (!state
|| !state
->foo
)
1098 panic("ah_hmac_sha2_512_loop: what?");
1100 ctxt
= (SHA512_CTX
*)(void *)(((u_char
*)state
->foo
) + 256);
1101 SHA512_Update(ctxt
, (const u_int8_t
*) addr
, (size_t)len
);
1105 ah_hmac_sha2_512_result(struct ah_algorithm_state
*state
,
1109 u_char digest
[SHA512_DIGEST_LENGTH
] __attribute__((aligned(4)));
1114 if (!state
|| !state
->foo
)
1115 panic("ah_hmac_sha2_512_result: what?");
1117 ipad
= (u_char
*)state
->foo
;
1118 opad
= (u_char
*)(ipad
+ 128);
1119 ctxt
= (SHA512_CTX
*)(void *)(opad
+ 128);
1121 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1124 SHA512_Update(ctxt
, opad
, 128);
1125 SHA512_Update(ctxt
, (const u_int8_t
*)digest
, sizeof(digest
));
1126 SHA512_Final((u_int8_t
*)digest
, ctxt
);
1128 bcopy(&digest
[0], (void *)addr
, sizeof(digest
) > l
? l
: sizeof(digest
));
1130 FREE(state
->foo
, M_TEMP
);
1132 #endif /* AH_ALL_CRYPTO */
1134 /*------------------------------------------------------------*/
1137 * go generate the checksum.
1140 ah_update_mbuf(struct mbuf
*m
,int off
, int len
,
1141 const struct ah_algorithm
*algo
,
1142 struct ah_algorithm_state
*algos
)
1147 /* easy case first */
1148 if (off
+ len
<= m
->m_len
) {
1149 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1153 for (n
= m
; n
; n
= n
->m_next
) {
1161 panic("ah_update_mbuf: wrong offset specified");
1163 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1166 if (n
->m_len
- off
< len
)
1167 tlen
= n
->m_len
- off
;
1171 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1180 * Go generate the checksum. This function won't modify the mbuf chain
1183 * NOTE: the function does not free mbuf on failure.
1184 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1187 ah4_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1188 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1192 size_t advancewidth
;
1193 struct ah_algorithm_state algos
;
1194 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
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
)
1327 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1328 (algo
->update
)(&algos
, (caddr_t
)p
, hlen
- sizeof(struct ip
));
1334 hdrtype
= (iphdr
.ip_p
) & 0xff;
1335 advancewidth
= hlen
;
1346 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1347 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1349 : sizeof(struct newah
);
1350 siz
= (*algo
->sumsiz
)(sav
);
1351 totlen
= (ah
.ah_len
+ 2) << 2;
1354 * special treatment is necessary for the first one, not others
1357 if (totlen
> m
->m_pkthdr
.len
- off
||
1358 totlen
> MCLBYTES
) {
1362 MGET(n
, M_DONTWAIT
, MT_DATA
);
1363 if (n
&& totlen
> MLEN
) {
1364 MCLGET(n
, M_DONTWAIT
);
1365 if ((n
->m_flags
& M_EXT
) == 0) {
1374 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1376 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1377 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1381 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1384 hdrtype
= ah
.ah_nxt
;
1385 advancewidth
= totlen
;
1390 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1391 advancewidth
= m
->m_pkthdr
.len
- off
;
1395 off
+= advancewidth
;
1396 if (off
< m
->m_pkthdr
.len
)
1399 if (len
< (*algo
->sumsiz
)(sav
)) {
1404 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1405 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1420 * Go generate the checksum. This function won't modify the mbuf chain
1423 * NOTE: the function does not free mbuf on failure.
1424 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1427 ah6_calccksum(struct mbuf
*m
, caddr_t ahdat
, size_t len
,
1428 const struct ah_algorithm
*algo
, struct secasvar
*sav
)
1432 struct mbuf
*n
= NULL
;
1435 struct ah_algorithm_state algos
;
1436 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1438 if ((m
->m_flags
& M_PKTHDR
) == 0)
1441 error
= (algo
->init
)(&algos
, sav
);
1446 proto
= IPPROTO_IPV6
;
1451 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1453 newoff
= m
->m_pkthdr
.len
;
1454 else if (newoff
<= off
) {
1462 * special treatment is necessary for the first one, not others
1465 struct ip6_hdr ip6copy
;
1467 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1472 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1474 ip6copy
.ip6_flow
= 0;
1475 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1476 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1477 ip6copy
.ip6_hlim
= 0;
1478 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
))
1479 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1480 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
))
1481 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1482 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1483 sizeof(struct ip6_hdr
));
1485 newoff
= m
->m_pkthdr
.len
;
1486 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1496 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1498 : sizeof(struct newah
);
1499 siz
= (*algo
->sumsiz
)(sav
);
1502 * special treatment is necessary for the first one, not others
1505 if (newoff
- off
> MCLBYTES
) {
1509 MGET(n
, M_DONTWAIT
, MT_DATA
);
1510 if (n
&& newoff
- off
> MLEN
) {
1511 MCLGET(n
, M_DONTWAIT
);
1512 if ((n
->m_flags
& M_EXT
) == 0) {
1521 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1522 n
->m_len
= newoff
- off
;
1523 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1524 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1528 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1533 case IPPROTO_HOPOPTS
:
1534 case IPPROTO_DSTOPTS
:
1536 struct ip6_ext
*ip6e
;
1538 u_int8_t
*p
, *optend
, *optp
;
1540 if (newoff
- off
> MCLBYTES
) {
1544 MGET(n
, M_DONTWAIT
, MT_DATA
);
1545 if (n
&& newoff
- off
> MLEN
) {
1546 MCLGET(n
, M_DONTWAIT
);
1547 if ((n
->m_flags
& M_EXT
) == 0) {
1556 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1557 n
->m_len
= newoff
- off
;
1559 ip6e
= mtod(n
, struct ip6_ext
*);
1560 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1561 if (newoff
- off
< hdrlen
) {
1567 p
= mtod(n
, u_int8_t
*);
1568 optend
= p
+ hdrlen
;
1571 * ICV calculation for the options header including all
1572 * options. This part is a little tricky since there are
1573 * two type of options; mutable and immutable. We try to
1574 * null-out mutable ones here.
1577 while (optp
< optend
) {
1578 if (optp
[0] == IP6OPT_PAD1
)
1581 if (optp
+ 2 > optend
) {
1587 optlen
= optp
[1] + 2;
1589 if (optp
[0] & IP6OPT_MUTABLE
)
1590 bzero(optp
+ 2, optlen
- 2);
1596 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1602 case IPPROTO_ROUTING
:
1604 * For an input packet, we can just calculate `as is'.
1605 * For an output packet, we assume ip6_output have already
1606 * made packet how it will be received at the final
1612 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1616 if (newoff
< m
->m_pkthdr
.len
) {
1622 if (len
< (*algo
->sumsiz
)(sav
)) {
1627 (algo
->result
)(&algos
, (caddr_t
) &sumbuf
[0], sizeof(sumbuf
));
1628 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));