Changeset View
Changeset View
Standalone View
Standalone View
sys/opencrypto/xform_cml.c
Show First 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | |||||
struct camellia_cbc_ctx { | struct camellia_cbc_ctx { | ||||
camellia_ctx state; | camellia_ctx state; | ||||
char iv[CAMELLIA_BLOCK_LEN]; | char iv[CAMELLIA_BLOCK_LEN]; | ||||
}; | }; | ||||
static int cml_setkey(void *, const uint8_t *, int); | static int cml_setkey(void *, const uint8_t *, int); | ||||
static void cml_encrypt(void *, const uint8_t *, uint8_t *); | static void cml_encrypt(void *, const uint8_t *, uint8_t *); | ||||
static void cml_decrypt(void *, const uint8_t *, uint8_t *); | static void cml_decrypt(void *, const uint8_t *, uint8_t *); | ||||
static void cml_encrypt_multi(void *, const uint8_t *, uint8_t *, size_t); | |||||
static void cml_decrypt_multi(void *, const uint8_t *, uint8_t *, size_t); | |||||
static void cml_reinit(void *, const uint8_t *, size_t); | static void cml_reinit(void *, const uint8_t *, size_t); | ||||
/* Encryption instances */ | /* Encryption instances */ | ||||
const struct enc_xform enc_xform_camellia = { | const struct enc_xform enc_xform_camellia = { | ||||
.type = CRYPTO_CAMELLIA_CBC, | .type = CRYPTO_CAMELLIA_CBC, | ||||
.name = "Camellia-CBC", | .name = "Camellia-CBC", | ||||
.ctxsize = sizeof(struct camellia_cbc_ctx), | .ctxsize = sizeof(struct camellia_cbc_ctx), | ||||
.blocksize = CAMELLIA_BLOCK_LEN, | .blocksize = CAMELLIA_BLOCK_LEN, | ||||
.ivsize = CAMELLIA_BLOCK_LEN, | .ivsize = CAMELLIA_BLOCK_LEN, | ||||
.minkey = CAMELLIA_MIN_KEY, | .minkey = CAMELLIA_MIN_KEY, | ||||
.maxkey = CAMELLIA_MAX_KEY, | .maxkey = CAMELLIA_MAX_KEY, | ||||
.encrypt = cml_encrypt, | |||||
.decrypt = cml_decrypt, | |||||
.setkey = cml_setkey, | .setkey = cml_setkey, | ||||
.reinit = cml_reinit, | .reinit = cml_reinit, | ||||
.encrypt = cml_encrypt, | |||||
.decrypt = cml_decrypt, | |||||
.encrypt_multi = cml_encrypt_multi, | |||||
.decrypt_multi = cml_decrypt_multi, | |||||
}; | }; | ||||
/* | /* | ||||
* Encryption wrapper routines. | * Encryption wrapper routines. | ||||
*/ | */ | ||||
static void | static void | ||||
cml_encrypt(void *vctx, const uint8_t *in, uint8_t *out) | cml_encrypt(void *vctx, const uint8_t *in, uint8_t *out) | ||||
{ | { | ||||
Show All 11 Lines | cml_decrypt(void *vctx, const uint8_t *in, uint8_t *out) | ||||
struct camellia_cbc_ctx *ctx = vctx; | struct camellia_cbc_ctx *ctx = vctx; | ||||
char block[CAMELLIA_BLOCK_LEN]; | char block[CAMELLIA_BLOCK_LEN]; | ||||
memcpy(block, in, CAMELLIA_BLOCK_LEN); | memcpy(block, in, CAMELLIA_BLOCK_LEN); | ||||
camellia_decrypt(&ctx->state, in, out); | camellia_decrypt(&ctx->state, in, out); | ||||
for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) | for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) | ||||
out[i] ^= ctx->iv[i]; | out[i] ^= ctx->iv[i]; | ||||
memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN); | memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN); | ||||
explicit_bzero(block, sizeof(block)); | |||||
} | |||||
static void | |||||
cml_encrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len) | |||||
{ | |||||
struct camellia_cbc_ctx *ctx = vctx; | |||||
KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length", | |||||
__func__)); | |||||
while (len > 0) { | |||||
for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) | |||||
out[i] = in[i] ^ ctx->iv[i]; | |||||
camellia_encrypt(&ctx->state, out, out); | |||||
memcpy(ctx->iv, out, CAMELLIA_BLOCK_LEN); | |||||
out += CAMELLIA_BLOCK_LEN; | |||||
in += CAMELLIA_BLOCK_LEN; | |||||
len -= CAMELLIA_BLOCK_LEN; | |||||
} | |||||
} | |||||
static void | |||||
cml_decrypt_multi(void *vctx, const uint8_t *in, uint8_t *out, size_t len) | |||||
{ | |||||
struct camellia_cbc_ctx *ctx = vctx; | |||||
char block[CAMELLIA_BLOCK_LEN]; | |||||
KASSERT(len % CAMELLIA_BLOCK_LEN == 0, ("%s: invalid length", | |||||
__func__)); | |||||
while (len > 0) { | |||||
memcpy(block, in, CAMELLIA_BLOCK_LEN); | |||||
camellia_decrypt(&ctx->state, in, out); | |||||
for (u_int i = 0; i < CAMELLIA_BLOCK_LEN; i++) | |||||
out[i] ^= ctx->iv[i]; | |||||
memcpy(ctx->iv, block, CAMELLIA_BLOCK_LEN); | |||||
out += CAMELLIA_BLOCK_LEN; | |||||
in += CAMELLIA_BLOCK_LEN; | |||||
len -= CAMELLIA_BLOCK_LEN; | |||||
} | |||||
explicit_bzero(block, sizeof(block)); | explicit_bzero(block, sizeof(block)); | ||||
} | } | ||||
static int | static int | ||||
cml_setkey(void *vctx, const uint8_t *key, int len) | cml_setkey(void *vctx, const uint8_t *key, int len) | ||||
{ | { | ||||
struct camellia_cbc_ctx *ctx = vctx; | struct camellia_cbc_ctx *ctx = vctx; | ||||
Show All 15 Lines |