1160814Ssimon/* crypto/rand/rand_nw.c */
2160814Ssimon/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3160814Ssimon * All rights reserved.
4160814Ssimon *
5160814Ssimon * This package is an SSL implementation written
6160814Ssimon * by Eric Young (eay@cryptsoft.com).
7160814Ssimon * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
9160814Ssimon * This library is free for commercial and non-commercial use as long as
10160814Ssimon * the following conditions are aheared to.  The following conditions
11160814Ssimon * apply to all code found in this distribution, be it the RC4, RSA,
12160814Ssimon * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13160814Ssimon * included with this distribution is covered by the same copyright terms
14160814Ssimon * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
16160814Ssimon * Copyright remains Eric Young's, and as such any Copyright notices in
17160814Ssimon * the code are not to be removed.
18160814Ssimon * If this package is used in a product, Eric Young should be given attribution
19160814Ssimon * as the author of the parts of the library used.
20160814Ssimon * This can be in the form of a textual message at program startup or
21160814Ssimon * in documentation (online or textual) provided with the package.
22296465Sdelphij *
23160814Ssimon * Redistribution and use in source and binary forms, with or without
24160814Ssimon * modification, are permitted provided that the following conditions
25160814Ssimon * are met:
26160814Ssimon * 1. Redistributions of source code must retain the copyright
27160814Ssimon *    notice, this list of conditions and the following disclaimer.
28160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
29160814Ssimon *    notice, this list of conditions and the following disclaimer in the
30160814Ssimon *    documentation and/or other materials provided with the distribution.
31160814Ssimon * 3. All advertising materials mentioning features or use of this software
32160814Ssimon *    must display the following acknowledgement:
33160814Ssimon *    "This product includes cryptographic software written by
34160814Ssimon *     Eric Young (eay@cryptsoft.com)"
35160814Ssimon *    The word 'cryptographic' can be left out if the rouines from the library
36160814Ssimon *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
38160814Ssimon *    the apps directory (application code) you must include an acknowledgement:
39160814Ssimon *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
41160814Ssimon * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44160814Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51160814Ssimon * SUCH DAMAGE.
52296465Sdelphij *
53160814Ssimon * The licence and distribution terms for any publically available version or
54160814Ssimon * derivative of this code cannot be changed.  i.e. this code cannot simply be
55160814Ssimon * copied and put under another distribution licence
56160814Ssimon * [including the GNU Public Licence.]
57160814Ssimon */
58160814Ssimon/* ====================================================================
59160814Ssimon * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
60160814Ssimon *
61160814Ssimon * Redistribution and use in source and binary forms, with or without
62160814Ssimon * modification, are permitted provided that the following conditions
63160814Ssimon * are met:
64160814Ssimon *
65160814Ssimon * 1. Redistributions of source code must retain the above copyright
66296465Sdelphij *    notice, this list of conditions and the following disclaimer.
67160814Ssimon *
68160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
69160814Ssimon *    notice, this list of conditions and the following disclaimer in
70160814Ssimon *    the documentation and/or other materials provided with the
71160814Ssimon *    distribution.
72160814Ssimon *
73160814Ssimon * 3. All advertising materials mentioning features or use of this
74160814Ssimon *    software must display the following acknowledgment:
75160814Ssimon *    "This product includes software developed by the OpenSSL Project
76160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77160814Ssimon *
78160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79160814Ssimon *    endorse or promote products derived from this software without
80160814Ssimon *    prior written permission. For written permission, please contact
81160814Ssimon *    openssl-core@openssl.org.
82160814Ssimon *
83160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
84160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
85160814Ssimon *    permission of the OpenSSL Project.
86160814Ssimon *
87160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
88160814Ssimon *    acknowledgment:
89160814Ssimon *    "This product includes software developed by the OpenSSL Project
90160814Ssimon *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91160814Ssimon *
92160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
104160814Ssimon * ====================================================================
105160814Ssimon *
106160814Ssimon * This product includes cryptographic software written by Eric Young
107160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
108160814Ssimon * Hudson (tjh@cryptsoft.com).
109160814Ssimon *
110160814Ssimon */
111160814Ssimon
112160814Ssimon#include "cryptlib.h"
113160814Ssimon#include <openssl/rand.h>
114160814Ssimon#include "rand_lcl.h"
115160814Ssimon
116160814Ssimon#if defined (OPENSSL_SYS_NETWARE)
117160814Ssimon
118296465Sdelphij# if defined(NETWARE_LIBC)
119296465Sdelphij#  include <nks/thread.h>
120296465Sdelphij# else
121296465Sdelphij#  include <nwthread.h>
122296465Sdelphij# endif
123160814Ssimon
124194206Ssimonextern int GetProcessSwitchCount(void);
125296465Sdelphij# if !defined(NETWARE_LIBC) || (CURRENT_NDK_THRESHOLD < 509220000)
126296465Sdelphijextern void *RunningProcess;    /* declare here same as found in newer NDKs */
127194206Ssimonextern unsigned long GetSuperHighResolutionTimer(void);
128296465Sdelphij# endif
129160814Ssimon
130296465Sdelphij   /*
131296465Sdelphij    * the FAQ indicates we need to provide at least 20 bytes (160 bits) of
132296465Sdelphij    * seed
133296465Sdelphij    */
134160814Ssimonint RAND_poll(void)
135160814Ssimon{
136296465Sdelphij    unsigned long l;
137296465Sdelphij    unsigned long tsc;
138296465Sdelphij    int i;
139160814Ssimon
140296465Sdelphij    /*
141296465Sdelphij     * There are several options to gather miscellaneous data but for now we
142296465Sdelphij     * will loop checking the time stamp counter (rdtsc) and the
143296465Sdelphij     * SuperHighResolutionTimer.  Each iteration will collect 8 bytes of data
144296465Sdelphij     * but it is treated as only 1 byte of entropy.  The call to
145296465Sdelphij     * ThreadSwitchWithDelay() will introduce additional variability into the
146296465Sdelphij     * data returned by rdtsc. Applications can agument the seed material by
147296465Sdelphij     * adding additional stuff with RAND_add() and should probably do so.
148296465Sdelphij     */
149296465Sdelphij    l = GetProcessSwitchCount();
150296465Sdelphij    RAND_add(&l, sizeof(l), 1);
151160814Ssimon
152296465Sdelphij    /* need to cast the void* to unsigned long here */
153296465Sdelphij    l = (unsigned long)RunningProcess;
154296465Sdelphij    RAND_add(&l, sizeof(l), 1);
155160814Ssimon
156296465Sdelphij    for (i = 2; i < ENTROPY_NEEDED; i++) {
157296465Sdelphij# ifdef __MWERKS__
158296465Sdelphij        asm {
159296465Sdelphij        rdtsc mov tsc, eax}
160296465Sdelphij# elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
161296465Sdelphij        asm volatile ("rdtsc":"=a" (tsc)::"edx");
162296465Sdelphij# endif
163160814Ssimon
164296465Sdelphij        RAND_add(&tsc, sizeof(tsc), 1);
165160814Ssimon
166296465Sdelphij        l = GetSuperHighResolutionTimer();
167296465Sdelphij        RAND_add(&l, sizeof(l), 0);
168296465Sdelphij
169160814Ssimon# if defined(NETWARE_LIBC)
170296465Sdelphij        NXThreadYield();
171296465Sdelphij# else                          /* NETWARE_CLIB */
172296465Sdelphij        ThreadSwitchWithDelay();
173160814Ssimon# endif
174296465Sdelphij    }
175160814Ssimon
176296465Sdelphij    return 1;
177160814Ssimon}
178160814Ssimon
179296465Sdelphij#endif
180