summaryrefslogtreecommitdiff
path: root/openssl0.9.8/engines/devcrypto/e_devcrypto.c
blob: 2b64f82ad5db0469d1847bb12966295d83731245 (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

#pragma ident	"@(#)e_devcrypto.c	1.1	10/10/19 SMI"

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <cryptoutil.h>
#include <sys/crypto/ioctl.h>
#include <sys/crypto/api.h>
#include <openssl/bio.h>
#include <openssl/aes.h>
#include <openssl/engine.h>
#include <security/cryptoki.h>

#define	DEVCRYPTO_LIB_NAME "devcrypto engine"
#include "e_devcrypto_err.c"

/* DEVCRYPTO CONTEXT */
typedef struct devcrypto_ctx {
	uint_t session_id;
} devcrypto_ctx_t;

/* Index for the supported ciphers */
typedef enum {
	DEV_DES_CBC,
	DEV_DES3_CBC,
	DEV_DES_ECB,
	DEV_DES3_ECB,
	DEV_RC4,
	DEV_AES_128_CBC,
	DEV_AES_192_CBC,
	DEV_AES_256_CBC,
	DEV_AES_128_ECB,
	DEV_AES_192_ECB,
	DEV_AES_256_ECB,
	DEV_BLOWFISH_CBC,
	DEV_AES_128_CTR,
	DEV_AES_192_CTR,
	DEV_AES_256_CTR,
	DEV_CIPHER_MAX
} DEV_CIPHER_ID;

typedef struct devcrypto_cipher {
	DEV_CIPHER_ID		id;
	int			nid;
	int			iv_len;
	int			min_key_len;
	int			max_key_len;
	CK_KEY_TYPE		key_type;
	CK_MECHANISM_TYPE	mech_type;
	crypto_mech_type_t	pn_internal_number;
} devcrypto_cipher_t;


/* Constants used when creating the ENGINE */
static const char *ENGINE_DEVCRYPTO_ID = "devcrypto";
static const char *ENGINE_DEVCRYPTO_NAME = "/dev/crypto engine support";
static const char *CRYPTO_DEVICE = "/dev/crypto";

/* static variables */
static int kernel_fd = -1;
static int kernel_fd_ref = 0;
static int slot_count = 0;
static CK_SLOT_ID *kernel_provider_id = NULL;
static int cipher_count = 0;
static int *cipher_nids = NULL;
pthread_mutex_t *kernel_fd_lock;

/*
 * NIDs for AES counter mode. They will be defined during the engine
 * initialization.
 */
static int NID_aes_128_ctr = NID_undef;
static int NID_aes_192_ctr = NID_undef;
static int NID_aes_256_ctr = NID_undef;

/*
 * Cipher Table for all supported symmetric ciphers.
 */
static devcrypto_cipher_t cipher_table[] = {
	{ DEV_DES_CBC,		NID_des_cbc,		8,	 8,   8,
		CKK_DES,	CKM_DES_CBC, CRYPTO_MECH_INVALID},
	{ DEV_DES3_CBC,		NID_des_ede3_cbc,	8,	24,  24,
		CKK_DES3,	CKM_DES3_CBC, CRYPTO_MECH_INVALID},
	{ DEV_DES_ECB,		NID_des_ecb,		0,	 8,   8,
		CKK_DES,	CKM_DES_ECB, CRYPTO_MECH_INVALID},
	{ DEV_DES3_ECB,		NID_des_ede3_ecb,	0,	24,  24,
		CKK_DES3,	CKM_DES3_ECB, CRYPTO_MECH_INVALID},
	{ DEV_RC4,		NID_rc4,		0,	16, 256,
		CKK_RC4,	CKM_RC4, CRYPTO_MECH_INVALID},
	{ DEV_AES_128_CBC,	NID_aes_128_cbc,	16,	16,  16,
		CKK_AES,	CKM_AES_CBC, CRYPTO_MECH_INVALID},
	{ DEV_AES_192_CBC,	NID_aes_192_cbc,	16,	24,  24,
		CKK_AES,	CKM_AES_CBC, CRYPTO_MECH_INVALID},
	{ DEV_AES_256_CBC,	NID_aes_256_cbc,	16,	32,  32,
		CKK_AES,	CKM_AES_CBC, CRYPTO_MECH_INVALID},
	{ DEV_AES_128_ECB,	NID_aes_128_ecb,	0,	16,  16,
		CKK_AES,	CKM_AES_ECB, CRYPTO_MECH_INVALID},
	{ DEV_AES_192_ECB,	NID_aes_192_ecb,	0,	24,  24,
		CKK_AES,	CKM_AES_ECB, CRYPTO_MECH_INVALID},
	{ DEV_AES_256_ECB,	NID_aes_256_ecb,	0,	32,  32,
		CKK_AES,	CKM_AES_ECB, CRYPTO_MECH_INVALID},
	{ DEV_BLOWFISH_CBC,	NID_bf_cbc,		8,	16,  16,
		CKK_BLOWFISH,	CKM_BLOWFISH_CBC, CRYPTO_MECH_INVALID},
	/*
	 * For the following 3 AES counter mode entries, we don't know the
	 * NIDs until the engine is initialized
	 */
	{ DEV_AES_128_CTR,	NID_undef,		16,	16,  16,
		CKK_AES,	CKM_AES_CTR, CRYPTO_MECH_INVALID},
	{ DEV_AES_192_CTR,	NID_undef,		16,	24,  24,
		CKK_AES,	CKM_AES_CTR, CRYPTO_MECH_INVALID},
	{ DEV_AES_256_CTR,	NID_undef,		16,	32,  32,
		CKK_AES,	CKM_AES_CTR, CRYPTO_MECH_INVALID},
	};


/* Formal declaration for functions in EVP_CIPHER structure */
static int devcrypto_cipher_init(EVP_CIPHER_CTX *, const unsigned char *,
    const unsigned char *, int);
static int devcrypto_cipher_do_cipher(EVP_CIPHER_CTX *, unsigned char *,
    const unsigned char *, unsigned int);
static int devcrypto_cipher_cleanup(EVP_CIPHER_CTX *);

/* OpenSSL's libcrypto EVP stuff. This is how this engine gets wired to EVP. */
static const EVP_CIPHER dev_des_cbc = {
	NID_des_cbc,
	8, 8, 8,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

static const EVP_CIPHER dev_3des_cbc = {
	NID_des_ede3_cbc,
	8, 24, 8,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

/*
 * ECB modes don't use an Initial Vector, therefore set_asn1_parameters and
 * get_asn1_parameters fields are set to NULL.
 */
static const EVP_CIPHER dev_des_ecb = {
	NID_des_ecb,
	8, 8, 8,
	EVP_CIPH_ECB_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_3des_ecb = {
	NID_des_ede3_ecb,
	8, 24, 8,
	EVP_CIPH_ECB_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_rc4 = {
	NID_rc4,
	1, 16, 0,
	EVP_CIPH_VARIABLE_LENGTH,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_aes_128_cbc = {
	NID_aes_128_cbc,
	16, 16, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

static const EVP_CIPHER dev_aes_192_cbc = {
	NID_aes_192_cbc,
	16, 24, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

static const EVP_CIPHER dev_aes_256_cbc = {
	NID_aes_256_cbc,
	16, 32, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};


/*
 * ECB modes don't use IV, therefore set_asn1_parameters and
 * get_asn1_parameters are set to NULL.
 */
static const EVP_CIPHER dev_aes_128_ecb = {
	NID_aes_128_ecb,
	16, 16, 0,
	EVP_CIPH_ECB_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_aes_192_ecb = {
	NID_aes_192_ecb,
	16, 24, 0,
	EVP_CIPH_ECB_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_aes_256_ecb = {
	NID_aes_256_ecb,
	16, 32, 0,
	EVP_CIPH_ECB_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	NULL,
	NULL,
	NULL
};

static const EVP_CIPHER dev_bf_cbc = {
	NID_bf_cbc,
	8, 16, 8,
	EVP_CIPH_VARIABLE_LENGTH,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};


/*
 * NID_undef's will be changed for AES counter mode, as soon they are created.
 */
static EVP_CIPHER dev_aes_128_ctr = {
	NID_undef,
	16, 16, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

static EVP_CIPHER dev_aes_192_ctr = {
	NID_undef,
	16, 24, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};

static EVP_CIPHER dev_aes_256_ctr = {
	NID_undef,
	16, 32, 16,
	EVP_CIPH_CBC_MODE,
	devcrypto_cipher_init,
	devcrypto_cipher_do_cipher,
	devcrypto_cipher_cleanup,
	sizeof (devcrypto_ctx_t),
	EVP_CIPHER_set_asn1_iv,
	EVP_CIPHER_get_asn1_iv,
	NULL
};



/*
 * This function creates a new NID.
 */
static int
devcrypto_add_NID(char *sn, char *ln)
{
	ASN1_OBJECT *o;
	int nid;

	if ((o = ASN1_OBJECT_create(OBJ_new_nid(1), (unsigned char *)"",
	    1, sn, ln)) == NULL) {
		return (0);
	}

	nid = OBJ_add_object(o); /* will return NID_undef on error */
	ASN1_OBJECT_free(o);
	return (nid);
}


/*
 * This function creates new NIDs for AES counter mode algorithms.
 * Note that OpenSSL doesn't support them now so we have to help
 * ourselves here.
 */
static int
devcrypto_add_aes_ctr_NIDs(void)
{
	if (NID_aes_256_ctr != NID_undef) /* already set */
		return (1);

	NID_aes_128_ctr = devcrypto_add_NID("AES-128-CTR", "aes-128-ctr");
	if (NID_aes_128_ctr == NID_undef)
		goto failed;
	cipher_table[DEV_AES_128_CTR].nid =
	    dev_aes_128_ctr.nid = NID_aes_128_ctr;

	NID_aes_192_ctr = devcrypto_add_NID("AES-192-CTR", "aes-192-ctr");
	if (NID_aes_192_ctr == NID_undef)
		goto failed;
	cipher_table[DEV_AES_192_CTR].nid =
	    dev_aes_192_ctr.nid = NID_aes_192_ctr;

	NID_aes_256_ctr = devcrypto_add_NID("AES-256-CTR", "aes-256-ctr");
	if (NID_aes_256_ctr == NID_undef)
		goto failed;
	cipher_table[DEV_AES_256_CTR].nid =
	    dev_aes_256_ctr.nid = NID_aes_256_ctr;

	return (1);

failed:
	return (0);
}


static void
devcrypto_free_aes_ctr_NIDs(void)
{
	ASN1_OBJECT *ob = NULL;

	if (NID_aes_128_ctr != NID_undef) {
		ob = OBJ_nid2obj(NID_aes_128_ctr);
		if (ob != NULL)
			ASN1_OBJECT_free(ob);
	}

	if (NID_aes_192_ctr != NID_undef) {
		ob = OBJ_nid2obj(NID_aes_192_ctr);
		if (ob != NULL)
			ASN1_OBJECT_free(ob);
	}

	if (NID_aes_256_ctr != NID_undef) {
		ob = OBJ_nid2obj(NID_aes_256_ctr);
		if (ob != NULL)
		ASN1_OBJECT_free(ob);
	}
}

/*
 * Open the /dev/crypto device
 */
static int
devcrypto_open()
{
	int fd = -1;

	if (kernel_fd != -1) {  /* already open */
		(void) pthread_mutex_lock(kernel_fd_lock);
		kernel_fd_ref++;
		(void) pthread_mutex_unlock(kernel_fd_lock);
		return (1);
	}

	(void) pthread_mutex_lock(kernel_fd_lock);
	fd = open(CRYPTO_DEVICE, O_RDWR);
	if (fd == -1) {
#ifdef DEBUG
		(void) fprintf(stderr,
		    "libdevcrypto: open /dev/crypto failed, errno=%x\n",
		    errno);
#endif
		(void) pthread_mutex_unlock(kernel_fd_lock);
		return (0);
	}

	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto: failed to fcntl\n");
#endif
		(void) close(fd);
		(void) pthread_mutex_unlock(kernel_fd_lock);
		return (0);
	}

	kernel_fd = fd;
	kernel_fd_ref++;
	(void) pthread_mutex_unlock(kernel_fd_lock);
	return (1);
}


/*
 * This function gets the total number of hardware providers presented in
 * the system first.  If there is any hardware providers, then it will get
 * the kernel provider id for each hardware slot also.
 */
static int
devcrypto_get_slot_info()
{
	crypto_get_provider_list_t *pl = NULL;
	int ret = 1;
	int r;
	int i;

	/* Already have the information */
	if (kernel_provider_id != NULL)
		return (1);

	/* Find out how many hardware slots are presented. */
	pl = OPENSSL_malloc(sizeof (crypto_get_provider_list_t));
	if (pl == NULL)
		return (0);

	pl->pl_count = 0;
	while ((r = ioctl(kernel_fd, CRYPTO_GET_PROVIDER_LIST, pl)) < 0) {
		if (errno != EINTR)
			break;
	}
	if (r < 0 || pl->pl_return_value != CRYPTO_SUCCESS) {
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto:CRYPTO_GET_PROVIDER_LIST:"
		    "ret (r) = 0x%x, (rv) = 0x%x\n", r,  pl->pl_return_value);
#endif /* DEBUG */
		ret = 0;
		goto out;
	}

	slot_count = pl->pl_count;
	if (slot_count == 0) {
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto: no hw providers\n");
#endif /* DEBUG */
		ret = 0;
		goto out;
	}

	/* Get the provider ID for each slot from kernel and save it */
	kernel_provider_id = OPENSSL_malloc(sizeof (CK_SLOT_ID) * slot_count);
	if (kernel_provider_id == NULL) {
		ret = 0;
		goto out;
	}

	(void) OPENSSL_free(pl);
	pl = OPENSSL_malloc(slot_count * sizeof (crypto_get_provider_list_t));
	if (pl == NULL) {
		ret = 0;
		goto out;
	}

	pl->pl_count = slot_count;
	while ((r = ioctl(kernel_fd, CRYPTO_GET_PROVIDER_LIST, pl)) < 0) {
		if (errno != EINTR)
			break;
	}
	if (r < 0 || (pl->pl_return_value != CRYPTO_SUCCESS)) {
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto:CRYPTO_GET_PROVIDER_LIST:"
		    "ret (r) = 0x%x, (rv) = 0x%x\n", r,  pl->pl_return_value);
#endif /* DEBUG */
		ret = 0;
		goto out;
	}

	for (i = 0; i < slot_count; i++) {
		kernel_provider_id[i] = pl->pl_list[i].pe_provider_id;
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto: i = %d, "
		    "kernel_provider_id = %d\n", i, kernel_provider_id[i]);
#endif /* DEBUG */
	}

out:
	if (pl != NULL)
		(void) OPENSSL_free(pl);

	if (ret == 0 && kernel_provider_id != NULL) {
		(void) OPENSSL_free(kernel_provider_id);
		kernel_provider_id = NULL;
	}

	return (ret);
}


/*
 * This function checks if the "nid" is already in the nid list.
 */
static int
nid_in_list(int nid, int *nid_list, int count)
{
	int i = 0;

	if (nid_list == NULL || count <= 0)
		return (0);

	while (i < count) {
		if (nid == nid_list[i])
			break;
		i++;
	}
	return (i < count ? 1 : 0);
}

/*
 * This function is to get all the ciphers supported by hardware providers.
 * If this function is successfully completed, then the following 2 global
 * variables will be set.
 * cipher_count - the number of ciphers found in all hardware providers.
 * cipher_nids - the nid list for all the ciphers.
 */
static int
devcrypto_get_hw_ciphers(void)
{
	crypto_get_provider_mechanism_info_t mechinfo;
	int max_cipher_count;
	int *tmp_nids = NULL;
	const char *mech_string;
	int r;
	int i, j;

	if (slot_count <= 0)  /* no hardware provider */
		return (0);

	max_cipher_count = slot_count * DEV_CIPHER_MAX + 1;
	tmp_nids = OPENSSL_malloc(max_cipher_count * sizeof (int));
	if (tmp_nids == NULL) {
		/* not enough memory */
		goto failed;
	}

	for (i = 0; i < slot_count; i++) {
		mechinfo.mi_provider_id = kernel_provider_id[i];
		for (j = 0; j < DEV_CIPHER_MAX; j++) {
			mech_string =
			    pkcs11_mech2str(cipher_table[j].mech_type);
			if (mech_string == NULL) {
				continue; /* shouldn't happen; skip it */
			}

			(void) strlcpy(mechinfo.mi_mechanism_name,
			    mech_string, CRYPTO_MAX_MECH_NAME);
			while ((r = ioctl(kernel_fd,
			    CRYPTO_GET_PROVIDER_MECHANISM_INFO,
			    &mechinfo)) < 0) {
				if (errno != EINTR)
					break;
			}
			if (r < 0) {
				goto failed;
			}

			if (mechinfo.mi_return_value == CRYPTO_SUCCESS) {
				/*
				 * Found this mechanism in hardware providers.
				 * If it is not in the nid list yet, add it.
				 */
				if (!nid_in_list(cipher_table[j].nid,
				    tmp_nids, cipher_count)) {
					tmp_nids[cipher_count] =
					    cipher_table[j].nid;
					cipher_count++;
				}
			}
		}
	}

	if (cipher_count > 0) {
		cipher_nids = tmp_nids;
	}

	return (1);

failed:
	if (r < 0 || cipher_count == 0) {
		if (tmp_nids != NULL)
			OPENSSL_free(tmp_nids);
	}
	return (0);
}

/*
 * Registered by the ENGINE when used to find out how to deal with
 * a particular NID in the ENGINE. This says what we'll do at the
 * top level - note, that list is restricted by what we answer with.
 */
static int
devcrypto_get_all_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
    const int **nids, int nid)
{
	if (!cipher) {
		*nids = (cipher_count > 0) ? cipher_nids : NULL;
		return (cipher_count);
	}

	switch (nid) {
	case NID_des_cbc:
	*cipher = &dev_des_cbc;
		break;
	case NID_des_ede3_cbc:
		*cipher = &dev_3des_cbc;
		break;
	case NID_des_ecb:
		*cipher = &dev_des_ecb;
		break;
	case NID_des_ede3_ecb:
		*cipher = &dev_3des_ecb;
		break;
	case NID_rc4:
		*cipher = &dev_rc4;
		break;
	case NID_aes_128_cbc:
		*cipher = &dev_aes_128_cbc;
		break;
	case NID_aes_192_cbc:
		*cipher = &dev_aes_192_cbc;
		break;
	case NID_aes_256_cbc:
		*cipher = &dev_aes_256_cbc;
		break;
	case NID_aes_128_ecb:
		*cipher = &dev_aes_128_ecb;
		break;
	case NID_aes_192_ecb:
		*cipher = &dev_aes_192_ecb;
		break;
	case NID_aes_256_ecb:
		*cipher = &dev_aes_256_ecb;
		break;
	case NID_bf_cbc:
		*cipher = &dev_bf_cbc;
		break;
	default:
		/*
		 * We cannot put the NIDs for AES counter mode in separated
		 * cases as above because they are not constants.
		 */
		if (nid == NID_aes_128_ctr)
			*cipher = &dev_aes_128_ctr;
		else if (nid == NID_aes_192_ctr)
			*cipher = &dev_aes_192_ctr;
		else if (nid == NID_aes_256_ctr)
			*cipher = &dev_aes_256_ctr;
		else
			*cipher = NULL;
		break;
	}

	return (*cipher != NULL);
}


static int
get_cipher_id_by_nid(int nid)
{
	int i;

	for (i = 0; i < DEV_CIPHER_MAX; i++)
		if (cipher_table[i].nid == nid)
			return (cipher_table[i].id);
	return (-1);
}


static int
get_slotid_by_mechanism(const char *mech_string, CK_SLOT_ID *slot_id)
{
	crypto_get_provider_mechanism_info_t mechanism_info;
	uint_t rv;
	int r;
	int i = 0;

	(void) strlcpy(mechanism_info.mi_mechanism_name, mech_string,
	    CRYPTO_MAX_MECH_NAME);
	while (i < slot_count) {
		mechanism_info.mi_provider_id = kernel_provider_id[i];
		while ((r = ioctl(kernel_fd,
		    CRYPTO_GET_PROVIDER_MECHANISM_INFO,
		    &mechanism_info)) < 0) {
			if (errno != EINTR)
				break;
		}
		if (r < 0) {
			return (0); /* ioctl function failed */
		}
		rv = mechanism_info.mi_return_value;
		if (rv == 0) { /* found it */
			*slot_id = kernel_provider_id[i];
			return (1);
		}
		i++;
	}

	return (0);
}


static int
devcrypto_cipher_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
    const unsigned char *iv, int enc)
{
	devcrypto_ctx_t *devc_ctx = ctx->cipher_data;
	crypto_encrypt_init_t encrypt_init;
	crypto_decrypt_init_t decrypt_init;
	crypto_open_session_t session;
	crypto_get_mechanism_number_t get_number;
	CK_AES_CTR_PARAMS aes_ctr_params;
	devcrypto_cipher_t *the_cipher;
	const char *mech_string;
	CK_SLOT_ID slot_id;
	int index;
	int r;
	uint_t rv = 0;


	if (key == NULL) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_CIPHER_KEY);
		return (0);
	}

	/* get the cipher entry index in cipher_table from nid */
	index = get_cipher_id_by_nid(ctx->cipher->nid);
	if (index < 0 || index >= DEV_CIPHER_MAX) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_CIPHER_NID);
		return (0);
	}
	the_cipher = &cipher_table[index];

	/* check key size */
	if (ctx->cipher->iv_len < the_cipher->iv_len ||
	    ctx->key_len < the_cipher->min_key_len ||
	    ctx->key_len > the_cipher->max_key_len) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_KEY_OR_IV_LEN_PROBLEM);
		return (0);
	}

	/* get the mechanism string */
	mech_string = pkcs11_mech2str(the_cipher->mech_type);
	if (mech_string == NULL) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_MECH_STRING);
		return (0);
	}

#ifdef DEBUG
	(void) fprintf(stderr, "libdevcrypto: mech_string=%s\n", mech_string);
#endif

	/* Find the slot that supports this mechanism */
	if (!get_slotid_by_mechanism(mech_string, &slot_id)) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_FIND_SLOT_BY_MECH);
#ifdef DEBUG
		(void) fprintf(stderr,
		    "libdevcrypto: failed to find a slot with %s\n",
		    mech_string);
#endif
		return (0);
	}

#ifdef DEBUG
	(void) fprintf(stderr, "libdevcrypto: found a slot with %s, "
	    "slot_id = %d\n", mech_string, slot_id);
#endif

	/* Open a session on this slot */
	session.os_provider_id = slot_id;
	session.os_flags = CKF_RW_SESSION | CKF_SERIAL_SESSION;
	while ((r = ioctl(kernel_fd, CRYPTO_OPEN_SESSION, &session)) < 0) {
		if (errno != EINTR)
			break;
	}
	rv = session.os_return_value;
	if (r || rv) {
		DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_OPEN_SESSION);
#ifdef DEBUG
		(void) fprintf(stderr,
		    "libdevcrypto:cipher_init:failed to open a session\n");
#endif /* DEBUG */
		goto failed;
	}

#ifdef DEBUG
	(void) fprintf(stderr, "libdevcrypto:cipher_init: open session = %d\n",
	    session.os_session);
#endif /* DEBUG */

	/* save the session_id */
	devc_ctx->session_id = session.os_session;

	/*
	 * Get the kernel mechanism number for this mechanism, if it has not
	 * been retrieved yet.
	 */
	if (the_cipher->pn_internal_number == CRYPTO_MECH_INVALID) {
		get_number.pn_mechanism_string = (char *)mech_string;
		get_number.pn_mechanism_len = strlen(mech_string) + 1;
		while ((r = ioctl(kernel_fd, CRYPTO_GET_MECHANISM_NUMBER,
		    &get_number)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = get_number.pn_return_value;
		if (r || rv) {
			DEVCRYPTOerr(DEVC_F_CIPHER_INIT,
			    DEVC_R_GET_MECHANISM_NUMBER);
#ifdef DEBUG
			(void) fprintf(stderr, "libdevcrypto:cipher_init: "
			    "failed to get the kernel mech number.\n");
#endif /* DEBUG */
			goto failed;
		}

		the_cipher->pn_internal_number = get_number.pn_internal_number;
	}

	/* Crypto Init */
	if (ctx->encrypt) {
		encrypt_init.ei_session = session.os_session;
		encrypt_init.ei_key.ck_format = CRYPTO_KEY_RAW;
		encrypt_init.ei_key.ck_obj_id = 0;
		encrypt_init.ei_key.ck_data = (void *) key;
		encrypt_init.ei_key.ck_length = ctx->key_len * 8;
		encrypt_init.ei_mech.cm_type = the_cipher->pn_internal_number;

		if (ctx->cipher->nid == NID_aes_128_ctr ||
		    ctx->cipher->nid == NID_aes_192_ctr ||
		    ctx->cipher->nid == NID_aes_256_ctr) {
			encrypt_init.ei_mech.cm_param =
			    (void *) (&aes_ctr_params);
			encrypt_init.ei_mech.cm_param_len =
			    sizeof (aes_ctr_params);

			aes_ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8;
			OPENSSL_assert(ctx->cipher->iv_len == AES_BLOCK_SIZE);
			(void) memcpy(aes_ctr_params.cb, ctx->iv,
			    AES_BLOCK_SIZE);
		} else {
			if (the_cipher->iv_len > 0) {
				encrypt_init.ei_mech.cm_param =
				    (char *)ctx->iv;
				encrypt_init.ei_mech.cm_param_len =
				    ctx->cipher->iv_len;
			} else {
				encrypt_init.ei_mech.cm_param = NULL;
				encrypt_init.ei_mech.cm_param_len = 0;
			}
		}

		while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_INIT,
		    &encrypt_init)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = encrypt_init.ei_return_value;

	} else {
		decrypt_init.di_session = session.os_session;
		decrypt_init.di_key.ck_format = CRYPTO_KEY_RAW;
		decrypt_init.di_key.ck_obj_id = 0;
		decrypt_init.di_key.ck_data = (void *) key;
		decrypt_init.di_key.ck_length = ctx->key_len * 8;
		decrypt_init.di_mech.cm_type = the_cipher->pn_internal_number;

		if (ctx->cipher->nid == NID_aes_128_ctr ||
		    ctx->cipher->nid == NID_aes_192_ctr ||
		    ctx->cipher->nid == NID_aes_256_ctr) {
			decrypt_init.di_mech.cm_param =
			    (void *)(&aes_ctr_params);
			decrypt_init.di_mech.cm_param_len =
			    sizeof (aes_ctr_params);
			aes_ctr_params.ulCounterBits = AES_BLOCK_SIZE * 8;
			OPENSSL_assert(ctx->cipher->iv_len == AES_BLOCK_SIZE);
			(void) memcpy(aes_ctr_params.cb, ctx->iv,
			    AES_BLOCK_SIZE);
		} else {
			if (the_cipher->iv_len > 0) {
				decrypt_init.di_mech.cm_param =
				    (char *)ctx->iv;
				decrypt_init.di_mech.cm_param_len =
				    ctx->cipher->iv_len;
			} else {
				decrypt_init.di_mech.cm_param = NULL;
				decrypt_init.di_mech.cm_param_len = 0;
			}
		}

		while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_INIT,
		    &decrypt_init)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = decrypt_init.di_return_value;
	}

failed:
	if (r || rv) {
		if (ctx->encrypt)
			DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_ENCRYPT_INIT);
		else
			DEVCRYPTOerr(DEVC_F_CIPHER_INIT, DEVC_R_DECRYPT_INIT);

		return (0);
	}

	return (1);
}


/*
 * ENCRYPT_UPDATE or DECRYPT_UPDATE
 */
static int
devcrypto_cipher_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
    const unsigned char *in, unsigned int inl)
{
	crypto_encrypt_update_t encrypt_update;
	crypto_decrypt_update_t decrypt_update;
	devcrypto_ctx_t *devc_ctx = ctx->cipher_data;
	int r = 0, rv = 0;

	if (ctx->encrypt) {
		encrypt_update.eu_session = devc_ctx->session_id;
		encrypt_update.eu_databuf = (char *)in;
		encrypt_update.eu_datalen = inl;
		encrypt_update.eu_encrbuf = (char *)out;
		encrypt_update.eu_encrlen = inl;

		while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_UPDATE,
		    &encrypt_update)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = encrypt_update.eu_return_value;

	} else { /* decrypt */
		decrypt_update.du_session = devc_ctx->session_id;
		decrypt_update.du_encrbuf = (char *)in;
		decrypt_update.du_encrlen = inl;
		decrypt_update.du_databuf = (char *)out;
		decrypt_update.du_datalen = inl;

		while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_UPDATE,
		    &decrypt_update)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = decrypt_update.du_return_value;
	}

	if (r || rv) {
		if (ctx->encrypt)
			DEVCRYPTOerr(DEVC_F_CIPHER_DO_CIPHER,
			    DEVC_R_ENCRYPT_UPDATE);
		else
			DEVCRYPTOerr(DEVC_F_CIPHER_DO_CIPHER,
			    DEVC_R_DECRYPT_UPDATE);

#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto:crypto_do ret (r) = 0x%x,"
		    "crypto ret (rv) = 0x%x,", r, rv);
#endif /* DEBUG */
		return (0);
	}

	return (1);
}


/*
 * ENCRYPT_FINAL or DECRYPT_FINAL
 */
static int
devcrypto_cipher_cleanup(EVP_CIPHER_CTX *ctx)
{
	crypto_encrypt_final_t encrypt_final;
	crypto_decrypt_final_t decrypt_final;
	crypto_close_session_t session;
	devcrypto_ctx_t *devc_ctx = ctx->cipher_data;
	char buf[EVP_MAX_BLOCK_LENGTH];
	int r;
	uint_t rv = 0;
	int ret = 1;

	if (ctx->encrypt) {
		encrypt_final.ef_session = devc_ctx->session_id;
		encrypt_final.ef_encrbuf = buf;
		encrypt_final.ef_encrlen = sizeof (buf);
		while ((r = ioctl(kernel_fd, CRYPTO_ENCRYPT_FINAL,
		    &encrypt_final)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = encrypt_final.ef_return_value;

	} else {
		decrypt_final.df_session = devc_ctx->session_id;
		decrypt_final.df_databuf = buf;
		decrypt_final.df_datalen = sizeof (buf);
		while ((r = ioctl(kernel_fd, CRYPTO_DECRYPT_FINAL,
		    &decrypt_final)) < 0) {
			if (errno != EINTR)
				break;
		}
		rv = decrypt_final.df_return_value;
	}

#ifdef DEBUG
	if (ctx->encrypt)
		(void) fprintf(stderr, "libdevcrypto:CRYPTO_ENCRYPT_FINAL "
		    "ret (r) = 0x%x, (rv) = 0x%x\n", r, rv);
	else
		(void) fprintf(stderr, "libdevcrypto:CRYPTO_DECRYPT_FINAL "
		    "ret (r) = 0x%x, (rv) = 0x%x\n", r, rv);
#endif /* DEBUG */

	if (r || rv) {
		if (ctx->encrypt)
			DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP,
			    DEVC_R_ENCRYPT_FINAL);
		else
			DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP,
			    DEVC_R_DECRYPT_FINAL);
		ret = 0;
	}

	/* close the session */
	session.cs_session = devc_ctx->session_id;
	while ((r = ioctl(kernel_fd, CRYPTO_CLOSE_SESSION, &session)) < 0) {
		if (errno != EINTR)
			break;
	}

#ifdef DEBUG
	(void) fprintf(stderr, "libdevcrypto:CRYPTO_CLOSE_SESSION, "
	    "session id = %d ret (r) = 0x%x, crypto ret (rv) = 0x%x\n",
	    devc_ctx->session_id, r, rv);
#endif /* DEBUG */

	if (r || rv) {
		DEVCRYPTOerr(DEVC_F_CIPHER_CLEANUP, DEVC_R_CLOSE_SESSION);
		ret = 0;
	}

	return (ret);
}

static void
devcrypto_cleanup()
{
	if (kernel_fd == -1)
		return;

	(void) pthread_mutex_lock(kernel_fd_lock);
	kernel_fd_ref--;
	(void) pthread_mutex_unlock(kernel_fd_lock);

	if (kernel_fd_ref == 0) {
		(void) pthread_mutex_lock(kernel_fd_lock);
		(void) close(kernel_fd);
		kernel_fd = -1;
		if (kernel_provider_id != NULL) {
			OPENSSL_free(kernel_provider_id);
			kernel_provider_id = NULL;
		}
		if (cipher_nids != NULL) {
			OPENSSL_free(cipher_nids);
			cipher_nids = NULL;
		}
		devcrypto_free_aes_ctr_NIDs();
		(void) pthread_mutex_unlock(kernel_fd_lock);
		(void) pthread_mutex_destroy(kernel_fd_lock);
		OPENSSL_free(kernel_fd_lock);
		kernel_fd_lock = NULL;
	}
}

static int
devcrypto_destroy(ENGINE *e)
{
	ERR_unload_devcrypto_strings();
	return (1);
}

static int
devcrypto_finish(ENGINE *e)
{
	devcrypto_cleanup();
	return (1);
}

/*
 * Set up the engine info and get the /dev/crypto engine ready.
 */
static int
devcrypto_bind(ENGINE *e)
{
#ifdef DEBUG
	int i;
#endif

	/* Get the NIDs for AES counter mode algorithms first. */
	if (devcrypto_add_aes_ctr_NIDs() == 0) {
		return (0);
	}

	/* Create a lock for the devcrypto device file descriptor */
	if (kernel_fd_lock == NULL) {
		kernel_fd_lock = OPENSSL_malloc(sizeof (pthread_mutex_t));
		if (kernel_fd_lock == NULL) {
			devcrypto_free_aes_ctr_NIDs();
			return (0);
		}

		if (pthread_mutex_init(kernel_fd_lock, NULL) != 0) {
			devcrypto_free_aes_ctr_NIDs();
			OPENSSL_free(kernel_fd_lock);
			kernel_fd_lock = NULL;
			return (0);
		}
	}

	/* Open the /dev/crypto device */
	if (devcrypto_open() == 0) {
		devcrypto_free_aes_ctr_NIDs();
		pthread_mutex_destroy(kernel_fd_lock);
		OPENSSL_free(kernel_fd_lock);
		kernel_fd_lock = NULL;
		return (0);
	}

	/* Get all hardware providers' information */
	if (devcrypto_get_slot_info() == 0) {
		goto failed;
	}

	if (devcrypto_get_hw_ciphers() == 0) {
		goto failed;
	}

#ifdef DEBUG
	(void) fprintf(stderr, "cipher_count = %d\n", cipher_count);
	for (i = 0; i < cipher_count; i++) {
		(void) fprintf(stderr,
		    "cipher_nids[i] = %d\n", cipher_nids[i]);
	}
#endif /* DEBUG */

	if (!ENGINE_set_id(e, ENGINE_DEVCRYPTO_ID) ||
	    !ENGINE_set_name(e, ENGINE_DEVCRYPTO_NAME) ||
	    !ENGINE_set_ciphers(e, devcrypto_get_all_ciphers) ||
	    !ENGINE_set_destroy_function(e, devcrypto_destroy) ||
	    !ENGINE_set_finish_function(e, devcrypto_finish)) {
		goto failed;
	}

	/* Set up the devcrypto error handling */
	ERR_load_devcrypto_strings();
	return (1);

failed:
	devcrypto_cleanup();
	return (0);
}


static int
bind_helper(ENGINE *e, const char *id)
{
	if (id != NULL && (strcmp(id, ENGINE_DEVCRYPTO_ID) != 0)) {
#ifdef DEBUG
		(void) fprintf(stderr, "libdevcrypto - bad engine id\n");
#endif /* DEBUG */
		return (0);
	}
	if (!devcrypto_bind(e)) {
#ifdef DEBUG
		(void) fprintf(stderr,
		    "libdevcrypto - failed to bind engine\n");
#endif /* DEBUG */
		return (0);
	}

	return (1);
}

IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)