2 Copyright (c) 2011, Network RADIUS SARL
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Copyright (c) 2006 Kungliga Tekniska HAÎåÎÝgskolan
29 * (Royal Institute of Technology, Stockholm, Sweden).
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the Institute nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * \brief Functions which should be customized for your local system.
72 volatile static HCRYPTPROV nr_cryptprovider = 0;
75 nr_CryptProvider(void)
78 HCRYPTPROV cryptprovider = 0;
80 if (nr_cryptprovider != 0)
81 return nr_cryptprovider;
83 rv = CryptAcquireContext(&cryptprovider, NULL,
84 MS_ENHANCED_PROV, PROV_RSA_FULL,
87 if (GetLastError() == NTE_BAD_KEYSET) {
89 rv = CryptAcquireContext(&cryptprovider, NULL,
90 MS_ENHANCED_PROV, PROV_RSA_FULL,
95 InterlockedCompareExchangePointer((PVOID *) &nr_cryptprovider,
96 (PVOID) cryptprovider, 0) != 0) {
98 CryptReleaseContext(cryptprovider, 0);
99 cryptprovider = nr_cryptprovider;
102 return cryptprovider;
105 ssize_t nr_rand_bytes(uint8_t *data, size_t data_len)
107 if (CryptGenRandom(nr_CryptProvider(), data_len, data))
112 ssize_t nr_rand_bytes(uint8_t *data, size_t data_len)
117 fd = open("/dev/urandom", O_RDONLY);
119 nr_strerror_printf("Error opening randomness: %s",
125 return read(fd, data, data_len);
129 uint32_t nr_rand(void)
133 nr_rand_bytes((void *)&lvalue, sizeof(lvalue));
139 #define USEC (1000000)
142 void nr_timeval_add(struct timeval *t, unsigned int seconds, unsigned int usec)
144 t->tv_sec += seconds;
145 t->tv_sec += usec / USEC;
146 t->tv_usec += usec % USEC;
147 if (t->tv_usec > USEC) {
153 int nr_timeval_cmp(const struct timeval *a, const struct timeval *b)
155 if (a->tv_sec > b->tv_sec) return +1;
156 if (a->tv_sec < b->tv_sec) return -1;
158 if (a->tv_usec > b->tv_usec) return +1;
159 if (a->tv_usec < b->tv_usec) return -1;