"Fossies" - the Fresh Open Source Software Archive 
Member "apg-2.2.3/rnd.c" (7 Aug 2003, 6961 Bytes) of package /linux/privat/old/apg-2.2.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 ** Copyright (c) 1999, 2000, 2001, 2002, 2003
3 ** Adel I. Mirzazhanov. All rights reserved
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 **
9 ** 1.Redistributions of source code must retain the above copyright notice,
10 ** this list of conditions and the following disclaimer.
11 ** 2.Redistributions in binary form must reproduce the above copyright
12 ** notice, this list of conditions and the following disclaimer in the
13 ** documentation and/or other materials provided with the distribution.
14 ** 3.The name of the author may not be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 ** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
33 #include <strings.h>
34 #endif
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include "rnd.h"
40
41 #ifndef APG_USE_SHA
42 # include "./cast/cast.h"
43 #else /* APG_USE_SHA */
44 # include "./sha/sha.h"
45 #endif /* APG_USE_SHA */
46
47 UINT32 __rnd_seed[2]; /* Random Seed 2*32=64 */
48
49 /*
50 ** randint(int n) - Produces a Random number from 0 to n-1.
51 ** INPUT:
52 ** int - limit
53 ** OUTPUT:
54 ** UINT - pandom number.
55 ** NOTES:
56 ** none.
57 */
58 UINT
59 randint(int n)
60 {
61 #ifndef APG_USE_SHA
62 return ( (UINT)( x917cast_rnd() % (UINT32)n ) );
63 #else /* APG_USE_SHA */
64 return ( (UINT)( x917sha1_rnd() % (UINT32)n ) );
65 #endif /* APG_USE_SHA */
66 }
67
68 #ifndef APG_USE_SHA
69 /*
70 ** ANSI X9.17 pseudorandom generator that uses CAST algorithm instead of DES
71 ** m = 1
72 ** INPUT:
73 ** none.
74 ** OUTPUT:
75 ** UINT32 - random number.
76 ** NOTES:
77 ** none.
78 */
79 UINT32
80 x917cast_rnd (void)
81 {
82 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
83 struct timeval local_time;
84 #else
85 clock_t local_time[2]; /* clock ticks for win32 */
86 #endif
87 UINT32 I[2] = {0L,0L};
88 UINT32 I_plus_s[2] = {0L,0L};
89 UINT32 Xi[2] = {0L,0L};
90 UINT32 Xi_plus_I[2] = {0L,0L};
91 cast_key ky;
92
93 /**********************************************************************
94 * ENCRYPTION KEY HEX : 0x000102030405060708090A0B0C0D0E0F (128-bit) *
95 * YOU CAN CHANGE IT IF YOU WANT *
96 **********************************************************************/
97 u8 ro_key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
98 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
99 /**********************************************************************
100 * ENCRYPTION KEY HEX : 0x000102030405060708090A0B0C0D0E0F (128-bit) *
101 * YOU CAN CHANGE IT IF YOU WANT *
102 **********************************************************************/
103 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
104 (void) gettimeofday (&local_time, 0);
105 #else
106 local_time[0] = clock();
107 local_time[1] = clock();
108 #endif
109 cast_setkey(&ky, (u8*)&ro_key[0], 16);
110 cast_encrypt (&ky, (u8 *)&local_time, (u8*)&I[0]); /* I=Ek(D), D-time */
111 I_plus_s[0] = I[0] ^ __rnd_seed[0]; /* I0 (+) s0 */
112 I_plus_s[1] = I[1] ^ __rnd_seed[1]; /* I1 (+) s1 */
113 cast_encrypt (&ky, (u8 *)&I_plus_s[0], (u8*)&Xi[0]); /* Xi=Ek( I (+) s ) */
114 Xi_plus_I[0] = Xi[0] ^ I[0]; /* Xi0 (+) I0 */
115 Xi_plus_I[1] = Xi[1] ^ I[1]; /* Xi1 (+) I1 */
116 cast_encrypt (&ky, (u8 *)&Xi_plus_I[0], (u8*)&__rnd_seed[0]); /* s=Ek( Xi (+) I ) */
117 return (Xi[0]);
118 }
119 #else /* APG_USE_SHA */
120 /*
121 ** ANSI X9.17 pseudorandom generator that uses SHA1 algorithm instead of DES
122 ** m=1
123 ** INPUT:
124 ** none.
125 ** OUTPUT:
126 ** UINT32 - random number.
127 ** NOTES:
128 ** none.
129 */
130 UINT32
131 x917sha1_rnd (void)
132 {
133 struct timeval local_time;
134 UINT32 I[2] = {0L,0L};
135 UINT32 I_plus_s[2] = {0L,0L};
136 UINT32 Xi[2] = {0L,0L};
137 UINT32 Xi_plus_I[2] = {0L,0L};
138
139 BYTE hash [SHA_DIGESTSIZE];
140 apg_SHA_INFO shaInfo;
141
142 (void) gettimeofday (&local_time, 0);
143 apg_shaInit ( &shaInfo );
144 apg_shaUpdate ( &shaInfo, (BYTE *)&local_time, 8);
145 apg_shaFinal ( &shaInfo, hash );
146 (void)memcpy ( (void *)&I[0], (void *)&hash[0], sizeof(I));
147 I_plus_s[0] = I[0] ^ __rnd_seed[0]; /* I0 (+) s0 */
148 I_plus_s[1] = I[1] ^ __rnd_seed[1]; /* I1 (+) s1 */
149
150 apg_shaInit(&shaInfo);
151 apg_shaUpdate( &shaInfo, (BYTE *)&I_plus_s, 8);
152 apg_shaFinal( &shaInfo, hash );
153 (void)memcpy ( (void *)&Xi[0], (void *)&hash[0], sizeof(Xi)); /* Xi=Ek( I (+) s ) */
154
155 Xi_plus_I[0] = Xi[0] ^ I[0]; /* Xi0 (+) I0 */
156 Xi_plus_I[1] = Xi[1] ^ I[1]; /* Xi1 (+) I1 */
157
158 apg_shaInit(&shaInfo);
159 apg_shaUpdate( &shaInfo, (BYTE *)&Xi_plus_I, 8);
160 apg_shaFinal(&shaInfo, hash);
161 (void)memcpy ( (void *)&__rnd_seed[0], (void *)&hash[0],
162 sizeof(__rnd_seed)); /* s=Ek( Xi (+) I ) */
163 return (Xi[0]);
164 }
165 #endif /* APG_USE_SHA */
166 /*
167 ** x917_setseed (UINT32 seed) - Initializes seed
168 ** INPUT:
169 ** UINT32 - seed value
170 ** int - quiet mode flag
171 ** OUTPUT:
172 ** none.
173 ** NOTES:
174 ** none.
175 */
176 void
177 x917_setseed (UINT32 seed, int quiet)
178 {
179 FILE * dr;
180 UINT32 drs[2];
181 UINT32 pid = 0;
182
183 pid = (UINT32)getpid();
184
185 if ( (dr = fopen(APG_DEVRANDOM, "r")) != NULL)
186 {
187 (void)fread( (void *)&drs[0], 8, 1, dr);
188 __rnd_seed[0] = seed ^ drs[0];
189 __rnd_seed[1] = seed ^ drs[1];
190 (void) fclose(dr);
191 }
192 else if ( (dr = fopen(APG_DEVURANDOM, "r")) != NULL)
193 {
194 (void)fread( (void *)&drs[0], 8, 1, dr);
195 __rnd_seed[0] = seed ^ drs[0];
196 __rnd_seed[1] = seed ^ drs[1];
197 (void) fclose(dr);
198 }
199 else
200 {
201 #ifndef CLISERV
202 #if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32) && !defined(__WIN32__)
203 if (quiet != TRUE)
204 {
205 fprintf(stderr,"CAN NOT USE RANDOM DEVICE TO GENERATE RANDOM SEED\n");
206 fprintf(stderr,"USING LOCAL TIME AND PID FOR SEED GENERATION !!!\n");
207 fflush(stderr);
208 }
209 #endif /* WIN32 */
210 #endif /* CLISERV */
211 __rnd_seed[0] = seed ^ pid;
212 __rnd_seed[1] = seed ^ pid;
213 }
214 }