Added assembler AES for x86 and x86_64.
[crypto.git] / lib / silccrypt / silccipher.h
1 /*
2
3   silccipher.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2006 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 SILCCIPHER_H
21 #define SILCCIPHER_H
22
23 /****h* silccrypt/SILC Cipher Interface
24  *
25  * DESCRIPTION
26  *
27  * This is the interface for cipher functions.  It provides cipher
28  * registering and unregistering routines, encryption and decryption
29  * routines.
30  *
31  ***/
32
33 /****s* silccrypt/SilcCipherAPI/SilcCipher
34  *
35  * NAME
36  *
37  *    typedef struct { ... } SilcCipher;
38  *
39  * DESCRIPTION
40  *
41  *    This context is the actual cipher context and is allocated
42  *    by silc_cipher_alloc and given as argument usually to all
43  *    silc_cipher _* functions.  It is freed by the silc_cipher_free
44  *    function.
45  *
46  ***/
47 typedef struct SilcCipherStruct *SilcCipher;
48
49 /* The default SILC Cipher object to represent any cipher in SILC. */
50 typedef struct {
51   char *name;
52   SilcBool (*set_key)(void *, const unsigned char *, SilcUInt32);
53   SilcBool (*encrypt)(void *, const unsigned char *, unsigned char *,
54                   SilcUInt32, unsigned char *);
55   SilcBool (*decrypt)(void *, const unsigned char *, unsigned char *,
56                   SilcUInt32, unsigned char *);
57   SilcUInt32 (*context_len)();
58   unsigned int key_len   : 12;
59   unsigned int block_len : 10;
60   unsigned int iv_len    : 10;
61 } SilcCipherObject;
62
63 #define SILC_CIPHER_MAX_IV_SIZE 16
64
65 /* Marks for all ciphers in silc. This can be used in silc_cipher_unregister
66    to unregister all ciphers at once. */
67 #define SILC_ALL_CIPHERS ((SilcCipherObject *)1)
68
69 /* Static list of ciphers for silc_cipher_register_default(). */
70 extern DLLAPI const SilcCipherObject silc_default_ciphers[];
71
72 /* Default cipher in the SILC protocol */
73 #define SILC_DEFAULT_CIPHER "aes-256-cbc"
74
75
76 /* Macros */
77
78 /* Function names in SILC Crypto modules. The name of the cipher
79    is appended into these names and used to the get correct symbol out
80    of the module. All SILC Crypto API compliant modules must support
81    these function names (use macros below to assure this). */
82 #define SILC_CIPHER_SIM_SET_KEY "set_key"
83 #define SILC_CIPHER_SIM_ENCRYPT_CBC "encrypt_cbc"
84 #define SILC_CIPHER_SIM_DECRYPT_CBC "decrypt_cbc"
85 #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len"
86
87 /* These macros can be used to implement the SILC Crypto API and to avoid
88    errors in the API these macros should be used always. */
89 #define SILC_CIPHER_API_SET_KEY(cipher)                 \
90 SilcBool silc_##cipher##_set_key(void *context,         \
91                              const unsigned char *key,  \
92                              SilcUInt32 keylen)
93 #define SILC_CIPHER_API_ENCRYPT_CBC(cipher)                     \
94 SilcBool silc_##cipher##_encrypt_cbc(void *context,                     \
95                                  const unsigned char *src,      \
96                                  unsigned char *dst,            \
97                                  SilcUInt32 len,                \
98                                  unsigned char *iv)
99 #define SILC_CIPHER_API_DECRYPT_CBC(cipher)                     \
100 SilcBool silc_##cipher##_decrypt_cbc(void *context,                     \
101                                  const unsigned char *src,      \
102                                  unsigned char *dst,            \
103                                  SilcUInt32 len,                \
104                                  unsigned char *iv)
105
106
107 #define SILC_CIPHER_API_CONTEXT_LEN(cipher)                     \
108 SilcUInt32 silc_##cipher##_context_len()
109
110
111 /* Prototypes */
112
113 /****f* silccrypt/SilcCipherAPI/silc_cipher_register
114  *
115  * SYNOPSIS
116  *
117  *    SilcBool silc_cipher_register(const SilcCipherObject *cipher);
118  *
119  * DESCRIPTION
120  *
121  *    Register a new cipher into SILC. This is used at the initialization of
122  *    the SILC. This function allocates a new object for the cipher to be
123  *    registered. Therefore, if memory has been allocated for the object sent
124  *    as argument it has to be free'd after this function returns succesfully.
125  *
126  ***/
127 SilcBool silc_cipher_register(const SilcCipherObject *cipher);
128
129 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister
130  *
131  * SYNOPSIS
132  *
133  *    SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
134  *
135  * DESCRIPTION
136  *
137  *    Unregister a cipher from the SILC.
138  *
139  ***/
140 SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
141
142 /****f* silccrypt/SilcCipherAPI/silc_cipher_register_default
143  *
144  * SYNOPSIS
145  *
146  *    SilcBool silc_cipher_register_default(void);
147  *
148  * DESCRIPTION
149  *
150  *    Function that registers all the default ciphers (all builtin ciphers).
151  *    The application may use this to register the default ciphers if specific
152  *    ciphers in any specific order is not wanted.
153  *
154  ***/
155 SilcBool silc_cipher_register_default(void);
156
157 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister_all
158  *
159  * SYNOPSIS
160  *
161  *    SilcBool silc_cipher_unregister_all(void);
162  *
163  * DESCRIPTION
164  *
165  *    Unregisters all ciphers.
166  *
167  ***/
168 SilcBool silc_cipher_unregister_all(void);
169
170 /****f* silccrypt/SilcCipherAPI/silc_cipher_alloc
171  *
172  * SYNOPSIS
173  *
174  *    SilcBool silc_cipher_alloc(const unsigned char *name,
175  *                           SilcCipher *new_cipher);
176  *
177  * DESCRIPTION
178  *
179  *    Allocates a new SILC cipher object. Function returns 1 on succes and 0
180  *    on error. The allocated cipher is returned in new_cipher argument. The
181  *    caller must set the key to the cipher after this function has returned
182  *    by calling the ciphers set_key function.
183  *
184  ***/
185 SilcBool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
186
187 /****f* silccrypt/SilcCipherAPI/silc_cipher_free
188  *
189  * SYNOPSIS
190  *
191  *    void silc_cipher_free(SilcCipher cipher);
192  *
193  * DESCRIPTION
194  *
195  *    Frees the given cipher.
196  *
197  ***/
198 void silc_cipher_free(SilcCipher cipher);
199
200 /****f* silccrypt/SilcCipherAPI/silc_cipher_is_supported
201  *
202  * SYNOPSIS
203  *
204  * SilcBool silc_cipher_is_supported(const unsigned char *name);
205  *
206  * DESCRIPTION
207  *
208  *    Returns TRUE if cipher `name' is supported.
209  *
210  ***/
211 SilcBool silc_cipher_is_supported(const unsigned char *name);
212
213 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_supported
214  *
215  * SYNOPSIS
216  *
217  *    char *silc_cipher_get_supported(void);
218  *
219  * DESCRIPTION
220  *
221  *    Returns comma separated list of supported ciphers.
222  *
223  ***/
224 char *silc_cipher_get_supported(void);
225
226 /****f* silccrypt/SilcCipherAPI/silc_cipher_encrypt
227  *
228  * SYNOPSIS
229  *
230  *    SilcBool silc_cipher_encrypt(SilcCipher cipher,
231  *                                 const unsigned char *src,
232  *                                 unsigned char *dst, SilcUInt32 len,
233  *                                 unsigned char *iv);
234  *
235  * DESCRIPTION
236  *
237  *    Encrypts data from `src' into `dst' with the specified cipher and
238  *    Initial Vector (IV).  If the `iv' is NULL then the cipher's internal
239  *    IV is used.  The `src' and `dst' maybe same buffer.
240  *
241  ***/
242 SilcBool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
243                              unsigned char *dst, SilcUInt32 len,
244                              unsigned char *iv);
245
246 /****f* silccrypt/SilcCipherAPI/silc_cipher_decrypt
247  *
248  * SYNOPSIS
249  *
250  *    SilcBool silc_cipher_decrypt(SilcCipher cipher,
251  *                                 const unsigned char *src,
252  *                                 unsigned char *dst, SilcUInt32 len,
253  *                                 unsigned char *iv);
254  *
255  * DESCRIPTION
256  *
257  *    Decrypts data from `src' into `dst' with the specified cipher and
258  *    Initial Vector (IV).  If the `iv' is NULL then the cipher's internal
259  *    IV is used.  The `src' and `dst' maybe same buffer.
260  *
261  ***/
262 SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
263                              unsigned char *dst, SilcUInt32 len,
264                              unsigned char *iv);
265
266 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_key
267  *
268  * SYNOPSIS
269  *
270  *    SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
271  *                             SilcUInt32 keylen);
272  *
273  * DESCRIPTION
274  *
275  *    Sets the key for the cipher.  The `keylen' is the key length in
276  *    bits.
277  *
278  ***/
279 SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
280                              SilcUInt32 keylen);
281
282 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
283  *
284  * SYNOPSIS
285  *
286  *    void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
287  *
288  * DESCRIPTION
289  *
290  *    Sets the IV (initial vector) for the cipher.  The `iv' must be
291  *    the size of the block size of the cipher.
292  *
293  ***/
294 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
295
296 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
297  *
298  * SYNOPSIS
299  *
300  *    unsigned char *silc_cipher_get_iv(SilcCipher cipher);
301  *
302  * DESCRIPTION
303  *
304  *    Returns the IV (initial vector) of the cipher.  The returned
305  *    pointer must not be freed by the caller.
306  *
307  ***/
308 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
309
310 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
311  *
312  * SYNOPSIS
313  *
314  *    SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
315  *
316  * DESCRIPTION
317  *
318  *    Returns the key length of the cipher in bits.
319  *
320  ***/
321 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
322
323 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
324  *
325  * SYNOPSIS
326  *
327  *    SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
328  *
329  * DESCRIPTION
330  *
331  *    Returns the block size of the cipher in bytes.
332  *
333  ***/
334 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
335
336 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv_len
337  *
338  * SYNOPSIS
339  *
340  *    SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
341  *
342  * DESCRIPTION
343  *
344  *    Returns the IV length of the cipher in bytes.
345  *
346  ***/
347 SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
348
349 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
350  *
351  * SYNOPSIS
352  *
353  *    const char *silc_cipher_get_name(SilcCipher cipher);
354  *
355  * DESCRIPTION
356  *
357  *    Returns the name of the cipher.
358  *
359  ***/
360 const char *silc_cipher_get_name(SilcCipher cipher);
361
362 #endif