2 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
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
12 #if defined(OPENSSL_SYS_VMS)
14 # include "internal/cryptlib.h"
15 # include <openssl/rand.h>
16 # include "internal/rand_int.h"
17 # include "rand_lcl.h"
24 # pragma message disable DOLLARID
27 # ifndef OPENSSL_RAND_SEED_OS
28 # error "Unsupported seeding method configured; must be os"
32 * Use 32-bit pointers almost everywhere. Define the type to which to cast a
33 * pointer passed to an external function.
35 # if __INITIAL_POINTER_SIZE == 64
36 # define PTR_T __void_ptr64
37 # pragma pointer_size save
38 # pragma pointer_size 32
43 static struct items_data_st {
44 short length, code; /* length is number of bytes */
50 {8, JPI$_LAST_LOGIN_I},
61 * We assume there we get about 4 bits of entropy per byte from the items
62 * above, with a bit of scrambling added rand_pool_acquire_entropy()
64 #define ENTROPY_BITS_PER_BYTE 4
66 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
68 /* determine the number of items in the JPI array */
69 struct items_data_st item_entry;
70 size_t item_entry_count = OSSL_NELEM(items_data);
71 /* Create the 32-bit JPI itemlist array to hold item_data content */
73 uint16_t length, code;
76 } item[item_entry_count], *pitem;
77 struct items_data_st *pitems_data;
78 /* 8 bytes (two longs) per entry max */
79 uint32_t data_buffer[(item_entry_count * 2) + 4];
84 size_t tmp_length = 0;
85 size_t total_length = 0;
86 size_t bytes_needed = rand_pool_bytes_needed(pool, ENTROPY_BITS_PER_BYTE);
87 size_t bytes_remaining = rand_pool_bytes_remaining(pool);
89 /* Setup itemlist for GETJPI */
90 pitems_data = items_data;
91 for (pitem = item; pitems_data->length != 0; pitem++) {
92 pitem->length = pitems_data->length;
93 pitem->code = pitems_data->code;
94 pitem->buffer = &data_buffer[total_length];
96 /* total_length is in longwords */
97 total_length += pitems_data->length / 4;
100 pitem->length = pitem->code = 0;
102 /* Fill data_buffer with various info bits from this process */
103 if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) != SS$_NORMAL)
106 /* Now twist that data to seed the SSL random number init */
107 for (i = 0; i < total_length; i++) {
108 sys$gettim((struct _generic_64 *)&sys_time[0]);
109 srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
111 if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
112 ptr = &data_buffer[i];
113 for (j = 0; j < 4; j++) {
114 data_buffer[i + j] = ptr[j];
115 /* OK to use rand() just to scramble the seed */
116 data_buffer[i + j] ^= (sys_time[0] ^ rand());
120 /* OK to use rand() just to scramble the seed */
121 data_buffer[i] ^= (sys_time[0] ^ rand());
125 total_length += (tmp_length - 1);
127 /* Change the total length to number of bytes */
131 * If we can't feed the requirements from the caller, we're in deep trouble.
133 if (!ossl_assert(total_length >= bytes_needed)) {
135 char availablestr[20];
137 BIO_snprintf(neededstr, sizeof(neededstr), "%zu", bytes_needed);
138 BIO_snprintf(availablestr, sizeof(availablestr), "%zu", total_length);
139 RANDerr(RAND_F_RAND_POOL_ACQUIRE_ENTROPY,
140 RAND_R_RANDOM_POOL_UNDERFLOW);
141 ERR_add_error_data(4, "Needed: ", neededstr, ", Available: ",
147 * Try not to overfeed the pool
149 if (total_length > bytes_remaining)
150 total_length = bytes_remaining;
152 rand_pool_add(pool, (PTR_T)data_buffer, total_length,
153 total_length * ENTROPY_BITS_PER_BYTE);
154 return rand_pool_entropy_available(pool);
157 int rand_pool_add_nonce_data(RAND_POOL *pool)
161 CRYPTO_THREAD_ID tid;
166 * Add process id, thread id, and a high resolution timestamp to
167 * ensure that the nonce is unique whith high probability for
168 * different process instances.
171 data.tid = CRYPTO_THREAD_get_current_id();
172 sys$gettim_prec((struct _generic_64 *)&data.time);
174 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
177 int rand_pool_add_additional_data(RAND_POOL *pool)
180 CRYPTO_THREAD_ID tid;
185 * Add some noise from the thread id and a high resolution timer.
186 * The thread id adds a little randomness if the drbg is accessed
187 * concurrently (which is the case for the <master> drbg).
189 data.tid = CRYPTO_THREAD_get_current_id();
190 sys$gettim_prec((struct _generic_64 *)&data.time);
192 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);