2
0

rand_vms.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  10. #if defined(OPENSSL_SYS_VMS)
  11. # include <unistd.h>
  12. # include "internal/cryptlib.h"
  13. # include <openssl/rand.h>
  14. # include "internal/rand_int.h"
  15. # include "rand_lcl.h"
  16. # include <descrip.h>
  17. # include <jpidef.h>
  18. # include <ssdef.h>
  19. # include <starlet.h>
  20. # include <efndef>
  21. # ifdef __DECC
  22. # pragma message disable DOLLARID
  23. # endif
  24. # ifndef OPENSSL_RAND_SEED_OS
  25. # error "Unsupported seeding method configured; must be os"
  26. # endif
  27. /*
  28. * Use 32-bit pointers almost everywhere. Define the type to which to cast a
  29. * pointer passed to an external function.
  30. */
  31. # if __INITIAL_POINTER_SIZE == 64
  32. # define PTR_T __void_ptr64
  33. # pragma pointer_size save
  34. # pragma pointer_size 32
  35. # else
  36. # define PTR_T void *
  37. # endif
  38. static struct items_data_st {
  39. short length, code; /* length is number of bytes */
  40. } items_data[] = {
  41. {4, JPI$_BUFIO},
  42. {4, JPI$_CPUTIM},
  43. {4, JPI$_DIRIO},
  44. {4, JPI$_IMAGECOUNT},
  45. {8, JPI$_LAST_LOGIN_I},
  46. {8, JPI$_LOGINTIM},
  47. {4, JPI$_PAGEFLTS},
  48. {4, JPI$_PID},
  49. {4, JPI$_PPGCNT},
  50. {4, JPI$_WSPEAK},
  51. {4, JPI$_FINALEXC},
  52. {0, 0}
  53. };
  54. /*
  55. * We assume there we get about 4 bits of entropy per byte from the items
  56. * above, with a bit of scrambling added rand_pool_acquire_entropy()
  57. */
  58. #define ENTROPY_BITS_PER_BYTE 4
  59. size_t rand_pool_acquire_entropy(RAND_POOL *pool)
  60. {
  61. /* determine the number of items in the JPI array */
  62. struct items_data_st item_entry;
  63. size_t item_entry_count = OSSL_NELEM(items_data);
  64. /* Create the 32-bit JPI itemlist array to hold item_data content */
  65. struct {
  66. uint16_t length, code;
  67. uint32_t *buffer;
  68. uint32_t *retlen;
  69. } item[item_entry_count], *pitem;
  70. struct items_data_st *pitems_data;
  71. /* 8 bytes (two longs) per entry max */
  72. uint32_t data_buffer[(item_entry_count * 2) + 4];
  73. uint32_t iosb[2];
  74. uint32_t sys_time[2];
  75. uint32_t *ptr;
  76. size_t i, j ;
  77. size_t tmp_length = 0;
  78. size_t total_length = 0;
  79. size_t bytes_needed = rand_pool_bytes_needed(pool, ENTROPY_BITS_PER_BYTE);
  80. size_t bytes_remaining = rand_pool_bytes_remaining(pool);
  81. /* Setup itemlist for GETJPI */
  82. pitems_data = items_data;
  83. for (pitem = item; pitems_data->length != 0; pitem++) {
  84. pitem->length = pitems_data->length;
  85. pitem->code = pitems_data->code;
  86. pitem->buffer = &data_buffer[total_length];
  87. pitem->retlen = 0;
  88. /* total_length is in longwords */
  89. total_length += pitems_data->length / 4;
  90. pitems_data++;
  91. }
  92. pitem->length = pitem->code = 0;
  93. /* Fill data_buffer with various info bits from this process */
  94. if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) != SS$_NORMAL)
  95. return 0;
  96. /* Now twist that data to seed the SSL random number init */
  97. for (i = 0; i < total_length; i++) {
  98. sys$gettim((struct _generic_64 *)&sys_time[0]);
  99. srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
  100. if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
  101. ptr = &data_buffer[i];
  102. for (j = 0; j < 4; j++) {
  103. data_buffer[i + j] = ptr[j];
  104. /* OK to use rand() just to scramble the seed */
  105. data_buffer[i + j] ^= (sys_time[0] ^ rand());
  106. tmp_length++;
  107. }
  108. } else {
  109. /* OK to use rand() just to scramble the seed */
  110. data_buffer[i] ^= (sys_time[0] ^ rand());
  111. }
  112. }
  113. total_length += (tmp_length - 1);
  114. /* Change the total length to number of bytes */
  115. total_length *= 4;
  116. /*
  117. * If we can't feed the requirements from the caller, we're in deep trouble.
  118. */
  119. if (!ossl_assert(total_length >= bytes_needed)) {
  120. char neededstr[20];
  121. char availablestr[20];
  122. BIO_snprintf(neededstr, sizeof(neededstr), "%zu", bytes_needed);
  123. BIO_snprintf(availablestr, sizeof(availablestr), "%zu", total_length);
  124. RANDerr(RAND_F_RAND_POOL_ACQUIRE_ENTROPY,
  125. RAND_R_RANDOM_POOL_UNDERFLOW);
  126. ERR_add_error_data(4, "Needed: ", neededstr, ", Available: ",
  127. availablestr);
  128. return 0;
  129. }
  130. /*
  131. * Try not to overfeed the pool
  132. */
  133. if (total_length > bytes_remaining)
  134. total_length = bytes_remaining;
  135. rand_pool_add(pool, (PTR_T)data_buffer, total_length,
  136. total_length * ENTROPY_BITS_PER_BYTE);
  137. return rand_pool_entropy_available(pool);
  138. }
  139. int rand_pool_add_nonce_data(RAND_POOL *pool)
  140. {
  141. struct {
  142. pid_t pid;
  143. CRYPTO_THREAD_ID tid;
  144. uint64_t time;
  145. } data = { 0 };
  146. /*
  147. * Add process id, thread id, and a high resolution timestamp to
  148. * ensure that the nonce is unique whith high probability for
  149. * different process instances.
  150. */
  151. data.pid = getpid();
  152. data.tid = CRYPTO_THREAD_get_current_id();
  153. sys$gettim_prec((struct _generic_64 *)&data.time);
  154. return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  155. }
  156. int rand_pool_add_additional_data(RAND_POOL *pool)
  157. {
  158. struct {
  159. CRYPTO_THREAD_ID tid;
  160. uint64_t time;
  161. } data = { 0 };
  162. /*
  163. * Add some noise from the thread id and a high resolution timer.
  164. * The thread id adds a little randomness if the drbg is accessed
  165. * concurrently (which is the case for the <master> drbg).
  166. */
  167. data.tid = CRYPTO_THREAD_get_current_id();
  168. sys$gettim_prec((struct _generic_64 *)&data.time);
  169. return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
  170. }
  171. #endif