1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 * |
|
21 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
|
22 */ |
|
23 |
|
24 #include <sys/types.h> |
|
25 #include <sys/stat.h> |
|
26 #include <string.h> |
|
27 #include <fcntl.h> |
|
28 #include <pthread.h> |
|
29 #include <errno.h> |
|
30 #include <cryptoutil.h> |
|
31 #include <sys/crypto/ioctl.h> |
|
32 #include <sys/crypto/api.h> |
|
33 #include <openssl/bio.h> |
|
34 #include <openssl/aes.h> |
|
35 #include <openssl/engine.h> |
|
36 #include <security/cryptoki.h> |
|
37 |
|
38 #define DEVCRYPTO_LIB_NAME "devcrypto engine" |
|
39 #include "e_devcrypto_err.c" |
|
40 |
|
41 /* DEVCRYPTO CONTEXT */ |
|
42 typedef struct devcrypto_ctx { |
|
43 uint_t session_id; |
|
44 } devcrypto_ctx_t; |
|
45 |
|
46 /* Index for the supported ciphers */ |
|
47 typedef enum { |
|
48 DEV_DES_CBC, |
|
49 DEV_DES3_CBC, |
|
50 DEV_DES_ECB, |
|
51 DEV_DES3_ECB, |
|
52 DEV_RC4, |
|
53 DEV_AES_128_CBC, |
|
54 DEV_AES_192_CBC, |
|
55 DEV_AES_256_CBC, |
|
56 DEV_AES_128_ECB, |
|
57 DEV_AES_192_ECB, |
|
58 DEV_AES_256_ECB, |
|
59 DEV_BLOWFISH_CBC, |
|
60 DEV_AES_128_CTR, |
|
61 DEV_AES_192_CTR, |
|
62 DEV_AES_256_CTR, |
|
63 DEV_CIPHER_MAX |
|
64 } DEV_CIPHER_ID; |
|
65 |
|
66 typedef struct devcrypto_cipher { |
|
67 DEV_CIPHER_ID id; |
|
68 int nid; |
|
69 int iv_len; |
|
70 int min_key_len; |
|
71 int max_key_len; |
|
72 CK_KEY_TYPE key_type; |
|
73 CK_MECHANISM_TYPE mech_type; |
|
74 unsigned long flags; |
|
75 crypto_mech_type_t pn_internal_number; |
|
76 } devcrypto_cipher_t; |
|
77 |
|
78 |
|
79 /* Constants used when creating the ENGINE */ |
|
80 static const char *ENGINE_DEVCRYPTO_ID = "devcrypto"; |
|
81 static const char *ENGINE_DEVCRYPTO_NAME = "/dev/crypto engine support"; |
|
82 static const char *CRYPTO_DEVICE = "/dev/crypto"; |
|
83 |
|
84 /* static variables */ |
|
85 static int kernel_fd = -1; |
|
86 static int kernel_fd_ref = 0; |
|
87 static int slot_count = 0; |
|
88 static CK_SLOT_ID *kernel_provider_id = NULL; |
|
89 static int cipher_count = 0; |
|
90 static int *cipher_nids = NULL; |
|
91 pthread_mutex_t *kernel_fd_lock; |
|
92 |
|
93 /* |
|
94 * NIDs for AES counter mode. They will be defined during the engine |
|
95 * initialization. |
|
96 */ |
|
97 static int NID_aes_128_ctr = NID_undef; |
|
98 static int NID_aes_192_ctr = NID_undef; |
|
99 static int NID_aes_256_ctr = NID_undef; |
|
100 |
|
101 /* |
|
102 * Cipher Table for all supported symmetric ciphers. |
|
103 */ |
|
104 static devcrypto_cipher_t cipher_table[] = { |
|
105 /* id, nid, iv_len, min_, max_key_len, */ |
|
106 /* key_type, mech_type, flags, pn_internal_number */ |
|
107 { DEV_DES_CBC, NID_des_cbc, 8, 8, 8, |
|
108 CKK_DES, CKM_DES_CBC, 0, CRYPTO_MECH_INVALID}, |
|
109 { DEV_DES3_CBC, NID_des_ede3_cbc, 8, 24, 24, |
|
110 CKK_DES3, CKM_DES3_CBC, 0, CRYPTO_MECH_INVALID}, |
|
111 { DEV_DES_ECB, NID_des_ecb, 0, 8, 8, |
|
112 CKK_DES, CKM_DES_ECB, 0, CRYPTO_MECH_INVALID}, |
|
113 { DEV_DES3_ECB, NID_des_ede3_ecb, 0, 24, 24, |
|
114 CKK_DES3, CKM_DES3_ECB, 0, CRYPTO_MECH_INVALID}, |
|
115 { DEV_RC4, NID_rc4, 0, 16, 256, |
|
116 CKK_RC4, CKM_RC4, 0, CRYPTO_MECH_INVALID}, |
|
117 { DEV_AES_128_CBC, NID_aes_128_cbc, 16, 16, 16, |
|
118 CKK_AES, CKM_AES_CBC, 0, CRYPTO_MECH_INVALID}, |
|
119 { DEV_AES_192_CBC, NID_aes_192_cbc, 16, 24, 24, |
|
120 CKK_AES, CKM_AES_CBC, 0, CRYPTO_MECH_INVALID}, |
|
121 { DEV_AES_256_CBC, NID_aes_256_cbc, 16, 32, 32, |
|
122 CKK_AES, CKM_AES_CBC, 0, CRYPTO_MECH_INVALID}, |
|
123 { DEV_AES_128_ECB, NID_aes_128_ecb, 0, 16, 16, |
|
124 CKK_AES, CKM_AES_ECB, 0, CRYPTO_MECH_INVALID}, |
|
125 { DEV_AES_192_ECB, NID_aes_192_ecb, 0, 24, 24, |
|
126 CKK_AES, CKM_AES_ECB, 0, CRYPTO_MECH_INVALID}, |
|
127 { DEV_AES_256_ECB, NID_aes_256_ecb, 0, 32, 32, |
|
128 CKK_AES, CKM_AES_ECB, 0, CRYPTO_MECH_INVALID}, |
|
129 { DEV_BLOWFISH_CBC, NID_bf_cbc, 8, 16, 16, |
|
130 CKK_BLOWFISH, CKM_BLOWFISH_CBC, 0, CRYPTO_MECH_INVALID}, |
|
131 /* |
|
132 * For the following 3 AES counter mode entries, we don't know the |
|
133 * NIDs until the engine is initialized |
|
134 */ |
|
135 { DEV_AES_128_CTR, NID_undef, 16, 16, 16, |
|
136 CKK_AES, CKM_AES_CTR, EVP_CIPH_NO_PADDING, |
|
137 CRYPTO_MECH_INVALID}, |
|
138 { DEV_AES_192_CTR, NID_undef, 16, 24, 24, |
|
139 CKK_AES, CKM_AES_CTR, EVP_CIPH_NO_PADDING, |
|
140 CRYPTO_MECH_INVALID}, |
|
141 { DEV_AES_256_CTR, NID_undef, 16, 32, 32, |
|
142 CKK_AES, CKM_AES_CTR, EVP_CIPH_NO_PADDING, |
|
143 CRYPTO_MECH_INVALID}, |
|
144 }; |
|
145 |
|
146 |
|
147 /* Formal declaration for functions in EVP_CIPHER structure */ |
|
148 static int devcrypto_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
|
149 const unsigned char *iv, int enc); |
|
150 static int devcrypto_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
|
151 const unsigned char *in, size_t inl); |
|
152 static int devcrypto_cipher_cleanup(EVP_CIPHER_CTX *ctx); |
|
153 |
|
154 /* |
|
155 * Cipher Algorithms |
|
156 * |
|
157 * OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP. |
|
158 * EVP_CIPHER is defined in evp.h. To maintain binary compatibility the |
|
159 * definition cannot be modified. |
|
160 * Stuff specific to the devcrypto engine is kept in devcrypto_ctx_t, which is |
|
161 * pointed to by cipher_data or md_data. |
|
162 * |
|
163 * Fields: nid, block_size, key_len, iv_len, flags, |
|
164 * init(), do_cipher(), cleanup(), |
|
165 * ctx_size, |
|
166 * set_asn1_parameters(), get_asn1_parameters(), ctrl(), app_data |
|
167 */ |
|
168 static const EVP_CIPHER dev_des_cbc = { |
|
169 NID_des_cbc, |
|
170 8, 8, 8, |
|
171 EVP_CIPH_CBC_MODE, |
|
172 devcrypto_cipher_init, |
|
173 devcrypto_cipher_do_cipher, |
|
174 devcrypto_cipher_cleanup, |
|
175 sizeof (devcrypto_ctx_t), |
|
176 EVP_CIPHER_set_asn1_iv, |
|
177 EVP_CIPHER_get_asn1_iv, |
|
178 NULL |
|
179 }; |
|
180 |
|
181 static const EVP_CIPHER dev_3des_cbc = { |
|
182 NID_des_ede3_cbc, |
|
183 8, 24, 8, |
|
184 EVP_CIPH_CBC_MODE, |
|
185 devcrypto_cipher_init, |
|
186 devcrypto_cipher_do_cipher, |
|
187 devcrypto_cipher_cleanup, |
|
188 sizeof (devcrypto_ctx_t), |
|
189 EVP_CIPHER_set_asn1_iv, |
|
190 EVP_CIPHER_get_asn1_iv, |
|
191 NULL |
|
192 }; |
|
193 |
|
194 /* |
|
195 * ECB modes don't use an Initial Vector, therefore set_asn1_parameters and |
|
196 * get_asn1_parameters fields are set to NULL. |
|
197 */ |
|
198 static const EVP_CIPHER dev_des_ecb = { |
|
199 NID_des_ecb, |
|
200 8, 8, 8, |
|
201 EVP_CIPH_ECB_MODE, |
|
202 devcrypto_cipher_init, |
|
203 devcrypto_cipher_do_cipher, |
|
204 devcrypto_cipher_cleanup, |
|
205 sizeof (devcrypto_ctx_t), |
|
206 NULL, |
|
207 NULL, |
|
208 NULL |
|
209 }; |
|
210 |
|
211 static const EVP_CIPHER dev_3des_ecb = { |
|
212 NID_des_ede3_ecb, |
|
213 8, 24, 8, |
|
214 EVP_CIPH_ECB_MODE, |
|
215 devcrypto_cipher_init, |
|
216 devcrypto_cipher_do_cipher, |
|
217 devcrypto_cipher_cleanup, |
|
218 sizeof (devcrypto_ctx_t), |
|
219 NULL, |
|
220 NULL, |
|
221 NULL |
|
222 }; |
|
223 |
|
224 static const EVP_CIPHER dev_rc4 = { |
|
225 NID_rc4, |
|
226 1, 16, 0, |
|
227 EVP_CIPH_VARIABLE_LENGTH, |
|
228 devcrypto_cipher_init, |
|
229 devcrypto_cipher_do_cipher, |
|
230 devcrypto_cipher_cleanup, |
|
231 sizeof (devcrypto_ctx_t), |
|
232 NULL, |
|
233 NULL, |
|
234 NULL |
|
235 }; |
|
236 |
|
237 static const EVP_CIPHER dev_aes_128_cbc = { |
|
238 NID_aes_128_cbc, |
|
239 16, 16, 16, |
|
240 EVP_CIPH_CBC_MODE, |
|
241 devcrypto_cipher_init, |
|
242 devcrypto_cipher_do_cipher, |
|
243 devcrypto_cipher_cleanup, |
|
244 sizeof (devcrypto_ctx_t), |
|
245 EVP_CIPHER_set_asn1_iv, |
|
246 EVP_CIPHER_get_asn1_iv, |
|
247 NULL |
|
248 }; |
|
249 |
|
250 static const EVP_CIPHER dev_aes_192_cbc = { |
|
251 NID_aes_192_cbc, |
|
252 16, 24, 16, |
|
253 EVP_CIPH_CBC_MODE, |
|
254 devcrypto_cipher_init, |
|
255 devcrypto_cipher_do_cipher, |
|
256 devcrypto_cipher_cleanup, |
|
257 sizeof (devcrypto_ctx_t), |
|
258 EVP_CIPHER_set_asn1_iv, |
|
259 EVP_CIPHER_get_asn1_iv, |
|
260 NULL |
|
261 }; |
|
262 |
|
263 static const EVP_CIPHER dev_aes_256_cbc = { |
|
264 NID_aes_256_cbc, |
|
265 16, 32, 16, |
|
266 EVP_CIPH_CBC_MODE, |
|
267 devcrypto_cipher_init, |
|
268 devcrypto_cipher_do_cipher, |
|
269 devcrypto_cipher_cleanup, |
|
270 sizeof (devcrypto_ctx_t), |
|
271 EVP_CIPHER_set_asn1_iv, |
|
272 EVP_CIPHER_get_asn1_iv, |
|
273 NULL |
|
274 }; |
|
275 |
|
276 |
|
277 /* |
|
278 * ECB modes don't use IV, therefore set_asn1_parameters and |
|
279 * get_asn1_parameters are set to NULL. |
|
280 */ |
|
281 static const EVP_CIPHER dev_aes_128_ecb = { |
|
282 NID_aes_128_ecb, |
|
283 16, 16, 0, |
|
284 EVP_CIPH_ECB_MODE, |
|
285 devcrypto_cipher_init, |
|
286 devcrypto_cipher_do_cipher, |
|
287 devcrypto_cipher_cleanup, |
|
288 sizeof (devcrypto_ctx_t), |
|
289 NULL, |
|
290 NULL, |
|
291 NULL |
|
292 }; |
|
293 |
|
294 static const EVP_CIPHER dev_aes_192_ecb = { |
|
295 NID_aes_192_ecb, |
|
296 16, 24, 0, |
|
297 EVP_CIPH_ECB_MODE, |
|
298 devcrypto_cipher_init, |
|
299 devcrypto_cipher_do_cipher, |
|
300 devcrypto_cipher_cleanup, |
|
301 sizeof (devcrypto_ctx_t), |
|
302 NULL, |
|
303 NULL, |
|
304 NULL |
|
305 }; |
|
306 |
|
307 static const EVP_CIPHER dev_aes_256_ecb = { |
|
308 NID_aes_256_ecb, |
|
309 16, 32, 0, |
|
310 EVP_CIPH_ECB_MODE, |
|
311 devcrypto_cipher_init, |
|
312 devcrypto_cipher_do_cipher, |
|
313 devcrypto_cipher_cleanup, |
|
314 sizeof (devcrypto_ctx_t), |
|
315 NULL, |
|
316 NULL, |
|
317 NULL |
|
318 }; |
|
319 |
|
320 static const EVP_CIPHER dev_bf_cbc = { |
|
321 NID_bf_cbc, |
|
322 8, 16, 8, |
|
323 EVP_CIPH_VARIABLE_LENGTH, |
|
324 devcrypto_cipher_init, |
|
325 devcrypto_cipher_do_cipher, |
|
326 devcrypto_cipher_cleanup, |
|
327 sizeof (devcrypto_ctx_t), |
|
328 EVP_CIPHER_set_asn1_iv, |
|
329 EVP_CIPHER_get_asn1_iv, |
|
330 NULL |
|
331 }; |
|
332 |
|
333 |
|
334 /* |
|
335 * NID_undef's will be changed for AES counter mode, as soon they are created. |
|
336 */ |
|
337 static EVP_CIPHER dev_aes_128_ctr = { |
|
338 NID_undef, |
|
339 16, 16, 16, |
|
340 EVP_CIPH_CBC_MODE, |
|
341 devcrypto_cipher_init, |
|
342 devcrypto_cipher_do_cipher, |
|
343 devcrypto_cipher_cleanup, |
|
344 sizeof (devcrypto_ctx_t), |
|
345 EVP_CIPHER_set_asn1_iv, |
|
346 EVP_CIPHER_get_asn1_iv, |
|
347 NULL |
|
348 }; |
|
349 |
|
350 static EVP_CIPHER dev_aes_192_ctr = { |
|
351 NID_undef, |
|
352 16, 24, 16, |
|
353 EVP_CIPH_CBC_MODE, |
|
354 devcrypto_cipher_init, |
|
355 devcrypto_cipher_do_cipher, |
|
356 devcrypto_cipher_cleanup, |
|
357 sizeof (devcrypto_ctx_t), |
|
358 EVP_CIPHER_set_asn1_iv, |
|
359 EVP_CIPHER_get_asn1_iv, |
|
360 NULL |
|
361 }; |
|
362 |
|
363 static EVP_CIPHER dev_aes_256_ctr = { |
|
364 NID_undef, |
|
365 16, 32, 16, |
|
366 EVP_CIPH_CBC_MODE, |
|
367 devcrypto_cipher_init, |
|
368 devcrypto_cipher_do_cipher, |
|
369 devcrypto_cipher_cleanup, |
|
370 sizeof (devcrypto_ctx_t), |
|
371 EVP_CIPHER_set_asn1_iv, |
|
372 EVP_CIPHER_get_asn1_iv, |
|
373 NULL |
|
374 }; |
|
375 |
|
376 |
|
377 |
|
378 /* |
|
379 * This function creates a new NID. |
|
380 */ |
|
381 static int |
|
382 devcrypto_add_NID(char *sn, char *ln) |
|
383 { |
|
384 ASN1_OBJECT *o; |
|
385 int nid; |
|
386 |
|
387 if ((o = ASN1_OBJECT_create(OBJ_new_nid(1), (unsigned char *)"", |
|
388 1, sn, ln)) == NULL) { |
|
389 return (0); |
|
390 } |
|
391 |
|
392 nid = OBJ_add_object(o); /* will return NID_undef on error */ |
|
393 ASN1_OBJECT_free(o); |
|
394 return (nid); |
|
395 } |
|
396 |
|
397 |
|
398 /* |
|
399 * This function creates new NIDs for AES counter mode algorithms. |
|
400 * Note that OpenSSL doesn't support them now so we have to help |
|
401 * ourselves here. |
|
402 */ |
|
403 static int |
|
404 devcrypto_add_aes_ctr_NIDs(void) |
|
405 { |
|
406 if (NID_aes_256_ctr != NID_undef) /* already set */ |
|
407 return (1); |
|
408 |
|
409 NID_aes_128_ctr = devcrypto_add_NID("AES-128-CTR", "aes-128-ctr"); |
|
410 if (NID_aes_128_ctr == NID_undef) |
|
411 goto failed; |
|
412 cipher_table[DEV_AES_128_CTR].nid = |
|
413 dev_aes_128_ctr.nid = NID_aes_128_ctr; |
|
414 |
|
415 NID_aes_192_ctr = devcrypto_add_NID("AES-192-CTR", "aes-192-ctr"); |
|
416 if (NID_aes_192_ctr == NID_undef) |
|
417 goto failed; |
|
418 cipher_table[DEV_AES_192_CTR].nid = |
|
419 dev_aes_192_ctr.nid = NID_aes_192_ctr; |
|
420 |
|
421 NID_aes_256_ctr = devcrypto_add_NID("AES-256-CTR", "aes-256-ctr"); |
|
422 if (NID_aes_256_ctr == NID_undef) |
|
423 goto failed; |
|
424 cipher_table[DEV_AES_256_CTR].nid = |
|
425 dev_aes_256_ctr.nid = NID_aes_256_ctr; |
|
426 |
|
427 return (1); |
|
428 |
|
429 failed: |
|
430 return (0); |
|
431 } |
|
432 |
|
433 |
|
434 static void |
|
435 devcrypto_free_aes_ctr_NIDs(void) |
|
436 { |
|
437 ASN1_OBJECT *ob = NULL; |
|
438 |
|
439 if (NID_aes_128_ctr != NID_undef) { |
|
440 ob = OBJ_nid2obj(NID_aes_128_ctr); |
|
441 if (ob != NULL) |
|
442 ASN1_OBJECT_free(ob); |
|
443 } |
|
444 |
|
445 if (NID_aes_192_ctr != NID_undef) { |
|
446 ob = OBJ_nid2obj(NID_aes_192_ctr); |
|
447 if (ob != NULL) |
|
448 ASN1_OBJECT_free(ob); |
|
449 } |
|
450 |
|
451 if (NID_aes_256_ctr != NID_undef) { |
|
452 ob = OBJ_nid2obj(NID_aes_256_ctr); |
|
453 if (ob != NULL) |
|
454 ASN1_OBJECT_free(ob); |
|
455 } |
|
456 } |
|
457 |
|
458 /* |
|
459 * Open the /dev/crypto device |
|
460 */ |
|
461 static int |
|
462 devcrypto_open(void) |
|
463 { |
|
464 int fd = -1; |
|
465 |
|
466 if (kernel_fd != -1) { /* already open */ |
|
467 (void) pthread_mutex_lock(kernel_fd_lock); |
|
468 kernel_fd_ref++; |
|
469 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
470 return (1); |
|
471 } |
|
472 |
|
473 (void) pthread_mutex_lock(kernel_fd_lock); |
|
474 fd = open(CRYPTO_DEVICE, O_RDWR); |
|
475 if (fd == -1) { |
|
476 #ifdef DEBUG |
|
477 (void) fprintf(stderr, |
|
478 "libdevcrypto: open /dev/crypto failed, errno=%x\n", |
|
479 errno); |
|
480 #endif |
|
481 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
482 return (0); |
|
483 } |
|
484 |
|
485 if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) { |
|
486 #ifdef DEBUG |
|
487 (void) fprintf(stderr, "libdevcrypto: failed to fcntl\n"); |
|
488 #endif |
|
489 (void) close(fd); |
|
490 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
491 return (0); |
|
492 } |
|
493 |
|
494 kernel_fd = fd; |
|
495 kernel_fd_ref++; |
|
496 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
497 return (1); |
|
498 } |
|
499 |
|
500 |
|
501 /* |
|
502 * This function gets the total number of hardware providers presented in |
|
503 * the system first. If there is any hardware providers, then it will get |
|
504 * the kernel provider id for each hardware slot also. |
|
505 */ |
|
506 static int |
|
507 devcrypto_get_slot_info(void) |
|
508 { |
|
509 crypto_get_provider_list_t *pl = NULL; |
|
510 int ret = 1; |
|
511 int r; |
|
512 int i; |
|
513 |
|
514 /* Already have the information */ |
|
515 if (kernel_provider_id != NULL) |
|
516 return (1); |
|
517 |
|
518 /* Find out how many hardware slots are presented. */ |
|
519 pl = OPENSSL_malloc(sizeof (crypto_get_provider_list_t)); |
|
520 if (pl == NULL) |
|
521 return (0); |
|
522 |
|
523 pl->pl_count = 0; |
|
524 while ((r = ioctl(kernel_fd, CRYPTO_GET_PROVIDER_LIST, pl)) < 0) { |
|
525 if (errno != EINTR) |
|
526 break; |
|
527 } |
|
528 if (r < 0 || pl->pl_return_value != CRYPTO_SUCCESS) { |
|
529 #ifdef DEBUG |
|
530 (void) fprintf(stderr, "libdevcrypto:CRYPTO_GET_PROVIDER_LIST:" |
|
531 "ret (r) = 0x%x, (rv) = 0x%x\n", r, pl->pl_return_value); |
|
532 #endif /* DEBUG */ |
|
533 ret = 0; |
|
534 goto out; |
|
535 } |
|
536 |
|
537 slot_count = pl->pl_count; |
|
538 if (slot_count == 0) { |
|
539 #ifdef DEBUG |
|
540 (void) fprintf(stderr, "libdevcrypto: no hw providers\n"); |
|
541 #endif /* DEBUG */ |
|
542 ret = 0; |
|
543 goto out; |
|
544 } |
|
545 |
|
546 /* Get the provider ID for each slot from kernel and save it */ |
|
547 kernel_provider_id = OPENSSL_malloc(sizeof (CK_SLOT_ID) * slot_count); |
|
548 if (kernel_provider_id == NULL) { |
|
549 ret = 0; |
|
550 goto out; |
|
551 } |
|
552 |
|
553 (void) OPENSSL_free(pl); |
|
554 pl = OPENSSL_malloc(slot_count * sizeof (crypto_get_provider_list_t)); |
|
555 if (pl == NULL) { |
|
556 ret = 0; |
|
557 goto out; |
|
558 } |
|
559 |
|
560 pl->pl_count = slot_count; |
|
561 while ((r = ioctl(kernel_fd, CRYPTO_GET_PROVIDER_LIST, pl)) < 0) { |
|
562 if (errno != EINTR) |
|
563 break; |
|
564 } |
|
565 if (r < 0 || (pl->pl_return_value != CRYPTO_SUCCESS)) { |
|
566 #ifdef DEBUG |
|
567 (void) fprintf(stderr, "libdevcrypto:CRYPTO_GET_PROVIDER_LIST:" |
|
568 "ret (r) = 0x%x, (rv) = 0x%x\n", r, pl->pl_return_value); |
|
569 #endif /* DEBUG */ |
|
570 ret = 0; |
|
571 goto out; |
|
572 } |
|
573 |
|
574 for (i = 0; i < slot_count; i++) { |
|
575 kernel_provider_id[i] = pl->pl_list[i].pe_provider_id; |
|
576 #ifdef DEBUG |
|
577 (void) fprintf(stderr, "libdevcrypto: i = %d, " |
|
578 "kernel_provider_id = %d\n", i, kernel_provider_id[i]); |
|
579 #endif /* DEBUG */ |
|
580 } |
|
581 |
|
582 out: |
|
583 if (pl != NULL) |
|
584 (void) OPENSSL_free(pl); |
|
585 |
|
586 if (ret == 0 && kernel_provider_id != NULL) { |
|
587 (void) OPENSSL_free(kernel_provider_id); |
|
588 kernel_provider_id = NULL; |
|
589 } |
|
590 |
|
591 return (ret); |
|
592 } |
|
593 |
|
594 |
|
595 /* |
|
596 * This function checks if the "nid" is already in the nid list. |
|
597 */ |
|
598 static int |
|
599 nid_in_list(int nid, int *nid_list, int count) |
|
600 { |
|
601 int i = 0; |
|
602 |
|
603 if (nid_list == NULL || count <= 0) |
|
604 return (0); |
|
605 |
|
606 while (i < count) { |
|
607 if (nid == nid_list[i]) |
|
608 break; |
|
609 i++; |
|
610 } |
|
611 return (i < count ? 1 : 0); |
|
612 } |
|
613 |
|
614 /* |
|
615 * This function is to get all the ciphers supported by hardware providers. |
|
616 * If this function is successfully completed, then the following 2 global |
|
617 * variables will be set. |
|
618 * cipher_count - the number of ciphers found in all hardware providers. |
|
619 * cipher_nids - the nid list for all the ciphers. |
|
620 */ |
|
621 static int |
|
622 devcrypto_get_hw_ciphers(void) |
|
623 { |
|
624 crypto_get_provider_mechanism_info_t mechinfo; |
|
625 int max_cipher_count; |
|
626 int *tmp_nids = NULL; |
|
627 const char *mech_string; |
|
628 int r; |
|
629 int i, j; |
|
630 |
|
631 if (slot_count <= 0) /* no hardware provider */ |
|
632 return (0); |
|
633 |
|
634 max_cipher_count = slot_count * DEV_CIPHER_MAX + 1; |
|
635 tmp_nids = OPENSSL_malloc(max_cipher_count * sizeof (int)); |
|
636 if (tmp_nids == NULL) { |
|
637 /* not enough memory */ |
|
638 goto failed; |
|
639 } |
|
640 |
|
641 for (i = 0; i < slot_count; i++) { |
|
642 mechinfo.mi_provider_id = kernel_provider_id[i]; |
|
643 for (j = 0; j < DEV_CIPHER_MAX; j++) { |
|
644 mech_string = |
|
645 pkcs11_mech2str(cipher_table[j].mech_type); |
|
646 if (mech_string == NULL) { |
|
647 continue; /* shouldn't happen; skip it */ |
|
648 } |
|
649 |
|
650 (void) strlcpy(mechinfo.mi_mechanism_name, |
|
651 mech_string, CRYPTO_MAX_MECH_NAME); |
|
652 while ((r = ioctl(kernel_fd, |
|
653 CRYPTO_GET_PROVIDER_MECHANISM_INFO, |
|
654 &mechinfo)) < 0) { |
|
655 if (errno != EINTR) |
|
656 break; |
|
657 } |
|
658 if (r < 0) { |
|
659 goto failed; |
|
660 } |
|
661 |
|
662 if (mechinfo.mi_return_value == CRYPTO_SUCCESS) { |
|
663 /* |
|
664 * Found this mechanism in hardware providers. |
|
665 * If it is not in the nid list yet, add it. |
|
666 */ |
|
667 if (!nid_in_list(cipher_table[j].nid, |
|
668 tmp_nids, cipher_count)) { |
|
669 tmp_nids[cipher_count] = |
|
670 cipher_table[j].nid; |
|
671 cipher_count++; |
|
672 } |
|
673 } |
|
674 } |
|
675 } |
|
676 |
|
677 if (cipher_count > 0) { |
|
678 cipher_nids = tmp_nids; |
|
679 } |
|
680 |
|
681 return (1); |
|
682 |
|
683 failed: |
|
684 if (r < 0 || cipher_count == 0) { |
|
685 if (tmp_nids != NULL) |
|
686 OPENSSL_free(tmp_nids); |
|
687 } |
|
688 return (0); |
|
689 } |
|
690 |
|
691 /* |
|
692 * Registered by the ENGINE when used to find out how to deal with |
|
693 * a particular NID in the ENGINE. This says what we'll do at the |
|
694 * top level - note, that list is restricted by what we answer with. |
|
695 */ |
|
696 static int |
|
697 devcrypto_get_all_ciphers(ENGINE *e, const EVP_CIPHER **cipher, |
|
698 const int **nids, int nid) |
|
699 { |
|
700 if (!cipher) { |
|
701 *nids = (cipher_count > 0) ? cipher_nids : NULL; |
|
702 return (cipher_count); |
|
703 } |
|
704 |
|
705 switch (nid) { |
|
706 case NID_des_cbc: |
|
707 *cipher = &dev_des_cbc; |
|
708 break; |
|
709 case NID_des_ede3_cbc: |
|
710 *cipher = &dev_3des_cbc; |
|
711 break; |
|
712 case NID_des_ecb: |
|
713 *cipher = &dev_des_ecb; |
|
714 break; |
|
715 case NID_des_ede3_ecb: |
|
716 *cipher = &dev_3des_ecb; |
|
717 break; |
|
718 case NID_rc4: |
|
719 *cipher = &dev_rc4; |
|
720 break; |
|
721 case NID_aes_128_cbc: |
|
722 *cipher = &dev_aes_128_cbc; |
|
723 break; |
|
724 case NID_aes_192_cbc: |
|
725 *cipher = &dev_aes_192_cbc; |
|
726 break; |
|
727 case NID_aes_256_cbc: |
|
728 *cipher = &dev_aes_256_cbc; |
|
729 break; |
|
730 case NID_aes_128_ecb: |
|
731 *cipher = &dev_aes_128_ecb; |
|
732 break; |
|
733 case NID_aes_192_ecb: |
|
734 *cipher = &dev_aes_192_ecb; |
|
735 break; |
|
736 case NID_aes_256_ecb: |
|
737 *cipher = &dev_aes_256_ecb; |
|
738 break; |
|
739 case NID_bf_cbc: |
|
740 *cipher = &dev_bf_cbc; |
|
741 break; |
|
742 default: |
|
743 /* |
|
744 * We cannot put the NIDs for AES counter mode in separated |
|
745 * cases as above because they are not constants. |
|
746 */ |
|
747 if (nid == NID_aes_128_ctr) |
|
748 *cipher = &dev_aes_128_ctr; |
|
749 else if (nid == NID_aes_192_ctr) |
|
750 *cipher = &dev_aes_192_ctr; |
|
751 else if (nid == NID_aes_256_ctr) |
|
752 *cipher = &dev_aes_256_ctr; |
|
753 else |
|
754 *cipher = NULL; |
|
755 break; |
|
756 } |
|
757 |
|
758 return (*cipher != NULL); |
|
759 } |
|
760 |
|
761 |
|
762 static int |
|
763 get_cipher_id_by_nid(int nid) |
|
764 { |
|
765 int i; |
|
766 |
|
767 for (i = 0; i < DEV_CIPHER_MAX; i++) |
|
768 if (cipher_table[i].nid == nid) |
|
769 return (cipher_table[i].id); |
|
770 return (-1); |
|
771 } |
|
772 |
|
773 |
|
774 static int |
|
775 get_slotid_by_mechanism(const char *mech_string, CK_SLOT_ID *slot_id) |
|
776 { |
|
777 crypto_get_provider_mechanism_info_t mechanism_info; |
|
778 uint_t rv; |
|
779 int r; |
|
780 int i = 0; |
|
781 |
|
782 (void) strlcpy(mechanism_info.mi_mechanism_name, mech_string, |
|
783 CRYPTO_MAX_MECH_NAME); |
|
784 while (i < slot_count) { |
|
785 mechanism_info.mi_provider_id = kernel_provider_id[i]; |
|
786 while ((r = ioctl(kernel_fd, |
|
787 CRYPTO_GET_PROVIDER_MECHANISM_INFO, |
|
788 &mechanism_info)) < 0) { |
|
789 if (errno != EINTR) |
|
790 break; |
|
791 } |
|
792 if (r < 0) { |
|
793 return (0); /* ioctl function failed */ |
|
794 } |
|
795 rv = mechanism_info.mi_return_value; |
|
796 if (rv == 0) { /* found it */ |
|
797 *slot_id = kernel_provider_id[i]; |
|
798 return (1); |
|
799 } |
|
800 i++; |
|
801 } |
|
802 |
|
803 return (0); |
|
804 } |
|
805 |
|
806 |
|
807 static int |
|
808 devcrypto_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
|
809 const unsigned char *iv, int enc) |
|
810 { |
|
811 devcrypto_ctx_t *devc_ctx = ctx->cipher_data; |
|
812 crypto_encrypt_init_t encrypt_init; |
|
813 crypto_decrypt_init_t decrypt_init; |
|
814 crypto_open_session_t session; |
|
815 crypto_get_mechanism_number_t get_number; |
|
816 CK_AES_CTR_PARAMS aes_ctr_params; |
|
817 devcrypto_cipher_t *the_cipher; |
|
818 const char *mech_string; |
|
819 CK_SLOT_ID slot_id; |
|
820 int index; |
|
821 int r; |
|
822 uint_t rv = 0; |
|
823 |
|
824 if (key == NULL) { |
|
825 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_CIPHER_KEY); |
|
826 return (0); |
|
827 } |
|
828 |
|
829 /* get the cipher entry index in cipher_table from nid */ |
|
830 index = get_cipher_id_by_nid(ctx->cipher->nid); |
|
831 if (index < 0 || index >= DEV_CIPHER_MAX) { |
|
832 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_CIPHER_NID); |
|
833 return (0); |
|
834 } |
|
835 the_cipher = &cipher_table[index]; |
|
836 |
|
837 /* check key size */ |
|
838 if (ctx->cipher->iv_len < the_cipher->iv_len || |
|
839 ctx->key_len < the_cipher->min_key_len || |
|
840 ctx->key_len > the_cipher->max_key_len) { |
|
841 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_KEY_OR_IV_LEN_PROBLEM); |
|
842 return (0); |
|
843 } |
|
844 |
|
845 /* Set cipher flags, if any */ |
|
846 ctx->flags |= the_cipher->flags; |
|
847 |
|
848 /* get the mechanism string */ |
|
849 mech_string = pkcs11_mech2str(the_cipher->mech_type); |
|
850 if (mech_string == NULL) { |
|
851 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_MECH_STRING); |
|
852 return (0); |
|
853 } |
|
854 |
|
855 #ifdef DEBUG |
|
856 (void) fprintf(stderr, "libdevcrypto: mech_string=%s\n", mech_string); |
|
857 #endif |
|
858 |
|
859 /* Find the slot that supports this mechanism */ |
|
860 if (!get_slotid_by_mechanism(mech_string, &slot_id)) { |
|
861 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_FIND_SLOT_BY_MECH); |
|
862 #ifdef DEBUG |
|
863 (void) fprintf(stderr, |
|
864 "libdevcrypto: failed to find a slot with %s\n", |
|
865 mech_string); |
|
866 #endif |
|
867 return (0); |
|
868 } |
|
869 |
|
870 #ifdef DEBUG |
|
871 (void) fprintf(stderr, "libdevcrypto: found a slot with %s, " |
|
872 "slot_id = %d\n", mech_string, slot_id); |
|
873 #endif |
|
874 |
|
875 /* Open a session on this slot */ |
|
876 session.os_provider_id = slot_id; |
|
877 session.os_flags = CKF_RW_SESSION | CKF_SERIAL_SESSION; |
|
878 while ((r = ioctl(kernel_fd, CRYPTO_OPEN_SESSION, &session)) < 0) { |
|
879 if (errno != EINTR) |
|
880 break; |
|
881 } |
|
882 rv = session.os_return_value; |
|
883 if (r || rv) { |
|
884 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_OPEN_SESSION); |
|
885 #ifdef DEBUG |
|
886 (void) fprintf(stderr, |
|
887 "libdevcrypto:cipher_init:failed to open a session\n"); |
|
888 #endif /* DEBUG */ |
|
889 goto failed; |
|
890 } |
|
891 |
|
892 #ifdef DEBUG |
|
893 (void) fprintf(stderr, "libdevcrypto:cipher_init: open session = %d\n", |
|
894 session.os_session); |
|
895 #endif /* DEBUG */ |
|
896 |
|
897 /* save the session_id */ |
|
898 devc_ctx->session_id = session.os_session; |
|
899 |
|
900 /* |
|
901 * Get the kernel mechanism number for this mechanism, if it has not |
|
902 * been retrieved yet. |
|
903 */ |
|
904 if (the_cipher->pn_internal_number == CRYPTO_MECH_INVALID) { |
|
905 get_number.pn_mechanism_string = (char *)mech_string; |
|
906 get_number.pn_mechanism_len = strlen(mech_string) + 1; |
|
907 while ((r = ioctl(kernel_fd, CRYPTO_GET_MECHANISM_NUMBER, |
|
908 &get_number)) < 0) { |
|
909 if (errno != EINTR) |
|
910 break; |
|
911 } |
|
912 rv = get_number.pn_return_value; |
|
913 if (r || rv) { |
|
914 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, |
|
915 DEVC_R_GET_MECHANISM_NUMBER); |
|
916 #ifdef DEBUG |
|
917 (void) fprintf(stderr, "libdevcrypto:cipher_init: " |
|
918 "failed to get the kernel mech number.\n"); |
|
919 #endif /* DEBUG */ |
|
920 goto failed; |
|
921 } |
|
922 |
|
923 the_cipher->pn_internal_number = get_number.pn_internal_number; |
|
924 } |
|
925 |
|
926 /* Crypto Init */ |
|
927 if (ctx->encrypt) { |
|
928 encrypt_init.ei_session = session.os_session; |
|
929 encrypt_init.ei_key.ck_format = CRYPTO_KEY_RAW; |
|
930 encrypt_init.ei_key.ck_obj_id = 0; |
|
931 encrypt_init.ei_key.ck_data = (void *) key; |
|
932 encrypt_init.ei_key.ck_length = ctx->key_len * 8; |
|
933 encrypt_init.ei_mech.cm_type = the_cipher->pn_internal_number; |
|
934 |
|
935 if (ctx->cipher->nid == NID_aes_128_ctr || |
|
936 ctx->cipher->nid == NID_aes_192_ctr || |
|
937 ctx->cipher->nid == NID_aes_256_ctr) { |
|
938 encrypt_init.ei_mech.cm_param = |
|
939 (void *) (&aes_ctr_params); |
|
940 encrypt_init.ei_mech.cm_param_len = |
|
941 sizeof (aes_ctr_params); |
|
942 |
|
943 aes_ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8; |
|
944 OPENSSL_assert(ctx->cipher->iv_len == AES_BLOCK_SIZE); |
|
945 (void) memcpy(aes_ctr_params.cb, ctx->iv, |
|
946 AES_BLOCK_SIZE); |
|
947 } else { |
|
948 if (the_cipher->iv_len > 0) { |
|
949 encrypt_init.ei_mech.cm_param = |
|
950 (char *)ctx->iv; |
|
951 encrypt_init.ei_mech.cm_param_len = |
|
952 ctx->cipher->iv_len; |
|
953 } else { |
|
954 encrypt_init.ei_mech.cm_param = NULL; |
|
955 encrypt_init.ei_mech.cm_param_len = 0; |
|
956 } |
|
957 } |
|
958 |
|
959 while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_INIT, |
|
960 &encrypt_init)) < 0) { |
|
961 if (errno != EINTR) |
|
962 break; |
|
963 } |
|
964 rv = encrypt_init.ei_return_value; |
|
965 |
|
966 } else { |
|
967 decrypt_init.di_session = session.os_session; |
|
968 decrypt_init.di_key.ck_format = CRYPTO_KEY_RAW; |
|
969 decrypt_init.di_key.ck_obj_id = 0; |
|
970 decrypt_init.di_key.ck_data = (void *) key; |
|
971 decrypt_init.di_key.ck_length = ctx->key_len * 8; |
|
972 decrypt_init.di_mech.cm_type = the_cipher->pn_internal_number; |
|
973 |
|
974 if (ctx->cipher->nid == NID_aes_128_ctr || |
|
975 ctx->cipher->nid == NID_aes_192_ctr || |
|
976 ctx->cipher->nid == NID_aes_256_ctr) { |
|
977 decrypt_init.di_mech.cm_param = |
|
978 (void *)(&aes_ctr_params); |
|
979 decrypt_init.di_mech.cm_param_len = |
|
980 sizeof (aes_ctr_params); |
|
981 aes_ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8; |
|
982 OPENSSL_assert(ctx->cipher->iv_len == AES_BLOCK_SIZE); |
|
983 (void) memcpy(aes_ctr_params.cb, ctx->iv, |
|
984 AES_BLOCK_SIZE); |
|
985 } else { |
|
986 if (the_cipher->iv_len > 0) { |
|
987 decrypt_init.di_mech.cm_param = |
|
988 (char *)ctx->iv; |
|
989 decrypt_init.di_mech.cm_param_len = |
|
990 ctx->cipher->iv_len; |
|
991 } else { |
|
992 decrypt_init.di_mech.cm_param = NULL; |
|
993 decrypt_init.di_mech.cm_param_len = 0; |
|
994 } |
|
995 } |
|
996 |
|
997 while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_INIT, |
|
998 &decrypt_init)) < 0) { |
|
999 if (errno != EINTR) |
|
1000 break; |
|
1001 } |
|
1002 rv = decrypt_init.di_return_value; |
|
1003 } |
|
1004 |
|
1005 failed: |
|
1006 if (r || rv) { |
|
1007 if (ctx->encrypt) |
|
1008 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_ENCRYPT_INIT); |
|
1009 else |
|
1010 DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_DECRYPT_INIT); |
|
1011 |
|
1012 return (0); |
|
1013 } |
|
1014 |
|
1015 return (1); |
|
1016 } |
|
1017 |
|
1018 |
|
1019 /* |
|
1020 * ENCRYPT_UPDATE or DECRYPT_UPDATE |
|
1021 */ |
|
1022 static int |
|
1023 devcrypto_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
|
1024 const unsigned char *in, size_t inl) |
|
1025 { |
|
1026 crypto_encrypt_update_t encrypt_update; |
|
1027 crypto_decrypt_update_t decrypt_update; |
|
1028 devcrypto_ctx_t *devc_ctx = ctx->cipher_data; |
|
1029 int r = 0, rv = 0; |
|
1030 |
|
1031 if (ctx->encrypt) { |
|
1032 encrypt_update.eu_session = devc_ctx->session_id; |
|
1033 encrypt_update.eu_databuf = (char *)in; |
|
1034 encrypt_update.eu_datalen = inl; |
|
1035 encrypt_update.eu_encrbuf = (char *)out; |
|
1036 encrypt_update.eu_encrlen = inl; |
|
1037 |
|
1038 while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_UPDATE, |
|
1039 &encrypt_update)) < 0) { |
|
1040 if (errno != EINTR) |
|
1041 break; |
|
1042 } |
|
1043 rv = encrypt_update.eu_return_value; |
|
1044 |
|
1045 } else { /* decrypt */ |
|
1046 decrypt_update.du_session = devc_ctx->session_id; |
|
1047 decrypt_update.du_encrbuf = (char *)in; |
|
1048 decrypt_update.du_encrlen = inl; |
|
1049 decrypt_update.du_databuf = (char *)out; |
|
1050 decrypt_update.du_datalen = inl; |
|
1051 |
|
1052 while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_UPDATE, |
|
1053 &decrypt_update)) < 0) { |
|
1054 if (errno != EINTR) |
|
1055 break; |
|
1056 } |
|
1057 rv = decrypt_update.du_return_value; |
|
1058 } |
|
1059 |
|
1060 if (r || rv) { |
|
1061 if (ctx->encrypt) |
|
1062 DEVCRYPTOerr(DEVC_F_CIPHER_DO_CIPHER, |
|
1063 DEVC_R_ENCRYPT_UPDATE); |
|
1064 else |
|
1065 DEVCRYPTOerr(DEVC_F_CIPHER_DO_CIPHER, |
|
1066 DEVC_R_DECRYPT_UPDATE); |
|
1067 |
|
1068 #ifdef DEBUG |
|
1069 (void) fprintf(stderr, "libdevcrypto:crypto_do ret (r) = 0x%x," |
|
1070 "crypto ret (rv) = 0x%x,", r, rv); |
|
1071 #endif /* DEBUG */ |
|
1072 return (0); |
|
1073 } |
|
1074 |
|
1075 return (1); |
|
1076 } |
|
1077 |
|
1078 |
|
1079 /* |
|
1080 * ENCRYPT_FINAL or DECRYPT_FINAL |
|
1081 */ |
|
1082 static int |
|
1083 devcrypto_cipher_cleanup(EVP_CIPHER_CTX *ctx) |
|
1084 { |
|
1085 crypto_encrypt_final_t encrypt_final; |
|
1086 crypto_decrypt_final_t decrypt_final; |
|
1087 crypto_close_session_t session; |
|
1088 devcrypto_ctx_t *devc_ctx = ctx->cipher_data; |
|
1089 char buf[EVP_MAX_BLOCK_LENGTH]; |
|
1090 int r; |
|
1091 uint_t rv = 0; |
|
1092 int ret = 1; |
|
1093 |
|
1094 if (ctx->encrypt) { |
|
1095 encrypt_final.ef_session = devc_ctx->session_id; |
|
1096 encrypt_final.ef_encrbuf = buf; |
|
1097 encrypt_final.ef_encrlen = sizeof (buf); |
|
1098 while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_FINAL, |
|
1099 &encrypt_final)) < 0) { |
|
1100 if (errno != EINTR) |
|
1101 break; |
|
1102 } |
|
1103 rv = encrypt_final.ef_return_value; |
|
1104 |
|
1105 } else { |
|
1106 decrypt_final.df_session = devc_ctx->session_id; |
|
1107 decrypt_final.df_databuf = buf; |
|
1108 decrypt_final.df_datalen = sizeof (buf); |
|
1109 while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_FINAL, |
|
1110 &decrypt_final)) < 0) { |
|
1111 if (errno != EINTR) |
|
1112 break; |
|
1113 } |
|
1114 rv = decrypt_final.df_return_value; |
|
1115 } |
|
1116 |
|
1117 #ifdef DEBUG |
|
1118 if (ctx->encrypt) |
|
1119 (void) fprintf(stderr, "libdevcrypto:CRYPTO_ENCRYPT_FINAL " |
|
1120 "ret (r) = 0x%x, (rv) = 0x%x\n", r, rv); |
|
1121 else |
|
1122 (void) fprintf(stderr, "libdevcrypto:CRYPTO_DECRYPT_FINAL " |
|
1123 "ret (r) = 0x%x, (rv) = 0x%x\n", r, rv); |
|
1124 #endif /* DEBUG */ |
|
1125 |
|
1126 if (r || rv) { |
|
1127 if (ctx->encrypt) |
|
1128 DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP, |
|
1129 DEVC_R_ENCRYPT_FINAL); |
|
1130 else |
|
1131 DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP, |
|
1132 DEVC_R_DECRYPT_FINAL); |
|
1133 ret = 0; |
|
1134 } |
|
1135 |
|
1136 /* close the session */ |
|
1137 session.cs_session = devc_ctx->session_id; |
|
1138 while ((r = ioctl(kernel_fd, CRYPTO_CLOSE_SESSION, &session)) < 0) { |
|
1139 if (errno != EINTR) |
|
1140 break; |
|
1141 } |
|
1142 |
|
1143 #ifdef DEBUG |
|
1144 (void) fprintf(stderr, "libdevcrypto:CRYPTO_CLOSE_SESSION, " |
|
1145 "session id = %d ret (r) = 0x%x, crypto ret (rv) = 0x%x\n", |
|
1146 devc_ctx->session_id, r, rv); |
|
1147 #endif /* DEBUG */ |
|
1148 |
|
1149 if (r || rv) { |
|
1150 DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP, DEVC_R_CLOSE_SESSION); |
|
1151 ret = 0; |
|
1152 } |
|
1153 |
|
1154 return (ret); |
|
1155 } |
|
1156 |
|
1157 static void |
|
1158 devcrypto_cleanup(void) |
|
1159 { |
|
1160 if (kernel_fd == -1) |
|
1161 return; |
|
1162 |
|
1163 (void) pthread_mutex_lock(kernel_fd_lock); |
|
1164 kernel_fd_ref--; |
|
1165 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
1166 |
|
1167 if (kernel_fd_ref == 0) { |
|
1168 (void) pthread_mutex_lock(kernel_fd_lock); |
|
1169 (void) close(kernel_fd); |
|
1170 kernel_fd = -1; |
|
1171 if (kernel_provider_id != NULL) { |
|
1172 OPENSSL_free(kernel_provider_id); |
|
1173 kernel_provider_id = NULL; |
|
1174 } |
|
1175 if (cipher_nids != NULL) { |
|
1176 OPENSSL_free(cipher_nids); |
|
1177 cipher_nids = NULL; |
|
1178 } |
|
1179 devcrypto_free_aes_ctr_NIDs(); |
|
1180 (void) pthread_mutex_unlock(kernel_fd_lock); |
|
1181 (void) pthread_mutex_destroy(kernel_fd_lock); |
|
1182 OPENSSL_free(kernel_fd_lock); |
|
1183 kernel_fd_lock = NULL; |
|
1184 } |
|
1185 } |
|
1186 |
|
1187 static int |
|
1188 devcrypto_destroy(ENGINE *e) |
|
1189 { |
|
1190 ERR_unload_devcrypto_strings(); |
|
1191 return (1); |
|
1192 } |
|
1193 |
|
1194 static int |
|
1195 devcrypto_finish(ENGINE *e) |
|
1196 { |
|
1197 devcrypto_cleanup(); |
|
1198 return (1); |
|
1199 } |
|
1200 |
|
1201 /* |
|
1202 * Set up the engine info and get the /dev/crypto engine ready. |
|
1203 */ |
|
1204 static int |
|
1205 devcrypto_bind(ENGINE *e) |
|
1206 { |
|
1207 #ifdef DEBUG |
|
1208 int i; |
|
1209 #endif |
|
1210 |
|
1211 /* Get the NIDs for AES counter mode algorithms first. */ |
|
1212 if (devcrypto_add_aes_ctr_NIDs() == 0) { |
|
1213 return (0); |
|
1214 } |
|
1215 |
|
1216 /* Create a lock for the devcrypto device file descriptor */ |
|
1217 if (kernel_fd_lock == NULL) { |
|
1218 kernel_fd_lock = OPENSSL_malloc(sizeof (pthread_mutex_t)); |
|
1219 if (kernel_fd_lock == NULL) { |
|
1220 devcrypto_free_aes_ctr_NIDs(); |
|
1221 return (0); |
|
1222 } |
|
1223 |
|
1224 if (pthread_mutex_init(kernel_fd_lock, NULL) != 0) { |
|
1225 devcrypto_free_aes_ctr_NIDs(); |
|
1226 OPENSSL_free(kernel_fd_lock); |
|
1227 kernel_fd_lock = NULL; |
|
1228 return (0); |
|
1229 } |
|
1230 } |
|
1231 |
|
1232 /* Open the /dev/crypto device */ |
|
1233 if (devcrypto_open() == 0) { |
|
1234 devcrypto_free_aes_ctr_NIDs(); |
|
1235 pthread_mutex_destroy(kernel_fd_lock); |
|
1236 OPENSSL_free(kernel_fd_lock); |
|
1237 kernel_fd_lock = NULL; |
|
1238 return (0); |
|
1239 } |
|
1240 |
|
1241 /* Get all hardware providers' information */ |
|
1242 if (devcrypto_get_slot_info() == 0) { |
|
1243 goto failed; |
|
1244 } |
|
1245 |
|
1246 if (devcrypto_get_hw_ciphers() == 0) { |
|
1247 goto failed; |
|
1248 } |
|
1249 |
|
1250 #ifdef DEBUG |
|
1251 (void) fprintf(stderr, "cipher_count = %d\n", cipher_count); |
|
1252 for (i = 0; i < cipher_count; i++) { |
|
1253 (void) fprintf(stderr, |
|
1254 "cipher_nids[i] = %d\n", cipher_nids[i]); |
|
1255 } |
|
1256 #endif /* DEBUG */ |
|
1257 |
|
1258 if (!ENGINE_set_id(e, ENGINE_DEVCRYPTO_ID) || |
|
1259 !ENGINE_set_name(e, ENGINE_DEVCRYPTO_NAME) || |
|
1260 !ENGINE_set_ciphers(e, devcrypto_get_all_ciphers) || |
|
1261 !ENGINE_set_destroy_function(e, devcrypto_destroy) || |
|
1262 !ENGINE_set_finish_function(e, devcrypto_finish)) { |
|
1263 goto failed; |
|
1264 } |
|
1265 |
|
1266 /* Set up the devcrypto error handling */ |
|
1267 ERR_load_devcrypto_strings(); |
|
1268 return (1); |
|
1269 |
|
1270 failed: |
|
1271 devcrypto_cleanup(); |
|
1272 return (0); |
|
1273 } |
|
1274 |
|
1275 |
|
1276 static int |
|
1277 bind_helper(ENGINE *e, const char *id) |
|
1278 { |
|
1279 if (id != NULL && (strcmp(id, ENGINE_DEVCRYPTO_ID) != 0)) { |
|
1280 #ifdef DEBUG |
|
1281 (void) fprintf(stderr, "libdevcrypto - bad engine id\n"); |
|
1282 #endif /* DEBUG */ |
|
1283 return (0); |
|
1284 } |
|
1285 if (!devcrypto_bind(e)) { |
|
1286 #ifdef DEBUG |
|
1287 (void) fprintf(stderr, |
|
1288 "libdevcrypto - failed to bind engine\n"); |
|
1289 #endif /* DEBUG */ |
|
1290 return (0); |
|
1291 } |
|
1292 |
|
1293 return (1); |
|
1294 } |
|
1295 |
|
1296 IMPLEMENT_DYNAMIC_CHECK_FN() |
|
1297 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper) |
|