2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * lib/crypto/pbkdf2.c
2N/A *
2N/A * Copyright 2002 by the Massachusetts Institute of Technology.
2N/A * All Rights Reserved.
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
2N/A * distribute this software and its documentation for any purpose and
2N/A * without fee is hereby granted, provided that the above copyright
2N/A * notice appear in all copies and that both that copyright notice and
2N/A * this permission notice appear in supporting documentation, and that
2N/A * the name of M.I.T. not be used in advertising or publicity pertaining
2N/A * to distribution of the software without specific, written prior
2N/A * permission. Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A * M.I.T. makes no representations about the suitability of
2N/A * this software for any purpose. It is provided "as is" without express
2N/A * or implied warranty.
2N/A *
2N/A *
2N/A * Implementation of PBKDF2 from RFC 2898.
2N/A * Not currently used; likely to be used when we get around to AES support.
2N/A */
2N/A
2N/A#ifndef _KERNEL
2N/A
2N/A#include <ctype.h>
2N/A#include "k5-int.h"
2N/A#include "hash_provider.h"
2N/A
2N/A/*
2N/A * Solaris Kerberos:
2N/A * MIT code ripped out, use PBKDF2 algorithm from PKCS#11
2N/A * provider.
2N/A */
2N/Akrb5_error_code
2N/Akrb5int_pbkdf2_hmac_sha1(
2N/A krb5_context context,
2N/A const krb5_data *out,
2N/A unsigned long count,
2N/A krb5_enctype enctype,
2N/A const krb5_data *pass, const krb5_data *salt)
2N/A{
2N/A krb5_error_code ret = 0;
2N/A CK_RV rv;
2N/A CK_PKCS5_PBKD2_PARAMS params;
2N/A CK_MECHANISM mechanism;
2N/A CK_OBJECT_CLASS class = CKO_SECRET_KEY;
2N/A CK_ATTRIBUTE tmpl[3];
2N/A CK_KEY_TYPE keytype;
2N/A CK_OBJECT_HANDLE hKey;
2N/A int attrs = 0;
2N/A CK_ULONG outlen, passlen;
2N/A
2N/A mechanism.mechanism = CKM_PKCS5_PBKD2;
2N/A mechanism.pParameter = &params;
2N/A mechanism.ulParameterLen = sizeof (params);
2N/A
2N/A tmpl[attrs].type = CKA_CLASS;
2N/A tmpl[attrs].pValue = &class;
2N/A tmpl[attrs].ulValueLen = sizeof (class);
2N/A attrs++;
2N/A
2N/A rv = get_key_type(enctype, &keytype);
2N/A if (rv != CKR_OK)
2N/A return (PKCS_ERR);
2N/A
2N/A tmpl[attrs].type = CKA_KEY_TYPE;
2N/A tmpl[attrs].pValue = &keytype;
2N/A tmpl[attrs].ulValueLen = sizeof (keytype);
2N/A attrs++;
2N/A
2N/A /*
2N/A * For DES key types, do not include the value len attr.
2N/A */
2N/A if (out->length > 0 &&
2N/A enctype != ENCTYPE_DES_CBC_CRC &&
2N/A enctype != ENCTYPE_DES_CBC_MD5 &&
2N/A enctype != ENCTYPE_DES_CBC_RAW &&
2N/A enctype != ENCTYPE_DES_HMAC_SHA1 &&
2N/A enctype != ENCTYPE_DES3_CBC_SHA1 &&
2N/A enctype != ENCTYPE_DES3_CBC_RAW) {
2N/A tmpl[attrs].type = CKA_VALUE_LEN;
2N/A /* using outlen to avoid 64bit alignment issues */
2N/A outlen = (CK_ULONG)out->length;
2N/A tmpl[attrs].pValue = &outlen;
2N/A tmpl[attrs].ulValueLen = sizeof (outlen);
2N/A attrs++;
2N/A }
2N/A
2N/A params.saltSource = CKZ_SALT_SPECIFIED;
2N/A params.pSaltSourceData = (void *)salt->data;
2N/A params.ulSaltSourceDataLen = salt->length;
2N/A params.iterations = count;
2N/A params.prf = CKP_PKCS5_PBKD2_HMAC_SHA1;
2N/A params.pPrfData = NULL;
2N/A params.ulPrfDataLen = 0;
2N/A params.pPassword = (CK_UTF8CHAR_PTR)pass->data;
2N/A /* using passlen to avoid 64bit alignment issues */
2N/A passlen = (CK_ULONG)pass->length;
2N/A params.ulPasswordLen = &passlen;
2N/A
2N/A rv = C_GenerateKey(krb_ctx_hSession(context), &mechanism, tmpl,
2N/A attrs, &hKey);
2N/A
2N/A if (rv != CKR_OK)
2N/A ret = PKCS_ERR;
2N/A else {
2N/A /* Get the value from the key object. */
2N/A tmpl[0].type = CKA_VALUE;
2N/A tmpl[0].pValue = out->data;
2N/A tmpl[0].ulValueLen = out->length;
2N/A rv = C_GetAttributeValue(krb_ctx_hSession(context), hKey,
2N/A tmpl, 1);
2N/A if (rv != CKR_OK)
2N/A ret = PKCS_ERR;
2N/A (void) C_DestroyObject(krb_ctx_hSession(context), hKey);
2N/A }
2N/A
2N/A return (ret);
2N/A}
2N/A#endif /* !_KERNEL */