]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_core.c
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 <libkern/crypto/des.h>
106 #include <net/net_osdep.h>
108 #include <sys/kdebug.h>
109 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
110 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
111 #define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
112 #define MAX_SBUF_LEN 2000
114 extern lck_mtx_t
*sadb_mutex
;
116 static int esp_null_mature(struct secasvar
*);
117 static int esp_null_decrypt(struct mbuf
*, size_t,
118 struct secasvar
*, const struct esp_algorithm
*, int);
119 static int esp_null_encrypt(struct mbuf
*, size_t, size_t,
120 struct secasvar
*, const struct esp_algorithm
*, int);
121 static int esp_descbc_mature(struct secasvar
*);
122 static int esp_descbc_ivlen(const struct esp_algorithm
*,
124 static int esp_des_schedule(const struct esp_algorithm
*,
126 static int esp_des_schedlen(const struct esp_algorithm
*);
127 static int esp_des_blockdecrypt(const struct esp_algorithm
*,
128 struct secasvar
*, u_int8_t
*, u_int8_t
*);
129 static int esp_des_blockencrypt(const struct esp_algorithm
*,
130 struct secasvar
*, u_int8_t
*, u_int8_t
*);
131 static int esp_cbc_mature(struct secasvar
*);
132 static int esp_3des_schedule(const struct esp_algorithm
*,
134 static int esp_3des_schedlen(const struct esp_algorithm
*);
135 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
136 struct secasvar
*, u_int8_t
*, u_int8_t
*);
137 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
138 struct secasvar
*, u_int8_t
*, u_int8_t
*);
139 static int esp_common_ivlen(const struct esp_algorithm
*,
141 static int esp_cbc_decrypt(struct mbuf
*, size_t,
142 struct secasvar
*, const struct esp_algorithm
*, int);
143 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
144 struct secasvar
*, const struct esp_algorithm
*, int);
148 static const struct esp_algorithm des_cbc
=
149 { 8, -1, esp_descbc_mature
, 64, 64, esp_des_schedlen
,
151 esp_descbc_ivlen
, esp_cbc_decrypt
,
152 esp_cbc_encrypt
, esp_des_schedule
,
153 esp_des_blockdecrypt
, esp_des_blockencrypt
, };
154 static const struct esp_algorithm des3_cbc
=
155 { 8, 8, esp_cbc_mature
, 192, 192, esp_3des_schedlen
,
157 esp_common_ivlen
, esp_cbc_decrypt
,
158 esp_cbc_encrypt
, esp_3des_schedule
,
159 esp_3des_blockdecrypt
, esp_3des_blockencrypt
, };
160 static const struct esp_algorithm null_esp
=
161 { 1, 0, esp_null_mature
, 0, 2048, 0, "null",
162 esp_common_ivlen
, esp_null_decrypt
,
163 esp_null_encrypt
, NULL
, NULL
, NULL
};
164 static const struct esp_algorithm aes_cbc
=
165 { 16, 16, esp_cbc_mature
, 128, 256, esp_aes_schedlen
,
167 esp_common_ivlen
, esp_cbc_decrypt_aes
,
168 esp_cbc_encrypt_aes
, esp_aes_schedule
,
171 static const struct esp_algorithm
*esp_algorithms
[] = {
178 const struct esp_algorithm
*
179 esp_algorithm_lookup(idx
)
183 case SADB_EALG_DESCBC
:
185 case SADB_EALG_3DESCBC
:
189 case SADB_X_EALG_RIJNDAELCBC
:
203 for (idx
= 0; idx
< sizeof(esp_algorithms
)/sizeof(esp_algorithms
[0]);
205 if (esp_algorithms
[idx
]->ivlenval
> ivlen
)
206 ivlen
= esp_algorithms
[idx
]->ivlenval
;
213 esp_schedule(algo
, sav
)
214 const struct esp_algorithm
*algo
;
215 struct secasvar
*sav
;
219 /* check for key length */
220 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
221 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
223 "esp_schedule %s: unsupported key length %d: "
224 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
225 algo
->keymin
, algo
->keymax
));
229 lck_mtx_lock(sadb_mutex
);
230 /* already allocated */
231 if (sav
->sched
&& sav
->schedlen
!= 0) {
232 lck_mtx_unlock(sadb_mutex
);
235 /* no schedule necessary */
236 if (!algo
->schedule
|| !algo
->schedlen
) {
237 lck_mtx_unlock(sadb_mutex
);
241 sav
->schedlen
= (*algo
->schedlen
)(algo
);
242 if ((signed) sav
->schedlen
< 0) {
243 lck_mtx_unlock(sadb_mutex
);
247 //#### that malloc should be replaced by a saved buffer...
248 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
251 lck_mtx_unlock(sadb_mutex
);
255 error
= (*algo
->schedule
)(algo
, sav
);
257 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
259 bzero(sav
->sched
, sav
->schedlen
);
260 FREE(sav
->sched
, M_SECA
);
264 lck_mtx_unlock(sadb_mutex
);
270 __unused
struct secasvar
*sav
)
273 /* anything is okay */
279 __unused
struct mbuf
*m
,
280 __unused
size_t off
, /* offset to ESP header */
281 __unused
struct secasvar
*sav
,
282 __unused
const struct esp_algorithm
*algo
,
286 return 0; /* do nothing */
291 __unused
struct mbuf
*m
,
292 __unused
size_t off
, /* offset to ESP header */
293 __unused
size_t plen
, /* payload length (to be encrypted) */
294 __unused
struct secasvar
*sav
,
295 __unused
const struct esp_algorithm
*algo
,
299 return 0; /* do nothing */
303 esp_descbc_mature(sav
)
304 struct secasvar
*sav
;
306 const struct esp_algorithm
*algo
;
308 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
309 ipseclog((LOG_ERR
, "esp_cbc_mature: "
310 "algorithm incompatible with 4 octets IV length\n"));
315 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
319 algo
= esp_algorithm_lookup(sav
->alg_enc
);
322 "esp_descbc_mature: unsupported algorithm.\n"));
326 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
327 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
329 "esp_descbc_mature: invalid key length %d.\n",
330 _KEYBITS(sav
->key_enc
)));
335 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
337 "esp_descbc_mature: weak key was passed.\n"));
346 __unused
const struct esp_algorithm
*algo
,
347 struct secasvar
*sav
)
352 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
354 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
361 __unused
const struct esp_algorithm
*algo
)
363 return sizeof(des_ecb_key_schedule
);
368 __unused
const struct esp_algorithm
*algo
,
369 struct secasvar
*sav
)
372 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
373 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
374 (des_ecb_key_schedule
*)sav
->sched
))
381 esp_des_blockdecrypt(
382 __unused
const struct esp_algorithm
*algo
,
383 struct secasvar
*sav
,
387 /* assumption: d has a good alignment */
388 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
389 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
390 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
395 esp_des_blockencrypt(
396 __unused
const struct esp_algorithm
*algo
,
397 struct secasvar
*sav
,
401 /* assumption: d has a good alignment */
402 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
403 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
404 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
410 struct secasvar
*sav
;
413 const struct esp_algorithm
*algo
;
415 if (sav
->flags
& SADB_X_EXT_OLD
) {
417 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
420 if (sav
->flags
& SADB_X_EXT_DERIV
) {
422 "esp_cbc_mature: algorithm incompatible with derived\n"));
427 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
431 algo
= esp_algorithm_lookup(sav
->alg_enc
);
434 "esp_cbc_mature %s: unsupported algorithm.\n", algo
->name
));
438 keylen
= sav
->key_enc
->sadb_key_bits
;
439 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
441 "esp_cbc_mature %s: invalid key length %d.\n",
442 algo
->name
, sav
->key_enc
->sadb_key_bits
));
445 switch (sav
->alg_enc
) {
446 case SADB_EALG_3DESCBC
:
448 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
449 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
450 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
452 "esp_cbc_mature %s: weak key was passed.\n",
457 case SADB_X_EALG_RIJNDAELCBC
:
458 /* allows specific key sizes only */
459 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
461 "esp_cbc_mature %s: invalid key length %d.\n",
462 algo
->name
, keylen
));
473 __unused
const struct esp_algorithm
*algo
)
476 return sizeof(des3_ecb_key_schedule
);
481 __unused
const struct esp_algorithm
*algo
,
482 struct secasvar
*sav
)
484 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
486 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
487 (des3_ecb_key_schedule
*)sav
->sched
))
494 esp_3des_blockdecrypt(
495 __unused
const struct esp_algorithm
*algo
,
496 struct secasvar
*sav
,
500 /* assumption: d has a good alignment */
501 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
502 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
503 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
508 esp_3des_blockencrypt(
509 __unused
const struct esp_algorithm
*algo
,
510 struct secasvar
*sav
,
514 /* assumption: d has a good alignment */
515 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
516 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
517 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
523 const struct esp_algorithm
*algo
,
524 __unused
struct secasvar
*sav
)
528 panic("esp_common_ivlen: unknown algorithm");
529 return algo
->ivlenval
;
533 esp_cbc_decrypt(m
, off
, sav
, algo
, ivlen
)
536 struct secasvar
*sav
;
537 const struct esp_algorithm
*algo
;
541 struct mbuf
*d
, *d0
, *dp
;
542 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
543 int sn
, dn
; /* offset from the head of the mbuf, to meat */
544 size_t ivoff
, bodyoff
;
545 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
546 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
554 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
555 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
556 "unsupported ivlen %d\n", algo
->name
, ivlen
));
561 /* assumes blocklen == padbound */
562 blocklen
= algo
->padbound
;
565 if (blocklen
> sizeof(iv
)) {
566 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
567 "unsupported blocklen %d\n", algo
->name
, blocklen
));
573 if (sav
->flags
& SADB_X_EXT_OLD
) {
575 ivoff
= off
+ sizeof(struct esp
);
576 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
580 if (sav
->flags
& SADB_X_EXT_DERIV
) {
582 * draft-ietf-ipsec-ciph-des-derived-00.txt
583 * uses sequence number field as IV field.
585 ivoff
= off
+ sizeof(struct esp
);
586 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
587 ivlen
= sizeof(u_int32_t
);
590 ivoff
= off
+ sizeof(struct newesp
);
591 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
597 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
600 if (ivlen
== blocklen
)
602 else if (ivlen
== 4 && blocklen
== 8) {
603 bcopy(&iv
[0], &iv
[4], 4);
609 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
610 "unsupported ivlen/blocklen: %d %d\n",
611 algo
->name
, ivlen
, blocklen
));
616 if (m
->m_pkthdr
.len
< bodyoff
) {
617 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
618 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
622 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
623 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
624 "payload length must be multiple of %d\n",
625 algo
->name
, blocklen
));
632 soff
= doff
= sn
= dn
= 0;
636 while (soff
< bodyoff
) {
637 if (soff
+ s
->m_len
> bodyoff
) {
648 /* skip over empty mbuf */
649 while (s
&& s
->m_len
== 0)
652 // Allocate blocksized buffer for unaligned or non-contiguous access
653 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
656 while (soff
< m
->m_pkthdr
.len
) {
658 if (sn
+ blocklen
<= s
->m_len
) {
659 /* body is continuous */
660 sp
= mtod(s
, u_int8_t
*) + sn
;
662 /* body is non-continuous */
663 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
668 if (!d
|| dn
+ blocklen
> d
->m_len
) {
671 MGET(d
, M_DONTWAIT
, MT_DATA
);
672 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
674 MCLGET(d
, M_DONTWAIT
);
675 if ((d
->m_flags
& M_EXT
) == 0) {
692 // try to make mbuf data aligned
693 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
694 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
698 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
705 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
706 if (IPSEC_IS_P2ALIGNED(sp
)) {
711 memcpy(sp
, sp_unaligned
, blocklen
);
713 // no need to check output pointer alignment
714 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
716 // update unaligned pointers
717 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
723 q
= mtod(d
, u_int8_t
*) + dn
;
724 for (i
= 0; i
< blocklen
; i
++)
729 bcopy(sbuf
, iv
, blocklen
);
737 /* find the next source block */
738 while (s
&& sn
>= s
->m_len
) {
745 m_freem(scut
->m_next
);
746 scut
->m_len
= scutoff
;
750 bzero(iv
, sizeof(iv
));
751 bzero(sbuf
, sizeof(sbuf
));
762 __unused
size_t plen
,
763 struct secasvar
*sav
,
764 const struct esp_algorithm
*algo
,
768 struct mbuf
*d
, *d0
, *dp
;
769 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
770 int sn
, dn
; /* offset from the head of the mbuf, to meat */
771 size_t ivoff
, bodyoff
;
772 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
773 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
781 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
782 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
783 "unsupported ivlen %d\n", algo
->name
, ivlen
));
788 /* assumes blocklen == padbound */
789 blocklen
= algo
->padbound
;
792 if (blocklen
> sizeof(iv
)) {
793 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
794 "unsupported blocklen %d\n", algo
->name
, blocklen
));
800 if (sav
->flags
& SADB_X_EXT_OLD
) {
802 ivoff
= off
+ sizeof(struct esp
);
803 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
807 if (sav
->flags
& SADB_X_EXT_DERIV
) {
809 * draft-ietf-ipsec-ciph-des-derived-00.txt
810 * uses sequence number field as IV field.
812 ivoff
= off
+ sizeof(struct esp
);
813 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
814 ivlen
= sizeof(u_int32_t
);
817 ivoff
= off
+ sizeof(struct newesp
);
818 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
823 /* put iv into the packet. if we are in derived mode, use seqno. */
825 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
827 bcopy(sav
->iv
, iv
, ivlen
);
828 /* maybe it is better to overwrite dest, not source */
829 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
833 if (ivlen
== blocklen
)
835 else if (ivlen
== 4 && blocklen
== 8) {
836 bcopy(&iv
[0], &iv
[4], 4);
842 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
843 "unsupported ivlen/blocklen: %d %d\n",
844 algo
->name
, ivlen
, blocklen
));
849 if (m
->m_pkthdr
.len
< bodyoff
) {
850 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
851 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
855 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
856 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
857 "payload length must be multiple of %lu\n",
858 algo
->name
, (u_int32_t
)algo
->padbound
));
865 soff
= doff
= sn
= dn
= 0;
869 while (soff
< bodyoff
) {
870 if (soff
+ s
->m_len
> bodyoff
) {
881 /* skip over empty mbuf */
882 while (s
&& s
->m_len
== 0)
885 // Allocate blocksized buffer for unaligned or non-contiguous access
886 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
889 while (soff
< m
->m_pkthdr
.len
) {
891 if (sn
+ blocklen
<= s
->m_len
) {
892 /* body is continuous */
893 sp
= mtod(s
, u_int8_t
*) + sn
;
895 /* body is non-continuous */
896 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
901 if (!d
|| dn
+ blocklen
> d
->m_len
) {
904 MGET(d
, M_DONTWAIT
, MT_DATA
);
905 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
907 MCLGET(d
, M_DONTWAIT
);
908 if ((d
->m_flags
& M_EXT
) == 0) {
925 // try to make mbuf data aligned
926 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
927 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
931 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
940 for (i
= 0; i
< blocklen
; i
++)
944 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
945 if (IPSEC_IS_P2ALIGNED(sp
)) {
950 memcpy(sp
, sp_unaligned
, blocklen
);
952 // no need to check output pointer alignment
953 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
955 // update unaligned pointers
956 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
961 ivp
= mtod(d
, u_int8_t
*) + dn
;
966 /* find the next source block */
967 while (s
&& sn
>= s
->m_len
) {
974 m_freem(scut
->m_next
);
975 scut
->m_len
= scutoff
;
979 bzero(iv
, sizeof(iv
));
980 bzero(sbuf
, sizeof(sbuf
));
989 /*------------------------------------------------------------*/
991 /* does not free m0 on error */
993 esp_auth(m0
, skip
, length
, sav
, sum
)
995 size_t skip
; /* offset to ESP header */
996 size_t length
; /* payload length */
997 struct secasvar
*sav
;
1002 struct ah_algorithm_state s
;
1003 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1004 const struct ah_algorithm
*algo
;
1009 if (m0
->m_pkthdr
.len
< skip
) {
1010 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1013 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1014 ipseclog((LOG_DEBUG
,
1015 "esp_auth: mbuf length < skip + length\n"));
1019 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
,length
,0,0,0);
1021 * length of esp part (excluding authentication data) must be 4n,
1022 * since nexthdr must be at offset 4n+3.
1025 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1026 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1,0,0,0,0);
1030 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1031 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2,0,0,0,0);
1034 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1037 "esp_auth: bad ESP auth algorithm passed: %d\n",
1039 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3,0,0,0,0);
1046 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1047 if (sizeof(sumbuf
) < siz
) {
1048 ipseclog((LOG_DEBUG
,
1049 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1051 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4,0,0,0,0);
1055 /* skip the header */
1058 panic("mbuf chain?");
1059 if (m
->m_len
<= skip
) {
1069 error
= (*algo
->init
)(&s
, sav
);
1071 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5,0,0,0,0);
1074 while (0 < length
) {
1076 panic("mbuf chain?");
1078 if (m
->m_len
- off
< length
) {
1079 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1081 length
-= m
->m_len
- off
;
1085 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1089 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1090 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1091 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6,0,0,0,0);