rand_vxworks.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_pool_add_nonce_data(RAND_POOL *pool)
  65. {
  66. struct {
  67. pid_t pid;
  68. CRYPTO_THREAD_ID tid;
  69. uint64_t time;
  70. } data;
  71. memset(&data, 0, sizeof(data));
  72. /*
  73. * Add process id, thread id, and a high resolution timestamp to
  74. * ensure that the nonce is unique with high probability for
  75. * different process instances.
  76. */
  77. data.pid = getpid();
  78. data.tid = CRYPTO_THREAD_get_current_id();
  79. data.time = get_time_stamp();
  80. return ossl_rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  81. }
  82. size_t ossl_pool_acquire_entropy(RAND_POOL *pool)
  83. {
  84. #if defined(RAND_SEED_VXRANDLIB)
  85. /* vxRandLib based entropy method */
  86. size_t bytes_needed;
  87. bytes_needed = ossl_rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
  88. if (bytes_needed > 0)
  89. {
  90. int retryCount = 0;
  91. STATUS result = ERROR;
  92. unsigned char *buffer;
  93. buffer = ossl_rand_pool_add_begin(pool, bytes_needed);
  94. while ((result != OK) && (retryCount < 10)) {
  95. RANDOM_NUM_GEN_STATUS status = randStatus();
  96. if ((status == RANDOM_NUM_GEN_ENOUGH_ENTROPY)
  97. || (status == RANDOM_NUM_GEN_MAX_ENTROPY)) {
  98. result = randBytes(buffer, bytes_needed);
  99. if (result == OK)
  100. ossl_rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
  101. /*
  102. * no else here: randStatus said ok, if randBytes failed
  103. * it will result in another loop or no entropy
  104. */
  105. } else {
  106. /*
  107. * give a minimum delay here to allow OS to collect more
  108. * entropy. taskDelay duration will depend on the system tick,
  109. * this is by design as the sw-random lib uses interrupts
  110. * which will at least happen during ticks
  111. */
  112. taskDelay(5);
  113. }
  114. retryCount++;
  115. }
  116. }
  117. return ossl_rand_pool_entropy_available(pool);
  118. #else
  119. /*
  120. * SEED_NONE means none, without randlib we dont have entropy and
  121. * rely on it being added externally
  122. */
  123. return ossl_rand_pool_entropy_available(pool);
  124. #endif /* defined(RAND_SEED_VXRANDLIB) */
  125. }