Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / tests / pkcs11 / benchmarks / find.c
blobfcb414e403d8d8fd2db8d3c3242f491d54d3c5c7
1 /* $NetBSD: find.c,v 1.1.1.4 2014/12/10 03:34:28 christos Exp $ */
3 /*
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
24 * are met:
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.
43 /* find [-m module] [-s $slot] [-p pin] [-n count] */
45 /*! \file */
47 #include <config.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
55 #include <isc/commandline.h>
56 #include <isc/result.h>
57 #include <isc/types.h>
59 #include <pk11/pk11.h>
60 #include <pk11/result.h>
62 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
63 #define getpassphrase(x) getpass(x)
64 #endif
66 #ifndef HAVE_CLOCK_GETTIME
67 #ifndef CLOCK_REALTIME
68 #define CLOCK_REALTIME 0
69 #endif
71 int
72 clock_gettime(int32_t id, struct timespec *tp)
74 struct timeval tv;
75 int result;
77 result = gettimeofday(&tv, NULL);
78 if (result)
79 return (result);
80 tp->tv_sec = tv.tv_sec;
81 tp->tv_nsec = (long) tv.tv_usec * 1000;
82 return (result);
84 #endif
86 CK_BYTE label[] = "foo??bar!!";
88 int
89 main(int argc, char *argv[]) {
90 isc_result_t result;
91 CK_RV rv;
92 CK_SLOT_ID slot = 0;
93 CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
94 CK_ATTRIBUTE sTemplate[] =
96 { CKA_LABEL, label, (CK_ULONG) sizeof(label) },
98 CK_OBJECT_HANDLE sKey = CK_INVALID_HANDLE;
99 CK_ULONG found = 0;
100 pk11_context_t pctx;
101 pk11_optype_t op_type = OP_RSA;
102 char *lib_name = NULL;
103 char *pin = NULL;
104 int error = 0;
105 int c, errflg = 0;
106 unsigned int count = 1000;
107 unsigned int i;
108 struct timespec starttime;
109 struct timespec endtime;
111 while ((c = isc_commandline_parse(argc, argv, ":m:s:p:n:")) != -1) {
112 switch (c) {
113 case 'm':
114 lib_name = isc_commandline_argument;
115 break;
116 case 's':
117 slot = atoi(isc_commandline_argument);
118 op_type = OP_ANY;
119 break;
120 case 'p':
121 pin = isc_commandline_argument;
122 break;
123 case 'n':
124 count = atoi(isc_commandline_argument);
125 break;
126 case ':':
127 fprintf(stderr,
128 "Option -%c requires an operand\n",
129 isc_commandline_option);
130 errflg++;
131 break;
132 case '?':
133 default:
134 fprintf(stderr, "Unrecognised option: -%c\n",
135 isc_commandline_option);
136 errflg++;
140 if (errflg) {
141 fprintf(stderr, "Usage:\n");
142 fprintf(stderr,
143 "\tfind [-m module] [-s slot] [-p pin] [-n count]\n");
144 exit(1);
147 pk11_result_register();
149 /* Initialize the CRYPTOKI library */
150 if (lib_name != NULL)
151 pk11_set_lib_name(lib_name);
153 if (pin == NULL)
154 pin = getpassphrase("Enter Pin: ");
156 result = pk11_get_session(&pctx, op_type, ISC_FALSE, ISC_FALSE,
157 ISC_TRUE, (const char *) pin, slot);
158 if ((result != ISC_R_SUCCESS) &&
159 (result != PK11_R_NORANDOMSERVICE) &&
160 (result != PK11_R_NODIGESTSERVICE) &&
161 (result != PK11_R_NOAESSERVICE)) {
162 fprintf(stderr, "Error initializing PKCS#11: %s\n",
163 isc_result_totext(result));
164 exit(1);
167 if (pin != NULL)
168 memset(pin, 0, strlen((char *)pin));
170 hSession = pctx.session;
172 if (clock_gettime(CLOCK_REALTIME, &starttime) < 0) {
173 perror("clock_gettime(start)");
174 goto exit_objects;
177 for (i = 0; !error && (i < count); i++) {
178 rv = pkcs_C_FindObjectsInit(hSession, sTemplate, 1);
179 if (rv != CKR_OK) {
180 fprintf(stderr,
181 "C_FindObjectsInit[%u]: Error = 0x%.8lX\n",
182 i, rv);
183 error = 1;
184 break;
187 rv = pkcs_C_FindObjects(hSession, &sKey, 1, &found);
188 if (rv != CKR_OK) {
189 fprintf(stderr,
190 "C_FindObjects[%u]: Error = 0x%.8lX\n",
191 i, rv);
192 error = 1;
193 /* no break here! */
196 rv = pkcs_C_FindObjectsFinal(hSession);
197 if (rv != CKR_OK) {
198 fprintf(stderr,
199 "C_FindObjectsFinal[%u]: Error = 0x%.8lX\n",
200 i, rv);
201 error = 1;
202 break;
206 if (clock_gettime(CLOCK_REALTIME, &endtime) < 0) {
207 perror("clock_gettime(end)");
208 goto exit_objects;
211 endtime.tv_sec -= starttime.tv_sec;
212 endtime.tv_nsec -= starttime.tv_nsec;
213 while (endtime.tv_nsec < 0) {
214 endtime.tv_sec -= 1;
215 endtime.tv_nsec += 1000000000;
217 printf("%u object searches in %ld.%09lds\n", i,
218 endtime.tv_sec, endtime.tv_nsec);
219 if (i > 0)
220 printf("%g object searches/s\n",
221 1024 * i / ((double) endtime.tv_sec +
222 (double) endtime.tv_nsec / 1000000000.));
224 exit_objects:
225 pk11_return_session(&pctx);
226 (void) pk11_finalize();
228 exit(error);