]>
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>
68 #include <kern/locks.h>
71 #include <net/route.h>
73 #include <netinet6/ipsec.h>
74 #include <netinet6/esp.h>
75 #include <netinet6/esp_rijndael.h>
77 #include <crypto/aes/aes.h>
79 #include <netkey/key.h>
81 #include <net/net_osdep.h>
83 #define AES_BLOCKLEN 16
85 extern lck_mtx_t
*sadb_mutex
;
89 __unused
const struct esp_algorithm
*algo
)
92 return sizeof(aes_ctx
);
97 __unused
const struct esp_algorithm
*algo
,
101 lck_mtx_assert(sadb_mutex
, LCK_MTX_ASSERT_OWNED
);
102 aes_ctx
*ctx
= (aes_ctx
*)sav
->sched
;
104 aes_decrypt_key((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
), &ctx
->decrypt
);
105 aes_encrypt_key((const unsigned char *) _KEYBUF(sav
->key_enc
), _KEYLEN(sav
->key_enc
), &ctx
->encrypt
);
111 /* The following 2 functions decrypt or encrypt the contents of
112 * the mbuf chain passed in keeping the IP and ESP header's in place,
114 * The code attempts to call the crypto code with the largest chunk
115 * of data it can based on the amount of source data in
116 * the current source mbuf and the space remaining in the current
117 * destination mbuf. The crypto code requires data to be a multiples
118 * of 16 bytes. A separate buffer is used when a 16 byte block spans
122 * off = offset to ESP header
124 * local vars for source:
125 * soff = offset from beginning of the chain to the head of the
127 * scut = last mbuf that contains headers to be retained
128 * scutoff = offset to end of the headers in scut
129 * s = the current mbuf
130 * sn = current offset to data in s (next source data to process)
132 * local vars for dest:
135 * dn = current offset in d (next location to store result)
140 esp_cbc_decrypt_aes(m
, off
, sav
, algo
, ivlen
)
143 struct secasvar
*sav
;
144 const struct esp_algorithm
*algo
;
148 struct mbuf
*d
, *d0
, *dp
;
149 int soff
; /* offset from the head of chain, to head of this mbuf */
150 int sn
, dn
; /* offset from the head of the mbuf, to meat */
151 size_t ivoff
, bodyoff
;
152 u_int8_t iv
[AES_BLOCKLEN
], *dptr
;
153 u_int8_t sbuf
[AES_BLOCKLEN
], *sp
;
159 if (ivlen
!= AES_BLOCKLEN
) {
160 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
161 "unsupported ivlen %d\n", algo
->name
, ivlen
));
166 if (sav
->flags
& SADB_X_EXT_OLD
) {
168 ivoff
= off
+ sizeof(struct esp
);
169 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
171 ivoff
= off
+ sizeof(struct newesp
);
172 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
175 if (m
->m_pkthdr
.len
< bodyoff
) {
176 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: bad len %d/%lu\n",
177 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
181 if ((m
->m_pkthdr
.len
- bodyoff
) % AES_BLOCKLEN
) {
182 ipseclog((LOG_ERR
, "esp_cbc_decrypt %s: "
183 "payload length must be multiple of %d\n",
184 algo
->name
, AES_BLOCKLEN
));
190 m_copydata(m
, ivoff
, ivlen
, (caddr_t
) iv
);
197 /* skip header/IV offset */
198 while (soff
< bodyoff
) {
199 if (soff
+ s
->m_len
> bodyoff
) {
210 /* skip over empty mbuf */
211 while (s
&& s
->m_len
== 0)
214 while (soff
< m
->m_pkthdr
.len
) {
216 if (sn
+ AES_BLOCKLEN
<= s
->m_len
) {
217 /* body is continuous */
218 sp
= mtod(s
, u_int8_t
*) + sn
;
220 len
-= len
% AES_BLOCKLEN
; // full blocks only
222 /* body is non-continuous */
223 m_copydata(s
, sn
, AES_BLOCKLEN
, (caddr_t
) sbuf
);
225 len
= AES_BLOCKLEN
; // 1 block only in sbuf
229 if (!d
|| dn
+ AES_BLOCKLEN
> d
->m_len
) {
232 MGET(d
, M_DONTWAIT
, MT_DATA
);
233 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
235 MCLGET(d
, M_DONTWAIT
);
236 if ((d
->m_flags
& M_EXT
) == 0) {
237 d
= m_mbigget(d
, M_DONTWAIT
);
238 if ((d
->m_flags
& M_EXT
) == 0) {
254 d
->m_len
= M_TRAILINGSPACE(d
);
255 d
->m_len
-= d
->m_len
% AES_BLOCKLEN
;
258 dptr
= mtod(d
, u_int8_t
*);
262 /* adjust len if greater than space available in dest */
263 if (len
> d
->m_len
- dn
)
267 aes_decrypt_cbc(sp
, iv
, len
>> 4, dptr
+ dn
,
268 (aes_decrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->decrypt
)));
275 bcopy(sp
+ len
- AES_BLOCKLEN
, iv
, AES_BLOCKLEN
);
277 /* find the next source block */
278 while (s
&& sn
>= s
->m_len
) {
286 /* free un-needed source mbufs and add dest mbufs to chain */
287 m_freem(scut
->m_next
);
288 scut
->m_len
= scutoff
;
292 bzero(iv
, sizeof(iv
));
293 bzero(sbuf
, sizeof(sbuf
));
302 __unused
size_t plen
,
303 struct secasvar
*sav
,
304 const struct esp_algorithm
*algo
,
308 struct mbuf
*d
, *d0
, *dp
;
309 int soff
; /* offset from the head of chain, to head of this mbuf */
310 int sn
, dn
; /* offset from the head of the mbuf, to meat */
311 size_t ivoff
, bodyoff
;
312 u_int8_t
*ivp
, *dptr
;
313 u_int8_t sbuf
[AES_BLOCKLEN
], *sp
;
318 if (ivlen
!= AES_BLOCKLEN
) {
319 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
320 "unsupported ivlen %d\n", algo
->name
, ivlen
));
325 if (sav
->flags
& SADB_X_EXT_OLD
) {
327 ivoff
= off
+ sizeof(struct esp
);
328 bodyoff
= off
+ sizeof(struct esp
) + ivlen
;
330 ivoff
= off
+ sizeof(struct newesp
);
331 bodyoff
= off
+ sizeof(struct newesp
) + ivlen
;
334 /* put iv into the packet */
335 m_copyback(m
, ivoff
, ivlen
, sav
->iv
);
336 ivp
= (u_int8_t
*) sav
->iv
;
338 if (m
->m_pkthdr
.len
< bodyoff
) {
339 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: bad len %d/%lu\n",
340 algo
->name
, m
->m_pkthdr
.len
, (u_int32_t
)bodyoff
));
344 if ((m
->m_pkthdr
.len
- bodyoff
) % AES_BLOCKLEN
) {
345 ipseclog((LOG_ERR
, "esp_cbc_encrypt %s: "
346 "payload length must be multiple of %lu\n",
347 algo
->name
, AES_BLOCKLEN
));
357 /* skip headers/IV */
358 while (soff
< bodyoff
) {
359 if (soff
+ s
->m_len
> bodyoff
) {
370 /* skip over empty mbuf */
371 while (s
&& s
->m_len
== 0)
374 while (soff
< m
->m_pkthdr
.len
) {
376 if (sn
+ AES_BLOCKLEN
<= s
->m_len
) {
377 /* body is continuous */
378 sp
= mtod(s
, u_int8_t
*) + sn
;
380 len
-= len
% AES_BLOCKLEN
; // full blocks only
382 /* body is non-continuous */
383 m_copydata(s
, sn
, AES_BLOCKLEN
, (caddr_t
) sbuf
);
385 len
= AES_BLOCKLEN
; // 1 block only in sbuf
389 if (!d
|| dn
+ AES_BLOCKLEN
> d
->m_len
) {
392 MGET(d
, M_DONTWAIT
, MT_DATA
);
393 i
= m
->m_pkthdr
.len
- (soff
+ sn
);
395 MCLGET(d
, M_DONTWAIT
);
396 if ((d
->m_flags
& M_EXT
) == 0) {
397 d
= m_mbigget(d
, M_DONTWAIT
);
398 if ((d
->m_flags
& M_EXT
) == 0) {
415 d
->m_len
= M_TRAILINGSPACE(d
);
416 d
->m_len
-= d
->m_len
% AES_BLOCKLEN
;
419 dptr
= mtod(d
, u_int8_t
*);
423 /* adjust len if greater than space available */
424 if (len
> d
->m_len
- dn
)
428 aes_encrypt_cbc(sp
, ivp
, len
>> 4, dptr
+ dn
,
429 (aes_encrypt_ctx
*)(&(((aes_ctx
*)sav
->sched
)->encrypt
)));
436 ivp
= dptr
+ dn
- AES_BLOCKLEN
; // last block encrypted
438 /* find the next source block and skip empty mbufs */
439 while (s
&& sn
>= s
->m_len
) {
447 /* free un-needed source mbufs and add dest mbufs to chain */
448 m_freem(scut
->m_next
);
449 scut
->m_len
= scutoff
;
453 bzero(sbuf
, sizeof(sbuf
));