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