diff options
Diffstat (limited to 'bsd-misc.c')
-rw-r--r-- | bsd-misc.c | 114 |
1 files changed, 11 insertions, 103 deletions
diff --git a/bsd-misc.c b/bsd-misc.c index e6b529e1e..67614eb76 100644 --- a/bsd-misc.c +++ b/bsd-misc.c | |||
@@ -27,107 +27,9 @@ | |||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ | 28 | */ |
29 | 29 | ||
30 | #include "config.h" | 30 | #include "includes.h" |
31 | |||
32 | #include <stdio.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <string.h> | ||
35 | #include <errno.h> | ||
36 | #include <unistd.h> | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | #include <fcntl.h> | ||
41 | #ifdef HAVE_STDDEF_H | ||
42 | #include <stddef.h> | ||
43 | #endif | ||
44 | |||
45 | #include "xmalloc.h" | 31 | #include "xmalloc.h" |
46 | #include "ssh.h" | 32 | #include "ssh.h" |
47 | #include "bsd-misc.h" | ||
48 | #include "entropy.h" | ||
49 | |||
50 | #include <openssl/rand.h> | ||
51 | |||
52 | #ifndef HAVE_ARC4RANDOM | ||
53 | |||
54 | typedef struct | ||
55 | { | ||
56 | unsigned int s[256]; | ||
57 | int i; | ||
58 | int j; | ||
59 | } rc4_t; | ||
60 | |||
61 | void rc4_key(rc4_t *r, unsigned char *key, int len); | ||
62 | void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len); | ||
63 | |||
64 | static rc4_t *rc4 = NULL; | ||
65 | |||
66 | void rc4_key(rc4_t *r, unsigned char *key, int len) | ||
67 | { | ||
68 | int t; | ||
69 | |||
70 | for(r->i = 0; r->i < 256; r->i++) | ||
71 | r->s[r->i] = r->i; | ||
72 | |||
73 | r->j = 0; | ||
74 | for(r->i = 0; r->i < 256; r->i++) | ||
75 | { | ||
76 | r->j = (r->j + r->s[r->i] + key[r->i % len]) % 256; | ||
77 | t = r->s[r->i]; | ||
78 | r->s[r->i] = r->s[r->j]; | ||
79 | r->s[r->j] = t; | ||
80 | } | ||
81 | r->i = r->j = 0; | ||
82 | } | ||
83 | |||
84 | void rc4_getbytes(rc4_t *r, unsigned char *buffer, int len) | ||
85 | { | ||
86 | int t; | ||
87 | int c; | ||
88 | |||
89 | c = 0; | ||
90 | while(c < len) | ||
91 | { | ||
92 | r->i = (r->i + 1) % 256; | ||
93 | r->j = (r->j + r->s[r->i]) % 256; | ||
94 | t = r->s[r->i]; | ||
95 | r->s[r->i] = r->s[r->j]; | ||
96 | r->s[r->j] = t; | ||
97 | |||
98 | t = (r->s[r->i] + r->s[r->j]) % 256; | ||
99 | |||
100 | buffer[c] = r->s[t]; | ||
101 | c++; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | unsigned int arc4random(void) | ||
106 | { | ||
107 | unsigned int r; | ||
108 | |||
109 | if (rc4 == NULL) | ||
110 | arc4random_stir(); | ||
111 | |||
112 | rc4_getbytes(rc4, (unsigned char *)&r, sizeof(r)); | ||
113 | |||
114 | return(r); | ||
115 | } | ||
116 | |||
117 | void arc4random_stir(void) | ||
118 | { | ||
119 | unsigned char rand_buf[32]; | ||
120 | |||
121 | if (rc4 == NULL) | ||
122 | rc4 = xmalloc(sizeof(*rc4)); | ||
123 | |||
124 | seed_rng(); | ||
125 | RAND_bytes(rand_buf, sizeof(rand_buf)); | ||
126 | |||
127 | rc4_key(rc4, rand_buf, sizeof(rand_buf)); | ||
128 | memset(rand_buf, 0, sizeof(rand_buf)); | ||
129 | } | ||
130 | #endif /* !HAVE_ARC4RANDOM */ | ||
131 | 33 | ||
132 | #ifndef HAVE_SETPROCTITLE | 34 | #ifndef HAVE_SETPROCTITLE |
133 | void setproctitle(const char *fmt, ...) | 35 | void setproctitle(const char *fmt, ...) |
@@ -158,9 +60,15 @@ int seteuid(uid_t euid) | |||
158 | } | 60 | } |
159 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ | 61 | #endif /* !defined(HAVE_SETEUID) && defined(HAVE_SETREUID) */ |
160 | 62 | ||
161 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) | 63 | #if !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) && defined(HAVE_SYS_NERR) |
162 | const char *strerror(void) | 64 | const char *strerror(int e) |
163 | { | 65 | { |
164 | return(sys_errlist[errno]); | 66 | extern int sys_nerr; |
67 | extern char *sys_errlist[]; | ||
68 | |||
69 | if ((e >= 0) || (e < sys_nerr)) | ||
70 | return("unlisted error"); | ||
71 | else | ||
72 | return(sys_errlist[e]); | ||
165 | } | 73 | } |
166 | #endif /* !defined(HAVE_STRERROR) && defined(HAVE_SYS_ERRLIST) */ | 74 | #endif |