rand_vxworks.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2019-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 <openssl/opensslconf.h>
  10. #include <openssl/rand.h>
  11. #include "crypto/rand_pool.h"
  12. #include "crypto/rand.h"
  13. #include "internal/cryptlib.h"
  14. #include "prov/seeding.h"
  15. #include <version.h>
  16. #include <taskLib.h>
  17. #if defined(OPENSSL_RAND_SEED_NONE)
  18. /* none means none */
  19. # undef OPENSSL_RAND_SEED_OS
  20. #endif
  21. #if defined(OPENSSL_RAND_SEED_OS)
  22. # if _WRS_VXWORKS_MAJOR >= 7
  23. # define RAND_SEED_VXRANDLIB
  24. # else
  25. # error "VxWorks <7 only support RAND_SEED_NONE"
  26. # endif
  27. #endif
  28. #if defined(RAND_SEED_VXRANDLIB)
  29. # include <randomNumGen.h>
  30. #endif
  31. /* Macro to convert two thirty two bit values into a sixty four bit one */
  32. #define TWO32TO64(a, b) ((((uint64_t)(a)) << 32) + (b))
  33. static uint64_t get_time_stamp(void)
  34. {
  35. struct timespec ts;
  36. if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
  37. return TWO32TO64(ts.tv_sec, ts.tv_nsec);
  38. return time(NULL);
  39. }
  40. static uint64_t get_timer_bits(void)
  41. {
  42. uint64_t res = OPENSSL_rdtsc();
  43. struct timespec ts;
  44. if (res != 0)
  45. return res;
  46. if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
  47. return TWO32TO64(ts.tv_sec, ts.tv_nsec);
  48. return time(NULL);
  49. }
  50. /*
  51. * empty implementation
  52. * vxworks does not need to init/cleanup or keep open the random lib
  53. */
  54. int ossl_rand_pool_init(void)
  55. {
  56. return 1;
  57. }
  58. void ossl_rand_pool_cleanup(void)
  59. {
  60. }
  61. void ossl_rand_pool_keep_random_devices_open(int keep)
  62. {
  63. }
  64. int ossl_rand_pool_add_additional_data(RAND_POOL *pool)
  65. {
  66. struct {
  67. CRYPTO_THREAD_ID tid;
  68. uint64_t time;
  69. } data;
  70. memset(&data, 0, sizeof(data));
  71. /*
  72. * Add some noise from the thread id and a high resolution timer.
  73. * The thread id adds a little randomness if the drbg is accessed
  74. * concurrently (which is the case for the <master> drbg).
  75. */
  76. data.tid = CRYPTO_THREAD_get_current_id();
  77. data.time = get_timer_bits();
  78. return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  79. }
  80. int ossl_pool_add_nonce_data(RAND_POOL *pool)
  81. {
  82. struct {
  83. pid_t pid;
  84. CRYPTO_THREAD_ID tid;
  85. uint64_t time;
  86. } data;
  87. memset(&data, 0, sizeof(data));
  88. /*
  89. * Add process id, thread id, and a high resolution timestamp to
  90. * ensure that the nonce is unique with high probability for
  91. * different process instances.
  92. */
  93. data.pid = getpid();
  94. data.tid = CRYPTO_THREAD_get_current_id();
  95. data.time = get_time_stamp();
  96. return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  97. }
  98. size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
  99. {
  100. #if defined(RAND_SEED_VXRANDLIB)
  101. /* vxRandLib based entropy method */
  102. size_t bytes_needed;
  103. bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  104. if (bytes_needed > 0)
  105. {
  106. int retryCount = 0;
  107. STATUS result = ERROR;
  108. unsigned char *buffer;
  109. buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
  110. while ((result != OK) && (retryCount < 10)) {
  111. RANDOM_NUM_GEN_STATUS status = randStatus();
  112. if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
  113. || (status == RANDOM_NUM_GEN_MAX_ENTROPY)) {
  114. result = randBytes(buffer, bytes_needed);
  115. if (result == OK)
  116. ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  117. /*
  118. * no else here: randStatus said ok, if randBytes failed
  119. * it will result in another loop or no entropy
  120. */
  121. } else {
  122. /*
  123. * give a minimum delay here to allow OS to collect more
  124. * entropy. taskDelay duration will depend on the system tick,
  125. * this is by design as the sw-random lib uses interrupts
  126. * which will at least happen during ticks
  127. */
  128. taskDelay(5);
  129. }
  130. retryCount++;
  131. }
  132. }
  133. return ossl_rand_pool_entropy_available(pool);
  134. #else
  135. /*
  136. * SEED_NONE means none, without randlib we dont have entropy and
  137. * rely on it being added externally
  138. */
  139. return ossl_rand_pool_entropy_available(pool);
  140. #endif /* defined(RAND_SEED_VXRANDLIB) */
  141. }