2 * Copyright (c) 2008-2019 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>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet/icmp6.h>
86 #include <netinet6/ipsec.h>
87 #include <netinet6/ipsec6.h>
88 #include <netinet6/ah.h>
89 #include <netinet6/ah6.h>
90 #include <netinet6/esp.h>
91 #include <netinet6/esp6.h>
92 #include <netinet6/esp_rijndael.h>
93 #include <netinet6/esp_chachapoly.h>
94 #include <net/pfkeyv2.h>
95 #include <netkey/keydb.h>
96 #include <netkey/key.h>
97 #include <libkern/crypto/des.h>
99 #include <net/net_osdep.h>
101 #include <sys/kdebug.h>
102 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
103 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
104 #define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
105 #define MAX_SBUF_LEN 2000
107 extern lck_mtx_t
*sadb_mutex
;
108 os_log_t esp_mpkl_log_object
= NULL
;
110 static int esp_null_mature(struct secasvar
*);
111 static int esp_null_decrypt(struct mbuf
*, size_t,
112 struct secasvar
*, const struct esp_algorithm
*, int);
113 static int esp_null_encrypt(struct mbuf
*, size_t, size_t,
114 struct secasvar
*, const struct esp_algorithm
*, int);
115 static int esp_descbc_mature(struct secasvar
*);
116 static int esp_descbc_ivlen(const struct esp_algorithm
*,
118 static int esp_des_schedule(const struct esp_algorithm
*,
120 static size_t esp_des_schedlen(const struct esp_algorithm
*);
121 static int esp_des_blockdecrypt(const struct esp_algorithm
*,
122 struct secasvar
*, u_int8_t
*, u_int8_t
*);
123 static int esp_des_blockencrypt(const struct esp_algorithm
*,
124 struct secasvar
*, u_int8_t
*, u_int8_t
*);
125 static int esp_cbc_mature(struct secasvar
*);
126 static int esp_3des_schedule(const struct esp_algorithm
*,
128 static size_t esp_3des_schedlen(const struct esp_algorithm
*);
129 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
130 struct secasvar
*, u_int8_t
*, u_int8_t
*);
131 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
132 struct secasvar
*, u_int8_t
*, u_int8_t
*);
133 static int esp_common_ivlen(const struct esp_algorithm
*,
135 static int esp_cbc_decrypt(struct mbuf
*, size_t,
136 struct secasvar
*, const struct esp_algorithm
*, int);
137 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
138 struct secasvar
*, const struct esp_algorithm
*, int);
139 static int esp_gcm_mature(struct secasvar
*);
143 #define ESP_AESGCM_KEYLEN128 160 // 16-bytes key + 4 bytes salt
144 #define ESP_AESGCM_KEYLEN192 224 // 24-bytes key + 4 bytes salt
145 #define ESP_AESGCM_KEYLEN256 288 // 32-bytes key + 4 bytes salt
147 static const struct esp_algorithm des_cbc
= {
150 .mature
= esp_descbc_mature
,
153 .schedlen
= esp_des_schedlen
,
155 .ivlen
= esp_descbc_ivlen
,
156 .decrypt
= esp_cbc_decrypt
,
157 .encrypt
= esp_cbc_encrypt
,
158 .schedule
= esp_des_schedule
,
159 .blockdecrypt
= esp_des_blockdecrypt
,
160 .blockencrypt
= esp_des_blockencrypt
,
162 .finalizedecrypt
= NULL
,
163 .finalizeencrypt
= NULL
166 static const struct esp_algorithm des3_cbc
= {
169 .mature
= esp_cbc_mature
,
172 .schedlen
= esp_3des_schedlen
,
174 .ivlen
= esp_common_ivlen
,
175 .decrypt
= esp_cbc_decrypt
,
176 .encrypt
= esp_cbc_encrypt
,
177 .schedule
= esp_3des_schedule
,
178 .blockdecrypt
= esp_3des_blockdecrypt
,
179 .blockencrypt
= esp_3des_blockencrypt
,
181 .finalizedecrypt
= NULL
,
182 .finalizeencrypt
= NULL
185 static const struct esp_algorithm null_esp
= {
188 .mature
= esp_null_mature
,
193 .ivlen
= esp_common_ivlen
,
194 .decrypt
= esp_null_decrypt
,
195 .encrypt
= esp_null_encrypt
,
197 .blockdecrypt
= NULL
,
198 .blockencrypt
= NULL
,
200 .finalizedecrypt
= NULL
,
201 .finalizeencrypt
= NULL
204 static const struct esp_algorithm aes_cbc
= {
207 .mature
= esp_cbc_mature
,
210 .schedlen
= esp_aes_schedlen
,
212 .ivlen
= esp_common_ivlen
,
213 .decrypt
= esp_cbc_decrypt_aes
,
214 .encrypt
= esp_cbc_encrypt_aes
,
215 .schedule
= esp_aes_schedule
,
216 .blockdecrypt
= NULL
,
217 .blockencrypt
= NULL
,
219 .finalizedecrypt
= NULL
,
220 .finalizeencrypt
= NULL
223 static const struct esp_algorithm aes_gcm
= {
226 .mature
= esp_gcm_mature
,
227 .keymin
= ESP_AESGCM_KEYLEN128
,
228 .keymax
= ESP_AESGCM_KEYLEN256
,
229 .schedlen
= esp_gcm_schedlen
,
231 .ivlen
= esp_common_ivlen
,
232 .decrypt
= esp_gcm_decrypt_aes
,
233 .encrypt
= esp_gcm_encrypt_aes
,
234 .schedule
= esp_gcm_schedule
,
235 .blockdecrypt
= NULL
,
236 .blockencrypt
= NULL
,
238 .finalizedecrypt
= esp_gcm_decrypt_finalize
,
239 .finalizeencrypt
= esp_gcm_encrypt_finalize
242 static const struct esp_algorithm chacha_poly
= {
243 .padbound
= ESP_CHACHAPOLY_PAD_BOUND
,
244 .ivlenval
= ESP_CHACHAPOLY_IV_LEN
,
245 .mature
= esp_chachapoly_mature
,
246 .keymin
= ESP_CHACHAPOLY_KEYBITS_WITH_SALT
,
247 .keymax
= ESP_CHACHAPOLY_KEYBITS_WITH_SALT
,
248 .schedlen
= esp_chachapoly_schedlen
,
249 .name
= "chacha-poly",
250 .ivlen
= esp_chachapoly_ivlen
,
251 .decrypt
= esp_chachapoly_decrypt
,
252 .encrypt
= esp_chachapoly_encrypt
,
253 .schedule
= esp_chachapoly_schedule
,
254 .blockdecrypt
= NULL
,
255 .blockencrypt
= NULL
,
256 .icvlen
= ESP_CHACHAPOLY_ICV_LEN
,
257 .finalizedecrypt
= esp_chachapoly_decrypt_finalize
,
258 .finalizeencrypt
= esp_chachapoly_encrypt_finalize
261 static const struct esp_algorithm
*esp_algorithms
[] = {
270 const struct esp_algorithm
*
271 esp_algorithm_lookup(int idx
)
274 case SADB_EALG_DESCBC
:
276 case SADB_EALG_3DESCBC
:
280 case SADB_X_EALG_RIJNDAELCBC
:
282 case SADB_X_EALG_AES_GCM
:
284 case SADB_X_EALG_CHACHA20POLY1305
:
298 for (idx
= 0; idx
< sizeof(esp_algorithms
) / sizeof(esp_algorithms
[0]);
300 if (esp_algorithms
[idx
]->ivlenval
> ivlen
) {
301 ivlen
= esp_algorithms
[idx
]->ivlenval
;
309 esp_schedule(const struct esp_algorithm
*algo
, struct secasvar
*sav
)
313 /* check for key length */
314 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
315 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
317 "esp_schedule %s: unsupported key length %d: "
318 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
319 algo
->keymin
, algo
->keymax
));
323 lck_mtx_lock(sadb_mutex
);
324 /* already allocated */
325 if (sav
->sched
&& sav
->schedlen
!= 0) {
326 lck_mtx_unlock(sadb_mutex
);
330 /* prevent disallowed implicit IV */
331 if (((sav
->flags
& SADB_X_EXT_IIV
) != 0) &&
332 (sav
->alg_enc
!= SADB_X_EALG_AES_GCM
) &&
333 (sav
->alg_enc
!= SADB_X_EALG_CHACHA20POLY1305
)) {
335 "esp_schedule %s: implicit IV not allowed\n",
337 lck_mtx_unlock(sadb_mutex
);
341 /* no schedule necessary */
342 if (!algo
->schedule
|| !algo
->schedlen
) {
343 lck_mtx_unlock(sadb_mutex
);
347 sav
->schedlen
= (*algo
->schedlen
)(algo
);
348 if ((signed) sav
->schedlen
< 0) {
349 lck_mtx_unlock(sadb_mutex
);
353 //#### that malloc should be replaced by a saved buffer...
354 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
357 lck_mtx_unlock(sadb_mutex
);
361 error
= (*algo
->schedule
)(algo
, sav
);
363 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
365 bzero(sav
->sched
, sav
->schedlen
);
366 FREE(sav
->sched
, M_SECA
);
370 lck_mtx_unlock(sadb_mutex
);
376 __unused
struct secasvar
*sav
)
378 /* anything is okay */
384 __unused
struct mbuf
*m
,
385 __unused
size_t off
, /* offset to ESP header */
386 __unused
struct secasvar
*sav
,
387 __unused
const struct esp_algorithm
*algo
,
390 return 0; /* do nothing */
395 __unused
struct mbuf
*m
,
396 __unused
size_t off
, /* offset to ESP header */
397 __unused
size_t plen
, /* payload length (to be encrypted) */
398 __unused
struct secasvar
*sav
,
399 __unused
const struct esp_algorithm
*algo
,
402 return 0; /* do nothing */
406 esp_descbc_mature(struct secasvar
*sav
)
408 const struct esp_algorithm
*algo
;
410 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
411 ipseclog((LOG_ERR
, "esp_cbc_mature: "
412 "algorithm incompatible with 4 octets IV length\n"));
417 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
421 algo
= esp_algorithm_lookup(sav
->alg_enc
);
424 "esp_descbc_mature: unsupported algorithm.\n"));
428 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
429 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
431 "esp_descbc_mature: invalid key length %d.\n",
432 _KEYBITS(sav
->key_enc
)));
437 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
439 "esp_descbc_mature: weak key was passed.\n"));
448 __unused
const struct esp_algorithm
*algo
,
449 struct secasvar
*sav
)
454 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
457 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
)) {
465 __unused
const struct esp_algorithm
*algo
)
467 return sizeof(des_ecb_key_schedule
);
472 __unused
const struct esp_algorithm
*algo
,
473 struct secasvar
*sav
)
475 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
476 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
477 (des_ecb_key_schedule
*)sav
->sched
)) {
485 esp_des_blockdecrypt(
486 __unused
const struct esp_algorithm
*algo
,
487 struct secasvar
*sav
,
491 /* assumption: d has a good alignment */
492 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
493 return des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
494 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
498 esp_des_blockencrypt(
499 __unused
const struct esp_algorithm
*algo
,
500 struct secasvar
*sav
,
504 /* assumption: d has a good alignment */
505 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
506 return des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
507 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
511 esp_cbc_mature(struct secasvar
*sav
)
514 const struct esp_algorithm
*algo
;
516 if (sav
->flags
& SADB_X_EXT_OLD
) {
518 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
521 if (sav
->flags
& SADB_X_EXT_DERIV
) {
523 "esp_cbc_mature: algorithm incompatible with derived\n"));
528 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
532 algo
= esp_algorithm_lookup(sav
->alg_enc
);
535 "esp_cbc_mature: unsupported algorithm.\n"));
539 keylen
= sav
->key_enc
->sadb_key_bits
;
540 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
542 "esp_cbc_mature %s: invalid key length %d.\n",
543 algo
->name
, sav
->key_enc
->sadb_key_bits
));
546 switch (sav
->alg_enc
) {
547 case SADB_EALG_3DESCBC
:
549 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
550 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
551 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
553 "esp_cbc_mature %s: weak key was passed.\n",
558 case SADB_X_EALG_RIJNDAELCBC
:
559 /* allows specific key sizes only */
560 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
562 "esp_cbc_mature %s: invalid key length %d.\n",
563 algo
->name
, keylen
));
573 esp_gcm_mature(struct secasvar
*sav
)
576 const struct esp_algorithm
*algo
;
578 if (sav
->flags
& SADB_X_EXT_OLD
) {
580 "esp_gcm_mature: algorithm incompatible with esp-old\n"));
583 if (sav
->flags
& SADB_X_EXT_DERIV
) {
585 "esp_gcm_mature: algorithm incompatible with derived\n"));
588 if (sav
->flags
& SADB_X_EXT_IIV
) {
590 "esp_gcm_mature: implicit IV not currently implemented\n"));
595 ipseclog((LOG_ERR
, "esp_gcm_mature: no key is given.\n"));
599 algo
= esp_algorithm_lookup(sav
->alg_enc
);
602 "esp_gcm_mature: unsupported algorithm.\n"));
606 keylen
= sav
->key_enc
->sadb_key_bits
;
607 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
609 "esp_gcm_mature %s: invalid key length %d.\n",
610 algo
->name
, sav
->key_enc
->sadb_key_bits
));
613 switch (sav
->alg_enc
) {
614 case SADB_X_EALG_AES_GCM
:
615 /* allows specific key sizes only */
616 if (!(keylen
== ESP_AESGCM_KEYLEN128
|| keylen
== ESP_AESGCM_KEYLEN192
|| keylen
== ESP_AESGCM_KEYLEN256
)) {
618 "esp_gcm_mature %s: invalid key length %d.\n",
619 algo
->name
, keylen
));
625 "esp_gcm_mature %s: invalid algo %d.\n", algo
->name
, sav
->alg_enc
));
634 __unused
const struct esp_algorithm
*algo
)
636 return sizeof(des3_ecb_key_schedule
);
641 __unused
const struct esp_algorithm
*algo
,
642 struct secasvar
*sav
)
644 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
646 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
647 (des3_ecb_key_schedule
*)sav
->sched
)) {
655 esp_3des_blockdecrypt(
656 __unused
const struct esp_algorithm
*algo
,
657 struct secasvar
*sav
,
661 /* assumption: d has a good alignment */
662 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
663 return des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
664 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
668 esp_3des_blockencrypt(
669 __unused
const struct esp_algorithm
*algo
,
670 struct secasvar
*sav
,
674 /* assumption: d has a good alignment */
675 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
676 return des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
677 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
682 const struct esp_algorithm
*algo
,
683 __unused
struct secasvar
*sav
)
686 panic("esp_common_ivlen: unknown algorithm");
688 return algo
->ivlenval
;
692 esp_cbc_decrypt(struct mbuf
*m
, size_t off
, struct secasvar
*sav
,
693 const struct esp_algorithm
*algo
, int ivlen
)
696 struct mbuf
*d
, *d0
, *dp
;
697 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
698 int sn
, dn
; /* offset from the head of the mbuf, to meat */
699 size_t ivoff
, bodyoff
;
700 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
701 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
709 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
710 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
711 "unsupported ivlen %d\n", algo
->name
, ivlen
));
716 /* assumes blocklen == padbound */
717 blocklen
= algo
->padbound
;
720 if (blocklen
> sizeof(iv
)) {
721 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
722 "unsupported blocklen %d\n", algo
->name
, blocklen
));
728 if (sav
->flags
& SADB_X_EXT_OLD
) {
730 ivoff
= off
+ sizeof(struct esp
);
731 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
735 if (sav
->flags
& SADB_X_EXT_DERIV
) {
737 * draft-ietf-ipsec-ciph-des-derived-00.txt
738 * uses sequence number field as IV field.
740 ivoff
= off
+ sizeof(struct esp
);
741 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
742 ivlen
= sizeof(u_int32_t
);
745 ivoff
= off
+ sizeof(struct newesp
);
746 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
751 VERIFY(ivoff
<= INT_MAX
);
753 m_copydata(m
, (int)ivoff
, ivlen
, (caddr_t
) iv
);
756 if (ivlen
== blocklen
) {
758 } else if (ivlen
== 4 && blocklen
== 8) {
759 bcopy(&iv
[0], &iv
[4], 4);
765 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
766 "unsupported ivlen/blocklen: %d %d\n",
767 algo
->name
, ivlen
, blocklen
));
772 if (m
->m_pkthdr
.len
< bodyoff
) {
773 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%u\n",
774 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
778 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
779 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
780 "payload length must be multiple of %d\n",
781 algo
->name
, blocklen
));
788 soff
= doff
= sn
= dn
= 0;
792 while (soff
< bodyoff
) {
793 if (soff
+ s
->m_len
> bodyoff
) {
794 sn
= (int)(bodyoff
- soff
);
804 /* skip over empty mbuf */
805 while (s
&& s
->m_len
== 0) {
809 // Allocate blocksized buffer for unaligned or non-contiguous access
810 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
814 while (soff
< m
->m_pkthdr
.len
) {
816 if (sn
+ blocklen
<= s
->m_len
) {
817 /* body is continuous */
818 sp
= mtod(s
, u_int8_t
*) + sn
;
820 /* body is non-continuous */
821 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
826 if (!d
|| dn
+ blocklen
> d
->m_len
) {
830 MGET(d
, M_DONTWAIT
, MT_DATA
);
831 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
833 MCLGET(d
, M_DONTWAIT
);
834 if ((d
->m_flags
& M_EXT
) == 0) {
854 // try to make mbuf data aligned
855 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
856 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
860 d
->m_len
= (int)((M_TRAILINGSPACE(d
) / blocklen
) * blocklen
);
868 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
869 if (IPSEC_IS_P2ALIGNED(sp
)) {
874 memcpy(sp
, sp_unaligned
, blocklen
);
876 // no need to check output pointer alignment
877 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
879 // update unaligned pointers
880 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
886 q
= mtod(d
, u_int8_t
*) + dn
;
887 for (i
= 0; i
< blocklen
; i
++) {
893 bcopy(sbuf
, iv
, blocklen
);
902 /* find the next source block */
903 while (s
&& sn
>= s
->m_len
) {
910 m_freem(scut
->m_next
);
911 scut
->m_len
= scutoff
;
915 bzero(iv
, sizeof(iv
));
916 bzero(sbuf
, blocklen
);
928 __unused
size_t plen
,
929 struct secasvar
*sav
,
930 const struct esp_algorithm
*algo
,
934 struct mbuf
*d
, *d0
, *dp
;
935 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
936 int sn
, dn
; /* offset from the head of the mbuf, to meat */
937 size_t ivoff
, bodyoff
;
938 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
939 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
947 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
948 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
949 "unsupported ivlen %d\n", algo
->name
, ivlen
));
954 /* assumes blocklen == padbound */
955 blocklen
= algo
->padbound
;
958 if (blocklen
> sizeof(iv
)) {
959 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
960 "unsupported blocklen %d\n", algo
->name
, blocklen
));
966 if (sav
->flags
& SADB_X_EXT_OLD
) {
968 ivoff
= off
+ sizeof(struct esp
);
969 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
973 if (sav
->flags
& SADB_X_EXT_DERIV
) {
975 * draft-ietf-ipsec-ciph-des-derived-00.txt
976 * uses sequence number field as IV field.
978 ivoff
= off
+ sizeof(struct esp
);
979 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
980 ivlen
= sizeof(u_int32_t
);
983 ivoff
= off
+ sizeof(struct newesp
);
984 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
989 VERIFY(ivoff
<= INT_MAX
);
991 /* put iv into the packet. if we are in derived mode, use seqno. */
993 m_copydata(m
, (int)ivoff
, ivlen
, (caddr_t
) iv
);
995 bcopy(sav
->iv
, iv
, ivlen
);
996 /* maybe it is better to overwrite dest, not source */
997 m_copyback(m
, (int)ivoff
, ivlen
, (caddr_t
) iv
);
1001 if (ivlen
== blocklen
) {
1003 } else if (ivlen
== 4 && blocklen
== 8) {
1004 bcopy(&iv
[0], &iv
[4], 4);
1010 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
1011 "unsupported ivlen/blocklen: %d %d\n",
1012 algo
->name
, ivlen
, blocklen
));
1017 if (m
->m_pkthdr
.len
< bodyoff
) {
1018 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%u\n",
1019 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
1023 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
1024 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
1025 "payload length must be multiple of %u\n",
1026 algo
->name
, (u_int32_t
)algo
->padbound
));
1033 soff
= doff
= sn
= dn
= 0;
1037 while (soff
< bodyoff
) {
1038 if (soff
+ s
->m_len
> bodyoff
) {
1039 sn
= (int)(bodyoff
- soff
);
1049 /* skip over empty mbuf */
1050 while (s
&& s
->m_len
== 0) {
1054 // Allocate blocksized buffer for unaligned or non-contiguous access
1055 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
1059 while (soff
< m
->m_pkthdr
.len
) {
1061 if (sn
+ blocklen
<= s
->m_len
) {
1062 /* body is continuous */
1063 sp
= mtod(s
, u_int8_t
*) + sn
;
1065 /* body is non-continuous */
1066 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
1071 if (!d
|| dn
+ blocklen
> d
->m_len
) {
1075 MGET(d
, M_DONTWAIT
, MT_DATA
);
1076 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
1077 if (d
&& i
> MLEN
) {
1078 MCLGET(d
, M_DONTWAIT
);
1079 if ((d
->m_flags
& M_EXT
) == 0) {
1099 // try to make mbuf data aligned
1100 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
1101 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
1105 d
->m_len
= (int)((M_TRAILINGSPACE(d
) / blocklen
) * blocklen
);
1115 for (i
= 0; i
< blocklen
; i
++) {
1120 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
1121 if (IPSEC_IS_P2ALIGNED(sp
)) {
1122 sp_unaligned
= NULL
;
1126 memcpy(sp
, sp_unaligned
, blocklen
);
1128 // no need to check output pointer alignment
1129 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1131 // update unaligned pointers
1132 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1137 ivp
= mtod(d
, u_int8_t
*) + dn
;
1142 /* find the next source block */
1143 while (s
&& sn
>= s
->m_len
) {
1150 m_freem(scut
->m_next
);
1151 scut
->m_len
= scutoff
;
1155 bzero(iv
, sizeof(iv
));
1156 bzero(sbuf
, blocklen
);
1158 key_sa_stir_iv(sav
);
1166 /*------------------------------------------------------------*/
1168 /* does not free m0 on error */
1172 size_t skip
, /* offset to ESP header */
1173 size_t length
, /* payload length */
1174 struct secasvar
*sav
,
1179 struct ah_algorithm_state s
;
1180 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1181 const struct ah_algorithm
*algo
;
1186 if (m0
->m_pkthdr
.len
< skip
) {
1187 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1190 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1191 ipseclog((LOG_DEBUG
,
1192 "esp_auth: mbuf length < skip + length\n"));
1196 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
, length
, 0, 0, 0);
1198 * length of esp part (excluding authentication data) must be 4n,
1199 * since nexthdr must be at offset 4n+3.
1202 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1203 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1, 0, 0, 0, 0);
1207 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1208 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2, 0, 0, 0, 0);
1211 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1214 "esp_auth: bad ESP auth algorithm passed: %d\n",
1216 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3, 0, 0, 0, 0);
1223 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1224 if (sizeof(sumbuf
) < siz
) {
1225 ipseclog((LOG_DEBUG
,
1226 "esp_auth: AH_MAXSUMSIZE is too small: siz=%u\n",
1228 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4, 0, 0, 0, 0);
1232 /* skip the header */
1235 panic("mbuf chain?");
1237 if (m
->m_len
<= skip
) {
1247 error
= (*algo
->init
)(&s
, sav
);
1249 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5, 0, 0, 0, 0);
1252 while (0 < length
) {
1254 panic("mbuf chain?");
1257 if (m
->m_len
- off
< length
) {
1258 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1260 length
-= m
->m_len
- off
;
1264 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1268 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1269 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1270 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6, 0, 0, 0, 0);
1277 static int esp_initialized
= 0;
1279 if (esp_initialized
) {
1283 esp_initialized
= 1;
1285 esp_mpkl_log_object
= MPKL_CREATE_LOGOBJECT("com.apple.xnu.esp");
1286 if (esp_mpkl_log_object
== NULL
) {
1287 panic("MPKL_CREATE_LOGOBJECT for ESP failed");