2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include <openssl/opensslconf.h>
12 #include <openssl/crypto.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/rand.h>
20 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
21 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
26 int RAND_egd(const char *path)
31 int RAND_egd_bytes(const char *path, int bytes)
40 # include <sys/types.h>
41 # include <sys/socket.h>
43 # ifdef OPENSSL_SYS_VXWORKS
44 # include <streams/un.h>
50 short sun_family; /* AF_UNIX */
51 char sun_path[108]; /* path name (gag) */
53 # endif /* NO_SYS_UN_H */
57 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
60 struct sockaddr_un addr;
61 int mybuffer, ret = -1, i, numbytes, fd;
62 unsigned char tempbuf[255];
64 if (bytes > (int)sizeof(tempbuf))
68 memset(&addr, 0, sizeof(addr));
69 addr.sun_family = AF_UNIX;
70 if (strlen(path) >= sizeof(addr.sun_path))
72 strcpy(addr.sun_path, path);
73 i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
74 fd = socket(AF_UNIX, SOCK_STREAM, 0);
75 if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
81 if (connect(fd, (struct sockaddr *)&addr, i) == 0)
100 /* No error, try again */
108 /* Make request, see how many bytes we can get back. */
111 if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
113 if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
115 numbytes = tempbuf[0];
117 /* Which buffer are we using? */
118 mybuffer = buf == NULL;
123 i = fread(buf, sizeof(char), numbytes, fp);
128 RAND_add(tempbuf, i, i);
136 int RAND_egd_bytes(const char *path, int bytes)
140 num = RAND_query_egd_bytes(path, NULL, bytes);
143 if (RAND_status() != 1)
148 int RAND_egd(const char *path)
150 return RAND_egd_bytes(path, 255);