xref: /titanic_41/usr/src/lib/libcryptoutil/common/mechstr.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Convert Algorithm names as strings to PKCS#11 Mech numbers and vice versa.
31  */
32 
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <security/cryptoki.h>
37 #include <security/pkcs11t.h>
38 
39 #include <cryptoutil.h>
40 
41 /*
42  * The table below is dynamically generated by parsing pkcs11t.h
43  * from the Makefile.
44  */
45 static const struct {
46 	const char *str;
47 	CK_MECHANISM_TYPE mech;
48 } mapping[] = {
49 	{ "CKM_RSA_PKCS_KEY_PAIR_GEN", CKM_RSA_PKCS_KEY_PAIR_GEN },
50 	{ "CKM_RSA_PKCS", CKM_RSA_PKCS },
51 	{ "CKM_RSA_9796", CKM_RSA_9796 },
52 	{ "CKM_RSA_X_509", CKM_RSA_X_509 },
53 	{ "CKM_MD2_RSA_PKCS", CKM_MD2_RSA_PKCS },
54 	{ "CKM_MD5_RSA_PKCS", CKM_MD5_RSA_PKCS },
55 	{ "CKM_SHA1_RSA_PKCS", CKM_SHA1_RSA_PKCS },
56 	{ "CKM_SHA256_RSA_PKCS", CKM_SHA256_RSA_PKCS },
57 	{ "CKM_SHA384_RSA_PKCS", CKM_SHA384_RSA_PKCS },
58 	{ "CKM_SHA512_RSA_PKCS", CKM_SHA512_RSA_PKCS },
59 	{ "CKM_RIPEMD128_RSA_PKCS", CKM_RIPEMD128_RSA_PKCS },
60 	{ "CKM_RIPEMD160_RSA_PKCS", CKM_RIPEMD160_RSA_PKCS },
61 	{ "CKM_RSA_PKCS_OAEP", CKM_RSA_PKCS_OAEP },
62 	{ "CKM_RSA_X9_31_KEY_PAIR_GEN", CKM_RSA_X9_31_KEY_PAIR_GEN },
63 	{ "CKM_RSA_X9_31", CKM_RSA_X9_31 },
64 	{ "CKM_SHA1_RSA_X9_31", CKM_SHA1_RSA_X9_31 },
65 	{ "CKM_RSA_PKCS_PSS", CKM_RSA_PKCS_PSS },
66 	{ "CKM_SHA1_RSA_PKCS_PSS", CKM_SHA1_RSA_PKCS_PSS },
67 	{ "CKM_DSA_KEY_PAIR_GEN", CKM_DSA_KEY_PAIR_GEN },
68 	{ "CKM_DSA", CKM_DSA },
69 	{ "CKM_DSA_SHA1", CKM_DSA_SHA1 },
70 	{ "CKM_DH_PKCS_KEY_PAIR_GEN", CKM_DH_PKCS_KEY_PAIR_GEN },
71 	{ "CKM_DH_PKCS_DERIVE", CKM_DH_PKCS_DERIVE },
72 	{ "CKM_X9_42_DH_KEY_PAIR_GEN", CKM_X9_42_DH_KEY_PAIR_GEN },
73 	{ "CKM_X9_42_DH_DERIVE", CKM_X9_42_DH_DERIVE },
74 	{ "CKM_X9_42_DH_HYBRID_DERIVE", CKM_X9_42_DH_HYBRID_DERIVE },
75 	{ "CKM_X9_42_MQV_DERIVE", CKM_X9_42_MQV_DERIVE },
76 	{ "CKM_SHA256_RSA_PKCS", CKM_SHA256_RSA_PKCS },
77 	{ "CKM_SHA384_RSA_PKCS", CKM_SHA384_RSA_PKCS },
78 	{ "CKM_SHA512_RSA_PKCS", CKM_SHA512_RSA_PKCS },
79 	{ "CKM_SHA256_RSA_PKCS_PSS", CKM_SHA256_RSA_PKCS_PSS },
80 	{ "CKM_SHA384_RSA_PKCS_PSS", CKM_SHA384_RSA_PKCS_PSS },
81 	{ "CKM_SHA512_RSA_PKCS_PSS", CKM_SHA512_RSA_PKCS_PSS },
82 	{ "CKM_RC2_KEY_GEN", CKM_RC2_KEY_GEN },
83 	{ "CKM_RC2_ECB", CKM_RC2_ECB },
84 	{ "CKM_RC2_CBC", CKM_RC2_CBC },
85 	{ "CKM_RC2_MAC", CKM_RC2_MAC },
86 	{ "CKM_RC2_MAC_GENERAL", CKM_RC2_MAC_GENERAL },
87 	{ "CKM_RC2_CBC_PAD", CKM_RC2_CBC_PAD },
88 	{ "CKM_RC4_KEY_GEN", CKM_RC4_KEY_GEN },
89 	{ "CKM_RC4", CKM_RC4 },
90 	{ "CKM_DES_KEY_GEN", CKM_DES_KEY_GEN },
91 	{ "CKM_DES_ECB", CKM_DES_ECB },
92 	{ "CKM_DES_CBC", CKM_DES_CBC },
93 	{ "CKM_DES_MAC", CKM_DES_MAC },
94 	{ "CKM_DES_MAC_GENERAL", CKM_DES_MAC_GENERAL },
95 	{ "CKM_DES_CBC_PAD", CKM_DES_CBC_PAD },
96 	{ "CKM_DES2_KEY_GEN", CKM_DES2_KEY_GEN },
97 	{ "CKM_DES3_KEY_GEN", CKM_DES3_KEY_GEN },
98 	{ "CKM_DES3_ECB", CKM_DES3_ECB },
99 	{ "CKM_DES3_CBC", CKM_DES3_CBC },
100 	{ "CKM_DES3_MAC", CKM_DES3_MAC },
101 	{ "CKM_DES3_MAC_GENERAL", CKM_DES3_MAC_GENERAL },
102 	{ "CKM_DES3_CBC_PAD", CKM_DES3_CBC_PAD },
103 	{ "CKM_CDMF_KEY_GEN", CKM_CDMF_KEY_GEN },
104 	{ "CKM_CDMF_ECB", CKM_CDMF_ECB },
105 	{ "CKM_CDMF_CBC", CKM_CDMF_CBC },
106 	{ "CKM_CDMF_MAC", CKM_CDMF_MAC },
107 	{ "CKM_CDMF_MAC_GENERAL", CKM_CDMF_MAC_GENERAL },
108 	{ "CKM_CDMF_CBC_PAD", CKM_CDMF_CBC_PAD },
109 	{ "CKM_DES_OFB64", CKM_DES_OFB64 },
110 	{ "CKM_DES_OFB8", CKM_DES_OFB8 },
111 	{ "CKM_DES_CFB64", CKM_DES_CFB64 },
112 	{ "CKM_DES_CFB8", CKM_DES_CFB8 },
113 	{ "CKM_MD2", CKM_MD2 },
114 	{ "CKM_MD2_HMAC", CKM_MD2_HMAC },
115 	{ "CKM_MD2_HMAC_GENERAL", CKM_MD2_HMAC_GENERAL },
116 	{ "CKM_MD5", CKM_MD5 },
117 	{ "CKM_MD5_HMAC", CKM_MD5_HMAC },
118 	{ "CKM_MD5_HMAC_GENERAL", CKM_MD5_HMAC_GENERAL },
119 	{ "CKM_SHA_1", CKM_SHA_1 },
120 	{ "CKM_SHA_1_HMAC", CKM_SHA_1_HMAC },
121 	{ "CKM_SHA_1_HMAC_GENERAL", CKM_SHA_1_HMAC_GENERAL },
122 	{ "CKM_RIPEMD128", CKM_RIPEMD128 },
123 	{ "CKM_RIPEMD128_HMAC", CKM_RIPEMD128_HMAC },
124 	{ "CKM_RIPEMD128_HMAC_GENERAL", CKM_RIPEMD128_HMAC_GENERAL },
125 	{ "CKM_RIPEMD160", CKM_RIPEMD160 },
126 	{ "CKM_RIPEMD160_HMAC", CKM_RIPEMD160_HMAC },
127 	{ "CKM_RIPEMD160_HMAC_GENERAL", CKM_RIPEMD160_HMAC_GENERAL },
128 	{ "CKM_SHA256", CKM_SHA256 },
129 	{ "CKM_SHA256_HMAC", CKM_SHA256_HMAC },
130 	{ "CKM_SHA256_HMAC_GENERAL", CKM_SHA256_HMAC_GENERAL },
131 	{ "CKM_SHA384", CKM_SHA384 },
132 	{ "CKM_SHA384_HMAC", CKM_SHA384_HMAC },
133 	{ "CKM_SHA384_HMAC_GENERAL", CKM_SHA384_HMAC_GENERAL },
134 	{ "CKM_SHA512", CKM_SHA512 },
135 	{ "CKM_SHA512_HMAC", CKM_SHA512_HMAC },
136 	{ "CKM_SHA512_HMAC_GENERAL", CKM_SHA512_HMAC_GENERAL },
137 	{ "CKM_CAST_KEY_GEN", CKM_CAST_KEY_GEN },
138 	{ "CKM_CAST_ECB", CKM_CAST_ECB },
139 	{ "CKM_CAST_CBC", CKM_CAST_CBC },
140 	{ "CKM_CAST_MAC", CKM_CAST_MAC },
141 	{ "CKM_CAST_MAC_GENERAL", CKM_CAST_MAC_GENERAL },
142 	{ "CKM_CAST_CBC_PAD", CKM_CAST_CBC_PAD },
143 	{ "CKM_CAST3_KEY_GEN", CKM_CAST3_KEY_GEN },
144 	{ "CKM_CAST3_ECB", CKM_CAST3_ECB },
145 	{ "CKM_CAST3_CBC", CKM_CAST3_CBC },
146 	{ "CKM_CAST3_MAC", CKM_CAST3_MAC },
147 	{ "CKM_CAST3_MAC_GENERAL", CKM_CAST3_MAC_GENERAL },
148 	{ "CKM_CAST3_CBC_PAD", CKM_CAST3_CBC_PAD },
149 	{ "CKM_CAST5_KEY_GEN", CKM_CAST5_KEY_GEN },
150 	{ "CKM_CAST128_KEY_GEN", CKM_CAST128_KEY_GEN },
151 	{ "CKM_CAST5_ECB", CKM_CAST5_ECB },
152 	{ "CKM_CAST128_ECB", CKM_CAST128_ECB },
153 	{ "CKM_CAST5_CBC", CKM_CAST5_CBC },
154 	{ "CKM_CAST128_CBC", CKM_CAST128_CBC },
155 	{ "CKM_CAST5_MAC", CKM_CAST5_MAC },
156 	{ "CKM_CAST128_MAC", CKM_CAST128_MAC },
157 	{ "CKM_CAST5_MAC_GENERAL", CKM_CAST5_MAC_GENERAL },
158 	{ "CKM_CAST128_MAC_GENERAL", CKM_CAST128_MAC_GENERAL },
159 	{ "CKM_CAST5_CBC_PAD", CKM_CAST5_CBC_PAD },
160 	{ "CKM_CAST128_CBC_PAD", CKM_CAST128_CBC_PAD },
161 	{ "CKM_RC5_KEY_GEN", CKM_RC5_KEY_GEN },
162 	{ "CKM_RC5_ECB", CKM_RC5_ECB },
163 	{ "CKM_RC5_CBC", CKM_RC5_CBC },
164 	{ "CKM_RC5_MAC", CKM_RC5_MAC },
165 	{ "CKM_RC5_MAC_GENERAL", CKM_RC5_MAC_GENERAL },
166 	{ "CKM_RC5_CBC_PAD", CKM_RC5_CBC_PAD },
167 	{ "CKM_IDEA_KEY_GEN", CKM_IDEA_KEY_GEN },
168 	{ "CKM_IDEA_ECB", CKM_IDEA_ECB },
169 	{ "CKM_IDEA_CBC", CKM_IDEA_CBC },
170 	{ "CKM_IDEA_MAC", CKM_IDEA_MAC },
171 	{ "CKM_IDEA_MAC_GENERAL", CKM_IDEA_MAC_GENERAL },
172 	{ "CKM_IDEA_CBC_PAD", CKM_IDEA_CBC_PAD },
173 	{ "CKM_GENERIC_SECRET_KEY_GEN", CKM_GENERIC_SECRET_KEY_GEN },
174 	{ "CKM_CONCATENATE_BASE_AND_KEY", CKM_CONCATENATE_BASE_AND_KEY },
175 	{ "CKM_CONCATENATE_BASE_AND_DATA", CKM_CONCATENATE_BASE_AND_DATA },
176 	{ "CKM_CONCATENATE_DATA_AND_BASE", CKM_CONCATENATE_DATA_AND_BASE },
177 	{ "CKM_XOR_BASE_AND_DATA", CKM_XOR_BASE_AND_DATA },
178 	{ "CKM_EXTRACT_KEY_FROM_KEY", CKM_EXTRACT_KEY_FROM_KEY },
179 	{ "CKM_SSL3_PRE_MASTER_KEY_GEN", CKM_SSL3_PRE_MASTER_KEY_GEN },
180 	{ "CKM_SSL3_MASTER_KEY_DERIVE", CKM_SSL3_MASTER_KEY_DERIVE },
181 	{ "CKM_SSL3_KEY_AND_MAC_DERIVE", CKM_SSL3_KEY_AND_MAC_DERIVE },
182 	{ "CKM_SSL3_MASTER_KEY_DERIVE_DH", CKM_SSL3_MASTER_KEY_DERIVE_DH },
183 	{ "CKM_TLS_PRE_MASTER_KEY_GEN", CKM_TLS_PRE_MASTER_KEY_GEN },
184 	{ "CKM_TLS_MASTER_KEY_DERIVE", CKM_TLS_MASTER_KEY_DERIVE },
185 	{ "CKM_TLS_KEY_AND_MAC_DERIVE", CKM_TLS_KEY_AND_MAC_DERIVE },
186 	{ "CKM_TLS_MASTER_KEY_DERIVE_DH", CKM_TLS_MASTER_KEY_DERIVE_DH },
187 	{ "CKM_TLS_PRF", CKM_TLS_PRF },
188 	{ "CKM_SSL3_MD5_MAC", CKM_SSL3_MD5_MAC },
189 	{ "CKM_SSL3_SHA1_MAC", CKM_SSL3_SHA1_MAC },
190 	{ "CKM_MD5_KEY_DERIVATION", CKM_MD5_KEY_DERIVATION },
191 	{ "CKM_MD2_KEY_DERIVATION", CKM_MD2_KEY_DERIVATION },
192 	{ "CKM_SHA1_KEY_DERIVATION", CKM_SHA1_KEY_DERIVATION },
193 	{ "CKM_SHA256_KEY_DERIVATION", CKM_SHA256_KEY_DERIVATION },
194 	{ "CKM_SHA384_KEY_DERIVATION", CKM_SHA384_KEY_DERIVATION },
195 	{ "CKM_SHA512_KEY_DERIVATION", CKM_SHA512_KEY_DERIVATION },
196 	{ "CKM_PBE_MD2_DES_CBC", CKM_PBE_MD2_DES_CBC },
197 	{ "CKM_PBE_MD5_DES_CBC", CKM_PBE_MD5_DES_CBC },
198 	{ "CKM_PBE_MD5_CAST_CBC", CKM_PBE_MD5_CAST_CBC },
199 	{ "CKM_PBE_MD5_CAST3_CBC", CKM_PBE_MD5_CAST3_CBC },
200 	{ "CKM_PBE_MD5_CAST5_CBC", CKM_PBE_MD5_CAST5_CBC },
201 	{ "CKM_PBE_MD5_CAST128_CBC", CKM_PBE_MD5_CAST128_CBC },
202 	{ "CKM_PBE_SHA1_CAST5_CBC", CKM_PBE_SHA1_CAST5_CBC },
203 	{ "CKM_PBE_SHA1_CAST128_CBC", CKM_PBE_SHA1_CAST128_CBC },
204 	{ "CKM_PBE_SHA1_RC4_128", CKM_PBE_SHA1_RC4_128 },
205 	{ "CKM_PBE_SHA1_RC4_40", CKM_PBE_SHA1_RC4_40 },
206 	{ "CKM_PBE_SHA1_DES3_EDE_CBC", CKM_PBE_SHA1_DES3_EDE_CBC },
207 	{ "CKM_PBE_SHA1_DES2_EDE_CBC", CKM_PBE_SHA1_DES2_EDE_CBC },
208 	{ "CKM_PBE_SHA1_RC2_128_CBC", CKM_PBE_SHA1_RC2_128_CBC },
209 	{ "CKM_PBE_SHA1_RC2_40_CBC", CKM_PBE_SHA1_RC2_40_CBC },
210 	{ "CKM_PKCS5_PBKD2", CKM_PKCS5_PBKD2 },
211 	{ "CKM_PBA_SHA1_WITH_SHA1_HMAC", CKM_PBA_SHA1_WITH_SHA1_HMAC },
212 	{ "CKM_KEY_WRAP_LYNKS", CKM_KEY_WRAP_LYNKS },
213 	{ "CKM_KEY_WRAP_SET_OAEP", CKM_KEY_WRAP_SET_OAEP },
214 	{ "CKM_SKIPJACK_KEY_GEN", CKM_SKIPJACK_KEY_GEN },
215 	{ "CKM_SKIPJACK_ECB64", CKM_SKIPJACK_ECB64 },
216 	{ "CKM_SKIPJACK_CBC64", CKM_SKIPJACK_CBC64 },
217 	{ "CKM_SKIPJACK_OFB64", CKM_SKIPJACK_OFB64 },
218 	{ "CKM_SKIPJACK_CFB64", CKM_SKIPJACK_CFB64 },
219 	{ "CKM_SKIPJACK_CFB32", CKM_SKIPJACK_CFB32 },
220 	{ "CKM_SKIPJACK_CFB16", CKM_SKIPJACK_CFB16 },
221 	{ "CKM_SKIPJACK_CFB8", CKM_SKIPJACK_CFB8 },
222 	{ "CKM_SKIPJACK_WRAP", CKM_SKIPJACK_WRAP },
223 	{ "CKM_SKIPJACK_PRIVATE_WRAP", CKM_SKIPJACK_PRIVATE_WRAP },
224 	{ "CKM_SKIPJACK_RELAYX", CKM_SKIPJACK_RELAYX },
225 	{ "CKM_KEA_KEY_PAIR_GEN", CKM_KEA_KEY_PAIR_GEN },
226 	{ "CKM_KEA_KEY_DERIVE", CKM_KEA_KEY_DERIVE },
227 	{ "CKM_FORTEZZA_TIMESTAMP", CKM_FORTEZZA_TIMESTAMP },
228 	{ "CKM_BATON_KEY_GEN", CKM_BATON_KEY_GEN },
229 	{ "CKM_BATON_ECB128", CKM_BATON_ECB128 },
230 	{ "CKM_BATON_ECB96", CKM_BATON_ECB96 },
231 	{ "CKM_BATON_CBC128", CKM_BATON_CBC128 },
232 	{ "CKM_BATON_COUNTER", CKM_BATON_COUNTER },
233 	{ "CKM_BATON_SHUFFLE", CKM_BATON_SHUFFLE },
234 	{ "CKM_BATON_WRAP", CKM_BATON_WRAP },
235 	{ "CKM_ECDSA_KEY_PAIR_GEN", CKM_ECDSA_KEY_PAIR_GEN },
236 	{ "CKM_EC_KEY_PAIR_GEN", CKM_EC_KEY_PAIR_GEN },
237 	{ "CKM_ECDSA", CKM_ECDSA },
238 	{ "CKM_ECDSA_SHA1", CKM_ECDSA_SHA1 },
239 	{ "CKM_ECDH1_DERIVE", CKM_ECDH1_DERIVE },
240 	{ "CKM_ECDH1_COFACTOR_DERIVE", CKM_ECDH1_COFACTOR_DERIVE },
241 	{ "CKM_ECMQV_DERIVE", CKM_ECMQV_DERIVE },
242 	{ "CKM_JUNIPER_KEY_GEN", CKM_JUNIPER_KEY_GEN },
243 	{ "CKM_JUNIPER_ECB128", CKM_JUNIPER_ECB128 },
244 	{ "CKM_JUNIPER_CBC128", CKM_JUNIPER_CBC128 },
245 	{ "CKM_JUNIPER_COUNTER", CKM_JUNIPER_COUNTER },
246 	{ "CKM_JUNIPER_SHUFFLE", CKM_JUNIPER_SHUFFLE },
247 	{ "CKM_JUNIPER_WRAP", CKM_JUNIPER_WRAP },
248 	{ "CKM_FASTHASH", CKM_FASTHASH },
249 	{ "CKM_AES_KEY_GEN", CKM_AES_KEY_GEN },
250 	{ "CKM_AES_ECB", CKM_AES_ECB },
251 	{ "CKM_AES_CBC", CKM_AES_CBC },
252 	{ "CKM_AES_MAC", CKM_AES_MAC },
253 	{ "CKM_AES_MAC_GENERAL", CKM_AES_MAC_GENERAL },
254 	{ "CKM_AES_CBC_PAD", CKM_AES_CBC_PAD },
255 	{ "CKM_BLOWFISH_KEY_GEN", CKM_BLOWFISH_KEY_GEN },
256 	{ "CKM_BLOWFISH_CBC", CKM_BLOWFISH_CBC },
257 	{ "CKM_TWOFISH_KEY_GEN", CKM_TWOFISH_KEY_GEN },
258 	{ "CKM_TWOFISH_CBC", CKM_TWOFISH_CBC },
259 	{ "CKM_DES_ECB_ENCRYPT_DATA", CKM_DES_ECB_ENCRYPT_DATA },
260 	{ "CKM_DES_CBC_ENCRYPT_DATA", CKM_DES_CBC_ENCRYPT_DATA },
261 	{ "CKM_DES3_ECB_ENCRYPT_DATA", CKM_DES3_ECB_ENCRYPT_DATA },
262 	{ "CKM_DES3_CBC_ENCRYPT_DATA", CKM_DES3_CBC_ENCRYPT_DATA },
263 	{ "CKM_AES_ECB_ENCRYPT_DATA", CKM_AES_ECB_ENCRYPT_DATA },
264 	{ "CKM_AES_CBC_ENCRYPT_DATA", CKM_AES_CBC_ENCRYPT_DATA },
265 	{ "CKM_DSA_PARAMETER_GEN", CKM_DSA_PARAMETER_GEN },
266 	{ "CKM_DH_PKCS_PARAMETER_GEN", CKM_DH_PKCS_PARAMETER_GEN },
267 	{ "CKM_X9_42_DH_PARAMETER_GEN", CKM_X9_42_DH_PARAMETER_GEN },
268 	{ "CKM_VENDOR_DEFINED", CKM_VENDOR_DEFINED },
269 	{ NULL, 0 }
270 };
271 
272 /*
273  * pkcs11_mech2str - convert PKCS#11 mech to a string
274  *
275  * Anything below CKM_VENDOR_DEFINED that wasn't in the mapping table
276  * at build time causes NULL to be returned.  Anything above it also
277  * returns NULL since we have no way to know what its real name is.
278  */
279 char
280 *pkcs11_mech2str(CK_MECHANISM_TYPE mech)
281 {
282 	int i;
283 	char buf[11];	/* Num chars for representing ulong in ASCII */
284 
285 	if (mech > CKM_VENDOR_DEFINED) {
286 		(void) snprintf(buf, sizeof (buf), "%#lx", mech);
287 		return (strdup(buf));
288 	}
289 
290 	for (i = 0; mapping[i].str; i++) {
291 		if (mapping[i].mech == mech)
292 			return (strdup(mapping[i].str));
293 	}
294 
295 	return (NULL);
296 }
297 
298 /*
299  * pkcs11_str2mech - convert a string into a PKCS#11 mech number.
300  *
301  * Since there isn't reserved value for an invalid mech we return
302  * CKR_MECHANISM_INVALID for anything we don't recognise.
303  * The value in mech isn't meaningful in these cases.
304  */
305 CK_RV
306 pkcs11_str2mech(char *mech_str, CK_MECHANISM_TYPE_PTR mech)
307 {
308 	int i;
309 	char *tmech_str;
310 
311 	if (mech_str == NULL)
312 		return (CKR_MECHANISM_INVALID);
313 
314 	if (strncasecmp(mech_str, "0x8", 3) == 0) {
315 		cryptodebug("pkcs11_str2mech: hex string passed in: %s",
316 		    mech_str);
317 		*mech = strtoll(mech_str, NULL, 16);
318 		return (CKR_OK);
319 	}
320 
321 	if (strncasecmp(mech_str, "CKM_", 4) != 0) {
322 		size_t tmech_strlen = strlen(mech_str) + 4 + 1;
323 		cryptodebug("pkcs11_str2mech: no CKM_ prefix: %s", mech_str);
324 		tmech_str = malloc(tmech_strlen * sizeof (char));
325 		(void) snprintf(tmech_str, tmech_strlen, "CKM_%s", mech_str);
326 		cryptodebug("pkcs11_str2mech: with prefix: %s", tmech_str);
327 	} else {
328 		tmech_str = mech_str;
329 	}
330 
331 	for (i = 0; mapping[i].str; i++) {
332 		if (strcasecmp(mapping[i].str, tmech_str) == 0) {
333 			*mech = mapping[i].mech;
334 			if (tmech_str != mech_str)
335 				free(tmech_str);
336 			return (CKR_OK);
337 		}
338 	}
339 	if (tmech_str != mech_str)
340 		free(tmech_str);
341 
342 	return (CKR_MECHANISM_INVALID);
343 }
344