Skip to content

Commit

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

Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Tested-by: Jerome Forissier <jerome.forissier@linaro.org> (HiKey)
Tested-by: Jens Wiklander <jens.wiklander@linaro.org> (QEMU)
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
  • Loading branch information
jenswi-linaro committed Feb 10, 2016
1 parent 3015f56 commit f17691b
Show file tree
Hide file tree
Showing 38 changed files with 100 additions and 104 deletions.
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/include/tomcrypt_cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ typedef struct {


/** cipher descriptor table, last entry has "name == NULL" to mark the end of table */
extern struct ltc_cipher_descriptor {
extern const struct ltc_cipher_descriptor {
/** name of cipher */
const char *name;
/** internal ID */
Expand Down Expand Up @@ -437,7 +437,7 @@ extern struct ltc_cipher_descriptor {
int (*accel_xts_decrypt)(const unsigned char *ct, unsigned char *pt,
unsigned long blocks, unsigned char *tweak, symmetric_key *skey1,
symmetric_key *skey2);
} cipher_descriptor[];
} *cipher_descriptor[];


/* make aes an alias */
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/encauth/ccm/ccm_add_aad.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int ccm_add_aad(ccm_state *ccm,
for (y = 0; y < adatalen; y++) {
if (ccm->x == 16) {
/* full block so let's encrypt it */
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return CRYPT_ERROR;
}
ccm->x = 0;
Expand All @@ -76,7 +76,7 @@ int ccm_add_aad(ccm_state *ccm,
/* remainder? */
if (ccm->aadlen == ccm->current_aadlen) {
if (ccm->x != 0) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return CRYPT_ERROR;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/encauth/ccm/ccm_add_nonce.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int ccm_add_nonce(ccm_state *ccm,
}

/* encrypt PAD */
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}

Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/encauth/ccm/ccm_done.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int ccm_done(ccm_state *ccm,
LTC_ARGCHK(taglen != NULL);

if (ccm->x != 0) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
}
Expand All @@ -73,11 +73,11 @@ int ccm_done(ccm_state *ccm,
for (y = 15; y > 15 - ccm->L; y--) {
ccm->ctr[y] = 0x00;
}
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
return err;
}

cipher_descriptor[ccm->cipher].done(&ccm->K);
cipher_descriptor[ccm->cipher]->done(&ccm->K);

/* store the TAG */
for (x = 0; x < 16 && x < *taglen; x++) {
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/encauth/ccm/ccm_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ int ccm_init(ccm_state *ccm, int cipher,
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
if (cipher_descriptor[cipher].block_length != 16) {
if (cipher_descriptor[cipher]->block_length != 16) {
return CRYPT_INVALID_CIPHER;
}

Expand All @@ -84,7 +84,7 @@ int ccm_init(ccm_state *ccm, int cipher,
}

/* schedule key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->cipher = cipher;
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/encauth/ccm/ccm_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int ccm_process(ccm_state *ccm,
ccm->ctr[z] = (ccm->ctr[z] + 1) & 255;
if (ccm->ctr[z]) break;
}
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->ctr, ccm->CTRPAD, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->CTRlen = 0;
Expand All @@ -99,7 +99,7 @@ int ccm_process(ccm_state *ccm,
}

if (ccm->x == 16) {
if ((err = cipher_descriptor[ccm->cipher].ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[ccm->cipher]->ecb_encrypt(ccm->PAD, ccm->PAD, &ccm->K)) != CRYPT_OK) {
return err;
}
ccm->x = 0;
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/encauth/gcm/gcm_done.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ int gcm_done(gcm_state *gcm,
gcm_mult_h(gcm, gcm->X);

/* encrypt original counter */
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[gcm->cipher]->ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
for (x = 0; x < 16 && x < *taglen; x++) {
tag[x] = gcm->buf[x] ^ gcm->X[x];
}
*taglen = x;

cipher_descriptor[gcm->cipher].done(&gcm->K);
cipher_descriptor[gcm->cipher]->done(&gcm->K);

return CRYPT_OK;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/encauth/gcm/gcm_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ int gcm_init(gcm_state *gcm, int cipher,
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
if (cipher_descriptor[cipher].block_length != 16) {
if (cipher_descriptor[cipher]->block_length != 16) {
return CRYPT_INVALID_CIPHER;
}

/* schedule key */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, &gcm->K)) != CRYPT_OK) {
return err;
}

/* H = E(0) */
zeromem(B, 16);
if ((err = cipher_descriptor[cipher].ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(B, gcm->H, &gcm->K)) != CRYPT_OK) {
return err;
}

Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/encauth/gcm/gcm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ int gcm_memory( int cipher,
return err;
}

if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
if (cipher_descriptor[cipher]->accel_gcm_memory != NULL) {
return
cipher_descriptor[cipher].accel_gcm_memory
cipher_descriptor[cipher]->accel_gcm_memory
(key, keylen,
IV, IVlen,
adata, adatalen,
Expand Down
8 changes: 4 additions & 4 deletions core/lib/libtomcrypt/src/encauth/gcm/gcm_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int gcm_process(gcm_state *gcm,
if (++gcm->Y[y] & 255) { break; }
}
/* encrypt the counter */
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[gcm->cipher]->ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[gcm->cipher]->ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
}
Expand All @@ -136,7 +136,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[gcm->cipher]->ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
}
Expand All @@ -154,7 +154,7 @@ int gcm_process(gcm_state *gcm,
for (y = 15; y >= 12; y--) {
if (++gcm->Y[y] & 255) { break; }
}
if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
if ((err = cipher_descriptor[gcm->cipher]->ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {
return err;
}
gcm->buflen = 0;
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/mac/omac/omac_done.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ int omac_done(omac_state *omac, unsigned char *out, unsigned long *outlen)
}

/* encrypt it */
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[omac->cipher_idx]->ecb_encrypt(omac->block, omac->block, &omac->key)) != CRYPT_OK) {
return err;
}
cipher_descriptor[omac->cipher_idx].done(&omac->key);
cipher_descriptor[omac->cipher_idx]->done(&omac->key);

/* output it */
for (x = 0; x < (unsigned)omac->blklen && x < *outlen; x++) {
Expand Down
10 changes: 5 additions & 5 deletions core/lib/libtomcrypt/src/mac/omac/omac_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
}

#ifdef LTC_FAST
if (cipher_descriptor[cipher].block_length % sizeof(LTC_FAST_TYPE)) {
if (cipher_descriptor[cipher]->block_length % sizeof(LTC_FAST_TYPE)) {
return CRYPT_INVALID_ARG;
}
#endif

/* now setup the system */
switch (cipher_descriptor[cipher].block_length) {
switch (cipher_descriptor[cipher]->block_length) {
case 8: mask = 0x1B;
len = 8;
break;
Expand All @@ -82,15 +82,15 @@ int omac_init(omac_state *omac, int cipher, const unsigned char *key, unsigned l
default: return CRYPT_INVALID_ARG;
}

if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, &omac->key)) != CRYPT_OK) {
return err;
}

/* ok now we need Lu and Lu^2 [calc one from the other] */

/* first calc L which is Ek(0) */
zeromem(omac->Lu[0], cipher_descriptor[cipher].block_length);
if ((err = cipher_descriptor[cipher].ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {
zeromem(omac->Lu[0], cipher_descriptor[cipher]->block_length);
if ((err = cipher_descriptor[cipher]->ecb_encrypt(omac->Lu[0], omac->Lu[0], &omac->key)) != CRYPT_OK) {
return err;
}

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

/* Use accelerator if found */
if (cipher_descriptor[cipher].omac_memory != NULL) {
return cipher_descriptor[cipher].omac_memory(key, keylen, in, inlen, out, outlen);
if (cipher_descriptor[cipher]->omac_memory != NULL) {
return cipher_descriptor[cipher]->omac_memory(key, keylen, in, inlen, out, outlen);
}

/* allocate ram for omac state */
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/mac/omac/omac_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)
}

#ifdef LTC_FAST
unsigned long blklen = cipher_descriptor[omac->cipher_idx].block_length;
unsigned long blklen = cipher_descriptor[omac->cipher_idx]->block_length;
if (omac->buflen == 0 && inlen > blklen) {
unsigned long y;
for (x = 0; x < (inlen - blklen); x += blklen) {
for (y = 0; y < blklen; y += sizeof(LTC_FAST_TYPE)) {
*((LTC_FAST_TYPE*)(&omac->prev[y])) ^= *((LTC_FAST_TYPE*)(&in[y]));
}
in += blklen;
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[omac->cipher_idx]->ecb_encrypt(omac->prev, omac->prev, &omac->key)) != CRYPT_OK) {
return err;
}
}
Expand All @@ -91,7 +91,7 @@ int omac_process(omac_state *omac, const unsigned char *in, unsigned long inlen)
for (x = 0; x < (unsigned long)omac->blklen; x++) {
omac->block[x] ^= omac->prev[x];
}
if ((err = cipher_descriptor[omac->cipher_idx].ecb_encrypt(omac->block, omac->prev, &omac->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[omac->cipher_idx]->ecb_encrypt(omac->block, omac->prev, &omac->key)) != CRYPT_OK) {
return err;
}
omac->buflen = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
Stores the cipher descriptor table, Tom St Denis
*/

struct ltc_cipher_descriptor cipher_descriptor[TAB_SIZE] = {
{ NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
const struct ltc_cipher_descriptor *cipher_descriptor[TAB_SIZE];

LTC_MUTEX_GLOBAL(ltc_cipher_mutex)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
int cipher_is_valid(int idx)
{
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
if (idx < 0 || idx >= TAB_SIZE || cipher_descriptor[idx].name == NULL) {
if (idx < 0 || idx >= TAB_SIZE || cipher_descriptor[idx] == NULL) {
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return CRYPT_INVALID_CIPHER;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_find_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int find_cipher(const char *name)
LTC_ARGCHK(name != NULL);
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].name != NULL && !XSTRCMP(cipher_descriptor[x].name, name)) {
if (cipher_descriptor[x] != NULL && !XSTRCMP(cipher_descriptor[x]->name, name)) {
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return x;
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_find_cipher_any.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ int find_cipher_any(const char *name, int blocklen, int keylen)

LTC_MUTEX_LOCK(&ltc_cipher_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].name == NULL) {
if (cipher_descriptor[x] == NULL) {
continue;
}
if (blocklen <= (int)cipher_descriptor[x].block_length && keylen <= (int)cipher_descriptor[x].max_key_length) {
if (blocklen <= (int)cipher_descriptor[x]->block_length && keylen <= (int)cipher_descriptor[x]->max_key_length) {
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return x;
}
Expand Down
5 changes: 2 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_find_cipher_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ int find_cipher_id(unsigned char ID)
{
int x;
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].ID == ID) {
x = (cipher_descriptor[x].name == NULL) ? -1 : x;
for (x = 0; x < TAB_SIZE && cipher_descriptor[x] != NULL; x++) {
if (cipher_descriptor[x]->ID == ID) {
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return x;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_register_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ int register_cipher(const struct ltc_cipher_descriptor *cipher)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].name != NULL && cipher_descriptor[x].ID == cipher->ID) {
if (cipher_descriptor[x] != NULL && cipher_descriptor[x]->ID == cipher->ID) {
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return x;
}
}

/* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) {
if (cipher_descriptor[x].name == NULL) {
XMEMCPY(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor));
if (cipher_descriptor[x] == NULL) {
cipher_descriptor[x] = cipher;
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return x;
}
Expand Down
5 changes: 2 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_unregister_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ int unregister_cipher(const struct ltc_cipher_descriptor *cipher)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_cipher_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (XMEMCMP(&cipher_descriptor[x], cipher, sizeof(struct ltc_cipher_descriptor)) == 0) {
cipher_descriptor[x].name = NULL;
cipher_descriptor[x].ID = 255;
if (cipher_descriptor[x] == cipher) {
cipher_descriptor[x] = NULL;
LTC_MUTEX_UNLOCK(&ltc_cipher_mutex);
return CRYPT_OK;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/modes/cbc/cbc_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ int cbc_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, s
}
#endif

if (cipher_descriptor[cbc->cipher].accel_cbc_decrypt != NULL) {
return cipher_descriptor[cbc->cipher].accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
if (cipher_descriptor[cbc->cipher]->accel_cbc_decrypt != NULL) {
return cipher_descriptor[cbc->cipher]->accel_cbc_decrypt(ct, pt, len / cbc->blocklen, cbc->IV, &cbc->key);
} else {
while (len) {
/* decrypt */
if ((err = cipher_descriptor[cbc->cipher].ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[cbc->cipher]->ecb_decrypt(ct, tmp, &cbc->key)) != CRYPT_OK) {
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/modes/cbc/cbc_done.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int cbc_done(symmetric_CBC *cbc)
if ((err = cipher_is_valid(cbc->cipher)) != CRYPT_OK) {
return err;
}
cipher_descriptor[cbc->cipher].done(&cbc->key);
cipher_descriptor[cbc->cipher]->done(&cbc->key);
return CRYPT_OK;
}

Expand Down

0 comments on commit f17691b

Please sign in to comment.