Skip to content

Commit

Permalink
random: more unit tests to check entropy injection
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisstjohn committed Nov 15, 2023
1 parent b3cc153 commit 2eaa1da
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
12 changes: 11 additions & 1 deletion soft/lib/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
#define RANDOM_POLY1 0x4E /**< x^14+x^11+x^10+x^9 */
#define RANDOM_POLY0 0x63 /**< x^6+x^5+x^1+1 */

#define STATIC static
#ifdef TEST
# define STATIC /* extern */
#else
# define STATIC static
#endif

/** @note LFSR must never be zero or it will get stuck there */
STATIC uint8_t random_lfsr[4] = { 0, 0, 0, 0x80 };
Expand Down Expand Up @@ -100,6 +104,7 @@ uint8_t random_get(uint8_t maximum)
for(mask = 0xFF; maximum <= (mask>>1); mask >>= 1);
}

#if 1
/* Pump LFSR until a number in range is returned. On average this
* should not take more than 2 iterations. */
for(;;)
Expand All @@ -112,4 +117,9 @@ uint8_t random_get(uint8_t maximum)
return value;
}
}
#else
/* Test the unit test :-) */
random_pump();
return (uint8_t)(random_lfsr[0] % (1+(unsigned)maximum));
#endif
}
75 changes: 75 additions & 0 deletions soft/test/unittest/test_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@

#include "random.h" /* Module under test */

#include <stdbool.h>
#include <stdlib.h> /* calloc, free */

/* Private access into module under test */
extern uint8_t random_lfsr[4];
extern void random_pump(void);

void do_distribution(uint8_t maximum, unsigned iterations, float tolerance)
{
unsigned* histogram = calloc((maximum+1), sizeof(unsigned));
Expand Down Expand Up @@ -72,3 +77,73 @@ void test_coin(void)
{
do_distribution(1, 1000000, 0.001);
}

void test_pump(void)
{
/* Without carry */
random_lfsr[0] = 1;
random_lfsr[1] = 2;
random_lfsr[2] = 3;
random_lfsr[3] = 4;
random_pump();
TEST_ASSERT_EQUAL(2, random_lfsr[0]);
TEST_ASSERT_EQUAL(4, random_lfsr[1]);
TEST_ASSERT_EQUAL(6, random_lfsr[2]);
TEST_ASSERT_EQUAL(8, random_lfsr[3]);

/* With carry */
random_lfsr[0] = 1;
random_lfsr[1] = 2;
random_lfsr[2] = 3;
random_lfsr[3] = 0x84;
random_pump();
TEST_ASSERT_NOT_EQUAL(2, random_lfsr[0]);
TEST_ASSERT_NOT_EQUAL(4, random_lfsr[1]);
TEST_ASSERT_NOT_EQUAL(6, random_lfsr[2]);
TEST_ASSERT_NOT_EQUAL(8, random_lfsr[3]);
}

void test_add_entropy(void)
{
/* Clear entropy */
random_lfsr[0] = 0;
random_lfsr[1] = 0;
random_lfsr[2] = 0;
random_lfsr[3] = 0;

/* Add 1 bit of entropy */
random_add(0x1);

/* Look for some entropy */
bool entropic = false;
for (unsigned i = 0; i < 4; i++)
{
/* Not 0 (initial), 1 (seed) or 1<<1 (pumped seed) */
if (random_lfsr[i] > 2)
{
entropic = true;
}
}
TEST_ASSERT_TRUE(entropic);
}

void test_clear_entropy(void)
{
/* Try to clear entropy with add */
random_lfsr[0] = 0;
random_lfsr[1] = 0;
random_lfsr[2] = 0;
random_lfsr[3] = 0;
random_add(0);

/* Look for some entropy */
bool entropic = false;
for (unsigned i = 0; i < 4; i++)
{
if (random_lfsr[i] != 0)
{
entropic = true;
}
}
TEST_ASSERT_TRUE(entropic);
}

0 comments on commit 2eaa1da

Please sign in to comment.