xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/keyhash_provider/k_hmac_md5.c (revision 505d05c73a6e56769f263d4803b22eddd168ee24)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 /*
9  * lib/crypto/keyhash_provider/hmac_md5.c
10  *
11 (I don't know)
12 .
13  * Copyright2001 by the Massachusetts Institute of Technology.
14  * All Rights Reserved.
15  *
16  * Export of this software from the United States of America may
17  *   require a specific license from the United States Government.
18  *   It is the responsibility of any person or organization contemplating
19  *   export to obtain such a license before exporting.
20  *
21  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
22  * distribute this software and its documentation for any purpose and
23  * without fee is hereby granted, provided that the above copyright
24  * notice appear in all copies and that both that copyright notice and
25  * this permission notice appear in supporting documentation, and that
26  * the name of M.I.T. not be used in advertising or publicity pertaining
27  * to distribution of the software without specific, written prior
28  * permission.  Furthermore if you modify this software you must label
29  * your software as modified software and not distribute it in such a
30  * fashion that it might be confused with the original M.I.T. software.
31  * M.I.T. makes no representations about the suitability of
32  * this software for any purpose.  It is provided "as is" without express
33  * or implied warranty.
34  *
35  *
36 * Implementation of the Microsoft hmac-md5 checksum type.
37 * Implemented based on draft-brezak-win2k-krb-rc4-hmac-03
38  */
39 
40 #include <k5-int.h>
41 #include <etypes.h>
42 #include <keyhash_provider.h>
43 #include <arcfour.h>
44 #include <hash_provider.h>
45 
46 /*ARGSUSED*/
47 static  krb5_error_code
48 k5_hmac_md5_hash (krb5_context context,
49 	const krb5_keyblock *key, krb5_keyusage usage,
50 	const krb5_data *iv,
51 	const krb5_data *input, krb5_data *output)
52 {
53   krb5_keyusage ms_usage;
54   krb5_error_code ret;
55   krb5_keyblock ks;
56   krb5_data ds, ks_constant, md5tmp;
57   krb5_data hash_input[2];
58   int i;
59   char t[4], outbuf[MD5_CKSUM_LENGTH];
60 
61 #ifdef _KERNEL
62   KRB5_LOG1(KRB5_INFO, "k5_hmac_md5_hash() hash_mt = %ld cipher_mt = %ld",
63 	(ulong_t) context->kef_hash_mt,
64 	(ulong_t) context->kef_cipher_mt);
65 #endif
66 
67   for (i=0; i<krb5_enctypes_length; i++) {
68 	if (krb5_enctypes_list[i].etype == key->enctype)
69             break;
70   }
71   if (i == krb5_enctypes_length) {
72 	KRB5_LOG(KRB5_ERR, "krb5_ck_make_checksum bad enctype: %d",
73 		key->enctype);
74 	return(KRB5_BAD_ENCTYPE);
75   }
76 
77   bzero(&ks, sizeof(krb5_keyblock));
78   ds.length = key->length;
79   ds.data = MALLOC(ds.length);
80   if (ds.data == NULL)
81     return (ENOMEM);
82 
83   ks.contents = (void *) ds.data;
84   ks.length = key->length;
85 #ifdef _KERNEL
86   if (key->kef_key.ck_data == NULL) {
87 	ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt,
88 			(krb5_keyblock *)key);
89 	if (ret)
90 		goto cleanup;
91   }
92 
93   ret = init_key_kef(krb5_enctypes_list[i].kef_cipher_mt, &ks);
94   if (ret)
95 	goto cleanup;
96 #endif /* _KERNEL */
97 
98   ks_constant.data = "signaturekey";
99   ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
100 
101 #ifdef _KERNEL
102   ret = krb5_hmac(context, (krb5_keyblock *)key, &ks_constant, &ds);
103 #else
104   ret = krb5_hmac(context, &krb5int_hash_md5, key, 1, &ks_constant, &ds);
105 #endif /* _KERNEL */
106   if (ret)
107     goto cleanup;
108 
109   ms_usage = krb5int_arcfour_translate_usage (usage);
110   t[0] = (ms_usage) & 0xff;
111   t[1] = (ms_usage>>8) & 0xff;
112   t[2] = (ms_usage >>16) & 0xff;
113   t[3] = (ms_usage>>24) & 0XFF;
114 
115   hash_input[0].data = (char *)&t;
116   hash_input[0].length = 4;
117   hash_input[1].data = input->data;
118   hash_input[1].length = input->length;
119 
120   md5tmp.data = (void *)outbuf;
121   md5tmp.length = sizeof(outbuf);
122 
123   /* Use generic hash function that calls to kEF */
124   if (k5_ef_hash(context, 2, hash_input, &md5tmp)) {
125 	return (KRB5_KEF_ERROR);
126   }
127 
128 #ifdef _KERNEL
129   ret = krb5_hmac (context, &ks, &md5tmp, output);
130 #else
131   ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
132 #endif /* _KERNEL */
133 
134 cleanup:
135   bzero(md5tmp.data, md5tmp.length);
136   bzero(ks.contents, ks.length);
137   FREE (ks.contents, ks.length);
138   return (ret);
139 }
140 
141  const struct krb5_keyhash_provider
142 krb5int_keyhash_hmac_md5 = {
143                 	MD5_CKSUM_LENGTH,
144 			k5_hmac_md5_hash,
145 			NULL /*checksum  again*/
146 			};
147 
148