2 * Copyright (c) 2008-2016 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
;
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
*);
133 static int esp_3des_schedule(const struct esp_algorithm
*,
135 static int esp_3des_schedlen(const struct esp_algorithm
*);
136 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
137 struct secasvar
*, u_int8_t
*, u_int8_t
*);
138 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
139 struct secasvar
*, u_int8_t
*, u_int8_t
*);
140 static int esp_common_ivlen(const struct esp_algorithm
*,
142 static int esp_cbc_decrypt(struct mbuf
*, size_t,
143 struct secasvar
*, const struct esp_algorithm
*, int);
144 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
145 struct secasvar
*, const struct esp_algorithm
*, int);
146 static int esp_gcm_mature(struct secasvar
*);
150 #define ESP_AESGCM_KEYLEN128 160 // 16-bytes key + 4 bytes salt
151 #define ESP_AESGCM_KEYLEN192 224 // 24-bytes key + 4 bytes salt
152 #define ESP_AESGCM_KEYLEN256 288 // 32-bytes key + 4 bytes salt
154 static const struct esp_algorithm des_cbc
=
155 { 8, -1, esp_descbc_mature
, 64, 64, esp_des_schedlen
,
157 esp_descbc_ivlen
, esp_cbc_decrypt
,
158 esp_cbc_encrypt
, esp_des_schedule
,
159 esp_des_blockdecrypt
, esp_des_blockencrypt
,
161 static const struct esp_algorithm des3_cbc
=
162 { 8, 8, esp_cbc_mature
, 192, 192, esp_3des_schedlen
,
164 esp_common_ivlen
, esp_cbc_decrypt
,
165 esp_cbc_encrypt
, esp_3des_schedule
,
166 esp_3des_blockdecrypt
, esp_3des_blockencrypt
,
168 static const struct esp_algorithm null_esp
=
169 { 1, 0, esp_null_mature
, 0, 2048, 0, "null",
170 esp_common_ivlen
, esp_null_decrypt
,
171 esp_null_encrypt
, NULL
, NULL
, NULL
,
173 static const struct esp_algorithm aes_cbc
=
174 { 16, 16, esp_cbc_mature
, 128, 256, esp_aes_schedlen
,
176 esp_common_ivlen
, esp_cbc_decrypt_aes
,
177 esp_cbc_encrypt_aes
, esp_aes_schedule
,
180 static const struct esp_algorithm aes_gcm
=
181 { 4, 8, esp_gcm_mature
, ESP_AESGCM_KEYLEN128
, ESP_AESGCM_KEYLEN256
, esp_gcm_schedlen
,
183 esp_common_ivlen
, esp_gcm_decrypt_aes
,
184 esp_gcm_encrypt_aes
, esp_gcm_schedule
,
186 16, esp_gcm_decrypt_finalize
, esp_gcm_encrypt_finalize
};
187 static const struct esp_algorithm chacha_poly
=
188 { ESP_CHACHAPOLY_PAD_BOUND
, ESP_CHACHAPOLY_IV_LEN
,
189 esp_chachapoly_mature
, ESP_CHACHAPOLY_KEYBITS_WITH_SALT
,
190 ESP_CHACHAPOLY_KEYBITS_WITH_SALT
, esp_chachapoly_schedlen
,
191 "chacha-poly", esp_common_ivlen
, esp_chachapoly_decrypt
,
192 esp_chachapoly_encrypt
, esp_chachapoly_schedule
,
193 NULL
, NULL
, ESP_CHACHAPOLY_ICV_LEN
,
194 esp_chachapoly_decrypt_finalize
, esp_chachapoly_encrypt_finalize
};
196 static const struct esp_algorithm
*esp_algorithms
[] = {
205 const struct esp_algorithm
*
206 esp_algorithm_lookup(int idx
)
209 case SADB_EALG_DESCBC
:
211 case SADB_EALG_3DESCBC
:
215 case SADB_X_EALG_RIJNDAELCBC
:
217 case SADB_X_EALG_AES_GCM
:
219 case SADB_X_EALG_CHACHA20POLY1305
:
233 for (idx
= 0; idx
< sizeof(esp_algorithms
)/sizeof(esp_algorithms
[0]);
235 if (esp_algorithms
[idx
]->ivlenval
> ivlen
)
236 ivlen
= esp_algorithms
[idx
]->ivlenval
;
243 esp_schedule(const struct esp_algorithm
*algo
, struct secasvar
*sav
)
247 /* check for key length */
248 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
249 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
251 "esp_schedule %s: unsupported key length %d: "
252 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
253 algo
->keymin
, algo
->keymax
));
257 lck_mtx_lock(sadb_mutex
);
258 /* already allocated */
259 if (sav
->sched
&& sav
->schedlen
!= 0) {
260 lck_mtx_unlock(sadb_mutex
);
264 /* prevent disallowed implicit IV */
265 if (((sav
->flags
& SADB_X_EXT_IIV
) != 0) &&
266 (sav
->alg_enc
!= SADB_X_EALG_AES_GCM
) &&
267 (sav
->alg_enc
!= SADB_X_EALG_CHACHA20POLY1305
)) {
269 "esp_schedule %s: implicit IV not allowed\n",
274 /* no schedule necessary */
275 if (!algo
->schedule
|| !algo
->schedlen
) {
276 lck_mtx_unlock(sadb_mutex
);
280 sav
->schedlen
= (*algo
->schedlen
)(algo
);
281 if ((signed) sav
->schedlen
< 0) {
282 lck_mtx_unlock(sadb_mutex
);
286 //#### that malloc should be replaced by a saved buffer...
287 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
290 lck_mtx_unlock(sadb_mutex
);
294 error
= (*algo
->schedule
)(algo
, sav
);
296 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
298 bzero(sav
->sched
, sav
->schedlen
);
299 FREE(sav
->sched
, M_SECA
);
303 lck_mtx_unlock(sadb_mutex
);
309 __unused
struct secasvar
*sav
)
312 /* anything is okay */
318 __unused
struct mbuf
*m
,
319 __unused
size_t off
, /* offset to ESP header */
320 __unused
struct secasvar
*sav
,
321 __unused
const struct esp_algorithm
*algo
,
325 return 0; /* do nothing */
330 __unused
struct mbuf
*m
,
331 __unused
size_t off
, /* offset to ESP header */
332 __unused
size_t plen
, /* payload length (to be encrypted) */
333 __unused
struct secasvar
*sav
,
334 __unused
const struct esp_algorithm
*algo
,
338 return 0; /* do nothing */
342 esp_descbc_mature(struct secasvar
*sav
)
344 const struct esp_algorithm
*algo
;
346 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
347 ipseclog((LOG_ERR
, "esp_cbc_mature: "
348 "algorithm incompatible with 4 octets IV length\n"));
353 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
357 algo
= esp_algorithm_lookup(sav
->alg_enc
);
360 "esp_descbc_mature: unsupported algorithm.\n"));
364 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
365 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
367 "esp_descbc_mature: invalid key length %d.\n",
368 _KEYBITS(sav
->key_enc
)));
373 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
375 "esp_descbc_mature: weak key was passed.\n"));
384 __unused
const struct esp_algorithm
*algo
,
385 struct secasvar
*sav
)
390 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
392 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
399 __unused
const struct esp_algorithm
*algo
)
401 return sizeof(des_ecb_key_schedule
);
406 __unused
const struct esp_algorithm
*algo
,
407 struct secasvar
*sav
)
410 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
411 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
412 (des_ecb_key_schedule
*)sav
->sched
))
419 esp_des_blockdecrypt(
420 __unused
const struct esp_algorithm
*algo
,
421 struct secasvar
*sav
,
425 /* assumption: d has a good alignment */
426 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
427 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
428 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
433 esp_des_blockencrypt(
434 __unused
const struct esp_algorithm
*algo
,
435 struct secasvar
*sav
,
439 /* assumption: d has a good alignment */
440 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
441 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
442 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
447 esp_cbc_mature(struct secasvar
*sav
)
450 const struct esp_algorithm
*algo
;
452 if (sav
->flags
& SADB_X_EXT_OLD
) {
454 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
457 if (sav
->flags
& SADB_X_EXT_DERIV
) {
459 "esp_cbc_mature: algorithm incompatible with derived\n"));
464 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
468 algo
= esp_algorithm_lookup(sav
->alg_enc
);
471 "esp_cbc_mature: unsupported algorithm.\n"));
475 keylen
= sav
->key_enc
->sadb_key_bits
;
476 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
478 "esp_cbc_mature %s: invalid key length %d.\n",
479 algo
->name
, sav
->key_enc
->sadb_key_bits
));
482 switch (sav
->alg_enc
) {
483 case SADB_EALG_3DESCBC
:
485 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
486 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
487 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
489 "esp_cbc_mature %s: weak key was passed.\n",
494 case SADB_X_EALG_RIJNDAELCBC
:
495 /* allows specific key sizes only */
496 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
498 "esp_cbc_mature %s: invalid key length %d.\n",
499 algo
->name
, keylen
));
509 esp_gcm_mature(struct secasvar
*sav
)
512 const struct esp_algorithm
*algo
;
514 if (sav
->flags
& SADB_X_EXT_OLD
) {
516 "esp_gcm_mature: algorithm incompatible with esp-old\n"));
519 if (sav
->flags
& SADB_X_EXT_DERIV
) {
521 "esp_gcm_mature: algorithm incompatible with derived\n"));
524 if (sav
->flags
& SADB_X_EXT_IIV
) {
526 "esp_gcm_mature: implicit IV not currently implemented\n"));
531 ipseclog((LOG_ERR
, "esp_gcm_mature: no key is given.\n"));
535 algo
= esp_algorithm_lookup(sav
->alg_enc
);
538 "esp_gcm_mature: unsupported algorithm.\n"));
542 keylen
= sav
->key_enc
->sadb_key_bits
;
543 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
545 "esp_gcm_mature %s: invalid key length %d.\n",
546 algo
->name
, sav
->key_enc
->sadb_key_bits
));
549 switch (sav
->alg_enc
) {
550 case SADB_X_EALG_AES_GCM
:
551 /* allows specific key sizes only */
552 if (!(keylen
== ESP_AESGCM_KEYLEN128
|| keylen
== ESP_AESGCM_KEYLEN192
|| keylen
== ESP_AESGCM_KEYLEN256
)) {
554 "esp_gcm_mature %s: invalid key length %d.\n",
555 algo
->name
, keylen
));
561 "esp_gcm_mature %s: invalid algo %d.\n", sav
->alg_enc
));
570 __unused
const struct esp_algorithm
*algo
)
573 return sizeof(des3_ecb_key_schedule
);
578 __unused
const struct esp_algorithm
*algo
,
579 struct secasvar
*sav
)
581 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
583 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
584 (des3_ecb_key_schedule
*)sav
->sched
))
591 esp_3des_blockdecrypt(
592 __unused
const struct esp_algorithm
*algo
,
593 struct secasvar
*sav
,
597 /* assumption: d has a good alignment */
598 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
599 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
600 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
605 esp_3des_blockencrypt(
606 __unused
const struct esp_algorithm
*algo
,
607 struct secasvar
*sav
,
611 /* assumption: d has a good alignment */
612 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
613 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
614 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
620 const struct esp_algorithm
*algo
,
621 __unused
struct secasvar
*sav
)
625 panic("esp_common_ivlen: unknown algorithm");
626 return algo
->ivlenval
;
630 esp_cbc_decrypt(struct mbuf
*m
, size_t off
, struct secasvar
*sav
,
631 const struct esp_algorithm
*algo
, int ivlen
)
634 struct mbuf
*d
, *d0
, *dp
;
635 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
636 int sn
, dn
; /* offset from the head of the mbuf, to meat */
637 size_t ivoff
, bodyoff
;
638 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
639 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
647 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
648 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
649 "unsupported ivlen %d\n", algo
->name
, ivlen
));
654 /* assumes blocklen == padbound */
655 blocklen
= algo
->padbound
;
658 if (blocklen
> sizeof(iv
)) {
659 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
660 "unsupported blocklen %d\n", algo
->name
, blocklen
));
666 if (sav
->flags
& SADB_X_EXT_OLD
) {
668 ivoff
= off
+ sizeof(struct esp
);
669 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
673 if (sav
->flags
& SADB_X_EXT_DERIV
) {
675 * draft-ietf-ipsec-ciph-des-derived-00.txt
676 * uses sequence number field as IV field.
678 ivoff
= off
+ sizeof(struct esp
);
679 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
680 ivlen
= sizeof(u_int32_t
);
683 ivoff
= off
+ sizeof(struct newesp
);
684 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
690 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
693 if (ivlen
== blocklen
)
695 else if (ivlen
== 4 && blocklen
== 8) {
696 bcopy(&iv
[0], &iv
[4], 4);
702 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
703 "unsupported ivlen/blocklen: %d %d\n",
704 algo
->name
, ivlen
, blocklen
));
709 if (m
->m_pkthdr
.len
< bodyoff
) {
710 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
711 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
715 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
716 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
717 "payload length must be multiple of %d\n",
718 algo
->name
, blocklen
));
725 soff
= doff
= sn
= dn
= 0;
729 while (soff
< bodyoff
) {
730 if (soff
+ s
->m_len
> bodyoff
) {
741 /* skip over empty mbuf */
742 while (s
&& s
->m_len
== 0)
745 // Allocate blocksized buffer for unaligned or non-contiguous access
746 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
749 while (soff
< m
->m_pkthdr
.len
) {
751 if (sn
+ blocklen
<= s
->m_len
) {
752 /* body is continuous */
753 sp
= mtod(s
, u_int8_t
*) + sn
;
755 /* body is non-continuous */
756 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
761 if (!d
|| dn
+ blocklen
> d
->m_len
) {
764 MGET(d
, M_DONTWAIT
, MT_DATA
);
765 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
767 MCLGET(d
, M_DONTWAIT
);
768 if ((d
->m_flags
& M_EXT
) == 0) {
785 // try to make mbuf data aligned
786 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
787 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
791 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
798 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
799 if (IPSEC_IS_P2ALIGNED(sp
)) {
804 memcpy(sp
, sp_unaligned
, blocklen
);
806 // no need to check output pointer alignment
807 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
809 // update unaligned pointers
810 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
816 q
= mtod(d
, u_int8_t
*) + dn
;
817 for (i
= 0; i
< blocklen
; i
++)
822 bcopy(sbuf
, iv
, blocklen
);
830 /* find the next source block */
831 while (s
&& sn
>= s
->m_len
) {
838 m_freem(scut
->m_next
);
839 scut
->m_len
= scutoff
;
843 bzero(iv
, sizeof(iv
));
844 bzero(sbuf
, blocklen
);
855 __unused
size_t plen
,
856 struct secasvar
*sav
,
857 const struct esp_algorithm
*algo
,
861 struct mbuf
*d
, *d0
, *dp
;
862 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
863 int sn
, dn
; /* offset from the head of the mbuf, to meat */
864 size_t ivoff
, bodyoff
;
865 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
866 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
874 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
875 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
876 "unsupported ivlen %d\n", algo
->name
, ivlen
));
881 /* assumes blocklen == padbound */
882 blocklen
= algo
->padbound
;
885 if (blocklen
> sizeof(iv
)) {
886 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
887 "unsupported blocklen %d\n", algo
->name
, blocklen
));
893 if (sav
->flags
& SADB_X_EXT_OLD
) {
895 ivoff
= off
+ sizeof(struct esp
);
896 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
900 if (sav
->flags
& SADB_X_EXT_DERIV
) {
902 * draft-ietf-ipsec-ciph-des-derived-00.txt
903 * uses sequence number field as IV field.
905 ivoff
= off
+ sizeof(struct esp
);
906 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
907 ivlen
= sizeof(u_int32_t
);
910 ivoff
= off
+ sizeof(struct newesp
);
911 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
916 /* put iv into the packet. if we are in derived mode, use seqno. */
918 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
920 bcopy(sav
->iv
, iv
, ivlen
);
921 /* maybe it is better to overwrite dest, not source */
922 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
926 if (ivlen
== blocklen
)
928 else if (ivlen
== 4 && blocklen
== 8) {
929 bcopy(&iv
[0], &iv
[4], 4);
935 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
936 "unsupported ivlen/blocklen: %d %d\n",
937 algo
->name
, ivlen
, blocklen
));
942 if (m
->m_pkthdr
.len
< bodyoff
) {
943 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
944 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
948 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
949 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
950 "payload length must be multiple of %lu\n",
951 algo
->name
, (u_int32_t
)algo
->padbound
));
958 soff
= doff
= sn
= dn
= 0;
962 while (soff
< bodyoff
) {
963 if (soff
+ s
->m_len
> bodyoff
) {
974 /* skip over empty mbuf */
975 while (s
&& s
->m_len
== 0)
978 // Allocate blocksized buffer for unaligned or non-contiguous access
979 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
982 while (soff
< m
->m_pkthdr
.len
) {
984 if (sn
+ blocklen
<= s
->m_len
) {
985 /* body is continuous */
986 sp
= mtod(s
, u_int8_t
*) + sn
;
988 /* body is non-continuous */
989 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
994 if (!d
|| dn
+ blocklen
> d
->m_len
) {
997 MGET(d
, M_DONTWAIT
, MT_DATA
);
998 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
1000 MCLGET(d
, M_DONTWAIT
);
1001 if ((d
->m_flags
& M_EXT
) == 0) {
1018 // try to make mbuf data aligned
1019 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
1020 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
1024 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
1033 for (i
= 0; i
< blocklen
; i
++)
1037 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
1038 if (IPSEC_IS_P2ALIGNED(sp
)) {
1039 sp_unaligned
= NULL
;
1043 memcpy(sp
, sp_unaligned
, blocklen
);
1045 // no need to check output pointer alignment
1046 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1048 // update unaligned pointers
1049 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1054 ivp
= mtod(d
, u_int8_t
*) + dn
;
1059 /* find the next source block */
1060 while (s
&& sn
>= s
->m_len
) {
1067 m_freem(scut
->m_next
);
1068 scut
->m_len
= scutoff
;
1072 bzero(iv
, sizeof(iv
));
1073 bzero(sbuf
, blocklen
);
1075 key_sa_stir_iv(sav
);
1082 /*------------------------------------------------------------*/
1084 /* does not free m0 on error */
1088 size_t skip
, /* offset to ESP header */
1089 size_t length
, /* payload length */
1090 struct secasvar
*sav
,
1095 struct ah_algorithm_state s
;
1096 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1097 const struct ah_algorithm
*algo
;
1102 if (m0
->m_pkthdr
.len
< skip
) {
1103 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1106 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1107 ipseclog((LOG_DEBUG
,
1108 "esp_auth: mbuf length < skip + length\n"));
1112 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
,length
,0,0,0);
1114 * length of esp part (excluding authentication data) must be 4n,
1115 * since nexthdr must be at offset 4n+3.
1118 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1119 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1,0,0,0,0);
1123 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1124 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2,0,0,0,0);
1127 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1130 "esp_auth: bad ESP auth algorithm passed: %d\n",
1132 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3,0,0,0,0);
1139 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1140 if (sizeof(sumbuf
) < siz
) {
1141 ipseclog((LOG_DEBUG
,
1142 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1144 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4,0,0,0,0);
1148 /* skip the header */
1151 panic("mbuf chain?");
1152 if (m
->m_len
<= skip
) {
1162 error
= (*algo
->init
)(&s
, sav
);
1164 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5,0,0,0,0);
1167 while (0 < length
) {
1169 panic("mbuf chain?");
1171 if (m
->m_len
- off
< length
) {
1172 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1174 length
-= m
->m_len
- off
;
1178 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1182 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1183 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1184 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6,0,0,0,0);