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