reduce typecasts and byte swapping
This commit is contained in:
parent
7ee80c6f6e
commit
2706a22ac6
|
@ -1,4 +1,4 @@
|
|||
/* $NetBSD: gmac.c,v 1.1 2011/05/26 21:50:03 drochner Exp $ */
|
||||
/* $NetBSD: gmac.c,v 1.2 2011/06/08 10:14:16 drochner Exp $ */
|
||||
/* OpenBSD: gmac.c,v 1.3 2011/01/11 15:44:23 deraadt Exp */
|
||||
|
||||
/*
|
||||
|
@ -37,23 +37,19 @@ void
|
|||
ghash_gfmul(const uint32_t *X, const uint32_t *Y, uint32_t *product)
|
||||
{
|
||||
uint32_t v[4];
|
||||
uint32_t z[4] = { 0, 0, 0, 0};
|
||||
const uint8_t *x = (const uint8_t *)X;
|
||||
uint32_t mul;
|
||||
int i;
|
||||
|
||||
v[0] = be32toh(Y[0]);
|
||||
v[1] = be32toh(Y[1]);
|
||||
v[2] = be32toh(Y[2]);
|
||||
v[3] = be32toh(Y[3]);
|
||||
memcpy(v, Y, GMAC_BLOCK_LEN);
|
||||
memset(product, 0, GMAC_BLOCK_LEN);
|
||||
|
||||
for (i = 0; i < GMAC_BLOCK_LEN * 8; i++) {
|
||||
/* update Z */
|
||||
if (x[i >> 3] & (1 << (~i & 7))) {
|
||||
z[0] ^= v[0];
|
||||
z[1] ^= v[1];
|
||||
z[2] ^= v[2];
|
||||
z[3] ^= v[3];
|
||||
if (X[i >> 5] & (1 << (~i & 31))) {
|
||||
product[0] ^= v[0];
|
||||
product[1] ^= v[1];
|
||||
product[2] ^= v[2];
|
||||
product[3] ^= v[3];
|
||||
} /* else: we preserve old values */
|
||||
|
||||
/* update V */
|
||||
|
@ -63,28 +59,26 @@ ghash_gfmul(const uint32_t *X, const uint32_t *Y, uint32_t *product)
|
|||
v[1] = (v[0] << 31) | (v[1] >> 1);
|
||||
v[0] = (v[0] >> 1) ^ (0xe1000000 * mul);
|
||||
}
|
||||
|
||||
product[0] = htobe32(z[0]);
|
||||
product[1] = htobe32(z[1]);
|
||||
product[2] = htobe32(z[2]);
|
||||
product[3] = htobe32(z[3]);
|
||||
}
|
||||
|
||||
void
|
||||
ghash_update(GHASH_CTX *ctx, const uint8_t *X, size_t len)
|
||||
{
|
||||
uint8_t *s = (uint8_t *)ctx->S;
|
||||
uint8_t *y = (uint8_t *)ctx->Z;
|
||||
uint32_t x;
|
||||
uint32_t *s = ctx->S;
|
||||
uint32_t *y = ctx->Z;
|
||||
int i, j;
|
||||
|
||||
for (i = 0; i < len / GMAC_BLOCK_LEN; i++) {
|
||||
for (j = 0; j < GMAC_BLOCK_LEN; j++)
|
||||
s[j] = y[j] ^ X[j];
|
||||
for (j = 0; j < GMAC_BLOCK_LEN/4; j++) {
|
||||
x = (X[0] << 24) | (X[1] << 16) | (X[2] << 8) | X[3];
|
||||
s[j] = y[j] ^ x;
|
||||
X += 4;
|
||||
}
|
||||
|
||||
ghash_gfmul(ctx->S, ctx->H, ctx->S);
|
||||
ghash_gfmul(ctx->H, ctx->S, ctx->S);
|
||||
|
||||
y = s;
|
||||
X += GMAC_BLOCK_LEN;
|
||||
}
|
||||
|
||||
memcpy(ctx->Z, ctx->S, GMAC_BLOCK_LEN);
|
||||
|
@ -102,6 +96,8 @@ AES_GMAC_Init(AES_GMAC_CTX *ctx)
|
|||
void
|
||||
AES_GMAC_Setkey(AES_GMAC_CTX *ctx, const uint8_t *key, uint16_t klen)
|
||||
{
|
||||
int i;
|
||||
|
||||
ctx->rounds = rijndaelKeySetupEnc(ctx->K, (const u_char *)key,
|
||||
(klen - AESCTR_NONCESIZE) * 8);
|
||||
/* copy out salt to the counter block */
|
||||
|
@ -109,6 +105,8 @@ AES_GMAC_Setkey(AES_GMAC_CTX *ctx, const uint8_t *key, uint16_t klen)
|
|||
/* prepare a hash subkey */
|
||||
rijndaelEncrypt(ctx->K, ctx->rounds, (void *)ctx->ghash.H,
|
||||
(void *)ctx->ghash.H);
|
||||
for (i = 0; i < 4; i++)
|
||||
ctx->ghash.H[i] = be32toh(ctx->ghash.H[i]);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -121,18 +119,16 @@ AES_GMAC_Reinit(AES_GMAC_CTX *ctx, const uint8_t *iv, uint16_t ivlen)
|
|||
int
|
||||
AES_GMAC_Update(AES_GMAC_CTX *ctx, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint32_t blk[4] = { 0, 0, 0, 0 };
|
||||
uint8_t blk[16] = { 0 };
|
||||
int plen;
|
||||
|
||||
if (len > 0) {
|
||||
plen = len % GMAC_BLOCK_LEN;
|
||||
if (len >= GMAC_BLOCK_LEN)
|
||||
ghash_update(&ctx->ghash, (const uint8_t *)data,
|
||||
len - plen);
|
||||
ghash_update(&ctx->ghash, data, len - plen);
|
||||
if (plen) {
|
||||
memcpy(blk, data + (len - plen), plen);
|
||||
ghash_update(&ctx->ghash, (uint8_t *)blk,
|
||||
GMAC_BLOCK_LEN);
|
||||
ghash_update(&ctx->ghash, blk, GMAC_BLOCK_LEN);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
|
@ -141,13 +137,21 @@ AES_GMAC_Update(AES_GMAC_CTX *ctx, const uint8_t *data, uint16_t len)
|
|||
void
|
||||
AES_GMAC_Final(uint8_t digest[GMAC_DIGEST_LEN], AES_GMAC_CTX *ctx)
|
||||
{
|
||||
uint8_t keystream[GMAC_BLOCK_LEN];
|
||||
uint8_t keystream[GMAC_BLOCK_LEN], *k, *d;
|
||||
int i;
|
||||
|
||||
/* do one round of GCTR */
|
||||
ctx->J[GMAC_BLOCK_LEN - 1] = 1;
|
||||
rijndaelEncrypt(ctx->K, ctx->rounds, ctx->J, keystream);
|
||||
for (i = 0; i < GMAC_DIGEST_LEN; i++)
|
||||
digest[i] = ((uint8_t *)ctx->ghash.S)[i] ^ keystream[i];
|
||||
k = keystream;
|
||||
d = digest;
|
||||
for (i = 0; i < GMAC_DIGEST_LEN/4; i++) {
|
||||
d[0] = (uint8_t)(ctx->ghash.S[i] >> 24) ^ k[0];
|
||||
d[1] = (uint8_t)(ctx->ghash.S[i] >> 16) ^ k[1];
|
||||
d[2] = (uint8_t)(ctx->ghash.S[i] >> 8) ^ k[2];
|
||||
d[3] = (uint8_t)ctx->ghash.S[i] ^ k[3];
|
||||
d += 4;
|
||||
k += 4;
|
||||
}
|
||||
memset(keystream, 0, sizeof(keystream));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue