]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_core.c
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 <net/pfkeyv2.h>
102 #include <netkey/keydb.h>
103 #include <netkey/key.h>
104 #include <libkern/crypto/des.h>
106 #include <net/net_osdep.h>
108 #include <sys/kdebug.h>
109 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIPSEC, 1)
110 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIPSEC, 3)
111 #define DBG_FNC_ESPAUTH NETDBG_CODE(DBG_NETIPSEC, (8 << 8))
112 #define MAX_SBUF_LEN 2000
114 extern lck_mtx_t
*sadb_mutex
;
116 static int esp_null_mature(struct secasvar
*);
117 static int esp_null_decrypt(struct mbuf
*, size_t,
118 struct secasvar
*, const struct esp_algorithm
*, int);
119 static int esp_null_encrypt(struct mbuf
*, size_t, size_t,
120 struct secasvar
*, const struct esp_algorithm
*, int);
121 static int esp_descbc_mature(struct secasvar
*);
122 static int esp_descbc_ivlen(const struct esp_algorithm
*,
124 static int esp_des_schedule(const struct esp_algorithm
*,
126 static int esp_des_schedlen(const struct esp_algorithm
*);
127 static int esp_des_blockdecrypt(const struct esp_algorithm
*,
128 struct secasvar
*, u_int8_t
*, u_int8_t
*);
129 static int esp_des_blockencrypt(const struct esp_algorithm
*,
130 struct secasvar
*, u_int8_t
*, u_int8_t
*);
131 static int esp_cbc_mature(struct secasvar
*);
132 static int esp_3des_schedule(const struct esp_algorithm
*,
134 static int esp_3des_schedlen(const struct esp_algorithm
*);
135 static int esp_3des_blockdecrypt(const struct esp_algorithm
*,
136 struct secasvar
*, u_int8_t
*, u_int8_t
*);
137 static int esp_3des_blockencrypt(const struct esp_algorithm
*,
138 struct secasvar
*, u_int8_t
*, u_int8_t
*);
139 static int esp_common_ivlen(const struct esp_algorithm
*,
141 static int esp_cbc_decrypt(struct mbuf
*, size_t,
142 struct secasvar
*, const struct esp_algorithm
*, int);
143 static int esp_cbc_encrypt(struct mbuf
*, size_t, size_t,
144 struct secasvar
*, const struct esp_algorithm
*, int);
145 static int esp_gcm_mature(struct secasvar
*);
149 #define ESP_AESGCM_KEYLEN128 160 // 16-bytes key + 4 bytes salt
150 #define ESP_AESGCM_KEYLEN192 224 // 24-bytes key + 4 bytes salt
151 #define ESP_AESGCM_KEYLEN256 288 // 32-bytes key + 4 bytes salt
153 static const struct esp_algorithm des_cbc
=
154 { 8, -1, esp_descbc_mature
, 64, 64, esp_des_schedlen
,
156 esp_descbc_ivlen
, esp_cbc_decrypt
,
157 esp_cbc_encrypt
, esp_des_schedule
,
158 esp_des_blockdecrypt
, esp_des_blockencrypt
,
160 static const struct esp_algorithm des3_cbc
=
161 { 8, 8, esp_cbc_mature
, 192, 192, esp_3des_schedlen
,
163 esp_common_ivlen
, esp_cbc_decrypt
,
164 esp_cbc_encrypt
, esp_3des_schedule
,
165 esp_3des_blockdecrypt
, esp_3des_blockencrypt
,
167 static const struct esp_algorithm null_esp
=
168 { 1, 0, esp_null_mature
, 0, 2048, 0, "null",
169 esp_common_ivlen
, esp_null_decrypt
,
170 esp_null_encrypt
, NULL
, NULL
, NULL
,
172 static const struct esp_algorithm aes_cbc
=
173 { 16, 16, esp_cbc_mature
, 128, 256, esp_aes_schedlen
,
175 esp_common_ivlen
, esp_cbc_decrypt_aes
,
176 esp_cbc_encrypt_aes
, esp_aes_schedule
,
179 static const struct esp_algorithm aes_gcm
=
180 { 4, 8, esp_gcm_mature
, ESP_AESGCM_KEYLEN128
, ESP_AESGCM_KEYLEN256
, esp_gcm_schedlen
,
182 esp_common_ivlen
, esp_gcm_decrypt_aes
,
183 esp_gcm_encrypt_aes
, esp_gcm_schedule
,
185 16, esp_gcm_decrypt_finalize
, esp_gcm_encrypt_finalize
};
187 static const struct esp_algorithm
*esp_algorithms
[] = {
195 const struct esp_algorithm
*
196 esp_algorithm_lookup(int idx
)
199 case SADB_EALG_DESCBC
:
201 case SADB_EALG_3DESCBC
:
205 case SADB_X_EALG_RIJNDAELCBC
:
207 case SADB_X_EALG_AES_GCM
:
221 for (idx
= 0; idx
< sizeof(esp_algorithms
)/sizeof(esp_algorithms
[0]);
223 if (esp_algorithms
[idx
]->ivlenval
> ivlen
)
224 ivlen
= esp_algorithms
[idx
]->ivlenval
;
231 esp_schedule(const struct esp_algorithm
*algo
, struct secasvar
*sav
)
235 /* check for key length */
236 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
237 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
239 "esp_schedule %s: unsupported key length %d: "
240 "needs %d to %d bits\n", algo
->name
, _KEYBITS(sav
->key_enc
),
241 algo
->keymin
, algo
->keymax
));
245 lck_mtx_lock(sadb_mutex
);
246 /* already allocated */
247 if (sav
->sched
&& sav
->schedlen
!= 0) {
248 lck_mtx_unlock(sadb_mutex
);
251 /* no schedule necessary */
252 if (!algo
->schedule
|| !algo
->schedlen
) {
253 lck_mtx_unlock(sadb_mutex
);
257 sav
->schedlen
= (*algo
->schedlen
)(algo
);
258 if ((signed) sav
->schedlen
< 0) {
259 lck_mtx_unlock(sadb_mutex
);
263 //#### that malloc should be replaced by a saved buffer...
264 sav
->sched
= _MALLOC(sav
->schedlen
, M_SECA
, M_DONTWAIT
);
267 lck_mtx_unlock(sadb_mutex
);
271 error
= (*algo
->schedule
)(algo
, sav
);
273 ipseclog((LOG_ERR
, "esp_schedule %s: error %d\n",
275 bzero(sav
->sched
, sav
->schedlen
);
276 FREE(sav
->sched
, M_SECA
);
280 lck_mtx_unlock(sadb_mutex
);
286 __unused
struct secasvar
*sav
)
289 /* anything is okay */
295 __unused
struct mbuf
*m
,
296 __unused
size_t off
, /* offset to ESP header */
297 __unused
struct secasvar
*sav
,
298 __unused
const struct esp_algorithm
*algo
,
302 return 0; /* do nothing */
307 __unused
struct mbuf
*m
,
308 __unused
size_t off
, /* offset to ESP header */
309 __unused
size_t plen
, /* payload length (to be encrypted) */
310 __unused
struct secasvar
*sav
,
311 __unused
const struct esp_algorithm
*algo
,
315 return 0; /* do nothing */
319 esp_descbc_mature(struct secasvar
*sav
)
321 const struct esp_algorithm
*algo
;
323 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
)) {
324 ipseclog((LOG_ERR
, "esp_cbc_mature: "
325 "algorithm incompatible with 4 octets IV length\n"));
330 ipseclog((LOG_ERR
, "esp_descbc_mature: no key is given.\n"));
334 algo
= esp_algorithm_lookup(sav
->alg_enc
);
337 "esp_descbc_mature: unsupported algorithm.\n"));
341 if (_KEYBITS(sav
->key_enc
) < algo
->keymin
||
342 _KEYBITS(sav
->key_enc
) > algo
->keymax
) {
344 "esp_descbc_mature: invalid key length %d.\n",
345 _KEYBITS(sav
->key_enc
)));
350 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
))) {
352 "esp_descbc_mature: weak key was passed.\n"));
361 __unused
const struct esp_algorithm
*algo
,
362 struct secasvar
*sav
)
367 if ((sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_IV4B
))
369 if (!(sav
->flags
& SADB_X_EXT_OLD
) && (sav
->flags
& SADB_X_EXT_DERIV
))
376 __unused
const struct esp_algorithm
*algo
)
378 return sizeof(des_ecb_key_schedule
);
383 __unused
const struct esp_algorithm
*algo
,
384 struct secasvar
*sav
)
387 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
388 if (des_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
389 (des_ecb_key_schedule
*)sav
->sched
))
396 esp_des_blockdecrypt(
397 __unused
const struct esp_algorithm
*algo
,
398 struct secasvar
*sav
,
402 /* assumption: d has a good alignment */
403 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
404 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
405 (des_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
410 esp_des_blockencrypt(
411 __unused
const struct esp_algorithm
*algo
,
412 struct secasvar
*sav
,
416 /* assumption: d has a good alignment */
417 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
418 des_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
419 (des_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
424 esp_cbc_mature(struct secasvar
*sav
)
427 const struct esp_algorithm
*algo
;
429 if (sav
->flags
& SADB_X_EXT_OLD
) {
431 "esp_cbc_mature: algorithm incompatible with esp-old\n"));
434 if (sav
->flags
& SADB_X_EXT_DERIV
) {
436 "esp_cbc_mature: algorithm incompatible with derived\n"));
441 ipseclog((LOG_ERR
, "esp_cbc_mature: no key is given.\n"));
445 algo
= esp_algorithm_lookup(sav
->alg_enc
);
448 "esp_cbc_mature: unsupported algorithm.\n"));
452 keylen
= sav
->key_enc
->sadb_key_bits
;
453 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
455 "esp_cbc_mature %s: invalid key length %d.\n",
456 algo
->name
, sav
->key_enc
->sadb_key_bits
));
459 switch (sav
->alg_enc
) {
460 case SADB_EALG_3DESCBC
:
462 if (des_is_weak_key((des_cblock
*)_KEYBUF(sav
->key_enc
)) ||
463 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 8)) ||
464 des_is_weak_key((des_cblock
*)(_KEYBUF(sav
->key_enc
) + 16))) {
466 "esp_cbc_mature %s: weak key was passed.\n",
471 case SADB_X_EALG_RIJNDAELCBC
:
472 /* allows specific key sizes only */
473 if (!(keylen
== 128 || keylen
== 192 || keylen
== 256)) {
475 "esp_cbc_mature %s: invalid key length %d.\n",
476 algo
->name
, keylen
));
486 esp_gcm_mature(struct secasvar
*sav
)
489 const struct esp_algorithm
*algo
;
491 if (sav
->flags
& SADB_X_EXT_OLD
) {
493 "esp_gcm_mature: algorithm incompatible with esp-old\n"));
496 if (sav
->flags
& SADB_X_EXT_DERIV
) {
498 "esp_gcm_mature: algorithm incompatible with derived\n"));
503 ipseclog((LOG_ERR
, "esp_gcm_mature: no key is given.\n"));
507 algo
= esp_algorithm_lookup(sav
->alg_enc
);
510 "esp_gcm_mature: unsupported algorithm.\n"));
514 keylen
= sav
->key_enc
->sadb_key_bits
;
515 if (keylen
< algo
->keymin
|| algo
->keymax
< keylen
) {
517 "esp_gcm_mature %s: invalid key length %d.\n",
518 algo
->name
, sav
->key_enc
->sadb_key_bits
));
521 switch (sav
->alg_enc
) {
522 case SADB_X_EALG_AES_GCM
:
523 /* allows specific key sizes only */
524 if (!(keylen
== ESP_AESGCM_KEYLEN128
|| keylen
== ESP_AESGCM_KEYLEN192
|| keylen
== ESP_AESGCM_KEYLEN256
)) {
526 "esp_gcm_mature %s: invalid key length %d.\n",
527 algo
->name
, keylen
));
533 "esp_gcm_mature %s: invalid algo %d.\n", sav
->alg_enc
));
542 __unused
const struct esp_algorithm
*algo
)
545 return sizeof(des3_ecb_key_schedule
);
550 __unused
const struct esp_algorithm
*algo
,
551 struct secasvar
*sav
)
553 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
555 if (des3_ecb_key_sched((des_cblock
*)_KEYBUF(sav
->key_enc
),
556 (des3_ecb_key_schedule
*)sav
->sched
))
563 esp_3des_blockdecrypt(
564 __unused
const struct esp_algorithm
*algo
,
565 struct secasvar
*sav
,
569 /* assumption: d has a good alignment */
570 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
571 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
572 (des3_ecb_key_schedule
*)sav
->sched
, DES_DECRYPT
);
577 esp_3des_blockencrypt(
578 __unused
const struct esp_algorithm
*algo
,
579 struct secasvar
*sav
,
583 /* assumption: d has a good alignment */
584 bcopy(s
, d
, sizeof(DES_LONG
) * 2);
585 des3_ecb_encrypt((des_cblock
*)d
, (des_cblock
*)d
,
586 (des3_ecb_key_schedule
*)sav
->sched
, DES_ENCRYPT
);
592 const struct esp_algorithm
*algo
,
593 __unused
struct secasvar
*sav
)
597 panic("esp_common_ivlen: unknown algorithm");
598 return algo
->ivlenval
;
602 esp_cbc_decrypt(struct mbuf
*m
, size_t off
, struct secasvar
*sav
,
603 const struct esp_algorithm
*algo
, int ivlen
)
606 struct mbuf
*d
, *d0
, *dp
;
607 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
608 int sn
, dn
; /* offset from the head of the mbuf, to meat */
609 size_t ivoff
, bodyoff
;
610 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
611 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
619 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
620 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
621 "unsupported ivlen %d\n", algo
->name
, ivlen
));
626 /* assumes blocklen == padbound */
627 blocklen
= algo
->padbound
;
630 if (blocklen
> sizeof(iv
)) {
631 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
632 "unsupported blocklen %d\n", algo
->name
, blocklen
));
638 if (sav
->flags
& SADB_X_EXT_OLD
) {
640 ivoff
= off
+ sizeof(struct esp
);
641 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
645 if (sav
->flags
& SADB_X_EXT_DERIV
) {
647 * draft-ietf-ipsec-ciph-des-derived-00.txt
648 * uses sequence number field as IV field.
650 ivoff
= off
+ sizeof(struct esp
);
651 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
652 ivlen
= sizeof(u_int32_t
);
655 ivoff
= off
+ sizeof(struct newesp
);
656 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
662 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
665 if (ivlen
== blocklen
)
667 else if (ivlen
== 4 && blocklen
== 8) {
668 bcopy(&iv
[0], &iv
[4], 4);
674 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
675 "unsupported ivlen/blocklen: %d %d\n",
676 algo
->name
, ivlen
, blocklen
));
681 if (m
->m_pkthdr
.len
< bodyoff
) {
682 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
683 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
687 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
688 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
689 "payload length must be multiple of %d\n",
690 algo
->name
, blocklen
));
697 soff
= doff
= sn
= dn
= 0;
701 while (soff
< bodyoff
) {
702 if (soff
+ s
->m_len
> bodyoff
) {
713 /* skip over empty mbuf */
714 while (s
&& s
->m_len
== 0)
717 // Allocate blocksized buffer for unaligned or non-contiguous access
718 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
721 while (soff
< m
->m_pkthdr
.len
) {
723 if (sn
+ blocklen
<= s
->m_len
) {
724 /* body is continuous */
725 sp
= mtod(s
, u_int8_t
*) + sn
;
727 /* body is non-continuous */
728 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
733 if (!d
|| dn
+ blocklen
> d
->m_len
) {
736 MGET(d
, M_DONTWAIT
, MT_DATA
);
737 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
739 MCLGET(d
, M_DONTWAIT
);
740 if ((d
->m_flags
& M_EXT
) == 0) {
757 // try to make mbuf data aligned
758 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
759 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
763 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
770 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
771 if (IPSEC_IS_P2ALIGNED(sp
)) {
776 memcpy(sp
, sp_unaligned
, blocklen
);
778 // no need to check output pointer alignment
779 (*algo
->blockdecrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
781 // update unaligned pointers
782 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
788 q
= mtod(d
, u_int8_t
*) + dn
;
789 for (i
= 0; i
< blocklen
; i
++)
794 bcopy(sbuf
, iv
, blocklen
);
802 /* find the next source block */
803 while (s
&& sn
>= s
->m_len
) {
810 m_freem(scut
->m_next
);
811 scut
->m_len
= scutoff
;
815 bzero(iv
, sizeof(iv
));
816 bzero(sbuf
, blocklen
);
827 __unused
size_t plen
,
828 struct secasvar
*sav
,
829 const struct esp_algorithm
*algo
,
833 struct mbuf
*d
, *d0
, *dp
;
834 int soff
, doff
; /* offset from the head of chain, to head of this mbuf */
835 int sn
, dn
; /* offset from the head of the mbuf, to meat */
836 size_t ivoff
, bodyoff
;
837 u_int8_t iv
[MAXIVLEN
] __attribute__((aligned(4))), *ivp
;
838 u_int8_t
*sbuf
= NULL
, *sp
, *sp_unaligned
;
846 if (ivlen
!= sav
->ivlen
|| ivlen
> sizeof(iv
)) {
847 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
848 "unsupported ivlen %d\n", algo
->name
, ivlen
));
853 /* assumes blocklen == padbound */
854 blocklen
= algo
->padbound
;
857 if (blocklen
> sizeof(iv
)) {
858 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
859 "unsupported blocklen %d\n", algo
->name
, blocklen
));
865 if (sav
->flags
& SADB_X_EXT_OLD
) {
867 ivoff
= off
+ sizeof(struct esp
);
868 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
872 if (sav
->flags
& SADB_X_EXT_DERIV
) {
874 * draft-ietf-ipsec-ciph-des-derived-00.txt
875 * uses sequence number field as IV field.
877 ivoff
= off
+ sizeof(struct esp
);
878 bodyoff
= off
+ sizeof(struct esp
) + sizeof(u_int32_t
);
879 ivlen
= sizeof(u_int32_t
);
882 ivoff
= off
+ sizeof(struct newesp
);
883 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
888 /* put iv into the packet. if we are in derived mode, use seqno. */
890 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
892 bcopy(sav
->iv
, iv
, ivlen
);
893 /* maybe it is better to overwrite dest, not source */
894 m_copyback(m
, ivoff
, ivlen
, (caddr_t
) iv
);
898 if (ivlen
== blocklen
)
900 else if (ivlen
== 4 && blocklen
== 8) {
901 bcopy(&iv
[0], &iv
[4], 4);
907 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
908 "unsupported ivlen/blocklen: %d %d\n",
909 algo
->name
, ivlen
, blocklen
));
914 if (m
->m_pkthdr
.len
< bodyoff
) {
915 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
916 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
920 if ((m
->m_pkthdr
.len
- bodyoff
) % blocklen
) {
921 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
922 "payload length must be multiple of %lu\n",
923 algo
->name
, (u_int32_t
)algo
->padbound
));
930 soff
= doff
= sn
= dn
= 0;
934 while (soff
< bodyoff
) {
935 if (soff
+ s
->m_len
> bodyoff
) {
946 /* skip over empty mbuf */
947 while (s
&& s
->m_len
== 0)
950 // Allocate blocksized buffer for unaligned or non-contiguous access
951 sbuf
= (u_int8_t
*)_MALLOC(blocklen
, M_SECA
, M_DONTWAIT
);
954 while (soff
< m
->m_pkthdr
.len
) {
956 if (sn
+ blocklen
<= s
->m_len
) {
957 /* body is continuous */
958 sp
= mtod(s
, u_int8_t
*) + sn
;
960 /* body is non-continuous */
961 m_copydata(s
, sn
, blocklen
, (caddr_t
) sbuf
);
966 if (!d
|| dn
+ blocklen
> d
->m_len
) {
969 MGET(d
, M_DONTWAIT
, MT_DATA
);
970 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
972 MCLGET(d
, M_DONTWAIT
);
973 if ((d
->m_flags
& M_EXT
) == 0) {
990 // try to make mbuf data aligned
991 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
992 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
996 d
->m_len
= (M_TRAILINGSPACE(d
) / blocklen
) * blocklen
;
1005 for (i
= 0; i
< blocklen
; i
++)
1009 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
1010 if (IPSEC_IS_P2ALIGNED(sp
)) {
1011 sp_unaligned
= NULL
;
1015 memcpy(sp
, sp_unaligned
, blocklen
);
1017 // no need to check output pointer alignment
1018 (*algo
->blockencrypt
)(algo
, sav
, sp
, mtod(d
, u_int8_t
*) + dn
);
1020 // update unaligned pointers
1021 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1026 ivp
= mtod(d
, u_int8_t
*) + dn
;
1031 /* find the next source block */
1032 while (s
&& sn
>= s
->m_len
) {
1039 m_freem(scut
->m_next
);
1040 scut
->m_len
= scutoff
;
1044 bzero(iv
, sizeof(iv
));
1045 bzero(sbuf
, blocklen
);
1047 key_sa_stir_iv(sav
);
1054 /*------------------------------------------------------------*/
1056 /* does not free m0 on error */
1060 size_t skip
, /* offset to ESP header */
1061 size_t length
, /* payload length */
1062 struct secasvar
*sav
,
1067 struct ah_algorithm_state s
;
1068 u_char sumbuf
[AH_MAXSUMSIZE
] __attribute__((aligned(4)));
1069 const struct ah_algorithm
*algo
;
1074 if (m0
->m_pkthdr
.len
< skip
) {
1075 ipseclog((LOG_DEBUG
, "esp_auth: mbuf length < skip\n"));
1078 if (m0
->m_pkthdr
.len
< skip
+ length
) {
1079 ipseclog((LOG_DEBUG
,
1080 "esp_auth: mbuf length < skip + length\n"));
1084 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_START
, skip
,length
,0,0,0);
1086 * length of esp part (excluding authentication data) must be 4n,
1087 * since nexthdr must be at offset 4n+3.
1090 ipseclog((LOG_ERR
, "esp_auth: length is not multiple of 4\n"));
1091 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 1,0,0,0,0);
1095 ipseclog((LOG_DEBUG
, "esp_auth: NULL SA passed\n"));
1096 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 2,0,0,0,0);
1099 algo
= ah_algorithm_lookup(sav
->alg_auth
);
1102 "esp_auth: bad ESP auth algorithm passed: %d\n",
1104 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 3,0,0,0,0);
1111 siz
= (((*algo
->sumsiz
)(sav
) + 3) & ~(4 - 1));
1112 if (sizeof(sumbuf
) < siz
) {
1113 ipseclog((LOG_DEBUG
,
1114 "esp_auth: AH_MAXSUMSIZE is too small: siz=%lu\n",
1116 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 4,0,0,0,0);
1120 /* skip the header */
1123 panic("mbuf chain?");
1124 if (m
->m_len
<= skip
) {
1134 error
= (*algo
->init
)(&s
, sav
);
1136 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 5,0,0,0,0);
1139 while (0 < length
) {
1141 panic("mbuf chain?");
1143 if (m
->m_len
- off
< length
) {
1144 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
),
1146 length
-= m
->m_len
- off
;
1150 (*algo
->update
)(&s
, (caddr_t
)(mtod(m
, u_char
*) + off
), length
);
1154 (*algo
->result
)(&s
, (caddr_t
) sumbuf
, sizeof(sumbuf
));
1155 bcopy(sumbuf
, sum
, siz
); /*XXX*/
1156 KERNEL_DEBUG(DBG_FNC_ESPAUTH
| DBG_FUNC_END
, 6,0,0,0,0);