File: rnd.c

package info (click to toggle)
apg 2.2.3.dfsg.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 696 kB
  • ctags: 731
  • sloc: ansic: 4,867; php: 744; sh: 186; makefile: 140; perl: 9
file content (221 lines) | stat: -rw-r--r-- 7,152 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
** Copyright (c) 1999, 2000, 2001, 2002, 2003
** Adel I. Mirzazhanov. All rights reserved
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
** 
**     1.Redistributions of source code must retain the above copyright notice,
**       this list of conditions and the following disclaimer. 
**     2.Redistributions in binary form must reproduce the above copyright
**       notice, this list of conditions and the following disclaimer in the
**       documentation and/or other materials provided with the distribution. 
**     3.The name of the author may not be used to endorse or promote products
**       derived from this software without specific prior written permission. 
** 		  
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND ANY EXPRESS
** OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN  NO  EVENT  SHALL THE AUTHOR BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO,  PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES;  LOSS OF USE,  DATA,  OR  PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER  CAUSED  AND  ON  ANY  THEORY OF LIABILITY,
** WHETHER  IN  CONTRACT,   STRICT   LIABILITY,  OR  TORT  (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
#include <strings.h>
#endif
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include "rnd.h"

#ifndef APG_USE_SHA 
#  include "./cast/cast.h"
#else /* APG_USE_SHA */
#  include "./sha/sha.h"
#endif /* APG_USE_SHA */

UINT32 __rnd_seed[2]; /* Random Seed 2*32=64 */

/*
** randint(int n) - Produces a Random number from 0 to n-1.
** INPUT:
**   int - limit
** OUTPUT:
**   UINT - pandom number.
** NOTES:
**   none.
*/
UINT
randint(int n)
{
#ifndef APG_USE_SHA
 return ( (UINT)( x917cast_rnd() % (UINT32)n ) );
#else /* APG_USE_SHA */
 return ( (UINT)( x917sha1_rnd() % (UINT32)n ) );
#endif /* APG_USE_SHA */
}

#ifndef APG_USE_SHA
/*
** ANSI X9.17 pseudorandom generator that uses CAST algorithm instead of DES
** m = 1
** INPUT:
**   none.
** OUTPUT:
**   UINT32 - random number.
** NOTES:
**   none.
*/
UINT32
x917cast_rnd (void)
{
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
 struct timeval local_time; 
#else
 clock_t local_time[2]; /* clock ticks for win32 */
#endif
 UINT32 I[2] = {0L,0L};
 UINT32 I_plus_s[2] = {0L,0L};
 UINT32 Xi[2] = {0L,0L};
 UINT32 Xi_plus_I[2] = {0L,0L};
 cast_key ky;
 
/**********************************************************************
* ENCRYPTION KEY HEX : 0x000102030405060708090A0B0C0D0E0F (128-bit)   *
* YOU CAN CHANGE IT IF YOU WANT                                       *
**********************************************************************/
u8 ro_key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
/**********************************************************************
* ENCRYPTION KEY HEX : 0x000102030405060708090A0B0C0D0E0F (128-bit)   *
* YOU CAN CHANGE IT IF YOU WANT                                       *
**********************************************************************/
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
 (void) gettimeofday (&local_time, 0);
#else
 local_time[0] = clock();
 local_time[1] = clock();
#endif
 cast_setkey(&ky, (u8*)&ro_key[0], 16);
 cast_encrypt (&ky, (u8 *)&local_time, (u8*)&I[0]);            /* I=Ek(D), D-time  */
 I_plus_s[0] = I[0] ^ __rnd_seed[0];                           /* I0 (+) s0        */
 I_plus_s[1] = I[1] ^ __rnd_seed[1];                           /* I1 (+) s1        */
 cast_encrypt (&ky, (u8 *)&I_plus_s[0], (u8*)&Xi[0]);          /* Xi=Ek( I (+) s ) */
 Xi_plus_I[0] = Xi[0] ^ I[0];                                  /* Xi0 (+) I0       */
 Xi_plus_I[1] = Xi[1] ^ I[1];                                  /* Xi1 (+) I1       */
 cast_encrypt (&ky, (u8 *)&Xi_plus_I[0], (u8*)&__rnd_seed[0]); /* s=Ek( Xi (+) I ) */
 return (Xi[0]);
}
#else /* APG_USE_SHA */
/*
** ANSI X9.17 pseudorandom generator that uses SHA1 algorithm instead of DES
** m=1
** INPUT:
**   none.
** OUTPUT:
**   UINT32 - random number.
** NOTES:
**   none.
*/
UINT32
x917sha1_rnd (void)
{
 struct timeval local_time;
 UINT32 I[2] = {0L,0L};
 UINT32 I_plus_s[2] = {0L,0L};
 UINT32 Xi[2] = {0L,0L};
 UINT32 Xi_plus_I[2] = {0L,0L};

 BYTE hash [SHA_DIGESTSIZE];
 apg_SHA_INFO shaInfo;

 (void) gettimeofday (&local_time, 0);
 apg_shaInit ( &shaInfo );
 apg_shaUpdate ( &shaInfo, (BYTE *)&local_time, 8);
 apg_shaFinal ( &shaInfo, hash );
 (void)memcpy ( (void *)&I[0], (void *)&hash[0], sizeof(I));
 I_plus_s[0] = I[0] ^ __rnd_seed[0];                           /* I0 (+) s0        */
 I_plus_s[1] = I[1] ^ __rnd_seed[1];                           /* I1 (+) s1        */

 apg_shaInit(&shaInfo);
 apg_shaUpdate( &shaInfo, (BYTE *)&I_plus_s, 8);
 apg_shaFinal( &shaInfo, hash );
 (void)memcpy ( (void *)&Xi[0], (void *)&hash[0], sizeof(Xi));        /* Xi=Ek( I (+) s ) */

 Xi_plus_I[0] = Xi[0] ^ I[0];                                  /* Xi0 (+) I0       */
 Xi_plus_I[1] = Xi[1] ^ I[1];                                  /* Xi1 (+) I1       */

 apg_shaInit(&shaInfo);
 apg_shaUpdate( &shaInfo, (BYTE *)&Xi_plus_I, 8);
 apg_shaFinal(&shaInfo, hash);
 (void)memcpy ( (void *)&__rnd_seed[0], (void *)&hash[0],
         sizeof(__rnd_seed));                                  /* s=Ek( Xi (+) I ) */
 return (Xi[0]);
}
#endif /* APG_USE_SHA */
/*
** x917_setseed (UINT32 seed) - Initializes seed
** INPUT:
**   UINT32 - seed value
**   int - quiet mode flag
** OUTPUT:
**   none.
** NOTES:
**   none.
*/
void
x917_setseed (UINT32 seed, int quiet)
{
 int fd;
 UINT32 drs[2];
 UINT32 pid = 0;

 pid = (UINT32)getpid();
 
 /* NOTE: this function intentionally does not use
  *       the regular <stdio> API because fread may
  *       read much more data than requested (up to
  *       4K), which is extremely bad with /dev/random
  */

 if ( (fd = open(APG_DEVRANDOM, O_RDONLY)) != -1)
  {
   read(fd, (void *)&drs[0], 8);
   __rnd_seed[0] = seed ^ drs[0];
   __rnd_seed[1] = seed ^ drs[1];
   close(fd);
  }
 else if ( (fd = open(APG_DEVURANDOM, O_RDONLY)) != -1)
  {
   read(fd, &drs[0], 8);
   __rnd_seed[0] = seed ^ drs[0];
   __rnd_seed[1] = seed ^ drs[1];
   close(fd);
  }
 else
  {
#ifndef CLISERV
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
   if (quiet != TRUE)
    {
     fprintf(stderr,"CAN NOT USE RANDOM DEVICE TO GENERATE RANDOM SEED\n");
     fprintf(stderr,"USING LOCAL TIME AND PID FOR SEED GENERATION !!!\n");
     fflush(stderr);
    }
#endif /* WIN32 */
#endif /* CLISERV */
   __rnd_seed[0] = seed ^ pid;
   __rnd_seed[1] = seed ^ pid;
  }
}