]>
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
) {
292 if (sp_aligned
== NULL
) {
293 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
294 if (sp_aligned
== NULL
)
298 memcpy(sp
, sp_unaligned
, len
);
300 // no need to check output pointer alignment
301 aes_decrypt_cbc(sp
, iv
, len
>> 4, dptr
+ dn
,
302 (aes_decrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->decrypt
)));
304 // update unaligned pointers
305 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
314 bcopy(sp
+ len
- AES_BLOCKLEN
, iv
, AES_BLOCKLEN
);
316 /* find the next source block */
317 while (s
&& sn
>= s
->m_len
) {
325 /* free un-needed source mbufs and add dest mbufs to chain */
326 m_freem(scut
->m_next
);
327 scut
->m_len
= scutoff
;
331 if (sp_aligned
!= NULL
) {
332 FREE(sp_aligned
, M_SECA
);
337 bzero(iv
, sizeof(iv
));
338 bzero(sbuf
, sizeof(sbuf
));
347 __unused
size_t plen
,
348 struct secasvar
*sav
,
349 const struct esp_algorithm
*algo
,
353 struct mbuf
*d
, *d0
, *dp
;
354 int soff
; /* offset from the head of chain, to head of this mbuf */
355 int sn
, dn
; /* offset from the head of the mbuf, to meat */
356 size_t ivoff
, bodyoff
;
357 u_int8_t
*ivp
, *dptr
, *ivp_unaligned
;
358 u_int8_t sbuf
[AES_BLOCKLEN
] __attribute__((aligned(4))), *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
359 u_int8_t ivp_aligned_buf
[AES_BLOCKLEN
] __attribute__((aligned(4)));
364 if (ivlen
!= AES_BLOCKLEN
) {
365 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
366 "unsupported ivlen %d\n", algo
->name
, ivlen
));
371 if (sav
->flags
& SADB_X_EXT_OLD
) {
373 ivoff
= off
+ sizeof(struct esp
);
374 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
376 ivoff
= off
+ sizeof(struct newesp
);
377 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
380 /* put iv into the packet */
381 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
382 ivp
= (u_int8_t
*) sav
->iv
;
384 if (m
->m_pkthdr
.len
< bodyoff
) {
385 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
386 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
390 if ((m
->m_pkthdr
.len
- bodyoff
) % AES_BLOCKLEN
) {
391 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
392 "payload length must be multiple of %lu\n",
393 algo
->name
, AES_BLOCKLEN
));
403 /* skip headers/IV */
404 while (soff
< bodyoff
) {
405 if (soff
+ s
->m_len
> bodyoff
) {
416 /* skip over empty mbuf */
417 while (s
&& s
->m_len
== 0)
420 while (soff
< m
->m_pkthdr
.len
) {
422 if (sn
+ AES_BLOCKLEN
<= s
->m_len
) {
423 /* body is continuous */
424 sp
= mtod(s
, u_int8_t
*) + sn
;
426 len
-= len
% AES_BLOCKLEN
; // full blocks only
428 /* body is non-continuous */
429 m_copydata(s
, sn
, AES_BLOCKLEN
, (caddr_t
) sbuf
);
431 len
= AES_BLOCKLEN
; // 1 block only in sbuf
435 if (!d
|| dn
+ AES_BLOCKLEN
> d
->m_len
) {
438 MGET(d
, M_DONTWAIT
, MT_DATA
);
439 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
441 MCLGET(d
, M_DONTWAIT
);
442 if ((d
->m_flags
& M_EXT
) == 0) {
443 d
= m_mbigget(d
, M_DONTWAIT
);
444 if ((d
->m_flags
& M_EXT
) == 0) {
461 // try to make mbuf data aligned
462 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
463 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
466 d
->m_len
= M_TRAILINGSPACE(d
);
467 d
->m_len
-= d
->m_len
% AES_BLOCKLEN
;
470 dptr
= mtod(d
, u_int8_t
*);
474 /* adjust len if greater than space available */
475 if (len
> d
->m_len
- dn
)
479 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
480 if (IPSEC_IS_P2ALIGNED(sp
)) {
484 if (len
> MAX_REALIGN_LEN
) {
487 if (sp_aligned
== NULL
) {
488 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
489 if (sp_aligned
== NULL
)
493 memcpy(sp
, sp_unaligned
, len
);
495 // check ivp pointer alignment and use a separate aligned buffer (if ivp is not aligned on 4-byte boundary).
496 if (IPSEC_IS_P2ALIGNED(ivp
)) {
497 ivp_unaligned
= NULL
;
500 ivp
= ivp_aligned_buf
;
501 memcpy(ivp
, ivp_unaligned
, AES_BLOCKLEN
);
503 // no need to check output pointer alignment
504 aes_encrypt_cbc(sp
, ivp
, len
>> 4, dptr
+ dn
,
505 (aes_encrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->encrypt
)));
507 // update unaligned pointers
508 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
511 if (!IPSEC_IS_P2ALIGNED(ivp_unaligned
)) {
520 ivp
= dptr
+ dn
- AES_BLOCKLEN
; // last block encrypted
522 /* find the next source block and skip empty mbufs */
523 while (s
&& sn
>= s
->m_len
) {
530 /* free un-needed source mbufs and add dest mbufs to chain */
531 m_freem(scut
->m_next
);
532 scut
->m_len
= scutoff
;
536 if (sp_aligned
!= NULL
) {
537 FREE(sp_aligned
, M_SECA
);
542 bzero(sbuf
, sizeof(sbuf
));
550 __unused
const struct esp_algorithm
*algo
)
552 return (sizeof(aes_gcm_ctx
) + aes_decrypt_get_ctx_size_gcm() + aes_encrypt_get_ctx_size_gcm() + ESP_GCM_ALIGN
);
556 esp_gcm_schedule( __unused
const struct esp_algorithm
*algo
,
557 struct secasvar
*sav
)
559 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
560 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
561 u_int ivlen
= sav
->ivlen
;
562 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
565 ctx
->decrypt
= &ctx
->ctxt
[0];
566 ctx
->encrypt
= &ctx
->ctxt
[aes_decrypt_get_ctx_size_gcm() / sizeof(ccgcm_ctx
)];
568 rc
= aes_decrypt_key_gcm((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ctx
->decrypt
);
573 bzero(nonce
, ESP_GCM_SALT_LEN
+ ivlen
);
574 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
575 memcpy(nonce
+ESP_GCM_SALT_LEN
, sav
->iv
, ivlen
);
577 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
);
582 rc
= aes_encrypt_reset_gcm(ctx
->encrypt
);
591 esp_gcm_encrypt_finalize(struct secasvar
*sav
,
592 unsigned char *tag
, unsigned int tag_bytes
)
594 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
595 return (aes_encrypt_finalize_gcm(tag
, tag_bytes
, ctx
->encrypt
));
599 esp_gcm_decrypt_finalize(struct secasvar
*sav
,
600 unsigned char *tag
, unsigned int tag_bytes
)
602 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
603 return (aes_decrypt_finalize_gcm(tag
, tag_bytes
, ctx
->decrypt
));
610 __unused
size_t plen
,
611 struct secasvar
*sav
,
612 const struct esp_algorithm
*algo __unused
,
616 struct mbuf
*d
, *d0
, *dp
;
617 int soff
; /* offset from the head of chain, to head of this mbuf */
618 int sn
, dn
; /* offset from the head of the mbuf, to meat */
619 size_t ivoff
, bodyoff
;
620 u_int8_t
*dptr
, *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
625 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
627 if (ivlen
!= ESP_GCM_IVLEN
) {
628 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
633 if (sav
->flags
& SADB_X_EXT_OLD
) {
635 ivoff
= off
+ sizeof(struct esp
);
636 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
638 ivoff
= off
+ sizeof(struct newesp
);
639 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
642 bzero(nonce
, ESP_GCM_SALT_LEN
+ivlen
);
643 /* generate new iv */
644 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
646 if (aes_encrypt_reset_gcm(ctx
->encrypt
)) {
647 ipseclog((LOG_ERR
, "%s: gcm reset failure\n", __FUNCTION__
));
652 if (aes_encrypt_inc_iv_gcm((unsigned char *)nonce
, ctx
->encrypt
)) {
653 ipseclog((LOG_ERR
, "%s: iv generation failure\n", __FUNCTION__
));
659 * The IV is now generated within corecrypto and
660 * is provided to ESP using aes_encrypt_inc_iv_gcm().
661 * This makes the sav->iv redundant and is no longer
662 * used in GCM operations. But we still copy the IV
663 * back to sav->iv to ensure that any future code reading
664 * this value will get the latest IV.
666 memcpy(sav
->iv
, (nonce
+ ESP_GCM_SALT_LEN
), ivlen
);
667 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
668 bzero(nonce
, ESP_GCM_SALT_LEN
+ivlen
);
670 if (m
->m_pkthdr
.len
< bodyoff
) {
671 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
672 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
677 /* Set Additional Authentication Data */
678 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
680 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
681 if (aes_encrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->encrypt
)) {
682 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
693 /* skip headers/IV */
694 while (soff
< bodyoff
) {
695 if (soff
+ s
->m_len
> bodyoff
) {
706 /* skip over empty mbuf */
707 while (s
&& s
->m_len
== 0)
710 while (soff
< m
->m_pkthdr
.len
) {
712 sp
= mtod(s
, u_int8_t
*) + sn
;
716 if (!d
|| (dn
+ len
> d
->m_len
)) {
719 MGET(d
, M_DONTWAIT
, MT_DATA
);
720 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
722 MCLGET(d
, M_DONTWAIT
);
723 if ((d
->m_flags
& M_EXT
) == 0) {
724 d
= m_mbigget(d
, M_DONTWAIT
);
725 if ((d
->m_flags
& M_EXT
) == 0) {
742 // try to make mbuf data aligned
743 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
744 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
747 d
->m_len
= M_TRAILINGSPACE(d
);
752 dptr
= mtod(d
, u_int8_t
*);
756 /* adjust len if greater than space available */
757 if (len
> d
->m_len
- dn
)
761 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
762 if (IPSEC_IS_P2ALIGNED(sp
)) {
766 if (len
> MAX_REALIGN_LEN
) {
769 if (sp_aligned
== NULL
) {
770 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
771 if (sp_aligned
== NULL
)
775 memcpy(sp
, sp_unaligned
, len
);
778 if (aes_encrypt_gcm(sp
, len
, dptr
+dn
, ctx
->encrypt
)) {
779 ipseclog((LOG_ERR
, "%s: failed to encrypt\n", __FUNCTION__
));
784 // update unaligned pointers
785 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
793 /* find the next source block and skip empty mbufs */
794 while (s
&& sn
>= s
->m_len
) {
801 /* free un-needed source mbufs and add dest mbufs to chain */
802 m_freem(scut
->m_next
);
803 scut
->m_len
= scutoff
;
807 if (sp_aligned
!= NULL
) {
808 FREE(sp_aligned
, M_SECA
);
819 struct secasvar
*sav
,
820 const struct esp_algorithm
*algo __unused
,
824 struct mbuf
*d
, *d0
, *dp
;
825 int soff
; /* offset from the head of chain, to head of this mbuf */
826 int sn
, dn
; /* offset from the head of the mbuf, to meat */
827 size_t ivoff
, bodyoff
;
828 u_int8_t iv
[ESP_GCM_IVLEN
] __attribute__((aligned(4))), *dptr
;
829 u_int8_t
*sp
, *sp_unaligned
, *sp_aligned
= NULL
;
834 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
836 if (ivlen
!= ESP_GCM_IVLEN
) {
837 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
842 if (sav
->flags
& SADB_X_EXT_OLD
) {
844 ivoff
= off
+ sizeof(struct esp
);
845 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
847 ivoff
= off
+ sizeof(struct newesp
);
848 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
851 if (m
->m_pkthdr
.len
< bodyoff
) {
852 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
853 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
859 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
862 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
863 memcpy(nonce
+ESP_GCM_SALT_LEN
, iv
, ivlen
);
865 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
866 if (aes_decrypt_set_iv_gcm(nonce
, sizeof(nonce
), ctx
->decrypt
)) {
867 ipseclog((LOG_ERR
, "%s: failed to set IV\n", __FUNCTION__
));
869 bzero(nonce
, sizeof(nonce
));
872 bzero(nonce
, sizeof(nonce
));
874 /* Set Additional Authentication Data */
875 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
877 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
878 if (aes_decrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->decrypt
)) {
879 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
889 /* skip header/IV offset */
890 while (soff
< bodyoff
) {
891 if (soff
+ s
->m_len
> bodyoff
) {
902 /* skip over empty mbuf */
903 while (s
&& s
->m_len
== 0)
906 while (soff
< m
->m_pkthdr
.len
) {
908 sp
= mtod(s
, u_int8_t
*) + sn
;
912 if (!d
|| (dn
+ len
> d
->m_len
)) {
915 MGET(d
, M_DONTWAIT
, MT_DATA
);
916 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
918 MCLGET(d
, M_DONTWAIT
);
919 if ((d
->m_flags
& M_EXT
) == 0) {
920 d
= m_mbigget(d
, M_DONTWAIT
);
921 if ((d
->m_flags
& M_EXT
) == 0) {
938 // try to make mbuf data aligned
939 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
940 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
943 d
->m_len
= M_TRAILINGSPACE(d
);
948 dptr
= mtod(d
, u_int8_t
*);
952 /* adjust len if greater than space available in dest */
953 if (len
> d
->m_len
- dn
)
957 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
958 if (IPSEC_IS_P2ALIGNED(sp
)) {
962 if (len
> MAX_REALIGN_LEN
) {
965 if (sp_aligned
== NULL
) {
966 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
967 if (sp_aligned
== NULL
)
971 memcpy(sp
, sp_unaligned
, len
);
973 // no need to check output pointer alignment
975 if (aes_decrypt_gcm(sp
, len
, dptr
+ dn
, ctx
->decrypt
)) {
976 ipseclog((LOG_ERR
, "%s: failed to decrypt\n", __FUNCTION__
));
981 // update unaligned pointers
982 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
990 /* find the next source block */
991 while (s
&& sn
>= s
->m_len
) {
998 /* free un-needed source mbufs and add dest mbufs to chain */
999 m_freem(scut
->m_next
);
1000 scut
->m_len
= scutoff
;
1004 if (sp_aligned
!= NULL
) {
1005 FREE(sp_aligned
, M_SECA
);
1010 bzero(iv
, sizeof(iv
));