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