]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_rijndael.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_rijndael.c,v 1.1.2.1 2001/07/03 11:01:50 ume Exp $ */
30 /* $KAME: esp_rijndael.c,v 1.4 2001/03/02 05:53:05 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
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/socket.h>
64 #include <sys/queue.h>
65 #include <sys/syslog.h>
67 #include <sys/mcache.h>
69 #include <kern/locks.h>
72 #include <net/route.h>
74 #include <netinet6/ipsec.h>
75 #include <netinet6/esp.h>
76 #include <netinet6/esp_rijndael.h>
78 #include <libkern/crypto/aes.h>
80 #include <netkey/key.h>
82 #include <net/net_osdep.h>
84 #define MAX_REALIGN_LEN 2000
85 #define AES_BLOCKLEN 16
86 #define ESP_GCM_SALT_LEN 4 // RFC 4106 Section 4
87 #define ESP_GCM_IVLEN 8
88 #define ESP_GCM_ALIGN 16
90 extern lck_mtx_t
*sadb_mutex
;
100 __unused
const struct esp_algorithm
*algo
)
103 return sizeof(aes_ctx
);
108 __unused
const struct esp_algorithm
*algo
,
109 struct secasvar
*sav
)
112 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
113 aes_ctx
*ctx
= (aes_ctx
*)sav
->sched
;
115 aes_decrypt_key((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
), &ctx
->decrypt
);
116 aes_encrypt_key((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
), &ctx
->encrypt
);
122 /* The following 2 functions decrypt or encrypt the contents of
123 * the mbuf chain passed in keeping the IP and ESP header's in place,
125 * The code attempts to call the crypto code with the largest chunk
126 * of data it can based on the amount of source data in
127 * the current source mbuf and the space remaining in the current
128 * destination mbuf. The crypto code requires data to be a multiples
129 * of 16 bytes. A separate buffer is used when a 16 byte block spans
133 * off = offset to ESP header
135 * local vars for source:
136 * soff = offset from beginning of the chain to the head of the
138 * scut = last mbuf that contains headers to be retained
139 * scutoff = offset to end of the headers in scut
140 * s = the current mbuf
141 * sn = current offset to data in s (next source data to process)
143 * local vars for dest:
146 * dn = current offset in d (next location to store result)
154 struct secasvar
*sav
,
155 const struct esp_algorithm
*algo
,
159 struct mbuf
*d
, *d0
, *dp
;
160 int soff
; /* offset from the head of chain, to head of this mbuf */
161 int sn
, dn
; /* offset from the head of the mbuf, to meat */
162 size_t ivoff
, bodyoff
;
163 u_int8_t iv
[AES_BLOCKLEN
] __attribute__((aligned(4))), *dptr
;
164 u_int8_t sbuf
[AES_BLOCKLEN
] __attribute__((aligned(4))), *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
170 if (ivlen
!= AES_BLOCKLEN
) {
171 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
172 "unsupported ivlen %d\n", algo
->name
, ivlen
));
177 if (sav
->flags
& SADB_X_EXT_OLD
) {
179 ivoff
= off
+ sizeof(struct esp
);
180 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
182 ivoff
= off
+ sizeof(struct newesp
);
183 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
186 if (m
->m_pkthdr
.len
< bodyoff
) {
187 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
188 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
192 if ((m
->m_pkthdr
.len
- bodyoff
) % AES_BLOCKLEN
) {
193 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
194 "payload length must be multiple of %d\n",
195 algo
->name
, AES_BLOCKLEN
));
201 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
208 /* skip header/IV offset */
209 while (soff
< bodyoff
) {
210 if (soff
+ s
->m_len
> bodyoff
) {
221 /* skip over empty mbuf */
222 while (s
&& s
->m_len
== 0)
225 while (soff
< m
->m_pkthdr
.len
) {
227 if (sn
+ AES_BLOCKLEN
<= s
->m_len
) {
228 /* body is continuous */
229 sp
= mtod(s
, u_int8_t
*) + sn
;
231 len
-= len
% AES_BLOCKLEN
; // full blocks only
233 /* body is non-continuous */
234 m_copydata(s
, sn
, AES_BLOCKLEN
, (caddr_t
) sbuf
);
236 len
= AES_BLOCKLEN
; // 1 block only in sbuf
240 if (!d
|| dn
+ AES_BLOCKLEN
> d
->m_len
) {
243 MGET(d
, M_DONTWAIT
, MT_DATA
);
244 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
246 MCLGET(d
, M_DONTWAIT
);
247 if ((d
->m_flags
& M_EXT
) == 0) {
248 d
= m_mbigget(d
, M_DONTWAIT
);
249 if ((d
->m_flags
& M_EXT
) == 0) {
266 // try to make mbuf data aligned
267 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
268 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
271 d
->m_len
= M_TRAILINGSPACE(d
);
272 d
->m_len
-= d
->m_len
% AES_BLOCKLEN
;
275 dptr
= mtod(d
, u_int8_t
*);
279 /* adjust len if greater than space available in dest */
280 if (len
> d
->m_len
- dn
)
284 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
285 if (IPSEC_IS_P2ALIGNED(sp
)) {
289 if (len
> MAX_REALIGN_LEN
) {
294 if (sp_aligned
!= NULL
) {
295 FREE(sp_aligned
, M_SECA
);
300 if (sp_aligned
== NULL
) {
301 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
302 if (sp_aligned
== NULL
) {
311 memcpy(sp
, sp_unaligned
, len
);
313 // no need to check output pointer alignment
314 aes_decrypt_cbc(sp
, iv
, len
>> 4, dptr
+ dn
,
315 (aes_decrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->decrypt
)));
317 // update unaligned pointers
318 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
327 bcopy(sp
+ len
- AES_BLOCKLEN
, iv
, AES_BLOCKLEN
);
329 /* find the next source block */
330 while (s
&& sn
>= s
->m_len
) {
338 /* free un-needed source mbufs and add dest mbufs to chain */
339 m_freem(scut
->m_next
);
340 scut
->m_len
= scutoff
;
344 if (sp_aligned
!= NULL
) {
345 FREE(sp_aligned
, M_SECA
);
350 bzero(iv
, sizeof(iv
));
351 bzero(sbuf
, sizeof(sbuf
));
360 __unused
size_t plen
,
361 struct secasvar
*sav
,
362 const struct esp_algorithm
*algo
,
366 struct mbuf
*d
, *d0
, *dp
;
367 int soff
; /* offset from the head of chain, to head of this mbuf */
368 int sn
, dn
; /* offset from the head of the mbuf, to meat */
369 size_t ivoff
, bodyoff
;
370 u_int8_t
*ivp
, *dptr
, *ivp_unaligned
;
371 u_int8_t sbuf
[AES_BLOCKLEN
] __attribute__((aligned(4))), *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
372 u_int8_t ivp_aligned_buf
[AES_BLOCKLEN
] __attribute__((aligned(4)));
377 if (ivlen
!= AES_BLOCKLEN
) {
378 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
379 "unsupported ivlen %d\n", algo
->name
, ivlen
));
384 if (sav
->flags
& SADB_X_EXT_OLD
) {
386 ivoff
= off
+ sizeof(struct esp
);
387 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
389 ivoff
= off
+ sizeof(struct newesp
);
390 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
393 /* put iv into the packet */
394 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
395 ivp
= (u_int8_t
*) sav
->iv
;
397 if (m
->m_pkthdr
.len
< bodyoff
) {
398 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
399 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
403 if ((m
->m_pkthdr
.len
- bodyoff
) % AES_BLOCKLEN
) {
404 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
405 "payload length must be multiple of %lu\n",
406 algo
->name
, AES_BLOCKLEN
));
416 /* skip headers/IV */
417 while (soff
< bodyoff
) {
418 if (soff
+ s
->m_len
> bodyoff
) {
429 /* skip over empty mbuf */
430 while (s
&& s
->m_len
== 0)
433 while (soff
< m
->m_pkthdr
.len
) {
435 if (sn
+ AES_BLOCKLEN
<= s
->m_len
) {
436 /* body is continuous */
437 sp
= mtod(s
, u_int8_t
*) + sn
;
439 len
-= len
% AES_BLOCKLEN
; // full blocks only
441 /* body is non-continuous */
442 m_copydata(s
, sn
, AES_BLOCKLEN
, (caddr_t
) sbuf
);
444 len
= AES_BLOCKLEN
; // 1 block only in sbuf
448 if (!d
|| dn
+ AES_BLOCKLEN
> d
->m_len
) {
451 MGET(d
, M_DONTWAIT
, MT_DATA
);
452 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
454 MCLGET(d
, M_DONTWAIT
);
455 if ((d
->m_flags
& M_EXT
) == 0) {
456 d
= m_mbigget(d
, M_DONTWAIT
);
457 if ((d
->m_flags
& M_EXT
) == 0) {
474 // try to make mbuf data aligned
475 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
476 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
479 d
->m_len
= M_TRAILINGSPACE(d
);
480 d
->m_len
-= d
->m_len
% AES_BLOCKLEN
;
483 dptr
= mtod(d
, u_int8_t
*);
487 /* adjust len if greater than space available */
488 if (len
> d
->m_len
- dn
)
492 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
493 if (IPSEC_IS_P2ALIGNED(sp
)) {
497 if (len
> MAX_REALIGN_LEN
) {
502 if (sp_aligned
!= NULL
) {
503 FREE(sp_aligned
, M_SECA
);
508 if (sp_aligned
== NULL
) {
509 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
510 if (sp_aligned
== NULL
) {
519 memcpy(sp
, sp_unaligned
, len
);
521 // check ivp pointer alignment and use a separate aligned buffer (if ivp is not aligned on 4-byte boundary).
522 if (IPSEC_IS_P2ALIGNED(ivp
)) {
523 ivp_unaligned
= NULL
;
526 ivp
= ivp_aligned_buf
;
527 memcpy(ivp
, ivp_unaligned
, AES_BLOCKLEN
);
529 // no need to check output pointer alignment
530 aes_encrypt_cbc(sp
, ivp
, len
>> 4, dptr
+ dn
,
531 (aes_encrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->encrypt
)));
533 // update unaligned pointers
534 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
537 if (!IPSEC_IS_P2ALIGNED(ivp_unaligned
)) {
546 ivp
= dptr
+ dn
- AES_BLOCKLEN
; // last block encrypted
548 /* find the next source block and skip empty mbufs */
549 while (s
&& sn
>= s
->m_len
) {
556 /* free un-needed source mbufs and add dest mbufs to chain */
557 m_freem(scut
->m_next
);
558 scut
->m_len
= scutoff
;
562 if (sp_aligned
!= NULL
) {
563 FREE(sp_aligned
, M_SECA
);
568 bzero(sbuf
, sizeof(sbuf
));
576 __unused
const struct esp_algorithm
*algo
)
578 return (sizeof(aes_gcm_ctx
) + aes_decrypt_get_ctx_size_gcm() + aes_encrypt_get_ctx_size_gcm() + ESP_GCM_ALIGN
);
582 esp_gcm_schedule( __unused
const struct esp_algorithm
*algo
,
583 struct secasvar
*sav
)
585 LCK_MTX_ASSERT(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
586 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
587 u_int ivlen
= sav
->ivlen
;
588 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
591 ctx
->decrypt
= &ctx
->ctxt
[0];
592 ctx
->encrypt
= &ctx
->ctxt
[aes_decrypt_get_ctx_size_gcm() / sizeof(ccgcm_ctx
)];
594 rc
= aes_decrypt_key_gcm((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ctx
->decrypt
);
599 bzero(nonce
, ESP_GCM_SALT_LEN
+ ivlen
);
600 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
601 memcpy(nonce
+ESP_GCM_SALT_LEN
, sav
->iv
, ivlen
);
603 rc
= aes_encrypt_key_with_iv_gcm((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, nonce
, ctx
->encrypt
);
608 rc
= aes_encrypt_reset_gcm(ctx
->encrypt
);
617 esp_gcm_encrypt_finalize(struct secasvar
*sav
,
618 unsigned char *tag
, unsigned int tag_bytes
)
620 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
621 return (aes_encrypt_finalize_gcm(tag
, tag_bytes
, ctx
->encrypt
));
625 esp_gcm_decrypt_finalize(struct secasvar
*sav
,
626 unsigned char *tag
, unsigned int tag_bytes
)
628 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
629 return (aes_decrypt_finalize_gcm(tag
, tag_bytes
, ctx
->decrypt
));
636 __unused
size_t plen
,
637 struct secasvar
*sav
,
638 const struct esp_algorithm
*algo __unused
,
642 struct mbuf
*d
, *d0
, *dp
;
643 int soff
; /* offset from the head of chain, to head of this mbuf */
644 int sn
, dn
; /* offset from the head of the mbuf, to meat */
645 size_t ivoff
, bodyoff
;
646 u_int8_t
*dptr
, *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
651 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
653 if (ivlen
!= ESP_GCM_IVLEN
) {
654 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
659 if (sav
->flags
& SADB_X_EXT_OLD
) {
661 ivoff
= off
+ sizeof(struct esp
);
662 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
664 ivoff
= off
+ sizeof(struct newesp
);
665 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
668 bzero(nonce
, ESP_GCM_SALT_LEN
+ivlen
);
669 /* generate new iv */
670 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
672 if (aes_encrypt_reset_gcm(ctx
->encrypt
)) {
673 ipseclog((LOG_ERR
, "%s: gcm reset failure\n", __FUNCTION__
));
678 if (aes_encrypt_inc_iv_gcm((unsigned char *)nonce
, ctx
->encrypt
)) {
679 ipseclog((LOG_ERR
, "%s: iv generation failure\n", __FUNCTION__
));
685 * The IV is now generated within corecrypto and
686 * is provided to ESP using aes_encrypt_inc_iv_gcm().
687 * This makes the sav->iv redundant and is no longer
688 * used in GCM operations. But we still copy the IV
689 * back to sav->iv to ensure that any future code reading
690 * this value will get the latest IV.
692 memcpy(sav
->iv
, (nonce
+ ESP_GCM_SALT_LEN
), ivlen
);
693 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
694 bzero(nonce
, ESP_GCM_SALT_LEN
+ivlen
);
696 if (m
->m_pkthdr
.len
< bodyoff
) {
697 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
698 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
703 /* Set Additional Authentication Data */
704 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
706 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
707 if (aes_encrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->encrypt
)) {
708 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
719 /* skip headers/IV */
720 while (soff
< bodyoff
) {
721 if (soff
+ s
->m_len
> bodyoff
) {
732 /* skip over empty mbuf */
733 while (s
&& s
->m_len
== 0)
736 while (soff
< m
->m_pkthdr
.len
) {
738 sp
= mtod(s
, u_int8_t
*) + sn
;
742 if (!d
|| (dn
+ len
> d
->m_len
)) {
745 MGET(d
, M_DONTWAIT
, MT_DATA
);
746 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
748 MCLGET(d
, M_DONTWAIT
);
749 if ((d
->m_flags
& M_EXT
) == 0) {
750 d
= m_mbigget(d
, M_DONTWAIT
);
751 if ((d
->m_flags
& M_EXT
) == 0) {
768 // try to make mbuf data aligned
769 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
770 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
773 d
->m_len
= M_TRAILINGSPACE(d
);
778 dptr
= mtod(d
, u_int8_t
*);
782 /* adjust len if greater than space available */
783 if (len
> d
->m_len
- dn
)
787 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
788 if (IPSEC_IS_P2ALIGNED(sp
)) {
792 if (len
> MAX_REALIGN_LEN
) {
797 if (sp_aligned
!= NULL
) {
798 FREE(sp_aligned
, M_SECA
);
803 if (sp_aligned
== NULL
) {
804 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
805 if (sp_aligned
== NULL
) {
814 memcpy(sp
, sp_unaligned
, len
);
817 if (aes_encrypt_gcm(sp
, len
, dptr
+dn
, ctx
->encrypt
)) {
818 ipseclog((LOG_ERR
, "%s: failed to encrypt\n", __FUNCTION__
));
823 // update unaligned pointers
824 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
832 /* find the next source block and skip empty mbufs */
833 while (s
&& sn
>= s
->m_len
) {
840 /* free un-needed source mbufs and add dest mbufs to chain */
841 m_freem(scut
->m_next
);
842 scut
->m_len
= scutoff
;
846 if (sp_aligned
!= NULL
) {
847 FREE(sp_aligned
, M_SECA
);
858 struct secasvar
*sav
,
859 const struct esp_algorithm
*algo __unused
,
863 struct mbuf
*d
, *d0
, *dp
;
864 int soff
; /* offset from the head of chain, to head of this mbuf */
865 int sn
, dn
; /* offset from the head of the mbuf, to meat */
866 size_t ivoff
, bodyoff
;
867 u_int8_t iv
[ESP_GCM_IVLEN
] __attribute__((aligned(4))), *dptr
;
868 u_int8_t
*sp
, *sp_unaligned
, *sp_aligned
= NULL
;
873 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
875 if (ivlen
!= ESP_GCM_IVLEN
) {
876 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
881 if (sav
->flags
& SADB_X_EXT_OLD
) {
883 ivoff
= off
+ sizeof(struct esp
);
884 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
886 ivoff
= off
+ sizeof(struct newesp
);
887 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
890 if (m
->m_pkthdr
.len
< bodyoff
) {
891 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
892 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
898 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
901 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
902 memcpy(nonce
+ESP_GCM_SALT_LEN
, iv
, ivlen
);
904 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
905 if (aes_decrypt_set_iv_gcm(nonce
, sizeof(nonce
), ctx
->decrypt
)) {
906 ipseclog((LOG_ERR
, "%s: failed to set IV\n", __FUNCTION__
));
908 bzero(nonce
, sizeof(nonce
));
911 bzero(nonce
, sizeof(nonce
));
913 /* Set Additional Authentication Data */
914 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
916 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
917 if (aes_decrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->decrypt
)) {
918 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
928 /* skip header/IV offset */
929 while (soff
< bodyoff
) {
930 if (soff
+ s
->m_len
> bodyoff
) {
941 /* skip over empty mbuf */
942 while (s
&& s
->m_len
== 0)
945 while (soff
< m
->m_pkthdr
.len
) {
947 sp
= mtod(s
, u_int8_t
*) + sn
;
951 if (!d
|| (dn
+ len
> d
->m_len
)) {
954 MGET(d
, M_DONTWAIT
, MT_DATA
);
955 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
957 MCLGET(d
, M_DONTWAIT
);
958 if ((d
->m_flags
& M_EXT
) == 0) {
959 d
= m_mbigget(d
, M_DONTWAIT
);
960 if ((d
->m_flags
& M_EXT
) == 0) {
977 // try to make mbuf data aligned
978 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
979 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
982 d
->m_len
= M_TRAILINGSPACE(d
);
987 dptr
= mtod(d
, u_int8_t
*);
991 /* adjust len if greater than space available in dest */
992 if (len
> d
->m_len
- dn
)
996 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
997 if (IPSEC_IS_P2ALIGNED(sp
)) {
1001 if (len
> MAX_REALIGN_LEN
) {
1006 if (sp_aligned
!= NULL
) {
1007 FREE(sp_aligned
, M_SECA
);
1012 if (sp_aligned
== NULL
) {
1013 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
1014 if (sp_aligned
== NULL
) {
1023 memcpy(sp
, sp_unaligned
, len
);
1025 // no need to check output pointer alignment
1027 if (aes_decrypt_gcm(sp
, len
, dptr
+ dn
, ctx
->decrypt
)) {
1028 ipseclog((LOG_ERR
, "%s: failed to decrypt\n", __FUNCTION__
));
1033 // update unaligned pointers
1034 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
1038 /* udpate offsets */
1042 /* find the next source block */
1043 while (s
&& sn
>= s
->m_len
) {
1050 /* free un-needed source mbufs and add dest mbufs to chain */
1051 m_freem(scut
->m_next
);
1052 scut
->m_len
= scutoff
;
1056 if (sp_aligned
!= NULL
) {
1057 FREE(sp_aligned
, M_SECA
);
1062 bzero(iv
, sizeof(iv
));