Commit 66edc857 authored by Davide Galassi's avatar Davide Galassi

Merge branch 'feature/prng_refactory' into 'master'

PRNG refactory

Closes #6

See merge request !14
parents 60150f0b dcf9aafa
Pipeline #52088601 passed with stages
in 1 minute and 20 seconds
......@@ -27,6 +27,8 @@ Given a version number MAJOR.MINOR.PATCH
- CFB block cipher mode of operation
- CRC16-DNP
- removed trivial LRC and 8-bit checksum
- Incremental CMAC implementation
[0.0.7] - 2018-12-26
--------------------
......
......@@ -80,8 +80,8 @@ Pseudo random numbers generators
Message authentication code
---------------------------
- CMAC
- HMAC
- CMAC
Hash
......@@ -96,13 +96,9 @@ Hash
- CRC16-CCITT
- CRC16-IBM
- CRC16-DNP
- CRC32-Ethernet
### Checksums
- LRC
- Trivial checksum
Classical ciphers
-----------------
......
......@@ -25,9 +25,6 @@ CRY_COVERAGE=y
# Algorithms
################################################################################
# Options: aes|weak|posix|win
CRY_PRNG=aes
# Use Extended Euclidean Algorithm to compute inverse.
# If not defined the faster Binary Extended Algorithm (HAC 14.4.3) is used.
CRY_MPI_INV_EUCLID=y
......
......@@ -13,7 +13,7 @@ extern "C"{
#include <stddef.h>
/**
* Pseudo random number generator initialization.
* Strong pseudo random number generator initialization.
*
* Because very often people forget to perform initialization, other prng
* functions will automatically check to see if this routine has been called
......@@ -23,16 +23,38 @@ extern "C"{
* @param seed_siz Seed data size.
* @return 0 on success, -1 on failure.
*/
int cry_prng_init(const unsigned char *seed, size_t seed_siz);
int cry_prng_aes_init(const unsigned char *seed, size_t seed_siz);
/**
* Produce a pseudo random sequence.
* Strong pseudo random number generator production.
*
* @param buf Destination buffer
* @param siz Destination buffer size.
* @return 0 on success, -1 on failure.
*/
int cry_prng_rand(unsigned char *buf, size_t siz);
int cry_prng_aes_rand(unsigned char *buf, size_t siz);
/**
* Weak pseudo random number generator initialization.
*
* Because very often people forget to perform initialization, other prng
* functions will automatically check to see if this routine has been called
* and call it at that point if not.
*
* @param seed Seed data.
* @param seed_siz Seed data size.
* @return 0 on success, -1 on failure.
*/
int cry_prng_weak_init(const unsigned char *seed, size_t seed_siz);
/**
* Weak pseudo random number generator production.
*
* @param buf Destination buffer
* @param siz Destination buffer size.
* @return 0 on success, -1 on failure.
*/
int cry_prng_weak_rand(unsigned char *buf, size_t siz);
#ifdef __cplusplus
}
......
......@@ -166,7 +166,7 @@ static int keygen(unsigned char *key, unsigned char *ikey, size_t keylen,
do {
if (trials > 0) {
cry_prng_rand(key, keylen);
cry_prng_aes_rand(key, keylen);
trials--;
}
......
......@@ -30,11 +30,11 @@ static int nozero_rand(unsigned char *dst, unsigned int n)
int res, k;
unsigned char buf[16];
if ((res = cry_prng_rand(dst, n)) < 0)
if ((res = cry_prng_aes_rand(dst, n)) < 0)
return res;
while (n-- > 0) {
if (dst[n] == 0) {
cry_prng_rand(buf, sizeof(buf));
cry_prng_aes_rand(buf, sizeof(buf));
for (k = 0; k < sizeof(buf); k++) {
if (buf[k] != 0) {
dst[n] = buf[k];
......
......@@ -20,8 +20,9 @@ int cry_mpi_rand(cry_mpi *r, unsigned int bits)
}
r->used = digs;
if ((res = cry_prng_rand((unsigned char *)r->data,
digs * sizeof(cry_mpi_digit))) != 0)
res = cry_prng_aes_rand((unsigned char *)r->data,
digs * sizeof(cry_mpi_digit));
if (res != 0)
return res;
msb = bits - CRY_MPI_DIGIT_BITS * (digs - 1);
......
......@@ -15,7 +15,7 @@ struct prng_bc_ctx {
static struct prng_bc_ctx prng_ctx;
static int init = 0;
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
int cry_prng_aes_init(const unsigned char *seed, size_t seed_siz)
{
size_t sz;
unsigned char key[BLOCK_SIZ] = {0};
......@@ -42,10 +42,10 @@ int cry_prng_init(const unsigned char *seed, size_t seed_siz)
if (++ctr[__i]) break; \
} while (0)
int cry_prng_rand(unsigned char *buf, size_t siz)
int cry_prng_aes_rand(unsigned char *buf, size_t siz)
{
if (!init)
cry_prng_init(NULL, 0);
cry_prng_aes_init(NULL, 0);
/* eventually store the previous call residual */
for ( ; prng_ctx.ix && siz; siz--) {
......
#include <cry/prng.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
static int urand_fd = -1;
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
{
urand_fd = open("/dev/urandom", O_RDONLY);
if (urand_fd < 0)
return -1;
return 0;
}
int cry_prng_rand(unsigned char *buf, size_t siz)
{
int n;
if (urand_fd == -1 && cry_prng_init(NULL, 0) != 0)
return -1;
while (siz) {
if ((n = read(urand_fd, buf, siz)) < 0) {
if (errno == EINTR)
continue;
return -1;
}
buf += n;
siz -= n;
}
return 0;
}
......@@ -22,7 +22,7 @@ static int init = 0;
((r) = (z[0] ^ z[1] ^ z[2] ^ z[3])); \
} while(0)
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
int cry_prng_weak_init(const unsigned char *seed, size_t seed_siz)
{
init = 1;
uint32_t fallback[4] = {1,2,3,4};
......@@ -35,14 +35,14 @@ int cry_prng_init(const unsigned char *seed, size_t seed_siz)
return 0;
}
int cry_prng_rand(unsigned char *buf, size_t siz)
int cry_prng_weak_rand(unsigned char *buf, size_t siz)
{
int i, iter = siz / 4;
uint32_t *buf32 = (uint32_t *) buf;
uint32_t r;
if (!init)
cry_prng_init(NULL, 0);
cry_prng_weak_init(NULL, 0);
for (i = 0; i < iter; i++, buf32++)
RAND_GET(*buf32);
......
#include <windows.h>
#include <wincrypt.h>
static HCRYPTPROV hProvider = 0;
int cry_prng_init(const unsigned char *seed, size_t seed_siz)
{
if (!CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
return -1;
}
return 0;
}
int cry_prng_rand(unsigned char *buf, size_t siz)
{
if (!hProvider && cry_prng_init(NULL, 0) != 0)
return -1;
if (!CryptGenRandom(hProvider, siz, buf))
return -1;
return 0;
}
# src/prng/subdir.mk
ifeq ($(CRY_PRNG),posix)
objects-y += prng_posix.o
else ifeq ($(CRY_PRNG),win)
objects-y += prng_win.o
else ifeq ($(CRY_PRNG),aes)
objects-y += prng_aes.o
else
objects-y += prng_weak.o
endif
objects-y += prng_aes.o prng_weak.o
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment