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