xref: /freebsd/crypto/openssl/providers/implementations/rands/seeding/rand_tsc.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9 
10 #include "internal/cryptlib.h"
11 #include <openssl/opensslconf.h>
12 #include "crypto/rand_pool.h"
13 #include "prov/seeding.h"
14 
15 #ifdef OPENSSL_RAND_SEED_RDTSC
16 /*
17  * IMPORTANT NOTE:  It is not currently possible to use this code
18  * because we are not sure about the amount of randomness it provides.
19  * Some SP800-90B tests have been run, but there is internal skepticism.
20  * So for now this code is not used.
21  */
22 # error "RDTSC enabled?  Should not be possible!"
23 
24 /*
25  * Acquire entropy from high-speed clock
26  *
27  * Since we get some randomness from the low-order bits of the
28  * high-speed clock, it can help.
29  *
30  * Returns the total entropy count, if it exceeds the requested
31  * entropy count. Otherwise, returns an entropy count of 0.
32  */
33 size_t ossl_prov_acquire_entropy_from_tsc(RAND_POOL *pool)
34 {
35     unsigned char c;
36     int i;
37 
38     if ((OPENSSL_ia32cap_P[0] & (1 << 4)) != 0) {
39         for (i = 0; i < TSC_READ_COUNT; i++) {
40             c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
41             ossl_rand_pool_add(pool, &c, 1, 4);
42         }
43     }
44     return ossl_rand_pool_entropy_available(pool);
45 }
46 #else
47 NON_EMPTY_TRANSLATION_UNIT
48 #endif
49