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