hmac_link.c revision 1.2.4.2 1 1.2.4.2 yamt /* $NetBSD: hmac_link.c,v 1.2.4.2 2013/01/16 05:32:27 yamt Exp $ */
2 1.2.4.2 yamt
3 1.2.4.2 yamt /*
4 1.2.4.2 yamt * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
5 1.2.4.2 yamt *
6 1.2.4.2 yamt * Permission to use, copy modify, and distribute this software for any
7 1.2.4.2 yamt * purpose with or without fee is hereby granted, provided that the above
8 1.2.4.2 yamt * copyright notice and this permission notice appear in all copies.
9 1.2.4.2 yamt *
10 1.2.4.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
11 1.2.4.2 yamt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 1.2.4.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 1.2.4.2 yamt * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
14 1.2.4.2 yamt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 1.2.4.2 yamt * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 1.2.4.2 yamt * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 1.2.4.2 yamt * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
18 1.2.4.2 yamt */
19 1.2.4.2 yamt #include <sys/cdefs.h>
20 1.2.4.2 yamt #if 0
21 1.2.4.2 yamt static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp ";
22 1.2.4.2 yamt #else
23 1.2.4.2 yamt __RCSID("$NetBSD: hmac_link.c,v 1.2.4.2 2013/01/16 05:32:27 yamt Exp $");
24 1.2.4.2 yamt #endif
25 1.2.4.2 yamt
26 1.2.4.2 yamt /*%
27 1.2.4.2 yamt * This file contains an implementation of the HMAC-MD5 algorithm.
28 1.2.4.2 yamt */
29 1.2.4.2 yamt #include "port_before.h"
30 1.2.4.2 yamt
31 1.2.4.2 yamt #include <stdio.h>
32 1.2.4.2 yamt #include <unistd.h>
33 1.2.4.2 yamt #include <stdlib.h>
34 1.2.4.2 yamt #include <string.h>
35 1.2.4.2 yamt #include <memory.h>
36 1.2.4.2 yamt #include <sys/param.h>
37 1.2.4.2 yamt #include <sys/time.h>
38 1.2.4.2 yamt #include <netinet/in.h>
39 1.2.4.2 yamt #include <arpa/nameser.h>
40 1.2.4.2 yamt #include <resolv.h>
41 1.2.4.2 yamt
42 1.2.4.2 yamt #include "dst_internal.h"
43 1.2.4.2 yamt
44 1.2.4.2 yamt #include <md5.h>
45 1.2.4.2 yamt #include "port_after.h"
46 1.2.4.2 yamt
47 1.2.4.2 yamt
48 1.2.4.2 yamt #define HMAC_LEN 64
49 1.2.4.2 yamt #define HMAC_IPAD 0x36
50 1.2.4.2 yamt #define HMAC_OPAD 0x5c
51 1.2.4.2 yamt #define MD5_LEN 16
52 1.2.4.2 yamt
53 1.2.4.2 yamt
54 1.2.4.2 yamt typedef struct hmackey {
55 1.2.4.2 yamt u_char hk_ipad[64], hk_opad[64];
56 1.2.4.2 yamt } HMAC_Key;
57 1.2.4.2 yamt
58 1.2.4.2 yamt
59 1.2.4.2 yamt /**************************************************************************
60 1.2.4.2 yamt * dst_hmac_md5_sign
61 1.2.4.2 yamt * Call HMAC signing functions to sign a block of data.
62 1.2.4.2 yamt * There are three steps to signing, INIT (initialize structures),
63 1.2.4.2 yamt * UPDATE (hash (more) data), FINAL (generate a signature). This
64 1.2.4.2 yamt * routine performs one or more of these steps.
65 1.2.4.2 yamt * Parameters
66 1.2.4.2 yamt * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
67 1.2.4.2 yamt * priv_key key to use for signing.
68 1.2.4.2 yamt * context the context to be used in this digest
69 1.2.4.2 yamt * data data to be signed.
70 1.2.4.2 yamt * len length in bytes of data.
71 1.2.4.2 yamt * signature location to store signature.
72 1.2.4.2 yamt * sig_len size of the signature location
73 1.2.4.2 yamt * returns
74 1.2.4.2 yamt * N Success on SIG_MODE_FINAL = returns signature length in bytes
75 1.2.4.2 yamt * 0 Success on SIG_MODE_INIT and UPDATE
76 1.2.4.2 yamt * <0 Failure
77 1.2.4.2 yamt */
78 1.2.4.2 yamt
79 1.2.4.2 yamt static int
80 1.2.4.2 yamt dst_hmac_md5_sign(const int mode, DST_KEY *d_key, void **context,
81 1.2.4.2 yamt const u_char *data, const int len,
82 1.2.4.2 yamt u_char *signature, const int sig_len)
83 1.2.4.2 yamt {
84 1.2.4.2 yamt HMAC_Key *key;
85 1.2.4.2 yamt int sign_len = 0;
86 1.2.4.2 yamt MD5_CTX *ctx = NULL;
87 1.2.4.2 yamt
88 1.2.4.2 yamt if (d_key == NULL || d_key->dk_KEY_struct == NULL)
89 1.2.4.2 yamt return (-1);
90 1.2.4.2 yamt
91 1.2.4.2 yamt if (mode & SIG_MODE_INIT)
92 1.2.4.2 yamt ctx = (MD5_CTX *) malloc(sizeof(*ctx));
93 1.2.4.2 yamt else if (context)
94 1.2.4.2 yamt ctx = (MD5_CTX *) *context;
95 1.2.4.2 yamt if (ctx == NULL)
96 1.2.4.2 yamt return (-1);
97 1.2.4.2 yamt
98 1.2.4.2 yamt key = (HMAC_Key *) d_key->dk_KEY_struct;
99 1.2.4.2 yamt
100 1.2.4.2 yamt if (mode & SIG_MODE_INIT) {
101 1.2.4.2 yamt MD5Init(ctx);
102 1.2.4.2 yamt MD5Update(ctx, key->hk_ipad, HMAC_LEN);
103 1.2.4.2 yamt }
104 1.2.4.2 yamt
105 1.2.4.2 yamt if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
106 1.2.4.2 yamt MD5Update(ctx, data, (unsigned int)len);
107 1.2.4.2 yamt
108 1.2.4.2 yamt if (mode & SIG_MODE_FINAL) {
109 1.2.4.2 yamt if (signature == NULL || sig_len < MD5_LEN)
110 1.2.4.2 yamt return (SIGN_FINAL_FAILURE);
111 1.2.4.2 yamt MD5Final(signature, ctx);
112 1.2.4.2 yamt
113 1.2.4.2 yamt /* perform outer MD5 */
114 1.2.4.2 yamt MD5Init(ctx);
115 1.2.4.2 yamt MD5Update(ctx, key->hk_opad, HMAC_LEN);
116 1.2.4.2 yamt MD5Update(ctx, signature, MD5_LEN);
117 1.2.4.2 yamt MD5Final(signature, ctx);
118 1.2.4.2 yamt sign_len = MD5_LEN;
119 1.2.4.2 yamt SAFE_FREE(ctx);
120 1.2.4.2 yamt }
121 1.2.4.2 yamt else {
122 1.2.4.2 yamt if (context == NULL)
123 1.2.4.2 yamt return (-1);
124 1.2.4.2 yamt *context = (void *) ctx;
125 1.2.4.2 yamt }
126 1.2.4.2 yamt return (sign_len);
127 1.2.4.2 yamt }
128 1.2.4.2 yamt
129 1.2.4.2 yamt
130 1.2.4.2 yamt /**************************************************************************
131 1.2.4.2 yamt * dst_hmac_md5_verify()
132 1.2.4.2 yamt * Calls HMAC verification routines. There are three steps to
133 1.2.4.2 yamt * verification, INIT (initialize structures), UPDATE (hash (more) data),
134 1.2.4.2 yamt * FINAL (generate a signature). This routine performs one or more of
135 1.2.4.2 yamt * these steps.
136 1.2.4.2 yamt * Parameters
137 1.2.4.2 yamt * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
138 1.2.4.2 yamt * dkey key to use for verify.
139 1.2.4.2 yamt * data data signed.
140 1.2.4.2 yamt * len length in bytes of data.
141 1.2.4.2 yamt * signature signature.
142 1.2.4.2 yamt * sig_len length in bytes of signature.
143 1.2.4.2 yamt * returns
144 1.2.4.2 yamt * 0 Success
145 1.2.4.2 yamt * <0 Failure
146 1.2.4.2 yamt */
147 1.2.4.2 yamt
148 1.2.4.2 yamt static int
149 1.2.4.2 yamt dst_hmac_md5_verify(const int mode, DST_KEY *d_key, void **context,
150 1.2.4.2 yamt const u_char *data, const int len,
151 1.2.4.2 yamt const u_char *signature, const int sig_len)
152 1.2.4.2 yamt {
153 1.2.4.2 yamt HMAC_Key *key;
154 1.2.4.2 yamt MD5_CTX *ctx = NULL;
155 1.2.4.2 yamt
156 1.2.4.2 yamt if (d_key == NULL || d_key->dk_KEY_struct == NULL)
157 1.2.4.2 yamt return (-1);
158 1.2.4.2 yamt
159 1.2.4.2 yamt if (mode & SIG_MODE_INIT)
160 1.2.4.2 yamt ctx = (MD5_CTX *) malloc(sizeof(*ctx));
161 1.2.4.2 yamt else if (context)
162 1.2.4.2 yamt ctx = (MD5_CTX *) *context;
163 1.2.4.2 yamt if (ctx == NULL)
164 1.2.4.2 yamt return (-1);
165 1.2.4.2 yamt
166 1.2.4.2 yamt key = (HMAC_Key *) d_key->dk_KEY_struct;
167 1.2.4.2 yamt if (mode & SIG_MODE_INIT) {
168 1.2.4.2 yamt MD5Init(ctx);
169 1.2.4.2 yamt MD5Update(ctx, key->hk_ipad, HMAC_LEN);
170 1.2.4.2 yamt }
171 1.2.4.2 yamt if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
172 1.2.4.2 yamt MD5Update(ctx, data, (unsigned int)len);
173 1.2.4.2 yamt
174 1.2.4.2 yamt if (mode & SIG_MODE_FINAL) {
175 1.2.4.2 yamt u_char digest[MD5_LEN];
176 1.2.4.2 yamt if (signature == NULL || key == NULL || sig_len != MD5_LEN)
177 1.2.4.2 yamt return (VERIFY_FINAL_FAILURE);
178 1.2.4.2 yamt MD5Final(digest, ctx);
179 1.2.4.2 yamt
180 1.2.4.2 yamt /* perform outer MD5 */
181 1.2.4.2 yamt MD5Init(ctx);
182 1.2.4.2 yamt MD5Update(ctx, key->hk_opad, HMAC_LEN);
183 1.2.4.2 yamt MD5Update(ctx, digest, MD5_LEN);
184 1.2.4.2 yamt MD5Final(digest, ctx);
185 1.2.4.2 yamt
186 1.2.4.2 yamt SAFE_FREE(ctx);
187 1.2.4.2 yamt if (memcmp(digest, signature, MD5_LEN) != 0)
188 1.2.4.2 yamt return (VERIFY_FINAL_FAILURE);
189 1.2.4.2 yamt }
190 1.2.4.2 yamt else {
191 1.2.4.2 yamt if (context == NULL)
192 1.2.4.2 yamt return (-1);
193 1.2.4.2 yamt *context = (void *) ctx;
194 1.2.4.2 yamt }
195 1.2.4.2 yamt return (0);
196 1.2.4.2 yamt }
197 1.2.4.2 yamt
198 1.2.4.2 yamt
199 1.2.4.2 yamt /**************************************************************************
200 1.2.4.2 yamt * dst_buffer_to_hmac_md5
201 1.2.4.2 yamt * Converts key from raw data to an HMAC Key
202 1.2.4.2 yamt * This function gets in a pointer to the data
203 1.2.4.2 yamt * Parameters
204 1.2.4.2 yamt * hkey the HMAC key to be filled in
205 1.2.4.2 yamt * key the key in raw format
206 1.2.4.2 yamt * keylen the length of the key
207 1.2.4.2 yamt * Return
208 1.2.4.2 yamt * 0 Success
209 1.2.4.2 yamt * <0 Failure
210 1.2.4.2 yamt */
211 1.2.4.2 yamt static int
212 1.2.4.2 yamt dst_buffer_to_hmac_md5(DST_KEY *dkey, const u_char *key, const int keylen)
213 1.2.4.2 yamt {
214 1.2.4.2 yamt int i;
215 1.2.4.2 yamt HMAC_Key *hkey = NULL;
216 1.2.4.2 yamt MD5_CTX ctx;
217 1.2.4.2 yamt int local_keylen = keylen;
218 1.2.4.2 yamt u_char tk[MD5_LEN];
219 1.2.4.2 yamt
220 1.2.4.2 yamt if (dkey == NULL || key == NULL || keylen < 0)
221 1.2.4.2 yamt return (-1);
222 1.2.4.2 yamt
223 1.2.4.2 yamt if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
224 1.2.4.2 yamt return (-2);
225 1.2.4.2 yamt
226 1.2.4.2 yamt memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
227 1.2.4.2 yamt memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
228 1.2.4.2 yamt
229 1.2.4.2 yamt /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
230 1.2.4.2 yamt if (keylen > HMAC_LEN) {
231 1.2.4.2 yamt MD5Init(&ctx);
232 1.2.4.2 yamt MD5Update(&ctx, key, (unsigned int)keylen);
233 1.2.4.2 yamt MD5Final(tk, &ctx);
234 1.2.4.2 yamt memset((void *) &ctx, 0, sizeof(ctx));
235 1.2.4.2 yamt key = tk;
236 1.2.4.2 yamt local_keylen = MD5_LEN;
237 1.2.4.2 yamt }
238 1.2.4.2 yamt /* start out by storing key in pads */
239 1.2.4.2 yamt memcpy(hkey->hk_ipad, key, local_keylen);
240 1.2.4.2 yamt memcpy(hkey->hk_opad, key, local_keylen);
241 1.2.4.2 yamt
242 1.2.4.2 yamt /* XOR key with hk_ipad and opad values */
243 1.2.4.2 yamt for (i = 0; i < HMAC_LEN; i++) {
244 1.2.4.2 yamt hkey->hk_ipad[i] ^= HMAC_IPAD;
245 1.2.4.2 yamt hkey->hk_opad[i] ^= HMAC_OPAD;
246 1.2.4.2 yamt }
247 1.2.4.2 yamt dkey->dk_key_size = local_keylen;
248 1.2.4.2 yamt dkey->dk_KEY_struct = (void *) hkey;
249 1.2.4.2 yamt return (1);
250 1.2.4.2 yamt }
251 1.2.4.2 yamt
252 1.2.4.2 yamt
253 1.2.4.2 yamt /**************************************************************************
254 1.2.4.2 yamt * dst_hmac_md5_key_to_file_format
255 1.2.4.2 yamt * Encodes an HMAC Key into the portable file format.
256 1.2.4.2 yamt * Parameters
257 1.2.4.2 yamt * hkey HMAC KEY structure
258 1.2.4.2 yamt * buff output buffer
259 1.2.4.2 yamt * buff_len size of output buffer
260 1.2.4.2 yamt * Return
261 1.2.4.2 yamt * 0 Failure - null input hkey
262 1.2.4.2 yamt * -1 Failure - not enough space in output area
263 1.2.4.2 yamt * N Success - Length of data returned in buff
264 1.2.4.2 yamt */
265 1.2.4.2 yamt
266 1.2.4.2 yamt static int
267 1.2.4.2 yamt dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
268 1.2.4.2 yamt const int buff_len)
269 1.2.4.2 yamt {
270 1.2.4.2 yamt char *bp;
271 1.2.4.2 yamt int len, i, key_len;
272 1.2.4.2 yamt u_char key[HMAC_LEN];
273 1.2.4.2 yamt HMAC_Key *hkey;
274 1.2.4.2 yamt
275 1.2.4.2 yamt if (dkey == NULL || dkey->dk_KEY_struct == NULL)
276 1.2.4.2 yamt return (0);
277 1.2.4.2 yamt /*
278 1.2.4.2 yamt * Using snprintf() would be so much simpler here.
279 1.2.4.2 yamt */
280 1.2.4.2 yamt if (buff == NULL ||
281 1.2.4.2 yamt buff_len <= (int)(strlen(KEY_FILE_FMT_STR) +
282 1.2.4.2 yamt strlen(KEY_FILE_FORMAT) + 4))
283 1.2.4.2 yamt return (-1); /*%< no OR not enough space in output area */
284 1.2.4.2 yamt hkey = (HMAC_Key *) dkey->dk_KEY_struct;
285 1.2.4.2 yamt memset(buff, 0, buff_len); /*%< just in case */
286 1.2.4.2 yamt /* write file header */
287 1.2.4.2 yamt snprintf(buff, buff_len, KEY_FILE_FMT_STR, KEY_FILE_FORMAT,
288 1.2.4.2 yamt KEY_HMAC_MD5, "HMAC");
289 1.2.4.2 yamt
290 1.2.4.2 yamt bp = buff + strlen(buff);
291 1.2.4.2 yamt
292 1.2.4.2 yamt memset(key, 0, HMAC_LEN);
293 1.2.4.2 yamt for (i = 0; i < HMAC_LEN; i++)
294 1.2.4.2 yamt key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
295 1.2.4.2 yamt for (i = HMAC_LEN - 1; i >= 0; i--)
296 1.2.4.2 yamt if (key[i] != 0)
297 1.2.4.2 yamt break;
298 1.2.4.2 yamt key_len = i + 1;
299 1.2.4.2 yamt
300 1.2.4.2 yamt if (buff_len - (bp - buff) < 6)
301 1.2.4.2 yamt return (-1);
302 1.2.4.2 yamt strcat(bp, "Key: ");
303 1.2.4.2 yamt bp += strlen("Key: ");
304 1.2.4.2 yamt
305 1.2.4.2 yamt len = b64_ntop(key, key_len, bp, (size_t)(buff_len - (bp - buff)));
306 1.2.4.2 yamt if (len < 0)
307 1.2.4.2 yamt return (-1);
308 1.2.4.2 yamt bp += len;
309 1.2.4.2 yamt if (buff_len - (bp - buff) < 2)
310 1.2.4.2 yamt return (-1);
311 1.2.4.2 yamt *(bp++) = '\n';
312 1.2.4.2 yamt *bp = '\0';
313 1.2.4.2 yamt
314 1.2.4.2 yamt return (int)(bp - buff);
315 1.2.4.2 yamt }
316 1.2.4.2 yamt
317 1.2.4.2 yamt
318 1.2.4.2 yamt /**************************************************************************
319 1.2.4.2 yamt * dst_hmac_md5_key_from_file_format
320 1.2.4.2 yamt * Converts contents of a key file into an HMAC key.
321 1.2.4.2 yamt * Parameters
322 1.2.4.2 yamt * hkey structure to put key into
323 1.2.4.2 yamt * buff buffer containing the encoded key
324 1.2.4.2 yamt * buff_len the length of the buffer
325 1.2.4.2 yamt * Return
326 1.2.4.2 yamt * n >= 0 Foot print of the key converted
327 1.2.4.2 yamt * n < 0 Error in conversion
328 1.2.4.2 yamt */
329 1.2.4.2 yamt
330 1.2.4.2 yamt static int
331 1.2.4.2 yamt dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
332 1.2.4.2 yamt const int buff_len)
333 1.2.4.2 yamt {
334 1.2.4.2 yamt const char *p = buff, *eol;
335 1.2.4.2 yamt u_char key[HMAC_LEN+1]; /* b64_pton needs more than 64 bytes do decode
336 1.2.4.2 yamt * it should probably be fixed rather than doing
337 1.2.4.2 yamt * this
338 1.2.4.2 yamt */
339 1.2.4.2 yamt u_char *tmp;
340 1.2.4.2 yamt int key_len, len;
341 1.2.4.2 yamt
342 1.2.4.2 yamt if (dkey == NULL)
343 1.2.4.2 yamt return (-2);
344 1.2.4.2 yamt if (buff == NULL || buff_len < 0)
345 1.2.4.2 yamt return (-1);
346 1.2.4.2 yamt
347 1.2.4.2 yamt memset(key, 0, sizeof(key));
348 1.2.4.2 yamt
349 1.2.4.2 yamt if (!dst_s_verify_str(&p, "Key: "))
350 1.2.4.2 yamt return (-3);
351 1.2.4.2 yamt
352 1.2.4.2 yamt eol = strchr(p, '\n');
353 1.2.4.2 yamt if (eol == NULL)
354 1.2.4.2 yamt return (-4);
355 1.2.4.2 yamt len = (int)(eol - p);
356 1.2.4.2 yamt tmp = malloc(len + 2);
357 1.2.4.2 yamt if (tmp == NULL)
358 1.2.4.2 yamt return (-5);
359 1.2.4.2 yamt memcpy(tmp, p, len);
360 1.2.4.2 yamt *(tmp + len) = 0x0;
361 1.2.4.2 yamt key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /*%< see above */
362 1.2.4.2 yamt SAFE_FREE2(tmp, len + 2);
363 1.2.4.2 yamt
364 1.2.4.2 yamt if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
365 1.2.4.2 yamt return (-6);
366 1.2.4.2 yamt }
367 1.2.4.2 yamt return (0);
368 1.2.4.2 yamt }
369 1.2.4.2 yamt
370 1.2.4.2 yamt /*%
371 1.2.4.2 yamt * dst_hmac_md5_to_dns_key()
372 1.2.4.2 yamt * function to extract hmac key from DST_KEY structure
373 1.2.4.2 yamt * intput:
374 1.2.4.2 yamt * in_key: HMAC-MD5 key
375 1.2.4.2 yamt * output:
376 1.2.4.2 yamt * out_str: buffer to write ot
377 1.2.4.2 yamt * out_len: size of output buffer
378 1.2.4.2 yamt * returns:
379 1.2.4.2 yamt * number of bytes written to output buffer
380 1.2.4.2 yamt */
381 1.2.4.2 yamt static int
382 1.2.4.2 yamt dst_hmac_md5_to_dns_key(const DST_KEY *in_key, u_char *out_str,
383 1.2.4.2 yamt const int out_len)
384 1.2.4.2 yamt {
385 1.2.4.2 yamt
386 1.2.4.2 yamt HMAC_Key *hkey;
387 1.2.4.2 yamt int i;
388 1.2.4.2 yamt
389 1.2.4.2 yamt if (in_key == NULL || in_key->dk_KEY_struct == NULL ||
390 1.2.4.2 yamt out_len <= in_key->dk_key_size || out_str == NULL)
391 1.2.4.2 yamt return (-1);
392 1.2.4.2 yamt
393 1.2.4.2 yamt hkey = (HMAC_Key *) in_key->dk_KEY_struct;
394 1.2.4.2 yamt for (i = 0; i < in_key->dk_key_size; i++)
395 1.2.4.2 yamt out_str[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
396 1.2.4.2 yamt return (i);
397 1.2.4.2 yamt }
398 1.2.4.2 yamt
399 1.2.4.2 yamt /**************************************************************************
400 1.2.4.2 yamt * dst_hmac_md5_compare_keys
401 1.2.4.2 yamt * Compare two keys for equality.
402 1.2.4.2 yamt * Return
403 1.2.4.2 yamt * 0 The keys are equal
404 1.2.4.2 yamt * NON-ZERO The keys are not equal
405 1.2.4.2 yamt */
406 1.2.4.2 yamt
407 1.2.4.2 yamt static int
408 1.2.4.2 yamt dst_hmac_md5_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
409 1.2.4.2 yamt {
410 1.2.4.2 yamt HMAC_Key *hkey1 = (HMAC_Key *) key1->dk_KEY_struct;
411 1.2.4.2 yamt HMAC_Key *hkey2 = (HMAC_Key *) key2->dk_KEY_struct;
412 1.2.4.2 yamt return memcmp(hkey1->hk_ipad, hkey2->hk_ipad, HMAC_LEN);
413 1.2.4.2 yamt }
414 1.2.4.2 yamt
415 1.2.4.2 yamt /**************************************************************************
416 1.2.4.2 yamt * dst_hmac_md5_free_key_structure
417 1.2.4.2 yamt * Frees all (none) dynamically allocated structures in hkey
418 1.2.4.2 yamt */
419 1.2.4.2 yamt
420 1.2.4.2 yamt static void *
421 1.2.4.2 yamt dst_hmac_md5_free_key_structure(void *key)
422 1.2.4.2 yamt {
423 1.2.4.2 yamt HMAC_Key *hkey = key;
424 1.2.4.2 yamt SAFE_FREE(hkey);
425 1.2.4.2 yamt return (NULL);
426 1.2.4.2 yamt }
427 1.2.4.2 yamt
428 1.2.4.2 yamt
429 1.2.4.2 yamt /***************************************************************************
430 1.2.4.2 yamt * dst_hmac_md5_generate_key
431 1.2.4.2 yamt * Creates a HMAC key of size size with a maximum size of 63 bytes
432 1.2.4.2 yamt * generating a HMAC key larger than 63 bytes makes no sense as that key
433 1.2.4.2 yamt * is digested before use.
434 1.2.4.2 yamt */
435 1.2.4.2 yamt
436 1.2.4.2 yamt static int
437 1.2.4.2 yamt /*ARGSUSED*/
438 1.2.4.2 yamt dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
439 1.2.4.2 yamt {
440 1.2.4.2 yamt return (-1);
441 1.2.4.2 yamt }
442 1.2.4.2 yamt
443 1.2.4.2 yamt /*%
444 1.2.4.2 yamt * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
445 1.2.4.2 yamt * related functions
446 1.2.4.2 yamt */
447 1.2.4.2 yamt int
448 1.2.4.2 yamt #ifdef SUNW_LIBMD5
449 1.2.4.2 yamt dst_md5_hmac_init(void)
450 1.2.4.2 yamt #else
451 1.2.4.2 yamt dst_hmac_md5_init(void)
452 1.2.4.2 yamt #endif
453 1.2.4.2 yamt {
454 1.2.4.2 yamt if (dst_t_func[KEY_HMAC_MD5] != NULL)
455 1.2.4.2 yamt return (1);
456 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
457 1.2.4.2 yamt if (dst_t_func[KEY_HMAC_MD5] == NULL)
458 1.2.4.2 yamt return (0);
459 1.2.4.2 yamt memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
460 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->sign = dst_hmac_md5_sign;
461 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->verify = dst_hmac_md5_verify;
462 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->compare = dst_hmac_md5_compare_keys;
463 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->generate = dst_hmac_md5_generate_key;
464 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->destroy = dst_hmac_md5_free_key_structure;
465 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->to_dns_key = dst_hmac_md5_to_dns_key;
466 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->from_dns_key = dst_buffer_to_hmac_md5;
467 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->to_file_fmt = dst_hmac_md5_key_to_file_format;
468 1.2.4.2 yamt dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
469 1.2.4.2 yamt return (1);
470 1.2.4.2 yamt }
471 1.2.4.2 yamt
472 1.2.4.2 yamt /*! \file */
473