rand_vms.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* crypto/rand/rand_vms.c */
  2. /*
  3. * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
  4. * 2000.
  5. */
  6. /*
  7. * Modified by VMS Software, Inc (2016)
  8. * Eliminate looping through all processes (performance)
  9. * Add additional randomizations using rand() function
  10. */
  11. /* ====================================================================
  12. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in
  23. * the documentation and/or other materials provided with the
  24. * distribution.
  25. *
  26. * 3. All advertising materials mentioning features or use of this
  27. * software must display the following acknowledgment:
  28. * "This product includes software developed by the OpenSSL Project
  29. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  30. *
  31. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  32. * endorse or promote products derived from this software without
  33. * prior written permission. For written permission, please contact
  34. * openssl-core@openssl.org.
  35. *
  36. * 5. Products derived from this software may not be called "OpenSSL"
  37. * nor may "OpenSSL" appear in their names without prior written
  38. * permission of the OpenSSL Project.
  39. *
  40. * 6. Redistributions of any form whatsoever must retain the following
  41. * acknowledgment:
  42. * "This product includes software developed by the OpenSSL Project
  43. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  44. *
  45. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  46. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  48. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  49. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  51. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  52. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  53. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  54. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  55. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  56. * OF THE POSSIBILITY OF SUCH DAMAGE.
  57. * ====================================================================
  58. *
  59. * This product includes cryptographic software written by Eric Young
  60. * (eay@cryptsoft.com). This product includes software written by Tim
  61. * Hudson (tjh@cryptsoft.com).
  62. *
  63. */
  64. #include <openssl/rand.h>
  65. #include "rand_lcl.h"
  66. #if defined(OPENSSL_SYS_VMS)
  67. # include <descrip.h>
  68. # include <jpidef.h>
  69. # include <ssdef.h>
  70. # include <starlet.h>
  71. # include <efndef>
  72. # ifdef __DECC
  73. # pragma message disable DOLLARID
  74. # endif
  75. /*
  76. * Use 32-bit pointers almost everywhere. Define the type to which to cast a
  77. * pointer passed to an external function.
  78. */
  79. # if __INITIAL_POINTER_SIZE == 64
  80. # define PTR_T __void_ptr64
  81. # pragma pointer_size save
  82. # pragma pointer_size 32
  83. # else /* __INITIAL_POINTER_SIZE == 64 */
  84. # define PTR_T void *
  85. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  86. static struct items_data_st {
  87. short length, code; /* length is number of bytes */
  88. } items_data[] = {
  89. {4, JPI$_BUFIO},
  90. {4, JPI$_CPUTIM},
  91. {4, JPI$_DIRIO},
  92. {4, JPI$_IMAGECOUNT},
  93. {8, JPI$_LAST_LOGIN_I},
  94. {8, JPI$_LOGINTIM},
  95. {4, JPI$_PAGEFLTS},
  96. {4, JPI$_PID},
  97. {4, JPI$_PPGCNT},
  98. {4, JPI$_WSPEAK},
  99. {4, JPI$_FINALEXC},
  100. {0, 0} /* zero terminated */
  101. };
  102. int RAND_poll(void)
  103. {
  104. /* determine the number of items in the JPI array */
  105. struct items_data_st item_entry;
  106. int item_entry_count = sizeof(items_data)/sizeof(item_entry);
  107. /* Create the JPI itemlist array to hold item_data content */
  108. struct {
  109. short length, code;
  110. int *buffer;
  111. int *retlen;
  112. } item[item_entry_count], *pitem; /* number of entries in items_data */
  113. struct items_data_st *pitems_data;
  114. pitems_data = items_data;
  115. pitem = item;
  116. int data_buffer[(item_entry_count*2)+4]; /* 8 bytes per entry max */
  117. int iosb[2];
  118. int sys_time[2];
  119. int *ptr;
  120. int i, j ;
  121. int tmp_length = 0;
  122. int total_length = 0;
  123. /* Setup itemlist for GETJPI */
  124. while (pitems_data->length) {
  125. pitem->length = pitems_data->length;
  126. pitem->code = pitems_data->code;
  127. pitem->buffer = &data_buffer[total_length];
  128. pitem->retlen = 0;
  129. /* total_length is in longwords */
  130. total_length += pitems_data->length/4;
  131. pitems_data++;
  132. pitem ++;
  133. }
  134. pitem->length = pitem->code = 0;
  135. /* Fill data_buffer with various info bits from this process */
  136. /* and twist that data to seed the SSL random number init */
  137. if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) == SS$_NORMAL) {
  138. for (i = 0; i < total_length; i++) {
  139. sys$gettim((struct _generic_64 *)&sys_time[0]);
  140. srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
  141. if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
  142. ptr = &data_buffer[i];
  143. for (j = 0; j < 4; j++) {
  144. data_buffer[i + j] = ptr[j];
  145. /* OK to use rand() just to scramble the seed */
  146. data_buffer[i + j] ^= (sys_time[0] ^ rand());
  147. tmp_length++;
  148. }
  149. } else {
  150. /* OK to use rand() just to scramble the seed */
  151. data_buffer[i] ^= (sys_time[0] ^ rand());
  152. }
  153. }
  154. total_length += (tmp_length - 1);
  155. /* size of seed is total_length*4 bytes (64bytes) */
  156. RAND_add((PTR_T) data_buffer, total_length*4, total_length * 2);
  157. } else {
  158. return 0;
  159. }
  160. return 1;
  161. }
  162. #endif