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