1 /* $KAME: auth.c,v 1.4 2004/09/07 05:03:02 jinmei Exp $ */
4 * Copyright (C) 2004 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
34 * Copyright (C) 2000, 2001 Internet Software Consortium.
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the above
38 * copyright notice and this permission notice appear in all copies.
40 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
41 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
42 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
43 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
44 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
45 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
46 * PERFORMANCE OF THIS SOFTWARE.
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/queue.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
63 #include <netinet/in.h>
78 #define HMACMD5_KEYLENGTH 64
88 unsigned char key
[HMACMD5_KEYLENGTH
];
91 static void hmacmd5_init
__P((hmacmd5_t
*, const unsigned char *,
93 static void hmacmd5_invalidate
__P((hmacmd5_t
*));
94 static void hmacmd5_update
__P((hmacmd5_t
*, const unsigned char *,
96 static void hmacmd5_sign
__P((hmacmd5_t
*, unsigned char *));
97 static int hmacmd5_verify
__P((hmacmd5_t
*, unsigned char *));
99 static void md5_init
__P((md5_t
*));
100 static void md5_invalidate
__P((md5_t
*));
101 static void md5_final
__P((md5_t
*, unsigned char *));
102 static void md5_update
__P((md5_t
*, const unsigned char *, unsigned int));
105 dhcp6_validate_key(key
)
110 if (key
->expire
== 0) /* never expire */
113 if (time(&now
) == -1)
114 return (-1); /* treat it as expiration (XXX) */
116 if (now
> key
->expire
)
123 dhcp6_calc_mac(buf
, len
, proto
, alg
, off
, key
)
130 unsigned char digest
[MD5_DIGESTLENGTH
];
132 /* right now, we don't care about the protocol */
134 if (alg
!= DHCP6_AUTHALG_HMACMD5
)
137 if (off
+ MD5_DIGESTLENGTH
> len
) {
139 * this should be assured by the caller, but check it here
145 hmacmd5_init(&ctx
, key
->secret
, key
->secretlen
);
146 hmacmd5_update(&ctx
, buf
, len
);
147 hmacmd5_sign(&ctx
, digest
);
149 memcpy(buf
+ off
, digest
, MD5_DIGESTLENGTH
);
155 dhcp6_verify_mac(buf
, len
, proto
, alg
, off
, key
)
163 unsigned char digest
[MD5_DIGESTLENGTH
];
166 /* right now, we don't care about the protocol */
168 if (alg
!= DHCP6_AUTHALG_HMACMD5
)
171 if (off
+ MD5_DIGESTLENGTH
> len
)
175 * Copy the MAC value and clear the field.
176 * XXX: should we make a local working copy?
178 memcpy(digest
, buf
+ off
, sizeof(digest
));
179 memset(buf
+ off
, 0, sizeof(digest
));
181 hmacmd5_init(&ctx
, key
->secret
, key
->secretlen
);
182 hmacmd5_update(&ctx
, buf
, len
);
183 result
= hmacmd5_verify(&ctx
, digest
);
185 /* copy back the digest value (XXX) */
186 memcpy(buf
+ off
, digest
, sizeof(digest
));
192 * This code implements the HMAC-MD5 keyed hash algorithm
193 * described in RFC 2104.
196 * Start HMAC-MD5 process. Initialize an md5 context and digest the key.
199 hmacmd5_init(hmacmd5_t
*ctx
, const unsigned char *key
, unsigned int len
)
201 unsigned char ipad
[PADLEN
];
204 memset(ctx
->key
, 0, sizeof(ctx
->key
));
205 if (len
> sizeof(ctx
->key
)) {
208 md5_update(&md5ctx
, key
, len
);
209 md5_final(&md5ctx
, ctx
->key
);
211 memcpy(ctx
->key
, key
, len
);
213 md5_init(&ctx
->md5ctx
);
214 memset(ipad
, IPAD
, sizeof(ipad
));
215 for (i
= 0; i
< PADLEN
; i
++)
216 ipad
[i
] ^= ctx
->key
[i
];
217 md5_update(&ctx
->md5ctx
, ipad
, sizeof(ipad
));
221 hmacmd5_invalidate(hmacmd5_t
*ctx
)
223 md5_invalidate(&ctx
->md5ctx
);
224 memset(ctx
->key
, 0, sizeof(ctx
->key
));
225 memset(ctx
, 0, sizeof(ctx
));
229 * Update context to reflect the concatenation of another buffer full
233 hmacmd5_update(hmacmd5_t
*ctx
, const unsigned char *buf
, unsigned int len
)
235 md5_update(&ctx
->md5ctx
, buf
, len
);
239 * Compute signature - finalize MD5 operation and reapply MD5.
242 hmacmd5_sign(hmacmd5_t
*ctx
, unsigned char *digest
)
244 unsigned char opad
[PADLEN
];
247 md5_final(&ctx
->md5ctx
, digest
);
249 memset(opad
, OPAD
, sizeof(opad
));
250 for (i
= 0; i
< PADLEN
; i
++)
251 opad
[i
] ^= ctx
->key
[i
];
253 md5_init(&ctx
->md5ctx
);
254 md5_update(&ctx
->md5ctx
, opad
, sizeof(opad
));
255 md5_update(&ctx
->md5ctx
, digest
, MD5_DIGESTLENGTH
);
256 md5_final(&ctx
->md5ctx
, digest
);
257 hmacmd5_invalidate(ctx
);
261 * Verify signature - finalize MD5 operation and reapply MD5, then
262 * compare to the supplied digest.
265 hmacmd5_verify(hmacmd5_t
*ctx
, unsigned char *digest
) {
266 unsigned char newdigest
[MD5_DIGESTLENGTH
];
268 hmacmd5_sign(ctx
, newdigest
);
269 return (memcmp(digest
, newdigest
, MD5_DIGESTLENGTH
));
273 * This code implements the MD5 message-digest algorithm.
274 * The algorithm is due to Ron Rivest. This code was
275 * written by Colin Plumb in 1993, no copyright is claimed.
276 * This code is in the public domain; do with it what you wish.
278 * Equivalent code is available from RSA Data Security, Inc.
279 * This code has been tested against that, and is equivalent,
280 * except that you don't need to include two pages of legalese
283 * To compute the message digest of a chunk of bytes, declare an
284 * MD5Context structure, pass it to MD5Init, call MD5Update as
285 * needed on buffers full of bytes, and then call MD5Final, which
286 * will fill a supplied 16-byte array with the digest.
290 byteSwap(u_int32_t
*buf
, unsigned words
)
292 unsigned char *p
= (unsigned char *)buf
;
295 *buf
++ = (u_int32_t
)((unsigned)p
[3] << 8 | p
[2]) << 16 |
296 ((unsigned)p
[1] << 8 | p
[0]);
302 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
303 * initialization constants.
308 ctx
->buf
[0] = 0x67452301;
309 ctx
->buf
[1] = 0xefcdab89;
310 ctx
->buf
[2] = 0x98badcfe;
311 ctx
->buf
[3] = 0x10325476;
318 md5_invalidate(md5_t
*ctx
)
320 memset(ctx
, 0, sizeof(md5_t
));
323 /* The four core functions - F1 is optimized somewhat */
325 /* #define F1(x, y, z) (x & y | ~x & z) */
326 #define F1(x, y, z) (z ^ (x & (y ^ z)))
327 #define F2(x, y, z) F1(z, x, y)
328 #define F3(x, y, z) (x ^ y ^ z)
329 #define F4(x, y, z) (y ^ (x | ~z))
331 /* This is the central step in the MD5 algorithm. */
332 #define MD5STEP(f,w,x,y,z,in,s) \
333 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
336 * The core of the MD5 algorithm, this alters an existing MD5 hash to
337 * reflect the addition of 16 longwords of new data. MD5Update blocks
338 * the data and converts bytes into longwords for this routine.
341 transform(u_int32_t buf
[4], u_int32_t
const in
[16]) {
342 register u_int32_t a
, b
, c
, d
;
349 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
350 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
351 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
352 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
353 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
354 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
355 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
356 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
357 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
358 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
359 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
360 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
361 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
362 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
363 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
364 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
366 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
367 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
368 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
369 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
370 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
371 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
372 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
373 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
374 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
375 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
376 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
377 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
378 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
379 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
380 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
381 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
383 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
384 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
385 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
386 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
387 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
388 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
389 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
390 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
391 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
392 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
393 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
394 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
395 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
396 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
397 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
398 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
400 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
401 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
402 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
403 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
404 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
405 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
406 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
407 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
408 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
409 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
410 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
411 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
412 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
413 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
414 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
415 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
424 * Update context to reflect the concatenation of another buffer full
428 md5_update(md5_t
*ctx
, const unsigned char *buf
, unsigned int len
)
432 /* Update byte count */
435 if ((ctx
->bytes
[0] = t
+ len
) < t
)
436 ctx
->bytes
[1]++; /* Carry from low to high */
438 t
= 64 - (t
& 0x3f); /* Space available in ctx->in (at least 1) */
440 memcpy((unsigned char *)ctx
->in
+ 64 - t
, buf
, len
);
443 /* First chunk is an odd size */
444 memcpy((unsigned char *)ctx
->in
+ 64 - t
, buf
, t
);
445 byteSwap(ctx
->in
, 16);
446 transform(ctx
->buf
, ctx
->in
);
450 /* Process data in 64-byte chunks */
452 memcpy(ctx
->in
, buf
, 64);
453 byteSwap(ctx
->in
, 16);
454 transform(ctx
->buf
, ctx
->in
);
459 /* Handle any remaining bytes of data. */
460 memcpy(ctx
->in
, buf
, len
);
464 * Final wrapup - pad to 64-byte boundary with the bit pattern
465 * 1 0* (64-bit count of bits processed, MSB-first)
468 md5_final(md5_t
*ctx
, unsigned char *digest
)
470 int count
= ctx
->bytes
[0] & 0x3f; /* Number of bytes in ctx->in */
471 unsigned char *p
= (unsigned char *)ctx
->in
+ count
;
473 /* Set the first char of padding to 0x80. There is always room. */
476 /* Bytes of padding needed to make 56 bytes (-8..55) */
477 count
= 56 - 1 - count
;
479 if (count
< 0) { /* Padding forces an extra block */
480 memset(p
, 0, count
+ 8);
481 byteSwap(ctx
->in
, 16);
482 transform(ctx
->buf
, ctx
->in
);
483 p
= (unsigned char *)ctx
->in
;
487 byteSwap(ctx
->in
, 14);
489 /* Append length in bits and transform */
490 ctx
->in
[14] = ctx
->bytes
[0] << 3;
491 ctx
->in
[15] = ctx
->bytes
[1] << 3 | ctx
->bytes
[0] >> 29;
492 transform(ctx
->buf
, ctx
->in
);
494 byteSwap(ctx
->buf
, 4);
495 memcpy(digest
, ctx
->buf
, 16);
496 memset(ctx
, 0, sizeof(md5_t
)); /* In case it's sensitive */