Debian Codegraph @

libdieharder/bauer/rng_aes.c

raw
1 /* rng_es
2 *
3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 James Theiler, Brian Gough
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <dieharder/libdieharder.h>
21 #include "rijndael-alg-fst.h"
22
23 // State blocks = 1 for weakest form
24 #define STATE_BLOCKS (1) /* MUST be 1 for AES */
25 #define BLOCKS_SIZE (16 * STATE_BLOCKS)
26 #define NR 10
27
28 /*
29 * This is a wrapping of the AES algorithm as a generator
30 */
31
32 typedef struct {
33 u32 rk[4*(NR + 1)];
34 unsigned char block[BLOCKS_SIZE];
35 short int pos;
36 } AES_state_t;
37
38 unsigned long int aes_get (void *vstate);
39 double aes_get_double (void *vstate);
40 void aes_set (void *vstate, unsigned long int s);
41
42 unsigned long int aes_get (void *vstate) {
43 AES_state_t *state = vstate;
44 unsigned int ret;
45
46 if (state->pos + sizeof(ret) > BLOCKS_SIZE) {
47 rijndaelEncrypt(state->rk, NR, state->block, state->block);
48 state->pos = 0;
49 }
50
51 ret = *((unsigned int *) (state->block + state->pos));
52 state->pos += sizeof(ret);
53
54 // ret &= 0x7fffffff;
55 return(ret);
56 }
57
58
59 double aes_get_double (void *vstate) {
60 // return aes_get_long(vstate) / (double) ULONG_MAX;
61 return (double) aes_get(vstate) / (double) (UINT_MAX >> 0);
62 }
63
64 void aes_set (void *vstate, unsigned long int s) {
65 AES_state_t *state = vstate;
66 int i;
67 u8 key[16];
68
69 memset(state, 0, sizeof(*state)); // Zero pos and block
70
71 for (i = 0; i < 16; i++) {
72 key[i] = (u8) (112 + i + (s >> i));
73 }
74 rijndaelKeySetupEnc(state->rk, key, 128);
75 rijndaelEncrypt(state->rk, NR, state->block, state->block);
76
77 return;
78 }
79
80 static const gsl_rng_type aes_type = {
81 "AES_OFB", /* name */
82 UINT_MAX>>0, // UINT_MAX, /* RAND_MAX */
83 0, /* RAND_MIN */
84 sizeof (AES_state_t),
85 &aes_set,
86 &aes_get,
87 &aes_get_double};
88
89 const gsl_rng_type *gsl_rng_aes = &aes_type;
90