1 /* $NetBSD: create.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 /* create [-m module] [-s $slot] [-p pin] [-t] [-n count] */
57 #include <isc/commandline.h>
58 #include <isc/print.h>
59 #include <isc/result.h>
60 #include <isc/types.h>
62 #include <pk11/pk11.h>
63 #include <pk11/result.h>
65 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
66 #define getpassphrase(x) getpass(x)
69 #ifndef HAVE_CLOCK_GETTIME
70 #ifndef CLOCK_REALTIME
71 #define CLOCK_REALTIME 0
75 clock_gettime(int32_t id
, struct timespec
*tp
)
80 result
= gettimeofday(&tv
, NULL
);
83 tp
->tv_sec
= tv
.tv_sec
;
84 tp
->tv_nsec
= (long) tv
.tv_usec
* 1000;
92 static CK_BBOOL truevalue
= TRUE
;
93 static CK_BBOOL falsevalue
= FALSE
;
96 main(int argc
, char *argv
[]) {
100 CK_SESSION_HANDLE hSession
= CK_INVALID_HANDLE
;
101 CK_OBJECT_HANDLE
*hKey
;
102 CK_OBJECT_CLASS kClass
= CKO_DATA
;
103 CK_ULONG len
= sizeof(buf
);
104 CK_ATTRIBUTE kTemplate
[] =
106 { CKA_CLASS
, &kClass
, (CK_ULONG
) sizeof(kClass
) },
107 { CKA_TOKEN
, &falsevalue
, (CK_ULONG
) sizeof(falsevalue
) },
108 { CKA_PRIVATE
, &falsevalue
, (CK_ULONG
) sizeof(falsevalue
) },
109 { CKA_LABEL
, (CK_BYTE_PTR
) label
, (CK_ULONG
) sizeof(label
) },
110 { CKA_VALUE
, buf
, (CK_ULONG
) sizeof(buf
) }
113 char *lib_name
= NULL
;
118 unsigned int count
= 1000;
120 struct timespec starttime
;
121 struct timespec endtime
;
123 while ((c
= isc_commandline_parse(argc
, argv
, ":m:s:p:tn:")) != -1) {
126 lib_name
= isc_commandline_argument
;
129 slot
= atoi(isc_commandline_argument
);
135 pin
= isc_commandline_argument
;
138 count
= atoi(isc_commandline_argument
);
142 "Option -%c requires an operand\n",
143 isc_commandline_option
);
148 fprintf(stderr
, "Unrecognised option: -%c\n",
149 isc_commandline_option
);
155 fprintf(stderr
, "Usage:\n");
157 "\tcreate [-m module] [-s slot] [-t] [-n count]\n");
161 pk11_result_register();
163 /* Allocate hanles */
164 hKey
= (CK_SESSION_HANDLE
*)
165 malloc(count
* sizeof(CK_SESSION_HANDLE
));
170 for (i
= 0; i
< count
; i
++)
171 hKey
[i
] = CK_INVALID_HANDLE
;
173 /* Initialize the CRYPTOKI library */
174 if (lib_name
!= NULL
)
175 pk11_set_lib_name(lib_name
);
178 pin
= getpassphrase("Enter Pin: ");
180 result
= pk11_get_session(&pctx
, OP_ANY
, ISC_TRUE
, ISC_TRUE
,
181 ISC_TRUE
, (const char *) pin
, slot
);
182 if ((result
!= ISC_R_SUCCESS
) &&
183 (result
!= PK11_R_NORANDOMSERVICE
) &&
184 (result
!= PK11_R_NODIGESTSERVICE
) &&
185 (result
!= PK11_R_NOAESSERVICE
)) {
186 fprintf(stderr
, "Error initializing PKCS#11: %s\n",
187 isc_result_totext(result
));
192 memset(pin
, 0, strlen((char *)pin
));
194 hSession
= pctx
.session
;
196 /* Randomize the buffer */
197 rv
= pkcs_C_GenerateRandom(hSession
, buf
, len
);
199 fprintf(stderr
, "C_GenerateRandom: Error = 0x%.8lX\n", rv
);
204 kTemplate
[1].pValue
= &truevalue
;
206 if (clock_gettime(CLOCK_REALTIME
, &starttime
) < 0) {
207 perror("clock_gettime(start)");
211 for (i
= 0; i
< count
; i
++) {
212 (void) snprintf(label
, sizeof(label
), "obj%u", i
);
213 kTemplate
[3].ulValueLen
= strlen(label
);
214 rv
= pkcs_C_CreateObject(hSession
, kTemplate
, 5, &hKey
[i
]);
217 "C_CreateObject[%u]: Error = 0x%.8lX\n",
226 if (clock_gettime(CLOCK_REALTIME
, &endtime
) < 0) {
227 perror("clock_gettime(end)");
231 endtime
.tv_sec
-= starttime
.tv_sec
;
232 endtime
.tv_nsec
-= starttime
.tv_nsec
;
233 while (endtime
.tv_nsec
< 0) {
235 endtime
.tv_nsec
+= 1000000000;
237 printf("%u created objects in %ld.%09lds\n", i
,
238 endtime
.tv_sec
, endtime
.tv_nsec
);
240 printf("%g created objects/s\n",
241 1024 * i
/ ((double) endtime
.tv_sec
+
242 (double) endtime
.tv_nsec
/ 1000000000.));
245 for (i
= 0; i
< count
; i
++) {
246 /* Destroy objects */
247 if (hKey
[i
] == CK_INVALID_HANDLE
)
249 rv
= pkcs_C_DestroyObject(hSession
, hKey
[i
]);
250 if ((rv
!= CKR_OK
) && !errflg
) {
252 "C_DestroyObject[%u]: Error = 0x%.8lX\n",
259 pk11_return_session(&pctx
);
260 (void) pk11_finalize();