rand_tsc.c 1.4 KB

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