- C2C0CAF7B1DF0AA3811A718F544A8E2B5AA1909F97876E4F4070D6B01DC55F444A71837E746D58DC9211FCC4E4033E01582EE4B14C9DCA898BB9B838CC022347
+ 234021F88C073C4C0E719125E55546A98351A2CF0DF584701FF19A9E8D9EA5B134FD9FDBCC24CD412C56802099A2929B608C41D3ACF7E8FB545EB1C04F8BDF9F
eucrypt/smg_rsa/truerandom.c
(89 . 3)(89 . 88)
190 return nread;
191 }
192
193 int rng_dirty_float(float *n) {
194 int status; /* for aborting in case of error */
195 uint32_t r; /* a random value on 32 bits */
196 uint32_t maxval = 0xffffffff; /* maximum value on 32 bits */
197
198 /* obtain a random number on 32 bits using ENTROPY_SOURCE */
199 status = rng_uint32( &r );
200 if ( status < 0 )
201 return status;
202
203 /* calculate and assign the floating-point random value as (r*1.0)/max val */
204 /* multiplication by 1.0 IS NEEDED to do float division rather than int div*/
205 *n = ( r * 1.0 ) / maxval;
206
207 return 1;
208 }
209
210 int rng_float_754_1985(float *n) {
211 /* Single float ieee 754/1985 has 23 bits that can be set for the mantissa
212 * (and one implicit bit=1).
213 * Full single float ieee 754/1985 representation takes 4 octets in total.
214 */
215 int noctets = 4; /* number of octets to read from ENTROPY_SOURCE */
216 int nread; /* number of octets *read* from ENTROPY_SOURCE */
217 unsigned char bits[ noctets ]; /* the random bits from ENTROPY_SOURCE */
218 int oSignExp, oExpM;/* offsets for sign+exponent octet, exponent+mantissa*/
219
220 /* obtain random bits */
221 nread = get_random_octets( noctets, bits );
222
223 if (nread != noctets )
224 return -1; /* something wrong at reading from ENTROPY_SOURCE, abort */
225
226 /* set offsets for bit diddling depending on endianness of iron */
227 if (is_bigendian()) {
228 oSignExp = 0;
229 oExpM = 1;
230 }
231 else {
232 oSignExp = 3;
233 oExpM = 2;
234 }
235
236 /* set sign=0; exponent=127; explicit mantissa = random bits (23 bits) */
237 *(bits+oExpM) = *(bits+2) | 0x80; /* one bit of exponent set */
238 *(bits+oSignExp) = 0x3f; /* sign=0; exponent bits for 127 */
239
240 /* now copy the bits to the result var (i.e. as a float's representation */
241 memcpy( n, bits, noctets );
242 return 1;
243 }
244
245 int rng_uint32( uint32_t *n ) {
246 int noctets = 4; /* 32 bits aka 4 octets to read from ENTROPY_SOURCE */
247 int nread; /* the number of octets read from ENTROPY_SOURCE */
248 unsigned char bits[ noctets ]; /* for storing the bits from ENTROPY_SOURCE */
249
250 /* read random 32 bits from ENTROPY_SOURCE */
251 nread = get_random_octets( noctets, bits );
252 if ( nread != noctets )
253 return -1;
254
255 /* copy the random bits to n, to be interpreted as uint32 */
256 /* endianness is irrelevant here - the bits are random anyway */
257 memcpy( n, bits, noctets );
258
259 return 1;
260 }
261
262 int rng_uint64( uint64_t *n ) {
263 int noctets = 8; /* 64 bits aka 8 octets to read from ENTROPY_SOURCE */
264 int nread; /* the number of octets read from ENTROPY_SOURCE */
265 unsigned char bits[ noctets ]; /* for storing the bits from ENTROPY_SOURCE */
266
267 /* read random 64 bits from ENTROPY_SOURCE */
268 nread = get_random_octets( noctets, bits );
269 if ( nread != noctets )
270 return -1;
271
272 /* copy the random bits to n, to be interpreted as uint64 */
273 /* endianness is irrelevant here - the bits are random anyway */
274 memcpy( n, bits, noctets );
275
276 return 1;
277 }