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