81064863bca2b9b6abc8124498277fb21111e35d
[openafs.git] / src / crypto / hcrypto / kernel / rand.c
1 /* A trivial implementation of hcrypto's RAND interface for
2  * kernel use */
3
4 #include <config.h>
5 #include <evp.h>
6 #include <evp-hcrypto.h>
7 #include <aes.h>
8 #include <sha.h>
9 #include <heim_threads.h>
10
11 /*
12  * This mutex is used to synchronize hcrypto operations in the kernel.
13  * We cheat and assume that all access into hcrypto comes through routines
14  * in this file, so that we can ensure it is initialized before it is used.
15  */
16 afs_kmutex_t hckernel_mutex;
17
18 /* Called from osi_Init(); will only run once. */
19 void
20 init_hckernel_mutex(void)
21 {
22     MUTEX_INIT(&hckernel_mutex, "hckernel", MUTEX_DEFAULT, 0);
23 }
24
25 void
26 RAND_seed(const void *indata, size_t size)
27 {
28     const RAND_METHOD *m = RAND_fortuna_method();
29     m->seed(indata, size);
30 }
31
32 int
33 RAND_bytes(void *outdata, size_t size)
34 {
35     if (size == 0)
36         return 0;
37 #if defined(AFS_AIX_ENV) || defined(AFS_DFBSD_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_SGI_ENV)
38     const RAND_METHOD *m = RAND_fortuna_method();
39     return m->bytes(outdata, size);
40 #else
41     if (osi_readRandom(outdata, size))
42         return 0;
43 #endif
44     return 1;
45 }