1 // SPDX-License-Identifier: BSD-2-Clause 2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis 3 * 4 * LibTomCrypt is a library that provides various cryptographic 5 * algorithms in a highly modular and flexible manner. 6 * 7 * The library is free for all purposes without any express 8 * guarantee it works. 9 */ 10 #include "tomcrypt_private.h" 11 12 /** 13 @file sprng.c 14 Secure PRNG, Tom St Denis 15 */ 16 17 /* A secure PRNG using the RNG functions. Basically this is a 18 * wrapper that allows you to use a secure RNG as a PRNG 19 * in the various other functions. 20 */ 21 22 #ifdef LTC_SPRNG 23 24 const struct ltc_prng_descriptor sprng_desc = 25 { 26 "sprng", 0, 27 &sprng_start, 28 &sprng_add_entropy, 29 &sprng_ready, 30 &sprng_read, 31 &sprng_done, 32 &sprng_export, 33 &sprng_import, 34 &sprng_test 35 }; 36 37 /** 38 Start the PRNG 39 @param prng [out] The PRNG state to initialize 40 @return CRYPT_OK if successful 41 */ 42 int sprng_start(prng_state *prng) 43 { 44 LTC_UNUSED_PARAM(prng); 45 return CRYPT_OK; 46 } 47 48 /** 49 Add entropy to the PRNG state 50 @param in The data to add 51 @param inlen Length of the data to add 52 @param prng PRNG state to update 53 @return CRYPT_OK if successful 54 */ 55 int sprng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng) 56 { 57 LTC_UNUSED_PARAM(in); 58 LTC_UNUSED_PARAM(inlen); 59 LTC_UNUSED_PARAM(prng); 60 return CRYPT_OK; 61 } 62 63 /** 64 Make the PRNG ready to read from 65 @param prng The PRNG to make active 66 @return CRYPT_OK if successful 67 */ 68 int sprng_ready(prng_state *prng) 69 { 70 LTC_UNUSED_PARAM(prng); 71 return CRYPT_OK; 72 } 73 74 /** 75 Read from the PRNG 76 @param out Destination 77 @param outlen Length of output 78 @param prng The active PRNG to read from 79 @return Number of octets read 80 */ 81 unsigned long sprng_read(unsigned char *out, unsigned long outlen, prng_state *prng) 82 { 83 LTC_ARGCHK(out != NULL); 84 LTC_UNUSED_PARAM(prng); 85 return rng_get_bytes(out, outlen, NULL); 86 } 87 88 /** 89 Terminate the PRNG 90 @param prng The PRNG to terminate 91 @return CRYPT_OK if successful 92 */ 93 int sprng_done(prng_state *prng) 94 { 95 LTC_UNUSED_PARAM(prng); 96 return CRYPT_OK; 97 } 98 99 /** 100 Export the PRNG state 101 @param out [out] Destination 102 @param outlen [in/out] Max size and resulting size of the state 103 @param prng The PRNG to export 104 @return CRYPT_OK if successful 105 */ 106 /* NOLINTNEXTLINE(readability-non-const-parameter) - silence clang-tidy warning */ 107 int sprng_export(unsigned char *out, unsigned long *outlen, prng_state *prng) 108 { 109 LTC_ARGCHK(outlen != NULL); 110 LTC_UNUSED_PARAM(out); 111 LTC_UNUSED_PARAM(prng); 112 113 *outlen = 0; 114 return CRYPT_OK; 115 } 116 117 /** 118 Import a PRNG state 119 @param in The PRNG state 120 @param inlen Size of the state 121 @param prng The PRNG to import 122 @return CRYPT_OK if successful 123 */ 124 int sprng_import(const unsigned char *in, unsigned long inlen, prng_state *prng) 125 { 126 LTC_UNUSED_PARAM(in); 127 LTC_UNUSED_PARAM(inlen); 128 LTC_UNUSED_PARAM(prng); 129 return CRYPT_OK; 130 } 131 132 /** 133 PRNG self-test 134 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled 135 */ 136 int sprng_test(void) 137 { 138 #ifndef LTC_TEST 139 return CRYPT_NOP; 140 #else 141 prng_state st; 142 unsigned char en[] = { 0x01, 0x02, 0x03, 0x04 }; 143 unsigned char out[1000]; 144 int err; 145 146 if ((err = sprng_start(&st)) != CRYPT_OK) return err; 147 if ((err = sprng_add_entropy(en, sizeof(en), &st)) != CRYPT_OK) return err; 148 if ((err = sprng_ready(&st)) != CRYPT_OK) return err; 149 if (sprng_read(out, 500, &st) != 500) return CRYPT_ERROR_READPRNG; /* skip 500 bytes */ 150 if ((err = sprng_done(&st)) != CRYPT_OK) return err; 151 152 return CRYPT_OK; 153 #endif 154 } 155 156 #endif 157 158 159 160 161 /* ref: $Format:%D$ */ 162 /* git commit: $Format:%H$ */ 163 /* commit time: $Format:%ai$ */ 164