mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-25 06:07:53 -05:00
Import code from previous AssetBuilder version
This commit is contained in:
247
thirdparty/libtomcrypt/prngs/chacha20.c
vendored
Normal file
247
thirdparty/libtomcrypt/prngs/chacha20.c
vendored
Normal file
@ -0,0 +1,247 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
/* the idea of re-keying loosely follows the approach used in:
|
||||
* http://bxr.su/OpenBSD/lib/libc/crypt/arc4random.c
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_CHACHA20_PRNG
|
||||
|
||||
const struct ltc_prng_descriptor chacha20_prng_desc =
|
||||
{
|
||||
"chacha20",
|
||||
40,
|
||||
&chacha20_prng_start,
|
||||
&chacha20_prng_add_entropy,
|
||||
&chacha20_prng_ready,
|
||||
&chacha20_prng_read,
|
||||
&chacha20_prng_done,
|
||||
&chacha20_prng_export,
|
||||
&chacha20_prng_import,
|
||||
&chacha20_prng_test
|
||||
};
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_start(prng_state *prng)
|
||||
{
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
XMEMSET(&prng->chacha.ent, 0, sizeof(prng->chacha.ent));
|
||||
prng->chacha.idx = 0;
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char buf[40];
|
||||
unsigned long i;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) {
|
||||
/* chacha20_prng_ready() was already called, do "rekey" operation */
|
||||
if ((err = chacha_keystream(&prng->chacha.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
|
||||
/* key 32 bytes, 20 rounds */
|
||||
if ((err = chacha_setup(&prng->chacha.s, buf, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* iv 8 bytes */
|
||||
if ((err = chacha_ivctr64(&prng->chacha.s, buf + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* clear KEY + IV */
|
||||
zeromem(buf, sizeof(buf));
|
||||
}
|
||||
else {
|
||||
/* chacha20_prng_ready() was not called yet, add entropy to ent buffer */
|
||||
while (inlen--) prng->chacha.ent[prng->chacha.idx++ % sizeof(prng->chacha.ent)] ^= *in++;
|
||||
}
|
||||
err = CRYPT_OK;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_ready(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
|
||||
/* key 32 bytes, 20 rounds */
|
||||
if ((err = chacha_setup(&prng->chacha.s, prng->chacha.ent, 32, 20)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* iv 8 bytes */
|
||||
if ((err = chacha_ivctr64(&prng->chacha.s, prng->chacha.ent + 32, 8, 0)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
XMEMSET(&prng->chacha.ent, 0, sizeof(prng->chacha.ent));
|
||||
prng->chacha.idx = 0;
|
||||
prng->ready = 1;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long chacha20_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
|
||||
if (chacha_keystream(&prng->chacha.s, out, outlen) != CRYPT_OK) outlen = 0;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_done(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
err = chacha_done(&prng->chacha.s);
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
unsigned long len = chacha20_prng_desc.export_size;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (chacha20_prng_read(out, len, prng) != len) {
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
*outlen = len;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int chacha20_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
if (inlen < (unsigned long)chacha20_prng_desc.export_size) return CRYPT_INVALID_ARG;
|
||||
|
||||
if ((err = chacha20_prng_start(prng)) != CRYPT_OK) return err;
|
||||
if ((err = chacha20_prng_add_entropy(in, inlen, prng)) != CRYPT_OK) return err;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int chacha20_prng_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
prng_state st;
|
||||
unsigned char en[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
||||
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
|
||||
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
|
||||
0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32 };
|
||||
unsigned char dmp[300];
|
||||
unsigned long dmplen = sizeof(dmp);
|
||||
unsigned char out[500];
|
||||
unsigned char t1[] = { 0x59, 0xB2, 0x26, 0x95, 0x2B, 0x01, 0x8F, 0x05, 0xBE, 0xD8 };
|
||||
unsigned char t2[] = { 0x47, 0xC9, 0x0D, 0x03, 0xE4, 0x75, 0x34, 0x27, 0xBD, 0xDE };
|
||||
unsigned char t3[] = { 0xBC, 0xFA, 0xEF, 0x59, 0x37, 0x7F, 0x1A, 0x91, 0x1A, 0xA6 };
|
||||
int err;
|
||||
|
||||
if ((err = chacha20_prng_start(&st)) != CRYPT_OK) return err;
|
||||
/* add entropy to uninitialized prng */
|
||||
if ((err = chacha20_prng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if ((err = chacha20_prng_ready(&st)) != CRYPT_OK) return err;
|
||||
if (chacha20_prng_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t1, sizeof(t1), "CHACHA-PRNG", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if (chacha20_prng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
/* add entropy to already initialized prng */
|
||||
if ((err = chacha20_prng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if (chacha20_prng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if ((err = chacha20_prng_export(dmp, &dmplen, &st)) != CRYPT_OK) return err;
|
||||
if (chacha20_prng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (chacha20_prng_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t2, sizeof(t2), "CHACHA-PRNG", 2)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = chacha20_prng_done(&st)) != CRYPT_OK) return err;
|
||||
if ((err = chacha20_prng_import(dmp, dmplen, &st)) != CRYPT_OK) return err;
|
||||
if ((err = chacha20_prng_ready(&st)) != CRYPT_OK) return err;
|
||||
if (chacha20_prng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (chacha20_prng_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t3, sizeof(t3), "CHACHA-PRNG", 3)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = chacha20_prng_done(&st)) != CRYPT_OK) return err;
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
498
thirdparty/libtomcrypt/prngs/fortuna.c
vendored
Normal file
498
thirdparty/libtomcrypt/prngs/fortuna.c
vendored
Normal file
@ -0,0 +1,498 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file fortuna.c
|
||||
Fortuna PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
/* Implementation of Fortuna by Tom St Denis
|
||||
|
||||
We deviate slightly here for reasons of simplicity [and to fit in the API]. First all "sources"
|
||||
in the AddEntropy function are fixed to 0. Second since no reliable timer is provided
|
||||
we reseed automatically when len(pool0) >= 64 or every LTC_FORTUNA_WD calls to the read function */
|
||||
|
||||
#ifdef LTC_FORTUNA
|
||||
|
||||
/* requries LTC_SHA256 and AES */
|
||||
#if !(defined(LTC_RIJNDAEL) && defined(LTC_SHA256))
|
||||
#error LTC_FORTUNA requires LTC_SHA256 and LTC_RIJNDAEL (AES)
|
||||
#endif
|
||||
|
||||
#ifndef LTC_FORTUNA_POOLS
|
||||
#warning LTC_FORTUNA_POOLS was not previously defined (old headers?)
|
||||
#define LTC_FORTUNA_POOLS 32
|
||||
#endif
|
||||
|
||||
#if LTC_FORTUNA_POOLS < 4 || LTC_FORTUNA_POOLS > 32
|
||||
#error LTC_FORTUNA_POOLS must be in [4..32]
|
||||
#endif
|
||||
|
||||
const struct ltc_prng_descriptor fortuna_desc = {
|
||||
"fortuna",
|
||||
(32 * LTC_FORTUNA_POOLS), /* default: 1024 */
|
||||
&fortuna_start,
|
||||
&fortuna_add_entropy,
|
||||
&fortuna_ready,
|
||||
&fortuna_read,
|
||||
&fortuna_done,
|
||||
&fortuna_export,
|
||||
&fortuna_import,
|
||||
&fortuna_test
|
||||
};
|
||||
|
||||
/* update the IV */
|
||||
static void _fortuna_update_iv(prng_state *prng)
|
||||
{
|
||||
int x;
|
||||
unsigned char *IV;
|
||||
/* update IV */
|
||||
IV = prng->fortuna.IV;
|
||||
for (x = 0; x < 16; x++) {
|
||||
IV[x] = (IV[x] + 1) & 255;
|
||||
if (IV[x] != 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
/* reseed the PRNG */
|
||||
static int _fortuna_reseed(prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
hash_state md;
|
||||
ulong64 reset_cnt;
|
||||
int err, x;
|
||||
|
||||
|
||||
/* new K == LTC_SHA256(K || s) where s == LTC_SHA256(P0) || LTC_SHA256(P1) ... */
|
||||
sha256_init(&md);
|
||||
if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
|
||||
reset_cnt = prng->fortuna.reset_cnt + 1;
|
||||
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if (x == 0 || ((reset_cnt >> (x-1)) & 1) == 0) {
|
||||
/* terminate this hash */
|
||||
if ((err = sha256_done(&prng->fortuna.pool[x], tmp)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* add it to the string */
|
||||
if ((err = sha256_process(&md, tmp, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
/* reset this pool */
|
||||
if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* finish key */
|
||||
if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
_fortuna_update_iv(prng);
|
||||
|
||||
/* reset/update internals */
|
||||
prng->fortuna.pool0_len = 0;
|
||||
prng->fortuna.wd = 0;
|
||||
prng->fortuna.reset_cnt = reset_cnt;
|
||||
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(&md, sizeof(md));
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
"Update Seed File"-compliant update of K
|
||||
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
static int _fortuna_update_seed(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
hash_state md;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
/* new K = LTC_SHA256(K || in) */
|
||||
sha256_init(&md);
|
||||
if ((err = sha256_process(&md, prng->fortuna.K, 32)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
if ((err = sha256_process(&md, in, inlen)) != CRYPT_OK) {
|
||||
sha256_done(&md, tmp);
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
/* finish key */
|
||||
if ((err = sha256_done(&md, prng->fortuna.K)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
_fortuna_update_iv(prng);
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(&md, sizeof(md));
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_start(prng_state *prng)
|
||||
{
|
||||
int err, x, y;
|
||||
unsigned char tmp[MAXBLOCKSIZE];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
|
||||
/* initialize the pools */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_init(&prng->fortuna.pool[x])) != CRYPT_OK) {
|
||||
for (y = 0; y < x; y++) {
|
||||
sha256_done(&prng->fortuna.pool[y], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
}
|
||||
prng->fortuna.pool_idx = prng->fortuna.pool0_len = prng->fortuna.wd = 0;
|
||||
prng->fortuna.reset_cnt = 0;
|
||||
|
||||
/* reset bufs */
|
||||
zeromem(prng->fortuna.K, 32);
|
||||
if ((err = rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey)) != CRYPT_OK) {
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
sha256_done(&prng->fortuna.pool[x], tmp);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
zeromem(prng->fortuna.IV, 16);
|
||||
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[2];
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
/* ensure inlen <= 32 */
|
||||
if (inlen > 32) {
|
||||
inlen = 32;
|
||||
}
|
||||
|
||||
/* add s || length(in) || in to pool[pool_idx] */
|
||||
tmp[0] = 0;
|
||||
tmp[1] = (unsigned char)inlen;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], tmp, 2)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
if ((err = sha256_process(&prng->fortuna.pool[prng->fortuna.pool_idx], in, inlen)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
if (prng->fortuna.pool_idx == 0) {
|
||||
prng->fortuna.pool0_len += inlen;
|
||||
}
|
||||
if (++(prng->fortuna.pool_idx) == LTC_FORTUNA_POOLS) {
|
||||
prng->fortuna.pool_idx = 0;
|
||||
}
|
||||
err = CRYPT_OK; /* success */
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_ready(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
err = _fortuna_reseed(prng);
|
||||
prng->ready = (err == CRYPT_OK) ? 1 : 0;
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long fortuna_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
unsigned char tmp[16];
|
||||
unsigned long tlen = 0;
|
||||
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if (!prng->ready) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* do we have to reseed? */
|
||||
if ((++prng->fortuna.wd == LTC_FORTUNA_WD) && (prng->fortuna.pool0_len >= 64)) {
|
||||
if (_fortuna_reseed(prng) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
}
|
||||
|
||||
/* ensure that one reseed happened before allowing to read */
|
||||
if (prng->fortuna.reset_cnt == 0) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* now generate the blocks required */
|
||||
tlen = outlen;
|
||||
|
||||
/* handle whole blocks without the extra XMEMCPY */
|
||||
while (outlen >= 16) {
|
||||
/* encrypt the IV and store it */
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, out, &prng->fortuna.skey);
|
||||
out += 16;
|
||||
outlen -= 16;
|
||||
_fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* left over bytes? */
|
||||
if (outlen > 0) {
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, tmp, &prng->fortuna.skey);
|
||||
XMEMCPY(out, tmp, outlen);
|
||||
_fortuna_update_iv(prng);
|
||||
}
|
||||
|
||||
/* generate new key */
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K , &prng->fortuna.skey);
|
||||
_fortuna_update_iv(prng);
|
||||
|
||||
rijndael_ecb_encrypt(prng->fortuna.IV, prng->fortuna.K+16, &prng->fortuna.skey);
|
||||
_fortuna_update_iv(prng);
|
||||
|
||||
if (rijndael_setup(prng->fortuna.K, 32, 0, &prng->fortuna.skey) != CRYPT_OK) {
|
||||
tlen = 0;
|
||||
}
|
||||
|
||||
LBL_UNLOCK:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return tlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_done(prng_state *prng)
|
||||
{
|
||||
int err, x;
|
||||
unsigned char tmp[32];
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
|
||||
/* terminate all the hashes */
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
if ((err = sha256_done(&(prng->fortuna.pool[x]), tmp)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
}
|
||||
/* call cipher done when we invent one ;-) */
|
||||
err = CRYPT_OK; /* success */
|
||||
|
||||
LBL_UNLOCK:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(tmp, sizeof(tmp));
|
||||
#endif
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
int x, err;
|
||||
hash_state *md;
|
||||
unsigned long len = fortuna_desc.export_size;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if (!prng->ready) {
|
||||
err = CRYPT_ERROR;
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* we'll write bytes for s&g's */
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
md = XMALLOC(sizeof(hash_state));
|
||||
if (md == NULL) {
|
||||
err = CRYPT_MEM;
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* to emit the state we copy each pool, terminate it then hash it again so
|
||||
* an attacker who sees the state can't determine the current state of the PRNG
|
||||
*/
|
||||
for (x = 0; x < LTC_FORTUNA_POOLS; x++) {
|
||||
/* copy the PRNG */
|
||||
XMEMCPY(md, &(prng->fortuna.pool[x]), sizeof(*md));
|
||||
|
||||
/* terminate it */
|
||||
if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* now hash it */
|
||||
if ((err = sha256_init(md)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = sha256_process(md, out+x*32, 32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((err = sha256_done(md, out+x*32)) != CRYPT_OK) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
*outlen = len;
|
||||
err = CRYPT_OK;
|
||||
|
||||
LBL_ERR:
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(md, sizeof(*md));
|
||||
#endif
|
||||
XFREE(md);
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int fortuna_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
if (inlen < (unsigned long)fortuna_desc.export_size) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
if ((err = fortuna_start(prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = _fortuna_update_seed(in, inlen, prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int fortuna_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
int err;
|
||||
|
||||
if ((err = sha256_test()) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
return rijndael_test();
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
250
thirdparty/libtomcrypt/prngs/rc4.c
vendored
Normal file
250
thirdparty/libtomcrypt/prngs/rc4.c
vendored
Normal file
@ -0,0 +1,250 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file prngs/rc4.c
|
||||
RC4 PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_RC4
|
||||
|
||||
const struct ltc_prng_descriptor rc4_desc =
|
||||
{
|
||||
"rc4",
|
||||
32,
|
||||
&rc4_start,
|
||||
&rc4_add_entropy,
|
||||
&rc4_ready,
|
||||
&rc4_read,
|
||||
&rc4_done,
|
||||
&rc4_export,
|
||||
&rc4_import,
|
||||
&rc4_test
|
||||
};
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_start(prng_state *prng)
|
||||
{
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
/* set entropy (key) size to zero */
|
||||
prng->rc4.s.x = 0;
|
||||
/* clear entropy (key) buffer */
|
||||
XMEMSET(&prng->rc4.s.buf, 0, sizeof(prng->rc4.s.buf));
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char buf[256];
|
||||
unsigned long i;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) {
|
||||
/* rc4_ready() was already called, do "rekey" operation */
|
||||
if ((err = rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
|
||||
/* initialize RC4 */
|
||||
if ((err = rc4_stream_setup(&prng->rc4.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* drop first 3072 bytes - https://en.wikipedia.org/wiki/RC4#Fluhrer.2C_Mantin_and_Shamir_attack */
|
||||
for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf));
|
||||
zeromem(buf, sizeof(buf));
|
||||
}
|
||||
else {
|
||||
/* rc4_ready() was not called yet, add entropy to the buffer */
|
||||
while (inlen--) prng->rc4.s.buf[prng->rc4.s.x++ % sizeof(prng->rc4.s.buf)] ^= *in++;
|
||||
}
|
||||
err = CRYPT_OK;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_ready(prng_state *prng)
|
||||
{
|
||||
unsigned char buf[256] = { 0 };
|
||||
unsigned long len;
|
||||
int err, i;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
|
||||
XMEMCPY(buf, prng->rc4.s.buf, sizeof(buf));
|
||||
/* initialize RC4 */
|
||||
len = MIN(prng->rc4.s.x, 256); /* TODO: we can perhaps always use all 256 bytes */
|
||||
if ((err = rc4_stream_setup(&prng->rc4.s, buf, len)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* drop first 3072 bytes - https://en.wikipedia.org/wiki/RC4#Fluhrer.2C_Mantin_and_Shamir_attack */
|
||||
for (i = 0; i < 12; i++) rc4_stream_keystream(&prng->rc4.s, buf, sizeof(buf));
|
||||
prng->ready = 1;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long rc4_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
|
||||
if (rc4_stream_keystream(&prng->rc4.s, out, outlen) != CRYPT_OK) outlen = 0;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_done(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
err = rc4_stream_done(&prng->rc4.s);
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
unsigned long len = rc4_desc.export_size;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (rc4_read(out, len, prng) != len) {
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
*outlen = len;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rc4_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
if (inlen < (unsigned long)rc4_desc.export_size) return CRYPT_INVALID_ARG;
|
||||
|
||||
if ((err = rc4_start(prng)) != CRYPT_OK) return err;
|
||||
if ((err = rc4_add_entropy(in, inlen, prng)) != CRYPT_OK) return err;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int rc4_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
prng_state st;
|
||||
unsigned char en[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
||||
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
|
||||
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
|
||||
0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32 };
|
||||
unsigned char dmp[500];
|
||||
unsigned long dmplen = sizeof(dmp);
|
||||
unsigned char out[1000];
|
||||
unsigned char t1[] = { 0xE0, 0x4D, 0x9A, 0xF6, 0xA8, 0x9D, 0x77, 0x53, 0xAE, 0x09 };
|
||||
unsigned char t2[] = { 0xEF, 0x80, 0xA2, 0xE6, 0x50, 0x91, 0xF3, 0x17, 0x4A, 0x8A };
|
||||
unsigned char t3[] = { 0x4B, 0xD6, 0x5C, 0x67, 0x99, 0x03, 0x56, 0x12, 0x80, 0x48 };
|
||||
int err;
|
||||
|
||||
if ((err = rc4_start(&st)) != CRYPT_OK) return err;
|
||||
/* add entropy to uninitialized prng */
|
||||
if ((err = rc4_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if ((err = rc4_ready(&st)) != CRYPT_OK) return err;
|
||||
if (rc4_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t1, sizeof(t1), "RC4-PRNG", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if (rc4_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
/* add entropy to already initialized prng */
|
||||
if ((err = rc4_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if (rc4_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if ((err = rc4_export(dmp, &dmplen, &st)) != CRYPT_OK) return err;
|
||||
if (rc4_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (rc4_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t2, sizeof(t2), "RC4-PRNG", 2)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = rc4_done(&st)) != CRYPT_OK) return err;
|
||||
if ((err = rc4_import(dmp, dmplen, &st)) != CRYPT_OK) return err;
|
||||
if ((err = rc4_ready(&st)) != CRYPT_OK) return err;
|
||||
if (rc4_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (rc4_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t3, sizeof(t3), "RC4-PRNG", 3)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = rc4_done(&st)) != CRYPT_OK) return err;
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
159
thirdparty/libtomcrypt/prngs/rng_get_bytes.c
vendored
Normal file
159
thirdparty/libtomcrypt/prngs/rng_get_bytes.c
vendored
Normal file
@ -0,0 +1,159 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_RNG_GET_BYTES
|
||||
/**
|
||||
@file rng_get_bytes.c
|
||||
portable way to get secure random bits to feed a PRNG (Tom St Denis)
|
||||
*/
|
||||
|
||||
#if defined(LTC_DEVRANDOM) && !defined(_WIN32)
|
||||
/* on *NIX read /dev/random */
|
||||
static unsigned long _rng_nix(unsigned char *buf, unsigned long len,
|
||||
void (*callback)(void))
|
||||
{
|
||||
#ifdef LTC_NO_FILE
|
||||
LTC_UNUSED_PARAM(callback);
|
||||
LTC_UNUSED_PARAM(buf);
|
||||
LTC_UNUSED_PARAM(len);
|
||||
return 0;
|
||||
#else
|
||||
FILE *f;
|
||||
unsigned long x;
|
||||
LTC_UNUSED_PARAM(callback);
|
||||
#ifdef LTC_TRY_URANDOM_FIRST
|
||||
f = fopen("/dev/urandom", "rb");
|
||||
if (f == NULL)
|
||||
#endif /* LTC_TRY_URANDOM_FIRST */
|
||||
f = fopen("/dev/random", "rb");
|
||||
|
||||
if (f == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* disable buffering */
|
||||
if (setvbuf(f, NULL, _IONBF, 0) != 0) {
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
x = (unsigned long)fread(buf, 1, (size_t)len, f);
|
||||
fclose(f);
|
||||
return x;
|
||||
#endif /* LTC_NO_FILE */
|
||||
}
|
||||
|
||||
#endif /* LTC_DEVRANDOM */
|
||||
|
||||
#if !defined(_WIN32_WCE)
|
||||
|
||||
#define ANSI_RNG
|
||||
|
||||
static unsigned long _rng_ansic(unsigned char *buf, unsigned long len,
|
||||
void (*callback)(void))
|
||||
{
|
||||
clock_t t1;
|
||||
int l, acc, bits, a, b;
|
||||
|
||||
l = len;
|
||||
bits = 8;
|
||||
acc = a = b = 0;
|
||||
while (len--) {
|
||||
if (callback != NULL) callback();
|
||||
while (bits--) {
|
||||
do {
|
||||
t1 = XCLOCK(); while (t1 == XCLOCK()) a ^= 1;
|
||||
t1 = XCLOCK(); while (t1 == XCLOCK()) b ^= 1;
|
||||
} while (a == b);
|
||||
acc = (acc << 1) | a;
|
||||
}
|
||||
*buf++ = acc;
|
||||
acc = 0;
|
||||
bits = 8;
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* Try the Microsoft CSP */
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
#ifndef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0400
|
||||
#endif
|
||||
#ifdef _WIN32_WCE
|
||||
#define UNDER_CE
|
||||
#define ARM
|
||||
#endif
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <wincrypt.h>
|
||||
|
||||
static unsigned long _rng_win32(unsigned char *buf, unsigned long len,
|
||||
void (*callback)(void))
|
||||
{
|
||||
HCRYPTPROV hProv = 0;
|
||||
LTC_UNUSED_PARAM(callback);
|
||||
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
||||
(CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
|
||||
!CryptAcquireContext (&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET))
|
||||
return 0;
|
||||
|
||||
if (CryptGenRandom(hProv, len, buf) == TRUE) {
|
||||
CryptReleaseContext(hProv, 0);
|
||||
return len;
|
||||
} else {
|
||||
CryptReleaseContext(hProv, 0);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* WIN32 */
|
||||
|
||||
/**
|
||||
Read the system RNG
|
||||
@param out Destination
|
||||
@param outlen Length desired (octets)
|
||||
@param callback Pointer to void function to act as "callback" when RNG is slow. This can be NULL
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long rng_get_bytes(unsigned char *out, unsigned long outlen,
|
||||
void (*callback)(void))
|
||||
{
|
||||
unsigned long x;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
|
||||
#ifdef LTC_PRNG_ENABLE_LTC_RNG
|
||||
if (ltc_rng) {
|
||||
x = ltc_rng(out, outlen, callback);
|
||||
if (x != 0) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN32_WCE)
|
||||
x = _rng_win32(out, outlen, callback); if (x != 0) { return x; }
|
||||
#elif defined(LTC_DEVRANDOM)
|
||||
x = _rng_nix(out, outlen, callback); if (x != 0) { return x; }
|
||||
#endif
|
||||
#ifdef ANSI_RNG
|
||||
x = _rng_ansic(out, outlen, callback); if (x != 0) { return x; }
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif /* #ifdef LTC_RNG_GET_BYTES */
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
69
thirdparty/libtomcrypt/prngs/rng_make_prng.c
vendored
Normal file
69
thirdparty/libtomcrypt/prngs/rng_make_prng.c
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
#ifdef LTC_RNG_MAKE_PRNG
|
||||
/**
|
||||
@file rng_make_prng.c
|
||||
portable way to get secure random bits to feed a PRNG (Tom St Denis)
|
||||
*/
|
||||
|
||||
/**
|
||||
Create a PRNG from a RNG
|
||||
@param bits Number of bits of entropy desired (64 ... 1024)
|
||||
@param wprng Index of which PRNG to setup
|
||||
@param prng [out] PRNG state to initialize
|
||||
@param callback A pointer to a void function for when the RNG is slow, this can be NULL
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int rng_make_prng(int bits, int wprng, prng_state *prng,
|
||||
void (*callback)(void))
|
||||
{
|
||||
unsigned char buf[256];
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
/* check parameter */
|
||||
if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (bits < 64 || bits > 1024) {
|
||||
return CRYPT_INVALID_PRNGSIZE;
|
||||
}
|
||||
|
||||
if ((err = prng_descriptor[wprng].start(prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
bits = ((bits/8)+((bits&7)!=0?1:0)) * 2;
|
||||
if (rng_get_bytes(buf, (unsigned long)bits, callback) != (unsigned long)bits) {
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
if ((err = prng_descriptor[wprng].add_entropy(buf, (unsigned long)bits, prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((err = prng_descriptor[wprng].ready(prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef LTC_CLEAN_STACK
|
||||
zeromem(buf, sizeof(buf));
|
||||
#endif
|
||||
return CRYPT_OK;
|
||||
}
|
||||
#endif /* #ifdef LTC_RNG_MAKE_PRNG */
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
249
thirdparty/libtomcrypt/prngs/sober128.c
vendored
Normal file
249
thirdparty/libtomcrypt/prngs/sober128.c
vendored
Normal file
@ -0,0 +1,249 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file prngs/sober128.c
|
||||
Implementation of SOBER-128 by Tom St Denis.
|
||||
Based on s128fast.c reference code supplied by Greg Rose of QUALCOMM.
|
||||
*/
|
||||
|
||||
#ifdef LTC_SOBER128
|
||||
|
||||
const struct ltc_prng_descriptor sober128_desc =
|
||||
{
|
||||
"sober128",
|
||||
40,
|
||||
&sober128_start,
|
||||
&sober128_add_entropy,
|
||||
&sober128_ready,
|
||||
&sober128_read,
|
||||
&sober128_done,
|
||||
&sober128_export,
|
||||
&sober128_import,
|
||||
&sober128_test
|
||||
};
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_start(prng_state *prng)
|
||||
{
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent));
|
||||
prng->sober128.idx = 0;
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
unsigned char buf[40];
|
||||
unsigned long i;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) {
|
||||
/* sober128_ready() was already called, do "rekey" operation */
|
||||
if ((err = sober128_stream_keystream(&prng->sober128.s, buf, sizeof(buf))) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
for(i = 0; i < inlen; i++) buf[i % sizeof(buf)] ^= in[i];
|
||||
/* key 32 bytes, 20 rounds */
|
||||
if ((err = sober128_stream_setup(&prng->sober128.s, buf, 32)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* iv 8 bytes */
|
||||
if ((err = sober128_stream_setiv(&prng->sober128.s, buf + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* clear KEY + IV */
|
||||
zeromem(buf, sizeof(buf));
|
||||
}
|
||||
else {
|
||||
/* sober128_ready() was not called yet, add entropy to ent buffer */
|
||||
while (inlen--) prng->sober128.ent[prng->sober128.idx++ % sizeof(prng->sober128.ent)] ^= *in++;
|
||||
}
|
||||
err = CRYPT_OK;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_ready(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (prng->ready) { err = CRYPT_OK; goto LBL_UNLOCK; }
|
||||
/* key 32 bytes, 20 rounds */
|
||||
if ((err = sober128_stream_setup(&prng->sober128.s, prng->sober128.ent, 32)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
/* iv 8 bytes */
|
||||
if ((err = sober128_stream_setiv(&prng->sober128.s, prng->sober128.ent + 32, 8)) != CRYPT_OK) goto LBL_UNLOCK;
|
||||
XMEMSET(&prng->sober128.ent, 0, sizeof(prng->sober128.ent));
|
||||
prng->sober128.idx = 0;
|
||||
prng->ready = 1;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long sober128_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
if (!prng->ready) { outlen = 0; goto LBL_UNLOCK; }
|
||||
if (sober128_stream_keystream(&prng->sober128.s, out, outlen) != CRYPT_OK) outlen = 0;
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_done(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
err = sober128_stream_done(&prng->sober128.s);
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
unsigned long len = sober128_desc.export_size;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (sober128_read(out, len, prng) != len) {
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
*outlen = len;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sober128_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
if (inlen < (unsigned long)sober128_desc.export_size) return CRYPT_INVALID_ARG;
|
||||
|
||||
if ((err = sober128_start(prng)) != CRYPT_OK) return err;
|
||||
if ((err = sober128_add_entropy(in, inlen, prng)) != CRYPT_OK) return err;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int sober128_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
prng_state st;
|
||||
unsigned char en[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
||||
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
|
||||
0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
|
||||
0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
|
||||
0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32 };
|
||||
unsigned char dmp[300];
|
||||
unsigned long dmplen = sizeof(dmp);
|
||||
unsigned char out[500];
|
||||
unsigned char t1[] = { 0x31, 0x82, 0xA7, 0xA5, 0x8B, 0xD7, 0xCB, 0x39, 0x86, 0x1A };
|
||||
unsigned char t2[] = { 0x6B, 0x43, 0x9E, 0xBC, 0xE7, 0x62, 0x9B, 0xE6, 0x9B, 0x83 };
|
||||
unsigned char t3[] = { 0x4A, 0x0E, 0x6C, 0xC1, 0xCF, 0xB4, 0x73, 0x49, 0x99, 0x05 };
|
||||
int err;
|
||||
|
||||
if ((err = sober128_start(&st)) != CRYPT_OK) return err;
|
||||
/* add entropy to uninitialized prng */
|
||||
if ((err = sober128_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if ((err = sober128_ready(&st)) != CRYPT_OK) return err;
|
||||
if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t1, sizeof(t1), "SOBER128-PRNG", 1)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
/* add entropy to already initialized prng */
|
||||
if ((err = sober128_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if ((err = sober128_export(dmp, &dmplen, &st)) != CRYPT_OK) return err;
|
||||
if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t2, sizeof(t2), "SOBER128-PRNG", 2)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = sober128_done(&st)) != CRYPT_OK) return err;
|
||||
if ((err = sober128_import(dmp, dmplen, &st)) != CRYPT_OK) return err;
|
||||
if ((err = sober128_ready(&st)) != CRYPT_OK) return err;
|
||||
if (sober128_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if (sober128_read(out, 10, &st) != 10) return CRYPT_ERROR_READPRNG; /* 10 bytes for testing */
|
||||
if (compare_testvector(out, 10, t3, sizeof(t3), "SOBER128-PRNG", 3)) return CRYPT_FAIL_TESTVECTOR;
|
||||
if ((err = sober128_done(&st)) != CRYPT_OK) return err;
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
161
thirdparty/libtomcrypt/prngs/sprng.c
vendored
Normal file
161
thirdparty/libtomcrypt/prngs/sprng.c
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file sprng.c
|
||||
Secure PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
/* A secure PRNG using the RNG functions. Basically this is a
|
||||
* wrapper that allows you to use a secure RNG as a PRNG
|
||||
* in the various other functions.
|
||||
*/
|
||||
|
||||
#ifdef LTC_SPRNG
|
||||
|
||||
const struct ltc_prng_descriptor sprng_desc =
|
||||
{
|
||||
"sprng", 0,
|
||||
&sprng_start,
|
||||
&sprng_add_entropy,
|
||||
&sprng_ready,
|
||||
&sprng_read,
|
||||
&sprng_done,
|
||||
&sprng_export,
|
||||
&sprng_import,
|
||||
&sprng_test
|
||||
};
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_start(prng_state *prng)
|
||||
{
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
LTC_UNUSED_PARAM(in);
|
||||
LTC_UNUSED_PARAM(inlen);
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_ready(prng_state *prng)
|
||||
{
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return rng_get_bytes(out, outlen, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_done(prng_state *prng)
|
||||
{
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_UNUSED_PARAM(out);
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
|
||||
*outlen = 0;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
LTC_UNUSED_PARAM(in);
|
||||
LTC_UNUSED_PARAM(inlen);
|
||||
LTC_UNUSED_PARAM(prng);
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int sprng_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
prng_state st;
|
||||
unsigned char en[] = { 0x01, 0x02, 0x03, 0x04 };
|
||||
unsigned char out[1000];
|
||||
int err;
|
||||
|
||||
if ((err = sprng_start(&st)) != CRYPT_OK) return err;
|
||||
if ((err = sprng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;
|
||||
if ((err = sprng_ready(&st)) != CRYPT_OK) return err;
|
||||
if (sprng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */
|
||||
if ((err = sprng_done(&st)) != CRYPT_OK) return err;
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
352
thirdparty/libtomcrypt/prngs/yarrow.c
vendored
Normal file
352
thirdparty/libtomcrypt/prngs/yarrow.c
vendored
Normal file
@ -0,0 +1,352 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
||||
*
|
||||
* LibTomCrypt is a library that provides various cryptographic
|
||||
* algorithms in a highly modular and flexible manner.
|
||||
*
|
||||
* The library is free for all purposes without any express
|
||||
* guarantee it works.
|
||||
*/
|
||||
#include "tomcrypt.h"
|
||||
|
||||
/**
|
||||
@file yarrow.c
|
||||
Yarrow PRNG, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_YARROW
|
||||
|
||||
const struct ltc_prng_descriptor yarrow_desc =
|
||||
{
|
||||
"yarrow", 64,
|
||||
&yarrow_start,
|
||||
&yarrow_add_entropy,
|
||||
&yarrow_ready,
|
||||
&yarrow_read,
|
||||
&yarrow_done,
|
||||
&yarrow_export,
|
||||
&yarrow_import,
|
||||
&yarrow_test
|
||||
};
|
||||
|
||||
/**
|
||||
Start the PRNG
|
||||
@param prng [out] The PRNG state to initialize
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_start(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
prng->ready = 0;
|
||||
|
||||
/* these are the default hash/cipher combo used */
|
||||
#ifdef LTC_RIJNDAEL
|
||||
#if LTC_YARROW_AES==0
|
||||
prng->yarrow.cipher = register_cipher(&rijndael_enc_desc);
|
||||
#elif LTC_YARROW_AES==1
|
||||
prng->yarrow.cipher = register_cipher(&aes_enc_desc);
|
||||
#elif LTC_YARROW_AES==2
|
||||
prng->yarrow.cipher = register_cipher(&rijndael_desc);
|
||||
#elif LTC_YARROW_AES==3
|
||||
prng->yarrow.cipher = register_cipher(&aes_desc);
|
||||
#endif
|
||||
#elif defined(LTC_BLOWFISH)
|
||||
prng->yarrow.cipher = register_cipher(&blowfish_desc);
|
||||
#elif defined(LTC_TWOFISH)
|
||||
prng->yarrow.cipher = register_cipher(&twofish_desc);
|
||||
#elif defined(LTC_RC6)
|
||||
prng->yarrow.cipher = register_cipher(&rc6_desc);
|
||||
#elif defined(LTC_RC5)
|
||||
prng->yarrow.cipher = register_cipher(&rc5_desc);
|
||||
#elif defined(LTC_SAFERP)
|
||||
prng->yarrow.cipher = register_cipher(&saferp_desc);
|
||||
#elif defined(LTC_RC2)
|
||||
prng->yarrow.cipher = register_cipher(&rc2_desc);
|
||||
#elif defined(LTC_NOEKEON)
|
||||
prng->yarrow.cipher = register_cipher(&noekeon_desc);
|
||||
#elif defined(LTC_ANUBIS)
|
||||
prng->yarrow.cipher = register_cipher(&anubis_desc);
|
||||
#elif defined(LTC_KSEED)
|
||||
prng->yarrow.cipher = register_cipher(&kseed_desc);
|
||||
#elif defined(LTC_KHAZAD)
|
||||
prng->yarrow.cipher = register_cipher(&khazad_desc);
|
||||
#elif defined(LTC_CAST5)
|
||||
prng->yarrow.cipher = register_cipher(&cast5_desc);
|
||||
#elif defined(LTC_XTEA)
|
||||
prng->yarrow.cipher = register_cipher(&xtea_desc);
|
||||
#elif defined(LTC_SAFER)
|
||||
prng->yarrow.cipher = register_cipher(&safer_sk128_desc);
|
||||
#elif defined(LTC_DES)
|
||||
prng->yarrow.cipher = register_cipher(&des3_desc);
|
||||
#else
|
||||
#error LTC_YARROW needs at least one CIPHER
|
||||
#endif
|
||||
if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef LTC_SHA256
|
||||
prng->yarrow.hash = register_hash(&sha256_desc);
|
||||
#elif defined(LTC_SHA512)
|
||||
prng->yarrow.hash = register_hash(&sha512_desc);
|
||||
#elif defined(LTC_TIGER)
|
||||
prng->yarrow.hash = register_hash(&tiger_desc);
|
||||
#elif defined(LTC_SHA1)
|
||||
prng->yarrow.hash = register_hash(&sha1_desc);
|
||||
#elif defined(LTC_RIPEMD320)
|
||||
prng->yarrow.hash = register_hash(&rmd320_desc);
|
||||
#elif defined(LTC_RIPEMD256)
|
||||
prng->yarrow.hash = register_hash(&rmd256_desc);
|
||||
#elif defined(LTC_RIPEMD160)
|
||||
prng->yarrow.hash = register_hash(&rmd160_desc);
|
||||
#elif defined(LTC_RIPEMD128)
|
||||
prng->yarrow.hash = register_hash(&rmd128_desc);
|
||||
#elif defined(LTC_MD5)
|
||||
prng->yarrow.hash = register_hash(&md5_desc);
|
||||
#elif defined(LTC_MD4)
|
||||
prng->yarrow.hash = register_hash(&md4_desc);
|
||||
#elif defined(LTC_MD2)
|
||||
prng->yarrow.hash = register_hash(&md2_desc);
|
||||
#elif defined(LTC_WHIRLPOOL)
|
||||
prng->yarrow.hash = register_hash(&whirlpool_desc);
|
||||
#else
|
||||
#error LTC_YARROW needs at least one HASH
|
||||
#endif
|
||||
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* zero the memory used */
|
||||
zeromem(prng->yarrow.pool, sizeof(prng->yarrow.pool));
|
||||
LTC_MUTEX_INIT(&prng->lock)
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Add entropy to the PRNG state
|
||||
@param in The data to add
|
||||
@param inlen Length of the data to add
|
||||
@param prng PRNG state to update
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
hash_state md;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(inlen > 0);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* start the hash */
|
||||
if ((err = hash_descriptor[prng->yarrow.hash].init(&md)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* hash the current pool */
|
||||
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, prng->yarrow.pool,
|
||||
hash_descriptor[prng->yarrow.hash].hashsize)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* add the new entropy */
|
||||
if ((err = hash_descriptor[prng->yarrow.hash].process(&md, in, inlen)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* store result */
|
||||
err = hash_descriptor[prng->yarrow.hash].done(&md, prng->yarrow.pool);
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Make the PRNG ready to read from
|
||||
@param prng The PRNG to make active
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_ready(prng_state *prng)
|
||||
{
|
||||
int ks, err;
|
||||
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if ((err = hash_is_valid(prng->yarrow.hash)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
if ((err = cipher_is_valid(prng->yarrow.cipher)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* setup CTR mode using the "pool" as the key */
|
||||
ks = (int)hash_descriptor[prng->yarrow.hash].hashsize;
|
||||
if ((err = cipher_descriptor[prng->yarrow.cipher].keysize(&ks)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
if ((err = ctr_start(prng->yarrow.cipher, /* what cipher to use */
|
||||
prng->yarrow.pool, /* IV */
|
||||
prng->yarrow.pool, ks, /* KEY and key size */
|
||||
0, /* number of rounds */
|
||||
CTR_COUNTER_LITTLE_ENDIAN, /* little endian counter */
|
||||
&prng->yarrow.ctr)) != CRYPT_OK) {
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
prng->ready = 1;
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Read from the PRNG
|
||||
@param out Destination
|
||||
@param outlen Length of output
|
||||
@param prng The active PRNG to read from
|
||||
@return Number of octets read
|
||||
*/
|
||||
unsigned long yarrow_read(unsigned char *out, unsigned long outlen, prng_state *prng)
|
||||
{
|
||||
if (outlen == 0 || prng == NULL || out == NULL) return 0;
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
|
||||
if (!prng->ready) {
|
||||
outlen = 0;
|
||||
goto LBL_UNLOCK;
|
||||
}
|
||||
|
||||
/* put out in predictable state first */
|
||||
zeromem(out, outlen);
|
||||
|
||||
/* now randomize it */
|
||||
if (ctr_encrypt(out, out, outlen, &prng->yarrow.ctr) != CRYPT_OK) {
|
||||
outlen = 0;
|
||||
}
|
||||
|
||||
LBL_UNLOCK:
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/**
|
||||
Terminate the PRNG
|
||||
@param prng The PRNG to terminate
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_done(prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
LTC_MUTEX_LOCK(&prng->lock);
|
||||
prng->ready = 0;
|
||||
|
||||
/* call cipher done when we invent one ;-) */
|
||||
|
||||
/* we invented one */
|
||||
err = ctr_done(&prng->yarrow.ctr);
|
||||
|
||||
LTC_MUTEX_UNLOCK(&prng->lock);
|
||||
LTC_MUTEX_DESTROY(&prng->lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
Export the PRNG state
|
||||
@param out [out] Destination
|
||||
@param outlen [in/out] Max size and resulting size of the state
|
||||
@param prng The PRNG to export
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
|
||||
{
|
||||
unsigned long len = yarrow_desc.export_size;
|
||||
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
|
||||
if (*outlen < len) {
|
||||
*outlen = len;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (yarrow_read(out, len, prng) != len) {
|
||||
return CRYPT_ERROR_READPRNG;
|
||||
}
|
||||
|
||||
*outlen = len;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
Import a PRNG state
|
||||
@param in The PRNG state
|
||||
@param inlen Size of the state
|
||||
@param prng The PRNG to import
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int yarrow_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
|
||||
{
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(prng != NULL);
|
||||
if (inlen < (unsigned long)yarrow_desc.export_size) return CRYPT_INVALID_ARG;
|
||||
|
||||
if ((err = yarrow_start(prng)) != CRYPT_OK) return err;
|
||||
if ((err = yarrow_add_entropy(in, inlen, prng)) != CRYPT_OK) return err;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
PRNG self-test
|
||||
@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
|
||||
*/
|
||||
int yarrow_test(void)
|
||||
{
|
||||
#ifndef LTC_TEST
|
||||
return CRYPT_NOP;
|
||||
#else
|
||||
int err;
|
||||
prng_state prng;
|
||||
|
||||
if ((err = yarrow_start(&prng)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* now let's test the hash/cipher that was chosen */
|
||||
if (cipher_descriptor[prng.yarrow.cipher].test &&
|
||||
((err = cipher_descriptor[prng.yarrow.cipher].test()) != CRYPT_OK)) {
|
||||
return err;
|
||||
}
|
||||
if (hash_descriptor[prng.yarrow.hash].test &&
|
||||
((err = hash_descriptor[prng.yarrow.hash].test()) != CRYPT_OK)) {
|
||||
return err;
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* ref: HEAD -> master, tag: v1.18.2 */
|
||||
/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
|
||||
/* commit time: 2018-07-01 22:49:01 +0200 */
|
Reference in New Issue
Block a user