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