Integer type name change.
[silc.git] / lib / silccrypt / silchmac.h
1 /*
2
3   silchmac.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1999 - 2001 Pekka Riikonen
8
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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef SILCHMAC_H
21 #define SILCHMAC_H
22
23 /****h* silccrypt/SilcHMACAPI
24  *
25  * DESCRIPTION
26  *
27  *    This is the interface for HMAC, or the keyed hash values, that are
28  *    used for packet and message authentication.  These routines uses
29  *    already implemented hash functions from the SilcHashAPI. These 
30  *    routines were created according to RFC 2104.
31  *
32  ***/
33
34 /****s* silccrypt/SilcHMACAPI/SilcHmac
35  *
36  * NAME
37  * 
38  *    typedef struct SilcHmacStruct *SilcHmac;
39  *
40  * DESCRIPTION
41  *
42  *    This context is the actual HMAC context and is allocated
43  *    by silc_hmac_alloc and given as argument usually to all
44  *    silc_hmac_* functions.  It is freed by the silc_hmac_free
45  *    function.
46  *
47  ***/
48 typedef struct SilcHmacStruct *SilcHmac;
49
50 /****s* silccrypt/SilcHMACAPI/SilcHmacObject
51  *
52  * NAME
53  * 
54  *    typedef struct { ... } SilcHmacObject;
55  *
56  * DESCRIPTION
57  *
58  *    This structure represents one HMAC.  The HMAC's name and the
59  *    MAC length is defined in the structure.  This structure is
60  *    then given as argument to the silc_hmac_register.  That function
61  *    is used to register all HMACs into SILC.  They can be then
62  *    allocated by the name found in this structure by calling the
63  *    silc_hmac_alloc.
64  *
65  ***/
66 typedef struct {
67   char *name;
68   SilcUInt32 len;
69 } SilcHmacObject;
70
71 /* Marks for all hmacs. This can be used in silc_hmac_unregister
72    to unregister all hmacs at once. */
73 #define SILC_ALL_HMACS ((SilcHmacObject *)1)
74
75 /* Default hmacs for silc_hmac_register_default(). */
76 extern DLLAPI SilcHmacObject silc_default_hmacs[];
77
78 /* Default HMAC in the SILC protocol */
79 #define SILC_DEFAULT_HMAC "hmac-sha1-96"
80
81 /* Prototypes */
82
83 /****f* silccrypt/SilcHMACAPI/silc_hmac_register
84  *
85  * SYNOPSIS
86  *
87  *    bool silc_hmac_register(SilcHmacObject *hmac);
88  *
89  * DESCRIPTION
90  *
91  *    Registers a new HMAC into the SILC. This function is used at the
92  *    initialization of the SILC.  All registered HMACs should be
93  *    unregistered with silc_hmac_unregister.  The `hmac' includes the
94  *    name of the HMAC and the length of the MAC.  Usually this
95  *    function is not called directly.  Instead, application can call
96  *    the silc_hmac_register_default to register all default HMACs
97  *    that are builtin the sources.  Returns FALSE on error.
98  *
99  ***/
100 bool silc_hmac_register(SilcHmacObject *hmac);
101
102 /****f* silccrypt/SilcHMACAPI/silc_hmac_unregister
103  *
104  * SYNOPSIS
105  *
106  *    bool silc_hmac_unregister(SilcHmacObject *hmac);
107  *
108  * DESCRIPTION
109  *
110  *    Unregister a HMAC from SILC by the HMAC structure `hmac'.  This
111  *    should be called for all registered HMAC's.  Returns FALSE on
112  *    error.
113  *
114  ***/
115 bool silc_hmac_unregister(SilcHmacObject *hmac);
116
117 /****f* silccrypt/SilcHMACAPI/silc_hmac_register_default
118  *
119  * SYNOPSIS
120  *
121  *    bool silc_hmac_register_default(void);
122  *
123  * DESCRIPTION
124  *
125  *    Registers all default HMACs into the SILC.  These are the HMACs
126  *    that are builtin in the sources.  See the list of default HMACs
127  *    in the silchmac.c source file.  The application may use this
128  *    to register default HMACs if specific HMAC in any specific order
129  *    is not wanted (application's configuration usually may decide
130  *    the order of the registration, in which case this should not be
131  *    used).
132  *
133  ***/
134 bool silc_hmac_register_default(void);
135
136 /****f* silccrypt/SilcHMACAPI/silc_hmac_alloc
137  *
138  * SYNOPSIS
139  *
140  *    bool silc_hmac_alloc(char *name, SilcHash hash, SilcHmac *new_hmac);
141  *
142  * DESCRIPTION
143  *
144  *    Allocates a new SilcHmac object of name of `name'.  The `hash' may
145  *    be provided as argument.  If provided it is used as the hash function
146  *    of the HMAC.  If it is NULL then the hash function is allocated and
147  *    the name of the hash algorithm is derived from the `name'.  Returns
148  *    FALSE if such HMAC does not exist.
149  *
150  ***/
151 bool silc_hmac_alloc(char *name, SilcHash hash, SilcHmac *new_hmac);
152
153 /****f* silccrypt/SilcHMACAPI/silc_hmac_free
154  *
155  * SYNOPSIS
156  *
157  *    void silc_hmac_free(SilcHmac hmac);
158  *
159  * DESCRIPTION
160  *
161  *    Frees the allocated HMAC context.  The key that may have been set
162  *    with the silc_hmac_set_key is also destroyed.
163  *
164  ***/
165 void silc_hmac_free(SilcHmac hmac);
166
167 /****f* silccrypt/SilcHMACAPI/silc_hmac_is_supported
168  *
169  * SYNOPSIS
170  *
171  *    bool silc_hmac_is_supported(const char *name);
172  *
173  * DESCRIPTION
174  *
175  *    Returns TRUE if the HMAC indicated by the `name' exists.
176  *
177  ***/
178 bool silc_hmac_is_supported(const char *name);
179
180 /****f* silccrypt/SilcHMACAPI/silc_hmac_get_supported
181  *
182  * SYNOPSIS
183  *
184  *    char *silc_hmac_get_supported(void);
185  *
186  * DESCRIPTION
187  *
188  *    Returns comma (`,') separated list of registered HMACs.  This is
189  *    used for example when sending supported HMAC list during the SILC
190  *    Key Exchange protocol (SKE).  The caller must free the returned
191  *    pointer.
192  *
193  ***/
194 char *silc_hmac_get_supported(void);
195
196 /****f* silccrypt/SilcHMACAPI/silc_hmac_len
197  *
198  * SYNOPSIS
199  *
200  *    SilcUInt32 silc_hmac_len(SilcHmac hmac);
201  *
202  * DESCRIPTION
203  *
204  *    Returns the length of the MAC that the HMAC will produce.
205  *
206  ***/
207 SilcUInt32 silc_hmac_len(SilcHmac hmac);
208
209 /****f* silccrypt/SilcHMACAPI/silc_hmac_get_hash
210  *
211  * SYNOPSIS
212  *
213  *    SilcHash silc_hmac_get_hash(SilcHmac hmac);
214  *
215  * DESCRIPTION
216  *
217  *    Returns the SilcHash context that has been associated with the
218  *    HMAC context.  The caller must not free the returned context.
219  *
220  ***/
221 SilcHash silc_hmac_get_hash(SilcHmac hmac);
222
223 /****f* silccrypt/SilcHMACAPI/silc_hmac_get_name
224  *
225  * SYNOPSIS
226  *
227  *    const char *silc_hmac_get_name(SilcHmac hmac);
228  *
229  * DESCRIPTION
230  *
231  *    Returns the name of the HMAC context.
232  *
233  ***/
234 const char *silc_hmac_get_name(SilcHmac hmac);
235
236 /****f* silccrypt/SilcHMACAPI/silc_hmac_set_key
237  *
238  * SYNOPSIS
239  *
240  *    void silc_hmac_set_key(SilcHmac hmac, const unsigned char *key,
241  *                           SilcUInt32 key_len);
242  *
243  * DESCRIPTION
244  *
245  *    Sets the key to be used in the HMAC operation.  This must be set
246  *    before calling silc_hmac_make or silc_hmac_final functions.  If
247  *    you do not want to set the key you can still produce a MAC by
248  *    calling the silc_hmac_make_with_key where you give the key as
249  *    argument.  Usually application still wants to set the key.
250  *
251  ***/
252 void silc_hmac_set_key(SilcHmac hmac, const unsigned char *key,
253                        SilcUInt32 key_len);
254
255 /****f* silccrypt/SilcHMACAPI/silc_hmac_make
256  *
257  * SYNOPSIS
258  *
259  *    void silc_hmac_make(SilcHmac hmac, unsigned char *data,
260  *                        SilcUInt32 data_len, unsigned char *return_hash,
261  *                        SilcUInt32 *return_len);
262  *
263  * DESCRIPTION
264  *
265  *    Computes a MAC from a data buffer indicated by the `data' of the
266  *    length of `data_len'.  The returned MAC is copied into the 
267  *    `return_hash' pointer which must be at least the size of the
268  *    value silc_hmac_len returns.  The returned length is still
269  *    returned to `return_len'.
270  *
271  ***/
272 void silc_hmac_make(SilcHmac hmac, unsigned char *data,
273                     SilcUInt32 data_len, unsigned char *return_hash,
274                     SilcUInt32 *return_len);
275
276 /****f* silccrypt/SilcHMACAPI/silc_hmac_make_with_key
277  *
278  * SYNOPSIS
279  *
280  *    void silc_hmac_make_with_key(SilcHmac hmac, unsigned char *data,
281  *                                 SilcUInt32 data_len, 
282  *                                 unsigned char *key, SilcUInt32 key_len,
283  *                                 unsigned char *return_hash,
284  *                                 SilcUInt32 *return_len);
285  *
286  * DESCRIPTION
287  *
288  *    Same as the silc_hmac_make but takes the key for the HMAC as
289  *    argument.  If this is used the key that may have been set by calling
290  *    silc_hmac_set_key is ignored.
291  *
292  ***/
293 void silc_hmac_make_with_key(SilcHmac hmac, unsigned char *data,
294                              SilcUInt32 data_len, 
295                              unsigned char *key, SilcUInt32 key_len,
296                              unsigned char *return_hash,
297                              SilcUInt32 *return_len);
298
299 /****f* silccrypt/SilcHMACAPI/silc_hmac_make_truncated
300  *
301  * SYNOPSIS
302  *
303  *    void silc_hmac_make_truncated(SilcHmac hmac, 
304  *                                  unsigned char *data, 
305  *                                  SilcUInt32 data_len,
306  *                                  SilcUInt32 truncated_len,
307  *                                  unsigned char *return_hash);
308  *
309  * DESCRIPTION
310  *
311  *    Same as the silc_hmac_make except that the returned MAC is
312  *    truncated to the length indicated by the `truncated_len'.  Some
313  *    special applications may need this function.  The `return_hash'
314  *    must be at least the size of `truncated_len'.
315  *
316  * NOTES
317  *
318  *    For security reasons, one should not truncate to less than half
319  *    of the length of the true MAC lenght.  However, since this routine
320  *    may be used to non-critical applications this allows these dangerous
321  *    truncations.
322  *
323  ***/
324 void silc_hmac_make_truncated(SilcHmac hmac, 
325                               unsigned char *data, 
326                               SilcUInt32 data_len,
327                               SilcUInt32 truncated_len,
328                               unsigned char *return_hash);
329
330 /****f* silccrypt/SilcHMACAPI/silc_hmac_init
331  *
332  * SYNOPSIS
333  *
334  *    void silc_hmac_init(SilcHmac hmac);
335  *
336  * DESCRIPTION
337  *
338  *    Sometimes calling the silc_hmac_make might not be the most
339  *    optimal case of doing MACs.  If you have a lot of different data
340  *    that you need to put together for computing a MAC you may either
341  *    put them into a buffer and compute the MAC from the buffer by
342  *    calling the silc_hmac_make, or you can use the silc_hmac_init,
343  *    silc_hmac_update and silc_hmac_final to do the MAC.  This function
344  *    prepares the allocated HMAC context for this kind of MAC 
345  *    computation.  The caller must have been called the function
346  *    silc_hmac_set_key before calling this function.  To add the
347  *    data to be used in the MAC computation call the silc_hmac_update
348  *    function.
349  *
350  ***/
351 void silc_hmac_init(SilcHmac hmac);
352
353 /****f* silccrypt/SilcHMACAPI/silc_hmac_init_with_key
354  *
355  * SYNOPSIS
356  *
357  *    void silc_hmac_init_with_key(SilcHmac hmac, const unsigned char *key,
358  *                                 SilcUInt32 key_len);
359  *
360  * DESCRIPTION
361  *
362  *    Same as silc_hmac_init but initializes with specific key.  The
363  *    key that may have been set with silc_hmac_set_key is ignored.
364  *
365  ***/
366 void silc_hmac_init_with_key(SilcHmac hmac, const unsigned char *key,
367                              SilcUInt32 key_len);
368
369 /****f* silccrypt/SilcHMACAPI/silc_hmac_update
370  *
371  * SYNOPSIS
372  *
373  *    void silc_hmac_update(SilcHmac hmac, const unsigned char *data,
374  *                          SilcUInt32 data_len);
375  *
376  * DESCRIPTION
377  *
378  *    This function may be called to add data to be used in the MAC
379  *    computation.  This can be called multiple times to add data from
380  *    many sources before actually performing the HMAC.  Once you've
381  *    added all the data you need you can call the silc_hmac_final to
382  *    actually produce the MAC.
383  *
384  * EXAMPLE
385  *
386  *    unsigned char mac[20];
387  *    SilcUInt32 mac_len;
388  *
389  *    silc_hmac_init(hmac);
390  *    silc_hmac_update(hmac, data, data_len);
391  *    silc_hmac_update(hmac, more_data, more_data_len);
392  *    silc_hmac_final(hmac, mac, &mac_len);
393  *
394  ***/
395 void silc_hmac_update(SilcHmac hmac, const unsigned char *data,
396                       SilcUInt32 data_len);
397
398 /****f* silccrypt/SilcHMACAPI/silc_hmac_final
399  *
400  * SYNOPSIS
401  *
402  *    void silc_hmac_final(SilcHmac hmac, unsigned char *return_hash,
403  *                         SilcUInt32 *return_len);
404  *
405  * DESCRIPTION
406  *
407  *    This function is used to produce the final MAC from the data
408  *    that has been added to the HMAC context by calling the 
409  *    silc_hmac_update function.  The MAC is copied in to the
410  *    `return_hash' pointer which must be at least the size that
411  *    the silc_hmac_len returns.  The length of the MAC is still
412  *    returned into `return_len'.
413  *
414  ***/
415 void silc_hmac_final(SilcHmac hmac, unsigned char *return_hash,
416                      SilcUInt32 *return_len);
417
418 #endif