summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c')
-rwxr-xr-xutils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c b/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c
new file mode 100755
index 0000000000..c28770a05b
--- /dev/null
+++ b/utils/zenutils/libraries/beecrypt-4.1.2/beecrypt/hmac.c
@@ -0,0 +1,123 @@
1/*
2 * Copyright (c) 1999, 2000, 2002 Virtual Unlimited B.V.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 */
19
20/*!\file hmac.c
21 * \brief HMAC algorithm.
22 *
23 * \see RFC2104 HMAC: Keyed-Hashing for Message Authentication.
24 * H. Krawczyk, M. Bellare, R. Canetti.
25 *
26 * \author Bob Deblier <bob.deblier@pandore.be>
27 * \ingroup HMAC_m
28 */
29
30#define BEECRYPT_DLL_EXPORT
31
32#if HAVE_CONFIG_H
33# include "config.h"
34#endif
35
36#include "beecrypt/hmac.h"
37#include "beecrypt/endianness.h"
38
39/*!\addtogroup HMAC_m
40 * \{
41 */
42
43#define HMAC_IPAD 0x36
44#define HMAC_OPAD 0x5c
45
46int hmacSetup(byte* kxi, byte* kxo, const hashFunction* hash, hashFunctionParam* param, const byte* key, size_t keybits)
47{
48 register unsigned int i;
49
50 size_t keybytes = keybits >> 3;
51
52 /* if the key is too large, hash it first */
53 if (keybytes > hash->blocksize)
54 {
55 /* if the hash digest is too large, this doesn't help; this is really a sanity check */
56 if (hash->digestsize > hash->blocksize)
57 return -1;
58
59 if (hash->reset(param))
60 return -1;
61
62 if (hash->update(param, key, keybytes))
63 return -1;
64
65 if (hash->digest(param, kxi))
66 return -1;
67
68 memcpy(kxo, kxi, keybytes = hash->digestsize);
69 }
70 else if (keybytes > 0)
71 {
72 memcpy(kxi, key, keybytes);
73 memcpy(kxo, key, keybytes);
74 }
75 else
76 return -1;
77
78 for (i = 0; i < keybytes; i++)
79 {
80 kxi[i] ^= HMAC_IPAD;
81 kxo[i] ^= HMAC_OPAD;
82 }
83
84 for (i = keybytes; i < hash->blocksize; i++)
85 {
86 kxi[i] = HMAC_IPAD;
87 kxo[i] = HMAC_OPAD;
88 }
89
90 return hmacReset(kxi, hash, param);
91}
92
93int hmacReset(const byte* kxi, const hashFunction* hash, hashFunctionParam* param)
94{
95 if (hash->reset(param))
96 return -1;
97 if (hash->update(param, kxi, hash->blocksize))
98 return -1;
99
100 return 0;
101}
102
103int hmacUpdate(const hashFunction* hash, hashFunctionParam* param, const byte* data, size_t size)
104{
105 return hash->update(param, data, size);
106}
107
108int hmacDigest(const byte* kxo, const hashFunction* hash, hashFunctionParam* param, byte* data)
109{
110 if (hash->digest(param, data))
111 return -1;
112 if (hash->update(param, kxo, hash->blocksize))
113 return -1;
114 if (hash->update(param, data, hash->digestsize))
115 return -1;
116 if (hash->digest(param, data))
117 return -1;
118
119 return 0;
120}
121
122/*!\}
123 */