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 (*set_key_with_string)(void *, const unsigned char *, SilcUInt32);
54 SilcBool (*encrypt)(void *, const unsigned char *, unsigned char *,
55 SilcUInt32, unsigned char *);
56 SilcBool (*decrypt)(void *, const unsigned char *, unsigned char *,
57 SilcUInt32, unsigned char *);
58 SilcUInt32 (*context_len)();
59 unsigned int key_len : 12;
60 unsigned int block_len : 10;
61 unsigned int iv_len : 10;
64 #define SILC_CIPHER_MAX_IV_SIZE 16
66 /* Marks for all ciphers in silc. This can be used in silc_cipher_unregister
67 to unregister all ciphers at once. */
68 #define SILC_ALL_CIPHERS ((SilcCipherObject *)1)
70 /* Static list of ciphers for silc_cipher_register_default(). */
71 extern DLLAPI const SilcCipherObject silc_default_ciphers[];
73 /* Default cipher in the SILC protocol */
74 #define SILC_DEFAULT_CIPHER "aes-256-cbc"
79 /* Function names in SILC Crypto modules. The name of the cipher
80 is appended into these names and used to the get correct symbol out
81 of the module. All SILC Crypto API compliant modules must support
82 these function names (use macros below to assure this). */
83 #define SILC_CIPHER_SIM_SET_KEY "set_key"
84 #define SILC_CIPHER_SIM_SET_KEY_WITH_STRING "set_key_with_string"
85 #define SILC_CIPHER_SIM_ENCRYPT_CBC "encrypt_cbc"
86 #define SILC_CIPHER_SIM_DECRYPT_CBC "decrypt_cbc"
87 #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len"
89 /* These macros can be used to implement the SILC Crypto API and to avoid
90 errors in the API these macros should be used always. */
91 #define SILC_CIPHER_API_SET_KEY(cipher) \
92 SilcBool silc_##cipher##_set_key(void *context, \
93 const unsigned char *key, \
95 #define SILC_CIPHER_API_SET_KEY_WITH_STRING(cipher) \
96 SilcBool silc_##cipher##_set_key_with_string(void *context, \
97 const unsigned char *string, \
99 #define SILC_CIPHER_API_ENCRYPT_CBC(cipher) \
100 SilcBool silc_##cipher##_encrypt_cbc(void *context, \
101 const unsigned char *src, \
102 unsigned char *dst, \
105 #define SILC_CIPHER_API_DECRYPT_CBC(cipher) \
106 SilcBool silc_##cipher##_decrypt_cbc(void *context, \
107 const unsigned char *src, \
108 unsigned char *dst, \
113 #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \
114 SilcUInt32 silc_##cipher##_context_len()
119 /****f* silccrypt/SilcCipherAPI/silc_cipher_register
123 * SilcBool silc_cipher_register(const SilcCipherObject *cipher);
127 * Register a new cipher into SILC. This is used at the initialization of
128 * the SILC. This function allocates a new object for the cipher to be
129 * registered. Therefore, if memory has been allocated for the object sent
130 * as argument it has to be free'd after this function returns succesfully.
133 SilcBool silc_cipher_register(const SilcCipherObject *cipher);
135 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister
139 * SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
143 * Unregister a cipher from the SILC.
146 SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
148 /****f* silccrypt/SilcCipherAPI/silc_cipher_register_default
152 * SilcBool silc_cipher_register_default(void);
156 * Function that registers all the default ciphers (all builtin ciphers).
157 * The application may use this to register the default ciphers if specific
158 * ciphers in any specific order is not wanted.
161 SilcBool silc_cipher_register_default(void);
163 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister_all
167 * SilcBool silc_cipher_unregister_all(void);
171 * Unregisters all ciphers.
174 SilcBool silc_cipher_unregister_all(void);
176 /****f* silccrypt/SilcCipherAPI/silc_cipher_alloc
180 * SilcBool silc_cipher_alloc(const unsigned char *name,
181 * SilcCipher *new_cipher);
185 * Allocates a new SILC cipher object. Function returns 1 on succes and 0
186 * on error. The allocated cipher is returned in new_cipher argument. The
187 * caller must set the key to the cipher after this function has returned
188 * by calling the ciphers set_key function.
191 SilcBool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
193 /****f* silccrypt/SilcCipherAPI/silc_cipher_free
197 * void silc_cipher_free(SilcCipher cipher);
201 * Frees the given cipher.
204 void silc_cipher_free(SilcCipher cipher);
206 /****f* silccrypt/SilcCipherAPI/silc_cipher_is_supported
210 * SilcBool silc_cipher_is_supported(const unsigned char *name);
214 * Returns TRUE if cipher `name' is supported.
217 SilcBool silc_cipher_is_supported(const unsigned char *name);
219 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_supported
223 * char *silc_cipher_get_supported(void);
227 * Returns comma separated list of supported ciphers.
230 char *silc_cipher_get_supported(void);
232 /****f* silccrypt/SilcCipherAPI/silc_cipher_encrypt
236 * SilcBool silc_cipher_encrypt(SilcCipher cipher,
237 * const unsigned char *src,
238 * unsigned char *dst, SilcUInt32 len,
239 * unsigned char *iv);
243 * Encrypts data from `src' into `dst' with the specified cipher and
244 * Initial Vector (IV). If the `iv' is NULL then the cipher's internal
245 * IV is used. The `src' and `dst' maybe same buffer.
248 SilcBool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
249 unsigned char *dst, SilcUInt32 len,
252 /****f* silccrypt/SilcCipherAPI/silc_cipher_decrypt
256 * SilcBool silc_cipher_decrypt(SilcCipher cipher,
257 * const unsigned char *src,
258 * unsigned char *dst, SilcUInt32 len,
259 * unsigned char *iv);
263 * Decrypts data from `src' into `dst' with the specified cipher and
264 * Initial Vector (IV). If the `iv' is NULL then the cipher's internal
265 * IV is used. The `src' and `dst' maybe same buffer.
268 SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
269 unsigned char *dst, SilcUInt32 len,
272 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_key
276 * SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
277 * SilcUInt32 keylen);
281 * Sets the key for the cipher. The `keylen' is the key length in
285 SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
288 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
292 * void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
296 * Sets the IV (initial vector) for the cipher. The `iv' must be
297 * the size of the block size of the cipher.
300 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
302 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
306 * unsigned char *silc_cipher_get_iv(SilcCipher cipher);
310 * Returns the IV (initial vector) of the cipher. The returned
311 * pointer must not be freed by the caller.
314 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
316 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
320 * SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
324 * Returns the key length of the cipher in bits.
327 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
329 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
333 * SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
337 * Returns the block size of the cipher in bytes.
340 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
342 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv_len
346 * SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
350 * Returns the IV length of the cipher in bytes.
353 SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
355 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
359 * const char *silc_cipher_get_name(SilcCipher cipher);
363 * Returns the name of the cipher.
366 const char *silc_cipher_get_name(SilcCipher cipher);