Merge pull request #678 from libtom/some-improvements
[libtomcrypt.git] / tests / no_prng.c
blob81d1adbf9c873407d7956707466bb2f64c3332ac
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
4 #include "tomcrypt.h"
5 #include "tomcrypt_test.h"
7 /**
8 @file no_prng.c
9 NO PRNG, Steffen Jaeckel
12 #ifdef LTC_PKCS_1
14 typedef struct
16 struct ltc_prng_descriptor desc;
17 char name[64];
18 unsigned char entropy[1024];
19 unsigned long len;
20 unsigned long offset;
21 } no_prng_desc_t;
23 /**
24 Start the PRNG
25 @param prng [out] The PRNG state to initialize
26 @return CRYPT_OK if successful
28 static int no_prng_start(prng_state *prng)
30 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
31 LTC_ARGCHK(no_prng != NULL);
32 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
33 no_prng->len = 0;
34 no_prng->offset = 0;
36 return CRYPT_OK;
39 /**
40 Add entropy to the PRNG state
41 @param in The data to add
42 @param inlen Length of the data to add
43 @param prng PRNG state to update
44 @return CRYPT_OK if successful
46 static int no_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
48 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
49 LTC_ARGCHK(no_prng != NULL);
50 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
51 LTC_ARGCHK(in != NULL);
52 LTC_ARGCHK(inlen <= sizeof(no_prng->entropy));
54 no_prng->len = MIN(inlen, sizeof(no_prng->entropy));
55 memcpy(no_prng->entropy, in, no_prng->len);
56 no_prng->offset = 0;
58 return CRYPT_OK;
62 /**
63 Make the PRNG ready to read from
64 @param prng The PRNG to make active
65 @return CRYPT_OK if successful
67 static int no_prng_ready(prng_state *prng)
69 LTC_ARGCHK(prng != NULL);
71 return CRYPT_OK;
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
81 static unsigned long no_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
83 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
84 LTC_ARGCHK(no_prng != NULL);
85 LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
86 LTC_ARGCHK(out != NULL);
88 outlen = MIN(outlen, no_prng->len - no_prng->offset);
89 memcpy(out, &no_prng->entropy[no_prng->offset], outlen);
90 no_prng->offset += outlen;
92 return outlen;
95 /**
96 Terminate the PRNG
97 @param prng The PRNG to terminate
98 @return CRYPT_OK if successful
100 static int no_prng_done(prng_state *prng)
102 LTC_UNUSED_PARAM(prng);
103 return CRYPT_OK;
107 Export the PRNG state
108 @param out [out] Destination
109 @param outlen [in/out] Max size and resulting size of the state
110 @param prng The PRNG to export
111 @return CRYPT_OK if successful
113 static int no_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
115 LTC_UNUSED_PARAM(out);
116 LTC_UNUSED_PARAM(outlen);
117 LTC_UNUSED_PARAM(prng);
118 return CRYPT_OK;
122 Import a PRNG state
123 @param in The PRNG state
124 @param inlen Size of the state
125 @param prng The PRNG to import
126 @return CRYPT_OK if successful
128 static int no_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
130 LTC_UNUSED_PARAM(in);
131 LTC_UNUSED_PARAM(inlen);
132 LTC_UNUSED_PARAM(prng);
133 return CRYPT_OK;
137 PRNG self-test
138 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
140 static int no_prng_test(void)
142 return CRYPT_OK;
145 static const struct ltc_prng_descriptor no_prng_desc =
147 NULL, 0,
148 &no_prng_start,
149 &no_prng_add_entropy,
150 &no_prng_ready,
151 &no_prng_read,
152 &no_prng_done,
153 &no_prng_export,
154 &no_prng_import,
155 &no_prng_test
158 struct ltc_prng_descriptor* no_prng_desc_get(void)
160 int ret;
161 no_prng_desc_t* no_prng = XMALLOC(sizeof(*no_prng));
162 if (no_prng == NULL) return NULL;
163 XMEMCPY(&no_prng->desc, &no_prng_desc, sizeof(no_prng_desc));
164 ret = snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng);
165 if((ret >= (int)sizeof(no_prng->name)) || (ret == -1)) {
166 XFREE(no_prng);
167 return NULL;
169 no_prng->desc.name = no_prng->name;
170 return &no_prng->desc;
173 void no_prng_desc_free(struct ltc_prng_descriptor* prng)
175 no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
176 LTC_ARGCHKVD(no_prng != NULL);
177 LTC_ARGCHKVD(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
178 XFREE(no_prng);
181 #endif