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>
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 <netinet6/esp_chachapoly.h>
102 #include <net/pfkeyv2.h>
103 #include <netkey/keydb.h>
104 #include <netkey/key.h>
105 #include <libkern/crypto/des.h>
107 #include <net/net_osdep.h>
109 #include <sys/kdebug.h>
110 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
111 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
112 #define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
113 #define MAX_SBUF_LEN 2000
115 extern lck_mtx_t
*sadb_mutex
;
116 os_log_t esp_mpkl_log_object
= NULL
;
118 static int esp_null_mature(struct secasvar
*);
119 static int esp_null_decrypt(struct mbuf
*, size_t,
120 struct secasvar
*, const struct esp_algorithm
*, int);
121 static int esp_null_encrypt(struct mbuf
*, size_t, size_t,
122 struct secasvar
*, const struct esp_algorithm
*, int);
123 static int esp_descbc_mature(struct secasvar
*);
124 static int esp_descbc_ivlen(const struct esp_algorithm
*,
126 static int esp_des_schedule(const struct esp_algorithm
*,
128 static int esp_des_schedlen(const struct esp_algorithm
*);
129 static int esp_des_blockdecrypt(const struct esp_algorithm
*,
130 struct secasvar
*, u_int8_t
*, u_int8_t
*);
131 static int esp_des_blockencrypt(const struct esp_algorithm
*,
132 struct secasvar
*, u_int8_t
*, u_int8_t
*);
133 static int esp_cbc_mature(struct secasvar
*);
134 static int esp_3des_schedule(const struct esp_algorithm
*,
136 static int esp_3des_schedlen(const struct esp_algorithm
*);
137 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
138 struct secasvar
*, u_int8_t
*, u_int8_t
*);
139 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
140 struct secasvar
*, u_int8_t
*, u_int8_t
*);
141 static int esp_common_ivlen(const struct esp_algorithm
*,
143 static int esp_cbc_decrypt(struct mbuf
*, size_t,
144 struct secasvar
*, const struct esp_algorithm
*, int);
145 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
146 struct secasvar
*, const struct esp_algorithm
*, int);
147 static int esp_gcm_mature(struct secasvar
*);
151 #define ESP_AESGCM_KEYLEN128 160 // 16-bytes key + 4 bytes salt
152 #define ESP_AESGCM_KEYLEN192 224 // 24-bytes key + 4 bytes salt
153 #define ESP_AESGCM_KEYLEN256 288 // 32-bytes key + 4 bytes salt
155 static const struct esp_algorithm des_cbc
= {
158 .mature
= esp_descbc_mature
,
161 .schedlen
= esp_des_schedlen
,
163 .ivlen
= esp_descbc_ivlen
,
164 .decrypt
= esp_cbc_decrypt
,
165 .encrypt
= esp_cbc_encrypt
,
166 .schedule
= esp_des_schedule
,
167 .blockdecrypt
= esp_des_blockdecrypt
,
168 .blockencrypt
= esp_des_blockencrypt
,
170 .finalizedecrypt
= NULL
,
171 .finalizeencrypt
= NULL
174 static const struct esp_algorithm des3_cbc
= {
177 .mature
= esp_cbc_mature
,
180 .schedlen
= esp_3des_schedlen
,
182 .ivlen
= esp_common_ivlen
,
183 .decrypt
= esp_cbc_decrypt
,
184 .encrypt
= esp_cbc_encrypt
,
185 .schedule
= esp_3des_schedule
,
186 .blockdecrypt
= esp_3des_blockdecrypt
,
187 .blockencrypt
= esp_3des_blockencrypt
,
189 .finalizedecrypt
= NULL
,
190 .finalizeencrypt
= NULL
193 static const struct esp_algorithm null_esp
= {
196 .mature
= esp_null_mature
,
201 .ivlen
= esp_common_ivlen
,
202 .decrypt
= esp_null_decrypt
,
203 .encrypt
= esp_null_encrypt
,
205 .blockdecrypt
= NULL
,
206 .blockencrypt
= NULL
,
208 .finalizedecrypt
= NULL
,
209 .finalizeencrypt
= NULL
212 static const struct esp_algorithm aes_cbc
= {
215 .mature
= esp_cbc_mature
,
218 .schedlen
= esp_aes_schedlen
,
220 .ivlen
= esp_common_ivlen
,
221 .decrypt
= esp_cbc_decrypt_aes
,
222 .encrypt
= esp_cbc_encrypt_aes
,
223 .schedule
= esp_aes_schedule
,
224 .blockdecrypt
= NULL
,
225 .blockencrypt
= NULL
,
227 .finalizedecrypt
= NULL
,
228 .finalizeencrypt
= NULL
231 static const struct esp_algorithm aes_gcm
= {
234 .mature
= esp_gcm_mature
,
235 .keymin
= ESP_AESGCM_KEYLEN128
,
236 .keymax
= ESP_AESGCM_KEYLEN256
,
237 .schedlen
= esp_gcm_schedlen
,
239 .ivlen
= esp_common_ivlen
,
240 .decrypt
= esp_gcm_decrypt_aes
,
241 .encrypt
= esp_gcm_encrypt_aes
,
242 .schedule
= esp_gcm_schedule
,
243 .blockdecrypt
= NULL
,
244 .blockencrypt
= NULL
,
246 .finalizedecrypt
= esp_gcm_decrypt_finalize
,
247 .finalizeencrypt
= esp_gcm_encrypt_finalize
250 static const struct esp_algorithm chacha_poly
= {
251 .padbound
= ESP_CHACHAPOLY_PAD_BOUND
,
252 .ivlenval
= ESP_CHACHAPOLY_IV_LEN
,
253 .mature
= esp_chachapoly_mature
,
254 .keymin
= ESP_CHACHAPOLY_KEYBITS_WITH_SALT
,
255 .keymax
= ESP_CHACHAPOLY_KEYBITS_WITH_SALT
,
256 .schedlen
= esp_chachapoly_schedlen
,
257 .name
= "chacha-poly",
258 .ivlen
= esp_chachapoly_ivlen
,
259 .decrypt
= esp_chachapoly_decrypt
,
260 .encrypt
= esp_chachapoly_encrypt
,
261 .schedule
= esp_chachapoly_schedule
,
262 .blockdecrypt
= NULL
,
263 .blockencrypt
= NULL
,
264 .icvlen
= ESP_CHACHAPOLY_ICV_LEN
,
265 .finalizedecrypt
= esp_chachapoly_decrypt_finalize
,
266 .finalizeencrypt
= esp_chachapoly_encrypt_finalize
269 static const struct esp_algorithm
*esp_algorithms
[] = {
278 const struct esp_algorithm
*
279 esp_algorithm_lookup(int idx
)
282 case SADB_EALG_DESCBC
:
284 case SADB_EALG_3DESCBC
:
288 case SADB_X_EALG_RIJNDAELCBC
:
290 case SADB_X_EALG_AES_GCM
:
292 case SADB_X_EALG_CHACHA20POLY1305
:
306 for (idx
= 0; idx
< sizeof(esp_algorithms
) / sizeof(esp_algorithms
[0]);
308 if (esp_algorithms
[idx
]->ivlenval
> ivlen
) {
309 ivlen
= esp_algorithms
[idx
]->ivlenval
;
317 esp_schedule(const struct esp_algorithm
*algo
, struct secasvar
*sav
)
321 /* check for key length */
322 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
323 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
325 "esp_schedule %s: unsupported key length %d: "
326 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
327 algo
->keymin
, algo
->keymax
));
331 lck_mtx_lock(sadb_mutex
);
332 /* already allocated */
333 if (sav
->sched
&& sav
->schedlen
!= 0) {
334 lck_mtx_unlock(sadb_mutex
);
338 /* prevent disallowed implicit IV */
339 if (((sav
->flags
& SADB_X_EXT_IIV
) != 0) &&
340 (sav
->alg_enc
!= SADB_X_EALG_AES_GCM
) &&
341 (sav
->alg_enc
!= SADB_X_EALG_CHACHA20POLY1305
)) {
343 "esp_schedule %s: implicit IV not allowed\n",
345 lck_mtx_unlock(sadb_mutex
);
349 /* no schedule necessary */
350 if (!algo
->schedule
|| !algo
->schedlen
) {
351 lck_mtx_unlock(sadb_mutex
);
355 sav
->schedlen
= (*algo
->schedlen
)(algo
);
356 if ((signed) sav
->schedlen
< 0) {
357 lck_mtx_unlock(sadb_mutex
);
361 //#### that malloc should be replaced by a saved buffer...
362 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
365 lck_mtx_unlock(sadb_mutex
);
369 error
= (*algo
->schedule
)(algo
, sav
);
371 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
373 bzero(sav
->sched
, sav
->schedlen
);
374 FREE(sav
->sched
, M_SECA
);
378 lck_mtx_unlock(sadb_mutex
);
384 __unused
struct secasvar
*sav
)
386 /* anything is okay */
392 __unused
struct mbuf
*m
,
393 __unused
size_t off
, /* offset to ESP header */
394 __unused
struct secasvar
*sav
,
395 __unused
const struct esp_algorithm
*algo
,
398 return 0; /* do nothing */
403 __unused
struct mbuf
*m
,
404 __unused
size_t off
, /* offset to ESP header */
405 __unused
size_t plen
, /* payload length (to be encrypted) */
406 __unused
struct secasvar
*sav
,
407 __unused
const struct esp_algorithm
*algo
,
410 return 0; /* do nothing */
414 esp_descbc_mature(struct secasvar
*sav
)
416 const struct esp_algorithm
*algo
;
418 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
419 ipseclog((LOG_ERR
, "esp_cbc_mature: "
420 "algorithm incompatible with 4 octets IV length\n"));
425 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
429 algo
= esp_algorithm_lookup(sav
->alg_enc
);
432 "esp_descbc_mature: unsupported algorithm.\n"));
436 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
437 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
439 "esp_descbc_mature: invalid key length %d.\n",
440 _KEYBITS(sav
->key_enc
)));
445 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
447 "esp_descbc_mature: weak key was passed.\n"));
456 __unused
const struct esp_algorithm
*algo
,
457 struct secasvar
*sav
)
462 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
465 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
)) {
473 __unused
const struct esp_algorithm
*algo
)
475 return sizeof(des_ecb_key_schedule
);
480 __unused
const struct esp_algorithm
*algo
,
481 struct secasvar
*sav
)
483 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
484 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
485 (des_ecb_key_schedule
*)sav
->sched
)) {
493 esp_des_blockdecrypt(
494 __unused
const struct esp_algorithm
*algo
,
495 struct secasvar
*sav
,
499 /* assumption: d has a good alignment */
500 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
501 return des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
502 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
506 esp_des_blockencrypt(
507 __unused
const struct esp_algorithm
*algo
,
508 struct secasvar
*sav
,
512 /* assumption: d has a good alignment */
513 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
514 return des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
515 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
519 esp_cbc_mature(struct secasvar
*sav
)
522 const struct esp_algorithm
*algo
;
524 if (sav
->flags
& SADB_X_EXT_OLD
) {
526 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
529 if (sav
->flags
& SADB_X_EXT_DERIV
) {
531 "esp_cbc_mature: algorithm incompatible with derived\n"));
536 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
540 algo
= esp_algorithm_lookup(sav
->alg_enc
);
543 "esp_cbc_mature: unsupported algorithm.\n"));
547 keylen
= sav
->key_enc
->sadb_key_bits
;
548 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
550 "esp_cbc_mature %s: invalid key length %d.\n",
551 algo
->name
, sav
->key_enc
->sadb_key_bits
));
554 switch (sav
->alg_enc
) {
555 case SADB_EALG_3DESCBC
:
557 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
558 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
559 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
561 "esp_cbc_mature %s: weak key was passed.\n",
566 case SADB_X_EALG_RIJNDAELCBC
:
567 /* allows specific key sizes only */
568 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
570 "esp_cbc_mature %s: invalid key length %d.\n",
571 algo
->name
, keylen
));
581 esp_gcm_mature(struct secasvar
*sav
)
584 const struct esp_algorithm
*algo
;
586 if (sav
->flags
& SADB_X_EXT_OLD
) {
588 "esp_gcm_mature: algorithm incompatible with esp-old\n"));
591 if (sav
->flags
& SADB_X_EXT_DERIV
) {
593 "esp_gcm_mature: algorithm incompatible with derived\n"));
596 if (sav
->flags
& SADB_X_EXT_IIV
) {
598 "esp_gcm_mature: implicit IV not currently implemented\n"));
603 ipseclog((LOG_ERR
, "esp_gcm_mature: no key is given.\n"));
607 algo
= esp_algorithm_lookup(sav
->alg_enc
);
610 "esp_gcm_mature: unsupported algorithm.\n"));
614 keylen
= sav
->key_enc
->sadb_key_bits
;
615 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
617 "esp_gcm_mature %s: invalid key length %d.\n",
618 algo
->name
, sav
->key_enc
->sadb_key_bits
));
621 switch (sav
->alg_enc
) {
622 case SADB_X_EALG_AES_GCM
:
623 /* allows specific key sizes only */
624 if (!(keylen
== ESP_AESGCM_KEYLEN128
|| keylen
== ESP_AESGCM_KEYLEN192
|| keylen
== ESP_AESGCM_KEYLEN256
)) {
626 "esp_gcm_mature %s: invalid key length %d.\n",
627 algo
->name
, keylen
));
633 "esp_gcm_mature %s: invalid algo %d.\n", sav
->alg_enc
));
642 __unused
const struct esp_algorithm
*algo
)
644 return sizeof(des3_ecb_key_schedule
);
649 __unused
const struct esp_algorithm
*algo
,
650 struct secasvar
*sav
)
652 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
654 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
655 (des3_ecb_key_schedule
*)sav
->sched
)) {
663 esp_3des_blockdecrypt(
664 __unused
const struct esp_algorithm
*algo
,
665 struct secasvar
*sav
,
669 /* assumption: d has a good alignment */
670 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
671 return des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
672 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
676 esp_3des_blockencrypt(
677 __unused
const struct esp_algorithm
*algo
,
678 struct secasvar
*sav
,
682 /* assumption: d has a good alignment */
683 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
684 return des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
685 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
690 const struct esp_algorithm
*algo
,
691 __unused
struct secasvar
*sav
)
694 panic("esp_common_ivlen: unknown algorithm");
696 return algo
->ivlenval
;
700 esp_cbc_decrypt(struct mbuf
*m
, size_t off
, struct secasvar
*sav
,
701 const struct esp_algorithm
*algo
, int ivlen
)
704 struct mbuf
*d
, *d0
, *dp
;
705 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
706 int sn
, dn
; /* offset from the head of the mbuf, to meat */
707 size_t ivoff
, bodyoff
;
708 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
709 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
717 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
718 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
719 "unsupported ivlen %d\n", algo
->name
, ivlen
));
724 /* assumes blocklen == padbound */
725 blocklen
= algo
->padbound
;
728 if (blocklen
> sizeof(iv
)) {
729 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
730 "unsupported blocklen %d\n", algo
->name
, blocklen
));
736 if (sav
->flags
& SADB_X_EXT_OLD
) {
738 ivoff
= off
+ sizeof(struct esp
);
739 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
743 if (sav
->flags
& SADB_X_EXT_DERIV
) {
745 * draft-ietf-ipsec-ciph-des-derived-00.txt
746 * uses sequence number field as IV field.
748 ivoff
= off
+ sizeof(struct esp
);
749 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
750 ivlen
= sizeof(u_int32_t
);
753 ivoff
= off
+ sizeof(struct newesp
);
754 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
760 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
763 if (ivlen
== blocklen
) {
765 } else if (ivlen
== 4 && blocklen
== 8) {
766 bcopy(&iv
[0], &iv
[4], 4);
772 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
773 "unsupported ivlen/blocklen: %d %d\n",
774 algo
->name
, ivlen
, blocklen
));
779 if (m
->m_pkthdr
.len
< bodyoff
) {
780 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
781 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
785 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
786 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
787 "payload length must be multiple of %d\n",
788 algo
->name
, blocklen
));
795 soff
= doff
= sn
= dn
= 0;
799 while (soff
< bodyoff
) {
800 if (soff
+ s
->m_len
> bodyoff
) {
811 /* skip over empty mbuf */
812 while (s
&& s
->m_len
== 0) {
816 // Allocate blocksized buffer for unaligned or non-contiguous access
817 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
821 while (soff
< m
->m_pkthdr
.len
) {
823 if (sn
+ blocklen
<= s
->m_len
) {
824 /* body is continuous */
825 sp
= mtod(s
, u_int8_t
*) + sn
;
827 /* body is non-continuous */
828 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
833 if (!d
|| dn
+ blocklen
> d
->m_len
) {
837 MGET(d
, M_DONTWAIT
, MT_DATA
);
838 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
840 MCLGET(d
, M_DONTWAIT
);
841 if ((d
->m_flags
& M_EXT
) == 0) {
861 // try to make mbuf data aligned
862 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
863 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
867 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
875 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
876 if (IPSEC_IS_P2ALIGNED(sp
)) {
881 memcpy(sp
, sp_unaligned
, blocklen
);
883 // no need to check output pointer alignment
884 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
886 // update unaligned pointers
887 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
893 q
= mtod(d
, u_int8_t
*) + dn
;
894 for (i
= 0; i
< blocklen
; i
++) {
900 bcopy(sbuf
, iv
, blocklen
);
909 /* find the next source block */
910 while (s
&& sn
>= s
->m_len
) {
917 m_freem(scut
->m_next
);
918 scut
->m_len
= scutoff
;
922 bzero(iv
, sizeof(iv
));
923 bzero(sbuf
, blocklen
);
935 __unused
size_t plen
,
936 struct secasvar
*sav
,
937 const struct esp_algorithm
*algo
,
941 struct mbuf
*d
, *d0
, *dp
;
942 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
943 int sn
, dn
; /* offset from the head of the mbuf, to meat */
944 size_t ivoff
, bodyoff
;
945 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
946 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
954 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
955 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
956 "unsupported ivlen %d\n", algo
->name
, ivlen
));
961 /* assumes blocklen == padbound */
962 blocklen
= algo
->padbound
;
965 if (blocklen
> sizeof(iv
)) {
966 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
967 "unsupported blocklen %d\n", algo
->name
, blocklen
));
973 if (sav
->flags
& SADB_X_EXT_OLD
) {
975 ivoff
= off
+ sizeof(struct esp
);
976 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
980 if (sav
->flags
& SADB_X_EXT_DERIV
) {
982 * draft-ietf-ipsec-ciph-des-derived-00.txt
983 * uses sequence number field as IV field.
985 ivoff
= off
+ sizeof(struct esp
);
986 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
987 ivlen
= sizeof(u_int32_t
);
990 ivoff
= off
+ sizeof(struct newesp
);
991 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
996 /* put iv into the packet. if we are in derived mode, use seqno. */
998 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
1000 bcopy(sav
->iv
, iv
, ivlen
);
1001 /* maybe it is better to overwrite dest, not source */
1002 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
1006 if (ivlen
== blocklen
) {
1008 } else if (ivlen
== 4 && blocklen
== 8) {
1009 bcopy(&iv
[0], &iv
[4], 4);
1015 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
1016 "unsupported ivlen/blocklen: %d %d\n",
1017 algo
->name
, ivlen
, blocklen
));
1022 if (m
->m_pkthdr
.len
< bodyoff
) {
1023 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
1024 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
1028 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
1029 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
1030 "payload length must be multiple of %lu\n",
1031 algo
->name
, (u_int32_t
)algo
->padbound
));
1038 soff
= doff
= sn
= dn
= 0;
1042 while (soff
< bodyoff
) {
1043 if (soff
+ s
->m_len
> bodyoff
) {
1044 sn
= bodyoff
- soff
;
1054 /* skip over empty mbuf */
1055 while (s
&& s
->m_len
== 0) {
1059 // Allocate blocksized buffer for unaligned or non-contiguous access
1060 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
1064 while (soff
< m
->m_pkthdr
.len
) {
1066 if (sn
+ blocklen
<= s
->m_len
) {
1067 /* body is continuous */
1068 sp
= mtod(s
, u_int8_t
*) + sn
;
1070 /* body is non-continuous */
1071 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
1076 if (!d
|| dn
+ blocklen
> d
->m_len
) {
1080 MGET(d
, M_DONTWAIT
, MT_DATA
);
1081 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
1082 if (d
&& i
> MLEN
) {
1083 MCLGET(d
, M_DONTWAIT
);
1084 if ((d
->m_flags
& M_EXT
) == 0) {
1104 // try to make mbuf data aligned
1105 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
1106 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
1110 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
1120 for (i
= 0; i
< blocklen
; i
++) {
1125 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
1126 if (IPSEC_IS_P2ALIGNED(sp
)) {
1127 sp_unaligned
= NULL
;
1131 memcpy(sp
, sp_unaligned
, blocklen
);
1133 // no need to check output pointer alignment
1134 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1136 // update unaligned pointers
1137 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1142 ivp
= mtod(d
, u_int8_t
*) + dn
;
1147 /* find the next source block */
1148 while (s
&& sn
>= s
->m_len
) {
1155 m_freem(scut
->m_next
);
1156 scut
->m_len
= scutoff
;
1160 bzero(iv
, sizeof(iv
));
1161 bzero(sbuf
, blocklen
);
1163 key_sa_stir_iv(sav
);
1171 /*------------------------------------------------------------*/
1173 /* does not free m0 on error */
1177 size_t skip
, /* offset to ESP header */
1178 size_t length
, /* payload length */
1179 struct secasvar
*sav
,
1184 struct ah_algorithm_state s
;
1185 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1186 const struct ah_algorithm
*algo
;
1191 if (m0
->m_pkthdr
.len
< skip
) {
1192 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1195 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1196 ipseclog((LOG_DEBUG
,
1197 "esp_auth: mbuf length < skip + length\n"));
1201 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
, length
, 0, 0, 0);
1203 * length of esp part (excluding authentication data) must be 4n,
1204 * since nexthdr must be at offset 4n+3.
1207 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1208 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1, 0, 0, 0, 0);
1212 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1213 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2, 0, 0, 0, 0);
1216 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1219 "esp_auth: bad ESP auth algorithm passed: %d\n",
1221 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3, 0, 0, 0, 0);
1228 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1229 if (sizeof(sumbuf
) < siz
) {
1230 ipseclog((LOG_DEBUG
,
1231 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1233 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4, 0, 0, 0, 0);
1237 /* skip the header */
1240 panic("mbuf chain?");
1242 if (m
->m_len
<= skip
) {
1252 error
= (*algo
->init
)(&s
, sav
);
1254 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5, 0, 0, 0, 0);
1257 while (0 < length
) {
1259 panic("mbuf chain?");
1262 if (m
->m_len
- off
< length
) {
1263 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1265 length
-= m
->m_len
- off
;
1269 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1273 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1274 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1275 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6, 0, 0, 0, 0);
1282 static int esp_initialized
= 0;
1284 if (esp_initialized
) {
1288 esp_initialized
= 1;
1290 esp_mpkl_log_object
= MPKL_CREATE_LOGOBJECT("com.apple.xnu.esp");
1291 if (esp_mpkl_log_object
== NULL
) {
1292 panic("MPKL_CREATE_LOGOBJECT for ESP failed");