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
387c478bdstevel@tonic-gate#include <k5-int.h>
397c478bdstevel@tonic-gate#include <etypes.h>
407c478bdstevel@tonic-gate#include <keyhash_provider.h>
417c478bdstevel@tonic-gate#include <arcfour.h>
427c478bdstevel@tonic-gate#include <hash_provider.h>
437c478bdstevel@tonic-gate
447c478bdstevel@tonic-gate/*ARGSUSED*/
457c478bdstevel@tonic-gatestatic  krb5_error_code
467c478bdstevel@tonic-gatek5_hmac_md5_hash (krb5_context context,
477c478bdstevel@tonic-gate	const krb5_keyblock *key, krb5_keyusage usage,
487c478bdstevel@tonic-gate	const krb5_data *iv,
497c478bdstevel@tonic-gate	const krb5_data *input, krb5_data *output)
507c478bdstevel@tonic-gate{
517c478bdstevel@tonic-gate  krb5_keyusage ms_usage;
527c478bdstevel@tonic-gate  krb5_error_code ret;
537c478bdstevel@tonic-gate  krb5_keyblock ks;
547c478bdstevel@tonic-gate  krb5_data ds, ks_constant, md5tmp;
557c478bdstevel@tonic-gate  krb5_data hash_input[2];
567c478bdstevel@tonic-gate  int i;
577c478bdstevel@tonic-gate  char t[4], outbuf[MD5_CKSUM_LENGTH];
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate#ifdef _KERNEL
607c478bdstevel@tonic-gate  KRB5_LOG1(KRB5_INFO, "k5_hmac_md5_hash() hash_mt = %ld cipher_mt = %ld",
617c478bdstevel@tonic-gate	(ulong_t) context->kef_hash_mt,
627c478bdstevel@tonic-gate	(ulong_t) context->kef_cipher_mt);
637c478bdstevel@tonic-gate#endif
647c478bdstevel@tonic-gate
657c478bdstevel@tonic-gate  for (i=0; i<krb5_enctypes_length; i++) {
667c478bdstevel@tonic-gate	if (krb5_enctypes_list[i].etype == key->enctype)
677c478bdstevel@tonic-gate            break;
687c478bdstevel@tonic-gate  }
697c478bdstevel@tonic-gate  if (i == krb5_enctypes_length) {
707c478bdstevel@tonic-gate	KRB5_LOG(KRB5_ERR, "krb5_ck_make_checksum bad enctype: %d",
717c478bdstevel@tonic-gate		key->enctype);
727c478bdstevel@tonic-gate	return(KRB5_BAD_ENCTYPE);
737c478bdstevel@tonic-gate  }
747c478bdstevel@tonic-gate
757c478bdstevel@tonic-gate  bzero(&ks, sizeof(krb5_keyblock));
76d7bec57Shawn Emery  /*
77d7bec57Shawn Emery   * Solaris Kerberos: The digest length is that of MD5_CKSUM_LENGTH not the key
78d7bec57Shawn Emery   * length, as keys can be of varying lengths but should not affect the digest
79d7bec57Shawn Emery   * length.  The signing key is the digest and therefore is also the same
80d7bec57Shawn Emery   * length, MD5_CKSUM_LENGTH.
81d7bec57Shawn Emery   */
82d7bec57Shawn Emery  ds.length = MD5_CKSUM_LENGTH;
837c478bdstevel@tonic-gate  ds.data = MALLOC(ds.length);
847c478bdstevel@tonic-gate  if (ds.data == NULL)
857c478bdstevel@tonic-gate    return (ENOMEM);
867c478bdstevel@tonic-gate  ks.contents = (void *) ds.data;
87d7bec57Shawn Emery  ks.length = MD5_CKSUM_LENGTH;
88d7bec57Shawn Emery
897c478bdstevel@tonic-gate#ifdef _KERNEL
907c478bdstevel@tonic-gate  if (key->kef_key.ck_data == NULL) {
917c478bdstevel@tonic-gate	ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt,
927c478bdstevel@tonic-gate			(krb5_keyblock *)key);
937c478bdstevel@tonic-gate	if (ret)
947c478bdstevel@tonic-gate		goto cleanup;
957c478bdstevel@tonic-gate  }
967c478bdstevel@tonic-gate
977c478bdstevel@tonic-gate  ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt, &ks);
987c478bdstevel@tonic-gate  if (ret)
997c478bdstevel@tonic-gate	goto cleanup;
1007c478bdstevel@tonic-gate#endif /* _KERNEL */
1017c478bdstevel@tonic-gate
1027c478bdstevel@tonic-gate  ks_constant.data = "signaturekey";
1037c478bdstevel@tonic-gate  ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
1047c478bdstevel@tonic-gate
1057c478bdstevel@tonic-gate#ifdef _KERNEL
1067c478bdstevel@tonic-gate  ret = krb5_hmac(context, (krb5_keyblock *)key, &ks_constant, &ds);
1077c478bdstevel@tonic-gate#else
1087c478bdstevel@tonic-gate  ret = krb5_hmac(context, &krb5int_hash_md5, key, 1, &ks_constant, &ds);
1097c478bdstevel@tonic-gate#endif /* _KERNEL */
1107c478bdstevel@tonic-gate  if (ret)
1117c478bdstevel@tonic-gate    goto cleanup;
1127c478bdstevel@tonic-gate
1137c478bdstevel@tonic-gate  ms_usage = krb5int_arcfour_translate_usage (usage);
1147c478bdstevel@tonic-gate  t[0] = (ms_usage) & 0xff;
1157c478bdstevel@tonic-gate  t[1] = (ms_usage>>8) & 0xff;
1167c478bdstevel@tonic-gate  t[2] = (ms_usage >>16) & 0xff;
1177c478bdstevel@tonic-gate  t[3] = (ms_usage>>24) & 0XFF;
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate  hash_input[0].data = (char *)&t;
1207c478bdstevel@tonic-gate  hash_input[0].length = 4;
1217c478bdstevel@tonic-gate  hash_input[1].data = input->data;
1227c478bdstevel@tonic-gate  hash_input[1].length = input->length;
1237c478bdstevel@tonic-gate
1247c478bdstevel@tonic-gate  md5tmp.data = (void *)outbuf;
1257c478bdstevel@tonic-gate  md5tmp.length = sizeof(outbuf);
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate  /* Use generic hash function that calls to kEF */
1287c478bdstevel@tonic-gate  if (k5_ef_hash(context, 2, hash_input, &md5tmp)) {
1297c478bdstevel@tonic-gate	return (KRB5_KEF_ERROR);
1307c478bdstevel@tonic-gate  }
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate#ifdef _KERNEL
1337c478bdstevel@tonic-gate  ret = krb5_hmac (context, &ks, &md5tmp, output);
1347c478bdstevel@tonic-gate#else
1357c478bdstevel@tonic-gate  ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
1367c478bdstevel@tonic-gate#endif /* _KERNEL */
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gatecleanup:
1397c478bdstevel@tonic-gate  bzero(md5tmp.data, md5tmp.length);
1407c478bdstevel@tonic-gate  bzero(ks.contents, ks.length);
1417c478bdstevel@tonic-gate  FREE (ks.contents, ks.length);
1427c478bdstevel@tonic-gate  return (ret);
1437c478bdstevel@tonic-gate}
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate const struct krb5_keyhash_provider
1467c478bdstevel@tonic-gatekrb5int_keyhash_hmac_md5 = {
147505d05cgtb                	MD5_CKSUM_LENGTH,
1487c478bdstevel@tonic-gate			k5_hmac_md5_hash,
1497c478bdstevel@tonic-gate			NULL /*checksum  again*/
1507c478bdstevel@tonic-gate			};
1517c478bdstevel@tonic-gate
152