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