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