From: tb Date: Mon, 18 Dec 2023 21:15:00 +0000 (+0000) Subject: cmac: use block_size rather than bl X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=eb570f66cab0de31f31f3fc7d6b9b1b68a374715;p=openbsd cmac: use block_size rather than bl This is purely mechanical apart from a single line wrap. --- diff --git a/lib/libcrypto/cmac/cmac.c b/lib/libcrypto/cmac/cmac.c index 0df40277bc4..9fe907609af 100644 --- a/lib/libcrypto/cmac/cmac.c +++ b/lib/libcrypto/cmac/cmac.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmac.c,v 1.17 2023/12/15 13:45:05 tb Exp $ */ +/* $OpenBSD: cmac.c,v 1.18 2023/12/18 21:15:00 tb Exp $ */ /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL * project. */ @@ -90,21 +90,21 @@ struct CMAC_CTX_st { * and R64 = (1 << 64) | 0x1b for the only supported block sizes 128 and 64. */ static void -make_kn(unsigned char *kn, const unsigned char *l, int bl) +make_kn(unsigned char *kn, const unsigned char *l, int block_size) { unsigned char mask, Rb; int i; /* Choose Rb according to the block size in bytes. */ - Rb = bl == 16 ? 0x87 : 0x1b; + Rb = block_size == 16 ? 0x87 : 0x1b; /* Compute l << 1 up to last byte. */ - for (i = 0; i < bl - 1; i++) + for (i = 0; i < block_size - 1; i++) kn[i] = (l[i] << 1) | (l[i + 1] >> 7); /* Only xor with Rb if the MSB is one. */ mask = 0 - (l[0] >> 7); - kn[bl - 1] = (l[bl - 1] << 1) ^ (Rb & mask); + kn[block_size - 1] = (l[block_size - 1] << 1) ^ (Rb & mask); } CMAC_CTX * @@ -154,17 +154,17 @@ LCRYPTO_ALIAS(CMAC_CTX_free); int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in) { - int bl; + int block_size; if (in->nlast_block == -1) return 0; if (!EVP_CIPHER_CTX_copy(&out->cctx, &in->cctx)) return 0; - bl = EVP_CIPHER_CTX_block_size(&in->cctx); - memcpy(out->k1, in->k1, bl); - memcpy(out->k2, in->k2, bl); - memcpy(out->tbl, in->tbl, bl); - memcpy(out->last_block, in->last_block, bl); + block_size = EVP_CIPHER_CTX_block_size(&in->cctx); + memcpy(out->k1, in->k1, block_size); + memcpy(out->k2, in->k2, block_size); + memcpy(out->tbl, in->tbl, block_size); + memcpy(out->last_block, in->last_block, block_size); out->nlast_block = in->nlast_block; return 1; } @@ -175,7 +175,7 @@ CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, const EVP_CIPHER *cipher, ENGINE *impl) { static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH]; - int bl; + int block_size; /* All zeros means restart */ if (key == NULL && cipher == NULL && keylen == 0) { @@ -208,8 +208,8 @@ CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, return 0; /* make_kn() only supports block sizes of 8 and 16 bytes. */ - bl = EVP_CIPHER_CTX_block_size(&ctx->cctx); - if (bl != 8 && bl != 16) + block_size = EVP_CIPHER_CTX_block_size(&ctx->cctx); + if (block_size != 8 && block_size != 16) return 0; /* @@ -220,13 +220,13 @@ CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen, return 0; if (!EVP_EncryptInit_ex(&ctx->cctx, NULL, NULL, key, zero_iv)) return 0; - if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, bl)) + if (!EVP_Cipher(&ctx->cctx, ctx->tbl, zero_iv, block_size)) return 0; /* Section 6.1, step 2: compute k1 from intermediate secret. */ - make_kn(ctx->k1, ctx->tbl, bl); + make_kn(ctx->k1, ctx->tbl, block_size); /* Section 6.1, step 3: compute k2 from k1. */ - make_kn(ctx->k2, ctx->k1, bl); + make_kn(ctx->k2, ctx->k1, block_size); /* Destroy intermediate secret and reset last block count. */ explicit_bzero(ctx->tbl, sizeof(ctx->tbl)); @@ -245,18 +245,18 @@ int CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen) { const unsigned char *data = in; - size_t bl; + size_t block_size; if (ctx->nlast_block == -1) return 0; if (dlen == 0) return 1; - bl = EVP_CIPHER_CTX_block_size(&ctx->cctx); + block_size = EVP_CIPHER_CTX_block_size(&ctx->cctx); /* Copy into partial block if we need to */ if (ctx->nlast_block > 0) { size_t nleft; - nleft = bl - ctx->nlast_block; + nleft = block_size - ctx->nlast_block; if (dlen < nleft) nleft = dlen; memcpy(ctx->last_block + ctx->nlast_block, data, nleft); @@ -267,15 +267,16 @@ CMAC_Update(CMAC_CTX *ctx, const void *in, size_t dlen) return 1; data += nleft; /* Else not final block so encrypt it */ - if (!EVP_Cipher(&ctx->cctx, ctx->tbl, ctx->last_block, bl)) + if (!EVP_Cipher(&ctx->cctx, ctx->tbl, ctx->last_block, + block_size)) return 0; } /* Encrypt all but one of the complete blocks left */ - while (dlen > bl) { - if (!EVP_Cipher(&ctx->cctx, ctx->tbl, data, bl)) + while (dlen > block_size) { + if (!EVP_Cipher(&ctx->cctx, ctx->tbl, data, block_size)) return 0; - dlen -= bl; - data += bl; + dlen -= block_size; + data += block_size; } /* Copy any data left to last block buffer */ memcpy(ctx->last_block, data, dlen); @@ -287,28 +288,28 @@ LCRYPTO_ALIAS(CMAC_Update); int CMAC_Final(CMAC_CTX *ctx, unsigned char *out, size_t *poutlen) { - int i, bl, lb; + int i, block_size, lb; if (ctx->nlast_block == -1) return 0; - bl = EVP_CIPHER_CTX_block_size(&ctx->cctx); - *poutlen = (size_t)bl; + block_size = EVP_CIPHER_CTX_block_size(&ctx->cctx); + *poutlen = (size_t)block_size; if (!out) return 1; lb = ctx->nlast_block; /* Is last block complete? */ - if (lb == bl) { - for (i = 0; i < bl; i++) + if (lb == block_size) { + for (i = 0; i < block_size; i++) out[i] = ctx->last_block[i] ^ ctx->k1[i]; } else { ctx->last_block[lb] = 0x80; - if (bl - lb > 1) - memset(ctx->last_block + lb + 1, 0, bl - lb - 1); - for (i = 0; i < bl; i++) + if (block_size - lb > 1) + memset(ctx->last_block + lb + 1, 0, block_size - lb - 1); + for (i = 0; i < block_size; i++) out[i] = ctx->last_block[i] ^ ctx->k2[i]; } - if (!EVP_Cipher(&ctx->cctx, out, out, bl)) { - explicit_bzero(out, bl); + if (!EVP_Cipher(&ctx->cctx, out, out, block_size)) { + explicit_bzero(out, block_size); return 0; } return 1;