/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/8#include "tomcrypt.h"910/**11@file sprng.c12Secure PRNG, Tom St Denis13*/1415/* A secure PRNG using the RNG functions. Basically this is a16* wrapper that allows you to use a secure RNG as a PRNG17* in the various other functions.18*/1920#ifdef LTC_SPRNG2122const struct ltc_prng_descriptor sprng_desc =23{24"sprng", 0,25&sprng_start,26&sprng_add_entropy,27&sprng_ready,28&sprng_read,29&sprng_done,30&sprng_export,31&sprng_import,32&sprng_test33};3435/**36Start the PRNG37@param prng [out] The PRNG state to initialize38@return CRYPT_OK if successful39*/40int sprng_start(prng_state *prng)41{42LTC_UNUSED_PARAM(prng);43return CRYPT_OK;44}4546/**47Add entropy to the PRNG state48@param in The data to add49@param inlen Length of the data to add50@param prng PRNG state to update51@return CRYPT_OK if successful52*/53int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)54{55LTC_UNUSED_PARAM(in);56LTC_UNUSED_PARAM(inlen);57LTC_UNUSED_PARAM(prng);58return CRYPT_OK;59}6061/**62Make the PRNG ready to read from63@param prng The PRNG to make active64@return CRYPT_OK if successful65*/66int sprng_ready(prng_state *prng)67{68LTC_UNUSED_PARAM(prng);69return CRYPT_OK;70}7172/**73Read from the PRNG74@param out Destination75@param outlen Length of output76@param prng The active PRNG to read from77@return Number of octets read78*/79unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng)80{81LTC_ARGCHK(out != NULL);82LTC_UNUSED_PARAM(prng);83return rng_get_bytes(out, outlen, NULL);84}8586/**87Terminate the PRNG88@param prng The PRNG to terminate89@return CRYPT_OK if successful90*/91int sprng_done(prng_state *prng)92{93LTC_UNUSED_PARAM(prng);94return CRYPT_OK;95}9697/**98Export the PRNG state99@param out [out] Destination100@param outlen [in/out] Max size and resulting size of the state101@param prng The PRNG to export102@return CRYPT_OK if successful103*/104int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)105{106LTC_ARGCHK(outlen != NULL);107LTC_UNUSED_PARAM(out);108LTC_UNUSED_PARAM(prng);109110*outlen = 0;111return CRYPT_OK;112}113114/**115Import a PRNG state116@param in The PRNG state117@param inlen Size of the state118@param prng The PRNG to import119@return CRYPT_OK if successful120*/121int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)122{123LTC_UNUSED_PARAM(in);124LTC_UNUSED_PARAM(inlen);125LTC_UNUSED_PARAM(prng);126return CRYPT_OK;127}128129/**130PRNG self-test131@return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled132*/133int sprng_test(void)134{135#ifndef LTC_TEST136return CRYPT_NOP;137#else138prng_state st;139unsigned char en[] = { 0x01, 0x02, 0x03, 0x04 };140unsigned char out[1000];141int err;142143if ((err = sprng_start(&st)) != CRYPT_OK) return err;144if ((err = sprng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err;145if ((err = sprng_ready(&st)) != CRYPT_OK) return err;146if (sprng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */147if ((err = sprng_done(&st)) != CRYPT_OK) return err;148149return CRYPT_OK;150#endif151}152153#endif154155156