]>
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
44 * it can be read. If it is ported to an environment where execution speed
45 * is more constant or where the RTC ticks at a much slower rate, or the
46 * clock can be read with fewer instructions, it is likely that the results
47 * would be far more predictable. This should only be used for legacy
50 * As a precaution, we generate four times the required amount of seed
53 int RAND_poll_ex(RAND_poll_cb rand_add
, void *arg
)
62 # ifdef OPENSSL_SYS_VOS_HPPA
64 extern void s$
sleep(long *_duration
, short int *_code
);
67 extern void s$
sleep2(long long *_duration
, short int *_code
);
71 * Seed with the gid, pid, and uid, to ensure *some* variation between
72 * different processes.
75 rand_add(arg
, &curr_gid
, sizeof curr_gid
, 0);
77 rand_add(arg
, &curr_pid
, sizeof curr_pid
, 0);
79 rand_add(arg
, &curr_uid
, sizeof curr_uid
, 0);
81 for (i
= 0; i
< (RANDOMNESS_NEEDED
* 4); i
++) {
83 * burn some cpu; hope for interrupts, cache collisions, bus
86 for (k
= 0; k
< 99; k
++)
87 ts
.tv_nsec
= random();
89 # ifdef OPENSSL_SYS_VOS_HPPA
90 /* sleep for 1/1024 of a second (976 us). */
92 s$
sleep(&duration
, &code
);
94 /* sleep for 1/65536 of a second (15 us). */
96 s$
sleep2(&duration
, &code
);
99 /* Get wall clock time, take 8 bits. */
100 clock_gettime(CLOCK_REALTIME
, &ts
);
101 v
= (unsigned char)(ts
.tv_nsec
& 0xFF);
102 rand_add(arg
, &v
, sizeof v
, 1);
109 # if defined(OPENSSL_RAND_SEED_EGD) && \
110 (defined(OPENSSL_NO_EGD) || !defined(DEVRANDOM_EGD))
111 # error "Seeding uses EGD but EGD is turned off or no device given"
114 # if defined(OPENSSL_RAND_SEED_DEVRANDOM) && !defined(DEVRANDOM)
115 # error "Seeding uses urandom but DEVRANDOM is not configured"
118 # if defined(OPENSSL_RAND_SEED_OS)
119 # if defined(DEVRANDOM)
120 # define OPENSSL_RAND_SEED_DEVRANDOM
122 # error "OS seeding requires DEVRANDOM to be configured"
126 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
127 # error "librandom not (yet) supported"
131 * Try the various seeding methods in turn, exit when succesful.
133 int RAND_poll_ex(RAND_poll_cb rand_add
, void *arg
)
135 # ifdef OPENSSL_RAND_SEED_NONE
139 char temp
[RANDOMNESS_NEEDED
];
140 # define TEMPSIZE (int)sizeof(temp)
142 # ifdef OPENSSL_RAND_SEED_GETRANDOM
144 int i
= getrandom(temp
, TEMPSIZE
, 0);
147 rand_add(arg
, temp
, i
, i
);
154 # if defined(OPENSSL_RAND_SEED_LIBRANDOM)
156 /* Not yet implemented. */
160 # ifdef OPENSSL_RAND_SEED_DEVRANDOM
162 static const char *paths
[] = { DEVRANDOM
, NULL
};
166 for (i
= 0; paths
[i
] != NULL
; i
++) {
167 if ((fp
= fopen(paths
[i
], "rb")) == NULL
)
170 if (fread(temp
, 1, TEMPSIZE
, fp
) == TEMPSIZE
) {
171 rand_add(arg
, temp
, TEMPSIZE
, TEMPSIZE
);
180 # ifdef OPENSSL_RAND_SEED_RDTSC
181 rand_read_tsc(cb
, arg
);
184 # ifdef OPENSSL_RAND_SEED_RDCPU
185 if (rand_read_cpu(cb
, arg
))
189 # ifdef OPENSSL_RAND_SEED_EGD
191 static const char *paths
[] = { DEVRANDOM_EGD
, NULL
};
194 for (i
= 0; paths
[i
] != NULL
; i
++) {
195 if (RAND_query_egd_bytes(paths
[i
], temp
, TEMPSIZE
) == TEMPSIZE
) {
196 rand_add(arg
, temp
, TEMPSIZE
, TEMPSIZE
);
206 OPENSSL_cleanse(temp
, TEMPSIZE
);