]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet6/esp_rijndael.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / bsd / netinet6 / esp_rijndael.c
index 0b6df997a78334273f8f05e32630d6ccfd46218c..af5ddff1f8d26f60cd2a606af1d7f42742ea41e3 100644 (file)
@@ -1,3 +1,31 @@
+/*
+ * Copyright (c) 2008 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
+ * 
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
+ * 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ * 
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
+ */
+
 /*     $FreeBSD: src/sys/netinet6/esp_rijndael.c,v 1.1.2.1 2001/07/03 11:01:50 ume Exp $       */
 /*     $KAME: esp_rijndael.c,v 1.4 2001/03/02 05:53:05 itojun Exp $    */
 
@@ -36,6 +64,7 @@
 #include <sys/queue.h>
 #include <sys/syslog.h>
 #include <sys/mbuf.h>
+#include <sys/mcache.h>
 
 #include <kern/locks.h>
 
 #include <netinet6/esp.h>
 #include <netinet6/esp_rijndael.h>
 
-#include <crypto/aes/aes.h>
+#include <libkern/crypto/aes.h>
 
 #include <netkey/key.h>
 
 #include <net/net_osdep.h>
 
 #define AES_BLOCKLEN 16
+#define MAX_SBUF_LEN 2000
 
 extern lck_mtx_t *sadb_mutex;
 
@@ -73,8 +103,8 @@ esp_aes_schedule(
        lck_mtx_assert(sadb_mutex, LCK_MTX_ASSERT_OWNED);
        aes_ctx *ctx = (aes_ctx*)sav->sched;
        
-       aes_decrypt_key(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc), &ctx->decrypt);
-       aes_encrypt_key(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc), &ctx->encrypt);
+       aes_decrypt_key((const unsigned char *) _KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc), &ctx->decrypt);
+       aes_encrypt_key((const unsigned char *) _KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc), &ctx->encrypt);
        
        return 0;
 }
@@ -121,8 +151,8 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
        int soff;       /* offset from the head of chain, to head of this mbuf */
        int sn, dn;     /* offset from the head of the mbuf, to meat */
        size_t ivoff, bodyoff;
-       u_int8_t iv[AES_BLOCKLEN], *dptr;
-       u_int8_t sbuf[AES_BLOCKLEN], *sp;
+       u_int8_t iv[AES_BLOCKLEN] __attribute__((aligned(4))), *dptr;
+       u_int8_t sbuf[MAX_SBUF_LEN] __attribute__((aligned(4))), *sp, *sp_unaligned;
        struct mbuf *scut;
        int scutoff;
        int     i, len;
@@ -146,7 +176,7 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
 
        if (m->m_pkthdr.len < bodyoff) {
                ipseclog((LOG_ERR, "esp_cbc_decrypt %s: bad len %d/%lu\n",
-                   algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
+                   algo->name, m->m_pkthdr.len, (u_int32_t)bodyoff));
                m_freem(m);
                return EINVAL;
        }
@@ -159,7 +189,7 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
        }
 
        /* grab iv */
-       m_copydata(m, ivoff, ivlen, iv);
+       m_copydata(m, ivoff, ivlen, (caddr_t) iv);
 
        s = m;
        soff = sn = dn = 0;
@@ -192,7 +222,7 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
                        len -= len % AES_BLOCKLEN;      // full blocks only
                } else {
                        /* body is non-continuous */
-                       m_copydata(s, sn, AES_BLOCKLEN, sbuf);
+                       m_copydata(s, sn, AES_BLOCKLEN, (caddr_t) sbuf);
                        sp = sbuf;
                        len = AES_BLOCKLEN;                     // 1 block only in sbuf
                }
@@ -223,6 +253,12 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
                                d0 = d;
                        if (dp)
                                dp->m_next = d;
+
+                       // try to make mbuf data aligned
+                       if (!IPSEC_IS_P2ALIGNED(d->m_data)) {
+                               m_adj(d, IPSEC_GET_P2UNALIGNED_OFS(d->m_data));
+                       }
+
                        d->m_len = M_TRAILINGSPACE(d);
                        d->m_len -= d->m_len % AES_BLOCKLEN;
                        if (d->m_len > i)
@@ -236,9 +272,23 @@ esp_cbc_decrypt_aes(m, off, sav, algo, ivlen)
                        len = d->m_len - dn;
 
                /* decrypt */
+               // check input pointer alignment and use a separate aligned buffer (if sp is unaligned on 4-byte boundary).
+               if (IPSEC_IS_P2ALIGNED(sp)) {
+                       sp_unaligned = NULL;
+               } else {
+                       sp_unaligned = sp;
+                       sp = sbuf;
+                       memcpy(sp, sp_unaligned, len);
+               }
+               // no need to check output pointer alignment
                aes_decrypt_cbc(sp, iv, len >> 4, dptr + dn, 
                                (aes_decrypt_ctx*)(&(((aes_ctx*)sav->sched)->decrypt)));
                
+               // update unaligned pointers
+               if (!IPSEC_IS_P2ALIGNED(sp_unaligned)) {
+                       sp = sp_unaligned;
+               }
+
                /* udpate offsets */
                sn += len;
                dn += len;
@@ -281,8 +331,9 @@ esp_cbc_encrypt_aes(
        int soff;       /* offset from the head of chain, to head of this mbuf */
        int sn, dn;     /* offset from the head of the mbuf, to meat */
        size_t ivoff, bodyoff;
-       u_int8_t *ivp, *dptr;
-       u_int8_t sbuf[AES_BLOCKLEN], *sp;
+       u_int8_t *ivp, *dptr, *ivp_unaligned;
+       u_int8_t sbuf[MAX_SBUF_LEN] __attribute__((aligned(4))), *sp, *sp_unaligned;
+       u_int8_t ivp_aligned_buf[AES_BLOCKLEN] __attribute__((aligned(4)));
        struct mbuf *scut;
        int scutoff;
        int i, len;
@@ -305,11 +356,11 @@ esp_cbc_encrypt_aes(
 
        /* put iv into the packet */
        m_copyback(m, ivoff, ivlen, sav->iv);
-       ivp = sav->iv;
+       ivp = (u_int8_t *) sav->iv;
 
        if (m->m_pkthdr.len < bodyoff) {
                ipseclog((LOG_ERR, "esp_cbc_encrypt %s: bad len %d/%lu\n",
-                   algo->name, m->m_pkthdr.len, (unsigned long)bodyoff));
+                   algo->name, m->m_pkthdr.len, (u_int32_t)bodyoff));
                m_freem(m);
                return EINVAL;
        }
@@ -352,7 +403,7 @@ esp_cbc_encrypt_aes(
                        len -= len % AES_BLOCKLEN;      // full blocks only
                } else {
                        /* body is non-continuous */
-                       m_copydata(s, sn, AES_BLOCKLEN, sbuf);
+                       m_copydata(s, sn, AES_BLOCKLEN, (caddr_t) sbuf);
                        sp = sbuf;
                        len = AES_BLOCKLEN;                     // 1 block only in sbuf
                }
@@ -384,6 +435,11 @@ esp_cbc_encrypt_aes(
                        if (dp)
                                dp->m_next = d;
 
+                       // try to make mbuf data aligned
+                       if (!IPSEC_IS_P2ALIGNED(d->m_data)) {
+                               m_adj(d, IPSEC_GET_P2UNALIGNED_OFS(d->m_data));
+                       }
+
                        d->m_len = M_TRAILINGSPACE(d);
                        d->m_len -= d->m_len % AES_BLOCKLEN;
                        if (d->m_len > i)
@@ -397,9 +453,34 @@ esp_cbc_encrypt_aes(
                        len = d->m_len - dn;
                
                /* encrypt */
+               // check input pointer alignment and use a separate aligned buffer (if sp is not aligned on 4-byte boundary).
+               if (IPSEC_IS_P2ALIGNED(sp)) {
+                       sp_unaligned = NULL;
+               } else {
+                       sp_unaligned = sp;
+                       sp = sbuf;
+                       memcpy(sp, sp_unaligned, len);
+               }
+               // check ivp pointer alignment and use a separate aligned buffer (if ivp is not aligned on 4-byte boundary).
+               if (IPSEC_IS_P2ALIGNED(ivp)) {
+                       ivp_unaligned = NULL;
+               } else {
+                       ivp_unaligned = ivp;
+                       ivp = ivp_aligned_buf;
+                       memcpy(ivp, ivp_unaligned, len);
+               }
+               // no need to check output pointer alignment
                aes_encrypt_cbc(sp, ivp, len >> 4, dptr + dn, 
                        (aes_encrypt_ctx*)(&(((aes_ctx*)sav->sched)->encrypt)));
 
+               // update unaligned pointers
+               if (!IPSEC_IS_P2ALIGNED(sp_unaligned)) {
+                       sp = sp_unaligned;
+               }
+               if (!IPSEC_IS_P2ALIGNED(ivp_unaligned)) {
+                       ivp = ivp_unaligned;
+               }
+
                /* update offsets */
                sn += len;
                dn += len;