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>
[jf: pick f17691b, apply changes to additional files]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
jenswi-linaro authored and jforissier committed Sep 12, 2019
1 parent cbf6e51 commit 271db0f
Show file tree
Hide file tree
Showing 84 changed files with 249 additions and 253 deletions.
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 @@ -35,7 +35,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 err;
}
ccm->x = 0;
Expand All @@ -46,7 +46,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 err;
}
}
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 @@ -66,7 +66,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 @@ -34,7 +34,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 @@ -43,11 +43,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 @@ -36,7 +36,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 @@ -47,7 +47,7 @@ int ccm_init(ccm_state *ccm, int cipher,
ccm->taglen = taglen;

/* 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
32 changes: 16 additions & 16 deletions core/lib/libtomcrypt/src/encauth/ccm/ccm_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int ccm_memory(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 @@ -86,8 +86,8 @@ int ccm_memory(int cipher,
}

/* is there an accelerator? */
if (cipher_descriptor[cipher].accel_ccm_memory != NULL) {
return cipher_descriptor[cipher].accel_ccm_memory(
if (cipher_descriptor[cipher]->accel_ccm_memory != NULL) {
return cipher_descriptor[cipher]->accel_ccm_memory(
key, keylen,
uskey,
nonce, noncelen,
Expand Down Expand Up @@ -123,7 +123,7 @@ int ccm_memory(int cipher,
}

/* initialize the cipher */
if ((err = cipher_descriptor[cipher].setup(key, keylen, 0, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->setup(key, keylen, 0, skey)) != CRYPT_OK) {
XFREE(skey);
return err;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ int ccm_memory(int cipher,
}

/* encrypt PAD */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}

Expand All @@ -194,7 +194,7 @@ int ccm_memory(int cipher,
for (y = 0; y < headerlen; y++) {
if (x == 16) {
/* full block so let's encrypt it */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
x = 0;
Expand All @@ -203,7 +203,7 @@ int ccm_memory(int cipher,
}

/* remainder */
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}

Expand All @@ -247,7 +247,7 @@ int ccm_memory(int cipher,
*(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z]));
*(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z]));
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
Expand All @@ -258,7 +258,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}

Expand All @@ -267,7 +267,7 @@ int ccm_memory(int cipher,
*(LTC_FAST_TYPE_PTR_CAST(&pt[y+z])) = *(LTC_FAST_TYPE_PTR_CAST(&ct[y+z])) ^ *(LTC_FAST_TYPE_PTR_CAST(&CTRPAD[z]));
*(LTC_FAST_TYPE_PTR_CAST(&PAD[z])) ^= *(LTC_FAST_TYPE_PTR_CAST(&pt[y+z]));
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
Expand All @@ -282,7 +282,7 @@ int ccm_memory(int cipher,
ctr[z] = (ctr[z] + 1) & 255;
if (ctr[z]) break;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}
CTRlen = 0;
Expand All @@ -298,7 +298,7 @@ int ccm_memory(int cipher,
}

if (x == 16) {
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
x = 0;
Expand All @@ -307,7 +307,7 @@ int ccm_memory(int cipher,
}

if (x != 0) {
if ((err = cipher_descriptor[cipher].ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(PAD, PAD, skey)) != CRYPT_OK) {
goto error;
}
}
Expand All @@ -317,12 +317,12 @@ int ccm_memory(int cipher,
for (y = 15; y > 15 - L; y--) {
ctr[y] = 0x00;
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ctr, CTRPAD, skey)) != CRYPT_OK) {
goto error;
}

if (skey != uskey) {
cipher_descriptor[cipher].done(skey);
cipher_descriptor[cipher]->done(skey);
#ifdef LTC_CLEAN_STACK
zeromem(skey, sizeof(*skey));
#endif
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 @@ -53,7 +53,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 @@ -69,7 +69,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/ccm/ccm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int ccm_test(void)
for (y = 0; y < 2; y++) {
taglen = tests[x].taglen;
if (y == 0) {
if ((err = cipher_descriptor[idx].setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
if ((err = cipher_descriptor[idx]->setup(tests[x].key, 16, 0, &skey)) != CRYPT_OK) {
return err;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ int ccm_test(void)
}

if (y == 0) {
cipher_descriptor[idx].done(&skey);
cipher_descriptor[idx]->done(&skey);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/encauth/eax/eax_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int eax_init(eax_state *eax, int cipher,
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
blklen = cipher_descriptor[cipher].block_length;
blklen = cipher_descriptor[cipher]->block_length;

/* allocate ram */
buf = XMALLOC(MAXBLOCKSIZE);
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 @@ -69,15 +69,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 @@ -45,18 +45,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 @@ -49,8 +49,8 @@ int gcm_memory( int cipher,
return err;
}

if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
return cipher_descriptor[cipher].accel_gcm_memory
if (cipher_descriptor[cipher]->accel_gcm_memory != NULL) {
return 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 @@ -70,7 +70,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 @@ -99,7 +99,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 @@ -117,7 +117,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 @@ -135,7 +135,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
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/encauth/ocb/ocb_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
if ((err = cipher_is_valid(ocb->cipher)) != CRYPT_OK) {
return err;
}
LTC_ARGCHK(cipher_descriptor[ocb->cipher].ecb_decrypt != NULL);
LTC_ARGCHK(cipher_descriptor[ocb->cipher]->ecb_decrypt != NULL);

/* check length */
if (ocb->block_len != cipher_descriptor[ocb->cipher].block_length) {
if (ocb->block_len != cipher_descriptor[ocb->cipher]->block_length) {
return CRYPT_INVALID_ARG;
}

Expand All @@ -49,7 +49,7 @@ int ocb_decrypt(ocb_state *ocb, const unsigned char *ct, unsigned char *pt)
for (x = 0; x < ocb->block_len; x++) {
tmp[x] = ct[x] ^ Z[x];
}
if ((err = cipher_descriptor[ocb->cipher].ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[ocb->cipher]->ecb_decrypt(tmp, pt, &ocb->key)) != CRYPT_OK) {
return err;
}
for (x = 0; x < ocb->block_len; x++) {
Expand Down
8 changes: 4 additions & 4 deletions core/lib/libtomcrypt/src/encauth/ocb/ocb_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int ocb_init(ocb_state *ocb, int cipher,
}

/* determine which polys to use */
ocb->block_len = cipher_descriptor[cipher].block_length;
ocb->block_len = cipher_descriptor[cipher]->block_length;
x = (int)(sizeof(polys)/sizeof(polys[0]));
for (poly = 0; poly < x; poly++) {
if (polys[poly].len == ocb->block_len) {
Expand All @@ -72,21 +72,21 @@ int ocb_init(ocb_state *ocb, int cipher,
}

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

/* find L = E[0] */
zeromem(ocb->L, ocb->block_len);
if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ocb->L, ocb->L, &ocb->key)) != CRYPT_OK) {
return err;
}

/* find R = E[N xor L] */
for (x = 0; x < ocb->block_len; x++) {
ocb->R[x] = ocb->L[x] ^ nonce[x];
}
if ((err = cipher_descriptor[cipher].ecb_encrypt(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) {
if ((err = cipher_descriptor[cipher]->ecb_encrypt(ocb->R, ocb->R, &ocb->key)) != CRYPT_OK) {
return err;
}

Expand Down

0 comments on commit 271db0f

Please sign in to comment.