hmac_md5.c revision 7c478bd95313f5f23a4c958a745db2134aa0324
17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
37c478bdstevel@tonic-gate * Use is subject to license terms.
47c478bdstevel@tonic-gate */
57c478bdstevel@tonic-gate
67c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
77c478bdstevel@tonic-gate
87c478bdstevel@tonic-gate/*
97c478bdstevel@tonic-gate * lib/crypto/keyhash_provider/hmac_md5.c
107c478bdstevel@tonic-gate *
117c478bdstevel@tonic-gate(I don't know)
127c478bdstevel@tonic-gate.
137c478bdstevel@tonic-gate * Copyright2001 by the Massachusetts Institute of Technology.
147c478bdstevel@tonic-gate * All Rights Reserved.
157c478bdstevel@tonic-gate *
167c478bdstevel@tonic-gate * Export of this software from the United States of America may
177c478bdstevel@tonic-gate *   require a specific license from the United States Government.
187c478bdstevel@tonic-gate *   It is the responsibility of any person or organization contemplating
197c478bdstevel@tonic-gate *   export to obtain such a license before exporting.
207c478bdstevel@tonic-gate *
217c478bdstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
227c478bdstevel@tonic-gate * distribute this software and its documentation for any purpose and
237c478bdstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
247c478bdstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
257c478bdstevel@tonic-gate * this permission notice appear in supporting documentation, and that
267c478bdstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
277c478bdstevel@tonic-gate * to distribution of the software without specific, written prior
287c478bdstevel@tonic-gate * permission.  Furthermore if you modify this software you must label
297c478bdstevel@tonic-gate * your software as modified software and not distribute it in such a
307c478bdstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
317c478bdstevel@tonic-gate * M.I.T. makes no representations about the suitability of
327c478bdstevel@tonic-gate * this software for any purpose.  It is provided "as is" without express
337c478bdstevel@tonic-gate * or implied warranty.
347c478bdstevel@tonic-gate *
357c478bdstevel@tonic-gate *
367c478bdstevel@tonic-gate* Implementation of the Microsoft hmac-md5 checksum type.
377c478bdstevel@tonic-gate* Implemented based on draft-brezak-win2k-krb-rc4-hmac-03
387c478bdstevel@tonic-gate */
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate#include <k5-int.h>
417c478bdstevel@tonic-gate#include <arcfour.h>
427c478bdstevel@tonic-gate#include <hash_provider.h>
437c478bdstevel@tonic-gate#include <keyhash_provider.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gatestatic void
467c478bdstevel@tonic-gatek5_hmac_md5_hash_size (size_t *output)
477c478bdstevel@tonic-gate{
487c478bdstevel@tonic-gate  *output = 16;
497c478bdstevel@tonic-gate}
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gatestatic  krb5_error_code
527c478bdstevel@tonic-gatek5_hmac_md5_hash (krb5_context context,
537c478bdstevel@tonic-gate	const krb5_keyblock *key, krb5_keyusage usage,
547c478bdstevel@tonic-gate	const krb5_data *iv,
557c478bdstevel@tonic-gate	const krb5_data *input, krb5_data *output)
567c478bdstevel@tonic-gate{
577c478bdstevel@tonic-gate  krb5_keyusage ms_usage;
587c478bdstevel@tonic-gate  krb5_error_code ret;
597c478bdstevel@tonic-gate  krb5_keyblock ks;
607c478bdstevel@tonic-gate  krb5_data ds, ks_constant, md5tmp;
617c478bdstevel@tonic-gate  krb5_data hash_input[2];
627c478bdstevel@tonic-gate  char digest[MD5_CKSUM_LENGTH];
637c478bdstevel@tonic-gate  char t[4];
647c478bdstevel@tonic-gate  CK_MECHANISM mechanism;
657c478bdstevel@tonic-gate  CK_RV rv;
667c478bdstevel@tonic-gate  CK_ULONG hashlen;
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate  bzero(&ks, sizeof(krb5_keyblock));
697c478bdstevel@tonic-gate  ds.length = key->length;
707c478bdstevel@tonic-gate  ks.length = key->length;
717c478bdstevel@tonic-gate  ds.data = malloc(ds.length);
727c478bdstevel@tonic-gate  if (ds.data == NULL)
737c478bdstevel@tonic-gate    return (ENOMEM);
747c478bdstevel@tonic-gate  ks.contents = (void *) ds.data;
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate  ks_constant.data = "signaturekey";
777c478bdstevel@tonic-gate  ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate  ret = krb5_hmac(context, &krb5int_hash_md5, key, 1,
807c478bdstevel@tonic-gate		&ks_constant, &ds);
817c478bdstevel@tonic-gate  if (ret)
827c478bdstevel@tonic-gate    goto cleanup;
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate  ms_usage = krb5int_arcfour_translate_usage (usage);
857c478bdstevel@tonic-gate  t[0] = (ms_usage) & 0xff;
867c478bdstevel@tonic-gate  t[1] = (ms_usage>>8) & 0xff;
877c478bdstevel@tonic-gate  t[2] = (ms_usage >>16) & 0xff;
887c478bdstevel@tonic-gate  t[3] = (ms_usage>>24) & 0XFF;
897c478bdstevel@tonic-gate
907c478bdstevel@tonic-gate  mechanism.mechanism = CKM_MD5;
917c478bdstevel@tonic-gate  mechanism.pParameter = NULL_PTR;
927c478bdstevel@tonic-gate  mechanism.ulParameterLen = 0;
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate  if ((rv = C_DigestInit(krb_ctx_hSession(context), &mechanism)) != CKR_OK) {
957c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestInit failed in k5_md5_hmac_hash: "
967c478bdstevel@tonic-gate	"rv = 0x%x.", rv);
977c478bdstevel@tonic-gate	ret = PKCS_ERR;
987c478bdstevel@tonic-gate	goto cleanup;
997c478bdstevel@tonic-gate  }
1007c478bdstevel@tonic-gate  if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
1017c478bdstevel@tonic-gate	(CK_BYTE_PTR)t, (CK_ULONG)sizeof(t))) != CKR_OK) {
1027c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
1037c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1047c478bdstevel@tonic-gate	ret = PKCS_ERR;
1057c478bdstevel@tonic-gate	goto cleanup;
1067c478bdstevel@tonic-gate  }
1077c478bdstevel@tonic-gate  if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
1087c478bdstevel@tonic-gate	(CK_BYTE_PTR)input->data, (CK_ULONG)input->length)) != CKR_OK) {
1097c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
1107c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1117c478bdstevel@tonic-gate	ret = PKCS_ERR;
1127c478bdstevel@tonic-gate	goto cleanup;
1137c478bdstevel@tonic-gate  }
1147c478bdstevel@tonic-gate  hashlen = MD5_CKSUM_LENGTH;
1157c478bdstevel@tonic-gate  if ((rv = C_DigestFinal(krb_ctx_hSession(context),
1167c478bdstevel@tonic-gate	(CK_BYTE_PTR)digest, (CK_ULONG_PTR)&hashlen)) != CKR_OK) {
1177c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestFinal failed in k5_md5_hmac_hash: "
1187c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1197c478bdstevel@tonic-gate	ret = PKCS_ERR;
1207c478bdstevel@tonic-gate	goto cleanup;
1217c478bdstevel@tonic-gate  }
1227c478bdstevel@tonic-gate
1237c478bdstevel@tonic-gate  md5tmp.data = digest;
1247c478bdstevel@tonic-gate  md5tmp.length = hashlen;
1257c478bdstevel@tonic-gate
1267c478bdstevel@tonic-gate  ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
1277c478bdstevel@tonic-gate
1287c478bdstevel@tonic-gatecleanup:
1297c478bdstevel@tonic-gate  bzero(ks.contents, ks.length);
1307c478bdstevel@tonic-gate  bzero(md5tmp.data, md5tmp.length);
1317c478bdstevel@tonic-gate  FREE (ks.contents, ks.length);
1327c478bdstevel@tonic-gate  return (ret);
1337c478bdstevel@tonic-gate}
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gateconst struct krb5_keyhash_provider
1367c478bdstevel@tonic-gatekrb5int_keyhash_hmac_md5 = {
1377c478bdstevel@tonic-gate			k5_hmac_md5_hash_size,
1387c478bdstevel@tonic-gate			k5_hmac_md5_hash,
1397c478bdstevel@tonic-gate			NULL /*checksum  again*/
1407c478bdstevel@tonic-gate};
1417c478bdstevel@tonic-gate
142