17c478bdstevel@tonic-gate/*
2d7bec57Shawn Emery * Copyright 2009 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 * lib/crypto/keyhash_provider/hmac_md5.c
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate(I don't know)
107c478bdstevel@tonic-gate.
117c478bdstevel@tonic-gate * Copyright2001 by the Massachusetts Institute of Technology.
127c478bdstevel@tonic-gate * All Rights Reserved.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * Export of this software from the United States of America may
157c478bdstevel@tonic-gate *   require a specific license from the United States Government.
167c478bdstevel@tonic-gate *   It is the responsibility of any person or organization contemplating
177c478bdstevel@tonic-gate *   export to obtain such a license before exporting.
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bdstevel@tonic-gate * distribute this software and its documentation for any purpose and
217c478bdstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
227c478bdstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
237c478bdstevel@tonic-gate * this permission notice appear in supporting documentation, and that
247c478bdstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bdstevel@tonic-gate * to distribution of the software without specific, written prior
267c478bdstevel@tonic-gate * permission.  Furthermore if you modify this software you must label
277c478bdstevel@tonic-gate * your software as modified software and not distribute it in such a
287c478bdstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
297c478bdstevel@tonic-gate * M.I.T. makes no representations about the suitability of
307c478bdstevel@tonic-gate * this software for any purpose.  It is provided "as is" without express
317c478bdstevel@tonic-gate * or implied warranty.
327c478bdstevel@tonic-gate *
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate* Implementation of the Microsoft hmac-md5 checksum type.
357c478bdstevel@tonic-gate* Implemented based on draft-brezak-win2k-krb-rc4-hmac-03
367c478bdstevel@tonic-gate */
377c478bdstevel@tonic-gate
38159d09aMark Phalan#include "k5-int.h"
39159d09aMark Phalan#include "keyhash_provider.h"
40159d09aMark Phalan#include "arcfour.h"
41159d09aMark Phalan#include "hash_provider.h"
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gatestatic  krb5_error_code
447c478bdstevel@tonic-gatek5_hmac_md5_hash (krb5_context context,
457c478bdstevel@tonic-gate	const krb5_keyblock *key, krb5_keyusage usage,
467c478bdstevel@tonic-gate	const krb5_data *iv,
477c478bdstevel@tonic-gate	const krb5_data *input, krb5_data *output)
487c478bdstevel@tonic-gate{
497c478bdstevel@tonic-gate  krb5_keyusage ms_usage;
507c478bdstevel@tonic-gate  krb5_error_code ret;
517c478bdstevel@tonic-gate  krb5_keyblock ks;
527c478bdstevel@tonic-gate  krb5_data ds, ks_constant, md5tmp;
537c478bdstevel@tonic-gate  krb5_data hash_input[2];
547c478bdstevel@tonic-gate  char digest[MD5_CKSUM_LENGTH];
557c478bdstevel@tonic-gate  char t[4];
567c478bdstevel@tonic-gate  CK_MECHANISM mechanism;
577c478bdstevel@tonic-gate  CK_RV rv;
587c478bdstevel@tonic-gate  CK_ULONG hashlen;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate  bzero(&ks, sizeof(krb5_keyblock));
61d7bec57Shawn Emery
62d7bec57Shawn Emery  /*
63d7bec57Shawn Emery   * Solaris Kerberos: The digest length is that of MD5_CKSUM_LENGTH not the key
64d7bec57Shawn Emery   * length, as keys can be of varying lengths but should not affect the digest
65d7bec57Shawn Emery   * length.  The signing key is the digest and therefore is also the same
66d7bec57Shawn Emery   * length, MD5_CKSUM_LENGTH.
67d7bec57Shawn Emery   */
68d7bec57Shawn Emery  ds.length = MD5_CKSUM_LENGTH;
69d7bec57Shawn Emery  ks.length = MD5_CKSUM_LENGTH;
707c478bdstevel@tonic-gate  ds.data = malloc(ds.length);
717c478bdstevel@tonic-gate  if (ds.data == NULL)
72159d09aMark Phalan    return ENOMEM;
737c478bdstevel@tonic-gate  ks.contents = (void *) ds.data;
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate  ks_constant.data = "signaturekey";
767c478bdstevel@tonic-gate  ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
777c478bdstevel@tonic-gate
78159d09aMark Phalan  /* Solaris Kerberos */
797c478bdstevel@tonic-gate  ret = krb5_hmac(context, &krb5int_hash_md5, key, 1,
80159d09aMark Phalan		   &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
90159d09aMark Phalan  /* Solaris Kerberos */
917c478bdstevel@tonic-gate  mechanism.mechanism = CKM_MD5;
927c478bdstevel@tonic-gate  mechanism.pParameter = NULL_PTR;
937c478bdstevel@tonic-gate  mechanism.ulParameterLen = 0;
947c478bdstevel@tonic-gate
957c478bdstevel@tonic-gate  if ((rv = C_DigestInit(krb_ctx_hSession(context), &mechanism)) != CKR_OK) {
967c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestInit failed in k5_md5_hmac_hash: "
977c478bdstevel@tonic-gate	"rv = 0x%x.", rv);
987c478bdstevel@tonic-gate	ret = PKCS_ERR;
997c478bdstevel@tonic-gate	goto cleanup;
1007c478bdstevel@tonic-gate  }
1017c478bdstevel@tonic-gate  if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
1027c478bdstevel@tonic-gate	(CK_BYTE_PTR)t, (CK_ULONG)sizeof(t))) != CKR_OK) {
1037c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
1047c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1057c478bdstevel@tonic-gate	ret = PKCS_ERR;
1067c478bdstevel@tonic-gate	goto cleanup;
1077c478bdstevel@tonic-gate  }
1087c478bdstevel@tonic-gate  if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
1097c478bdstevel@tonic-gate	(CK_BYTE_PTR)input->data, (CK_ULONG)input->length)) != CKR_OK) {
1107c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
1117c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1127c478bdstevel@tonic-gate	ret = PKCS_ERR;
1137c478bdstevel@tonic-gate	goto cleanup;
1147c478bdstevel@tonic-gate  }
1157c478bdstevel@tonic-gate  hashlen = MD5_CKSUM_LENGTH;
1167c478bdstevel@tonic-gate  if ((rv = C_DigestFinal(krb_ctx_hSession(context),
1177c478bdstevel@tonic-gate	(CK_BYTE_PTR)digest, (CK_ULONG_PTR)&hashlen)) != CKR_OK) {
1187c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "C_DigestFinal failed in k5_md5_hmac_hash: "
1197c478bdstevel@tonic-gate            "rv = 0x%x", rv);
1207c478bdstevel@tonic-gate	ret = PKCS_ERR;
1217c478bdstevel@tonic-gate	goto cleanup;
1227c478bdstevel@tonic-gate  }
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate  md5tmp.data = digest;
1257c478bdstevel@tonic-gate  md5tmp.length = hashlen;
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate  ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
1287c478bdstevel@tonic-gate
1297c478bdstevel@tonic-gatecleanup:
1307c478bdstevel@tonic-gate  bzero(ks.contents, ks.length);
1317c478bdstevel@tonic-gate  bzero(md5tmp.data, md5tmp.length);
1327c478bdstevel@tonic-gate  FREE (ks.contents, ks.length);
1337c478bdstevel@tonic-gate  return (ret);
1347c478bdstevel@tonic-gate}
1357c478bdstevel@tonic-gate
136159d09aMark Phalan
137159d09aMark Phalan
138159d09aMark Phalanconst struct krb5_keyhash_provider krb5int_keyhash_hmac_md5 = {
139159d09aMark Phalan  16,
140159d09aMark Phalan  k5_hmac_md5_hash,
141159d09aMark Phalan  NULL /*checksum  again*/
1427c478bdstevel@tonic-gate};
1437c478bdstevel@tonic-gate
144