1 /*
2 Bacula(R) - The Network Backup Solution
3
4 Copyright (C) 2000-2020 Kern Sibbald
5
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
8
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
13
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
16
17 Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19
20 /*
21 * Hashed Message Authentication Code using MD5 (HMAC-MD5)
22 *
23 * hmac_md5 was based on sample code in RFC2104 (thanks guys).
24 *
25 * Adapted to Bacula by Kern E. Sibbald, February MMI.
26 */
27 #include "bacula.h"
28
29 #define PAD_LEN 64 /* PAD length */
30 #define SIG_LEN MD5HashSize /* MD5 digest length */
31
32 void
hmac_md5(uint8_t * text,int text_len,uint8_t * key,int key_len,uint8_t * hmac)33 hmac_md5(
34 uint8_t* text, /* pointer to data stream */
35 int text_len, /* length of data stream */
36 uint8_t* key, /* pointer to authentication key */
37 int key_len, /* length of authentication key */
38 uint8_t *hmac) /* returned hmac-md5 */
39 {
40 MD5Context md5c;
41 uint8_t k_ipad[PAD_LEN]; /* inner padding - key XORd with ipad */
42 uint8_t k_opad[PAD_LEN]; /* outer padding - key XORd with opad */
43 uint8_t keysig[SIG_LEN];
44 int i;
45
46 /* if key is longer than PAD length, reset it to key=MD5(key) */
47 if (key_len > PAD_LEN) {
48 MD5Context md5key;
49
50 MD5Init(&md5key);
51 MD5Update(&md5key, key, key_len);
52 MD5Final(keysig, &md5key);
53
54 key = keysig;
55 key_len = SIG_LEN;
56 }
57
58 /*
59 * the HMAC_MD5 transform looks like:
60 *
61 * MD5(Key XOR opad, MD5(Key XOR ipad, text))
62 *
63 * where Key is an n byte key
64 * ipad is the byte 0x36 repeated 64 times
65
66 * opad is the byte 0x5c repeated 64 times
67 * and text is the data being protected
68 */
69
70 /* Zero pads and store key */
71 memset(k_ipad, 0, PAD_LEN);
72 memcpy(k_ipad, key, key_len);
73 memcpy(k_opad, k_ipad, PAD_LEN);
74
75 /* XOR key with ipad and opad values */
76 for (i=0; i<PAD_LEN; i++) {
77 k_ipad[i] ^= 0x36;
78 k_opad[i] ^= 0x5c;
79 }
80
81 /* perform inner MD5 */
82 MD5Init(&md5c); /* start inner hash */
83 MD5Update(&md5c, k_ipad, PAD_LEN); /* hash inner pad */
84 MD5Update(&md5c, text, text_len); /* hash text */
85 MD5Final(hmac, &md5c); /* store inner hash */
86
87 /* perform outer MD5 */
88 MD5Init(&md5c); /* start outer hash */
89 MD5Update(&md5c, k_opad, PAD_LEN); /* hash outer pad */
90 MD5Update(&md5c, hmac, SIG_LEN); /* hash inner hash */
91 MD5Final(hmac, &md5c); /* store results */
92 }
93 /*
94 Test Vectors (Trailing '\0' of a character string not included in test):
95
96 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
97 key_len = 16 bytes
98 data = "Hi There"
99 data_len = 8 bytes
100 digest = 0x9294727a3638bb1c13f48ef8158bfc9d
101
102 key = "Jefe"
103 data = "what do ya want for nothing?"
104 data_len = 28 bytes
105 digest = 0x750c783e6ab0b503eaa86e310a5db738
106
107 key = 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
108
109 key_len 16 bytes
110 data = 0xDDDDDDDDDDDDDDDDDDDD...
111 ..DDDDDDDDDDDDDDDDDDDD...
112 ..DDDDDDDDDDDDDDDDDDDD...
113 ..DDDDDDDDDDDDDDDDDDDD...
114 ..DDDDDDDDDDDDDDDDDDDD
115 data_len = 50 bytes
116 digest = 0x56be34521d144c88dbb8c733f0e8b3f6
117 */
118