7021ec7d5190d529fccd319f123d04b23ce654f0
[krb5.git] / src / lib / crypto / os / rnd_confoun.c
1 /*
2  * lib/crypto/os/rnd_confoun.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * krb5_random_confounder()
25  */
26
27 #include "k5-int.h"
28
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #ifdef TIME_WITH_SYS_TIME
32 #include <time.h>
33 #endif
34 #else
35 #include <time.h>
36 #endif
37
38 #ifdef HAVE_SRAND48
39 #define SRAND   srand48
40 #define RAND    lrand48
41 #define RAND_TYPE       long
42 #endif
43
44 #if !defined(RAND_TYPE) && defined(HAVE_SRAND)
45 #define SRAND   srand
46 #define RAND    rand
47 #define RAND_TYPE       int
48 #endif
49
50 #if !defined(RAND_TYPE) && defined(HAVE_SRANDOM)        
51 #define SRAND   srandom
52 #define RAND    random
53 #define RAND_TYPE       long
54 #endif
55
56 #if !defined(RAND_TYPE)
57 You need a random number generator!
58 #endif
59
60 /*
61  * Generate a random confounder
62  */
63 krb5_error_code INTERFACE
64 krb5_random_confounder(size, fillin)
65 int size;
66 krb5_pointer fillin;
67 {
68     static int seeded = 0;
69     register krb5_octet *real_fill;
70     RAND_TYPE   rval;
71
72     if (!seeded) {
73         /* time() defined in 4.12.2.4, but returns a time_t, which is an
74            "arithmetic type" (4.12.1) */
75         rval = (RAND_TYPE) time(0);
76         SRAND(rval);
77 #ifdef HAVE_GETPID
78         rval = RAND();
79         rval ^= getpid();
80         SRAND(rval);
81 #endif
82         seeded = 1;
83     }
84
85     real_fill = (krb5_octet *)fillin;
86     while (size > 0) {
87         rval = RAND();
88         *real_fill = rval & 0xff;
89         real_fill++;
90         size--;
91         if (size) {
92             *real_fill = (rval >> 8) & 0xff;
93             real_fill++;
94             size--;
95         }
96     }
97     return 0;
98 }