Skip to content

Commit 7aee873

Browse files
committed
cmac.c: optimize make_kn and move zero_iv to const segment.
Backport openssl/openssl@03cf7e7 to fix the following k1 stringop-overflow: In function 'make_kn', inlined from 'make_kn' at crypto/cmac/cmac.c:81:13, inlined from 'CMAC_Init' at crypto/cmac/cmac.c:205:9: crypto/cmac/cmac.c:92:20: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=] 92 | k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b; | ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~ crypto/cmac/cmac.c: In function 'CMAC_Init': crypto/cmac/cmac.c:69:19: note: at offset [-2147483649, -1] into destination object 'k1' of size 32 69 | unsigned char k1[EVP_MAX_BLOCK_LENGTH]; | ^~ Fixes: - http://autobuild.buildroot.org/results/97b6333cdc7bad24aba7af1b04890679e0058299 Signed-off-by: Fabrice Fontaine <[email protected]>
1 parent 126a07e commit 7aee873

File tree

1 file changed

+8
-9
lines changed
  • Cryptlib/OpenSSL/crypto/cmac

1 file changed

+8
-9
lines changed

Cryptlib/OpenSSL/crypto/cmac/cmac.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,17 @@ struct CMAC_CTX_st {
7878

7979
/* Make temporary keys K1 and K2 */
8080

81-
static void make_kn(unsigned char *k1, unsigned char *l, int bl)
81+
static void make_kn(unsigned char *k1, const unsigned char *l, int bl)
8282
{
8383
int i;
84+
unsigned char c = l[0], carry = c>>7, cnext;
85+
8486
/* Shift block to left, including carry */
85-
for (i = 0; i < bl; i++) {
86-
k1[i] = l[i] << 1;
87-
if (i < bl - 1 && l[i + 1] & 0x80)
88-
k1[i] |= 1;
89-
}
87+
for (i = 0; i < bl-1; i++, c = cnext)
88+
k1[i] = (c << 1) | ((cnext=l[i+1]) >> 7);
89+
9090
/* If MSB set fixup with R */
91-
if (l[0] & 0x80)
92-
k1[bl - 1] ^= bl == 16 ? 0x87 : 0x1b;
91+
k1[i] = (c << 1) ^ ((0-carry)&(bl==16?0x87:0x1b));
9392
}
9493

9594
CMAC_CTX *CMAC_CTX_new(void)
@@ -151,7 +150,7 @@ int CMAC_CTX_copy(CMAC_CTX *out, const CMAC_CTX *in)
151150
int CMAC_Init(CMAC_CTX *ctx, const void *key, size_t keylen,
152151
const EVP_CIPHER *cipher, ENGINE *impl)
153152
{
154-
static unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH];
153+
static const unsigned char zero_iv[EVP_MAX_BLOCK_LENGTH] = {0};
155154
#ifdef OPENSSL_FIPS
156155
if (FIPS_mode()) {
157156
/* If we have an ENGINE need to allow non FIPS */

0 commit comments

Comments
 (0)