summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rand/randfile.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/rand/randfile.c102
1 files changed, 53 insertions, 49 deletions
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
index 5326f710c5..0689908ce0 100644
--- a/src/lib/libcrypto/rand/randfile.c
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -75,7 +75,8 @@
75 75
76/* Note that these functions should not be used. */ 76/* Note that these functions should not be used. */
77 77
78int RAND_load_file(const char *file, long bytes) 78int
79RAND_load_file(const char *file, long bytes)
79{ 80{
80 /* the "whole" file */ 81 /* the "whole" file */
81 if (bytes == -1) 82 if (bytes == -1)
@@ -84,65 +85,68 @@ int RAND_load_file(const char *file, long bytes)
84 return bytes; 85 return bytes;
85} 86}
86 87
87int RAND_write_file(const char *file) 88int
88 { 89RAND_write_file(const char *file)
90{
89 unsigned char buf[BUFSIZE]; 91 unsigned char buf[BUFSIZE];
90 int i,ret=0,rand_err=0; 92 int i, ret = 0, rand_err = 0;
91 FILE *out = NULL; 93 FILE *out = NULL;
92 int n; 94 int n;
93 struct stat sb; 95 struct stat sb;
94 96
95 i=stat(file,&sb); 97 i = stat(file, &sb);
96 if (i != -1) { 98 if (i != -1) {
97 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { 99 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
98 /* this file is a device. we don't write back to it. 100 /* this file is a device. we don't write back to it.
99 * we "succeed" on the assumption this is some sort 101 * we "succeed" on the assumption this is some sort
100 * of random device. Otherwise attempting to write to 102 * of random device. Otherwise attempting to write to
101 * and chmod the device causes problems. 103 * and chmod the device causes problems.
102 */ 104 */
103 return(1); 105 return (1);
104 } 106 }
105 } 107 }
106 108
107 { 109 {
108 /* chmod(..., 0600) is too late to protect the file, 110 /* chmod(..., 0600) is too late to protect the file,
109 * permissions should be restrictive from the start */ 111 * permissions should be restrictive from the start */
110 int fd = open(file, O_WRONLY|O_CREAT, 0600); 112 int fd = open(file, O_WRONLY|O_CREAT, 0600);
111 if (fd != -1) 113 if (fd != -1)
112 out = fdopen(fd, "wb"); 114 out = fdopen(fd, "wb");
113 } 115 }
114 116
115 if (out == NULL) 117 if (out == NULL)
116 out = fopen(file,"wb"); 118 out = fopen(file, "wb");
117 if (out == NULL) goto err; 119 if (out == NULL)
120 goto err;
118 121
119 chmod(file,0600); 122 chmod(file, 0600);
120 n=RAND_DATA; 123 n = RAND_DATA;
121 for (;;) 124 for (;;) {
122 { 125 i = (n > BUFSIZE) ? BUFSIZE : n;
123 i=(n > BUFSIZE)?BUFSIZE:n; 126 n -= BUFSIZE;
124 n-=BUFSIZE; 127 if (RAND_bytes(buf, i) <= 0)
125 if (RAND_bytes(buf,i) <= 0) 128 rand_err = 1;
126 rand_err=1; 129 i = fwrite(buf, 1, i, out);
127 i=fwrite(buf,1,i,out); 130 if (i <= 0) {
128 if (i <= 0) 131 ret = 0;
129 {
130 ret=0;
131 break; 132 break;
132 } 133 }
133 ret+=i; 134 ret += i;
134 if (n <= 0) break; 135 if (n <= 0)
135 } 136 break;
137 }
136 138
137 fclose(out); 139 fclose(out);
138 OPENSSL_cleanse(buf,BUFSIZE); 140 OPENSSL_cleanse(buf, BUFSIZE);
141
139err: 142err:
140 return (rand_err ? -1 : ret); 143 return (rand_err ? -1 : ret);
141 } 144}
142 145
143const char *RAND_file_name(char *buf, size_t size) 146const char *
147RAND_file_name(char *buf, size_t size)
144{ 148{
145 if (strlcpy(buf,"/dev/urandom",size) >= size) 149 if (strlcpy(buf, "/dev/urandom", size) >= size)
146 return(NULL); 150 return (NULL);
147 return buf; 151 return buf;
148} 152}