Changeset View
Changeset View
Standalone View
Standalone View
sys/opencrypto/xform_aes_icm.c
Show First 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
#include <opencrypto/xform_enc.h> | #include <opencrypto/xform_enc.h> | ||||
static int aes_icm_setkey(u_int8_t **, u_int8_t *, int); | static int aes_icm_setkey(u_int8_t **, u_int8_t *, int); | ||||
static void aes_icm_crypt(caddr_t, u_int8_t *); | static void aes_icm_crypt(caddr_t, u_int8_t *); | ||||
static void aes_icm_zerokey(u_int8_t **); | static void aes_icm_zerokey(u_int8_t **); | ||||
static void aes_icm_reinit(caddr_t, u_int8_t *); | static void aes_icm_reinit(caddr_t, u_int8_t *); | ||||
static void aes_gcm_reinit(caddr_t, u_int8_t *); | static void aes_gcm_reinit(caddr_t, u_int8_t *); | ||||
static void aes_ccm_reinit(caddr_t, u_int8_t *); | |||||
/* Encryption instances */ | /* Encryption instances */ | ||||
struct enc_xform enc_xform_aes_icm = { | struct enc_xform enc_xform_aes_icm = { | ||||
CRYPTO_AES_ICM, "AES-ICM", | CRYPTO_AES_ICM, "AES-ICM", | ||||
AES_BLOCK_LEN, AES_BLOCK_LEN, AES_MIN_KEY, AES_MAX_KEY, | AES_BLOCK_LEN, AES_BLOCK_LEN, AES_MIN_KEY, AES_MAX_KEY, | ||||
aes_icm_crypt, | aes_icm_crypt, | ||||
aes_icm_crypt, | aes_icm_crypt, | ||||
aes_icm_setkey, | aes_icm_setkey, | ||||
aes_icm_zerokey, | aes_icm_zerokey, | ||||
aes_icm_reinit, | aes_icm_reinit, | ||||
}; | }; | ||||
struct enc_xform enc_xform_aes_nist_gcm = { | struct enc_xform enc_xform_aes_nist_gcm = { | ||||
CRYPTO_AES_NIST_GCM_16, "AES-GCM", | CRYPTO_AES_NIST_GCM_16, "AES-GCM", | ||||
AES_ICM_BLOCK_LEN, AES_GCM_IV_LEN, AES_MIN_KEY, AES_MAX_KEY, | AES_ICM_BLOCK_LEN, AES_GCM_IV_LEN, AES_MIN_KEY, AES_MAX_KEY, | ||||
aes_icm_crypt, | aes_icm_crypt, | ||||
aes_icm_crypt, | aes_icm_crypt, | ||||
aes_icm_setkey, | aes_icm_setkey, | ||||
aes_icm_zerokey, | aes_icm_zerokey, | ||||
aes_gcm_reinit, | aes_gcm_reinit, | ||||
}; | }; | ||||
struct enc_xform enc_xform_ccm = { | |||||
CRYPTO_AES_CCM_16, "AES-CCM", | |||||
AES_ICM_BLOCK_LEN, AES_GCM_IV_LEN, AES_MIN_KEY, AES_MAX_KEY, | |||||
aes_icm_crypt, | |||||
aes_icm_crypt, | |||||
aes_icm_setkey, | |||||
aes_icm_zerokey, | |||||
aes_ccm_reinit, | |||||
}; | |||||
/* | /* | ||||
* Encryption wrapper routines. | * Encryption wrapper routines. | ||||
*/ | */ | ||||
static void | static void | ||||
aes_icm_reinit(caddr_t key, u_int8_t *iv) | aes_icm_reinit(caddr_t key, u_int8_t *iv) | ||||
{ | { | ||||
struct aes_icm_ctx *ctx; | struct aes_icm_ctx *ctx; | ||||
ctx = (struct aes_icm_ctx *)key; | ctx = (struct aes_icm_ctx *)key; | ||||
bcopy(iv, ctx->ac_block, AESICM_BLOCKSIZE); | bcopy(iv, ctx->ac_block, AESICM_BLOCKSIZE); | ||||
} | } | ||||
static void | static void | ||||
aes_gcm_reinit(caddr_t key, u_int8_t *iv) | aes_gcm_reinit(caddr_t key, u_int8_t *iv) | ||||
{ | { | ||||
struct aes_icm_ctx *ctx; | struct aes_icm_ctx *ctx; | ||||
aes_icm_reinit(key, iv); | aes_icm_reinit(key, iv); | ||||
ctx = (struct aes_icm_ctx *)key; | ctx = (struct aes_icm_ctx *)key; | ||||
/* GCM starts with 2 as counter 1 is used for final xor of tag. */ | /* GCM starts with 2 as counter 1 is used for final xor of tag. */ | ||||
bzero(&ctx->ac_block[AESICM_BLOCKSIZE - 4], 4); | bzero(&ctx->ac_block[AESICM_BLOCKSIZE - 4], 4); | ||||
ctx->ac_block[AESICM_BLOCKSIZE - 1] = 2; | ctx->ac_block[AESICM_BLOCKSIZE - 1] = 2; | ||||
} | |||||
static void | |||||
aes_ccm_reinit(caddr_t key, u_int8_t *iv) | |||||
{ | |||||
struct aes_icm_ctx *ctx; | |||||
ctx = (struct aes_icm_ctx*)key; | |||||
/* CCM has flags, then the IV, then the counter, which starts at 1 */ | |||||
explicit_bzero(ctx->ac_block, sizeof(ctx->ac_block)); | |||||
/* 3 bytes for length field; this gives a nonce of 12 bytes */ | |||||
ctx->ac_block[0] = (15 - AES_GCM_IV_LEN) - 1; | |||||
bcopy(iv, ctx->ac_block+1, AES_GCM_IV_LEN); | |||||
ctx->ac_block[AESICM_BLOCKSIZE - 1] = 1; | |||||
} | } | ||||
static void | static void | ||||
aes_icm_crypt(caddr_t key, u_int8_t *data) | aes_icm_crypt(caddr_t key, u_int8_t *data) | ||||
{ | { | ||||
struct aes_icm_ctx *ctx; | struct aes_icm_ctx *ctx; | ||||
u_int8_t keystream[AESICM_BLOCKSIZE]; | u_int8_t keystream[AESICM_BLOCKSIZE]; | ||||
int i; | int i; | ||||
ctx = (struct aes_icm_ctx *)key; | ctx = (struct aes_icm_ctx *)key; | ||||
rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream); | rijndaelEncrypt(ctx->ac_ek, ctx->ac_nr, ctx->ac_block, keystream); | ||||
mav: Again tool long line. | |||||
for (i = 0; i < AESICM_BLOCKSIZE; i++) | for (i = 0; i < AESICM_BLOCKSIZE; i++) | ||||
data[i] ^= keystream[i]; | data[i] ^= keystream[i]; | ||||
explicit_bzero(keystream, sizeof(keystream)); | explicit_bzero(keystream, sizeof(keystream)); | ||||
/* increment counter */ | /* increment counter */ | ||||
for (i = AESICM_BLOCKSIZE - 1; | for (i = AESICM_BLOCKSIZE - 1; | ||||
i >= 0; i--) | i >= 0; i--) | ||||
if (++ctx->ac_block[i]) /* continue on overflow */ | if (++ctx->ac_block[i]) /* continue on overflow */ | ||||
Show All 29 Lines |
Again tool long line.