Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2008 Frediano Ziglio
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <config.h>
21 :
22 : #if HAVE_STRING_H
23 : #include <string.h>
24 : #endif /* HAVE_STRING_H */
25 :
26 : #include <tds_sysdep_public.h>
27 : #include <freetds/utils/md5.h>
28 : #include <freetds/utils/hmac_md5.h>
29 : #include <memory.h>
30 :
31 : /**
32 : * Calculates the HMAC-MD5 hash of the given data using the specified
33 : * hashing key.
34 : *
35 : * @param key The hashing key.
36 : * @param data The data for which the hash will be calculated.
37 : * @param data_len data length.
38 : * @param digest The HMAC-MD5 hash of the given data.
39 : */
40 2142 : void hmac_md5(const unsigned char key[16],
41 : const unsigned char* data, size_t data_len,
42 : unsigned char* digest)
43 : {
44 : int i;
45 : MD5_CTX ctx;
46 : unsigned char k_ipad[64];
47 : unsigned char k_opad[64];
48 :
49 : /* compute ipad and opad */
50 2142 : memset(k_ipad, 0x36, sizeof(k_ipad));
51 2142 : memset(k_opad, 0x5c, sizeof(k_opad));
52 36414 : for (i=0; i<16; ++i) {
53 34272 : k_ipad[i] ^= key[i];
54 34272 : k_opad[i] ^= key[i];
55 : }
56 :
57 2142 : MD5Init(&ctx);
58 2142 : MD5Update(&ctx, k_ipad, 64);
59 2142 : if (data_len != 0)
60 1067 : MD5Update(&ctx, data, data_len);
61 2142 : MD5Final(&ctx, digest);
62 :
63 2142 : MD5Init(&ctx);
64 2142 : MD5Update(&ctx, k_opad, 64);
65 2142 : MD5Update(&ctx, digest, 16);
66 2142 : MD5Final(&ctx, digest);
67 2142 : }
68 :
|