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