Update copyright year
[openssl.git] / crypto / rand / rand_vms.c
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
10 #include "e_os.h"
11
12 #if defined(OPENSSL_SYS_VMS)
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
25 # ifndef OPENSSL_RAND_SEED_OS
26 #  error "Unsupported seeding method configured; must be os"
27 # endif
28
29 /*
30  * Use 32-bit pointers almost everywhere.  Define the type to which to cast a
31  * pointer passed to an external function.
32  */
33 # if __INITIAL_POINTER_SIZE == 64
34 #  define PTR_T __void_ptr64
35 #  pragma pointer_size save
36 #  pragma pointer_size 32
37 # else
38 #  define PTR_T void *
39 # endif
40
41 static struct items_data_st {
42     short length, code;         /* length is number of bytes */
43 } items_data[] = {
44     {4, JPI$_BUFIO},
45     {4, JPI$_CPUTIM},
46     {4, JPI$_DIRIO},
47     {4, JPI$_IMAGECOUNT},
48     {8, JPI$_LAST_LOGIN_I},
49     {8, JPI$_LOGINTIM},
50     {4, JPI$_PAGEFLTS},
51     {4, JPI$_PID},
52     {4, JPI$_PPGCNT},
53     {4, JPI$_WSPEAK},
54     {4, JPI$_FINALEXC},
55     {0, 0}
56 };
57
58 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
59 {
60     /* determine the number of items in the JPI array */
61     struct items_data_st item_entry;
62     int item_entry_count = OSSL_NELEM(items_data);
63     /* Create the JPI itemlist array to hold item_data content */
64     struct {
65         short length, code;
66         int *buffer;
67         int *retlen;
68     } item[item_entry_count], *pitem;
69     struct items_data_st *pitems_data;
70     int data_buffer[(item_entry_count * 2) + 4]; /* 8 bytes per entry max */
71     int iosb[2];
72     int sys_time[2];
73     int *ptr;
74     int i, j ;
75     int tmp_length   = 0;
76     int total_length = 0;
77
78     /* Setup itemlist for GETJPI */
79     pitems_data = items_data;
80     for (pitem = item; pitems_data->length != 0; pitem++) {
81         pitem->length = pitems_data->length;
82         pitem->code   = pitems_data->code;
83         pitem->buffer = &data_buffer[total_length];
84         pitem->retlen = 0;
85         /* total_length is in longwords */
86         total_length += pitems_data->length / 4;
87         pitems_data++;
88     }
89     pitem->length = pitem->code = 0;
90
91     /* Fill data_buffer with various info bits from this process */
92     if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) != SS$_NORMAL)
93         return 0;
94
95     /* Now twist that data to seed the SSL random number init */
96     for (i = 0; i < total_length; i++) {
97         sys$gettim((struct _generic_64 *)&sys_time[0]);
98         srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
99
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
114     total_length += (tmp_length - 1);
115
116     /*
117      * Size of seed is total_length*4 bytes (64bytes). The original assumption
118      * was that it contains 4 bits of entropy per byte. This makes a total
119      * amount of total_length*16 bits (256bits).
120      */
121     return rand_pool_add(pool,
122                          (PTR_T)data_buffer, total_length * 4,
123                          total_length * 16);
124 }
125
126 #endif