xref: /onnv-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/pbkdf2.c (revision 8090:881094b90540)
10Sstevel@tonic-gate /*
27934SMark.Phalan@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * lib/crypto/pbkdf2.c
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * Copyright 2002 by the Massachusetts Institute of Technology.
100Sstevel@tonic-gate  * All Rights Reserved.
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * Export of this software from the United States of America may
130Sstevel@tonic-gate  *   require a specific license from the United States Government.
140Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
150Sstevel@tonic-gate  *   export to obtain such a license before exporting.
16*8090SMark.Phalan@Sun.COM  *
170Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
180Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
190Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
200Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
210Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
220Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
230Sstevel@tonic-gate  * to distribution of the software without specific, written prior
240Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
250Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
260Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
270Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
280Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
290Sstevel@tonic-gate  * or implied warranty.
30*8090SMark.Phalan@Sun.COM  *
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * Implementation of PBKDF2 from RFC 2898.
330Sstevel@tonic-gate  * Not currently used; likely to be used when we get around to AES support.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifndef _KERNEL
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <ctype.h>
397934SMark.Phalan@Sun.COM #include "k5-int.h"
407934SMark.Phalan@Sun.COM #include "hash_provider.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
437934SMark.Phalan@Sun.COM  * Solaris Kerberos:
440Sstevel@tonic-gate  * MIT code ripped out, use PBKDF2 algorithm from PKCS#11
450Sstevel@tonic-gate  * provider.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate krb5_error_code
krb5int_pbkdf2_hmac_sha1(krb5_context context,const krb5_data * out,unsigned long count,krb5_enctype enctype,const krb5_data * pass,const krb5_data * salt)480Sstevel@tonic-gate krb5int_pbkdf2_hmac_sha1(
490Sstevel@tonic-gate 	krb5_context context,
500Sstevel@tonic-gate 	const krb5_data *out,
510Sstevel@tonic-gate 	unsigned long count,
520Sstevel@tonic-gate 	krb5_enctype enctype,
530Sstevel@tonic-gate 	const krb5_data *pass, const krb5_data *salt)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	krb5_error_code ret = 0;
560Sstevel@tonic-gate 	CK_RV rv;
570Sstevel@tonic-gate 	CK_PKCS5_PBKD2_PARAMS params;
580Sstevel@tonic-gate 	CK_MECHANISM mechanism;
590Sstevel@tonic-gate 	CK_OBJECT_CLASS class = CKO_SECRET_KEY;
600Sstevel@tonic-gate 	CK_ATTRIBUTE tmpl[3];
610Sstevel@tonic-gate 	CK_KEY_TYPE	keytype;
620Sstevel@tonic-gate 	CK_OBJECT_HANDLE hKey;
630Sstevel@tonic-gate 	int attrs = 0;
64279Swillf 	CK_ULONG outlen, passlen;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	mechanism.mechanism = CKM_PKCS5_PBKD2;
670Sstevel@tonic-gate 	mechanism.pParameter = &params;
680Sstevel@tonic-gate 	mechanism.ulParameterLen = sizeof (params);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	tmpl[attrs].type = CKA_CLASS;
710Sstevel@tonic-gate 	tmpl[attrs].pValue = &class;
720Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (class);
730Sstevel@tonic-gate 	attrs++;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	rv = get_key_type(enctype, &keytype);
760Sstevel@tonic-gate 	if (rv != CKR_OK)
770Sstevel@tonic-gate 		return (PKCS_ERR);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	tmpl[attrs].type = CKA_KEY_TYPE;
800Sstevel@tonic-gate 	tmpl[attrs].pValue = &keytype;
810Sstevel@tonic-gate 	tmpl[attrs].ulValueLen = sizeof (keytype);
820Sstevel@tonic-gate 	attrs++;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	/*
850Sstevel@tonic-gate 	 * For DES key types, do not include the value len attr.
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	if (out->length > 0 &&
880Sstevel@tonic-gate 	    enctype != ENCTYPE_DES_CBC_CRC &&
890Sstevel@tonic-gate 	    enctype != ENCTYPE_DES_CBC_MD5 &&
900Sstevel@tonic-gate 	    enctype != ENCTYPE_DES_CBC_RAW &&
910Sstevel@tonic-gate 	    enctype != ENCTYPE_DES_HMAC_SHA1 &&
920Sstevel@tonic-gate 	    enctype != ENCTYPE_DES3_CBC_SHA1 &&
930Sstevel@tonic-gate 	    enctype != ENCTYPE_DES3_CBC_RAW) {
940Sstevel@tonic-gate 		tmpl[attrs].type = CKA_VALUE_LEN;
95279Swillf 		/* using outlen to avoid 64bit alignment issues */
96279Swillf 		outlen = (CK_ULONG)out->length;
97279Swillf 		tmpl[attrs].pValue = &outlen;
98*8090SMark.Phalan@Sun.COM 		tmpl[attrs].ulValueLen = sizeof (outlen);
990Sstevel@tonic-gate 		attrs++;
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	params.saltSource = CKZ_SALT_SPECIFIED;
1030Sstevel@tonic-gate 	params.pSaltSourceData = (void *)salt->data;
1040Sstevel@tonic-gate 	params.ulSaltSourceDataLen = salt->length;
1050Sstevel@tonic-gate 	params.iterations = count;
1060Sstevel@tonic-gate 	params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
1070Sstevel@tonic-gate 	params.pPrfData = NULL;
1080Sstevel@tonic-gate 	params.ulPrfDataLen = 0;
1090Sstevel@tonic-gate 	params.pPassword = (CK_UTF8CHAR_PTR)pass->data;
110279Swillf 	/* using passlen to avoid 64bit alignment issues */
111279Swillf 	passlen = (CK_ULONG)pass->length;
112279Swillf 	params.ulPasswordLen = &passlen;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	rv = C_GenerateKey(krb_ctx_hSession(context), &mechanism, tmpl,
115*8090SMark.Phalan@Sun.COM 	    attrs, &hKey);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (rv != CKR_OK)
1180Sstevel@tonic-gate 		ret = PKCS_ERR;
1190Sstevel@tonic-gate 	else {
1200Sstevel@tonic-gate 		/* Get the value from the key object. */
1210Sstevel@tonic-gate 		tmpl[0].type = CKA_VALUE;
1220Sstevel@tonic-gate 		tmpl[0].pValue = out->data;
1230Sstevel@tonic-gate 		tmpl[0].ulValueLen = out->length;
1240Sstevel@tonic-gate 		rv = C_GetAttributeValue(krb_ctx_hSession(context), hKey,
125*8090SMark.Phalan@Sun.COM 		    tmpl, 1);
1260Sstevel@tonic-gate 		if (rv != CKR_OK)
1270Sstevel@tonic-gate 			ret = PKCS_ERR;
128*8090SMark.Phalan@Sun.COM 		(void) C_DestroyObject(krb_ctx_hSession(context), hKey);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	return (ret);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate #endif /* !_KERNEL */
134