]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/esp_rijndael.c
2 * Copyright (c) 2008 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)
151 esp_cbc_decrypt_aes(m
, off
, sav
, algo
, ivlen
)
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
);
563 ctx
->decrypt
= &ctx
->ctxt
[0];
564 ctx
->encrypt
= &ctx
->ctxt
[aes_decrypt_get_ctx_size_gcm() / sizeof(ccgcm_ctx
)];
566 rc
= aes_decrypt_key_gcm((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ctx
->decrypt
);
571 rc
= aes_encrypt_key_gcm((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ctx
->encrypt
);
579 esp_gcm_encrypt_finalize(struct secasvar
*sav
,
580 unsigned char *tag
, unsigned int tag_bytes
)
582 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
583 return (aes_encrypt_finalize_gcm(tag
, tag_bytes
, ctx
->encrypt
));
587 esp_gcm_decrypt_finalize(struct secasvar
*sav
,
588 unsigned char *tag
, unsigned int tag_bytes
)
590 aes_gcm_ctx
*ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
591 return (aes_decrypt_finalize_gcm(tag
, tag_bytes
, ctx
->decrypt
));
598 __unused
size_t plen
,
599 struct secasvar
*sav
,
600 const struct esp_algorithm
*algo __unused
,
604 struct mbuf
*d
, *d0
, *dp
;
605 int soff
; /* offset from the head of chain, to head of this mbuf */
606 int sn
, dn
; /* offset from the head of the mbuf, to meat */
607 size_t ivoff
, bodyoff
;
608 u_int8_t
*dptr
, *sp
, *sp_unaligned
, *sp_aligned
= NULL
;
613 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
615 if (ivlen
!= ESP_GCM_IVLEN
) {
616 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
621 if (sav
->flags
& SADB_X_EXT_OLD
) {
623 ivoff
= off
+ sizeof(struct esp
);
624 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
626 ivoff
= off
+ sizeof(struct newesp
);
627 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
630 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
632 if (m
->m_pkthdr
.len
< bodyoff
) {
633 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
634 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
640 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
641 memcpy(nonce
+ESP_GCM_SALT_LEN
, sav
->iv
, ivlen
);
643 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
644 if (aes_encrypt_set_iv_gcm(nonce
, sizeof(nonce
), ctx
->encrypt
)) {
645 ipseclog((LOG_ERR
, "%s: failed to set IV\n", __FUNCTION__
));
647 bzero(nonce
, sizeof(nonce
));
650 bzero(nonce
, sizeof(nonce
));
652 /* Set Additional Authentication Data */
653 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
655 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
656 if (aes_encrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->encrypt
)) {
657 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
668 /* skip headers/IV */
669 while (soff
< bodyoff
) {
670 if (soff
+ s
->m_len
> bodyoff
) {
681 /* skip over empty mbuf */
682 while (s
&& s
->m_len
== 0)
685 while (soff
< m
->m_pkthdr
.len
) {
687 sp
= mtod(s
, u_int8_t
*) + sn
;
691 if (!d
|| (dn
+ len
> d
->m_len
)) {
694 MGET(d
, M_DONTWAIT
, MT_DATA
);
695 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
697 MCLGET(d
, M_DONTWAIT
);
698 if ((d
->m_flags
& M_EXT
) == 0) {
699 d
= m_mbigget(d
, M_DONTWAIT
);
700 if ((d
->m_flags
& M_EXT
) == 0) {
717 // try to make mbuf data aligned
718 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
719 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
722 d
->m_len
= M_TRAILINGSPACE(d
);
727 dptr
= mtod(d
, u_int8_t
*);
731 /* adjust len if greater than space available */
732 if (len
> d
->m_len
- dn
)
736 // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
737 if (IPSEC_IS_P2ALIGNED(sp
)) {
741 if (len
> MAX_REALIGN_LEN
) {
744 if (sp_aligned
== NULL
) {
745 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
746 if (sp_aligned
== NULL
)
750 memcpy(sp
, sp_unaligned
, len
);
753 if (aes_encrypt_gcm(sp
, len
, dptr
+dn
, ctx
->encrypt
)) {
754 ipseclog((LOG_ERR
, "%s: failed to encrypt\n", __FUNCTION__
));
759 // update unaligned pointers
760 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
768 /* find the next source block and skip empty mbufs */
769 while (s
&& sn
>= s
->m_len
) {
776 /* free un-needed source mbufs and add dest mbufs to chain */
777 m_freem(scut
->m_next
);
778 scut
->m_len
= scutoff
;
782 if (sp_aligned
!= NULL
) {
783 FREE(sp_aligned
, M_SECA
);
787 /* generate new iv */
794 esp_gcm_decrypt_aes(m
, off
, sav
, algo
, ivlen
)
797 struct secasvar
*sav
;
798 const struct esp_algorithm
*algo __unused
;
802 struct mbuf
*d
, *d0
, *dp
;
803 int soff
; /* offset from the head of chain, to head of this mbuf */
804 int sn
, dn
; /* offset from the head of the mbuf, to meat */
805 size_t ivoff
, bodyoff
;
806 u_int8_t iv
[ESP_GCM_IVLEN
] __attribute__((aligned(4))), *dptr
;
807 u_int8_t
*sp
, *sp_unaligned
, *sp_aligned
= NULL
;
812 unsigned char nonce
[ESP_GCM_SALT_LEN
+ivlen
];
814 if (ivlen
!= ESP_GCM_IVLEN
) {
815 ipseclog((LOG_ERR
, "%s: unsupported ivlen %d\n", __FUNCTION__
, ivlen
));
820 if (sav
->flags
& SADB_X_EXT_OLD
) {
822 ivoff
= off
+ sizeof(struct esp
);
823 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
825 ivoff
= off
+ sizeof(struct newesp
);
826 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
829 if (m
->m_pkthdr
.len
< bodyoff
) {
830 ipseclog((LOG_ERR
, "%s: bad len %d/%lu\n", __FUNCTION__
,
831 m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
837 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
840 memcpy(nonce
, _KEYBUF(sav
->key_enc
)+_KEYLEN(sav
->key_enc
)-ESP_GCM_SALT_LEN
, ESP_GCM_SALT_LEN
);
841 memcpy(nonce
+ESP_GCM_SALT_LEN
, iv
, ivlen
);
843 ctx
= (aes_gcm_ctx
*)P2ROUNDUP(sav
->sched
, ESP_GCM_ALIGN
);
844 if (aes_decrypt_set_iv_gcm(nonce
, sizeof(nonce
), ctx
->decrypt
)) {
845 ipseclog((LOG_ERR
, "%s: failed to set IV\n", __FUNCTION__
));
847 bzero(nonce
, sizeof(nonce
));
850 bzero(nonce
, sizeof(nonce
));
852 /* Set Additional Authentication Data */
853 if (!(sav
->flags
& SADB_X_EXT_OLD
)) {
855 m_copydata(m
, off
, sizeof(esp
), (caddr_t
) &esp
);
856 if (aes_decrypt_aad_gcm((unsigned char*)&esp
, sizeof(esp
), ctx
->decrypt
)) {
857 ipseclog((LOG_ERR
, "%s: packet decryption AAD failure\n", __FUNCTION__
));
867 /* skip header/IV offset */
868 while (soff
< bodyoff
) {
869 if (soff
+ s
->m_len
> bodyoff
) {
880 /* skip over empty mbuf */
881 while (s
&& s
->m_len
== 0)
884 while (soff
< m
->m_pkthdr
.len
) {
886 sp
= mtod(s
, u_int8_t
*) + sn
;
890 if (!d
|| (dn
+ len
> d
->m_len
)) {
893 MGET(d
, M_DONTWAIT
, MT_DATA
);
894 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
896 MCLGET(d
, M_DONTWAIT
);
897 if ((d
->m_flags
& M_EXT
) == 0) {
898 d
= m_mbigget(d
, M_DONTWAIT
);
899 if ((d
->m_flags
& M_EXT
) == 0) {
916 // try to make mbuf data aligned
917 if (!IPSEC_IS_P2ALIGNED(d
->m_data
)) {
918 m_adj(d
, IPSEC_GET_P2UNALIGNED_OFS(d
->m_data
));
921 d
->m_len
= M_TRAILINGSPACE(d
);
926 dptr
= mtod(d
, u_int8_t
*);
930 /* adjust len if greater than space available in dest */
931 if (len
> d
->m_len
- dn
)
935 // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
936 if (IPSEC_IS_P2ALIGNED(sp
)) {
940 if (len
> MAX_REALIGN_LEN
) {
943 if (sp_aligned
== NULL
) {
944 sp_aligned
= (u_int8_t
*)_MALLOC(MAX_REALIGN_LEN
, M_SECA
, M_DONTWAIT
);
945 if (sp_aligned
== NULL
)
949 memcpy(sp
, sp_unaligned
, len
);
951 // no need to check output pointer alignment
953 if (aes_decrypt_gcm(sp
, len
, dptr
+ dn
, ctx
->decrypt
)) {
954 ipseclog((LOG_ERR
, "%s: failed to decrypt\n", __FUNCTION__
));
959 // update unaligned pointers
960 if (!IPSEC_IS_P2ALIGNED(sp_unaligned
)) {
968 /* find the next source block */
969 while (s
&& sn
>= s
->m_len
) {
976 /* free un-needed source mbufs and add dest mbufs to chain */
977 m_freem(scut
->m_next
);
978 scut
->m_len
= scutoff
;
982 if (sp_aligned
!= NULL
) {
983 FREE(sp_aligned
, M_SECA
);
988 bzero(iv
, sizeof(iv
));