1 /* $NetBSD: random.c,v 1.1.1.4 2014/12/10 03:34:28 christos Exp $ */
4 * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
20 * Portions copyright (c) 2008 Nominet UK. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 /* random [-m module] [-s $slot] [-n count] */
57 #include <isc/commandline.h>
58 #include <isc/result.h>
59 #include <isc/types.h>
61 #include <pk11/pk11.h>
62 #include <pk11/result.h>
64 #ifndef HAVE_CLOCK_GETTIME
65 #ifndef CLOCK_REALTIME
66 #define CLOCK_REALTIME 0
70 clock_gettime(int32_t id
, struct timespec
*tp
)
75 result
= gettimeofday(&tv
, NULL
);
78 tp
->tv_sec
= tv
.tv_sec
;
79 tp
->tv_nsec
= (long) tv
.tv_usec
* 1000;
87 main(int argc
, char *argv
[]) {
91 CK_SESSION_HANDLE hSession
= CK_INVALID_HANDLE
;
92 CK_ULONG len
= sizeof(buf
);
94 pk11_optype_t op_type
= OP_RAND
;
95 char *lib_name
= NULL
;
98 unsigned int count
= 1000;
100 struct timespec starttime
;
101 struct timespec endtime
;
103 while ((c
= isc_commandline_parse(argc
, argv
, ":m:s:n:")) != -1) {
106 lib_name
= isc_commandline_argument
;
109 slot
= atoi(isc_commandline_argument
);
113 count
= atoi(isc_commandline_argument
);
117 "Option -%c requires an operand\n",
118 isc_commandline_option
);
123 fprintf(stderr
, "Unrecognised option: -%c\n",
124 isc_commandline_option
);
130 fprintf(stderr
, "Usage:\n");
132 "\trandom [-m module] [-s slot] [-n count]\n");
136 pk11_result_register();
138 /* Initialize the CRYPTOKI library */
139 if (lib_name
!= NULL
)
140 pk11_set_lib_name(lib_name
);
142 result
= pk11_get_session(&pctx
, op_type
, ISC_FALSE
, ISC_FALSE
,
143 ISC_FALSE
, NULL
, slot
);
144 if ((result
!= ISC_R_SUCCESS
) &&
145 (result
!= PK11_R_NODIGESTSERVICE
) &&
146 (result
!= PK11_R_NOAESSERVICE
)) {
147 fprintf(stderr
, "Error initializing PKCS#11: %s\n",
148 isc_result_totext(result
));
152 hSession
= pctx
.session
;
154 if (clock_gettime(CLOCK_REALTIME
, &starttime
) < 0) {
155 perror("clock_gettime(start)");
159 for (i
= 0; i
< count
; i
++) {
160 /* Get random bytes */
161 rv
= pkcs_C_GenerateRandom(hSession
, buf
, len
);
164 "C_GenerateRandom[%u]: Error = 0x%.8lX\n",
171 if (clock_gettime(CLOCK_REALTIME
, &endtime
) < 0) {
172 perror("clock_gettime(end)");
176 endtime
.tv_sec
-= starttime
.tv_sec
;
177 endtime
.tv_nsec
-= starttime
.tv_nsec
;
178 while (endtime
.tv_nsec
< 0) {
180 endtime
.tv_nsec
+= 1000000000;
182 printf("%uK random bytes in %ld.%09lds\n", i
,
183 endtime
.tv_sec
, endtime
.tv_nsec
);
185 printf("%g random bytes/s\n",
186 1024 * i
/ ((double) endtime
.tv_sec
+
187 (double) endtime
.tv_nsec
/ 1000000000.));
190 pk11_return_session(&pctx
);
191 (void) pk11_finalize();