1 /*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2015 Roy Marples <roy@marples.name>
4 * All rights reserved
5
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <inttypes.h>
29 #include <string.h>
30
31 #include "crypt.h"
32
33 #include "../config.h"
34 #ifdef HAVE_MD5_H
35 # ifndef DEPGEN
36 # include <md5.h>
37 # endif
38 #else
39 # include "md5.h"
40 #endif
41
42 #define HMAC_PAD_LEN 64
43 #define IPAD 0x36
44 #define OPAD 0x5C
45
46 /* hmac_md5 as per RFC3118 */
47 void
hmac_md5(const uint8_t * text,size_t text_len,const uint8_t * key,size_t key_len,uint8_t * digest)48 hmac_md5(const uint8_t *text, size_t text_len,
49 const uint8_t *key, size_t key_len,
50 uint8_t *digest)
51 {
52 uint8_t k_ipad[HMAC_PAD_LEN], k_opad[HMAC_PAD_LEN];
53 uint8_t tk[MD5_DIGEST_LENGTH];
54 int i;
55 MD5_CTX context;
56
57 /* Ensure key is no bigger than HMAC_PAD_LEN */
58 if (key_len > HMAC_PAD_LEN) {
59 MD5Init(&context);
60 MD5Update(&context, key, (unsigned int)key_len);
61 MD5Final(tk, &context);
62 key = tk;
63 key_len = MD5_DIGEST_LENGTH;
64 }
65
66 /* store key in pads */
67 memcpy(k_ipad, key, key_len);
68 memcpy(k_opad, key, key_len);
69 memset(k_ipad + key_len, 0, sizeof(k_ipad) - key_len);
70 memset(k_opad + key_len, 0, sizeof(k_opad) - key_len);
71
72 /* XOR key with ipad and opad values */
73 for (i = 0; i < HMAC_PAD_LEN; i++) {
74 k_ipad[i] ^= IPAD;
75 k_opad[i] ^= OPAD;
76 }
77
78 /* inner MD5 */
79 MD5Init(&context);
80 MD5Update(&context, k_ipad, HMAC_PAD_LEN);
81 MD5Update(&context, text, (unsigned int)text_len);
82 MD5Final(digest, &context);
83
84 /* outer MD5 */
85 MD5Init(&context);
86 MD5Update(&context, k_opad, HMAC_PAD_LEN);
87 MD5Update(&context, digest, MD5_DIGEST_LENGTH);
88 MD5Final(digest, &context);
89 }
90