xref: /illumos-gate/usr/src/lib/libcryptoutil/common/mechstr.c (revision 2bbdd445a21f9d61f4a0ca0faf05d5ceb2bd91f3)
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 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Convert Algorithm names as strings to PKCS#11 Mech numbers and vice versa.
28  */
29 
30 #include <limits.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <security/cryptoki.h>
35 #include <security/pkcs11t.h>
36 
37 #include <cryptoutil.h>
38 
39 /*
40  * This table is a one-to-one mapping between mechanism names and numbers.
41  * As such, it should not contain deprecated mechanism names (aliases).
42  */
43 typedef struct {
44 	const char		*str;
45 	CK_MECHANISM_TYPE	mech;
46 } pkcs11_mapping_t;
47 
48 /*
49  * Note: elements in this table MUST be in numeric order,
50  * since bsearch(3C) is used to search this table.
51  */
52 static const pkcs11_mapping_t mapping[] = {
53 	{ "CKM_RSA_PKCS_KEY_PAIR_GEN", CKM_RSA_PKCS_KEY_PAIR_GEN },
54 	{ "CKM_RSA_PKCS", CKM_RSA_PKCS },
55 	{ "CKM_RSA_9796", CKM_RSA_9796 },
56 	{ "CKM_RSA_X_509", CKM_RSA_X_509 },
57 	{ "CKM_MD2_RSA_PKCS", CKM_MD2_RSA_PKCS },
58 	{ "CKM_MD5_RSA_PKCS", CKM_MD5_RSA_PKCS },
59 	{ "CKM_SHA1_RSA_PKCS", CKM_SHA1_RSA_PKCS },
60 	{ "CKM_RIPEMD128_RSA_PKCS", CKM_RIPEMD128_RSA_PKCS },
61 	{ "CKM_RIPEMD160_RSA_PKCS", CKM_RIPEMD160_RSA_PKCS },
62 	{ "CKM_RSA_PKCS_OAEP", CKM_RSA_PKCS_OAEP },
63 	{ "CKM_RSA_X9_31_KEY_PAIR_GEN", CKM_RSA_X9_31_KEY_PAIR_GEN },
64 	{ "CKM_RSA_X9_31", CKM_RSA_X9_31 },
65 	{ "CKM_SHA1_RSA_X9_31", CKM_SHA1_RSA_X9_31 },
66 	{ "CKM_RSA_PKCS_PSS", CKM_RSA_PKCS_PSS },
67 	{ "CKM_SHA1_RSA_PKCS_PSS", CKM_SHA1_RSA_PKCS_PSS },
68 	{ "CKM_DSA_KEY_PAIR_GEN", CKM_DSA_KEY_PAIR_GEN },
69 	{ "CKM_DSA", CKM_DSA },
70 	{ "CKM_DSA_SHA1", CKM_DSA_SHA1 },
71 	{ "CKM_DH_PKCS_KEY_PAIR_GEN", CKM_DH_PKCS_KEY_PAIR_GEN },
72 	{ "CKM_DH_PKCS_DERIVE", CKM_DH_PKCS_DERIVE },
73 	{ "CKM_X9_42_DH_KEY_PAIR_GEN", CKM_X9_42_DH_KEY_PAIR_GEN },
74 	{ "CKM_X9_42_DH_DERIVE", CKM_X9_42_DH_DERIVE },
75 	{ "CKM_X9_42_DH_HYBRID_DERIVE", CKM_X9_42_DH_HYBRID_DERIVE },
76 	{ "CKM_X9_42_MQV_DERIVE", CKM_X9_42_MQV_DERIVE },
77 	{ "CKM_SHA256_RSA_PKCS", CKM_SHA256_RSA_PKCS },
78 	{ "CKM_SHA384_RSA_PKCS", CKM_SHA384_RSA_PKCS },
79 	{ "CKM_SHA512_RSA_PKCS", CKM_SHA512_RSA_PKCS },
80 	{ "CKM_SHA256_RSA_PKCS_PSS", CKM_SHA256_RSA_PKCS_PSS },
81 	{ "CKM_SHA384_RSA_PKCS_PSS", CKM_SHA384_RSA_PKCS_PSS },
82 	{ "CKM_SHA512_RSA_PKCS_PSS", CKM_SHA512_RSA_PKCS_PSS },
83 	{ "CKM_SHA224_RSA_PKCS", CKM_SHA224_RSA_PKCS },
84 	{ "CKM_SHA224_RSA_PKCS_PSS", CKM_SHA224_RSA_PKCS_PSS },
85 	{ "CKM_RC2_KEY_GEN", CKM_RC2_KEY_GEN },
86 	{ "CKM_RC2_ECB", CKM_RC2_ECB },
87 	{ "CKM_RC2_CBC", CKM_RC2_CBC },
88 	{ "CKM_RC2_MAC", CKM_RC2_MAC },
89 	{ "CKM_RC2_MAC_GENERAL", CKM_RC2_MAC_GENERAL },
90 	{ "CKM_RC2_CBC_PAD", CKM_RC2_CBC_PAD },
91 	{ "CKM_RC4_KEY_GEN", CKM_RC4_KEY_GEN },
92 	{ "CKM_RC4", CKM_RC4 },
93 	{ "CKM_DES_KEY_GEN", CKM_DES_KEY_GEN },
94 	{ "CKM_DES_ECB", CKM_DES_ECB },
95 	{ "CKM_DES_CBC", CKM_DES_CBC },
96 	{ "CKM_DES_MAC", CKM_DES_MAC },
97 	{ "CKM_DES_MAC_GENERAL", CKM_DES_MAC_GENERAL },
98 	{ "CKM_DES_CBC_PAD", CKM_DES_CBC_PAD },
99 	{ "CKM_DES2_KEY_GEN", CKM_DES2_KEY_GEN },
100 	{ "CKM_DES3_KEY_GEN", CKM_DES3_KEY_GEN },
101 	{ "CKM_DES3_ECB", CKM_DES3_ECB },
102 	{ "CKM_DES3_CBC", CKM_DES3_CBC },
103 	{ "CKM_DES3_MAC", CKM_DES3_MAC },
104 	{ "CKM_DES3_MAC_GENERAL", CKM_DES3_MAC_GENERAL },
105 	{ "CKM_DES3_CBC_PAD", CKM_DES3_CBC_PAD },
106 	{ "CKM_CDMF_KEY_GEN", CKM_CDMF_KEY_GEN },
107 	{ "CKM_CDMF_ECB", CKM_CDMF_ECB },
108 	{ "CKM_CDMF_CBC", CKM_CDMF_CBC },
109 	{ "CKM_CDMF_MAC", CKM_CDMF_MAC },
110 	{ "CKM_CDMF_MAC_GENERAL", CKM_CDMF_MAC_GENERAL },
111 	{ "CKM_CDMF_CBC_PAD", CKM_CDMF_CBC_PAD },
112 	{ "CKM_DES_OFB64", CKM_DES_OFB64 },
113 	{ "CKM_DES_OFB8", CKM_DES_OFB8 },
114 	{ "CKM_DES_CFB64", CKM_DES_CFB64 },
115 	{ "CKM_DES_CFB8", CKM_DES_CFB8 },
116 	{ "CKM_MD2", CKM_MD2 },
117 	{ "CKM_MD2_HMAC", CKM_MD2_HMAC },
118 	{ "CKM_MD2_HMAC_GENERAL", CKM_MD2_HMAC_GENERAL },
119 	{ "CKM_MD5", CKM_MD5 },
120 	{ "CKM_MD5_HMAC", CKM_MD5_HMAC },
121 	{ "CKM_MD5_HMAC_GENERAL", CKM_MD5_HMAC_GENERAL },
122 	{ "CKM_SHA_1", CKM_SHA_1 },
123 	{ "CKM_SHA_1_HMAC", CKM_SHA_1_HMAC },
124 	{ "CKM_SHA_1_HMAC_GENERAL", CKM_SHA_1_HMAC_GENERAL },
125 	{ "CKM_RIPEMD128", CKM_RIPEMD128 },
126 	{ "CKM_RIPEMD128_HMAC", CKM_RIPEMD128_HMAC },
127 	{ "CKM_RIPEMD128_HMAC_GENERAL", CKM_RIPEMD128_HMAC_GENERAL },
128 	{ "CKM_RIPEMD160", CKM_RIPEMD160 },
129 	{ "CKM_RIPEMD160_HMAC", CKM_RIPEMD160_HMAC },
130 	{ "CKM_RIPEMD160_HMAC_GENERAL", CKM_RIPEMD160_HMAC_GENERAL },
131 	{ "CKM_SHA256", CKM_SHA256 },
132 	{ "CKM_SHA256_HMAC", CKM_SHA256_HMAC },
133 	{ "CKM_SHA256_HMAC_GENERAL", CKM_SHA256_HMAC_GENERAL },
134 	{ "CKM_SHA224", CKM_SHA224 },
135 	{ "CKM_SHA224_HMAC", CKM_SHA224_HMAC },
136 	{ "CKM_SHA224_HMAC_GENERAL", CKM_SHA224_HMAC_GENERAL },
137 	{ "CKM_SHA384", CKM_SHA384 },
138 	{ "CKM_SHA384_HMAC", CKM_SHA384_HMAC },
139 	{ "CKM_SHA384_HMAC_GENERAL", CKM_SHA384_HMAC_GENERAL },
140 	{ "CKM_SHA512", CKM_SHA512 },
141 	{ "CKM_SHA512_HMAC", CKM_SHA512_HMAC },
142 	{ "CKM_SHA512_HMAC_GENERAL", CKM_SHA512_HMAC_GENERAL },
143 	{ "CKM_SECURID_KEY_GEN", CKM_SECURID_KEY_GEN },
144 	{ "CKM_SECURID", CKM_SECURID },
145 	{ "CKM_HOTP_KEY_GEN", CKM_HOTP_KEY_GEN },
146 	{ "CKM_HOTP", CKM_HOTP },
147 	{ "CKM_ACTI", CKM_ACTI },
148 	{ "CKM_ACTI_KEY_GEN", CKM_ACTI_KEY_GEN },
149 	{ "CKM_CAST_KEY_GEN", CKM_CAST_KEY_GEN },
150 	{ "CKM_CAST_ECB", CKM_CAST_ECB },
151 	{ "CKM_CAST_CBC", CKM_CAST_CBC },
152 	{ "CKM_CAST_MAC", CKM_CAST_MAC },
153 	{ "CKM_CAST_MAC_GENERAL", CKM_CAST_MAC_GENERAL },
154 	{ "CKM_CAST_CBC_PAD", CKM_CAST_CBC_PAD },
155 	{ "CKM_CAST3_KEY_GEN", CKM_CAST3_KEY_GEN },
156 	{ "CKM_CAST3_ECB", CKM_CAST3_ECB },
157 	{ "CKM_CAST3_CBC", CKM_CAST3_CBC },
158 	{ "CKM_CAST3_MAC", CKM_CAST3_MAC },
159 	{ "CKM_CAST3_MAC_GENERAL", CKM_CAST3_MAC_GENERAL },
160 	{ "CKM_CAST3_CBC_PAD", CKM_CAST3_CBC_PAD },
161 	{ "CKM_CAST5_KEY_GEN", CKM_CAST5_KEY_GEN },
162 	{ "CKM_CAST128_KEY_GEN", CKM_CAST128_KEY_GEN },
163 	{ "CKM_CAST5_ECB", CKM_CAST5_ECB },
164 	{ "CKM_CAST128_ECB", CKM_CAST128_ECB },
165 	{ "CKM_CAST5_CBC", CKM_CAST5_CBC },
166 	{ "CKM_CAST128_CBC", CKM_CAST128_CBC },
167 	{ "CKM_CAST5_MAC", CKM_CAST5_MAC },
168 	{ "CKM_CAST128_MAC", CKM_CAST128_MAC },
169 	{ "CKM_CAST5_MAC_GENERAL", CKM_CAST5_MAC_GENERAL },
170 	{ "CKM_CAST128_MAC_GENERAL", CKM_CAST128_MAC_GENERAL },
171 	{ "CKM_CAST5_CBC_PAD", CKM_CAST5_CBC_PAD },
172 	{ "CKM_CAST128_CBC_PAD", CKM_CAST128_CBC_PAD },
173 	{ "CKM_RC5_KEY_GEN", CKM_RC5_KEY_GEN },
174 	{ "CKM_RC5_ECB", CKM_RC5_ECB },
175 	{ "CKM_RC5_CBC", CKM_RC5_CBC },
176 	{ "CKM_RC5_MAC", CKM_RC5_MAC },
177 	{ "CKM_RC5_MAC_GENERAL", CKM_RC5_MAC_GENERAL },
178 	{ "CKM_RC5_CBC_PAD", CKM_RC5_CBC_PAD },
179 	{ "CKM_IDEA_KEY_GEN", CKM_IDEA_KEY_GEN },
180 	{ "CKM_IDEA_ECB", CKM_IDEA_ECB },
181 	{ "CKM_IDEA_CBC", CKM_IDEA_CBC },
182 	{ "CKM_IDEA_MAC", CKM_IDEA_MAC },
183 	{ "CKM_IDEA_MAC_GENERAL", CKM_IDEA_MAC_GENERAL },
184 	{ "CKM_IDEA_CBC_PAD", CKM_IDEA_CBC_PAD },
185 	{ "CKM_GENERIC_SECRET_KEY_GEN", CKM_GENERIC_SECRET_KEY_GEN },
186 	{ "CKM_CONCATENATE_BASE_AND_KEY", CKM_CONCATENATE_BASE_AND_KEY },
187 	{ "CKM_CONCATENATE_BASE_AND_DATA", CKM_CONCATENATE_BASE_AND_DATA },
188 	{ "CKM_CONCATENATE_DATA_AND_BASE", CKM_CONCATENATE_DATA_AND_BASE },
189 	{ "CKM_XOR_BASE_AND_DATA", CKM_XOR_BASE_AND_DATA },
190 	{ "CKM_EXTRACT_KEY_FROM_KEY", CKM_EXTRACT_KEY_FROM_KEY },
191 	{ "CKM_SSL3_PRE_MASTER_KEY_GEN", CKM_SSL3_PRE_MASTER_KEY_GEN },
192 	{ "CKM_SSL3_MASTER_KEY_DERIVE", CKM_SSL3_MASTER_KEY_DERIVE },
193 	{ "CKM_SSL3_KEY_AND_MAC_DERIVE", CKM_SSL3_KEY_AND_MAC_DERIVE },
194 	{ "CKM_SSL3_MASTER_KEY_DERIVE_DH", CKM_SSL3_MASTER_KEY_DERIVE_DH },
195 	{ "CKM_TLS_PRE_MASTER_KEY_GEN", CKM_TLS_PRE_MASTER_KEY_GEN },
196 	{ "CKM_TLS_MASTER_KEY_DERIVE", CKM_TLS_MASTER_KEY_DERIVE },
197 	{ "CKM_TLS_KEY_AND_MAC_DERIVE", CKM_TLS_KEY_AND_MAC_DERIVE },
198 	{ "CKM_TLS_MASTER_KEY_DERIVE_DH", CKM_TLS_MASTER_KEY_DERIVE_DH },
199 	{ "CKM_TLS_PRF", CKM_TLS_PRF },
200 	{ "CKM_SSL3_MD5_MAC", CKM_SSL3_MD5_MAC },
201 	{ "CKM_SSL3_SHA1_MAC", CKM_SSL3_SHA1_MAC },
202 	{ "CKM_MD5_KEY_DERIVATION", CKM_MD5_KEY_DERIVATION },
203 	{ "CKM_MD2_KEY_DERIVATION", CKM_MD2_KEY_DERIVATION },
204 	{ "CKM_SHA1_KEY_DERIVATION", CKM_SHA1_KEY_DERIVATION },
205 	{ "CKM_SHA256_KEY_DERIVATION", CKM_SHA256_KEY_DERIVATION },
206 	{ "CKM_SHA384_KEY_DERIVATION", CKM_SHA384_KEY_DERIVATION },
207 	{ "CKM_SHA512_KEY_DERIVATION", CKM_SHA512_KEY_DERIVATION },
208 	{ "CKM_SHA224_KEY_DERIVATION", CKM_SHA224_KEY_DERIVATION },
209 	{ "CKM_PBE_MD2_DES_CBC", CKM_PBE_MD2_DES_CBC },
210 	{ "CKM_PBE_MD5_DES_CBC", CKM_PBE_MD5_DES_CBC },
211 	{ "CKM_PBE_MD5_CAST_CBC", CKM_PBE_MD5_CAST_CBC },
212 	{ "CKM_PBE_MD5_CAST3_CBC", CKM_PBE_MD5_CAST3_CBC },
213 	{ "CKM_PBE_MD5_CAST5_CBC", CKM_PBE_MD5_CAST5_CBC },
214 	{ "CKM_PBE_MD5_CAST128_CBC", CKM_PBE_MD5_CAST128_CBC },
215 	{ "CKM_PBE_SHA1_CAST5_CBC", CKM_PBE_SHA1_CAST5_CBC },
216 	{ "CKM_PBE_SHA1_CAST128_CBC", CKM_PBE_SHA1_CAST128_CBC },
217 	{ "CKM_PBE_SHA1_RC4_128", CKM_PBE_SHA1_RC4_128 },
218 	{ "CKM_PBE_SHA1_RC4_40", CKM_PBE_SHA1_RC4_40 },
219 	{ "CKM_PBE_SHA1_DES3_EDE_CBC", CKM_PBE_SHA1_DES3_EDE_CBC },
220 	{ "CKM_PBE_SHA1_DES2_EDE_CBC", CKM_PBE_SHA1_DES2_EDE_CBC },
221 	{ "CKM_PBE_SHA1_RC2_128_CBC", CKM_PBE_SHA1_RC2_128_CBC },
222 	{ "CKM_PBE_SHA1_RC2_40_CBC", CKM_PBE_SHA1_RC2_40_CBC },
223 	{ "CKM_PKCS5_PBKD2", CKM_PKCS5_PBKD2 },
224 	{ "CKM_PBA_SHA1_WITH_SHA1_HMAC", CKM_PBA_SHA1_WITH_SHA1_HMAC },
225 	{ "CKM_WTLS_PRE_MASTER_KEY_GEN", CKM_WTLS_PRE_MASTER_KEY_GEN },
226 	{ "CKM_WTLS_MASTER_KEY_DERIVE", CKM_WTLS_MASTER_KEY_DERIVE },
227 	{ "CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC",
228 	    CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC },
229 	{ "CKM_WTLS_PRF", CKM_WTLS_PRF },
230 	{ "CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE",
231 	    CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE },
232 	{ "CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE",
233 	    CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE },
234 	{ "CKM_KEY_WRAP_LYNKS", CKM_KEY_WRAP_LYNKS },
235 	{ "CKM_KEY_WRAP_SET_OAEP", CKM_KEY_WRAP_SET_OAEP },
236 	{ "CKM_KIP_DERIVE", CKM_KIP_DERIVE },
237 	{ "CKM_KIP_WRAP", CKM_KIP_WRAP },
238 	{ "CKM_KIP_MAC", CKM_KIP_MAC },
239 	{ "CKM_CAMELLIA_KEY_GEN", CKM_CAMELLIA_KEY_GEN },
240 	{ "CKM_CAMELLIA_ECB", CKM_CAMELLIA_ECB },
241 	{ "CKM_CAMELLIA_CBC", CKM_CAMELLIA_CBC },
242 	{ "CKM_CAMELLIA_MAC", CKM_CAMELLIA_MAC },
243 	{ "CKM_CAMELLIA_MAC_GENERAL", CKM_CAMELLIA_MAC_GENERAL },
244 	{ "CKM_CAMELLIA_CBC_PAD", CKM_CAMELLIA_CBC_PAD },
245 	{ "CKM_CAMELLIA_ECB_ENCRYPT_DATA", CKM_CAMELLIA_ECB_ENCRYPT_DATA },
246 	{ "CKM_CAMELLIA_CBC_ENCRYPT_DATA", CKM_CAMELLIA_CBC_ENCRYPT_DATA },
247 	{ "CKM_CAMELLIA_CTR", CKM_CAMELLIA_CTR },
248 	{ "CKM_ARIA_KEY_GEN", CKM_ARIA_KEY_GEN },
249 	{ "CKM_ARIA_ECB", CKM_ARIA_ECB },
250 	{ "CKM_ARIA_CBC", CKM_ARIA_CBC },
251 	{ "CKM_ARIA_MAC", CKM_ARIA_MAC },
252 	{ "CKM_ARIA_MAC_GENERAL", CKM_ARIA_MAC_GENERAL },
253 	{ "CKM_ARIA_CBC_PAD", CKM_ARIA_CBC_PAD },
254 	{ "CKM_ARIA_ECB_ENCRYPT_DATA", CKM_ARIA_ECB_ENCRYPT_DATA },
255 	{ "CKM_ARIA_CBC_ENCRYPT_DATA", CKM_ARIA_CBC_ENCRYPT_DATA },
256 	{ "CKM_CMS_SIG", CKM_CMS_SIG },
257 	{ "CKM_SKIPJACK_KEY_GEN", CKM_SKIPJACK_KEY_GEN },
258 	{ "CKM_SKIPJACK_ECB64", CKM_SKIPJACK_ECB64 },
259 	{ "CKM_SKIPJACK_CBC64", CKM_SKIPJACK_CBC64 },
260 	{ "CKM_SKIPJACK_OFB64", CKM_SKIPJACK_OFB64 },
261 	{ "CKM_SKIPJACK_CFB64", CKM_SKIPJACK_CFB64 },
262 	{ "CKM_SKIPJACK_CFB32", CKM_SKIPJACK_CFB32 },
263 	{ "CKM_SKIPJACK_CFB16", CKM_SKIPJACK_CFB16 },
264 	{ "CKM_SKIPJACK_CFB8", CKM_SKIPJACK_CFB8 },
265 	{ "CKM_SKIPJACK_WRAP", CKM_SKIPJACK_WRAP },
266 	{ "CKM_SKIPJACK_PRIVATE_WRAP", CKM_SKIPJACK_PRIVATE_WRAP },
267 	{ "CKM_SKIPJACK_RELAYX", CKM_SKIPJACK_RELAYX },
268 	{ "CKM_KEA_KEY_PAIR_GEN", CKM_KEA_KEY_PAIR_GEN },
269 	{ "CKM_KEA_KEY_DERIVE", CKM_KEA_KEY_DERIVE },
270 	{ "CKM_FORTEZZA_TIMESTAMP", CKM_FORTEZZA_TIMESTAMP },
271 	{ "CKM_BATON_KEY_GEN", CKM_BATON_KEY_GEN },
272 	{ "CKM_BATON_ECB128", CKM_BATON_ECB128 },
273 	{ "CKM_BATON_ECB96", CKM_BATON_ECB96 },
274 	{ "CKM_BATON_CBC128", CKM_BATON_CBC128 },
275 	{ "CKM_BATON_COUNTER", CKM_BATON_COUNTER },
276 	{ "CKM_BATON_SHUFFLE", CKM_BATON_SHUFFLE },
277 	{ "CKM_BATON_WRAP", CKM_BATON_WRAP },
278 	{ "CKM_EC_KEY_PAIR_GEN", CKM_EC_KEY_PAIR_GEN },
279 	{ "CKM_ECDSA", CKM_ECDSA },
280 	{ "CKM_ECDSA_SHA1", CKM_ECDSA_SHA1 },
281 	{ "CKM_ECDH1_DERIVE", CKM_ECDH1_DERIVE },
282 	{ "CKM_ECDH1_COFACTOR_DERIVE", CKM_ECDH1_COFACTOR_DERIVE },
283 	{ "CKM_ECMQV_DERIVE", CKM_ECMQV_DERIVE },
284 	{ "CKM_JUNIPER_KEY_GEN", CKM_JUNIPER_KEY_GEN },
285 	{ "CKM_JUNIPER_ECB128", CKM_JUNIPER_ECB128 },
286 	{ "CKM_JUNIPER_CBC128", CKM_JUNIPER_CBC128 },
287 	{ "CKM_JUNIPER_COUNTER", CKM_JUNIPER_COUNTER },
288 	{ "CKM_JUNIPER_SHUFFLE", CKM_JUNIPER_SHUFFLE },
289 	{ "CKM_JUNIPER_WRAP", CKM_JUNIPER_WRAP },
290 	{ "CKM_FASTHASH", CKM_FASTHASH },
291 	{ "CKM_AES_KEY_GEN", CKM_AES_KEY_GEN },
292 	{ "CKM_AES_ECB", CKM_AES_ECB },
293 	{ "CKM_AES_CBC", CKM_AES_CBC },
294 	{ "CKM_AES_MAC", CKM_AES_MAC },
295 	{ "CKM_AES_MAC_GENERAL", CKM_AES_MAC_GENERAL },
296 	{ "CKM_AES_CBC_PAD", CKM_AES_CBC_PAD },
297 	{ "CKM_AES_CTR", CKM_AES_CTR },
298 	{ "CKM_BLOWFISH_KEY_GEN", CKM_BLOWFISH_KEY_GEN },
299 	{ "CKM_BLOWFISH_CBC", CKM_BLOWFISH_CBC },
300 	{ "CKM_TWOFISH_KEY_GEN", CKM_TWOFISH_KEY_GEN },
301 	{ "CKM_TWOFISH_CBC", CKM_TWOFISH_CBC },
302 	{ "CKM_DES_ECB_ENCRYPT_DATA", CKM_DES_ECB_ENCRYPT_DATA },
303 	{ "CKM_DES_CBC_ENCRYPT_DATA", CKM_DES_CBC_ENCRYPT_DATA },
304 	{ "CKM_DES3_ECB_ENCRYPT_DATA", CKM_DES3_ECB_ENCRYPT_DATA },
305 	{ "CKM_DES3_CBC_ENCRYPT_DATA", CKM_DES3_CBC_ENCRYPT_DATA },
306 	{ "CKM_AES_ECB_ENCRYPT_DATA", CKM_AES_ECB_ENCRYPT_DATA },
307 	{ "CKM_AES_CBC_ENCRYPT_DATA", CKM_AES_CBC_ENCRYPT_DATA },
308 	{ "CKM_DSA_PARAMETER_GEN", CKM_DSA_PARAMETER_GEN },
309 	{ "CKM_DH_PKCS_PARAMETER_GEN", CKM_DH_PKCS_PARAMETER_GEN },
310 	{ "CKM_X9_42_DH_PARAMETER_GEN", CKM_X9_42_DH_PARAMETER_GEN },
311 	/*
312 	 * Values >= 0x8000000 (CKM_VENDOR_DEFINED) are represented
313 	 * as strings with hexadecimal numbers (e.g., "0x8123456").
314 	 */
315 	{ NULL, 0 }
316 };
317 
318 
319 /*
320  * pkcs11_mech_comp - compare two pkcs11_mapping_t structures
321  *
322  * Return a strcmp-like result (positive, zero, or negative).
323  * For use with bsearch(3C) in pkcs11_mech2str().
324  */
325 static int
326 pkcs11_mech_comp(const void *mapping1, const void *mapping2) {
327 	return (((pkcs11_mapping_t *)mapping1)->mech -
328 		((pkcs11_mapping_t *)mapping2)->mech);
329 }
330 
331 
332 /*
333  * pkcs11_mech2str - convert PKCS#11 mech to a string
334  *
335  * Anything below CKM_VENDOR_DEFINED that wasn't in the mapping table
336  * at build time causes NULL to be returned.  Anything above it also
337  * returns NULL since we have no way to know its real name.
338  */
339 const char
340 *pkcs11_mech2str(CK_MECHANISM_TYPE mech)
341 {
342 	pkcs11_mapping_t	target;
343 	pkcs11_mapping_t	*result = NULL;
344 
345 	if (mech >= CKM_VENDOR_DEFINED) {
346 		return (NULL);
347 	}
348 
349 	/* Search for the mechanism number using bsearch(3C) */
350 	target.mech = mech;
351 	target.str = NULL;
352 	result = (pkcs11_mapping_t *)bsearch((void *)&target, (void *)mapping,
353 	    (sizeof (mapping) / sizeof (pkcs11_mapping_t)) - 1,
354 	    sizeof (pkcs11_mapping_t), pkcs11_mech_comp);
355 	if (result != NULL) {
356 		return (result->str);
357 	}
358 
359 	return (NULL);
360 }
361 
362 /*
363  * pkcs11_str2mech - convert a string into a PKCS#11 mech number.
364  *
365  * Since there isn't a reserved value for an invalid mech we return
366  * CKR_MECHANISM_INVALID for anything we don't recognise.
367  * The value in mech isn't meaningful in these cases.
368  */
369 CK_RV
370 pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech)
371 {
372 	int	i;
373 	int	compare_off = 0;
374 
375 	if (mech_str == NULL)
376 		return (CKR_MECHANISM_INVALID);
377 
378 	if (strncasecmp(mech_str, "0x", 2) == 0) {
379 		long long llnum;
380 		cryptodebug("pkcs11_str2mech: hex string passed in: %s",
381 		    mech_str);
382 		llnum = strtoll(mech_str, NULL, 16);
383 		if ((llnum >= CKM_VENDOR_DEFINED) && (llnum <= UINT_MAX)) {
384 			*mech = llnum;
385 			return (CKR_OK);
386 		} else {
387 			return (CKR_MECHANISM_INVALID);
388 		}
389 	}
390 
391 	/* If there's no CKM_ prefix, then ignore it in comparisons */
392 	if (strncasecmp(mech_str, "CKM_", 4) != 0) {
393 		cryptodebug("pkcs11_str2mech: no CKM_ prefix: %s", mech_str);
394 		cryptodebug("pkcs11_str2mech: with prefix: CKM_%s", mech_str);
395 		compare_off = 4;
396 	}
397 
398 	/* Linear search for a matching string */
399 	for (i = 0; mapping[i].str; i++) {
400 		if (strcasecmp(&mapping[i].str[compare_off], mech_str) == 0) {
401 			*mech = mapping[i].mech;
402 			return (CKR_OK);
403 		}
404 	}
405 
406 	return (CKR_MECHANISM_INVALID);
407 }
408