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