MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
random_seed.c
Go to the documentation of this file.
1 /*
2  * random_seed.c
3  *
4  * Copyright (c) 2013 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
7  * This library is free software; you can redistribute it and/or modify
8  * it under the terms of the MIT license. See COPYING for details.
9  *
10  */
11 
12 #include <stdio.h>
13 #include "random_seed.h"
14 #include "config.h"
15 
16 #define DEBUG_SEED(s)
17 
18 
19 #if defined ENABLE_RDRAND
20 
21 /* cpuid */
22 
23 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
24 #define HAS_X86_CPUID 1
25 
26 static void do_cpuid(int regs[], int h)
27 {
28  __asm__ __volatile__(
29 #if defined __x86_64__
30  "pushq %%rbx;\n"
31 #else
32  "pushl %%ebx;\n"
33 #endif
34  "cpuid;\n"
35 #if defined __x86_64__
36  "popq %%rbx;\n"
37 #else
38  "popl %%ebx;\n"
39 #endif
40  : "=a"(regs[0]), [ebx] "=r"(regs[1]), "=c"(regs[2]), "=d"(regs[3])
41  : "a"(h));
42 }
43 
44 #elif defined _MSC_VER
45 
46 #define HAS_X86_CPUID 1
47 #define do_cpuid __cpuid
48 
49 #endif
50 
51 /* has_rdrand */
52 
53 #if HAS_X86_CPUID
54 
55 static int has_rdrand()
56 {
57  // CPUID.01H:ECX.RDRAND[bit 30] == 1
58  int regs[4];
59  do_cpuid(regs, 1);
60  return (regs[2] & (1 << 30)) != 0;
61 }
62 
63 #endif
64 
65 /* get_rdrand_seed - GCC x86 and X64 */
66 
67 #if defined __GNUC__ && (defined __i386__ || defined __x86_64__)
68 
69 #define HAVE_RDRAND 1
70 
71 static int get_rdrand_seed()
72 {
73  DEBUG_SEED("get_rdrand_seed");
74  int _eax;
75  // rdrand eax
76  __asm__ __volatile__("1: .byte 0x0F\n"
77  " .byte 0xC7\n"
78  " .byte 0xF0\n"
79  " jnc 1b;\n"
80  : "=a" (_eax));
81  return _eax;
82 }
83 
84 #endif
85 
86 #if defined _MSC_VER
87 
88 #if _MSC_VER >= 1700
89 #define HAVE_RDRAND 1
90 
91 /* get_rdrand_seed - Visual Studio 2012 and above */
92 
93 static int get_rdrand_seed()
94 {
95  DEBUG_SEED("get_rdrand_seed");
96  int r;
97  while (_rdrand32_step(&r) == 0);
98  return r;
99 }
100 
101 #elif defined _M_IX86
102 #define HAVE_RDRAND 1
103 
104 /* get_rdrand_seed - Visual Studio 2010 and below - x86 only */
105 
106 static int get_rdrand_seed()
107 {
108  DEBUG_SEED("get_rdrand_seed");
109  int _eax;
110 retry:
111  // rdrand eax
112  __asm _emit 0x0F __asm _emit 0xC7 __asm _emit 0xF0
113  __asm jnc retry
114  __asm mov _eax, eax
115  return _eax;
116 }
117 
118 #endif
119 #endif
120 
121 #endif /* defined ENABLE_RDRAND */
122 
123 
124 /* has_dev_urandom */
125 
126 #if defined (__APPLE__) || defined(__unix__) || defined(__linux__)
127 
128 #include <string.h>
129 #include <fcntl.h>
130 #include <unistd.h>
131 #include <errno.h>
132 #include <stdlib.h>
133 #include <sys/stat.h>
134 
135 #define HAVE_DEV_RANDOM 1
136 
137 static const char *dev_random_file = "/dev/urandom";
138 
139 static int has_dev_urandom()
140 {
141  struct stat buf;
142  if (stat(dev_random_file, &buf)) {
143  return 0;
144  }
145  return ((buf.st_mode & S_IFCHR) != 0);
146 }
147 
148 
149 /* get_dev_random_seed */
150 
151 static int get_dev_random_seed()
152 {
153  DEBUG_SEED("get_dev_random_seed");
154 
155  int fd = open(dev_random_file, O_RDONLY);
156  if (fd < 0) {
157  fprintf(stderr, "error opening %s: %s", dev_random_file, strerror(errno));
158  exit(1);
159  }
160 
161  int r;
162  ssize_t nread = read(fd, &r, sizeof(r));
163  if (nread != sizeof(r)) {
164  fprintf(stderr, "error read %s: %s", dev_random_file, strerror(errno));
165  exit(1);
166  }
167  else if (nread != sizeof(r)) {
168  fprintf(stderr, "error short read %s", dev_random_file);
169  exit(1);
170  }
171  close(fd);
172  return r;
173 }
174 
175 #endif
176 
177 
178 /* get_cryptgenrandom_seed */
179 
180 #ifdef WIN32
181 
182 #define HAVE_CRYPTGENRANDOM 1
183 
184 #include <windows.h>
185 #pragma comment(lib, "advapi32.lib")
186 
187 static int get_cryptgenrandom_seed()
188 {
189  DEBUG_SEED("get_cryptgenrandom_seed");
190 
191  HCRYPTPROV hProvider = 0;
192  int r;
193 
194  if (!CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
195  fprintf(stderr, "error CryptAcquireContextW");
196  exit(1);
197  }
198 
199  if (!CryptGenRandom(hProvider, sizeof(r), (BYTE*)&r)) {
200  fprintf(stderr, "error CryptGenRandom");
201  exit(1);
202  }
203 
204  CryptReleaseContext(hProvider, 0);
205 
206  return r;
207 }
208 
209 #endif
210 
211 
212 /* get_time_seed */
213 
214 #include <time.h>
215 
216 static int get_time_seed()
217 {
218  DEBUG_SEED("get_time_seed");
219 
220  return (int)time(NULL) * 433494437;
221 }
222 
223 
224 /* json_c_get_random_seed */
225 
227 {
228 #if HAVE_RDRAND
229  if (has_rdrand()) return get_rdrand_seed();
230 #endif
231 #if HAVE_DEV_RANDOM
232  if (has_dev_urandom()) return get_dev_random_seed();
233 #endif
234 #if HAVE_CRYPTGENRANDOM
235  return get_cryptgenrandom_seed();
236 #endif
237  return get_time_seed();
238 }
static int get_time_seed()
Definition: random_seed.c:216
int json_c_get_random_seed()
Definition: random_seed.c:226
#define DEBUG_SEED(s)
Definition: random_seed.c:16