1 /* $FreeBSD: src/sys/netinet6/ah_core.c,v 1.2.2.4 2001/07/03 11:01:49 ume Exp $ */
2 /* $KAME: ah_core.c,v 1.44 2001/03/12 11:24:39 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * RFC1826/2402 authentication header.
37 /* TODO: have shared routines for hmac-* algorithms */
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/errno.h>
49 #include <sys/syslog.h>
52 #include <net/route.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/in_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet/icmp6.h>
65 #include <netinet6/ipsec.h>
67 #include <netinet6/ipsec6.h>
69 #include <netinet6/ah.h>
71 #include <netinet6/ah6.h>
74 #include <netinet6/esp.h>
76 #include <netinet6/esp6.h>
79 #include <net/pfkeyv2.h>
80 #include <netkey/keydb.h>
82 #include <crypto/sha1.h>
83 #include <crypto/sha2/sha2.h>
85 #include <net/net_osdep.h>
89 static int ah_sumsiz_1216(struct secasvar
*);
90 static int ah_sumsiz_zero(struct secasvar
*);
91 static int ah_none_mature(struct secasvar
*);
92 static int ah_none_init(struct ah_algorithm_state
*, struct secasvar
*);
93 static void ah_none_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
94 static void ah_none_result(struct ah_algorithm_state
*, caddr_t
);
95 static int ah_keyed_md5_mature(struct secasvar
*);
96 static int ah_keyed_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
97 static void ah_keyed_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
98 static void ah_keyed_md5_result(struct ah_algorithm_state
*, caddr_t
);
99 static int ah_keyed_sha1_mature(struct secasvar
*);
100 static int ah_keyed_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
101 static void ah_keyed_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
102 static void ah_keyed_sha1_result(struct ah_algorithm_state
*, caddr_t
);
103 static int ah_hmac_md5_mature(struct secasvar
*);
104 static int ah_hmac_md5_init(struct ah_algorithm_state
*, struct secasvar
*);
105 static void ah_hmac_md5_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
106 static void ah_hmac_md5_result(struct ah_algorithm_state
*, caddr_t
);
107 static int ah_hmac_sha1_mature(struct secasvar
*);
108 static int ah_hmac_sha1_init(struct ah_algorithm_state
*, struct secasvar
*);
109 static void ah_hmac_sha1_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
110 static void ah_hmac_sha1_result(struct ah_algorithm_state
*, caddr_t
);
111 static int ah_hmac_sha2_256_mature(struct secasvar
*);
112 static int ah_hmac_sha2_256_init(struct ah_algorithm_state
*,
114 static void ah_hmac_sha2_256_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
115 static void ah_hmac_sha2_256_result(struct ah_algorithm_state
*, caddr_t
);
116 static int ah_hmac_sha2_384_mature(struct secasvar
*);
117 static int ah_hmac_sha2_384_init(struct ah_algorithm_state
*,
119 static void ah_hmac_sha2_384_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
120 static void ah_hmac_sha2_384_result(struct ah_algorithm_state
*, caddr_t
);
121 static int ah_hmac_sha2_512_mature(struct secasvar
*);
122 static int ah_hmac_sha2_512_init(struct ah_algorithm_state
*,
124 static void ah_hmac_sha2_512_loop(struct ah_algorithm_state
*, caddr_t
, size_t);
125 static void ah_hmac_sha2_512_result(struct ah_algorithm_state
*, caddr_t
);
127 static void ah_update_mbuf(struct mbuf
*, int, int,
128 const struct ah_algorithm
*, struct ah_algorithm_state
*);
130 const struct ah_algorithm
*
131 ah_algorithm_lookup(idx
)
134 /* checksum algorithms */
135 static struct ah_algorithm ah_algorithms
[] = {
136 { ah_sumsiz_1216
, ah_hmac_md5_mature
, 128, 128, "hmac-md5",
137 ah_hmac_md5_init
, ah_hmac_md5_loop
,
138 ah_hmac_md5_result
, },
139 { ah_sumsiz_1216
, ah_hmac_sha1_mature
, 160, 160, "hmac-sha1",
140 ah_hmac_sha1_init
, ah_hmac_sha1_loop
,
141 ah_hmac_sha1_result
, },
142 { ah_sumsiz_1216
, ah_keyed_md5_mature
, 128, 128, "keyed-md5",
143 ah_keyed_md5_init
, ah_keyed_md5_loop
,
144 ah_keyed_md5_result
, },
145 { ah_sumsiz_1216
, ah_keyed_sha1_mature
, 160, 160, "keyed-sha1",
146 ah_keyed_sha1_init
, ah_keyed_sha1_loop
,
147 ah_keyed_sha1_result
, },
148 { ah_sumsiz_zero
, ah_none_mature
, 0, 2048, "none",
149 ah_none_init
, ah_none_loop
, ah_none_result
, },
150 { ah_sumsiz_1216
, ah_hmac_sha2_256_mature
, 256, 256,
152 ah_hmac_sha2_256_init
, ah_hmac_sha2_256_loop
,
153 ah_hmac_sha2_256_result
, },
154 { ah_sumsiz_1216
, ah_hmac_sha2_384_mature
, 384, 384,
156 ah_hmac_sha2_384_init
, ah_hmac_sha2_384_loop
,
157 ah_hmac_sha2_384_result
, },
158 { ah_sumsiz_1216
, ah_hmac_sha2_512_mature
, 512, 512,
160 ah_hmac_sha2_512_init
, ah_hmac_sha2_512_loop
,
161 ah_hmac_sha2_512_result
, },
165 case SADB_AALG_MD5HMAC
:
166 return &ah_algorithms
[0];
167 case SADB_AALG_SHA1HMAC
:
168 return &ah_algorithms
[1];
169 case SADB_X_AALG_MD5
:
170 return &ah_algorithms
[2];
171 case SADB_X_AALG_SHA
:
172 return &ah_algorithms
[3];
173 case SADB_X_AALG_NULL
:
174 return &ah_algorithms
[4];
175 case SADB_X_AALG_SHA2_256
:
176 return &ah_algorithms
[5];
177 case SADB_X_AALG_SHA2_384
:
178 return &ah_algorithms
[6];
179 case SADB_X_AALG_SHA2_512
:
180 return &ah_algorithms
[7];
189 struct secasvar
*sav
;
193 if (sav
->flags
& SADB_X_EXT_OLD
)
201 struct secasvar
*sav
;
210 struct secasvar
*sav
;
212 if (sav
->sah
->saidx
.proto
== IPPROTO_AH
) {
214 "ah_none_mature: protocol and algorithm mismatch.\n"));
221 ah_none_init(state
, sav
)
222 struct ah_algorithm_state
*state
;
223 struct secasvar
*sav
;
230 ah_none_loop(state
, addr
, len
)
231 struct ah_algorithm_state
*state
;
238 ah_none_result(state
, addr
)
239 struct ah_algorithm_state
*state
;
245 ah_keyed_md5_mature(sav
)
246 struct secasvar
*sav
;
248 /* anything is okay */
253 ah_keyed_md5_init(state
, sav
)
254 struct ah_algorithm_state
*state
;
255 struct secasvar
*sav
;
262 panic("ah_keyed_md5_init: what?");
265 state
->foo
= (void *)_MALLOC(sizeof(MD5_CTX
), M_TEMP
, M_WAITOK
);
266 if (state
->foo
== NULL
)
269 MD5Init((MD5_CTX
*)state
->foo
);
271 MD5Update((MD5_CTX
*)state
->foo
,
272 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
273 (u_int
)_KEYLEN(state
->sav
->key_auth
));
277 * We cannot simply use md5_pad() since the function
278 * won't update the total length.
280 if (_KEYLEN(state
->sav
->key_auth
) < 56)
281 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
283 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
284 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
288 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], 1);
291 bzero(buf
, sizeof(buf
));
292 while (sizeof(buf
) < padlen
) {
293 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], sizeof(buf
));
294 padlen
-= sizeof(buf
);
297 MD5Update((MD5_CTX
*)state
->foo
, &buf
[0], padlen
);
300 buf
[0] = (keybitlen
>> 0) & 0xff;
301 buf
[1] = (keybitlen
>> 8) & 0xff;
302 buf
[2] = (keybitlen
>> 16) & 0xff;
303 buf
[3] = (keybitlen
>> 24) & 0xff;
304 MD5Update((MD5_CTX
*)state
->foo
, buf
, 8);
311 ah_keyed_md5_loop(state
, addr
, len
)
312 struct ah_algorithm_state
*state
;
317 panic("ah_keyed_md5_loop: what?");
319 MD5Update((MD5_CTX
*)state
->foo
, addr
, len
);
323 ah_keyed_md5_result(state
, addr
)
324 struct ah_algorithm_state
*state
;
330 panic("ah_keyed_md5_result: what?");
333 MD5Update((MD5_CTX
*)state
->foo
,
334 (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
335 (u_int
)_KEYLEN(state
->sav
->key_auth
));
337 MD5Final(&digest
[0], (MD5_CTX
*)state
->foo
);
338 FREE(state
->foo
, M_TEMP
);
339 bcopy(&digest
[0], (void *)addr
, sizeof(digest
));
343 ah_keyed_sha1_mature(sav
)
344 struct secasvar
*sav
;
346 const struct ah_algorithm
*algo
;
348 if (!sav
->key_auth
) {
349 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: no key is given.\n"));
353 algo
= ah_algorithm_lookup(sav
->alg_auth
);
355 ipseclog((LOG_ERR
, "ah_keyed_sha1_mature: unsupported algorithm.\n"));
359 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
360 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
362 "ah_keyed_sha1_mature: invalid key length %d.\n",
363 sav
->key_auth
->sadb_key_bits
));
371 ah_keyed_sha1_init(state
, sav
)
372 struct ah_algorithm_state
*state
;
373 struct secasvar
*sav
;
381 panic("ah_keyed_sha1_init: what?");
384 state
->foo
= (void *)_MALLOC(sizeof(SHA1_CTX
), M_TEMP
, M_WAITOK
);
388 ctxt
= (SHA1_CTX
*)state
->foo
;
392 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
393 (u_int
)_KEYLEN(state
->sav
->key_auth
));
398 if (_KEYLEN(state
->sav
->key_auth
) < 56)
399 padlen
= 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
401 padlen
= 64 + 64 - 8 - _KEYLEN(state
->sav
->key_auth
);
402 keybitlen
= _KEYLEN(state
->sav
->key_auth
);
406 SHA1Update(ctxt
, &buf
[0], 1);
409 bzero(buf
, sizeof(buf
));
410 while (sizeof(buf
) < padlen
) {
411 SHA1Update(ctxt
, &buf
[0], sizeof(buf
));
412 padlen
-= sizeof(buf
);
415 SHA1Update(ctxt
, &buf
[0], padlen
);
418 buf
[0] = (keybitlen
>> 0) & 0xff;
419 buf
[1] = (keybitlen
>> 8) & 0xff;
420 buf
[2] = (keybitlen
>> 16) & 0xff;
421 buf
[3] = (keybitlen
>> 24) & 0xff;
422 SHA1Update(ctxt
, buf
, 8);
429 ah_keyed_sha1_loop(state
, addr
, len
)
430 struct ah_algorithm_state
*state
;
436 if (!state
|| !state
->foo
)
437 panic("ah_keyed_sha1_loop: what?");
438 ctxt
= (SHA1_CTX
*)state
->foo
;
440 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
444 ah_keyed_sha1_result(state
, addr
)
445 struct ah_algorithm_state
*state
;
448 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
451 if (!state
|| !state
->foo
)
452 panic("ah_keyed_sha1_result: what?");
453 ctxt
= (SHA1_CTX
*)state
->foo
;
456 SHA1Update(ctxt
, (u_int8_t
*)_KEYBUF(state
->sav
->key_auth
),
457 (u_int
)_KEYLEN(state
->sav
->key_auth
));
459 SHA1Final((caddr_t
)&digest
[0], ctxt
);
460 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
462 FREE(state
->foo
, M_TEMP
);
466 ah_hmac_md5_mature(sav
)
467 struct secasvar
*sav
;
469 const struct ah_algorithm
*algo
;
471 if (!sav
->key_auth
) {
472 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: no key is given.\n"));
476 algo
= ah_algorithm_lookup(sav
->alg_auth
);
478 ipseclog((LOG_ERR
, "ah_hmac_md5_mature: unsupported algorithm.\n"));
482 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
483 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
485 "ah_hmac_md5_mature: invalid key length %d.\n",
486 sav
->key_auth
->sadb_key_bits
));
494 ah_hmac_md5_init(state
, sav
)
495 struct ah_algorithm_state
*state
;
496 struct secasvar
*sav
;
507 panic("ah_hmac_md5_init: what?");
510 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(MD5_CTX
), M_TEMP
, M_WAITOK
);
514 ipad
= (u_char
*)state
->foo
;
515 opad
= (u_char
*)(ipad
+ 64);
516 ctxt
= (MD5_CTX
*)(opad
+ 64);
518 /* compress the key if necessery */
519 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
521 MD5Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
522 _KEYLEN(state
->sav
->key_auth
));
523 MD5Final(&tk
[0], ctxt
);
527 key
= _KEYBUF(state
->sav
->key_auth
);
528 keylen
= _KEYLEN(state
->sav
->key_auth
);
533 bcopy(key
, ipad
, keylen
);
534 bcopy(key
, opad
, keylen
);
535 for (i
= 0; i
< 64; i
++) {
541 MD5Update(ctxt
, ipad
, 64);
547 ah_hmac_md5_loop(state
, addr
, len
)
548 struct ah_algorithm_state
*state
;
554 if (!state
|| !state
->foo
)
555 panic("ah_hmac_md5_loop: what?");
556 ctxt
= (MD5_CTX
*)(((caddr_t
)state
->foo
) + 128);
557 MD5Update(ctxt
, addr
, len
);
561 ah_hmac_md5_result(state
, addr
)
562 struct ah_algorithm_state
*state
;
570 if (!state
|| !state
->foo
)
571 panic("ah_hmac_md5_result: what?");
573 ipad
= (u_char
*)state
->foo
;
574 opad
= (u_char
*)(ipad
+ 64);
575 ctxt
= (MD5_CTX
*)(opad
+ 64);
577 MD5Final(&digest
[0], ctxt
);
580 MD5Update(ctxt
, opad
, 64);
581 MD5Update(ctxt
, &digest
[0], sizeof(digest
));
582 MD5Final(&digest
[0], ctxt
);
584 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
586 FREE(state
->foo
, M_TEMP
);
590 ah_hmac_sha1_mature(sav
)
591 struct secasvar
*sav
;
593 const struct ah_algorithm
*algo
;
595 if (!sav
->key_auth
) {
596 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: no key is given.\n"));
600 algo
= ah_algorithm_lookup(sav
->alg_auth
);
602 ipseclog((LOG_ERR
, "ah_hmac_sha1_mature: unsupported algorithm.\n"));
606 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
607 || algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
609 "ah_hmac_sha1_mature: invalid key length %d.\n",
610 sav
->key_auth
->sadb_key_bits
));
618 ah_hmac_sha1_init(state
, sav
)
619 struct ah_algorithm_state
*state
;
620 struct secasvar
*sav
;
625 u_char tk
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
631 panic("ah_hmac_sha1_init: what?");
634 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA1_CTX
),
639 ipad
= (u_char
*)state
->foo
;
640 opad
= (u_char
*)(ipad
+ 64);
641 ctxt
= (SHA1_CTX
*)(opad
+ 64);
643 /* compress the key if necessery */
644 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
646 SHA1Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
647 _KEYLEN(state
->sav
->key_auth
));
648 SHA1Final(&tk
[0], ctxt
);
650 keylen
= SHA1_RESULTLEN
;
652 key
= _KEYBUF(state
->sav
->key_auth
);
653 keylen
= _KEYLEN(state
->sav
->key_auth
);
658 bcopy(key
, ipad
, keylen
);
659 bcopy(key
, opad
, keylen
);
660 for (i
= 0; i
< 64; i
++) {
666 SHA1Update(ctxt
, ipad
, 64);
672 ah_hmac_sha1_loop(state
, addr
, len
)
673 struct ah_algorithm_state
*state
;
679 if (!state
|| !state
->foo
)
680 panic("ah_hmac_sha1_loop: what?");
682 ctxt
= (SHA1_CTX
*)(((u_char
*)state
->foo
) + 128);
683 SHA1Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
687 ah_hmac_sha1_result(state
, addr
)
688 struct ah_algorithm_state
*state
;
691 u_char digest
[SHA1_RESULTLEN
]; /* SHA-1 generates 160 bits */
696 if (!state
|| !state
->foo
)
697 panic("ah_hmac_sha1_result: what?");
699 ipad
= (u_char
*)state
->foo
;
700 opad
= (u_char
*)(ipad
+ 64);
701 ctxt
= (SHA1_CTX
*)(opad
+ 64);
703 SHA1Final((caddr_t
)&digest
[0], ctxt
);
706 SHA1Update(ctxt
, opad
, 64);
707 SHA1Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
708 SHA1Final((caddr_t
)&digest
[0], ctxt
);
710 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
712 FREE(state
->foo
, M_TEMP
);
716 ah_hmac_sha2_256_mature(sav
)
717 struct secasvar
*sav
;
719 const struct ah_algorithm
*algo
;
721 if (!sav
->key_auth
) {
723 "ah_hmac_sha2_256_mature: no key is given.\n"));
727 algo
= ah_algorithm_lookup(sav
->alg_auth
);
730 "ah_hmac_sha2_256_mature: unsupported algorithm.\n"));
734 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
735 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
737 "ah_hmac_sha2_256_mature: invalid key length %d.\n",
738 sav
->key_auth
->sadb_key_bits
));
746 ah_hmac_sha2_256_init(state
, sav
)
747 struct ah_algorithm_state
*state
;
748 struct secasvar
*sav
;
753 u_char tk
[SHA256_DIGEST_LENGTH
];
759 panic("ah_hmac_sha2_256_init: what?");
762 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA256_CTX
),
767 ipad
= (u_char
*)state
->foo
;
768 opad
= (u_char
*)(ipad
+ 64);
769 ctxt
= (SHA256_CTX
*)(opad
+ 64);
771 /* compress the key if necessery */
772 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
773 bzero(tk
, sizeof(tk
));
774 bzero(ctxt
, sizeof(*ctxt
));
776 SHA256_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
777 _KEYLEN(state
->sav
->key_auth
));
778 SHA256_Final(&tk
[0], ctxt
);
780 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
782 key
= _KEYBUF(state
->sav
->key_auth
);
783 keylen
= _KEYLEN(state
->sav
->key_auth
);
788 bcopy(key
, ipad
, keylen
);
789 bcopy(key
, opad
, keylen
);
790 for (i
= 0; i
< 64; i
++) {
795 bzero(ctxt
, sizeof(*ctxt
));
797 SHA256_Update(ctxt
, ipad
, 64);
803 ah_hmac_sha2_256_loop(state
, addr
, len
)
804 struct ah_algorithm_state
*state
;
810 if (!state
|| !state
->foo
)
811 panic("ah_hmac_sha2_256_loop: what?");
813 ctxt
= (SHA256_CTX
*)(((u_char
*)state
->foo
) + 128);
814 SHA256_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
818 ah_hmac_sha2_256_result(state
, addr
)
819 struct ah_algorithm_state
*state
;
822 u_char digest
[SHA256_DIGEST_LENGTH
];
827 if (!state
|| !state
->foo
)
828 panic("ah_hmac_sha2_256_result: what?");
830 ipad
= (u_char
*)state
->foo
;
831 opad
= (u_char
*)(ipad
+ 64);
832 ctxt
= (SHA256_CTX
*)(opad
+ 64);
834 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
836 bzero(ctxt
, sizeof(*ctxt
));
838 SHA256_Update(ctxt
, opad
, 64);
839 SHA256_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
840 SHA256_Final((caddr_t
)&digest
[0], ctxt
);
842 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
844 FREE(state
->foo
, M_TEMP
);
848 ah_hmac_sha2_384_mature(sav
)
849 struct secasvar
*sav
;
851 const struct ah_algorithm
*algo
;
853 if (!sav
->key_auth
) {
855 "ah_hmac_sha2_384_mature: no key is given.\n"));
859 algo
= ah_algorithm_lookup(sav
->alg_auth
);
862 "ah_hmac_sha2_384_mature: unsupported algorithm.\n"));
866 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
867 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
869 "ah_hmac_sha2_384_mature: invalid key length %d.\n",
870 sav
->key_auth
->sadb_key_bits
));
878 ah_hmac_sha2_384_init(state
, sav
)
879 struct ah_algorithm_state
*state
;
880 struct secasvar
*sav
;
885 u_char tk
[SHA384_DIGEST_LENGTH
];
891 panic("ah_hmac_sha2_384_init: what?");
894 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA384_CTX
),
898 bzero(state
->foo
, 64 + 64 + sizeof(SHA384_CTX
));
900 ipad
= (u_char
*)state
->foo
;
901 opad
= (u_char
*)(ipad
+ 64);
902 ctxt
= (SHA384_CTX
*)(opad
+ 64);
904 /* compress the key if necessery */
905 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
906 bzero(tk
, sizeof(tk
));
907 bzero(ctxt
, sizeof(*ctxt
));
909 SHA384_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
910 _KEYLEN(state
->sav
->key_auth
));
911 SHA384_Final(&tk
[0], ctxt
);
913 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
915 key
= _KEYBUF(state
->sav
->key_auth
);
916 keylen
= _KEYLEN(state
->sav
->key_auth
);
921 bcopy(key
, ipad
, keylen
);
922 bcopy(key
, opad
, keylen
);
923 for (i
= 0; i
< 64; i
++) {
928 bzero(ctxt
, sizeof(*ctxt
));
930 SHA384_Update(ctxt
, ipad
, 64);
936 ah_hmac_sha2_384_loop(state
, addr
, len
)
937 struct ah_algorithm_state
*state
;
943 if (!state
|| !state
->foo
)
944 panic("ah_hmac_sha2_384_loop: what?");
946 ctxt
= (SHA384_CTX
*)(((u_char
*)state
->foo
) + 128);
947 SHA384_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
951 ah_hmac_sha2_384_result(state
, addr
)
952 struct ah_algorithm_state
*state
;
955 u_char digest
[SHA384_DIGEST_LENGTH
];
960 if (!state
|| !state
->foo
)
961 panic("ah_hmac_sha2_384_result: what?");
963 ipad
= (u_char
*)state
->foo
;
964 opad
= (u_char
*)(ipad
+ 64);
965 ctxt
= (SHA384_CTX
*)(opad
+ 64);
967 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
969 bzero(ctxt
, sizeof(*ctxt
));
971 SHA384_Update(ctxt
, opad
, 64);
972 SHA384_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
973 SHA384_Final((caddr_t
)&digest
[0], ctxt
);
975 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
977 FREE(state
->foo
, M_TEMP
);
981 ah_hmac_sha2_512_mature(sav
)
982 struct secasvar
*sav
;
984 const struct ah_algorithm
*algo
;
986 if (!sav
->key_auth
) {
988 "ah_hmac_sha2_512_mature: no key is given.\n"));
992 algo
= ah_algorithm_lookup(sav
->alg_auth
);
995 "ah_hmac_sha2_512_mature: unsupported algorithm.\n"));
999 if (sav
->key_auth
->sadb_key_bits
< algo
->keymin
||
1000 algo
->keymax
< sav
->key_auth
->sadb_key_bits
) {
1002 "ah_hmac_sha2_512_mature: invalid key length %d.\n",
1003 sav
->key_auth
->sadb_key_bits
));
1011 ah_hmac_sha2_512_init(state
, sav
)
1012 struct ah_algorithm_state
*state
;
1013 struct secasvar
*sav
;
1018 u_char tk
[SHA512_DIGEST_LENGTH
];
1024 panic("ah_hmac_sha2_512_init: what?");
1027 state
->foo
= (void *)_MALLOC(64 + 64 + sizeof(SHA512_CTX
),
1031 bzero(state
->foo
, 64 + 64 + sizeof(SHA512_CTX
));
1033 ipad
= (u_char
*)state
->foo
;
1034 opad
= (u_char
*)(ipad
+ 64);
1035 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1037 /* compress the key if necessery */
1038 if (64 < _KEYLEN(state
->sav
->key_auth
)) {
1039 bzero(tk
, sizeof(tk
));
1040 bzero(ctxt
, sizeof(*ctxt
));
1042 SHA512_Update(ctxt
, _KEYBUF(state
->sav
->key_auth
),
1043 _KEYLEN(state
->sav
->key_auth
));
1044 SHA512_Final(&tk
[0], ctxt
);
1046 keylen
= sizeof(tk
) < 64 ? sizeof(tk
) : 64;
1048 key
= _KEYBUF(state
->sav
->key_auth
);
1049 keylen
= _KEYLEN(state
->sav
->key_auth
);
1054 bcopy(key
, ipad
, keylen
);
1055 bcopy(key
, opad
, keylen
);
1056 for (i
= 0; i
< 64; i
++) {
1061 bzero(ctxt
, sizeof(*ctxt
));
1063 SHA512_Update(ctxt
, ipad
, 64);
1069 ah_hmac_sha2_512_loop(state
, addr
, len
)
1070 struct ah_algorithm_state
*state
;
1076 if (!state
|| !state
->foo
)
1077 panic("ah_hmac_sha2_512_loop: what?");
1079 ctxt
= (SHA512_CTX
*)(((u_char
*)state
->foo
) + 128);
1080 SHA512_Update(ctxt
, (caddr_t
)addr
, (size_t)len
);
1084 ah_hmac_sha2_512_result(state
, addr
)
1085 struct ah_algorithm_state
*state
;
1088 u_char digest
[SHA512_DIGEST_LENGTH
];
1093 if (!state
|| !state
->foo
)
1094 panic("ah_hmac_sha2_512_result: what?");
1096 ipad
= (u_char
*)state
->foo
;
1097 opad
= (u_char
*)(ipad
+ 64);
1098 ctxt
= (SHA512_CTX
*)(opad
+ 64);
1100 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1102 bzero(ctxt
, sizeof(*ctxt
));
1104 SHA512_Update(ctxt
, opad
, 64);
1105 SHA512_Update(ctxt
, (caddr_t
)&digest
[0], sizeof(digest
));
1106 SHA512_Final((caddr_t
)&digest
[0], ctxt
);
1108 bcopy(&digest
[0], (void *)addr
, HMACSIZE
);
1110 FREE(state
->foo
, M_TEMP
);
1113 /*------------------------------------------------------------*/
1116 * go generate the checksum.
1119 ah_update_mbuf(m
, off
, len
, algo
, algos
)
1123 const struct ah_algorithm
*algo
;
1124 struct ah_algorithm_state
*algos
;
1129 /* easy case first */
1130 if (off
+ len
<= m
->m_len
) {
1131 (algo
->update
)(algos
, mtod(m
, caddr_t
) + off
, len
);
1135 for (n
= m
; n
; n
= n
->m_next
) {
1143 panic("ah_update_mbuf: wrong offset specified");
1145 for (/*nothing*/; n
&& len
> 0; n
= n
->m_next
) {
1148 if (n
->m_len
- off
< len
)
1149 tlen
= n
->m_len
- off
;
1153 (algo
->update
)(algos
, mtod(n
, caddr_t
) + off
, tlen
);
1162 * Go generate the checksum. This function won't modify the mbuf chain
1165 * NOTE: the function does not free mbuf on failure.
1166 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1169 ah4_calccksum(m
, ahdat
, len
, algo
, sav
)
1173 const struct ah_algorithm
*algo
;
1174 struct secasvar
*sav
;
1178 size_t advancewidth
;
1179 struct ah_algorithm_state algos
;
1180 u_char sumbuf
[AH_MAXSUMSIZE
];
1183 struct mbuf
*n
= NULL
;
1185 if ((m
->m_flags
& M_PKTHDR
) == 0)
1189 hdrtype
= -1; /*dummy, it is called IPPROTO_IP*/
1193 error
= (algo
->init
)(&algos
, sav
);
1197 advancewidth
= 0; /*safety*/
1202 case -1: /*first one only*/
1205 * copy ip hdr, modify to fit the AH checksum rule,
1206 * then take a checksum.
1211 m_copydata(m
, off
, sizeof(iphdr
), (caddr_t
)&iphdr
);
1213 hlen
= IP_VHL_HL(iphdr
.ip_vhl
) << 2;
1215 hlen
= iphdr
.ip_hl
<< 2;
1218 iphdr
.ip_sum
= htons(0);
1219 if (ip4_ah_cleartos
)
1221 iphdr
.ip_off
= htons(ntohs(iphdr
.ip_off
) & ip4_ah_offsetmask
);
1222 (algo
->update
)(&algos
, (caddr_t
)&iphdr
, sizeof(struct ip
));
1224 if (hlen
!= sizeof(struct ip
)) {
1228 if (hlen
> MCLBYTES
) {
1232 MGET(n
, M_DONTWAIT
, MT_DATA
);
1233 if (n
&& hlen
> MLEN
) {
1234 MCLGET(n
, M_DONTWAIT
);
1235 if ((n
->m_flags
& M_EXT
) == 0) {
1244 m_copydata(m
, off
, hlen
, mtod(n
, caddr_t
));
1247 * IP options processing.
1248 * See RFC2402 appendix A.
1250 p
= mtod(n
, u_char
*);
1251 i
= sizeof(struct ip
);
1253 if (i
+ IPOPT_OPTVAL
>= hlen
) {
1254 ipseclog((LOG_ERR
, "ah4_calccksum: "
1255 "invalid IP option\n"));
1259 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
||
1260 p
[i
+ IPOPT_OPTVAL
] == IPOPT_NOP
||
1261 i
+ IPOPT_OLEN
< hlen
)
1265 "ah4_calccksum: invalid IP option "
1267 p
[i
+ IPOPT_OPTVAL
]));
1273 switch (p
[i
+ IPOPT_OPTVAL
]) {
1279 case IPOPT_SECURITY
: /* 0x82 */
1280 case 0x85: /* Extended security */
1281 case 0x86: /* Commercial security */
1282 case 0x94: /* Router alert */
1283 case 0x95: /* RFC1770 */
1284 l
= p
[i
+ IPOPT_OLEN
];
1290 l
= p
[i
+ IPOPT_OLEN
];
1296 if (l
< 1 || hlen
- i
< l
) {
1299 "ah4_calccksum: invalid IP option "
1300 "(type=%02x len=%02x)\n",
1301 p
[i
+ IPOPT_OPTVAL
],
1302 p
[i
+ IPOPT_OLEN
]));
1308 if (p
[i
+ IPOPT_OPTVAL
] == IPOPT_EOL
)
1312 p
= mtod(n
, u_char
*) + sizeof(struct ip
);
1313 (algo
->update
)(&algos
, p
, hlen
- sizeof(struct ip
));
1319 hdrtype
= (iphdr
.ip_p
) & 0xff;
1320 advancewidth
= hlen
;
1331 m_copydata(m
, off
, sizeof(ah
), (caddr_t
)&ah
);
1332 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1334 : sizeof(struct newah
);
1335 siz
= (*algo
->sumsiz
)(sav
);
1336 totlen
= (ah
.ah_len
+ 2) << 2;
1339 * special treatment is necessary for the first one, not others
1342 if (totlen
> m
->m_pkthdr
.len
- off
||
1343 totlen
> MCLBYTES
) {
1347 MGET(n
, M_DONTWAIT
, MT_DATA
);
1348 if (n
&& totlen
> MLEN
) {
1349 MCLGET(n
, M_DONTWAIT
);
1350 if ((n
->m_flags
& M_EXT
) == 0) {
1359 m_copydata(m
, off
, totlen
, mtod(n
, caddr_t
));
1361 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1362 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1366 ah_update_mbuf(m
, off
, totlen
, algo
, &algos
);
1369 hdrtype
= ah
.ah_nxt
;
1370 advancewidth
= totlen
;
1375 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
, &algos
);
1376 advancewidth
= m
->m_pkthdr
.len
- off
;
1380 off
+= advancewidth
;
1381 if (off
< m
->m_pkthdr
.len
)
1384 if (len
< (*algo
->sumsiz
)(sav
)) {
1389 (algo
->result
)(&algos
, &sumbuf
[0]);
1390 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));
1405 * Go generate the checksum. This function won't modify the mbuf chain
1408 * NOTE: the function does not free mbuf on failure.
1409 * Don't use m_copy(), it will try to share cluster mbuf by using refcnt.
1412 ah6_calccksum(m
, ahdat
, len
, algo
, sav
)
1416 const struct ah_algorithm
*algo
;
1417 struct secasvar
*sav
;
1421 struct mbuf
*n
= NULL
;
1424 struct ah_algorithm_state algos
;
1425 u_char sumbuf
[AH_MAXSUMSIZE
];
1427 if ((m
->m_flags
& M_PKTHDR
) == 0)
1430 error
= (algo
->init
)(&algos
, sav
);
1435 proto
= IPPROTO_IPV6
;
1440 newoff
= ip6_nexthdr(m
, off
, proto
, &nxt
);
1442 newoff
= m
->m_pkthdr
.len
;
1443 else if (newoff
<= off
) {
1451 * special treatment is necessary for the first one, not others
1454 struct ip6_hdr ip6copy
;
1456 if (newoff
- off
!= sizeof(struct ip6_hdr
)) {
1461 m_copydata(m
, off
, newoff
- off
, (caddr_t
)&ip6copy
);
1463 ip6copy
.ip6_flow
= 0;
1464 ip6copy
.ip6_vfc
&= ~IPV6_VERSION_MASK
;
1465 ip6copy
.ip6_vfc
|= IPV6_VERSION
;
1466 ip6copy
.ip6_hlim
= 0;
1467 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_src
))
1468 ip6copy
.ip6_src
.s6_addr16
[1] = 0x0000;
1469 if (IN6_IS_ADDR_LINKLOCAL(&ip6copy
.ip6_dst
))
1470 ip6copy
.ip6_dst
.s6_addr16
[1] = 0x0000;
1471 (algo
->update
)(&algos
, (caddr_t
)&ip6copy
,
1472 sizeof(struct ip6_hdr
));
1474 newoff
= m
->m_pkthdr
.len
;
1475 ah_update_mbuf(m
, off
, m
->m_pkthdr
.len
- off
, algo
,
1485 hdrsiz
= (sav
->flags
& SADB_X_EXT_OLD
)
1487 : sizeof(struct newah
);
1488 siz
= (*algo
->sumsiz
)(sav
);
1491 * special treatment is necessary for the first one, not others
1494 if (newoff
- off
> MCLBYTES
) {
1498 MGET(n
, M_DONTWAIT
, MT_DATA
);
1499 if (n
&& newoff
- off
> MLEN
) {
1500 MCLGET(n
, M_DONTWAIT
);
1501 if ((n
->m_flags
& M_EXT
) == 0) {
1510 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1511 n
->m_len
= newoff
- off
;
1512 bzero(mtod(n
, caddr_t
) + hdrsiz
, siz
);
1513 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1517 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1522 case IPPROTO_HOPOPTS
:
1523 case IPPROTO_DSTOPTS
:
1525 struct ip6_ext
*ip6e
;
1527 u_int8_t
*p
, *optend
, *optp
;
1529 if (newoff
- off
> MCLBYTES
) {
1533 MGET(n
, M_DONTWAIT
, MT_DATA
);
1534 if (n
&& newoff
- off
> MLEN
) {
1535 MCLGET(n
, M_DONTWAIT
);
1536 if ((n
->m_flags
& M_EXT
) == 0) {
1545 m_copydata(m
, off
, newoff
- off
, mtod(n
, caddr_t
));
1546 n
->m_len
= newoff
- off
;
1548 ip6e
= mtod(n
, struct ip6_ext
*);
1549 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
1550 if (newoff
- off
< hdrlen
) {
1556 p
= mtod(n
, u_int8_t
*);
1557 optend
= p
+ hdrlen
;
1560 * ICV calculation for the options header including all
1561 * options. This part is a little tricky since there are
1562 * two type of options; mutable and immutable. We try to
1563 * null-out mutable ones here.
1566 while (optp
< optend
) {
1567 if (optp
[0] == IP6OPT_PAD1
)
1570 if (optp
+ 2 > optend
) {
1576 optlen
= optp
[1] + 2;
1578 if (optp
[0] & IP6OPT_MUTABLE
)
1579 bzero(optp
+ 2, optlen
- 2);
1585 (algo
->update
)(&algos
, mtod(n
, caddr_t
), n
->m_len
);
1591 case IPPROTO_ROUTING
:
1593 * For an input packet, we can just calculate `as is'.
1594 * For an output packet, we assume ip6_output have already
1595 * made packet how it will be received at the final
1601 ah_update_mbuf(m
, off
, newoff
- off
, algo
, &algos
);
1605 if (newoff
< m
->m_pkthdr
.len
) {
1611 if (len
< (*algo
->sumsiz
)(sav
)) {
1616 (algo
->result
)(&algos
, &sumbuf
[0]);
1617 bcopy(&sumbuf
[0], ahdat
, (*algo
->sumsiz
)(sav
));