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