Resync with broadcom drivers 5.100.138.20 and utilities.
[tomato.git] / release / src-rt / bcmcrypto / random.c
blobdfa9a4d2957ba5c59292fbac9372ec0b1e60fa07
1 /*
2 * random.c
3 * Copyright (C) 2010, Broadcom Corporation
4 * All Rights Reserved.
5 *
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 $
13 #include <stdio.h>
14 #if defined(__linux__)
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <signal.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <netinet/in.h>
24 #include <net/if.h>
25 #include <fcntl.h>
26 #include <linux/if_packet.h>
27 #elif (defined(__ECOS) || defined(TARGETOS_nucleus))
28 #include <stdlib.h>
29 #elif WIN32
30 #include <stdio.h>
31 #include <windows.h>
32 #include <Wincrypt.h>
33 #endif /* __linux__ */
35 #include <assert.h>
36 #include <typedefs.h>
37 #include <bcmcrypto/bn.h>
39 #if defined(__linux__)
40 void linux_random(uint8 *rand, int len);
41 #elif WIN32
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);
51 #elif WIN32
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
66 #endif
68 void linux_random(uint8 *rand, int len)
70 static int dev_random_fd = -1;
71 int status;
72 int i;
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);
81 if (status == -1) {
82 if (errno == EINTR)
83 continue;
85 assert(status != -1);
88 return;
91 assert(i != RANDOM_READ_TRY_MAX);
93 #elif __ECOS
94 void RAND_ecos_init()
96 BN_register_RAND(generic_random);
98 #elif WIN32
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,
118 UserName,
119 NULL,
120 PROV_RSA_FULL,
123 /* do nothing */
125 else
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,
136 UserName,
137 NULL,
138 PROV_RSA_FULL,
139 CRYPT_NEWKEYSET))
141 printf("Could not create a new key container.\n");
144 else
146 printf("A cryptographic service handle could not be acquired.\n");
150 if (hCryptProv)
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");
160 return;
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))
170 void
171 generic_random(uint8 * random, int len)
173 int tlen = len;
174 while (tlen--) {
175 *random = (uint8)rand();
176 *random++;
178 return;
180 #endif