]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rand/rand_unix.c
2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include "internal/cryptlib.h"
14 #include <openssl/rand.h>
18 #if !(defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI))
20 # if (defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)) && \
21 !defined(OPENSSL_RAND_SEED_NONE)
22 # error "UEFI and VXWorks only support seeding NONE"
25 # if defined(OPENSSL_SYS_VOS)
27 # ifndef OPENSSL_RAND_SEED_OS
28 # error "Unsupported seeding method configured; must be os"
31 # if defined(OPENSSL_SYS_VOS_HPPA) && defined(OPENSSL_SYS_VOS_IA32)
32 # error "Unsupported HP-PA and IA32 at the same time."
34 # if !defined(OPENSSL_SYS_VOS_HPPA) && !defined(OPENSSL_SYS_VOS_IA32)
35 # error "Must have one of HP-PA or IA32"
39 * The following algorithm repeatedly samples the real-time clock (RTC) to
40 * generate a sequence of unpredictable data. The algorithm relies upon the
41 * uneven execution speed of the code (due to factors such as cache misses,
42 * interrupts, bus activity, and scheduling) and upon the rather large
43 * relative difference between the speed of the clock and the rate at which
46 * If this code is ported to an environment where execution speed is more
47 * constant or where the RTC ticks at a much slower rate, or the clock can be
48 * read with fewer instructions, it is likely that the results would be far
51 * As a precaution, we generate 4 times the minimum required amount of seed
63 # ifdef OPENSSL_SYS_VOS_HPPA
65 extern void s$
sleep(long *_duration
, short int *_code
);
68 extern void s$
sleep2(long long *_duration
, short int *_code
);
72 * Seed with the gid, pid, and uid, to ensure *some* variation between
73 * different processes.
76 RAND_add(&curr_gid
, sizeof curr_gid
, 0);
78 RAND_add(&curr_pid
, sizeof curr_pid
, 0);
80 RAND_add(&curr_uid
, sizeof curr_uid
, 0);
82 for (i
= 0; i
< (RANDOMNESS_NEEDED
* 4); i
++) {
84 * burn some cpu; hope for interrupts, cache collisions, bus
87 for (k
= 0; k
< 99; k
++)
88 ts
.tv_nsec
= random();
90 # ifdef OPENSSL_SYS_VOS_HPPA
91 /* sleep for 1/1024 of a second (976 us). */
93 s$
sleep(&duration
, &code
);
95 /* sleep for 1/65536 of a second (15 us). */
97 s$
sleep2(&duration
, &code
);
100 /* Get wall clock time, take 8 bits. */
101 clock_gettime(CLOCK_REALTIME
, &ts
);
102 v
= (unsigned char)(ts
.tv_nsec
& 0xFF);
103 RAND_add(&v
, sizeof v
, 1);
110 # if defined(OPENSSL_RAND_SEED_EGD) && \
111 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
112 # error "Seeding uses EGD but EGD is turned off or no device given"
115 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
116 # error "Seeding uses urandom but DEVRANDOM is not configured"
119 # if defined(OPENSSL_RAND_SEED_OS)
120 # if defined(DEVRANDOM)
121 # define OPENSSL_RAND_SEED_DEVRANDOM
123 # error "OS seeding requires DEVRANDOM to be configured"
127 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
128 # error "librandom not (yet) supported"
133 # ifdef OPENSSL_RAND_SEED_NONE
137 char temp
[RANDOMNESS_NEEDED
];
138 # define TEMPSIZE (int)sizeof(temp)
140 # ifdef OPENSSL_RAND_SEED_RDTSC
144 # ifdef OPENSSL_RAND_SEED_RDCPU
149 # ifdef OPENSSL_RAND_SEED_EGD
151 static const char *paths
[] = { DEVRANDOM_EGD
, NULL
};
154 for (i
= 0; paths
[i
] != NULL
; i
++) {
155 if (RAND_query_egd_bytes(paths
[i
], temp
, TEMPSIZE
) == TEMPSIZE
) {
156 RAND_add(temp
, TEMPSIZE
, TEMPSIZE
);
164 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
166 static const char *paths
[] = { DEVRANDOM
, NULL
};
170 for (i
= 0; paths
[i
] != NULL
; i
++) {
171 if ((fp
= fopen(paths
[i
], "rb")) == NULL
)
174 if (fread(temp
, 1, TEMPSIZE
, fp
) == TEMPSIZE
) {
175 RAND_add(temp
, TEMPSIZE
, TEMPSIZE
);
184 # ifdef OPENSSL_RAND_SEED_GETRANDOM
186 int i
= getrandom(temp
, TEMPSIZE
, 0);
189 RAND_add(temp
, i
, i
);
196 OPENSSL_cleanse(temp
, TEMPSIZE
);
197 return ok
> 0 ? 1 : 0;