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  * Copyright (C) 1998 by the FundsXpress, INC.
10  *
11  * All rights reserved.
12  *
13  * Export of this software from the United States of America may require
14  * a specific license from the United States Government.  It is the
15  * responsibility of any person or organization contemplating export to
16  * obtain such a license before exporting.
17  *
18  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
19  * distribute this software and its documentation for any purpose and
20  * without fee is hereby granted, provided that the above copyright
21  * notice appear in all copies and that both that copyright notice and
22  * this permission notice appear in supporting documentation, and that
23  * the name of FundsXpress. not be used in advertising or publicity pertaining
24  * to distribution of the software without specific, written prior
25  * permission.  FundsXpress makes no representations about the suitability of
26  * this software for any purpose.  It is provided "as is" without express
27  * or implied warranty.
28  *
29  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
30  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
31  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  */
33 
34 #include <k5-int.h>
35 
36 #ifdef _KERNEL
37 /*
38  * In kernel, use the Kernel encryption framework HMAC
39  * operation, its far more efficient than the MIT method.
40  * Also, a template is used to further improve performance.
41  */
42 /* ARGSUSED */
43 krb5_error_code
44 krb5_hmac(krb5_context context, const krb5_keyblock *key,
45 	krb5_const krb5_data *input, krb5_data *output)
46 {
47 	int rv = CRYPTO_FAILED;
48         crypto_mechanism_t mac_mech;
49 	crypto_data_t dd;
50 	crypto_data_t mac;
51 
52 	KRB5_LOG0(KRB5_INFO, "krb5_hmac() start");
53 	if (output == NULL || output->data == NULL) {
54 		KRB5_LOG0(KRB5_INFO, "krb5_hmac() NULL output");
55 		return (rv);
56 	}
57 	if (input == NULL || input->data == NULL) {
58 		KRB5_LOG0(KRB5_INFO, "krb5_hmac() NULL input");
59 		return (rv);
60 	}
61 
62 	dd.cd_format = CRYPTO_DATA_RAW;
63 	dd.cd_offset = 0;
64 	dd.cd_length = input->length;
65 	dd.cd_raw.iov_base = (char *)input->data;
66 	dd.cd_raw.iov_len = input->length;
67 
68 	mac.cd_format = CRYPTO_DATA_RAW;
69 	mac.cd_offset = 0;
70 	mac.cd_length = output->length;
71 	mac.cd_raw.iov_base = (char *)output->data;
72 	mac.cd_raw.iov_len = output->length;
73 
74 	mac_mech.cm_type = context->kef_hash_mt;
75 	mac_mech.cm_param = NULL;
76 	mac_mech.cm_param_len = 0;
77 
78 	rv = crypto_mac(&mac_mech, &dd,
79 			(crypto_key_t *)&key->kef_key,
80 			key->key_tmpl, &mac, NULL);
81 
82 	if (rv != CRYPTO_SUCCESS) {
83 		KRB5_LOG(KRB5_ERR,"crypto_mac error: %0x", rv);
84 	}
85 
86 	KRB5_LOG(KRB5_INFO, "krb5_hmac() end ret=%d\n", rv);
87 	return(rv);
88 }
89 
90 #else
91 /* Userland implementation of HMAC algorithm */
92 
93 /*
94  * the HMAC transform looks like:
95  *
96  * H(K XOR opad, H(K XOR ipad, text))
97  *
98  * where H is a cryptographic hash
99  * K is an n byte key
100  * ipad is the byte 0x36 repeated blocksize times
101  * opad is the byte 0x5c repeated blocksize times
102  * and text is the data being protected
103  */
104 krb5_error_code
105 krb5_hmac(krb5_context context,
106 	krb5_const struct krb5_hash_provider *hash,
107 	krb5_const krb5_keyblock *key,
108 	krb5_const unsigned int icount,
109 	krb5_const krb5_data *input,
110 	krb5_data *output)
111 {
112     size_t hashsize, blocksize;
113     unsigned char *xorkey, *ihash;
114     int i;
115     krb5_data *hashin, hashout;
116     krb5_error_code ret;
117 
118     KRB5_LOG0(KRB5_INFO, "krb5_hmac() start\n");
119 
120     if (hash == NULL) {
121 	KRB5_LOG0(KRB5_ERR, "krb5_hmac() error hash == NULL\n");
122 	return(EINVAL);
123     }
124     if (key == NULL) {
125 	KRB5_LOG0(KRB5_ERR, "krb5_hmac() error key == NULL\n");
126 	return(EINVAL);
127     }
128     if (input == NULL) {
129 	KRB5_LOG0(KRB5_ERR, "krb5_hmac() error input == NULL\n");
130 	return(EINVAL);
131     }
132     if (output == NULL) {
133 	KRB5_LOG0(KRB5_ERR, "krb5_hmac() error output == NULL\n");
134 	return(EINVAL);
135     }
136 
137     hashsize = hash->hashsize;
138     blocksize = hash->blocksize;
139 
140     if (key->length > blocksize)
141 	return(KRB5_CRYPTO_INTERNAL);
142     if (output->length < hashsize)
143 	return(KRB5_BAD_MSIZE);
144     /* if this isn't > 0, then there won't be enough space in this
145        array to compute the outer hash */
146     if (icount == 0)
147 	return(KRB5_CRYPTO_INTERNAL);
148 
149     /* allocate space for the xor key, hash input vector, and inner hash */
150 
151     if ((xorkey = (unsigned char *) MALLOC(blocksize)) == NULL)
152 	return(ENOMEM);
153     if ((ihash = (unsigned char *) MALLOC(hashsize)) == NULL) {
154 	FREE(xorkey, blocksize);
155 	return(ENOMEM);
156     }
157     if ((hashin = (krb5_data *)MALLOC(sizeof(krb5_data)*(icount+1))) == NULL) {
158 	FREE(ihash, hashsize);
159 	FREE(xorkey, blocksize);
160 	return(ENOMEM);
161     }
162 
163     /* create the inner padded key */
164 
165     (void) memset(xorkey, 0x36, blocksize);
166 
167     for (i=0; i<key->length; i++) {
168 	xorkey[i] ^= key->contents[i];
169     }
170 
171     /* compute the inner hash */
172 
173     for (i=0; i<icount; i++) {
174 	hashin[0].length = blocksize;
175 	hashin[0].data = (char *) xorkey;
176 	hashin[i+1] = input[i];
177     }
178 
179     hashout.length = hashsize;
180     hashout.data = (char *) ihash;
181 
182     if ((ret = ((*(hash->hash))(context, icount+1, hashin, &hashout))))
183 	goto cleanup;
184 
185     /* create the outer padded key */
186 
187     (void) memset(xorkey, 0x5c, blocksize);
188 
189     for (i=0; i<key->length; i++)
190 	xorkey[i] ^= key->contents[i];
191 
192     /* compute the outer hash */
193 
194     hashin[0].length = blocksize;
195     hashin[0].data = (char *) xorkey;
196     hashin[1] = hashout;
197 
198     output->length = hashsize;
199 
200     if ((ret = ((*(hash->hash))(context, 2, hashin, output))))
201 	(void) memset(output->data, 0, output->length);
202 
203     /* ret is set correctly by the prior call */
204 
205 cleanup:
206     (void) memset(xorkey, 0, blocksize);
207     (void) memset(ihash, 0, hashsize);
208 
209     FREE(hashin, sizeof(krb5_data)*(icount+1));
210     FREE(ihash, hashsize);
211     FREE(xorkey, blocksize);
212 
213     KRB5_LOG(KRB5_INFO, "krb5_hmac() end ret=%d\n", ret);
214     return(ret);
215 }
216 #endif /* _KERNEL */
217