Skip to content

Commit

Permalink
ltc: make prng_descriptor a pointer to descriptors
Browse files Browse the repository at this point in the history
Saves 1184 bytes by making prng_descriptor an array of pointers to
descriptors 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 commit 7892cb1 ("ltc: make prng_descriptor a pointer to descriptors")]
[jf: apply change to additional source files]
[jf: squash commit c2f5808 ("ltc: bugfix find_prng()")]
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
  • Loading branch information
jenswi-linaro committed Apr 24, 2024
1 parent ae75124 commit 82feb7a
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/headers/tomcrypt_prng.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct {
} prng_state;

/** PRNG descriptor */
extern struct ltc_prng_descriptor {
extern const struct ltc_prng_descriptor {
/** Name of the PRNG */
const char *name;
/** size in bytes of exported state */
Expand Down Expand Up @@ -124,7 +124,7 @@ extern struct ltc_prng_descriptor {
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
*/
int (*test)(void);
} prng_descriptor[];
} *prng_descriptor[];

#ifdef LTC_YARROW
int yarrow_start(prng_state *prng);
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/math/rand_bn.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng)
if ((buf = XCALLOC(1, bytes)) == NULL) return CRYPT_MEM;

/* generate random bytes */
if (prng_descriptor[wprng].read(buf, bytes, prng) != (unsigned long)bytes) {
if (prng_descriptor[wprng]->read(buf, bytes, prng) != (unsigned long)bytes) {
res = CRYPT_ERROR_READPRNG;
goto cleanup;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/math/rand_prime.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int rand_prime(void *N, long len, prng_state *prng, int wprng)

do {
/* generate value */
if (prng_descriptor[wprng].read(buf, len, prng) != (unsigned long)len) {
if (prng_descriptor[wprng]->read(buf, len, prng) != (unsigned long)len) {
XFREE(buf);
return CRYPT_ERROR_READPRNG;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_find_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int find_prng(const char *name)
LTC_ARGCHK(name != NULL);
LTC_MUTEX_LOCK(&ltc_prng_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if ((prng_descriptor[x].name != NULL) && XSTRCMP(prng_descriptor[x].name, name) == 0) {
if ((prng_descriptor[x] != NULL) && XSTRCMP(prng_descriptor[x]->name, name) == 0) {
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
return x;
}
Expand Down
4 changes: 1 addition & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_prng_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
@file crypt_prng_descriptor.c
Stores the PRNG descriptors, Tom St Denis
*/
struct ltc_prng_descriptor prng_descriptor[TAB_SIZE] = {
{ NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
const struct ltc_prng_descriptor *prng_descriptor[TAB_SIZE];

LTC_MUTEX_GLOBAL(ltc_prng_mutex)

2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/misc/crypt/crypt_prng_is_valid.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
int prng_is_valid(int idx)
{
LTC_MUTEX_LOCK(&ltc_prng_mutex);
if (idx < 0 || idx >= TAB_SIZE || prng_descriptor[idx].name == NULL) {
if (idx < 0 || idx >= TAB_SIZE || prng_descriptor[idx] == NULL) {
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
return CRYPT_INVALID_PRNG;
}
Expand Down
6 changes: 3 additions & 3 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_register_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ int register_prng(const struct ltc_prng_descriptor *prng)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_prng_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) {
if (prng_descriptor[x] == prng) {
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
return x;
}
}

/* find a blank spot */
for (x = 0; x < TAB_SIZE; x++) {
if (prng_descriptor[x].name == NULL) {
XMEMCPY(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor));
if (prng_descriptor[x] == NULL) {
prng_descriptor[x] = prng;
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
return x;
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/misc/crypt/crypt_unregister_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ int unregister_prng(const struct ltc_prng_descriptor *prng)
/* is it already registered? */
LTC_MUTEX_LOCK(&ltc_prng_mutex);
for (x = 0; x < TAB_SIZE; x++) {
if (XMEMCMP(&prng_descriptor[x], prng, sizeof(struct ltc_prng_descriptor)) == 0) {
prng_descriptor[x].name = NULL;
if (prng_descriptor[x] == prng) {
prng_descriptor[x] = NULL;
LTC_MUTEX_UNLOCK(&ltc_prng_mutex);
return CRYPT_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/dh/dh_generate_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ int dh_generate_key(prng_state *prng, int wprng, dh_key *key)
key->type = PK_PRIVATE;
do {
/* make up random buf */
if (prng_descriptor[wprng].read(buf, keysize, prng) != keysize) {
if (prng_descriptor[wprng]->read(buf, keysize, prng) != keysize) {
err = CRYPT_ERROR_READPRNG;
goto freebuf;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/dsa/dsa_generate_pqg.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ static int s_dsa_make_params(prng_state *prng, int wprng, int group_size, int mo
for(found_p=0; !found_p;) {
/* q */
for(found_q=0; !found_q;) {
if (prng_descriptor[wprng].read(sbuf, seedbytes, prng) != seedbytes) { err = CRYPT_ERROR_READPRNG; goto cleanup; }
if (prng_descriptor[wprng]->read(sbuf, seedbytes, prng) != seedbytes) { err = CRYPT_ERROR_READPRNG; goto cleanup; }
i = outbytes;
if ((err = hash_memory(hash, sbuf, seedbytes, digest, &i)) != CRYPT_OK) { goto cleanup; }
if ((err = mp_read_unsigned_bin(U, digest, outbytes)) != CRYPT_OK) { goto cleanup; }
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/ec25519/tweetnacl.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ int tweetnacl_crypto_sign_keypair(prng_state *prng, int wprng, u8 *pk, u8 *sk)
return err;
}

if (prng_descriptor[wprng].read(sk,32, prng) != 32) {
if (prng_descriptor[wprng]->read(sk,32, prng) != 32) {
return CRYPT_ERROR_READPRNG;
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/pkcs1/pkcs_1_oaep_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ int pkcs_1_oaep_encode(const unsigned char *msg, unsigned long msglen,
}

/* now choose a random seed */
if (prng_descriptor[prng_idx].read(seed, hLen, prng) != hLen) {
if (prng_descriptor[prng_idx]->read(seed, hLen, prng) != hLen) {
err = CRYPT_ERROR_READPRNG;
goto LBL_ERR;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/pkcs1/pkcs_1_pss_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int pkcs_1_pss_encode(const unsigned char *msghash, unsigned long msghashlen,

/* generate random salt */
if (saltlen > 0) {
if (prng_descriptor[prng_idx].read(salt, saltlen, prng) != saltlen) {
if (prng_descriptor[prng_idx]->read(salt, saltlen, prng) != saltlen) {
err = CRYPT_ERROR_READPRNG;
goto LBL_ERR;
}
Expand Down
4 changes: 2 additions & 2 deletions core/lib/libtomcrypt/src/pk/pkcs1/pkcs_1_v1_5_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ int pkcs_1_v1_5_encode(const unsigned char *msg,

if (block_type == LTC_PKCS_1_EME) {
/* now choose a random ps */
if (prng_descriptor[prng_idx].read(ps, ps_len, prng) != ps_len) {
if (prng_descriptor[prng_idx]->read(ps, ps_len, prng) != ps_len) {
result = CRYPT_ERROR_READPRNG;
goto bail;
}

/* transform zero bytes (if any) to non-zero random bytes */
for (i = 0; i < ps_len; i++) {
while (ps[i] == 0) {
if (prng_descriptor[prng_idx].read(&ps[i], 1, prng) != 1) {
if (prng_descriptor[prng_idx]->read(&ps[i], 1, prng) != 1) {
result = CRYPT_ERROR_READPRNG;
goto bail;
}
Expand Down
2 changes: 1 addition & 1 deletion core/lib/libtomcrypt/src/pk/x25519/x25519_make_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int x25519_make_key(prng_state *prng, int wprng, curve25519_key *key)
return err;
}

if (prng_descriptor[wprng].read(key->priv, sizeof(key->priv), prng) != sizeof(key->priv)) {
if (prng_descriptor[wprng]->read(key->priv, sizeof(key->priv), prng) != sizeof(key->priv)) {
return CRYPT_ERROR_READPRNG;
}

Expand Down
10 changes: 5 additions & 5 deletions core/lib/libtomcrypt/src/prngs/rng_make_prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ int rng_make_prng(int bits, int wprng, prng_state *prng,
}

if (bits == -1) {
bytes = prng_descriptor[wprng].export_size;
bytes = prng_descriptor[wprng]->export_size;
} else if (bits < 64 || bits > 1024) {
return CRYPT_INVALID_PRNGSIZE;
} else {
bytes = (unsigned long)((bits+7)/8) * 2;
}

if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) {
if ((err = prng_descriptor[wprng]->start(prng)) != CRYPT_OK) {
return err;
}

Expand All @@ -58,15 +58,15 @@ int rng_make_prng(int bits, int wprng, prng_state *prng,
}

if (bits == -1) {
if ((err = prng_descriptor[wprng].pimport(buf, bytes, prng)) != CRYPT_OK) {
if ((err = prng_descriptor[wprng]->pimport(buf, bytes, prng)) != CRYPT_OK) {
goto LBL_ERR;
}
} else {
if ((err = prng_descriptor[wprng].add_entropy(buf, bytes, prng)) != CRYPT_OK) {
if ((err = prng_descriptor[wprng]->add_entropy(buf, bytes, prng)) != CRYPT_OK) {
goto LBL_ERR;
}
}
if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) {
if ((err = prng_descriptor[wprng]->ready(prng)) != CRYPT_OK) {
goto LBL_ERR;
}

Expand Down

0 comments on commit 82feb7a

Please sign in to comment.