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_chachapoly_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",
271 lck_mtx_unlock(sadb_mutex
);
275 /* no schedule necessary */
276 if (!algo
->schedule
|| !algo
->schedlen
) {
277 lck_mtx_unlock(sadb_mutex
);
281 sav
->schedlen
= (*algo
->schedlen
)(algo
);
282 if ((signed) sav
->schedlen
< 0) {
283 lck_mtx_unlock(sadb_mutex
);
287 //#### that malloc should be replaced by a saved buffer...
288 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
291 lck_mtx_unlock(sadb_mutex
);
295 error
= (*algo
->schedule
)(algo
, sav
);
297 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
299 bzero(sav
->sched
, sav
->schedlen
);
300 FREE(sav
->sched
, M_SECA
);
304 lck_mtx_unlock(sadb_mutex
);
310 __unused
struct secasvar
*sav
)
313 /* anything is okay */
319 __unused
struct mbuf
*m
,
320 __unused
size_t off
, /* offset to ESP header */
321 __unused
struct secasvar
*sav
,
322 __unused
const struct esp_algorithm
*algo
,
326 return 0; /* do nothing */
331 __unused
struct mbuf
*m
,
332 __unused
size_t off
, /* offset to ESP header */
333 __unused
size_t plen
, /* payload length (to be encrypted) */
334 __unused
struct secasvar
*sav
,
335 __unused
const struct esp_algorithm
*algo
,
339 return 0; /* do nothing */
343 esp_descbc_mature(struct secasvar
*sav
)
345 const struct esp_algorithm
*algo
;
347 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
348 ipseclog((LOG_ERR
, "esp_cbc_mature: "
349 "algorithm incompatible with 4 octets IV length\n"));
354 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
358 algo
= esp_algorithm_lookup(sav
->alg_enc
);
361 "esp_descbc_mature: unsupported algorithm.\n"));
365 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
366 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
368 "esp_descbc_mature: invalid key length %d.\n",
369 _KEYBITS(sav
->key_enc
)));
374 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
376 "esp_descbc_mature: weak key was passed.\n"));
385 __unused
const struct esp_algorithm
*algo
,
386 struct secasvar
*sav
)
391 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
393 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
400 __unused
const struct esp_algorithm
*algo
)
402 return sizeof(des_ecb_key_schedule
);
407 __unused
const struct esp_algorithm
*algo
,
408 struct secasvar
*sav
)
411 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
412 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
413 (des_ecb_key_schedule
*)sav
->sched
))
420 esp_des_blockdecrypt(
421 __unused
const struct esp_algorithm
*algo
,
422 struct secasvar
*sav
,
426 /* assumption: d has a good alignment */
427 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
428 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
429 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
434 esp_des_blockencrypt(
435 __unused
const struct esp_algorithm
*algo
,
436 struct secasvar
*sav
,
440 /* assumption: d has a good alignment */
441 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
442 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
443 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
448 esp_cbc_mature(struct secasvar
*sav
)
451 const struct esp_algorithm
*algo
;
453 if (sav
->flags
& SADB_X_EXT_OLD
) {
455 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
458 if (sav
->flags
& SADB_X_EXT_DERIV
) {
460 "esp_cbc_mature: algorithm incompatible with derived\n"));
465 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
469 algo
= esp_algorithm_lookup(sav
->alg_enc
);
472 "esp_cbc_mature: unsupported algorithm.\n"));
476 keylen
= sav
->key_enc
->sadb_key_bits
;
477 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
479 "esp_cbc_mature %s: invalid key length %d.\n",
480 algo
->name
, sav
->key_enc
->sadb_key_bits
));
483 switch (sav
->alg_enc
) {
484 case SADB_EALG_3DESCBC
:
486 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
487 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
488 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
490 "esp_cbc_mature %s: weak key was passed.\n",
495 case SADB_X_EALG_RIJNDAELCBC
:
496 /* allows specific key sizes only */
497 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
499 "esp_cbc_mature %s: invalid key length %d.\n",
500 algo
->name
, keylen
));
510 esp_gcm_mature(struct secasvar
*sav
)
513 const struct esp_algorithm
*algo
;
515 if (sav
->flags
& SADB_X_EXT_OLD
) {
517 "esp_gcm_mature: algorithm incompatible with esp-old\n"));
520 if (sav
->flags
& SADB_X_EXT_DERIV
) {
522 "esp_gcm_mature: algorithm incompatible with derived\n"));
525 if (sav
->flags
& SADB_X_EXT_IIV
) {
527 "esp_gcm_mature: implicit IV not currently implemented\n"));
532 ipseclog((LOG_ERR
, "esp_gcm_mature: no key is given.\n"));
536 algo
= esp_algorithm_lookup(sav
->alg_enc
);
539 "esp_gcm_mature: unsupported algorithm.\n"));
543 keylen
= sav
->key_enc
->sadb_key_bits
;
544 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
546 "esp_gcm_mature %s: invalid key length %d.\n",
547 algo
->name
, sav
->key_enc
->sadb_key_bits
));
550 switch (sav
->alg_enc
) {
551 case SADB_X_EALG_AES_GCM
:
552 /* allows specific key sizes only */
553 if (!(keylen
== ESP_AESGCM_KEYLEN128
|| keylen
== ESP_AESGCM_KEYLEN192
|| keylen
== ESP_AESGCM_KEYLEN256
)) {
555 "esp_gcm_mature %s: invalid key length %d.\n",
556 algo
->name
, keylen
));
562 "esp_gcm_mature %s: invalid algo %d.\n", sav
->alg_enc
));
571 __unused
const struct esp_algorithm
*algo
)
574 return sizeof(des3_ecb_key_schedule
);
579 __unused
const struct esp_algorithm
*algo
,
580 struct secasvar
*sav
)
582 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
584 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
585 (des3_ecb_key_schedule
*)sav
->sched
))
592 esp_3des_blockdecrypt(
593 __unused
const struct esp_algorithm
*algo
,
594 struct secasvar
*sav
,
598 /* assumption: d has a good alignment */
599 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
600 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
601 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
606 esp_3des_blockencrypt(
607 __unused
const struct esp_algorithm
*algo
,
608 struct secasvar
*sav
,
612 /* assumption: d has a good alignment */
613 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
614 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
615 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
621 const struct esp_algorithm
*algo
,
622 __unused
struct secasvar
*sav
)
626 panic("esp_common_ivlen: unknown algorithm");
627 return algo
->ivlenval
;
631 esp_cbc_decrypt(struct mbuf
*m
, size_t off
, struct secasvar
*sav
,
632 const struct esp_algorithm
*algo
, int ivlen
)
635 struct mbuf
*d
, *d0
, *dp
;
636 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
637 int sn
, dn
; /* offset from the head of the mbuf, to meat */
638 size_t ivoff
, bodyoff
;
639 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
640 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
648 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
649 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
650 "unsupported ivlen %d\n", algo
->name
, ivlen
));
655 /* assumes blocklen == padbound */
656 blocklen
= algo
->padbound
;
659 if (blocklen
> sizeof(iv
)) {
660 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
661 "unsupported blocklen %d\n", algo
->name
, blocklen
));
667 if (sav
->flags
& SADB_X_EXT_OLD
) {
669 ivoff
= off
+ sizeof(struct esp
);
670 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
674 if (sav
->flags
& SADB_X_EXT_DERIV
) {
676 * draft-ietf-ipsec-ciph-des-derived-00.txt
677 * uses sequence number field as IV field.
679 ivoff
= off
+ sizeof(struct esp
);
680 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
681 ivlen
= sizeof(u_int32_t
);
684 ivoff
= off
+ sizeof(struct newesp
);
685 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
691 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
694 if (ivlen
== blocklen
)
696 else if (ivlen
== 4 && blocklen
== 8) {
697 bcopy(&iv
[0], &iv
[4], 4);
703 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
704 "unsupported ivlen/blocklen: %d %d\n",
705 algo
->name
, ivlen
, blocklen
));
710 if (m
->m_pkthdr
.len
< bodyoff
) {
711 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
712 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
716 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
717 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
718 "payload length must be multiple of %d\n",
719 algo
->name
, blocklen
));
726 soff
= doff
= sn
= dn
= 0;
730 while (soff
< bodyoff
) {
731 if (soff
+ s
->m_len
> bodyoff
) {
742 /* skip over empty mbuf */
743 while (s
&& s
->m_len
== 0)
746 // Allocate blocksized buffer for unaligned or non-contiguous access
747 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
750 while (soff
< m
->m_pkthdr
.len
) {
752 if (sn
+ blocklen
<= s
->m_len
) {
753 /* body is continuous */
754 sp
= mtod(s
, u_int8_t
*) + sn
;
756 /* body is non-continuous */
757 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
762 if (!d
|| dn
+ blocklen
> d
->m_len
) {
765 MGET(d
, M_DONTWAIT
, MT_DATA
);
766 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
768 MCLGET(d
, M_DONTWAIT
);
769 if ((d
->m_flags
& M_EXT
) == 0) {
786 // try to make mbuf data aligned
787 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
788 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
792 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
799 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
800 if (IPSEC_IS_P2ALIGNED(sp
)) {
805 memcpy(sp
, sp_unaligned
, blocklen
);
807 // no need to check output pointer alignment
808 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
810 // update unaligned pointers
811 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
817 q
= mtod(d
, u_int8_t
*) + dn
;
818 for (i
= 0; i
< blocklen
; i
++)
823 bcopy(sbuf
, iv
, blocklen
);
831 /* find the next source block */
832 while (s
&& sn
>= s
->m_len
) {
839 m_freem(scut
->m_next
);
840 scut
->m_len
= scutoff
;
844 bzero(iv
, sizeof(iv
));
845 bzero(sbuf
, blocklen
);
856 __unused
size_t plen
,
857 struct secasvar
*sav
,
858 const struct esp_algorithm
*algo
,
862 struct mbuf
*d
, *d0
, *dp
;
863 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
864 int sn
, dn
; /* offset from the head of the mbuf, to meat */
865 size_t ivoff
, bodyoff
;
866 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
867 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
875 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
876 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
877 "unsupported ivlen %d\n", algo
->name
, ivlen
));
882 /* assumes blocklen == padbound */
883 blocklen
= algo
->padbound
;
886 if (blocklen
> sizeof(iv
)) {
887 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
888 "unsupported blocklen %d\n", algo
->name
, blocklen
));
894 if (sav
->flags
& SADB_X_EXT_OLD
) {
896 ivoff
= off
+ sizeof(struct esp
);
897 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
901 if (sav
->flags
& SADB_X_EXT_DERIV
) {
903 * draft-ietf-ipsec-ciph-des-derived-00.txt
904 * uses sequence number field as IV field.
906 ivoff
= off
+ sizeof(struct esp
);
907 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
908 ivlen
= sizeof(u_int32_t
);
911 ivoff
= off
+ sizeof(struct newesp
);
912 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
917 /* put iv into the packet. if we are in derived mode, use seqno. */
919 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
921 bcopy(sav
->iv
, iv
, ivlen
);
922 /* maybe it is better to overwrite dest, not source */
923 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
927 if (ivlen
== blocklen
)
929 else if (ivlen
== 4 && blocklen
== 8) {
930 bcopy(&iv
[0], &iv
[4], 4);
936 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
937 "unsupported ivlen/blocklen: %d %d\n",
938 algo
->name
, ivlen
, blocklen
));
943 if (m
->m_pkthdr
.len
< bodyoff
) {
944 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
945 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
949 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
950 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
951 "payload length must be multiple of %lu\n",
952 algo
->name
, (u_int32_t
)algo
->padbound
));
959 soff
= doff
= sn
= dn
= 0;
963 while (soff
< bodyoff
) {
964 if (soff
+ s
->m_len
> bodyoff
) {
975 /* skip over empty mbuf */
976 while (s
&& s
->m_len
== 0)
979 // Allocate blocksized buffer for unaligned or non-contiguous access
980 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
983 while (soff
< m
->m_pkthdr
.len
) {
985 if (sn
+ blocklen
<= s
->m_len
) {
986 /* body is continuous */
987 sp
= mtod(s
, u_int8_t
*) + sn
;
989 /* body is non-continuous */
990 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
995 if (!d
|| dn
+ blocklen
> d
->m_len
) {
998 MGET(d
, M_DONTWAIT
, MT_DATA
);
999 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
1000 if (d
&& i
> MLEN
) {
1001 MCLGET(d
, M_DONTWAIT
);
1002 if ((d
->m_flags
& M_EXT
) == 0) {
1019 // try to make mbuf data aligned
1020 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
1021 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
1025 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
1034 for (i
= 0; i
< blocklen
; i
++)
1038 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
1039 if (IPSEC_IS_P2ALIGNED(sp
)) {
1040 sp_unaligned
= NULL
;
1044 memcpy(sp
, sp_unaligned
, blocklen
);
1046 // no need to check output pointer alignment
1047 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1049 // update unaligned pointers
1050 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1055 ivp
= mtod(d
, u_int8_t
*) + dn
;
1060 /* find the next source block */
1061 while (s
&& sn
>= s
->m_len
) {
1068 m_freem(scut
->m_next
);
1069 scut
->m_len
= scutoff
;
1073 bzero(iv
, sizeof(iv
));
1074 bzero(sbuf
, blocklen
);
1076 key_sa_stir_iv(sav
);
1083 /*------------------------------------------------------------*/
1085 /* does not free m0 on error */
1089 size_t skip
, /* offset to ESP header */
1090 size_t length
, /* payload length */
1091 struct secasvar
*sav
,
1096 struct ah_algorithm_state s
;
1097 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1098 const struct ah_algorithm
*algo
;
1103 if (m0
->m_pkthdr
.len
< skip
) {
1104 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1107 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1108 ipseclog((LOG_DEBUG
,
1109 "esp_auth: mbuf length < skip + length\n"));
1113 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
,length
,0,0,0);
1115 * length of esp part (excluding authentication data) must be 4n,
1116 * since nexthdr must be at offset 4n+3.
1119 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1120 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1,0,0,0,0);
1124 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1125 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2,0,0,0,0);
1128 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1131 "esp_auth: bad ESP auth algorithm passed: %d\n",
1133 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3,0,0,0,0);
1140 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1141 if (sizeof(sumbuf
) < siz
) {
1142 ipseclog((LOG_DEBUG
,
1143 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1145 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4,0,0,0,0);
1149 /* skip the header */
1152 panic("mbuf chain?");
1153 if (m
->m_len
<= skip
) {
1163 error
= (*algo
->init
)(&s
, sav
);
1165 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5,0,0,0,0);
1168 while (0 < length
) {
1170 panic("mbuf chain?");
1172 if (m
->m_len
- off
< length
) {
1173 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1175 length
-= m
->m_len
- off
;
1179 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1183 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1184 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1185 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6,0,0,0,0);