1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
5 #include "tomcrypt_test.h"
9 NO PRNG, Steffen Jaeckel
16 struct ltc_prng_descriptor desc
;
18 unsigned char entropy
[1024];
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
));
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
);
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
);
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
;
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
);
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
);
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
);
138 @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
140 static int no_prng_test(void)
145 static const struct ltc_prng_descriptor no_prng_desc
=
149 &no_prng_add_entropy
,
158 struct ltc_prng_descriptor
* no_prng_desc_get(void)
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)) {
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
));