Starting migration from gnuk to own solution.
gnuk/openpgp will be left as another pkcs15 app. Lots of work has been done in the meanwhile. Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
92
random.c
92
random.c
@@ -31,93 +31,87 @@
|
||||
#define RANDOM_BYTES_LENGTH 32
|
||||
static uint32_t random_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];
|
||||
|
||||
void
|
||||
random_init (void)
|
||||
void random_init (void)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
|
||||
neug_init (random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));
|
||||
neug_init (random_word, RANDOM_BYTES_LENGTH/sizeof (uint32_t));
|
||||
|
||||
for (i = 0; i < NEUG_PRE_LOOP; i++)
|
||||
(void)neug_get (NEUG_KICK_FILLING);
|
||||
for (i = 0; i < NEUG_PRE_LOOP; i++)
|
||||
neug_get (NEUG_KICK_FILLING);
|
||||
}
|
||||
|
||||
void
|
||||
random_fini (void)
|
||||
void random_fini (void)
|
||||
{
|
||||
neug_fini ();
|
||||
neug_fini ();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return pointer to random 32-byte
|
||||
*/
|
||||
const uint8_t *
|
||||
random_bytes_get (void)
|
||||
const uint8_t * random_bytes_get (void)
|
||||
{
|
||||
static uint32_t return_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];
|
||||
neug_wait_full ();
|
||||
memcpy(return_word, random_word, sizeof(return_word));
|
||||
return (const uint8_t *)return_word;
|
||||
static uint32_t return_word[RANDOM_BYTES_LENGTH/sizeof (uint32_t)];
|
||||
neug_wait_full ();
|
||||
memcpy(return_word, random_word, sizeof(return_word));
|
||||
return (const uint8_t *)return_word;
|
||||
}
|
||||
|
||||
/*
|
||||
* Free pointer to random 32-byte
|
||||
*/
|
||||
void
|
||||
random_bytes_free (const uint8_t *p)
|
||||
void random_bytes_free (const uint8_t *p)
|
||||
{
|
||||
(void)p;
|
||||
memset (random_word, 0, RANDOM_BYTES_LENGTH);
|
||||
neug_flush ();
|
||||
(void)p;
|
||||
memset (random_word, 0, RANDOM_BYTES_LENGTH);
|
||||
neug_flush ();
|
||||
}
|
||||
|
||||
/*
|
||||
* Return 4-byte salt
|
||||
*/
|
||||
void
|
||||
random_get_salt (uint8_t *p)
|
||||
void random_get_salt (uint8_t *p)
|
||||
{
|
||||
uint32_t rnd;
|
||||
uint32_t rnd;
|
||||
|
||||
rnd = neug_get (NEUG_KICK_FILLING);
|
||||
memcpy (p, &rnd, sizeof (uint32_t));
|
||||
rnd = neug_get (NEUG_KICK_FILLING);
|
||||
memcpy (p + sizeof (uint32_t), &rnd, sizeof (uint32_t));
|
||||
rnd = neug_get (NEUG_KICK_FILLING);
|
||||
memcpy (p, &rnd, sizeof (uint32_t));
|
||||
rnd = neug_get (NEUG_KICK_FILLING);
|
||||
memcpy (p + sizeof (uint32_t), &rnd, sizeof (uint32_t));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Random byte iterator
|
||||
*/
|
||||
int
|
||||
random_gen (void *arg, unsigned char *out, size_t out_len)
|
||||
int random_gen (void *arg, unsigned char *out, size_t out_len)
|
||||
{
|
||||
uint8_t *index_p = (uint8_t *)arg;
|
||||
uint8_t index = index_p ? *index_p : 0;
|
||||
size_t n;
|
||||
uint8_t *index_p = (uint8_t *)arg;
|
||||
uint8_t index = index_p ? *index_p : 0;
|
||||
size_t n;
|
||||
|
||||
while (out_len)
|
||||
while (out_len)
|
||||
{
|
||||
neug_wait_full ();
|
||||
neug_wait_full ();
|
||||
|
||||
n = RANDOM_BYTES_LENGTH - index;
|
||||
if (n > out_len)
|
||||
n = out_len;
|
||||
n = RANDOM_BYTES_LENGTH - index;
|
||||
if (n > out_len)
|
||||
n = out_len;
|
||||
|
||||
memcpy (out, ((unsigned char *)random_word) + index, n);
|
||||
out += n;
|
||||
out_len -= n;
|
||||
index += n;
|
||||
memcpy (out, ((unsigned char *)random_word) + index, n);
|
||||
out += n;
|
||||
out_len -= n;
|
||||
index += n;
|
||||
|
||||
if (index >= RANDOM_BYTES_LENGTH)
|
||||
{
|
||||
index = 0;
|
||||
neug_flush ();
|
||||
}
|
||||
if (index >= RANDOM_BYTES_LENGTH)
|
||||
{
|
||||
index = 0;
|
||||
neug_flush ();
|
||||
}
|
||||
}
|
||||
|
||||
if (index_p)
|
||||
*index_p = index;
|
||||
if (index_p)
|
||||
*index_p = index;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user