5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 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; version 2 of the License.
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.
23 /****h* silccrypt/SILC Cipher Interface
27 * This is the interface for cipher functions. It provides cipher
28 * registering and unregistering routines, encryption and decryption
33 /****s* silccrypt/SilcCipherAPI/SilcCipher
37 * typedef struct { ... } SilcCipher;
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
47 typedef struct SilcCipherStruct *SilcCipher;
49 /* The default SILC Cipher object to represent any cipher in SILC. */
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;
63 #define SILC_CIPHER_MAX_IV_SIZE 16
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)
69 /* Static list of ciphers for silc_cipher_register_default(). */
70 extern DLLAPI const SilcCipherObject silc_default_ciphers[];
72 /* Default cipher in the SILC protocol */
73 #define SILC_DEFAULT_CIPHER "aes-256-cbc"
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"
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, \
93 #define SILC_CIPHER_API_ENCRYPT_CBC(cipher) \
94 SilcBool silc_##cipher##_encrypt_cbc(void *context, \
95 const unsigned char *src, \
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, \
107 #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \
108 SilcUInt32 silc_##cipher##_context_len()
113 /****f* silccrypt/SilcCipherAPI/silc_cipher_register
117 * SilcBool silc_cipher_register(const SilcCipherObject *cipher);
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.
127 SilcBool silc_cipher_register(const SilcCipherObject *cipher);
129 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister
133 * SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
137 * Unregister a cipher from the SILC.
140 SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
142 /****f* silccrypt/SilcCipherAPI/silc_cipher_register_default
146 * SilcBool silc_cipher_register_default(void);
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.
155 SilcBool silc_cipher_register_default(void);
157 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister_all
161 * SilcBool silc_cipher_unregister_all(void);
165 * Unregisters all ciphers.
168 SilcBool silc_cipher_unregister_all(void);
170 /****f* silccrypt/SilcCipherAPI/silc_cipher_alloc
174 * SilcBool silc_cipher_alloc(const unsigned char *name,
175 * SilcCipher *new_cipher);
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.
185 SilcBool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
187 /****f* silccrypt/SilcCipherAPI/silc_cipher_free
191 * void silc_cipher_free(SilcCipher cipher);
195 * Frees the given cipher.
198 void silc_cipher_free(SilcCipher cipher);
200 /****f* silccrypt/SilcCipherAPI/silc_cipher_is_supported
204 * SilcBool silc_cipher_is_supported(const unsigned char *name);
208 * Returns TRUE if cipher `name' is supported.
211 SilcBool silc_cipher_is_supported(const unsigned char *name);
213 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_supported
217 * char *silc_cipher_get_supported(void);
221 * Returns comma separated list of supported ciphers.
224 char *silc_cipher_get_supported(void);
226 /****f* silccrypt/SilcCipherAPI/silc_cipher_encrypt
230 * SilcBool silc_cipher_encrypt(SilcCipher cipher,
231 * const unsigned char *src,
232 * unsigned char *dst, SilcUInt32 len,
233 * unsigned char *iv);
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.
242 SilcBool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
243 unsigned char *dst, SilcUInt32 len,
246 /****f* silccrypt/SilcCipherAPI/silc_cipher_decrypt
250 * SilcBool silc_cipher_decrypt(SilcCipher cipher,
251 * const unsigned char *src,
252 * unsigned char *dst, SilcUInt32 len,
253 * unsigned char *iv);
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.
262 SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
263 unsigned char *dst, SilcUInt32 len,
266 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_key
270 * SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
271 * SilcUInt32 keylen);
275 * Sets the key for the cipher. The `keylen' is the key length in
279 SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
282 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
286 * void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
290 * Sets the IV (initial vector) for the cipher. The `iv' must be
291 * the size of the block size of the cipher.
294 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
296 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
300 * unsigned char *silc_cipher_get_iv(SilcCipher cipher);
304 * Returns the IV (initial vector) of the cipher. The returned
305 * pointer must not be freed by the caller.
308 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
310 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
314 * SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
318 * Returns the key length of the cipher in bits.
321 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
323 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
327 * SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
331 * Returns the block size of the cipher in bytes.
334 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
336 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv_len
340 * SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
344 * Returns the IV length of the cipher in bytes.
347 SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
349 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
353 * const char *silc_cipher_get_name(SilcCipher cipher);
357 * Returns the name of the cipher.
360 const char *silc_cipher_get_name(SilcCipher cipher);