Change rand_pool_bytes_needed to handle less entropy than 1 per 8 bits
[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 <unistd.h>
14 # include "internal/cryptlib.h"
15 # include <openssl/rand.h>
16 # include "internal/rand_int.h"
17 # include "rand_lcl.h"
18 # include <descrip.h>
19 # include <jpidef.h>
20 # include <ssdef.h>
21 # include <starlet.h>
22 # include <efndef>
23 # ifdef __DECC
24 #  pragma message disable DOLLARID
25 # endif
26
27 # ifndef OPENSSL_RAND_SEED_OS
28 #  error "Unsupported seeding method configured; must be os"
29 # endif
30
31 /*
32  * Use 32-bit pointers almost everywhere.  Define the type to which to cast a
33  * pointer passed to an external function.
34  */
35 # if __INITIAL_POINTER_SIZE == 64
36 #  define PTR_T __void_ptr64
37 #  pragma pointer_size save
38 #  pragma pointer_size 32
39 # else
40 #  define PTR_T void *
41 # endif
42
43 static struct items_data_st {
44     short length, code;         /* length is number of bytes */
45 } items_data[] = {
46     {4, JPI$_BUFIO},
47     {4, JPI$_CPUTIM},
48     {4, JPI$_DIRIO},
49     {4, JPI$_IMAGECOUNT},
50     {8, JPI$_LAST_LOGIN_I},
51     {8, JPI$_LOGINTIM},
52     {4, JPI$_PAGEFLTS},
53     {4, JPI$_PID},
54     {4, JPI$_PPGCNT},
55     {4, JPI$_WSPEAK},
56     {4, JPI$_FINALEXC},
57     {0, 0}
58 };
59
60 /*
61  * This number expresses how many bits of data contain 1 bit of entropy.
62  *
63  * For the moment, we assume about 0.5 entropy bits per data bit, or 1
64  * bit of entropy per 2 data bits.
65  */
66 #define ENTROPY_FACTOR  2
67
68 size_t rand_pool_acquire_entropy(RAND_POOL *pool)
69 {
70     /* determine the number of items in the JPI array */
71     struct items_data_st item_entry;
72     size_t item_entry_count = OSSL_NELEM(items_data);
73     /* Create the 32-bit JPI itemlist array to hold item_data content */
74     struct {
75         uint16_t length, code;
76         uint32_t *buffer;
77         uint32_t *retlen;
78     } item[item_entry_count], *pitem;
79     struct items_data_st *pitems_data;
80     /* 8 bytes (two longs) per entry max */
81     uint32_t data_buffer[(item_entry_count * 2) + 4];
82     uint32_t iosb[2];
83     uint32_t sys_time[2];
84     uint32_t *ptr;
85     size_t i, j ;
86     size_t tmp_length   = 0;
87     size_t total_length = 0;
88     size_t bytes_needed = rand_pool_bytes_needed(pool, ENTROPY_FACTOR);
89     size_t bytes_remaining = rand_pool_bytes_remaining(pool);
90
91     /* Setup itemlist for GETJPI */
92     pitems_data = items_data;
93     for (pitem = item; pitems_data->length != 0; pitem++) {
94         pitem->length = pitems_data->length;
95         pitem->code   = pitems_data->code;
96         pitem->buffer = &data_buffer[total_length];
97         pitem->retlen = 0;
98         /* total_length is in longwords */
99         total_length += pitems_data->length / 4;
100         pitems_data++;
101     }
102     pitem->length = pitem->code = 0;
103
104     /* Fill data_buffer with various info bits from this process */
105     if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) != SS$_NORMAL)
106         return 0;
107
108     /* Now twist that data to seed the SSL random number init */
109     for (i = 0; i < total_length; i++) {
110         sys$gettim((struct _generic_64 *)&sys_time[0]);
111         srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
112
113         if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
114             ptr = &data_buffer[i];
115             for (j = 0; j < 4; j++) {
116                 data_buffer[i + j] = ptr[j];
117                 /* OK to use rand() just to scramble the seed */
118                 data_buffer[i + j] ^= (sys_time[0] ^ rand());
119                 tmp_length++;
120             }
121         } else {
122             /* OK to use rand() just to scramble the seed */
123             data_buffer[i] ^= (sys_time[0] ^ rand());
124         }
125     }
126
127     total_length += (tmp_length - 1);
128
129     /* Change the total length to number of bytes */
130     total_length *= 4;
131
132     /*
133      * If we can't feed the requirements from the caller, we're in deep trouble.
134      */
135     if (!ossl_assert(total_length >= bytes_needed)) {
136         char neededstr[20];
137         char availablestr[20];
138
139         BIO_snprintf(neededstr, sizeof(neededstr), "%zu", bytes_needed);
140         BIO_snprintf(availablestr, sizeof(availablestr), "%zu", total_length);
141         RANDerr(RAND_F_RAND_POOL_ACQUIRE_ENTROPY,
142                 RAND_R_RANDOM_POOL_UNDERFLOW);
143         ERR_add_error_data(4, "Needed: ", neededstr, ", Available: ",
144                            availablestr);
145         return 0;
146     }
147
148     /*
149      * Try not to overfeed the pool
150      */
151     if (total_length > bytes_remaining)
152         total_length = bytes_remaining;
153
154     /* We give the pessimistic value for the amount of entropy */
155     rand_pool_add(pool, (PTR_T)data_buffer, total_length,
156                   total_length / ENTROPY_FACTOR);
157     return rand_pool_entropy_available(pool);
158 }
159
160 int rand_pool_add_nonce_data(RAND_POOL *pool)
161 {
162     struct {
163         pid_t pid;
164         CRYPTO_THREAD_ID tid;
165         uint64_t time;
166     } data = { 0 };
167
168     /*
169      * Add process id, thread id, and a high resolution timestamp to
170      * ensure that the nonce is unique whith high probability for
171      * different process instances.
172      */
173     data.pid = getpid();
174     data.tid = CRYPTO_THREAD_get_current_id();
175     sys$gettim_prec((struct _generic_64 *)&data.time);
176
177     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
178 }
179
180 int rand_pool_add_additional_data(RAND_POOL *pool)
181 {
182     struct {
183         CRYPTO_THREAD_ID tid;
184         uint64_t time;
185     } data = { 0 };
186
187     /*
188      * Add some noise from the thread id and a high resolution timer.
189      * The thread id adds a little randomness if the drbg is accessed
190      * concurrently (which is the case for the <master> drbg).
191      */
192     data.tid = CRYPTO_THREAD_get_current_id();
193     sys$gettim_prec((struct _generic_64 *)&data.time);
194
195     return rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0);
196 }
197
198 #endif