Changeset View
Changeset View
Standalone View
Standalone View
sys/crypto/via/padlock_hash.c
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx)); | CTASSERT(sizeof(struct padlock_sha_ctx) <= sizeof(union authctx)); | ||||
static void padlock_sha_init(void *vctx); | static void padlock_sha_init(void *vctx); | ||||
static int padlock_sha_update(void *vctx, const void *buf, u_int bufsize); | static int padlock_sha_update(void *vctx, const void *buf, u_int bufsize); | ||||
static void padlock_sha1_final(uint8_t *hash, void *vctx); | static void padlock_sha1_final(uint8_t *hash, void *vctx); | ||||
static void padlock_sha256_final(uint8_t *hash, void *vctx); | static void padlock_sha256_final(uint8_t *hash, void *vctx); | ||||
static struct auth_hash padlock_hmac_sha1 = { | static const struct auth_hash padlock_hmac_sha1 = { | ||||
.type = CRYPTO_SHA1_HMAC, | .type = CRYPTO_SHA1_HMAC, | ||||
.name = "HMAC-SHA1", | .name = "HMAC-SHA1", | ||||
.keysize = SHA1_BLOCK_LEN, | .keysize = SHA1_BLOCK_LEN, | ||||
.hashsize = SHA1_HASH_LEN, | .hashsize = SHA1_HASH_LEN, | ||||
.ctxsize = sizeof(struct padlock_sha_ctx), | .ctxsize = sizeof(struct padlock_sha_ctx), | ||||
.blocksize = SHA1_BLOCK_LEN, | .blocksize = SHA1_BLOCK_LEN, | ||||
.Init = padlock_sha_init, | .Init = padlock_sha_init, | ||||
.Update = padlock_sha_update, | .Update = padlock_sha_update, | ||||
.Final = padlock_sha1_final, | .Final = padlock_sha1_final, | ||||
}; | }; | ||||
static struct auth_hash padlock_hmac_sha256 = { | static const struct auth_hash padlock_hmac_sha256 = { | ||||
.type = CRYPTO_SHA2_256_HMAC, | .type = CRYPTO_SHA2_256_HMAC, | ||||
.name = "HMAC-SHA2-256", | .name = "HMAC-SHA2-256", | ||||
.keysize = SHA2_256_BLOCK_LEN, | .keysize = SHA2_256_BLOCK_LEN, | ||||
.hashsize = SHA2_256_HASH_LEN, | .hashsize = SHA2_256_HASH_LEN, | ||||
.ctxsize = sizeof(struct padlock_sha_ctx), | .ctxsize = sizeof(struct padlock_sha_ctx), | ||||
.blocksize = SHA2_256_BLOCK_LEN, | .blocksize = SHA2_256_BLOCK_LEN, | ||||
.Init = padlock_sha_init, | .Init = padlock_sha_init, | ||||
.Update = padlock_sha_update, | .Update = padlock_sha_update, | ||||
▲ Show 20 Lines • Show All 120 Lines • ▼ Show 20 Lines | padlock_sha256_final(uint8_t *hash, void *vctx) | ||||
struct padlock_sha_ctx *ctx; | struct padlock_sha_ctx *ctx; | ||||
ctx = vctx; | ctx = vctx; | ||||
padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset); | padlock_do_sha256(ctx->psc_buf, hash, ctx->psc_offset); | ||||
padlock_sha_free(ctx); | padlock_sha_free(ctx); | ||||
} | } | ||||
static void | static void | ||||
padlock_copy_ctx(struct auth_hash *axf, void *sctx, void *dctx) | padlock_copy_ctx(const struct auth_hash *axf, void *sctx, void *dctx) | ||||
{ | { | ||||
if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && | if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && | ||||
(axf->type == CRYPTO_SHA1_HMAC || | (axf->type == CRYPTO_SHA1_HMAC || | ||||
axf->type == CRYPTO_SHA2_256_HMAC)) { | axf->type == CRYPTO_SHA2_256_HMAC)) { | ||||
struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx; | struct padlock_sha_ctx *spctx = sctx, *dpctx = dctx; | ||||
dpctx->psc_offset = spctx->psc_offset; | dpctx->psc_offset = spctx->psc_offset; | ||||
dpctx->psc_size = spctx->psc_size; | dpctx->psc_size = spctx->psc_size; | ||||
dpctx->psc_buf = malloc(dpctx->psc_size, M_PADLOCK, M_WAITOK); | dpctx->psc_buf = malloc(dpctx->psc_size, M_PADLOCK, M_WAITOK); | ||||
bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size); | bcopy(spctx->psc_buf, dpctx->psc_buf, dpctx->psc_size); | ||||
} else { | } else { | ||||
bcopy(sctx, dctx, axf->ctxsize); | bcopy(sctx, dctx, axf->ctxsize); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
padlock_free_ctx(struct auth_hash *axf, void *ctx) | padlock_free_ctx(const struct auth_hash *axf, void *ctx) | ||||
{ | { | ||||
if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && | if ((via_feature_xcrypt & VIA_HAS_SHA) != 0 && | ||||
(axf->type == CRYPTO_SHA1_HMAC || | (axf->type == CRYPTO_SHA1_HMAC || | ||||
axf->type == CRYPTO_SHA2_256_HMAC)) { | axf->type == CRYPTO_SHA2_256_HMAC)) { | ||||
padlock_sha_free(ctx); | padlock_sha_free(ctx); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
padlock_hash_key_setup(struct padlock_session *ses, const uint8_t *key, | padlock_hash_key_setup(struct padlock_session *ses, const uint8_t *key, | ||||
int klen) | int klen) | ||||
{ | { | ||||
struct auth_hash *axf; | const struct auth_hash *axf; | ||||
axf = ses->ses_axf; | axf = ses->ses_axf; | ||||
/* | /* | ||||
* Try to free contexts before using them, because | * Try to free contexts before using them, because | ||||
* padlock_hash_key_setup() can be called twice - once from | * padlock_hash_key_setup() can be called twice - once from | ||||
* padlock_newsession() and again from padlock_process(). | * padlock_newsession() and again from padlock_process(). | ||||
*/ | */ | ||||
padlock_free_ctx(axf, ses->ses_ictx); | padlock_free_ctx(axf, ses->ses_ictx); | ||||
padlock_free_ctx(axf, ses->ses_octx); | padlock_free_ctx(axf, ses->ses_octx); | ||||
hmac_init_ipad(axf, key, klen, ses->ses_ictx); | hmac_init_ipad(axf, key, klen, ses->ses_ictx); | ||||
hmac_init_opad(axf, key, klen, ses->ses_octx); | hmac_init_opad(axf, key, klen, ses->ses_octx); | ||||
} | } | ||||
/* | /* | ||||
* Compute keyed-hash authenticator. | * Compute keyed-hash authenticator. | ||||
*/ | */ | ||||
static int | static int | ||||
padlock_authcompute(struct padlock_session *ses, struct cryptop *crp) | padlock_authcompute(struct padlock_session *ses, struct cryptop *crp) | ||||
{ | { | ||||
u_char hash[HASH_MAX_LEN], hash2[HASH_MAX_LEN]; | u_char hash[HASH_MAX_LEN], hash2[HASH_MAX_LEN]; | ||||
struct auth_hash *axf; | const struct auth_hash *axf; | ||||
union authctx ctx; | union authctx ctx; | ||||
int error; | int error; | ||||
axf = ses->ses_axf; | axf = ses->ses_axf; | ||||
padlock_copy_ctx(axf, ses->ses_ictx, &ctx); | padlock_copy_ctx(axf, ses->ses_ictx, &ctx); | ||||
error = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length, | error = crypto_apply(crp, crp->crp_aad_start, crp->crp_aad_length, | ||||
axf->Update, &ctx); | axf->Update, &ctx); | ||||
Show All 20 Lines | if (timingsafe_bcmp(hash, hash2, ses->ses_mlen) != 0) | ||||
return (EBADMSG); | return (EBADMSG); | ||||
} else | } else | ||||
crypto_copyback(crp, crp->crp_digest_start, ses->ses_mlen, | crypto_copyback(crp, crp->crp_digest_start, ses->ses_mlen, | ||||
hash); | hash); | ||||
return (0); | return (0); | ||||
} | } | ||||
/* Find software structure which describes HMAC algorithm. */ | /* Find software structure which describes HMAC algorithm. */ | ||||
static struct auth_hash * | static const struct auth_hash * | ||||
padlock_hash_lookup(int alg) | padlock_hash_lookup(int alg) | ||||
{ | { | ||||
struct auth_hash *axf; | const struct auth_hash *axf; | ||||
switch (alg) { | switch (alg) { | ||||
case CRYPTO_NULL_HMAC: | case CRYPTO_NULL_HMAC: | ||||
axf = &auth_hash_null; | axf = &auth_hash_null; | ||||
break; | break; | ||||
case CRYPTO_SHA1_HMAC: | case CRYPTO_SHA1_HMAC: | ||||
if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) | if ((via_feature_xcrypt & VIA_HAS_SHA) != 0) | ||||
axf = &padlock_hmac_sha1; | axf = &padlock_hmac_sha1; | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |