sl@0: /* crypto/rand/rand_egd.c */ sl@0: /* Written by Ulf Moeller and Lutz Jaenicke for the OpenSSL project. */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * openssl-core@openssl.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.openssl.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* sl@0: * Query the EGD . sl@0: * sl@0: * This module supplies three routines: sl@0: * sl@0: * RAND_query_egd_bytes(path, buf, bytes) sl@0: * will actually query "bytes" bytes of entropy form the egd-socket located sl@0: * at path and will write them to buf (if supplied) or will directly feed sl@0: * it to RAND_seed() if buf==NULL. sl@0: * The number of bytes is not limited by the maximum chunk size of EGD, sl@0: * which is 255 bytes. If more than 255 bytes are wanted, several chunks sl@0: * of entropy bytes are requested. The connection is left open until the sl@0: * query is competed. sl@0: * RAND_query_egd_bytes() returns with sl@0: * -1 if an error occured during connection or communication. sl@0: * num the number of bytes read from the EGD socket. This number is either sl@0: * the number of bytes requested or smaller, if the EGD pool is sl@0: * drained and the daemon signals that the pool is empty. sl@0: * This routine does not touch any RAND_status(). This is necessary, since sl@0: * PRNG functions may call it during initialization. sl@0: * sl@0: * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them sl@0: * used to seed the PRNG. sl@0: * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL. sl@0: * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the sl@0: * seed status so that the return value can reflect the seed state: sl@0: * -1 if an error occured during connection or communication _or_ sl@0: * if the PRNG has still not received the required seeding. sl@0: * num the number of bytes read from the EGD socket. This number is either sl@0: * the number of bytes requested or smaller, if the EGD pool is sl@0: * drained and the daemon signals that the pool is empty. sl@0: * sl@0: * RAND_egd(path) will query 255 bytes and use the bytes retreived to seed sl@0: * the PRNG. sl@0: * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #if defined(SYMBIAN)||defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_VOS) sl@0: EXPORT_C int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes) sl@0: { sl@0: return(-1); sl@0: } sl@0: EXPORT_C int RAND_egd(const char *path) sl@0: { sl@0: return(-1); sl@0: } sl@0: sl@0: EXPORT_C int RAND_egd_bytes(const char *path,int bytes) sl@0: { sl@0: return(-1); sl@0: } sl@0: sl@0: #else sl@0: #include sl@0: #include OPENSSL_UNISTD sl@0: #include sl@0: #include sl@0: #ifndef NO_SYS_UN_H sl@0: # ifdef OPENSSL_SYS_VXWORKS sl@0: # include sl@0: # else sl@0: # include sl@0: # endif sl@0: #else sl@0: struct sockaddr_un { sl@0: short sun_family; /* AF_UNIX */ sl@0: char sun_path[108]; /* path name (gag) */ sl@0: }; sl@0: #endif /* NO_SYS_UN_H */ sl@0: #include sl@0: #include sl@0: sl@0: #ifndef offsetof sl@0: # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) sl@0: #endif sl@0: sl@0: int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes) sl@0: { sl@0: int ret = 0; sl@0: struct sockaddr_un addr; sl@0: int len, num, numbytes; sl@0: int fd = -1; sl@0: int success; sl@0: unsigned char egdbuf[2], tempbuf[255], *retrievebuf; sl@0: sl@0: memset(&addr, 0, sizeof(addr)); sl@0: addr.sun_family = AF_UNIX; sl@0: if (strlen(path) >= sizeof(addr.sun_path)) sl@0: return (-1); sl@0: BUF_strlcpy(addr.sun_path,path,sizeof addr.sun_path); sl@0: len = offsetof(struct sockaddr_un, sun_path) + strlen(path); sl@0: fd = socket(AF_UNIX, SOCK_STREAM, 0); sl@0: if (fd == -1) return (-1); sl@0: success = 0; sl@0: while (!success) sl@0: { sl@0: if (connect(fd, (struct sockaddr *)&addr, len) == 0) sl@0: success = 1; sl@0: else sl@0: { sl@0: switch (errno) sl@0: { sl@0: #ifdef EINTR sl@0: case EINTR: sl@0: #endif sl@0: #ifdef EAGAIN sl@0: case EAGAIN: sl@0: #endif sl@0: #ifdef EINPROGRESS sl@0: case EINPROGRESS: sl@0: #endif sl@0: #ifdef EALREADY sl@0: case EALREADY: sl@0: #endif sl@0: /* No error, try again */ sl@0: break; sl@0: #ifdef EISCONN sl@0: case EISCONN: sl@0: success = 1; sl@0: break; sl@0: #endif sl@0: default: sl@0: goto err; /* failure */ sl@0: } sl@0: } sl@0: } sl@0: sl@0: while(bytes > 0) sl@0: { sl@0: egdbuf[0] = 1; sl@0: egdbuf[1] = bytes < 255 ? bytes : 255; sl@0: numbytes = 0; sl@0: while (numbytes != 2) sl@0: { sl@0: num = write(fd, egdbuf + numbytes, 2 - numbytes); sl@0: if (num >= 0) sl@0: numbytes += num; sl@0: else sl@0: { sl@0: switch (errno) sl@0: { sl@0: #ifdef EINTR sl@0: case EINTR: sl@0: #endif sl@0: #ifdef EAGAIN sl@0: case EAGAIN: sl@0: #endif sl@0: /* No error, try again */ sl@0: break; sl@0: default: sl@0: ret = -1; sl@0: goto err; /* failure */ sl@0: } sl@0: } sl@0: } sl@0: numbytes = 0; sl@0: while (numbytes != 1) sl@0: { sl@0: num = read(fd, egdbuf, 1); sl@0: if (num == 0) sl@0: goto err; /* descriptor closed */ sl@0: else if (num > 0) sl@0: numbytes += num; sl@0: else sl@0: { sl@0: switch (errno) sl@0: { sl@0: #ifdef EINTR sl@0: case EINTR: sl@0: #endif sl@0: #ifdef EAGAIN sl@0: case EAGAIN: sl@0: #endif sl@0: /* No error, try again */ sl@0: break; sl@0: default: sl@0: ret = -1; sl@0: goto err; /* failure */ sl@0: } sl@0: } sl@0: } sl@0: if(egdbuf[0] == 0) sl@0: goto err; sl@0: if (buf) sl@0: retrievebuf = buf + ret; sl@0: else sl@0: retrievebuf = tempbuf; sl@0: numbytes = 0; sl@0: while (numbytes != egdbuf[0]) sl@0: { sl@0: num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes); sl@0: if (num == 0) sl@0: goto err; /* descriptor closed */ sl@0: else if (num > 0) sl@0: numbytes += num; sl@0: else sl@0: { sl@0: switch (errno) sl@0: { sl@0: #ifdef EINTR sl@0: case EINTR: sl@0: #endif sl@0: #ifdef EAGAIN sl@0: case EAGAIN: sl@0: #endif sl@0: /* No error, try again */ sl@0: break; sl@0: default: sl@0: ret = -1; sl@0: goto err; /* failure */ sl@0: } sl@0: } sl@0: } sl@0: ret += egdbuf[0]; sl@0: bytes -= egdbuf[0]; sl@0: if (!buf) sl@0: RAND_seed(tempbuf, egdbuf[0]); sl@0: } sl@0: err: sl@0: if (fd != -1) close(fd); sl@0: return(ret); sl@0: } sl@0: sl@0: sl@0: int RAND_egd_bytes(const char *path, int bytes) sl@0: { sl@0: int num, ret = 0; sl@0: sl@0: num = RAND_query_egd_bytes(path, NULL, bytes); sl@0: if (num < 1) goto err; sl@0: if (RAND_status() == 1) sl@0: ret = num; sl@0: err: sl@0: return(ret); sl@0: } sl@0: sl@0: sl@0: int RAND_egd(const char *path) sl@0: { sl@0: return (RAND_egd_bytes(path, 255)); sl@0: } sl@0: sl@0: sl@0: #endif