5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
24 /* Allocates a new SilcHmac object. First argument is the hash function
25 object to tell the hmac which hash function should be used when creating
26 HMAC's. The new SilcHmac object is returned to new_hmac argument. */
28 int silc_hmac_alloc(SilcHash hash, SilcHmac *new_hmac)
30 SILC_LOG_DEBUG(("Allocating new hmac object"));
32 *new_hmac = silc_calloc(1, sizeof(**new_hmac));
33 (*new_hmac)->hash = hash;
34 (*new_hmac)->set_key = silc_hmac_set_key;
35 (*new_hmac)->make_hmac = silc_hmac_make;
36 (*new_hmac)->make_hmac_with_key = silc_hmac_make_with_key;
37 (*new_hmac)->make_hmac_truncated = silc_hmac_make_truncated;
42 /* Free's the SilcHmac object. */
44 void silc_hmac_free(SilcHmac hmac)
50 /* Creates the HMAC. The created keyed hash value is returned to
51 return_hash argument. */
53 void silc_hmac_make_internal(SilcHmac hmac, unsigned char *data,
54 unsigned int data_len, unsigned char *key,
55 unsigned int key_len, unsigned char *return_hash)
57 SilcHash hash = hmac->hash;
58 unsigned char inner_pad[hash->hash->block_len + 1];
59 unsigned char outer_pad[hash->hash->block_len + 1];
60 unsigned char hvalue[hash->hash->hash_len];
64 SILC_LOG_DEBUG(("Making HMAC for message"));
66 hash_context = silc_calloc(1, hash->hash->context_len());
68 memset(inner_pad, 0, sizeof(inner_pad));
69 memset(outer_pad, 0, sizeof(outer_pad));
71 /* If the key length is more than block size of the hash function, the
73 if (key_len > hash->hash->block_len) {
74 hash->make_hash(hash, key, key_len, hvalue);
76 key_len = hash->hash->hash_len;
79 /* Copy the key into the pads */
80 memcpy(inner_pad, key, key_len);
81 memcpy(outer_pad, key, key_len);
83 /* XOR the key with pads */
84 for (i = 0; i < hash->hash->block_len; i++) {
89 /* Do the HMAC transform (too bad I can't do make_hash directly, sigh) */
90 hash->hash->init(hash_context);
91 hash->hash->update(hash_context, inner_pad, hash->hash->block_len);
92 hash->hash->update(hash_context, data, data_len);
93 hash->hash->final(hash_context, return_hash);
94 hash->hash->init(hash_context);
95 hash->hash->update(hash_context, outer_pad, hash->hash->block_len);
96 hash->hash->update(hash_context, return_hash, hash->hash->hash_len);
97 hash->hash->final(hash_context, return_hash);
100 /* Create the HMAC. This is thee make_hmac function pointer. This
101 uses the internal key set with silc_hmac_set_key. */
103 void silc_hmac_make(SilcHmac hmac, unsigned char *data,
104 unsigned int data_len, unsigned char *return_hash)
106 silc_hmac_make_internal(hmac, data, data_len, hmac->key,
107 hmac->key_len, return_hash);
110 /* Creates the HMAC just as above except that the hash value is truncated
111 to the truncated_len sent as argument. NOTE: One should not truncate to
112 less than half of the length of original hash value. However, this
113 routine allows these dangerous truncations. */
115 void silc_hmac_make_truncated(SilcHmac hmac, unsigned char *data,
116 unsigned int data_len,
117 unsigned int truncated_len,
118 unsigned char *return_hash)
120 unsigned char hvalue[hmac->hash->hash->hash_len];
122 silc_hmac_make_internal(hmac, data, data_len,
123 hmac->key, hmac->key_len, hvalue);
124 memcpy(return_hash, hvalue, truncated_len);
125 memset(hvalue, 0, sizeof(hvalue));
128 /* Creates HMAC just as above except that this doesn't use the internal
129 key. The key is sent as argument to the function. */
131 void silc_hmac_make_with_key(SilcHmac hmac, unsigned char *data,
132 unsigned int data_len,
133 unsigned char *key, unsigned int key_len,
134 unsigned char *return_hash)
136 silc_hmac_make_internal(hmac, data, data_len, key, key_len, return_hash);
139 /* Sets the HMAC key used in the HMAC creation */
141 void silc_hmac_set_key(SilcHmac hmac, const unsigned char *key,
142 unsigned int key_len)
144 hmac->key = silc_calloc(key_len, sizeof(unsigned char));
145 hmac->key_len = key_len;
146 memcpy(hmac->key, key, key_len);