xref: /titanic_50/usr/src/lib/libcryptoutil/common/mechkeygen.c (revision 43b9c05035ac59f7f7a8e7827598db5a15f30ed3)
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 #include <cryptoutil.h>
29 
30 /*
31  * Get the key generation mechanism for the given mechanism.
32  *
33  * All mechanisms in PKCS #11 v2.20 are listed here.
34  */
35 CK_RV
36 pkcs11_mech2keygen(CK_MECHANISM_TYPE mech_type, CK_MECHANISM_TYPE *gen_mech)
37 {
38 	switch (mech_type) {
39 
40 	case CKM_RSA_PKCS_KEY_PAIR_GEN:
41 	case CKM_RSA_PKCS:
42 	case CKM_RSA_9796:
43 	case CKM_RSA_X_509:
44 	case CKM_MD2_RSA_PKCS:
45 	case CKM_MD5_RSA_PKCS:
46 	case CKM_SHA1_RSA_PKCS:
47 	case CKM_SHA256_RSA_PKCS:
48 	case CKM_SHA384_RSA_PKCS:
49 	case CKM_SHA512_RSA_PKCS:
50 	case CKM_SHA256_RSA_PKCS_PSS:
51 	case CKM_SHA384_RSA_PKCS_PSS:
52 	case CKM_SHA512_RSA_PKCS_PSS:
53 	case CKM_RIPEMD128_RSA_PKCS:
54 	case CKM_RIPEMD160_RSA_PKCS:
55 	case CKM_RSA_PKCS_OAEP:
56 		*gen_mech = CKM_RSA_PKCS_KEY_PAIR_GEN;
57 		break;
58 
59 	case CKM_RSA_X9_31_KEY_PAIR_GEN:
60 	case CKM_RSA_X9_31:
61 	case CKM_SHA1_RSA_X9_31:
62 		*gen_mech = CKM_RSA_X9_31_KEY_PAIR_GEN;
63 		break;
64 
65 	case CKM_RSA_PKCS_PSS:
66 	case CKM_SHA1_RSA_PKCS_PSS:
67 		*gen_mech = CKM_RSA_PKCS_KEY_PAIR_GEN;
68 		break;
69 
70 	case CKM_DH_PKCS_PARAMETER_GEN:
71 		*gen_mech = CKM_DH_PKCS_PARAMETER_GEN;
72 		break;
73 
74 	case CKM_DSA_KEY_PAIR_GEN:
75 	case CKM_DSA:
76 	case CKM_DSA_SHA1:
77 		*gen_mech = CKM_DSA_KEY_PAIR_GEN;
78 		break;
79 
80 	case CKM_DSA_PARAMETER_GEN:
81 		*gen_mech = CKM_DSA_PARAMETER_GEN;
82 		break;
83 
84 	case CKM_FORTEZZA_TIMESTAMP:
85 		*gen_mech = CKM_DSA_KEY_PAIR_GEN;
86 		break;
87 
88 	case CKM_DH_PKCS_KEY_PAIR_GEN:
89 	case CKM_DH_PKCS_DERIVE:
90 		*gen_mech = CKM_DH_PKCS_KEY_PAIR_GEN;
91 		break;
92 
93 	case CKM_ECDSA:
94 	case CKM_ECDSA_SHA1:
95 	case CKM_EC_KEY_PAIR_GEN:
96 	case CKM_ECDH1_DERIVE:
97 	case CKM_ECDH1_COFACTOR_DERIVE:
98 	case CKM_ECMQV_DERIVE:
99 		*gen_mech = CKM_EC_KEY_PAIR_GEN;
100 		break;
101 
102 	case CKM_X9_42_DH_KEY_PAIR_GEN:
103 	case CKM_X9_42_DH_DERIVE:
104 	case CKM_X9_42_DH_HYBRID_DERIVE:
105 	case CKM_X9_42_MQV_DERIVE:
106 		*gen_mech = CKM_X9_42_DH_KEY_PAIR_GEN;
107 		break;
108 
109 	case CKM_X9_42_DH_PARAMETER_GEN:
110 		*gen_mech = CKM_X9_42_DH_PARAMETER_GEN;
111 		break;
112 
113 	case CKM_KEA_KEY_PAIR_GEN:
114 	case CKM_KEA_KEY_DERIVE:
115 		*gen_mech = CKM_KEA_KEY_PAIR_GEN;
116 		break;
117 
118 	case CKM_MD2:
119 	case CKM_MD2_HMAC:
120 	case CKM_MD2_HMAC_GENERAL:
121 	case CKM_MD5:
122 	case CKM_MD5_HMAC:
123 	case CKM_MD5_HMAC_GENERAL:
124 	case CKM_SHA_1:
125 	case CKM_SHA_1_HMAC:
126 	case CKM_SHA_1_HMAC_GENERAL:
127 	case CKM_SHA256:
128 	case CKM_SHA256_HMAC:
129 	case CKM_SHA256_HMAC_GENERAL:
130 	case CKM_SHA384:
131 	case CKM_SHA384_HMAC:
132 	case CKM_SHA384_HMAC_GENERAL:
133 	case CKM_SHA512:
134 	case CKM_SHA512_HMAC:
135 	case CKM_SHA512_HMAC_GENERAL:
136 	case CKM_GENERIC_SECRET_KEY_GEN:
137 	case CKM_FASTHASH:
138 	case CKM_PKCS5_PBKD2:
139 	case CKM_PBA_SHA1_WITH_SHA1_HMAC:
140 		*gen_mech = CKM_GENERIC_SECRET_KEY_GEN;
141 		break;
142 
143 	case CKM_SSL3_MD5_MAC:
144 	case CKM_SSL3_SHA1_MAC:
145 	case CKM_SSL3_PRE_MASTER_KEY_GEN:
146 	case CKM_SSL3_MASTER_KEY_DERIVE:
147 	case CKM_SSL3_KEY_AND_MAC_DERIVE:
148 	case CKM_SSL3_MASTER_KEY_DERIVE_DH:
149 		*gen_mech = CKM_SSL3_PRE_MASTER_KEY_GEN;
150 		break;
151 
152 	case CKM_TLS_PRE_MASTER_KEY_GEN:
153 	case CKM_TLS_MASTER_KEY_DERIVE:
154 	case CKM_TLS_KEY_AND_MAC_DERIVE:
155 	case CKM_TLS_MASTER_KEY_DERIVE_DH:
156 	case CKM_TLS_PRF:
157 		*gen_mech = CKM_TLS_PRE_MASTER_KEY_GEN;
158 		break;
159 
160 	case CKM_WTLS_PRE_MASTER_KEY_GEN:
161 	case CKM_WTLS_MASTER_KEY_DERIVE:
162 	case CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC:
163 	case CKM_WTLS_PRF:
164 	case CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE:
165 	case CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE:
166 		*gen_mech = CKM_WTLS_PRE_MASTER_KEY_GEN;
167 		break;
168 
169 	case CKM_CONCATENATE_BASE_AND_KEY:
170 	case CKM_CONCATENATE_BASE_AND_DATA:
171 	case CKM_CONCATENATE_DATA_AND_BASE:
172 	case CKM_XOR_BASE_AND_DATA:
173 	case CKM_EXTRACT_KEY_FROM_KEY:
174 	case CKM_RIPEMD128:
175 	case CKM_RIPEMD128_HMAC:
176 	case CKM_RIPEMD128_HMAC_GENERAL:
177 	case CKM_RIPEMD160:
178 	case CKM_RIPEMD160_HMAC:
179 	case CKM_RIPEMD160_HMAC_GENERAL:
180 	case CKM_SHA1_KEY_DERIVATION:
181 	case CKM_SHA256_KEY_DERIVATION:
182 	case CKM_SHA384_KEY_DERIVATION:
183 	case CKM_SHA512_KEY_DERIVATION:
184 	case CKM_MD5_KEY_DERIVATION:
185 	case CKM_MD2_KEY_DERIVATION:
186 	/* not sure the following 2 should be CKK_DES or not */
187 	case CKM_KEY_WRAP_LYNKS: /* wrap/unwrap secret key w/ DES key */
188 	case CKM_KEY_WRAP_SET_OAEP:  /* wrap/unwarp DES key w/ RSA key */
189 		*gen_mech = CKM_GENERIC_SECRET_KEY_GEN;
190 		break;
191 
192 	case CKM_RC2_KEY_GEN:
193 	case CKM_RC2_ECB:
194 	case CKM_RC2_CBC:
195 	case CKM_RC2_MAC:
196 	case CKM_RC2_MAC_GENERAL:
197 	case CKM_RC2_CBC_PAD:
198 	case CKM_PBE_SHA1_RC2_128_CBC:
199 	case CKM_PBE_SHA1_RC2_40_CBC:
200 		*gen_mech = CKM_RC2_KEY_GEN;
201 		break;
202 
203 	case CKM_RC4_KEY_GEN:
204 	case CKM_RC4:
205 	case CKM_PBE_SHA1_RC4_128:
206 	case CKM_PBE_SHA1_RC4_40:
207 		*gen_mech = CKM_RC4_KEY_GEN;
208 		break;
209 
210 	case CKM_DES_KEY_GEN:
211 	case CKM_DES_ECB:
212 	case CKM_DES_CBC:
213 	case CKM_DES_MAC:
214 	case CKM_DES_MAC_GENERAL:
215 	case CKM_DES_CBC_PAD:
216 	case CKM_PBE_MD2_DES_CBC:
217 	case CKM_PBE_MD5_DES_CBC:
218 	case CKM_DES_OFB64:
219 	case CKM_DES_OFB8:
220 	case CKM_DES_CFB64:
221 	case CKM_DES_CFB8:
222 	case CKM_DES_ECB_ENCRYPT_DATA:
223 	case CKM_DES_CBC_ENCRYPT_DATA:
224 		*gen_mech = CKM_DES_KEY_GEN;
225 		break;
226 
227 	case CKM_DES2_KEY_GEN:
228 	case CKM_PBE_SHA1_DES2_EDE_CBC:
229 		*gen_mech = CKM_DES2_KEY_GEN;
230 		break;
231 
232 	case CKM_DES3_KEY_GEN:
233 	case CKM_DES3_ECB:
234 	case CKM_DES3_CBC:
235 	case CKM_DES3_MAC:
236 	case CKM_DES3_MAC_GENERAL:
237 	case CKM_DES3_CBC_PAD:
238 	case CKM_PBE_SHA1_DES3_EDE_CBC:
239 	case CKM_DES3_ECB_ENCRYPT_DATA:
240 	case CKM_DES3_CBC_ENCRYPT_DATA:
241 		*gen_mech = CKM_DES3_KEY_GEN;
242 		break;
243 
244 	case CKM_CAST_KEY_GEN:
245 	case CKM_CAST_ECB:
246 	case CKM_CAST_CBC:
247 	case CKM_CAST_MAC:
248 	case CKM_CAST_MAC_GENERAL:
249 	case CKM_CAST_CBC_PAD:
250 	case CKM_PBE_MD5_CAST_CBC:
251 		*gen_mech = CKM_CAST_KEY_GEN;
252 		break;
253 
254 	case CKM_CAST3_KEY_GEN:
255 	case CKM_CAST3_ECB:
256 	case CKM_CAST3_CBC:
257 	case CKM_CAST3_MAC:
258 	case CKM_CAST3_MAC_GENERAL:
259 	case CKM_CAST3_CBC_PAD:
260 	case CKM_PBE_MD5_CAST3_CBC:
261 		*gen_mech = CKM_CAST3_KEY_GEN;
262 		break;
263 
264 	case CKM_CAST128_KEY_GEN:
265 	case CKM_CAST128_ECB:
266 	case CKM_CAST128_CBC:
267 	case CKM_CAST128_MAC:
268 	case CKM_CAST128_MAC_GENERAL:
269 	case CKM_CAST128_CBC_PAD:
270 	case CKM_PBE_MD5_CAST128_CBC:
271 	case CKM_PBE_SHA1_CAST128_CBC:
272 		*gen_mech = CKM_CAST128_KEY_GEN;
273 		break;
274 
275 	case CKM_RC5_KEY_GEN:
276 	case CKM_RC5_ECB:
277 	case CKM_RC5_CBC:
278 	case CKM_RC5_MAC:
279 	case CKM_RC5_MAC_GENERAL:
280 	case CKM_RC5_CBC_PAD:
281 		*gen_mech = CKM_RC5_KEY_GEN;
282 		break;
283 
284 	case CKM_IDEA_KEY_GEN:
285 	case CKM_IDEA_ECB:
286 	case CKM_IDEA_CBC:
287 	case CKM_IDEA_MAC:
288 	case CKM_IDEA_MAC_GENERAL:
289 	case CKM_IDEA_CBC_PAD:
290 		*gen_mech = CKM_IDEA_KEY_GEN;
291 		break;
292 
293 	case CKM_SKIPJACK_KEY_GEN:
294 	case CKM_SKIPJACK_ECB64:
295 	case CKM_SKIPJACK_CBC64:
296 	case CKM_SKIPJACK_OFB64:
297 	case CKM_SKIPJACK_CFB64:
298 	case CKM_SKIPJACK_CFB32:
299 	case CKM_SKIPJACK_CFB16:
300 	case CKM_SKIPJACK_CFB8:
301 	case CKM_SKIPJACK_WRAP:
302 	case CKM_SKIPJACK_PRIVATE_WRAP:
303 	case CKM_SKIPJACK_RELAYX:
304 		*gen_mech = CKM_SKIPJACK_KEY_GEN;
305 		break;
306 
307 	case CKM_BATON_KEY_GEN:
308 	case CKM_BATON_ECB128:
309 	case CKM_BATON_ECB96:
310 	case CKM_BATON_CBC128:
311 	case CKM_BATON_COUNTER:
312 	case CKM_BATON_SHUFFLE:
313 	case CKM_BATON_WRAP:
314 		*gen_mech = CKM_BATON_KEY_GEN;
315 		break;
316 
317 	case CKM_JUNIPER_KEY_GEN:
318 	case CKM_JUNIPER_ECB128:
319 	case CKM_JUNIPER_CBC128:
320 	case CKM_JUNIPER_COUNTER:
321 	case CKM_JUNIPER_SHUFFLE:
322 	case CKM_JUNIPER_WRAP:
323 		*gen_mech = CKM_JUNIPER_KEY_GEN;
324 		break;
325 
326 	case CKM_CDMF_KEY_GEN:
327 	case CKM_CDMF_ECB:
328 	case CKM_CDMF_CBC:
329 	case CKM_CDMF_MAC:
330 	case CKM_CDMF_MAC_GENERAL:
331 	case CKM_CDMF_CBC_PAD:
332 		*gen_mech = CKM_CDMF_KEY_GEN;
333 		break;
334 
335 	case CKM_AES_KEY_GEN:
336 	case CKM_AES_ECB:
337 	case CKM_AES_CBC:
338 	case CKM_AES_MAC:
339 	case CKM_AES_MAC_GENERAL:
340 	case CKM_AES_CBC_PAD:
341 	case CKM_AES_ECB_ENCRYPT_DATA:
342 	case CKM_AES_CBC_ENCRYPT_DATA:
343 		*gen_mech = CKM_AES_KEY_GEN;
344 		break;
345 
346 	case CKM_BLOWFISH_KEY_GEN:
347 	case CKM_BLOWFISH_CBC:
348 		*gen_mech = CKM_BLOWFISH_KEY_GEN;
349 		break;
350 
351 	case CKM_TWOFISH_KEY_GEN:
352 	case CKM_TWOFISH_CBC:
353 		*gen_mech = CKM_TWOFISH_KEY_GEN;
354 		break;
355 
356 	default:
357 		return (CKR_MECHANISM_INVALID);
358 		break;
359 	}
360 
361 	return (CKR_OK);
362 }
363