1109998Smarkm/* crypto/rand/rand_os2.c */ 2109998Smarkm/* ==================================================================== 3109998Smarkm * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 4109998Smarkm * 5109998Smarkm * Redistribution and use in source and binary forms, with or without 6109998Smarkm * modification, are permitted provided that the following conditions 7109998Smarkm * are met: 8109998Smarkm * 9109998Smarkm * 1. Redistributions of source code must retain the above copyright 10296465Sdelphij * notice, this list of conditions and the following disclaimer. 11109998Smarkm * 12109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright 13109998Smarkm * notice, this list of conditions and the following disclaimer in 14109998Smarkm * the documentation and/or other materials provided with the 15109998Smarkm * distribution. 16109998Smarkm * 17109998Smarkm * 3. All advertising materials mentioning features or use of this 18109998Smarkm * software must display the following acknowledgment: 19109998Smarkm * "This product includes software developed by the OpenSSL Project 20109998Smarkm * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21109998Smarkm * 22109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23109998Smarkm * endorse or promote products derived from this software without 24109998Smarkm * prior written permission. For written permission, please contact 25109998Smarkm * openssl-core@openssl.org. 26109998Smarkm * 27109998Smarkm * 5. Products derived from this software may not be called "OpenSSL" 28109998Smarkm * nor may "OpenSSL" appear in their names without prior written 29109998Smarkm * permission of the OpenSSL Project. 30109998Smarkm * 31109998Smarkm * 6. Redistributions of any form whatsoever must retain the following 32109998Smarkm * acknowledgment: 33109998Smarkm * "This product includes software developed by the OpenSSL Project 34109998Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35109998Smarkm * 36109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39109998Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 48109998Smarkm * ==================================================================== 49109998Smarkm * 50109998Smarkm * This product includes cryptographic software written by Eric Young 51109998Smarkm * (eay@cryptsoft.com). This product includes software written by Tim 52109998Smarkm * Hudson (tjh@cryptsoft.com). 53109998Smarkm * 54109998Smarkm */ 55109998Smarkm 56109998Smarkm#include "cryptlib.h" 57109998Smarkm#include <openssl/rand.h> 58109998Smarkm#include "rand_lcl.h" 59109998Smarkm 60109998Smarkm#ifdef OPENSSL_SYS_OS2 61109998Smarkm 62296465Sdelphij# define INCL_DOSPROCESS 63296465Sdelphij# define INCL_DOSPROFILE 64296465Sdelphij# define INCL_DOSMISC 65296465Sdelphij# define INCL_DOSMODULEMGR 66296465Sdelphij# include <os2.h> 67109998Smarkm 68296465Sdelphij# define CMD_KI_RDCNT (0x63) 69109998Smarkm 70109998Smarkmtypedef struct _CPUUTIL { 71296465Sdelphij ULONG ulTimeLow; /* Low 32 bits of time stamp */ 72296465Sdelphij ULONG ulTimeHigh; /* High 32 bits of time stamp */ 73296465Sdelphij ULONG ulIdleLow; /* Low 32 bits of idle time */ 74296465Sdelphij ULONG ulIdleHigh; /* High 32 bits of idle time */ 75296465Sdelphij ULONG ulBusyLow; /* Low 32 bits of busy time */ 76296465Sdelphij ULONG ulBusyHigh; /* High 32 bits of busy time */ 77296465Sdelphij ULONG ulIntrLow; /* Low 32 bits of interrupt time */ 78109998Smarkm ULONG ulIntrHigh; /* High 32 bits of interrupt time */ 79109998Smarkm} CPUUTIL; 80109998Smarkm 81296465SdelphijAPIRET APIENTRY(*DosPerfSysCall) (ULONG ulCommand, ULONG ulParm1, 82296465Sdelphij ULONG ulParm2, ULONG ulParm3) = NULL; 83296465SdelphijAPIRET APIENTRY(*DosQuerySysState) (ULONG func, ULONG arg1, ULONG pid, 84296465Sdelphij ULONG _res_, PVOID buf, ULONG bufsz) = 85296465Sdelphij NULL; 86109998SmarkmHMODULE hDoscalls = 0; 87109998Smarkm 88109998Smarkmint RAND_poll(void) 89109998Smarkm{ 90109998Smarkm char failed_module[20]; 91109998Smarkm QWORD qwTime; 92109998Smarkm ULONG SysVars[QSV_FOREGROUND_PROCESS]; 93109998Smarkm 94109998Smarkm if (hDoscalls == 0) { 95296465Sdelphij ULONG rc = 96296465Sdelphij DosLoadModule(failed_module, sizeof(failed_module), "DOSCALLS", 97296465Sdelphij &hDoscalls); 98109998Smarkm 99109998Smarkm if (rc == 0) { 100296465Sdelphij rc = DosQueryProcAddr(hDoscalls, 976, NULL, 101296465Sdelphij (PFN *) & DosPerfSysCall); 102109998Smarkm 103109998Smarkm if (rc) 104109998Smarkm DosPerfSysCall = NULL; 105109998Smarkm 106296465Sdelphij rc = DosQueryProcAddr(hDoscalls, 368, NULL, 107296465Sdelphij (PFN *) & DosQuerySysState); 108109998Smarkm 109109998Smarkm if (rc) 110109998Smarkm DosQuerySysState = NULL; 111109998Smarkm } 112109998Smarkm } 113109998Smarkm 114109998Smarkm /* Sample the hi-res timer, runs at around 1.1 MHz */ 115109998Smarkm DosTmrQueryTime(&qwTime); 116109998Smarkm RAND_add(&qwTime, sizeof(qwTime), 2); 117109998Smarkm 118296465Sdelphij /* 119296465Sdelphij * Sample a bunch of system variables, includes various process & memory 120296465Sdelphij * statistics 121296465Sdelphij */ 122109998Smarkm DosQuerySysInfo(1, QSV_FOREGROUND_PROCESS, SysVars, sizeof(SysVars)); 123109998Smarkm RAND_add(SysVars, sizeof(SysVars), 4); 124109998Smarkm 125296465Sdelphij /* 126296465Sdelphij * If available, sample CPU registers that count at CPU MHz Only fairly 127296465Sdelphij * new CPUs (PPro & K6 onwards) & OS/2 versions support this 128109998Smarkm */ 129109998Smarkm if (DosPerfSysCall) { 130109998Smarkm CPUUTIL util; 131109998Smarkm 132296465Sdelphij if (DosPerfSysCall(CMD_KI_RDCNT, (ULONG) & util, 0, 0) == 0) { 133109998Smarkm RAND_add(&util, sizeof(util), 10); 134296465Sdelphij } else { 135109998Smarkm DosPerfSysCall = NULL; 136109998Smarkm } 137109998Smarkm } 138109998Smarkm 139296465Sdelphij /* 140296465Sdelphij * DosQuerySysState() gives us a huge quantity of process, thread, memory 141296465Sdelphij * & handle stats 142296465Sdelphij */ 143109998Smarkm if (DosQuerySysState) { 144109998Smarkm char *buffer = OPENSSL_malloc(256 * 1024); 145109998Smarkm 146109998Smarkm if (DosQuerySysState(0x1F, 0, 0, 0, buffer, 256 * 1024) == 0) { 147296465Sdelphij /* 148296465Sdelphij * First 4 bytes in buffer is a pointer to the thread count there 149296465Sdelphij * should be at least 1 byte of entropy per thread 150109998Smarkm */ 151296465Sdelphij RAND_add(buffer, 256 * 1024, **(ULONG **) buffer); 152109998Smarkm } 153109998Smarkm 154109998Smarkm OPENSSL_free(buffer); 155109998Smarkm return 1; 156109998Smarkm } 157109998Smarkm 158109998Smarkm return 0; 159109998Smarkm} 160109998Smarkm 161296465Sdelphij#endif /* OPENSSL_SYS_OS2 */ 162