bn/asm/x86_64-mont5.pl: fix carry bug in bn_sqrx8x_internal.
[openssl.git] / crypto / rand / rand_vms.c
1 /* crypto/rand/rand_vms.c */
2 /*
3  * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
4  * 2000.
5  */
6 /*
7  * Modified by VMS Software, Inc (2016)
8  *    Eliminate looping through all processes (performance)
9  *    Add additional randomizations using rand() function
10  */
11 /* ====================================================================
12  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in
23  *    the documentation and/or other materials provided with the
24  *    distribution.
25  *
26  * 3. All advertising materials mentioning features or use of this
27  *    software must display the following acknowledgment:
28  *    "This product includes software developed by the OpenSSL Project
29  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
30  *
31  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
32  *    endorse or promote products derived from this software without
33  *    prior written permission. For written permission, please contact
34  *    openssl-core@openssl.org.
35  *
36  * 5. Products derived from this software may not be called "OpenSSL"
37  *    nor may "OpenSSL" appear in their names without prior written
38  *    permission of the OpenSSL Project.
39  *
40  * 6. Redistributions of any form whatsoever must retain the following
41  *    acknowledgment:
42  *    "This product includes software developed by the OpenSSL Project
43  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
46  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
49  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
54  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
56  * OF THE POSSIBILITY OF SUCH DAMAGE.
57  * ====================================================================
58  *
59  * This product includes cryptographic software written by Eric Young
60  * (eay@cryptsoft.com).  This product includes software written by Tim
61  * Hudson (tjh@cryptsoft.com).
62  *
63  */
64
65 #include <openssl/rand.h>
66 #include "rand_lcl.h"
67
68 #if defined(OPENSSL_SYS_VMS)
69 # include <descrip.h>
70 # include <jpidef.h>
71 # include <ssdef.h>
72 # include <starlet.h>
73 # include <efndef>
74 # ifdef __DECC
75 #  pragma message disable DOLLARID
76 # endif
77
78 /*
79  * Use 32-bit pointers almost everywhere.  Define the type to which to cast a
80  * pointer passed to an external function.
81  */
82 # if __INITIAL_POINTER_SIZE == 64
83 #  define PTR_T __void_ptr64
84 #  pragma pointer_size save
85 #  pragma pointer_size 32
86 # else                          /* __INITIAL_POINTER_SIZE == 64 */
87 #  define PTR_T void *
88 # endif                         /* __INITIAL_POINTER_SIZE == 64 [else] */
89
90 static struct items_data_st {
91     short length, code;         /* length is number of bytes */
92 } items_data[] = {
93     {4, JPI$_BUFIO},
94     {4, JPI$_CPUTIM},
95     {4, JPI$_DIRIO},
96     {4, JPI$_IMAGECOUNT},
97     {8, JPI$_LAST_LOGIN_I},
98     {8, JPI$_LOGINTIM},
99     {4, JPI$_PAGEFLTS},
100     {4, JPI$_PID},
101     {4, JPI$_PPGCNT},
102     {4, JPI$_WSPEAK},
103     {4, JPI$_FINALEXC},
104     {0, 0}                      /* zero terminated */
105 };
106
107 int RAND_poll(void)
108 {
109
110     /* determine the number of items in the JPI array */
111
112     struct items_data_st item_entry;
113     int item_entry_count = sizeof(items_data)/sizeof(item_entry);
114
115     /* Create the JPI itemlist array to hold item_data content */
116
117     struct {
118         short length, code;
119         int *buffer;
120         int *retlen;
121     } item[item_entry_count], *pitem; /* number of entries in items_data */
122
123     struct items_data_st *pitems_data;
124     pitems_data = items_data;
125     pitem = item;
126     int data_buffer[(item_entry_count*2)+4]; /* 8 bytes per entry max */
127     int iosb[2];
128     int sys_time[2];
129     int *ptr;
130     int i, j ;
131     int tmp_length   = 0;
132     int total_length = 0;
133
134     /* Setup itemlist for GETJPI */
135
136     while (pitems_data->length) {
137         pitem->length = pitems_data->length;
138         pitem->code   = pitems_data->code;
139         pitem->buffer = &data_buffer[total_length];
140         pitem->retlen = 0;
141         /* total_length is in longwords */
142         total_length += pitems_data->length/4;
143         pitems_data++;
144         pitem ++;
145     }
146     pitem->length = pitem->code = 0;
147
148     /* Fill data_buffer with various info bits from this process */
149     /* and twist that data to seed the SSL random number init    */
150
151     if (sys$getjpiw(EFN$C_ENF, NULL, NULL, item, &iosb, 0, 0) == SS$_NORMAL) {
152         for (i = 0; i < total_length; i++) {
153             sys$gettim((struct _generic_64 *)&sys_time[0]);
154             srand(sys_time[0] * data_buffer[0] * data_buffer[1] + i);
155
156             if (i == (total_length - 1)) { /* for JPI$_FINALEXC */
157                 ptr = &data_buffer[i];
158                 for (j = 0; j < 4; j++) {
159                     data_buffer[i + j] = ptr[j];
160                     /* OK to use rand() just to scramble the seed */
161                     data_buffer[i + j] ^= (sys_time[0] ^ rand());
162                     tmp_length++;
163                 }
164             } else {
165                 /* OK to use rand() just to scramble the seed */
166                 data_buffer[i] ^= (sys_time[0] ^ rand());
167             }
168         }
169
170         total_length += (tmp_length - 1);
171
172         /* size of seed is total_length*4 bytes (64bytes) */
173         RAND_add((PTR_T) data_buffer, total_length*4, total_length * 2);
174     } else {
175         return 0;
176     }
177
178     return 1;
179 }
180 #endif