1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2001-2006 Free Software Foundation Europe e.V.
5    Copyright (C) 2013-2020 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version two of the GNU Lesser General
9    Public License as published by the Free Software Foundation plus
10    additions in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Lesser Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Hashed Message Authentication Code using MD5 (HMAC-MD5)
24  *
25  * hmac_md5 was based on sample code in RFC2104 (thanks guys).
26  *
27  * Adapted to BACULA by Kern E. Sibbald, February MMI.
28  */
29 
30 #include "include/bareos.h"
31 
32 #define PAD_LEN 64 /* PAD length */
33 #define SIG_LEN 16 /* MD5 digest length */
34 
hmac_md5(uint8_t * text,int text_len,uint8_t * key,int key_len,uint8_t * hmac)35 void hmac_md5(uint8_t* text, /* pointer to data stream */
36               int text_len,  /* length of data stream */
37               uint8_t* key,  /* pointer to authentication key */
38               int key_len,   /* length of authentication key */
39               uint8_t* hmac) /* returned hmac-md5 */
40 {
41   MD5_CTX md5c;
42   uint8_t k_ipad[PAD_LEN]; /* inner padding - key XORd with ipad */
43   uint8_t k_opad[PAD_LEN]; /* outer padding - key XORd with opad */
44   uint8_t keysig[SIG_LEN];
45   int i;
46 
47   /* if key is longer than PAD length, reset it to key=MD5(key) */
48   if (key_len > PAD_LEN) {
49     MD5_CTX md5key;
50 
51     MD5_Init(&md5key);
52     MD5_Update(&md5key, key, key_len);
53     MD5_Final(keysig, &md5key);
54 
55     key = keysig;
56     key_len = SIG_LEN;
57   }
58 
59   /*
60    * the HMAC_MD5 transform looks like:
61    *
62    * MD5(Key XOR opad, MD5(Key XOR ipad, text))
63    *
64    * where Key is an n byte key
65    * ipad is the byte 0x36 repeated 64 times
66 
67    * opad is the byte 0x5c repeated 64 times
68    * and text is the data being protected
69    */
70 
71   /* Zero pads and store key */
72   memset(k_ipad, 0, PAD_LEN);
73   memcpy(k_ipad, key, key_len);
74   memcpy(k_opad, k_ipad, PAD_LEN);
75 
76   /* XOR key with ipad and opad values */
77   for (i = 0; i < PAD_LEN; i++) {
78     k_ipad[i] ^= 0x36;
79     k_opad[i] ^= 0x5c;
80   }
81 
82   /* perform inner MD5 */
83   MD5_Init(&md5c);                    /* start inner hash */
84   MD5_Update(&md5c, k_ipad, PAD_LEN); /* hash inner pad */
85   MD5_Update(&md5c, text, text_len);  /* hash text */
86   MD5_Final(hmac, &md5c);             /* store inner hash */
87 
88   /* perform outer MD5 */
89   MD5_Init(&md5c);                    /* start outer hash */
90   MD5_Update(&md5c, k_opad, PAD_LEN); /* hash outer pad */
91   MD5_Update(&md5c, hmac, SIG_LEN);   /* hash inner hash */
92   MD5_Final(hmac, &md5c);             /* store results */
93 }
94 /*
95 Test Vectors (Trailing '\0' of a character string not included in test):
96 
97   key =         0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
98   key_len =     16 bytes
99   data =        "Hi There"
100   data_len =    8  bytes
101   digest =      0x9294727a3638bb1c13f48ef8158bfc9d
102 
103   key =         "Jefe"
104   data =        "what do ya want for nothing?"
105   data_len =    28 bytes
106   digest =      0x750c783e6ab0b503eaa86e310a5db738
107 
108   key =         0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
109 
110   key_len       16 bytes
111   data =        0xDDDDDDDDDDDDDDDDDDDD...
112                 ..DDDDDDDDDDDDDDDDDDDD...
113                 ..DDDDDDDDDDDDDDDDDDDD...
114                 ..DDDDDDDDDDDDDDDDDDDD...
115                 ..DDDDDDDDDDDDDDDDDDDD
116   data_len =    50 bytes
117   digest =      0x56be34521d144c88dbb8c733f0e8b3f6
118 */
119