xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/crypto/keyhash_provider/hmac_md5.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1 /*
2  * Copyright 2004 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 <arcfour.h>
42 #include <hash_provider.h>
43 #include <keyhash_provider.h>
44 
45 static void
46 k5_hmac_md5_hash_size (size_t *output)
47 {
48   *output = 16;
49 }
50 
51 static  krb5_error_code
52 k5_hmac_md5_hash (krb5_context context,
53 	const krb5_keyblock *key, krb5_keyusage usage,
54 	const krb5_data *iv,
55 	const krb5_data *input, krb5_data *output)
56 {
57   krb5_keyusage ms_usage;
58   krb5_error_code ret;
59   krb5_keyblock ks;
60   krb5_data ds, ks_constant, md5tmp;
61   krb5_data hash_input[2];
62   char digest[MD5_CKSUM_LENGTH];
63   char t[4];
64   CK_MECHANISM mechanism;
65   CK_RV rv;
66   CK_ULONG hashlen;
67 
68   bzero(&ks, sizeof(krb5_keyblock));
69   ds.length = key->length;
70   ks.length = key->length;
71   ds.data = malloc(ds.length);
72   if (ds.data == NULL)
73     return (ENOMEM);
74   ks.contents = (void *) ds.data;
75 
76   ks_constant.data = "signaturekey";
77   ks_constant.length = strlen(ks_constant.data)+1; /* Including null*/
78 
79   ret = krb5_hmac(context, &krb5int_hash_md5, key, 1,
80 		&ks_constant, &ds);
81   if (ret)
82     goto cleanup;
83 
84   ms_usage = krb5int_arcfour_translate_usage (usage);
85   t[0] = (ms_usage) & 0xff;
86   t[1] = (ms_usage>>8) & 0xff;
87   t[2] = (ms_usage >>16) & 0xff;
88   t[3] = (ms_usage>>24) & 0XFF;
89 
90   mechanism.mechanism = CKM_MD5;
91   mechanism.pParameter = NULL_PTR;
92   mechanism.ulParameterLen = 0;
93 
94   if ((rv = C_DigestInit(krb_ctx_hSession(context), &mechanism)) != CKR_OK) {
95 	KRB5_LOG(KRB5_ERR, "C_DigestInit failed in k5_md5_hmac_hash: "
96 	"rv = 0x%x.", rv);
97 	ret = PKCS_ERR;
98 	goto cleanup;
99   }
100   if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
101 	(CK_BYTE_PTR)t, (CK_ULONG)sizeof(t))) != CKR_OK) {
102 	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
103             "rv = 0x%x", rv);
104 	ret = PKCS_ERR;
105 	goto cleanup;
106   }
107   if ((rv = C_DigestUpdate(krb_ctx_hSession(context),
108 	(CK_BYTE_PTR)input->data, (CK_ULONG)input->length)) != CKR_OK) {
109 	KRB5_LOG(KRB5_ERR, "C_DigestUpdate failed in k5_md5_hmac_hash: "
110             "rv = 0x%x", rv);
111 	ret = PKCS_ERR;
112 	goto cleanup;
113   }
114   hashlen = MD5_CKSUM_LENGTH;
115   if ((rv = C_DigestFinal(krb_ctx_hSession(context),
116 	(CK_BYTE_PTR)digest, (CK_ULONG_PTR)&hashlen)) != CKR_OK) {
117 	KRB5_LOG(KRB5_ERR, "C_DigestFinal failed in k5_md5_hmac_hash: "
118             "rv = 0x%x", rv);
119 	ret = PKCS_ERR;
120 	goto cleanup;
121   }
122 
123   md5tmp.data = digest;
124   md5tmp.length = hashlen;
125 
126   ret = krb5_hmac (context, &krb5int_hash_md5, &ks, 1, &md5tmp, output);
127 
128 cleanup:
129   bzero(ks.contents, ks.length);
130   bzero(md5tmp.data, md5tmp.length);
131   FREE (ks.contents, ks.length);
132   return (ret);
133 }
134 
135 const struct krb5_keyhash_provider
136 krb5int_keyhash_hmac_md5 = {
137 			k5_hmac_md5_hash_size,
138 			k5_hmac_md5_hash,
139 			NULL /*checksum  again*/
140 };
141 
142