1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
/* hmac.c
*
* Copyright (C) 2009 Tillmann Werner <tillmann.werner@gmx.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation. You may not use, modify or
* distribute this program under any other version of the GNU General
* Public License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* This code is based on an OpenSSL-compatible implementation of the RSA
* Data Security, * Inc. MD5 Message-Digest Algorithm, written by Solar
* Designer <solar at openwall.com> in 2001, and placed in the public
* domain. There's absolutely no warranty.
*
* This implementation is meant to be fast, but not as fast as possible.
* Some known optimizations are not included to reduce source code size
* and avoid compile-time configuration.
*
*
* $Id$
*/
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "hmac.h"
#include "sha512.h"
char *hmac(const u_char *ipad, const u_char *opad, u_char **msg, ssize_t len) {
char *inner, *outer;
// append inner padding to message
if ((*msg = realloc(*msg, len + HMAC_BLOCK_SIZE)) == NULL) return NULL;
memcpy(*msg + len, ipad, HMAC_BLOCK_SIZE);
// compute inner hash
if ((inner = mem_sha512sum(*msg, len+HMAC_BLOCK_SIZE)) == NULL) return(NULL);
// append outer padding to iner hash
if ((inner = realloc(inner, HMAC_HASH_SIZE+HMAC_BLOCK_SIZE)) == NULL) {
free(inner);
return NULL;
}
memcpy(&inner[HMAC_HASH_SIZE], opad, HMAC_BLOCK_SIZE);
// compute outer hash
outer = mem_sha512sum((u_char *) inner, HMAC_HASH_SIZE+HMAC_BLOCK_SIZE);
free(inner);
return outer;
}
|