create separate no_prng instances

now they shouldn't collide anymore and can always be unregistered
This commit is contained in:
Steffen Jaeckel 2017-06-09 15:04:46 +02:00
parent 42a82ce3ed
commit 3b4e5df858
6 changed files with 70 additions and 40 deletions

View File

@ -17,9 +17,14 @@
#ifdef LTC_PKCS_1
static unsigned char no_prng_entropy[1024];
static unsigned long no_prng_len = 0;
static unsigned long no_prng_offset = 0;
typedef struct
{
struct ltc_prng_descriptor desc;
char name[64];
unsigned char entropy[1024];
unsigned long len;
unsigned long offset;
} no_prng_desc_t;
/**
Start the PRNG
@ -28,11 +33,13 @@ static unsigned long no_prng_offset = 0;
*/
int no_prng_start(prng_state *prng)
{
LTC_UNUSED_PARAM(prng);
no_prng_len = 0;
no_prng_offset = 0;
no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
LTC_ARGCHK(no_prng != NULL);
LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
no_prng->len = 0;
no_prng->offset = 0;
return CRYPT_OK;
return CRYPT_OK;
}
/**
@ -44,15 +51,17 @@ int no_prng_start(prng_state *prng)
*/
int no_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
{
LTC_UNUSED_PARAM(prng);
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(inlen <= sizeof(no_prng_entropy));
no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
LTC_ARGCHK(no_prng != NULL);
LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(inlen <= sizeof(no_prng->entropy));
no_prng_len = MIN(inlen, sizeof(no_prng_entropy));
memcpy(no_prng_entropy, in, no_prng_len);
no_prng_offset = 0;
no_prng->len = MIN(inlen, sizeof(no_prng->entropy));
memcpy(no_prng->entropy, in, no_prng->len);
no_prng->offset = 0;
return CRYPT_OK;
return CRYPT_OK;
}
@ -77,12 +86,14 @@ int no_prng_ready(prng_state *prng)
*/
unsigned long no_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
{
LTC_UNUSED_PARAM(prng);
no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
LTC_ARGCHK(no_prng != NULL);
LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
LTC_ARGCHK(out != NULL);
outlen = MIN(outlen, no_prng_len - no_prng_offset);
memcpy(out, &no_prng_entropy[no_prng_offset], outlen);
no_prng_offset += outlen;
outlen = MIN(outlen, no_prng->len - no_prng->offset);
memcpy(out, &no_prng->entropy[no_prng->offset], outlen);
no_prng->offset += outlen;
return outlen;
}
@ -137,9 +148,9 @@ int no_prng_test(void)
return CRYPT_OK;
}
const struct ltc_prng_descriptor no_prng_desc =
static const struct ltc_prng_descriptor no_prng_desc =
{
"no_prng", 0,
NULL, 0,
&no_prng_start,
&no_prng_add_entropy,
&no_prng_ready,
@ -150,6 +161,24 @@ const struct ltc_prng_descriptor no_prng_desc =
&no_prng_test
};
struct ltc_prng_descriptor* no_prng_desc_get(void)
{
no_prng_desc_t* no_prng = XMALLOC(sizeof(*no_prng));
LTC_ARGCHK(no_prng != NULL);
XMEMCPY(&no_prng->desc, &no_prng_desc, sizeof(no_prng_desc));
LTC_ARGCHK(snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng) < (int)sizeof(no_prng->name));
no_prng->desc.name = no_prng->name;
return &no_prng->desc;
}
void no_prng_desc_free(struct ltc_prng_descriptor* prng)
{
no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
LTC_ARGCHK(no_prng != NULL);
LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
XFREE(no_prng);
}
#endif

View File

@ -8,7 +8,8 @@
int pkcs_1_eme_test(void)
{
int prng_idx = register_prng(&no_prng_desc);
struct ltc_prng_descriptor* no_prng_desc = no_prng_desc_get();
int prng_idx = register_prng(no_prng_desc);
int hash_idx = find_hash("sha1");
unsigned int i;
unsigned int j;
@ -37,8 +38,8 @@ int pkcs_1_eme_test(void)
unsigned char buf[256], obuf[256];
unsigned long buflen = sizeof(buf), obuflen = sizeof(obuf);
int stat;
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, NULL);
DOX(rsa_encrypt_key_ex(s->o1, s->o1_l, obuf, &obuflen, NULL, 0, NULL, prng_idx, -1, LTC_PKCS_1_V1_5, key), s->name);
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, (prng_state*)no_prng_desc);
DOX(rsa_encrypt_key_ex(s->o1, s->o1_l, obuf, &obuflen, NULL, 0, (prng_state*)no_prng_desc, prng_idx, -1, LTC_PKCS_1_V1_5, key), s->name);
DOX(obuflen == (unsigned long)s->o3_l?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(memcmp(s->o3, obuf, s->o3_l)==0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(rsa_decrypt_key_ex(obuf, obuflen, buf, &buflen, NULL, 0, -1, LTC_PKCS_1_V1_5, &stat, key), s->name);
@ -48,9 +49,8 @@ int pkcs_1_eme_test(void)
mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL);
} /* for */
#ifndef LTC_PTHREAD
unregister_prng(&no_prng_desc);
#endif
unregister_prng(no_prng_desc);
no_prng_desc_free(no_prng_desc);
return 0;
}

View File

@ -8,7 +8,8 @@
int pkcs_1_oaep_test(void)
{
int prng_idx = register_prng(&no_prng_desc);
struct ltc_prng_descriptor* no_prng_desc = no_prng_desc_get();
int prng_idx = register_prng(no_prng_desc);
int hash_idx = find_hash("sha1");
unsigned int i;
unsigned int j;
@ -37,8 +38,8 @@ int pkcs_1_oaep_test(void)
unsigned char buf[256], obuf[256];
unsigned long buflen = sizeof(buf), obuflen = sizeof(obuf);
int stat;
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, NULL);
DOX(rsa_encrypt_key(s->o1, s->o1_l, obuf, &obuflen, NULL, 0, NULL, prng_idx, hash_idx, key), s->name);
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, (prng_state*)no_prng_desc);
DOX(rsa_encrypt_key(s->o1, s->o1_l, obuf, &obuflen, NULL, 0, (prng_state*)no_prng_desc, prng_idx, hash_idx, key), s->name);
DOX(obuflen == (unsigned long)s->o3_l?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(memcmp(s->o3, obuf, s->o3_l)==0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(rsa_decrypt_key(obuf, obuflen, buf, &buflen, NULL, 0, hash_idx, &stat, key), s->name);
@ -48,9 +49,8 @@ int pkcs_1_oaep_test(void)
mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL);
} /* for */
#ifndef LTC_PTHREAD
unregister_prng(&no_prng_desc);
#endif
unregister_prng(no_prng_desc);
no_prng_desc_free(no_prng_desc);
return 0;
}

View File

@ -8,7 +8,8 @@
int pkcs_1_pss_test(void)
{
int prng_idx = register_prng(&no_prng_desc);
struct ltc_prng_descriptor* no_prng_desc = no_prng_desc_get();
int prng_idx = register_prng(no_prng_desc);
int hash_idx = find_hash("sha1");
unsigned int i;
unsigned int j;
@ -37,9 +38,9 @@ int pkcs_1_pss_test(void)
unsigned char buf[20], obuf[256];
unsigned long buflen = sizeof(buf), obuflen = sizeof(obuf);
int stat;
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, NULL);
prng_descriptor[prng_idx].add_entropy(s->o2, s->o2_l, (prng_state*)no_prng_desc);
DOX(hash_memory(hash_idx, s->o1, s->o1_l, buf, &buflen), s->name);
DOX(rsa_sign_hash(buf, buflen, obuf, &obuflen, NULL, prng_idx, hash_idx, s->o2_l, key), s->name);
DOX(rsa_sign_hash(buf, buflen, obuf, &obuflen, (prng_state*)no_prng_desc, prng_idx, hash_idx, s->o2_l, key), s->name);
DOX(obuflen == (unsigned long)s->o3_l?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(memcmp(s->o3, obuf, s->o3_l)==0?CRYPT_OK:CRYPT_FAIL_TESTVECTOR, s->name);
DOX(rsa_verify_hash(obuf, obuflen, buf, buflen, hash_idx, s->o2_l, &stat, key), s->name);
@ -49,9 +50,8 @@ int pkcs_1_pss_test(void)
mp_clear_multi(key->d, key->e, key->N, key->dQ, key->dP, key->qP, key->p, key->q, NULL);
} /* for */
#ifndef LTC_PTHREAD
unregister_prng(&no_prng_desc);
#endif
unregister_prng(no_prng_desc);
no_prng_desc_free(no_prng_desc);
return 0;
}

View File

@ -49,7 +49,7 @@ int prng_test(void)
/* test prngs (test, import/export) */
for (x = 0; prng_descriptor[x].name != NULL; x++) {
if(strcmp(prng_descriptor[x].name, "no_prng") == 0) continue;
if(strstr(prng_descriptor[x].name, "no_prng") == prng_descriptor[x].name) continue;
err = CRYPT_OK;
DOX(prng_descriptor[x].test(), prng_descriptor[x].name);
DOX(prng_descriptor[x].start(&nprng), prng_descriptor[x].name);

View File

@ -54,7 +54,8 @@ int multi_test(void);
int prng_test(void);
#ifdef LTC_PKCS_1
extern const struct ltc_prng_descriptor no_prng_desc;
struct ltc_prng_descriptor* no_prng_desc_get(void);
void no_prng_desc_free(struct ltc_prng_descriptor*);
#endif
#endif