-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
249 lines (189 loc) · 6.94 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* Copyright © 2015, Intel Corporation. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
/*! \file main.c
* \brief Test Program to demonstrate DRNG Lib.
*
* This is the main file of the test project.
* It demonstrates the use of different APIs for RDSEED and RDRAND.
*/
#include <stdio.h>
#include <string.h>
#include "drng.h"
#define BUFFSIZE 1275
/*! \def RDSEED_CUTOFF
* Minimum number of RDSEED results required
*/
#define RDSEED_CUTOFF 10
/*! \def RDRAND_CUTOFF
* Minimum number of RDRAND results required
*/
#define RDRAND_CUTOFF 10
/*! \def MAX_RETRY_LIMIT
* Maximum number of rdseed retries when nesting multiple calls to rdseed in a wrapper function.
*/
#define MAX_RETRY_LIMIT 75
int main()
{
int i, j;
int r;
uint16_t u16;
uint32_t u32;
uint32_t array32[10];
unsigned char buffer[BUFFSIZE];
#ifdef _NOT_WIN32
uint64_t u64;
uint64_t array64[10];
#endif
/* -------------------- Sample code for RDRAND Starts here --------------- */
printf("*************** RDRAND TEST STARTS HERE **************\n");
if (RdRand_isSupported())
{
r = rdrand_16(&u16, 10);
if (r != DRNG_SUCCESS) printf("rdrand instruction failed with code %d\n", r);
printf("RDRAND uint16: %u\n", u16);
r = rdrand_32(&u32, 10);
if (r != DRNG_SUCCESS) printf("rdrand instruction failed with code %d\n", r);
printf("RDRAND uint32: %u\n", u32);
/* rdrand_64 is not defined for Windows 32bit compiler */
#ifdef _NOT_WIN32
r = rdrand_64(&u64, 10);
if (r != DRNG_SUCCESS) printf("rdrand instruction failed with code %d\n", r);
printf("RDRAND uint64: %llu\n", u64);
#endif
r = rdrand_get_n_32(RDRAND_CUTOFF, array32);
if (r == DRNG_SUCCESS) {
printf("\nRDRAND 10 uint32's:\n");
for (i = 0; i < RDRAND_CUTOFF; ++i) {
printf("%u\n", array32[i]);
}
}
else printf("rdrand instruction failed with code %d\n", r);
/* rdrand_64 is not defined for Windows 32bit compiler */
#ifdef _NOT_WIN32
r = rdrand_get_n_64(RDRAND_CUTOFF, array64);
if (r == DRNG_SUCCESS) {
printf("\nRDRAND 10 uint64's:\n");
for (i = 0; i < RDRAND_CUTOFF; ++i) {
printf("%llu\n", array64[i]);
}
}
else printf("rdrand instruction failed with code %d\n", r);
#endif
memset(buffer, 0, BUFFSIZE);
r = rdrand_get_bytes(BUFFSIZE, buffer);
if (r == DRNG_SUCCESS) {
printf("\nRDRAND Buffer of %d bytes:\n", BUFFSIZE);
j = 0;
for (i = 0; i < BUFFSIZE; ++i)
{
printf("%02x ", buffer[i]);
++j;
if (j == 16) {
j = 0;
printf("\n");
}
else if (j == 8) printf(" ");
}
printf("\n");
}
else printf("rdrand instruction failed with code %d\n", r);
}
else printf("\nRdRand instruction is not supported by your system\n");
printf("\n***************** RDRAND TEST ENDS HERE ***************\n\n");
/* --------------------- Sample code for RDRAND Ends here ------------------ */
/* --------------------- Sample code for RDSEED Start here ----------------- */
printf("\n*************** RDSEED TEST STARTS HERE **************\n");
if (RdSeed_isSupported())
{
r = rdseed_16(&u16, MAX_RETRY_LIMIT);
if (r != DRNG_SUCCESS) printf("rdseed instruction failed with code %d\n", r);
printf("RDSEED uint16: %u\n", u16);
r = rdseed_32(&u32, MAX_RETRY_LIMIT);
if (r != DRNG_SUCCESS) printf("rdseed instruction failed with code %d\n", r);
printf("RDSEED uint32: %u\n", u32);
/* rdseed_64 is not defined for Windows 32bit compiler */
#ifdef _NOT_WIN32
r = rdseed_64(&u64, MAX_RETRY_LIMIT);
if (r != DRNG_SUCCESS) printf("rdseed instruction failed with code %d\n", r);
printf("RDSEED uint64: %llu\n", u64);
#endif
r = 0;
r = rdseed_get_n_32(RDSEED_CUTOFF, array32, r, MAX_RETRY_LIMIT);
if (r == DRNG_UNSUPPORTED)
printf("RDSEED is not supported by system\n");
while (r < RDSEED_CUTOFF)
{
printf("\nRSEED numbers %d uint32's and continuing...\n", r);
r = rdseed_get_n_32(RDSEED_CUTOFF, array32, r, MAX_RETRY_LIMIT);
}
printf("\nRDSEED numbers %d uint32's:\n", r);
for (i = 0; i < RDSEED_CUTOFF; ++i) {
printf("%u\n", array32[i]);
}
/* rdseed_64 is not defined for Windows 32bit compiler */
#ifdef _NOT_WIN32
r = 0;
r = rdseed_get_n_64(RDSEED_CUTOFF, array64, r, MAX_RETRY_LIMIT);
if (r == DRNG_UNSUPPORTED)
printf("RDSEED is not supported by system\n");
while (r < RDSEED_CUTOFF)
{
printf("\nRDSEED numbers %d uint64's and continuing...\n", r);
r = rdseed_get_n_64(RDSEED_CUTOFF, array64, r, MAX_RETRY_LIMIT);
}
printf("\nRDSEED numbers %d uint64's:\n", r);
for (i = 0; i < RDSEED_CUTOFF; ++i) {
printf("%llu\n", array64[i]);
}
#endif
memset(buffer, 0, BUFFSIZE);
r = 0;
r = rdseed_get_bytes(BUFFSIZE, buffer, r, MAX_RETRY_LIMIT);
if (r == DRNG_UNSUPPORTED)
printf("RDSEED is not supported by system\n");
else
{
while (r < BUFFSIZE)
{
printf("\nRDSEED generated %d bytes and continuing.... \n", r);
r = rdseed_get_bytes(BUFFSIZE, buffer, r, MAX_RETRY_LIMIT);
}
printf("\nTotal generated RDSEED Buffer of %d bytes:\n", r);
j = 0;
for (i = 0; i < BUFFSIZE; ++i)
{
printf("%02x ", buffer[i]);
++j;
if (j == 16) {
j = 0;
printf("\n");
}
else if (j == 8) printf(" ");
}
printf("\n");
}
}
else printf("\nRdSeed instruction is not supported by your system\n");
printf("\n*************** RDSEED TEST ENDS HERE **************\n\n");
/* -------------------- Sample code for RDSEED Ends here --------------- */
return 0;
}