xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/keyhash_provider/k_hmac_md5.c (revision d7bec57c3803769d0e8bf1960016b866617d455c)
17c478bd9Sstevel@tonic-gate /*
2*d7bec57cSShawn Emery  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * lib/crypto/keyhash_provider/hmac_md5.c
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate (I don't know)
107c478bd9Sstevel@tonic-gate .
117c478bd9Sstevel@tonic-gate  * Copyright2001 by the Massachusetts Institute of Technology.
127c478bd9Sstevel@tonic-gate  * All Rights Reserved.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may
157c478bd9Sstevel@tonic-gate  *   require a specific license from the United States Government.
167c478bd9Sstevel@tonic-gate  *   It is the responsibility of any person or organization contemplating
177c478bd9Sstevel@tonic-gate  *   export to obtain such a license before exporting.
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
207c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
217c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
227c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
237c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
247c478bd9Sstevel@tonic-gate  * the name of M.I.T. not be used in advertising or publicity pertaining
257c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
267c478bd9Sstevel@tonic-gate  * permission.  Furthermore if you modify this software you must label
277c478bd9Sstevel@tonic-gate  * your software as modified software and not distribute it in such a
287c478bd9Sstevel@tonic-gate  * fashion that it might be confused with the original M.I.T. software.
297c478bd9Sstevel@tonic-gate  * M.I.T. makes no representations about the suitability of
307c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
317c478bd9Sstevel@tonic-gate  * or implied warranty.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate * Implementation of the Microsoft hmac-md5 checksum type.
357c478bd9Sstevel@tonic-gate * Implemented based on draft-brezak-win2k-krb-rc4-hmac-03
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <k5-int.h>
397c478bd9Sstevel@tonic-gate #include <etypes.h>
407c478bd9Sstevel@tonic-gate #include <keyhash_provider.h>
417c478bd9Sstevel@tonic-gate #include <arcfour.h>
427c478bd9Sstevel@tonic-gate #include <hash_provider.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
457c478bd9Sstevel@tonic-gate static  krb5_error_code
467c478bd9Sstevel@tonic-gate k5_hmac_md5_hash (krb5_context context,
477c478bd9Sstevel@tonic-gate 	const krb5_keyblock *key, krb5_keyusage usage,
487c478bd9Sstevel@tonic-gate 	const krb5_data *iv,
497c478bd9Sstevel@tonic-gate 	const krb5_data *input, krb5_data *output)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate   krb5_keyusage ms_usage;
527c478bd9Sstevel@tonic-gate   krb5_error_code ret;
537c478bd9Sstevel@tonic-gate   krb5_keyblock ks;
547c478bd9Sstevel@tonic-gate   krb5_data ds, ks_constant, md5tmp;
557c478bd9Sstevel@tonic-gate   krb5_data hash_input[2];
567c478bd9Sstevel@tonic-gate   int i;
577c478bd9Sstevel@tonic-gate   char t[4], outbuf[MD5_CKSUM_LENGTH];
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #ifdef _KERNEL
607c478bd9Sstevel@tonic-gate   KRB5_LOG1(KRB5_INFO, "k5_hmac_md5_hash() hash_mt = %ld cipher_mt = %ld",
617c478bd9Sstevel@tonic-gate 	(ulong_t) context->kef_hash_mt,
627c478bd9Sstevel@tonic-gate 	(ulong_t) context->kef_cipher_mt);
637c478bd9Sstevel@tonic-gate #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate   for (i=0; i<krb5_enctypes_length; i++) {
667c478bd9Sstevel@tonic-gate 	if (krb5_enctypes_list[i].etype == key->enctype)
677c478bd9Sstevel@tonic-gate             break;
687c478bd9Sstevel@tonic-gate   }
697c478bd9Sstevel@tonic-gate   if (i == krb5_enctypes_length) {
707c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "krb5_ck_make_checksum bad enctype: %d",
717c478bd9Sstevel@tonic-gate 		key->enctype);
727c478bd9Sstevel@tonic-gate 	return(KRB5_BAD_ENCTYPE);
737c478bd9Sstevel@tonic-gate   }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate   bzero(&ks, sizeof(krb5_keyblock));
76*d7bec57cSShawn Emery   /*
77*d7bec57cSShawn Emery    * Solaris Kerberos: The digest length is that of MD5_CKSUM_LENGTH not the key
78*d7bec57cSShawn Emery    * length, as keys can be of varying lengths but should not affect the digest
79*d7bec57cSShawn Emery    * length.  The signing key is the digest and therefore is also the same
80*d7bec57cSShawn Emery    * length, MD5_CKSUM_LENGTH.
81*d7bec57cSShawn Emery    */
82*d7bec57cSShawn Emery   ds.length = MD5_CKSUM_LENGTH;
837c478bd9Sstevel@tonic-gate   ds.data = MALLOC(ds.length);
847c478bd9Sstevel@tonic-gate   if (ds.data == NULL)
857c478bd9Sstevel@tonic-gate     return (ENOMEM);
867c478bd9Sstevel@tonic-gate   ks.contents = (void *) ds.data;
87*d7bec57cSShawn Emery   ks.length = MD5_CKSUM_LENGTH;
88*d7bec57cSShawn Emery 
897c478bd9Sstevel@tonic-gate #ifdef _KERNEL
907c478bd9Sstevel@tonic-gate   if (key->kef_key.ck_data == NULL) {
917c478bd9Sstevel@tonic-gate 	ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt,
927c478bd9Sstevel@tonic-gate 			(krb5_keyblock *)key);
937c478bd9Sstevel@tonic-gate 	if (ret)
947c478bd9Sstevel@tonic-gate 		goto cleanup;
957c478bd9Sstevel@tonic-gate   }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate   ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt, &ks);
987c478bd9Sstevel@tonic-gate   if (ret)
997c478bd9Sstevel@tonic-gate 	goto cleanup;
1007c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate   ks_constant.data = "signaturekey";
1037c478bd9Sstevel@tonic-gate   ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1067c478bd9Sstevel@tonic-gate   ret = krb5_hmac(context, (krb5_keyblock *)key, &ks_constant, &ds);
1077c478bd9Sstevel@tonic-gate #else
1087c478bd9Sstevel@tonic-gate   ret = krb5_hmac(context, &krb5int_hash_md5, key, 1, &ks_constant, &ds);
1097c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1107c478bd9Sstevel@tonic-gate   if (ret)
1117c478bd9Sstevel@tonic-gate     goto cleanup;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate   ms_usage = krb5int_arcfour_translate_usage (usage);
1147c478bd9Sstevel@tonic-gate   t[0] = (ms_usage) & 0xff;
1157c478bd9Sstevel@tonic-gate   t[1] = (ms_usage>>8) & 0xff;
1167c478bd9Sstevel@tonic-gate   t[2] = (ms_usage >>16) & 0xff;
1177c478bd9Sstevel@tonic-gate   t[3] = (ms_usage>>24) & 0XFF;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate   hash_input[0].data = (char *)&t;
1207c478bd9Sstevel@tonic-gate   hash_input[0].length = 4;
1217c478bd9Sstevel@tonic-gate   hash_input[1].data = input->data;
1227c478bd9Sstevel@tonic-gate   hash_input[1].length = input->length;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate   md5tmp.data = (void *)outbuf;
1257c478bd9Sstevel@tonic-gate   md5tmp.length = sizeof(outbuf);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate   /* Use generic hash function that calls to kEF */
1287c478bd9Sstevel@tonic-gate   if (k5_ef_hash(context, 2, hash_input, &md5tmp)) {
1297c478bd9Sstevel@tonic-gate 	return (KRB5_KEF_ERROR);
1307c478bd9Sstevel@tonic-gate   }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #ifdef _KERNEL
1337c478bd9Sstevel@tonic-gate   ret = krb5_hmac (context, &ks, &md5tmp, output);
1347c478bd9Sstevel@tonic-gate #else
1357c478bd9Sstevel@tonic-gate   ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
1367c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate cleanup:
1397c478bd9Sstevel@tonic-gate   bzero(md5tmp.data, md5tmp.length);
1407c478bd9Sstevel@tonic-gate   bzero(ks.contents, ks.length);
1417c478bd9Sstevel@tonic-gate   FREE (ks.contents, ks.length);
1427c478bd9Sstevel@tonic-gate   return (ret);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate  const struct krb5_keyhash_provider
1467c478bd9Sstevel@tonic-gate krb5int_keyhash_hmac_md5 = {
147505d05c7Sgtb                 	MD5_CKSUM_LENGTH,
1487c478bd9Sstevel@tonic-gate 			k5_hmac_md5_hash,
1497c478bd9Sstevel@tonic-gate 			NULL /*checksum  again*/
1507c478bd9Sstevel@tonic-gate 			};
1517c478bd9Sstevel@tonic-gate 
152