2 * Copyright 2001-2021 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)
13 # define __NEW_STARLET 1 /* New starlet definitions since VMS 7.0 */
15 # include "internal/cryptlib.h"
16 # include <openssl/bio.h>
17 # include <openssl/err.h>
18 # include <openssl/rand.h>
19 # include "crypto/rand.h"
20 # include "rand_local.h"
29 # include <gen64def.h>
32 # include <lib$routines.h>
34 # pragma message disable DOLLARID
37 # include <dlfcn.h> /* SYS$GET_ENTROPY presence */
39 # ifndef OPENSSL_RAND_SEED_OS
40 # error "Unsupported seeding method configured; must be os"
44 * DATA COLLECTION METHOD
45 * ======================
47 * This is a method to get low quality entropy.
48 * It works by collecting all kinds of statistical data that
49 * VMS offers and using them as random seed.
52 /* We need to make sure we have the right size pointer in some cases */
53 # if __INITIAL_POINTER_SIZE == 64
54 # pragma pointer_size save
55 # pragma pointer_size 32
57 typedef uint32_t *uint32_t__ptr32;
58 # if __INITIAL_POINTER_SIZE == 64
59 # pragma pointer_size restore
63 short length, code; /* length is number of bytes */
66 static const struct item_st DVI_item_data[] = {
71 static const struct item_st JPI_item_data[] = {
81 * Note: the direct result is just a 32-bit address. However, it points
82 * to a list of 4 32-bit words, so we make extra space for them so we can
83 * do in-place replacement of values
88 static const struct item_st JPI_item_data_64bit[] = {
89 {8, JPI$_LAST_LOGIN_I},
93 static const struct item_st RMI_item_data[] = {
165 /* We currently get a fault when trying these. TODO: To be figured out. */
167 {140, RMI$_MSCP_EVERYTHING}, /* 35 32-bit words */
168 {152, RMI$_DDTM_ALL}, /* 38 32-bit words */
169 {80, RMI$_TMSCP_EVERYTHING} /* 20 32-bit words */
171 {4, RMI$_LPZ_PAGCNT},
173 {4, RMI$_LPZ_MISSES},
174 {4, RMI$_LPZ_EXPCNT},
175 {4, RMI$_LPZ_ALLOCF},
176 {4, RMI$_LPZ_ALLOC2},
186 {4, RMI$_FILHDR_HIT},
187 {4, RMI$_DIRFCB_HIT},
188 {4, RMI$_DIRFCB_MISS},
189 {4, RMI$_DIRDATA_HIT},
194 {4, RMI$_STORAGMAP_HIT},
199 {4, RMI$_XQPCACHEWAIT},
200 {4, RMI$_DIRDATA_MISS},
201 {4, RMI$_FILHDR_MISS},
202 {4, RMI$_STORAGMAP_MISS},
203 {4, RMI$_PROCCNTMAX},
204 {4, RMI$_PROCBATCNT},
205 {4, RMI$_PROCINTCNT},
206 {4, RMI$_PROCNETCNT},
207 {4, RMI$_PROCSWITCHCNT},
208 {4, RMI$_PROCBALSETCNT},
209 {4, RMI$_PROCLOADCNT},
212 {4, RMI$_HDRINSWAPS},
213 {4, RMI$_HDROUTSWAPS},
227 {4, RMI$_BUFOBJPAGPEAK},
228 {4, RMI$_BUFOBJPAGS01},
229 {4, RMI$_BUFOBJPAGS2},
230 {4, RMI$_BUFOBJPAGMAXS01},
231 {4, RMI$_BUFOBJPAGMAXS2},
232 {4, RMI$_BUFOBJPAGPEAKS01},
233 {4, RMI$_BUFOBJPAGPEAKS2},
234 {4, RMI$_BUFOBJPGLTMAXS01},
235 {4, RMI$_BUFOBJPGLTMAXS2},
236 {4, RMI$_DLCK_INCMPLT},
237 {4, RMI$_DLCKMSGS_IN},
238 {4, RMI$_DLCKMSGS_OUT},
243 static const struct item_st RMI_item_data_64bit[] = {
248 {8, RMI$_LCKMGR_REQCNT},
249 {8, RMI$_LCKMGR_REQTIME},
250 {8, RMI$_LCKMGR_SPINCNT},
251 {8, RMI$_LCKMGR_SPINTIME},
253 {8, RMI$_CPUMPSYNCH},
263 {8, RMI$_TQEUSRTIMR},
264 {8, RMI$_TQEUSRWAKE},
267 static const struct item_st SYI_item_data[] = {
268 {4, SYI$_PAGEFILE_FREE},
273 * items_data - an array of lengths and codes
274 * items_data_num - number of elements in that array
277 * items - pre-allocated ILE3 array to be filled.
278 * It's assumed to have items_data_num elements plus
279 * one extra for the terminating NULL element
280 * databuffer - pre-allocated 32-bit word array.
282 * Returns the number of elements used in databuffer
284 static size_t prepare_item_list(const struct item_st *items_input,
285 size_t items_input_num,
287 uint32_t__ptr32 databuffer)
291 for (; items_input_num-- > 0; items_input++, items++) {
293 items->ile3$w_code = items_input->code;
294 /* Special treatment of JPI$_FINALEXC */
295 if (items->ile3$w_code == JPI$_FINALEXC)
296 items->ile3$w_length = 4;
298 items->ile3$w_length = items_input->length;
300 items->ile3$ps_bufaddr = databuffer;
301 items->ile3$ps_retlen_addr = 0;
303 databuffer += items_input->length / sizeof(databuffer[0]);
304 data_sz += items_input->length;
306 /* Terminating NULL entry */
307 items->ile3$w_length = items->ile3$w_code = 0;
308 items->ile3$ps_bufaddr = items->ile3$ps_retlen_addr = NULL;
310 return data_sz / sizeof(databuffer[0]);
313 static void massage_JPI(ILE3 *items)
316 * Special treatment of JPI$_FINALEXC
317 * The result of that item's data buffer is a 32-bit address to a list of
320 for (; items->ile3$w_length != 0; items++) {
321 if (items->ile3$w_code == JPI$_FINALEXC) {
322 uint32_t *data = items->ile3$ps_bufaddr;
323 uint32_t *ptr = (uint32_t *)*data;
327 * We know we made space for 4 32-bit words, so we can do in-place
330 for (j = 0; j < 4; j++)
339 * This number expresses how many bits of data contain 1 bit of entropy.
341 * For the moment, we assume about 0.05 entropy bits per data bit, or 1
342 * bit of entropy per 20 data bits.
344 #define ENTROPY_FACTOR 20
346 size_t data_collect_method(RAND_POOL *pool)
348 ILE3 JPI_items_64bit[OSSL_NELEM(JPI_item_data_64bit) + 1];
349 ILE3 RMI_items_64bit[OSSL_NELEM(RMI_item_data_64bit) + 1];
350 ILE3 DVI_items[OSSL_NELEM(DVI_item_data) + 1];
351 ILE3 JPI_items[OSSL_NELEM(JPI_item_data) + 1];
352 ILE3 RMI_items[OSSL_NELEM(RMI_item_data) + 1];
353 ILE3 SYI_items[OSSL_NELEM(SYI_item_data) + 1];
355 /* This ensures buffer starts at 64 bit boundary */
357 uint32_t buffer[OSSL_NELEM(JPI_item_data_64bit) * 2
358 + OSSL_NELEM(RMI_item_data_64bit) * 2
359 + OSSL_NELEM(DVI_item_data)
360 + OSSL_NELEM(JPI_item_data)
361 + OSSL_NELEM(RMI_item_data)
362 + OSSL_NELEM(SYI_item_data)
363 + 4 /* For JPI$_FINALEXC */];
365 size_t total_elems = 0;
366 size_t total_length = 0;
367 size_t bytes_needed = rand_pool_bytes_needed(pool, ENTROPY_FACTOR);
368 size_t bytes_remaining = rand_pool_bytes_remaining(pool);
370 /* Take all the 64-bit items first, to ensure proper alignment of data */
372 prepare_item_list(JPI_item_data_64bit, OSSL_NELEM(JPI_item_data_64bit),
373 JPI_items_64bit, &data.buffer[total_elems]);
375 prepare_item_list(RMI_item_data_64bit, OSSL_NELEM(RMI_item_data_64bit),
376 RMI_items_64bit, &data.buffer[total_elems]);
377 /* Now the 32-bit items */
378 total_elems += prepare_item_list(DVI_item_data, OSSL_NELEM(DVI_item_data),
379 DVI_items, &data.buffer[total_elems]);
380 total_elems += prepare_item_list(JPI_item_data, OSSL_NELEM(JPI_item_data),
381 JPI_items, &data.buffer[total_elems]);
382 total_elems += prepare_item_list(RMI_item_data, OSSL_NELEM(RMI_item_data),
383 RMI_items, &data.buffer[total_elems]);
384 total_elems += prepare_item_list(SYI_item_data, OSSL_NELEM(SYI_item_data),
385 SYI_items, &data.buffer[total_elems]);
386 total_length = total_elems * sizeof(data.buffer[0]);
388 /* Fill data.buffer with various info bits from this process */
393 $DESCRIPTOR(SYSDEVICE,"SYS$SYSDEVICE:");
395 if ((status = sys$getdviw(EFN$C_ENF, 0, &SYSDEVICE, DVI_items,
396 0, 0, 0, 0, 0)) != SS$_NORMAL) {
400 if ((status = sys$getjpiw(EFN$C_ENF, 0, 0, JPI_items_64bit, 0, 0, 0))
405 if ((status = sys$getjpiw(EFN$C_ENF, 0, 0, JPI_items, 0, 0, 0))
410 if ((status = sys$getsyiw(EFN$C_ENF, 0, 0, SYI_items, 0, 0, 0))
416 * The RMI service is a bit special, as there is no synchronous
417 * variant, so we MUST create an event flag to synchronise on.
419 if ((status = lib$get_ef(&efn)) != SS$_NORMAL) {
423 if ((status = sys$getrmi(efn, 0, 0, RMI_items_64bit, &iosb, 0, 0))
428 if ((status = sys$synch(efn, &iosb)) != SS$_NORMAL) {
432 if (iosb.iosb$l_getxxi_status != SS$_NORMAL) {
433 lib$signal(iosb.iosb$l_getxxi_status);
436 if ((status = sys$getrmi(efn, 0, 0, RMI_items, &iosb, 0, 0))
441 if ((status = sys$synch(efn, &iosb)) != SS$_NORMAL) {
445 if (iosb.iosb$l_getxxi_status != SS$_NORMAL) {
446 lib$signal(iosb.iosb$l_getxxi_status);
449 if ((status = lib$free_ef(&efn)) != SS$_NORMAL) {
455 massage_JPI(JPI_items);
458 * If we can't feed the requirements from the caller, we're in deep trouble.
460 if (!ossl_assert(total_length >= bytes_needed)) {
461 char buf[100]; /* That should be enough */
463 BIO_snprintf(buf, sizeof(buf), "Needed: %zu, Available: %zu",
464 bytes_needed, total_length);
465 RANDerr(RAND_F_DATA_COLLECT_METHOD, RAND_R_RANDOM_POOL_UNDERFLOW);
466 ERR_add_error_data(1, buf);
471 * Try not to overfeed the pool
473 if (total_length > bytes_remaining)
474 total_length = bytes_remaining;
476 /* We give the pessimistic value for the amount of entropy */
477 rand_pool_add(pool, (unsigned char *)data.buffer, total_length,
478 8 * total_length / ENTROPY_FACTOR);
479 return rand_pool_entropy_available(pool);
482 int rand_pool_add_nonce_data(RAND_POOL *pool)
486 CRYPTO_THREAD_ID tid;
487 unsigned __int64 time;
491 * Add process id, thread id, and a high resolution timestamp
492 * (where available, which is OpenVMS v8.4 and up) to ensure that
493 * the nonce is unique with high probability for different process
497 data.tid = CRYPTO_THREAD_get_current_id();
498 #if __CRTL_VER >= 80400000
499 sys$gettim_prec(&data.time);
501 sys$gettim((void*)&data.time);
504 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
508 * SYS$GET_ENTROPY METHOD
509 * ======================
511 * This is a high entropy method based on a new system service that is
512 * based on getentropy() from FreeBSD 12. It's only used if available,
513 * and its availability is detected at run-time.
515 * We assume that this function provides full entropy random output.
517 #define PUBLIC_VECTORS "SYS$LIBRARY:SYS$PUBLIC_VECTORS.EXE"
518 #define GET_ENTROPY "SYS$GET_ENTROPY"
520 static int get_entropy_address_flag = 0;
521 static int (*get_entropy_address)(void *buffer, size_t buffer_size) = NULL;
522 static int init_get_entropy_address(void)
524 if (get_entropy_address_flag == 0)
525 get_entropy_address = dlsym(dlopen(PUBLIC_VECTORS, 0), GET_ENTROPY);
526 get_entropy_address_flag = 1;
527 return get_entropy_address != NULL;
530 size_t get_entropy_method(RAND_POOL *pool)
533 * The documentation says that SYS$GET_ENTROPY will give a maximum of
536 unsigned char buffer[256];
538 size_t bytes_to_get = 0;
541 for (bytes_needed = rand_pool_bytes_needed(pool, 1);
543 bytes_needed -= bytes_to_get) {
545 bytes_needed > sizeof(buffer) ? sizeof(buffer) : bytes_needed;
547 status = get_entropy_address(buffer, bytes_to_get);
548 if (status == SS$_RETRY) {
549 /* Set to zero so the loop doesn't diminish |bytes_needed| */
551 /* Should sleep some amount of time */
555 if (status != SS$_NORMAL) {
560 rand_pool_add(pool, buffer, bytes_to_get, 8 * bytes_to_get);
563 return rand_pool_entropy_available(pool);
567 * MAIN ENTROPY ACQUISITION FUNCTIONS
568 * ==================================
570 * These functions are called by the RAND / DRBG functions
573 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
575 if (init_get_entropy_address())
576 return get_entropy_method(pool);
577 return data_collect_method(pool);
581 int rand_pool_add_additional_data(RAND_POOL *pool)
584 CRYPTO_THREAD_ID tid;
585 unsigned __int64 time;
589 * Add some noise from the thread id and a high resolution timer.
590 * The thread id adds a little randomness if the drbg is accessed
591 * concurrently (which is the case for the <master> drbg).
593 data.tid = CRYPTO_THREAD_get_current_id();
594 #if __CRTL_VER >= 80400000
595 sys$gettim_prec(&data.time);
597 sys$gettim((void*)&data.time);
600 return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
603 int rand_pool_init(void)
608 void rand_pool_cleanup(void)
612 void rand_pool_keep_random_devices_open(int keep)