2 * Copyright (c) 2008 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/esp_core.c,v 1.1.2.4 2002/03/26 10:12:29 ume Exp $ */
30 /* $KAME: esp_core.c,v 1.50 2000/11/02 12:27:38 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
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
67 #include <sys/domain.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/errno.h>
72 #include <sys/kernel.h>
73 #include <sys/syslog.h>
75 #include <kern/locks.h>
78 #include <net/route.h>
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet/icmp6.h>
88 #include <netinet6/ipsec.h>
90 #include <netinet6/ipsec6.h>
92 #include <netinet6/ah.h>
94 #include <netinet6/ah6.h>
96 #include <netinet6/esp.h>
98 #include <netinet6/esp6.h>
100 #include <netinet6/esp_rijndael.h>
101 #include <net/pfkeyv2.h>
102 #include <netkey/keydb.h>
103 #include <netkey/key.h>
104 #include <crypto/des/des.h>
105 #include <crypto/blowfish/blowfish.h>
106 #include <crypto/cast128/cast128.h>
108 #include <net/net_osdep.h>
110 #include <sys/kdebug.h>
111 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
112 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
113 #define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
115 extern lck_mtx_t
*sadb_mutex
;
117 static int esp_null_mature(struct secasvar
*);
118 static int esp_null_decrypt(struct mbuf
*, size_t,
119 struct secasvar
*, const struct esp_algorithm
*, int);
120 static int esp_null_encrypt(struct mbuf
*, size_t, size_t,
121 struct secasvar
*, const struct esp_algorithm
*, int);
122 static int esp_descbc_mature(struct secasvar
*);
123 static int esp_descbc_ivlen(const struct esp_algorithm
*,
125 static int esp_des_schedule(const struct esp_algorithm
*,
127 static int esp_des_schedlen(const struct esp_algorithm
*);
128 static int esp_des_blockdecrypt(const struct esp_algorithm
*,
129 struct secasvar
*, u_int8_t
*, u_int8_t
*);
130 static int esp_des_blockencrypt(const struct esp_algorithm
*,
131 struct secasvar
*, u_int8_t
*, u_int8_t
*);
132 static int esp_cbc_mature(struct secasvar
*);
134 static int esp_blowfish_schedule(const struct esp_algorithm
*,
136 static int esp_blowfish_schedlen(const struct esp_algorithm
*);
137 static int esp_blowfish_blockdecrypt(const struct esp_algorithm
*,
138 struct secasvar
*, u_int8_t
*, u_int8_t
*);
139 static int esp_blowfish_blockencrypt(const struct esp_algorithm
*,
140 struct secasvar
*, u_int8_t
*, u_int8_t
*);
141 static int esp_cast128_schedule(const struct esp_algorithm
*,
143 static int esp_cast128_schedlen(const struct esp_algorithm
*);
144 static int esp_cast128_blockdecrypt(const struct esp_algorithm
*,
145 struct secasvar
*, u_int8_t
*, u_int8_t
*);
146 static int esp_cast128_blockencrypt(const struct esp_algorithm
*,
147 struct secasvar
*, u_int8_t
*, u_int8_t
*);
148 #endif /* ALLCRYPTO */
149 static int esp_3des_schedule(const struct esp_algorithm
*,
151 static int esp_3des_schedlen(const struct esp_algorithm
*);
152 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
153 struct secasvar
*, u_int8_t
*, u_int8_t
*);
154 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
155 struct secasvar
*, u_int8_t
*, u_int8_t
*);
156 static int esp_common_ivlen(const struct esp_algorithm
*,
158 static int esp_cbc_decrypt(struct mbuf
*, size_t,
159 struct secasvar
*, const struct esp_algorithm
*, int);
160 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
161 struct secasvar
*, const struct esp_algorithm
*, int);
165 static const struct esp_algorithm des_cbc
=
166 { 8, -1, esp_descbc_mature
, 64, 64, esp_des_schedlen
,
168 esp_descbc_ivlen
, esp_cbc_decrypt
,
169 esp_cbc_encrypt
, esp_des_schedule
,
170 esp_des_blockdecrypt
, esp_des_blockencrypt
, };
171 static const struct esp_algorithm des3_cbc
=
172 { 8, 8, esp_cbc_mature
, 192, 192, esp_3des_schedlen
,
174 esp_common_ivlen
, esp_cbc_decrypt
,
175 esp_cbc_encrypt
, esp_3des_schedule
,
176 esp_3des_blockdecrypt
, esp_3des_blockencrypt
, };
177 static const struct esp_algorithm null_esp
=
178 { 1, 0, esp_null_mature
, 0, 2048, 0, "null",
179 esp_common_ivlen
, esp_null_decrypt
,
180 esp_null_encrypt
, NULL
, NULL
, NULL
};
182 static const struct esp_algorithm blowfish_cbc
=
183 { 8, 8, esp_cbc_mature
, 40, 448, esp_blowfish_schedlen
, "blowfish-cbc",
184 esp_common_ivlen
, esp_cbc_decrypt
,
185 esp_cbc_encrypt
, esp_blowfish_schedule
,
186 esp_blowfish_blockdecrypt
, esp_blowfish_blockencrypt
, };
187 static const struct esp_algorithm cast128_cbc
=
188 { 8, 8, esp_cbc_mature
, 40, 128, esp_cast128_schedlen
,
190 esp_common_ivlen
, esp_cbc_decrypt
,
191 esp_cbc_encrypt
, esp_cast128_schedule
,
192 esp_cast128_blockdecrypt
, esp_cast128_blockencrypt
, };
193 #endif /* ALLCRYPTO */
194 static const struct esp_algorithm aes_cbc
=
195 { 16, 16, esp_cbc_mature
, 128, 256, esp_aes_schedlen
,
197 esp_common_ivlen
, esp_cbc_decrypt_aes
,
198 esp_cbc_encrypt_aes
, esp_aes_schedule
,
201 static const struct esp_algorithm
*esp_algorithms
[] = {
208 #endif /* ALLCRYPTO */
212 const struct esp_algorithm
*
213 esp_algorithm_lookup(idx
)
218 case SADB_EALG_DESCBC
:
220 case SADB_EALG_3DESCBC
:
225 case SADB_X_EALG_BLOWFISHCBC
:
226 return &blowfish_cbc
;
227 case SADB_X_EALG_CAST128CBC
:
229 #endif /* ALLCRYPTO */
230 case SADB_X_EALG_RIJNDAELCBC
:
244 for (idx
= 0; idx
< sizeof(esp_algorithms
)/sizeof(esp_algorithms
[0]);
246 if (esp_algorithms
[idx
]->ivlenval
> ivlen
)
247 ivlen
= esp_algorithms
[idx
]->ivlenval
;
254 esp_schedule(algo
, sav
)
255 const struct esp_algorithm
*algo
;
256 struct secasvar
*sav
;
260 /* check for key length */
261 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
262 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
264 "esp_schedule %s: unsupported key length %d: "
265 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
266 algo
->keymin
, algo
->keymax
));
270 lck_mtx_lock(sadb_mutex
);
271 /* already allocated */
272 if (sav
->sched
&& sav
->schedlen
!= 0) {
273 lck_mtx_unlock(sadb_mutex
);
276 /* no schedule necessary */
277 if (!algo
->schedule
|| !algo
->schedlen
) {
278 lck_mtx_unlock(sadb_mutex
);
282 sav
->schedlen
= (*algo
->schedlen
)(algo
);
283 if ((signed) sav
->schedlen
< 0) {
284 lck_mtx_unlock(sadb_mutex
);
288 //#### that malloc should be replaced by a saved buffer...
289 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
292 lck_mtx_unlock(sadb_mutex
);
296 error
= (*algo
->schedule
)(algo
, sav
);
298 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
300 bzero(sav
->sched
, sav
->schedlen
);
301 FREE(sav
->sched
, M_SECA
);
305 lck_mtx_unlock(sadb_mutex
);
311 __unused
struct secasvar
*sav
)
314 /* anything is okay */
320 __unused
struct mbuf
*m
,
321 __unused
size_t off
, /* offset to ESP header */
322 __unused
struct secasvar
*sav
,
323 __unused
const struct esp_algorithm
*algo
,
327 return 0; /* do nothing */
332 __unused
struct mbuf
*m
,
333 __unused
size_t off
, /* offset to ESP header */
334 __unused
size_t plen
, /* payload length (to be encrypted) */
335 __unused
struct secasvar
*sav
,
336 __unused
const struct esp_algorithm
*algo
,
340 return 0; /* do nothing */
344 esp_descbc_mature(sav
)
345 struct secasvar
*sav
;
347 const struct esp_algorithm
*algo
;
349 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
350 ipseclog((LOG_ERR
, "esp_cbc_mature: "
351 "algorithm incompatible with 4 octets IV length\n"));
356 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
360 algo
= esp_algorithm_lookup(sav
->alg_enc
);
363 "esp_descbc_mature: unsupported algorithm.\n"));
367 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
368 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
370 "esp_descbc_mature: invalid key length %d.\n",
371 _KEYBITS(sav
->key_enc
)));
376 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
378 "esp_descbc_mature: weak key was passed.\n"));
387 __unused
const struct esp_algorithm
*algo
,
388 struct secasvar
*sav
)
393 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
395 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
402 __unused
const struct esp_algorithm
*algo
)
405 return sizeof(des_key_schedule
);
410 __unused
const struct esp_algorithm
*algo
,
411 struct secasvar
*sav
)
414 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
415 if (des_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
416 *(des_key_schedule
*)sav
->sched
))
423 esp_des_blockdecrypt(
424 __unused
const struct esp_algorithm
*algo
,
425 struct secasvar
*sav
,
430 /* assumption: d has a good alignment */
431 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
432 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
433 *(des_key_schedule
*)sav
->sched
, DES_DECRYPT
);
438 esp_des_blockencrypt(
439 __unused
const struct esp_algorithm
*algo
,
440 struct secasvar
*sav
,
445 /* assumption: d has a good alignment */
446 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
447 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
448 *(des_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
454 struct secasvar
*sav
;
457 const struct esp_algorithm
*algo
;
459 if (sav
->flags
& SADB_X_EXT_OLD
) {
461 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
464 if (sav
->flags
& SADB_X_EXT_DERIV
) {
466 "esp_cbc_mature: algorithm incompatible with derived\n"));
471 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
475 algo
= esp_algorithm_lookup(sav
->alg_enc
);
478 "esp_cbc_mature %s: unsupported algorithm.\n", algo
->name
));
482 keylen
= sav
->key_enc
->sadb_key_bits
;
483 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
485 "esp_cbc_mature %s: invalid key length %d.\n",
486 algo
->name
, sav
->key_enc
->sadb_key_bits
));
489 switch (sav
->alg_enc
) {
490 case SADB_EALG_3DESCBC
:
492 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
493 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
494 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
496 "esp_cbc_mature %s: weak key was passed.\n",
501 case SADB_X_EALG_BLOWFISHCBC
:
502 case SADB_X_EALG_CAST128CBC
:
504 case SADB_X_EALG_RIJNDAELCBC
:
505 /* allows specific key sizes only */
506 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
508 "esp_cbc_mature %s: invalid key length %d.\n",
509 algo
->name
, keylen
));
520 esp_blowfish_schedlen(
521 __unused
const struct esp_algorithm
*algo
)
524 return sizeof(BF_KEY
);
528 esp_blowfish_schedule(
529 __unused
const struct esp_algorithm
*algo
,
530 struct secasvar
*sav
)
533 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
534 BF_set_key((BF_KEY
*)sav
->sched
, _KEYLEN(sav
->key_enc
),
535 (u_int8_t
*) _KEYBUF(sav
->key_enc
));
540 esp_blowfish_blockdecrypt(
541 __unused
const struct esp_algorithm
*algo
,
542 struct secasvar
*sav
,
546 /* HOLY COW! BF_decrypt() takes values in host byteorder */
549 bcopy(s
, t
, sizeof(t
));
552 BF_decrypt(t
, (BF_KEY
*)sav
->sched
);
555 bcopy(t
, d
, sizeof(t
));
560 esp_blowfish_blockencrypt(
561 __unused
const struct esp_algorithm
*algo
,
562 struct secasvar
*sav
,
566 /* HOLY COW! BF_encrypt() takes values in host byteorder */
569 bcopy(s
, t
, sizeof(t
));
572 BF_encrypt(t
, (BF_KEY
*)sav
->sched
);
575 bcopy(t
, d
, sizeof(t
));
580 esp_cast128_schedlen(
581 __unused
const struct esp_algorithm
*algo
)
584 return sizeof(u_int32_t
) * 32;
588 esp_cast128_schedule(
589 __unused
const struct esp_algorithm
*algo
,
590 struct secasvar
*sav
)
592 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
593 set_cast128_subkey((u_int32_t
*)sav
->sched
, (u_int8_t
*) _KEYBUF(sav
->key_enc
),
594 _KEYLEN(sav
->key_enc
));
599 esp_cast128_blockdecrypt(
600 __unused
const struct esp_algorithm
*algo
,
601 struct secasvar
*sav
,
606 if (_KEYLEN(sav
->key_enc
) <= 80 / 8)
607 cast128_decrypt_round12(d
, s
, (u_int32_t
*)sav
->sched
);
609 cast128_decrypt_round16(d
, s
, (u_int32_t
*)sav
->sched
);
614 esp_cast128_blockencrypt(
615 __unused
const struct esp_algorithm
*algo
,
616 struct secasvar
*sav
,
621 if (_KEYLEN(sav
->key_enc
) <= 80 / 8)
622 cast128_encrypt_round12(d
, s
, (u_int32_t
*)sav
->sched
);
624 cast128_encrypt_round16(d
, s
, (u_int32_t
*)sav
->sched
);
627 #endif /* ALLCRYPTO */
631 __unused
const struct esp_algorithm
*algo
)
634 return sizeof(des_key_schedule
) * 3;
639 __unused
const struct esp_algorithm
*algo
,
640 struct secasvar
*sav
)
647 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
648 p
= (des_key_schedule
*)sav
->sched
;
649 k
= _KEYBUF(sav
->key_enc
);
650 for (i
= 0; i
< 3; i
++) {
651 error
= des_key_sched((des_cblock
*)(k
+ 8 * i
), p
[i
]);
659 esp_3des_blockdecrypt(
660 __unused
const struct esp_algorithm
*algo
,
661 struct secasvar
*sav
,
667 /* assumption: d has a good alignment */
668 p
= (des_key_schedule
*)sav
->sched
;
669 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
670 des_ecb3_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
671 p
[0], p
[1], p
[2], DES_DECRYPT
);
676 esp_3des_blockencrypt(
677 __unused
const struct esp_algorithm
*algo
,
678 struct secasvar
*sav
,
684 /* assumption: d has a good alignment */
685 p
= (des_key_schedule
*)sav
->sched
;
686 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
687 des_ecb3_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
688 p
[0], p
[1], p
[2], DES_ENCRYPT
);
694 const struct esp_algorithm
*algo
,
695 __unused
struct secasvar
*sav
)
699 panic("esp_common_ivlen: unknown algorithm");
700 return algo
->ivlenval
;
704 esp_cbc_decrypt(m
, off
, sav
, algo
, ivlen
)
707 struct secasvar
*sav
;
708 const struct esp_algorithm
*algo
;
712 struct mbuf
*d
, *d0
, *dp
;
713 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
714 int sn
, dn
; /* offset from the head of the mbuf, to meat */
715 size_t ivoff
, bodyoff
;
716 u_int8_t iv
[MAXIVLEN
], *ivp
;
717 u_int8_t sbuf
[MAXIVLEN
], *sp
;
725 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
726 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
727 "unsupported ivlen %d\n", algo
->name
, ivlen
));
732 /* assumes blocklen == padbound */
733 blocklen
= algo
->padbound
;
736 if (blocklen
> sizeof(iv
)) {
737 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
738 "unsupported blocklen %d\n", algo
->name
, blocklen
));
744 if (sav
->flags
& SADB_X_EXT_OLD
) {
746 ivoff
= off
+ sizeof(struct esp
);
747 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
751 if (sav
->flags
& SADB_X_EXT_DERIV
) {
753 * draft-ietf-ipsec-ciph-des-derived-00.txt
754 * uses sequence number field as IV field.
756 ivoff
= off
+ sizeof(struct esp
);
757 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
758 ivlen
= sizeof(u_int32_t
);
761 ivoff
= off
+ sizeof(struct newesp
);
762 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
768 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
771 if (ivlen
== blocklen
)
773 else if (ivlen
== 4 && blocklen
== 8) {
774 bcopy(&iv
[0], &iv
[4], 4);
780 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
781 "unsupported ivlen/blocklen: %d %d\n",
782 algo
->name
, ivlen
, blocklen
));
787 if (m
->m_pkthdr
.len
< bodyoff
) {
788 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
789 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
793 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
794 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
795 "payload length must be multiple of %d\n",
796 algo
->name
, blocklen
));
803 soff
= doff
= sn
= dn
= 0;
807 while (soff
< bodyoff
) {
808 if (soff
+ s
->m_len
> bodyoff
) {
819 /* skip over empty mbuf */
820 while (s
&& s
->m_len
== 0)
823 while (soff
< m
->m_pkthdr
.len
) {
825 if (sn
+ blocklen
<= s
->m_len
) {
826 /* body is continuous */
827 sp
= mtod(s
, u_int8_t
*) + sn
;
829 /* body is non-continuous */
830 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
835 if (!d
|| dn
+ blocklen
> d
->m_len
) {
838 MGET(d
, M_DONTWAIT
, MT_DATA
);
839 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
841 MCLGET(d
, M_DONTWAIT
);
842 if ((d
->m_flags
& M_EXT
) == 0) {
858 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
865 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
869 q
= mtod(d
, u_int8_t
*) + dn
;
870 for (i
= 0; i
< blocklen
; i
++)
875 bcopy(sbuf
, iv
, blocklen
);
883 /* find the next source block */
884 while (s
&& sn
>= s
->m_len
) {
891 m_freem(scut
->m_next
);
892 scut
->m_len
= scutoff
;
896 bzero(iv
, sizeof(iv
));
897 bzero(sbuf
, sizeof(sbuf
));
906 __unused
size_t plen
,
907 struct secasvar
*sav
,
908 const struct esp_algorithm
*algo
,
912 struct mbuf
*d
, *d0
, *dp
;
913 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
914 int sn
, dn
; /* offset from the head of the mbuf, to meat */
915 size_t ivoff
, bodyoff
;
916 u_int8_t iv
[MAXIVLEN
], *ivp
;
917 u_int8_t sbuf
[MAXIVLEN
], *sp
;
925 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
926 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
927 "unsupported ivlen %d\n", algo
->name
, ivlen
));
932 /* assumes blocklen == padbound */
933 blocklen
= algo
->padbound
;
936 if (blocklen
> sizeof(iv
)) {
937 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
938 "unsupported blocklen %d\n", algo
->name
, blocklen
));
944 if (sav
->flags
& SADB_X_EXT_OLD
) {
946 ivoff
= off
+ sizeof(struct esp
);
947 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
951 if (sav
->flags
& SADB_X_EXT_DERIV
) {
953 * draft-ietf-ipsec-ciph-des-derived-00.txt
954 * uses sequence number field as IV field.
956 ivoff
= off
+ sizeof(struct esp
);
957 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
958 ivlen
= sizeof(u_int32_t
);
961 ivoff
= off
+ sizeof(struct newesp
);
962 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
967 /* put iv into the packet. if we are in derived mode, use seqno. */
969 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
971 bcopy(sav
->iv
, iv
, ivlen
);
972 /* maybe it is better to overwrite dest, not source */
973 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
977 if (ivlen
== blocklen
)
979 else if (ivlen
== 4 && blocklen
== 8) {
980 bcopy(&iv
[0], &iv
[4], 4);
986 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
987 "unsupported ivlen/blocklen: %d %d\n",
988 algo
->name
, ivlen
, blocklen
));
993 if (m
->m_pkthdr
.len
< bodyoff
) {
994 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
995 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
999 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
1000 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
1001 "payload length must be multiple of %lu\n",
1002 algo
->name
, (u_int32_t
)algo
->padbound
));
1009 soff
= doff
= sn
= dn
= 0;
1013 while (soff
< bodyoff
) {
1014 if (soff
+ s
->m_len
> bodyoff
) {
1015 sn
= bodyoff
- soff
;
1025 /* skip over empty mbuf */
1026 while (s
&& s
->m_len
== 0)
1029 while (soff
< m
->m_pkthdr
.len
) {
1031 if (sn
+ blocklen
<= s
->m_len
) {
1032 /* body is continuous */
1033 sp
= mtod(s
, u_int8_t
*) + sn
;
1035 /* body is non-continuous */
1036 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
1041 if (!d
|| dn
+ blocklen
> d
->m_len
) {
1044 MGET(d
, M_DONTWAIT
, MT_DATA
);
1045 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
1046 if (d
&& i
> MLEN
) {
1047 MCLGET(d
, M_DONTWAIT
);
1048 if ((d
->m_flags
& M_EXT
) == 0) {
1064 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
1073 for (i
= 0; i
< blocklen
; i
++)
1077 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1080 ivp
= mtod(d
, u_int8_t
*) + dn
;
1085 /* find the next source block */
1086 while (s
&& sn
>= s
->m_len
) {
1093 m_freem(scut
->m_next
);
1094 scut
->m_len
= scutoff
;
1098 bzero(iv
, sizeof(iv
));
1099 bzero(sbuf
, sizeof(sbuf
));
1101 key_sa_stir_iv(sav
);
1106 /*------------------------------------------------------------*/
1108 /* does not free m0 on error */
1110 esp_auth(m0
, skip
, length
, sav
, sum
)
1112 size_t skip
; /* offset to ESP header */
1113 size_t length
; /* payload length */
1114 struct secasvar
*sav
;
1119 struct ah_algorithm_state s
;
1120 u_char sumbuf
[AH_MAXSUMSIZE
];
1121 const struct ah_algorithm
*algo
;
1126 if (m0
->m_pkthdr
.len
< skip
) {
1127 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1130 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1131 ipseclog((LOG_DEBUG
,
1132 "esp_auth: mbuf length < skip + length\n"));
1136 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
,length
,0,0,0);
1138 * length of esp part (excluding authentication data) must be 4n,
1139 * since nexthdr must be at offset 4n+3.
1142 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1143 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1,0,0,0,0);
1147 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1148 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2,0,0,0,0);
1151 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1154 "esp_auth: bad ESP auth algorithm passed: %d\n",
1156 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3,0,0,0,0);
1163 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1164 if (sizeof(sumbuf
) < siz
) {
1165 ipseclog((LOG_DEBUG
,
1166 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1168 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4,0,0,0,0);
1172 /* skip the header */
1175 panic("mbuf chain?");
1176 if (m
->m_len
<= skip
) {
1186 error
= (*algo
->init
)(&s
, sav
);
1188 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5,0,0,0,0);
1191 while (0 < length
) {
1193 panic("mbuf chain?");
1195 if (m
->m_len
- off
< length
) {
1196 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1198 length
-= m
->m_len
- off
;
1202 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1206 (*algo
->result
)(&s
, (caddr_t
) sumbuf
);
1207 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1208 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6,0,0,0,0);