Skip to content

Commit

Permalink
ltc: make hash_descriptor a pointer to descriptors
Browse files Browse the repository at this point in the history
Saves 3288 bytes by making hash_descriptor an array of pointers to
descriptor instead of an array of descriptors.

Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
[jf: pick 3015f56, apply change to additional source files]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
jenswi-linaro authored and jforissier committed Sep 23, 2022
1 parent 741c8e8 commit 5c148a1
Show file tree
Hide file tree
Showing 33 changed files with 103 additions and 106 deletions.
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/hashes/chc/chc.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ int chc_register(int cipher)
}

/* store into descriptor */
hash_descriptor[idx].hashsize =
hash_descriptor[idx].blocksize = cipher_descriptor[cipher].block_length;
hash_descriptor[idx]->hashsize =
hash_descriptor[idx]->blocksize = cipher_descriptor[cipher].block_length;

/* store the idx and block size */
cipher_idx = cipher;
Expand Down
12 changes: 6 additions & 6 deletions core/lib/libtomcrypt/src/hashes/helper/hash_filehandle.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ int hash_filehandle(int hash, FILE *in, unsigned char *out, unsigned long *outle
goto LBL_ERR;
}

if (*outlen < hash_descriptor[hash].hashsize) {
*outlen = hash_descriptor[hash].hashsize;
if (*outlen < hash_descriptor[hash]->hashsize) {
*outlen = hash_descriptor[hash]->hashsize;
err = CRYPT_BUFFER_OVERFLOW;
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].init(&md)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->init(&md)) != CRYPT_OK) {
goto LBL_ERR;
}

do {
x = fread(buf, 1, LTC_FILE_READ_BUFSIZE, in);
if ((err = hash_descriptor[hash].process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(&md, buf, (unsigned long)x)) != CRYPT_OK) {
goto LBL_CLEANBUF;
}
} while (x == LTC_FILE_READ_BUFSIZE);
if ((err = hash_descriptor[hash].done(&md, out)) == CRYPT_OK) {
*outlen = hash_descriptor[hash].hashsize;
if ((err = hash_descriptor[hash]->done(&md, out)) == CRYPT_OK) {
*outlen = hash_descriptor[hash]->hashsize;
}

LBL_CLEANBUF:
Expand Down
12 changes: 6 additions & 6 deletions core/lib/libtomcrypt/src/hashes/helper/hash_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned
return err;
}

if (*outlen < hash_descriptor[hash].hashsize) {
*outlen = hash_descriptor[hash].hashsize;
if (*outlen < hash_descriptor[hash]->hashsize) {
*outlen = hash_descriptor[hash]->hashsize;
return CRYPT_BUFFER_OVERFLOW;
}

Expand All @@ -40,14 +40,14 @@ int hash_memory(int hash, const unsigned char *in, unsigned long inlen, unsigned
return CRYPT_MEM;
}

if ((err = hash_descriptor[hash].init(md)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->init(md)) != CRYPT_OK) {
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].process(md, in, inlen)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(md, in, inlen)) != CRYPT_OK) {
goto LBL_ERR;
}
err = hash_descriptor[hash].done(md, out);
*outlen = hash_descriptor[hash].hashsize;
err = hash_descriptor[hash]->done(md, out);
*outlen = hash_descriptor[hash]->hashsize;
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(md, sizeof(hash_state));
Expand Down
12 changes: 6 additions & 6 deletions core/lib/libtomcrypt/src/hashes/helper/hash_memory_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
return err;
}

if (*outlen < hash_descriptor[hash].hashsize) {
*outlen = hash_descriptor[hash].hashsize;
if (*outlen < hash_descriptor[hash]->hashsize) {
*outlen = hash_descriptor[hash]->hashsize;
return CRYPT_BUFFER_OVERFLOW;
}

Expand All @@ -46,7 +46,7 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
return CRYPT_MEM;
}

if ((err = hash_descriptor[hash].init(md)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->init(md)) != CRYPT_OK) {
goto LBL_ERR;
}

Expand All @@ -55,7 +55,7 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
curlen = inlen;
for (;;) {
/* process buf */
if ((err = hash_descriptor[hash].process(md, curptr, curlen)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(md, curptr, curlen)) != CRYPT_OK) {
goto LBL_ERR;
}
/* step to next */
Expand All @@ -65,8 +65,8 @@ int hash_memory_multi(int hash, unsigned char *out, unsigned long *outlen,
}
curlen = va_arg(args, unsigned long);
}
err = hash_descriptor[hash].done(md, out);
*outlen = hash_descriptor[hash].hashsize;
err = hash_descriptor[hash]->done(md, out);
*outlen = hash_descriptor[hash]->hashsize;
LBL_ERR:
#ifdef LTC_CLEAN_STACK
zeromem(md, sizeof(hash_state));
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/headers/tomcrypt_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ typedef union Hash_state {
} hash_state;

/** hash descriptor */
extern struct ltc_hash_descriptor {
extern const struct ltc_hash_descriptor {
/** name of hash */
const char *name;
/** internal ID */
Expand Down Expand Up @@ -238,7 +238,7 @@ extern struct ltc_hash_descriptor {
const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen);

} hash_descriptor[];
} *hash_descriptor[];

#ifdef LTC_CHC_HASH
int chc_register(int cipher);
Expand Down
14 changes: 7 additions & 7 deletions core/lib/libtomcrypt/src/mac/hmac/hmac_done.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifdef LTC_HMAC

#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash]->blocksize

/**
Terminate an HMAC session
Expand All @@ -34,7 +34,7 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
}

/* get the hash message digest size */
hashsize = hash_descriptor[hash].hashsize;
hashsize = hash_descriptor[hash]->hashsize;

/* allocate buffers */
buf = XMALLOC(LTC_HMAC_BLOCKSIZE);
Expand All @@ -50,7 +50,7 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
}

/* Get the hash of the first HMAC vector plus the data */
if ((err = hash_descriptor[hash].done(&hmac->md, isha)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->done(&hmac->md, isha)) != CRYPT_OK) {
goto LBL_ERR;
}

Expand All @@ -60,16 +60,16 @@ int hmac_done(hmac_state *hmac, unsigned char *out, unsigned long *outlen)
}

/* Now calculate the "outer" hash for step (5), (6), and (7) */
if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->init(&hmac->md)) != CRYPT_OK) {
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(&hmac->md, isha, hashsize)) != CRYPT_OK) {
goto LBL_ERR;
}
if ((err = hash_descriptor[hash].done(&hmac->md, buf)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->done(&hmac->md, buf)) != CRYPT_OK) {
goto LBL_ERR;
}

Expand Down
8 changes: 4 additions & 4 deletions core/lib/libtomcrypt/src/mac/hmac/hmac_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#ifdef LTC_HMAC

#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash].blocksize
#define LTC_HMAC_BLOCKSIZE hash_descriptor[hash]->blocksize

/**
Initialize an HMAC context.
Expand All @@ -34,7 +34,7 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
return err;
}
hmac->hash = hash;
hashsize = hash_descriptor[hash].hashsize;
hashsize = hash_descriptor[hash]->hashsize;

/* valid key length? */
if (keylen == 0) {
Expand Down Expand Up @@ -74,11 +74,11 @@ int hmac_init(hmac_state *hmac, int hash, const unsigned char *key, unsigned lon
}

/* Pre-pend that to the hash data */
if ((err = hash_descriptor[hash].init(&hmac->md)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->init(&hmac->md)) != CRYPT_OK) {
goto LBL_ERR;
}

if ((err = hash_descriptor[hash].process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
if ((err = hash_descriptor[hash]->process(&hmac->md, buf, LTC_HMAC_BLOCKSIZE)) != CRYPT_OK) {
goto LBL_ERR;
}

Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/mac/hmac/hmac_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ int hmac_memory(int hash,
}

/* is there a descriptor? */
if (hash_descriptor[hash].hmac_block != NULL) {
return hash_descriptor[hash].hmac_block(key, keylen, in, inlen, out, outlen);
if (hash_descriptor[hash]->hmac_block != NULL) {
return hash_descriptor[hash]->hmac_block(key, keylen, in, inlen, out, outlen);
}

/* nope, so call the hmac functions */
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/mac/hmac/hmac_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int hmac_process(hmac_state *hmac, const unsigned char *in, unsigned long inlen)
if ((err = hash_is_valid(hmac->hash)) != CRYPT_OK) {
return err;
}
return hash_descriptor[hmac->hash].process(&hmac->md, in, inlen);
return hash_descriptor[hmac->hash]->process(&hmac->md, in, inlen);
}

#endif
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/mac/hmac/hmac_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ int hmac_test(void)
return err;
}

if(compare_testvector(digest, outlen, cases[i].digest, (size_t)hash_descriptor[hash].hashsize, cases[i].num, i)) {
if(compare_testvector(digest, outlen, cases[i].digest, (size_t)hash_descriptor[hash]->hashsize, cases[i].num, i)) {
failed++;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_find_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int find_hash(const char *name)
LTC_ARGCHK(name != NULL);
LTC_MUTEX_LOCK(&ltc_hash_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].name != NULL && XSTRCMP(hash_descriptor[x].name, name) == 0) {
if (hash_descriptor[x] != NULL && XSTRCMP(hash_descriptor[x]->name, name) == 0) {
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return x;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_find_hash_any.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
y = MAXBLOCKSIZE+1;
z = -1;
for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].name == NULL) {
if (hash_descriptor[x] == NULL) {
continue;
}
if ((int)hash_descriptor[x].hashsize >= digestlen && (int)hash_descriptor[x].hashsize < y) {
if ((int)hash_descriptor[x]->hashsize >= digestlen && (int)hash_descriptor[x]->hashsize < y) {
z = x;
y = hash_descriptor[x].hashsize;
y = hash_descriptor[x]->hashsize;
}
}
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
Expand Down
3 changes: 1 addition & 2 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_find_hash_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ int find_hash_id(unsigned char ID)
int x;
LTC_MUTEX_LOCK(&ltc_hash_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].ID == ID) {
x = (hash_descriptor[x].name == NULL) ? -1 : x;
if (hash_descriptor[x] && hash_descriptor[x]->ID == ID) {
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return x;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_find_hash_oid.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ int find_hash_oid(const unsigned long *ID, unsigned long IDlen)
LTC_ARGCHK(ID != NULL);
LTC_MUTEX_LOCK(&ltc_hash_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].name != NULL && hash_descriptor[x].OIDlen == IDlen && !XMEMCMP(hash_descriptor[x].OID, ID, sizeof(unsigned long) * IDlen)) {
if (hash_descriptor[x] != NULL && hash_descriptor[x]->OIDlen == IDlen && !XMEMCMP(hash_descriptor[x]->OID, ID, sizeof(unsigned long) * IDlen)) {
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return x;
}
Expand Down
4 changes: 1 addition & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_hash_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
Stores the hash descriptor table, Tom St Denis
*/

struct ltc_hash_descriptor hash_descriptor[TAB_SIZE] = {
{ NULL, 0, 0, 0, { 0 }, 0, NULL, NULL, NULL, NULL, NULL }
};
const struct ltc_hash_descriptor *hash_descriptor[TAB_SIZE];

LTC_MUTEX_GLOBAL(ltc_hash_mutex)

2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
int hash_is_valid(int idx)
{
LTC_MUTEX_LOCK(&ltc_hash_mutex);
if (idx < 0 || idx >= TAB_SIZE || hash_descriptor[idx].name == NULL) {
if (idx < 0 || idx >= TAB_SIZE || hash_descriptor[idx] == NULL) {
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return CRYPT_INVALID_HASH;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_register_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ int register_hash(const struct ltc_hash_descriptor *hash)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_hash_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) {
if (hash_descriptor[x] == hash) {
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return x;
}
}

/* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) {
if (hash_descriptor[x].name == NULL) {
XMEMCPY(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor));
if (hash_descriptor[x] == NULL) {
hash_descriptor[x] = hash;
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return x;
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_unregister_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int unregister_hash(const struct ltc_hash_descriptor *hash)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_hash_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (XMEMCMP(&hash_descriptor[x], hash, sizeof(struct ltc_hash_descriptor)) == 0) {
hash_descriptor[x].name = NULL;
if (hash_descriptor[x] == hash) {
hash_descriptor[x] = NULL;
LTC_MUTEX_UNLOCK(&ltc_hash_mutex);
return CRYPT_OK;
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/misc/hkdf/hkdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen,
return err;
}

hashsize = hash_descriptor[hash_idx].hashsize;
hashsize = hash_descriptor[hash_idx]->hashsize;

/* RFC5869 parameter restrictions */
if (inlen < hashsize || outlen > hashsize * 255) {
Expand Down Expand Up @@ -113,7 +113,7 @@ int hkdf(int hash_idx, const unsigned char *salt, unsigned long saltlen,
return err;
}

hashsize = hash_descriptor[hash_idx].hashsize;
hashsize = hash_descriptor[hash_idx]->hashsize;

extracted = XMALLOC(hashsize); /* replace with static buffer? */
if (extracted == NULL) {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/misc/pkcs12/pkcs12_kdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ int pkcs12_kdf( int hash_id,
unsigned int iterations, unsigned char purpose,
unsigned char *out, unsigned long outlen)
{
unsigned long u = hash_descriptor[hash_id].hashsize;
unsigned long v = hash_descriptor[hash_id].blocksize;
unsigned long u = hash_descriptor[hash_id]->hashsize;
unsigned long v = hash_descriptor[hash_id]->blocksize;
unsigned long c = (outlen + u - 1) / u;
unsigned long Slen = ((saltlen + v - 1) / v) * v;
unsigned long Plen = ((pwlen + v - 1) / v) * v;
Expand Down

0 comments on commit 5c148a1

Please sign in to comment.