+/*
+ * 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 $ */
#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;
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;
}
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;
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;
}
}
/* grab iv */
- m_copydata(m, ivoff, ivlen, iv);
+ m_copydata(m, ivoff, ivlen, (caddr_t) iv);
s = m;
soff = sn = dn = 0;
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
}
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)
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;
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;
/* 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;
}
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
}
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)
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;