3 * Copyright (C) 2010, Broadcom Corporation
6 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation;
7 * the contents of this file may not be disclosed to third parties, copied
8 * or duplicated in any form, in whole or in part, without the prior
9 * written permission of Broadcom Corporation.
11 * $Id: random.c,v 1.13 2009-06-11 02:38:23 Exp $
14 #if defined(__linux__)
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <netinet/in.h>
26 #include <linux/if_packet.h>
27 #elif (defined(__ECOS) || defined(TARGETOS_nucleus))
33 #endif /* __linux__ */
37 #include <bcmcrypto/bn.h>
39 #if defined(__linux__)
40 void linux_random(uint8
*rand
, int len
);
42 void windows_random(uint8
*rand
, int len
);
43 #elif (defined(__ECOS) || defined(TARGETOS_nucleus))
44 void generic_random(uint8
* rand
, int len
);
45 #endif /* __linux__ */
47 void RAND_bytes(unsigned char *buf
, int num
)
49 #if defined(__linux__)
50 linux_random(buf
, num
);
52 windows_random(buf
, num
);
53 #elif (defined(__ECOS) || defined(TARGETOS_nucleus))
54 generic_random(buf
, num
);
55 #endif /* __linux__ */
58 #if defined(__linux__)
59 void RAND_linux_init()
61 BN_register_RAND(linux_random
);
64 #ifndef RANDOM_READ_TRY_MAX
65 #define RANDOM_READ_TRY_MAX 10
68 void linux_random(uint8
*rand
, int len
)
70 static int dev_random_fd
= -1;
74 if (dev_random_fd
== -1)
75 dev_random_fd
= open("/dev/urandom", O_RDONLY
|O_NONBLOCK
);
77 assert(dev_random_fd
!= -1);
79 for (i
= 0; i
< RANDOM_READ_TRY_MAX
; i
++) {
80 status
= read(dev_random_fd
, rand
, len
);
91 assert(i
!= RANDOM_READ_TRY_MAX
);
96 BN_register_RAND(generic_random
);
99 void RAND_windows_init()
101 BN_register_RAND(windows_random
);
104 void windows_random(uint8
*rand
, int len
)
106 /* Declare and initialize variables */
108 HCRYPTPROV hCryptProv
= NULL
;
109 LPCSTR UserName
= "{56E9D11F-76B8-42fa-8645-76980E4E8648}";
112 Attempt to acquire a context and a key
113 container. The context will use the default CSP
114 for the RSA_FULL provider type. DwFlags is set to 0
115 to attempt to open an existing key container.
117 if (CryptAcquireContext(&hCryptProv
,
128 An error occurred in acquiring the context. This could mean
129 that the key container requested does not exist. In this case,
130 the function can be called again to attempt to create a new key
131 container. Error codes are defined in winerror.h.
133 if (GetLastError() == NTE_BAD_KEYSET
)
135 if (!CryptAcquireContext(&hCryptProv
,
141 printf("Could not create a new key container.\n");
146 printf("A cryptographic service handle could not be acquired.\n");
152 /* Generate a random initialization vector. */
153 if (!CryptGenRandom(hCryptProv
, len
, rand
))
155 printf("Error during CryptGenRandom.\n");
157 if (!CryptReleaseContext(hCryptProv
, 0))
158 printf("Failed CryptReleaseContext\n");
162 #elif TARGETOS_nucleus
163 void RAND_generic_init()
165 BN_register_RAND(generic_random
);
167 #endif /* __linux__ */
169 #if (defined(__ECOS) || defined(TARGETOS_nucleus))
171 generic_random(uint8
* random
, int len
)
175 *random
= (uint8
)rand();