Example #1
0
int
aes_ctr_setkey(u_int8_t **sched, u_int8_t *key, int len)
{
	struct aes_ctr_ctx *ctx;

	if (len < AESCTR_NONCESIZE)
		return -1;

	*sched = kmalloc(sizeof(struct aes_ctr_ctx), M_CRYPTO_DATA,
	M_WAITOK | M_ZERO);
	ctx = (struct aes_ctr_ctx *)*sched;
	ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (u_char *)key,
	(len - AESCTR_NONCESIZE) * 8);
	if (ctx->ac_nr == 0) {
		aes_ctr_zerokey(sched);
		return -1;
	}
	bcopy(key + len - AESCTR_NONCESIZE, ctx->ac_block, AESCTR_NONCESIZE);
	return 0;
}
Example #2
0
int
aes_ctr_setkey(u_int8_t **sched, const u_int8_t *key, int len)
{
	struct aes_ctr_ctx *ctx;

	if (len < AESCTR_NONCESIZE)
		return EINVAL;

	ctx = malloc(sizeof(struct aes_ctr_ctx), M_CRYPTO_DATA,
		     M_NOWAIT|M_ZERO);
	if (!ctx)
		return ENOMEM;
	ctx->ac_nr = rijndaelKeySetupEnc(ctx->ac_ek, (const u_char *)key,
			(len - AESCTR_NONCESIZE) * 8);
	if (!ctx->ac_nr) { /* wrong key len */
		aes_ctr_zerokey((u_int8_t **)&ctx);
		return EINVAL;
	}
	memcpy(ctx->ac_block, key + len - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
	/* random start value for simple counter */
	cprng_fast(&ctx->ivgenctx.lastiv, sizeof(ctx->ivgenctx.lastiv));
	*sched = (void *)ctx;
	return 0;
}