1 /*
2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * lib/crypto/pbkdf2.c
10  *
11  * Copyright 2002 by the Massachusetts Institute of Technology.
12  * All Rights Reserved.
13  *
14  * Export of this software from the United States of America may
15  *   require a specific license from the United States Government.
16  *   It is the responsibility of any person or organization contemplating
17  *   export to obtain such a license before exporting.
18  *
19  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20  * distribute this software and its documentation for any purpose and
21  * without fee is hereby granted, provided that the above copyright
22  * notice appear in all copies and that both that copyright notice and
23  * this permission notice appear in supporting documentation, and that
24  * the name of M.I.T. not be used in advertising or publicity pertaining
25  * to distribution of the software without specific, written prior
26  * permission.  Furthermore if you modify this software you must label
27  * your software as modified software and not distribute it in such a
28  * fashion that it might be confused with the original M.I.T. software.
29  * M.I.T. makes no representations about the suitability of
30  * this software for any purpose.  It is provided "as is" without express
31  * or implied warranty.
32  *
33  *
34  * Implementation of PBKDF2 from RFC 2898.
35  * Not currently used; likely to be used when we get around to AES support.
36  */
37 
38 #ifndef _KERNEL
39 
40 #include <ctype.h>
41 #include <k5-int.h>
42 #include <hash_provider.h>
43 
44 /*
45  * MIT code ripped out, use PBKDF2 algorithm from PKCS#11
46  * provider.
47  */
48 krb5_error_code
49 krb5int_pbkdf2_hmac_sha1(
50 	krb5_context context,
51 	const krb5_data *out,
52 	unsigned long count,
53 	krb5_enctype enctype,
54 	const krb5_data *pass, const krb5_data *salt)
55 {
56 	krb5_error_code ret = 0;
57 	CK_RV rv;
58 	CK_PKCS5_PBKD2_PARAMS params;
59 	CK_MECHANISM mechanism;
60 	CK_OBJECT_CLASS class = CKO_SECRET_KEY;
61 	CK_ATTRIBUTE tmpl[3];
62 	CK_KEY_TYPE	keytype;
63 	CK_OBJECT_HANDLE hKey;
64 	int attrs = 0;
65 
66 	mechanism.mechanism = CKM_PKCS5_PBKD2;
67 	mechanism.pParameter = &params;
68 	mechanism.ulParameterLen = sizeof (params);
69 
70 	tmpl[attrs].type = CKA_CLASS;
71 	tmpl[attrs].pValue = &class;
72 	tmpl[attrs].ulValueLen = sizeof (class);
73 	attrs++;
74 
75 	rv = get_key_type(enctype, &keytype);
76 	if (rv != CKR_OK)
77 		return (PKCS_ERR);
78 
79 	tmpl[attrs].type = CKA_KEY_TYPE;
80 	tmpl[attrs].pValue = &keytype;
81 	tmpl[attrs].ulValueLen = sizeof (keytype);
82 	attrs++;
83 
84 	/*
85 	 * For DES key types, do not include the value len attr.
86 	 */
87 	if (out->length > 0 &&
88 	    enctype != ENCTYPE_DES_CBC_CRC &&
89 	    enctype != ENCTYPE_DES_CBC_MD5 &&
90 	    enctype != ENCTYPE_DES_CBC_RAW &&
91 	    enctype != ENCTYPE_DES_HMAC_SHA1 &&
92 	    enctype != ENCTYPE_DES3_CBC_SHA1 &&
93 	    enctype != ENCTYPE_DES3_CBC_RAW) {
94 		tmpl[attrs].type = CKA_VALUE_LEN;
95 		tmpl[attrs].pValue = (void *)&out->length;
96 		tmpl[attrs].ulValueLen = out->length;
97 		attrs++;
98 	}
99 
100 	params.saltSource = CKZ_SALT_SPECIFIED;
101 	params.pSaltSourceData = (void *)salt->data;
102 	params.ulSaltSourceDataLen = salt->length;
103 	params.iterations = count;
104 	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
105 	params.pPrfData = NULL;
106 	params.ulPrfDataLen = 0;
107 	params.pPassword = (CK_UTF8CHAR_PTR)pass->data;
108 	params.ulPasswordLen = (CK_ULONG *)&pass->length;
109 
110 	rv = C_GenerateKey(krb_ctx_hSession(context), &mechanism, tmpl,
111 		attrs, &hKey);
112 
113 	if (rv != CKR_OK)
114 		ret = PKCS_ERR;
115 	else {
116 		/* Get the value from the key object. */
117 		tmpl[0].type = CKA_VALUE;
118 		tmpl[0].pValue = out->data;
119 		tmpl[0].ulValueLen = out->length;
120 		rv = C_GetAttributeValue(krb_ctx_hSession(context), hKey,
121 			tmpl, 1);
122 		if (rv != CKR_OK)
123 			ret = PKCS_ERR;
124 	}
125 
126 	(void) C_DestroyObject(krb_ctx_hSession(context), hKey);
127 	return (ret);
128 }
129 #endif /* !_KERNEL */
130