rand_vms.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright 2001-2016 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. /*
  10. * Modified by VMS Software, Inc (2016)
  11. * Eliminate looping through all processes (performance)
  12. * Add additional randomizations using rand() function
  13. */
  14. #include <openssl/rand.h>
  15. #include "rand_lcl.h"
  16. #if defined(OPENSSL_SYS_VMS)
  17. # include <descrip.h>
  18. # include <jpidef.h>
  19. # include <ssdef.h>
  20. # include <starlet.h>
  21. # include <efndef>
  22. # ifdef __DECC
  23. # pragma message disable DOLLARID
  24. # endif
  25. /*
  26. * Use 32-bit pointers almost everywhere. Define the type to which to cast a
  27. * pointer passed to an external function.
  28. */
  29. # if __INITIAL_POINTER_SIZE == 64
  30. # define PTR_T __void_ptr64
  31. # pragma pointer_size save
  32. # pragma pointer_size 32
  33. # else /* __INITIAL_POINTER_SIZE == 64 */
  34. # define PTR_T void *
  35. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  36. static struct items_data_st {
  37. short length, code; /* length is number of bytes */
  38. } items_data[] = {
  39. {4, JPI$_BUFIO},
  40. {4, JPI$_CPUTIM},
  41. {4, JPI$_DIRIO},
  42. {4, JPI$_IMAGECOUNT},
  43. {8, JPI$_LAST_LOGIN_I},
  44. {8, JPI$_LOGINTIM},
  45. {4, JPI$_PAGEFLTS},
  46. {4, JPI$_PID},
  47. {4, JPI$_PPGCNT},
  48. {4, JPI$_WSPEAK},
  49. {4, JPI$_FINALEXC},
  50. {0, 0} /* zero terminated */
  51. };
  52. int RAND_poll(void)
  53. {
  54. /* determine the number of items in the JPI array */
  55. struct items_data_st item_entry;
  56. int item_entry_count = sizeof(items_data)/sizeof(item_entry);
  57. /* Create the JPI itemlist array to hold item_data content */
  58. struct {
  59. short length, code;
  60. int *buffer;
  61. int *retlen;
  62. } item[item_entry_count], *pitem; /* number of entries in items_data */
  63. struct items_data_st *pitems_data;
  64. int data_buffer[(item_entry_count*2)+4]; /* 8 bytes per entry max */
  65. int iosb[2];
  66. int sys_time[2];
  67. int *ptr;
  68. int i, j ;
  69. int tmp_length = 0;
  70. int total_length = 0;
  71. pitems_data = items_data;
  72. pitem = item;
  73. /* Setup itemlist for GETJPI */
  74. while (pitems_data->length) {
  75. pitem->length = pitems_data->length;
  76. pitem->code = pitems_data->code;
  77. pitem->buffer = &data_buffer[total_length];
  78. pitem->retlen = 0;
  79. /* total_length is in longwords */
  80. total_length += pitems_data->length/4;
  81. pitems_data++;
  82. pitem ++;
  83. }
  84. pitem->length = pitem->code = 0;
  85. /* Fill data_buffer with various info bits from this process */
  86. /* and twist that data to seed the SSL random number init */
  87. if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) == SS$_NORMAL) {
  88. for (i = 0; i < total_length; i++) {
  89. sys$gettim((struct _generic_64 *)&sys_time[0]);
  90. srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
  91. if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
  92. ptr = &data_buffer[i];
  93. for (j = 0; j < 4; j++) {
  94. data_buffer[i + j] = ptr[j];
  95. /* OK to use rand() just to scramble the seed */
  96. data_buffer[i + j] ^= (sys_time[0] ^ rand());
  97. tmp_length++;
  98. }
  99. } else {
  100. /* OK to use rand() just to scramble the seed */
  101. data_buffer[i] ^= (sys_time[0] ^ rand());
  102. }
  103. }
  104. total_length += (tmp_length - 1);
  105. /* size of seed is total_length*4 bytes (64bytes) */
  106. RAND_add((PTR_T) data_buffer, total_length*4, total_length * 2);
  107. } else {
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. #endif