#include <sys/cdefs.h>
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/ethernet.h>
#include <net80211/ieee80211_var.h>
#include <crypto/rijndael/rijndael.h>
#define AES_BLOCK_LEN 16
#define CCMP_128_MIC_LEN 8
#define CCMP_256_MIC_LEN 16
struct ccmp_ctx {
struct ieee80211vap *cc_vap;
struct ieee80211com *cc_ic;
rijndael_ctx cc_aes;
};
static void *ccmp_attach(struct ieee80211vap *, struct ieee80211_key *);
static void ccmp_detach(struct ieee80211_key *);
static int ccmp_setkey(struct ieee80211_key *);
static void ccmp_setiv(struct ieee80211_key *, uint8_t *);
static int ccmp_encap(struct ieee80211_key *, struct mbuf *);
static int ccmp_decap(struct ieee80211_key *, struct mbuf *, int);
static int ccmp_enmic(struct ieee80211_key *, struct mbuf *, int);
static int ccmp_demic(struct ieee80211_key *, struct mbuf *, int);
static const struct ieee80211_cipher ccmp = {
.ic_name = "AES-CCM",
.ic_cipher = IEEE80211_CIPHER_AES_CCM,
.ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
IEEE80211_WEP_EXTIVLEN,
.ic_trailer = CCMP_128_MIC_LEN,
.ic_miclen = 0,
.ic_attach = ccmp_attach,
.ic_detach = ccmp_detach,
.ic_setkey = ccmp_setkey,
.ic_setiv = ccmp_setiv,
.ic_encap = ccmp_encap,
.ic_decap = ccmp_decap,
.ic_enmic = ccmp_enmic,
.ic_demic = ccmp_demic,
};
static const struct ieee80211_cipher ccmp_256 = {
.ic_name = "AES-CCM-256",
.ic_cipher = IEEE80211_CIPHER_AES_CCM_256,
.ic_header = IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN +
IEEE80211_WEP_EXTIVLEN,
.ic_trailer = CCMP_256_MIC_LEN,
.ic_miclen = 0,
.ic_attach = ccmp_attach,
.ic_detach = ccmp_detach,
.ic_setkey = ccmp_setkey,
.ic_setiv = ccmp_setiv,
.ic_encap = ccmp_encap,
.ic_decap = ccmp_decap,
.ic_enmic = ccmp_enmic,
.ic_demic = ccmp_demic,
};
static int ccmp_encrypt(struct ieee80211_key *, struct mbuf *, int hdrlen);
static int ccmp_decrypt(struct ieee80211_key *, u_int64_t pn,
struct mbuf *, int hdrlen);
static int nrefs = 0;
static void *
ccmp_attach(struct ieee80211vap *vap, struct ieee80211_key *k)
{
struct ccmp_ctx *ctx;
ctx = (struct ccmp_ctx *) IEEE80211_MALLOC(sizeof(struct ccmp_ctx),
M_80211_CRYPTO, IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (ctx == NULL) {
vap->iv_stats.is_crypto_nomem++;
return NULL;
}
ctx->cc_vap = vap;
ctx->cc_ic = vap->iv_ic;
nrefs++;
return ctx;
}
static void
ccmp_detach(struct ieee80211_key *k)
{
struct ccmp_ctx *ctx = k->wk_private;
IEEE80211_FREE(ctx, M_80211_CRYPTO);
KASSERT(nrefs > 0, ("imbalanced attach/detach"));
nrefs--;
}
static int
ccmp_get_trailer_len(struct ieee80211_key *k)
{
return (k->wk_cipher->ic_trailer);
}
static int
ccmp_get_header_len(struct ieee80211_key *k)
{
return (k->wk_cipher->ic_header);
}
static int
ccmp_get_ccm_m(struct ieee80211_key *k)
{
if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM)
return (8);
if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM_256)
return (16);
return (8);
}
static int
ccmp_setkey(struct ieee80211_key *k)
{
uint32_t keylen;
struct ccmp_ctx *ctx = k->wk_private;
switch (k->wk_cipher->ic_cipher) {
case IEEE80211_CIPHER_AES_CCM:
keylen = 128;
break;
case IEEE80211_CIPHER_AES_CCM_256:
keylen = 256;
break;
default:
IEEE80211_DPRINTF(ctx->cc_vap, IEEE80211_MSG_CRYPTO,
"%s: Unexpected cipher (%u)",
__func__, k->wk_cipher->ic_cipher);
return (0);
}
if (k->wk_keylen != (keylen/NBBY)) {
IEEE80211_DPRINTF(ctx->cc_vap, IEEE80211_MSG_CRYPTO,
"%s: Invalid key length %u, expecting %u\n",
__func__, k->wk_keylen, keylen/NBBY);
return 0;
}
if (k->wk_flags & IEEE80211_KEY_SWENCRYPT)
rijndael_set_key(&ctx->cc_aes, k->wk_key, k->wk_keylen*NBBY);
return 1;
}
static void
ccmp_setiv(struct ieee80211_key *k, uint8_t *ivp)
{
struct ccmp_ctx *ctx = k->wk_private;
struct ieee80211vap *vap = ctx->cc_vap;
uint8_t keyid;
keyid = ieee80211_crypto_get_keyid(vap, k) << 6;
k->wk_keytsc++;
ivp[0] = k->wk_keytsc >> 0;
ivp[1] = k->wk_keytsc >> 8;
ivp[2] = 0;
ivp[3] = keyid | IEEE80211_WEP_EXTIV;
ivp[4] = k->wk_keytsc >> 16;
ivp[5] = k->wk_keytsc >> 24;
ivp[6] = k->wk_keytsc >> 32;
ivp[7] = k->wk_keytsc >> 40;
}
static int
ccmp_encap(struct ieee80211_key *k, struct mbuf *m)
{
const struct ieee80211_frame *wh;
struct ccmp_ctx *ctx = k->wk_private;
struct ieee80211com *ic = ctx->cc_ic;
uint8_t *ivp;
int hdrlen;
int is_mgmt;
hdrlen = ieee80211_hdrspace(ic, mtod(m, void *));
wh = mtod(m, const struct ieee80211_frame *);
is_mgmt = IEEE80211_IS_MGMT(wh);
if (is_mgmt && (k->wk_flags & IEEE80211_KEY_NOIVMGT))
return 1;
if ((! is_mgmt) && (k->wk_flags & IEEE80211_KEY_NOIV))
return 1;
M_PREPEND(m, ccmp_get_header_len(k), IEEE80211_M_NOWAIT);
if (m == NULL)
return 0;
ivp = mtod(m, uint8_t *);
ovbcopy(ivp + ccmp_get_header_len(k), ivp, hdrlen);
ivp += hdrlen;
ccmp_setiv(k, ivp);
if ((k->wk_flags & IEEE80211_KEY_SWENCRYPT) &&
!ccmp_encrypt(k, m, hdrlen))
return 0;
return 1;
}
static int
ccmp_enmic(struct ieee80211_key *k, struct mbuf *m, int force)
{
return 1;
}
static __inline uint64_t
READ_6(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
{
uint32_t iv32 = (b0 << 0) | (b1 << 8) | (b2 << 16) | (b3 << 24);
uint16_t iv16 = (b4 << 0) | (b5 << 8);
return (((uint64_t)iv16) << 32) | iv32;
}
static int
ccmp_decap(struct ieee80211_key *k, struct mbuf *m, int hdrlen)
{
const struct ieee80211_rx_stats *rxs;
struct ccmp_ctx *ctx = k->wk_private;
struct ieee80211vap *vap = ctx->cc_vap;
struct ieee80211_frame *wh;
uint8_t *ivp, tid;
uint64_t pn;
bool noreplaycheck;
rxs = ieee80211_get_rx_params_ptr(m);
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) != 0)
goto finish;
wh = mtod(m, struct ieee80211_frame *);
ivp = mtod(m, uint8_t *) + hdrlen;
if ((ivp[IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV) == 0) {
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
"%s", "missing ExtIV for AES-CCM cipher");
vap->iv_stats.is_rx_ccmpformat++;
return 0;
}
tid = ieee80211_gettid(wh);
pn = READ_6(ivp[0], ivp[1], ivp[4], ivp[5], ivp[6], ivp[7]);
noreplaycheck = (k->wk_flags & IEEE80211_KEY_NOREPLAY) != 0;
noreplaycheck |= (rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_PN_VALIDATED) != 0;
if (pn <= k->wk_keyrsc[tid] && !noreplaycheck) {
ieee80211_notify_replay_failure(vap, wh, k, pn, tid);
vap->iv_stats.is_rx_ccmpreplay++;
return 0;
}
if ((k->wk_flags & IEEE80211_KEY_SWDECRYPT) &&
!ccmp_decrypt(k, pn, m, hdrlen))
return 0;
finish:
if (! ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP))) {
ovbcopy(mtod(m, void *),
mtod(m, uint8_t *) + ccmp_get_header_len(k),
hdrlen);
m_adj(m, ccmp_get_header_len(k));
}
if ((rxs == NULL) || (rxs->c_pktflags & IEEE80211_RX_F_MIC_STRIP) == 0)
m_adj(m, -ccmp_get_trailer_len(k));
if ((rxs == NULL) || (rxs->c_pktflags & IEEE80211_RX_F_IV_STRIP) == 0) {
if (pn > k->wk_keyrsc[tid])
k->wk_keyrsc[tid] = pn;
}
return 1;
}
static int
ccmp_demic(struct ieee80211_key *k, struct mbuf *m, int force)
{
return 1;
}
static __inline void
xor_block(uint8_t *b, const uint8_t *a, size_t len)
{
int i;
for (i = 0; i < len; i++)
b[i] ^= a[i];
}
static void
ieee80211_crypto_ccmp_init_nonce_flags(const struct ieee80211_frame *wh,
char *b0)
{
if (IEEE80211_IS_DSTODS(wh)) {
if (IEEE80211_IS_QOS_ANY(wh)) {
const struct ieee80211_qosframe_addr4 *qwh4 =
(const struct ieee80211_qosframe_addr4 *) wh;
b0[1] = qwh4->i_qos[0] & 0x0f;
} else {
b0[1] = 0;
}
} else {
if (IEEE80211_IS_QOS_ANY(wh)) {
const struct ieee80211_qosframe *qwh =
(const struct ieee80211_qosframe *) wh;
b0[1] = qwh->i_qos[0] & 0x0f;
} else {
b0[1] = 0;
}
}
}
static void
ccmp_init_blocks(rijndael_ctx *ctx, struct ieee80211_frame *wh,
uint32_t m, u_int64_t pn, size_t dlen,
uint8_t b0[AES_BLOCK_LEN], uint8_t aad[2 * AES_BLOCK_LEN],
uint8_t auth[AES_BLOCK_LEN], uint8_t s0[AES_BLOCK_LEN])
{
m = (m - 2) / 2;
b0[0] = 0x40 | 0x01 | (m << 3);
ieee80211_crypto_ccmp_init_nonce_flags(wh, b0);
IEEE80211_ADDR_COPY(b0 + 2, wh->i_addr2);
b0[8] = pn >> 40;
b0[9] = pn >> 32;
b0[10] = pn >> 24;
b0[11] = pn >> 16;
b0[12] = pn >> 8;
b0[13] = pn >> 0;
b0[14] = (dlen >> 8) & 0xff;
b0[15] = dlen & 0xff;
(void) ieee80211_crypto_init_aad(wh, aad, 2 * AES_BLOCK_LEN);
rijndael_encrypt(ctx, b0, auth);
xor_block(auth, aad, AES_BLOCK_LEN);
rijndael_encrypt(ctx, auth, auth);
xor_block(auth, &aad[AES_BLOCK_LEN], AES_BLOCK_LEN);
rijndael_encrypt(ctx, auth, auth);
b0[0] &= 0x07;
b0[14] = b0[15] = 0;
rijndael_encrypt(ctx, b0, s0);
}
#define CCMP_ENCRYPT(_i, _b, _b0, _pos, _e, _len) do { \
\
xor_block(_b, _pos, _len); \
rijndael_encrypt(&ctx->cc_aes, _b, _b); \
\
_b0[14] = (_i >> 8) & 0xff; \
_b0[15] = _i & 0xff; \
rijndael_encrypt(&ctx->cc_aes, _b0, _e); \
xor_block(_pos, _e, _len); \
} while (0)
static int
ccmp_encrypt(struct ieee80211_key *key, struct mbuf *m0, int hdrlen)
{
struct ccmp_ctx *ctx = key->wk_private;
struct ieee80211_frame *wh;
struct mbuf *m = m0;
int data_len, i, space;
uint8_t aad[2 * AES_BLOCK_LEN], b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN],
e[AES_BLOCK_LEN], s0[AES_BLOCK_LEN];
uint8_t *pos;
ctx->cc_vap->iv_stats.is_crypto_ccmp++;
wh = mtod(m, struct ieee80211_frame *);
data_len = m->m_pkthdr.len - (hdrlen + ccmp_get_header_len(key));
ccmp_init_blocks(&ctx->cc_aes, wh, ccmp_get_ccm_m(key),
key->wk_keytsc, data_len, b0, aad, b, s0);
i = 1;
pos = mtod(m, uint8_t *) + hdrlen + ccmp_get_header_len(key);
space = m->m_len - (hdrlen + ccmp_get_header_len(key));
for (;;) {
if (space > data_len)
space = data_len;
while (space >= AES_BLOCK_LEN) {
CCMP_ENCRYPT(i, b, b0, pos, e, AES_BLOCK_LEN);
pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
data_len -= AES_BLOCK_LEN;
i++;
}
if (data_len <= 0)
break;
m = m->m_next;
if (m == NULL) {
if (space != 0) {
CCMP_ENCRYPT(i, b, b0, pos, e, space);
}
break;
}
if (space != 0) {
uint8_t *pos_next;
int space_next;
int len, dl, sp;
struct mbuf *n;
n = m;
dl = data_len;
sp = space;
for (;;) {
pos_next = mtod(n, uint8_t *);
len = min(dl, AES_BLOCK_LEN);
space_next = len > sp ? len - sp : 0;
if (n->m_len >= space_next) {
xor_block(b+sp, pos_next, space_next);
break;
}
xor_block(b+sp, pos_next, n->m_len);
sp += n->m_len, dl -= n->m_len;
n = n->m_next;
if (n == NULL)
break;
}
CCMP_ENCRYPT(i, b, b0, pos, e, space);
dl = data_len;
sp = space;
for (;;) {
pos_next = mtod(m, uint8_t *);
len = min(dl, AES_BLOCK_LEN);
space_next = len > sp ? len - sp : 0;
if (m->m_len >= space_next) {
xor_block(pos_next, e+sp, space_next);
break;
}
xor_block(pos_next, e+sp, m->m_len);
sp += m->m_len, dl -= m->m_len;
m = m->m_next;
if (m == NULL)
goto done;
}
data_len -= AES_BLOCK_LEN;
i++;
pos = pos_next + space_next;
space = m->m_len - space_next;
} else {
pos = mtod(m, uint8_t *);
space = m->m_len;
}
}
done:
xor_block(b, s0, ccmp_get_trailer_len(key));
return m_append(m0, ccmp_get_trailer_len(key), b);
}
#undef CCMP_ENCRYPT
#define CCMP_DECRYPT(_i, _b, _b0, _pos, _a, _len) do { \
\
_b0[14] = (_i >> 8) & 0xff; \
_b0[15] = _i & 0xff; \
rijndael_encrypt(&ctx->cc_aes, _b0, _b); \
xor_block(_pos, _b, _len); \
\
xor_block(_a, _pos, _len); \
rijndael_encrypt(&ctx->cc_aes, _a, _a); \
} while (0)
static int
ccmp_decrypt(struct ieee80211_key *key, u_int64_t pn, struct mbuf *m, int hdrlen)
{
const struct ieee80211_rx_stats *rxs;
struct ccmp_ctx *ctx = key->wk_private;
struct ieee80211vap *vap = ctx->cc_vap;
struct ieee80211_frame *wh;
uint8_t aad[2 * AES_BLOCK_LEN];
uint8_t b0[AES_BLOCK_LEN], b[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
uint8_t mic[AES_BLOCK_LEN];
size_t data_len;
int i;
uint8_t *pos;
u_int space;
rxs = ieee80211_get_rx_params_ptr(m);
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_DECRYPTED) != 0)
return (1);
ctx->cc_vap->iv_stats.is_crypto_ccmp++;
wh = mtod(m, struct ieee80211_frame *);
data_len = m->m_pkthdr.len -
(hdrlen + ccmp_get_header_len(key) + ccmp_get_trailer_len(key));
ccmp_init_blocks(&ctx->cc_aes, wh, ccmp_get_ccm_m(key), pn,
data_len, b0, aad, a, b);
m_copydata(m, m->m_pkthdr.len - ccmp_get_trailer_len(key),
ccmp_get_trailer_len(key), mic);
xor_block(mic, b, ccmp_get_trailer_len(key));
i = 1;
pos = mtod(m, uint8_t *) + hdrlen + ccmp_get_header_len(key);
space = m->m_len - (hdrlen + ccmp_get_header_len(key));
for (;;) {
if (space > data_len)
space = data_len;
while (space >= AES_BLOCK_LEN) {
CCMP_DECRYPT(i, b, b0, pos, a, AES_BLOCK_LEN);
pos += AES_BLOCK_LEN, space -= AES_BLOCK_LEN;
data_len -= AES_BLOCK_LEN;
i++;
}
if (data_len <= 0)
break;
m = m->m_next;
if (m == NULL) {
if (space != 0)
CCMP_DECRYPT(i, b, b0, pos, a, space);
break;
}
if (space != 0) {
uint8_t *pos_next;
u_int space_next;
u_int len;
pos_next = mtod(m, uint8_t *);
len = min(data_len, AES_BLOCK_LEN);
space_next = len > space ? len - space : 0;
KASSERT(m->m_len >= space_next,
("not enough data in following buffer, "
"m_len %u need %u\n", m->m_len, space_next));
xor_block(b+space, pos_next, space_next);
CCMP_DECRYPT(i, b, b0, pos, a, space);
xor_block(pos_next, b+space, space_next);
data_len -= len;
i++;
pos = pos_next + space_next;
space = m->m_len - space_next;
} else {
pos = mtod(m, uint8_t *);
space = m->m_len;
}
}
if ((rxs != NULL) && (rxs->c_pktflags & IEEE80211_RX_F_MIC_STRIP) != 0)
return (1);
if (memcmp(mic, a, ccmp_get_trailer_len(key)) != 0) {
IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_CRYPTO, wh->i_addr2,
"%s", "AES-CCM decrypt failed; MIC mismatch");
vap->iv_stats.is_rx_ccmpmic++;
return 0;
}
return 1;
}
#undef CCMP_DECRYPT
IEEE80211_CRYPTO_MODULE(ccmp, 1);
IEEE80211_CRYPTO_MODULE_ADD(ccmp_256);