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, SilcBool);
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"
77 /* Function names in SILC Crypto modules. The name of the cipher
78 is appended into these names and used to the get correct symbol out
79 of the module. All SILC Crypto API compliant modules must support
80 these function names (use macros below to assure this). */
81 #define SILC_CIPHER_SIM_SET_KEY "set_key"
82 #define SILC_CIPHER_SIM_ENCRYPT "encrypt"
83 #define SILC_CIPHER_SIM_DECRYPT "decrypt"
84 #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len"
85 #define SILC_CIPHER_SIM_SET_IV "set_iv"
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, \
94 #define SILC_CIPHER_API_ENCRYPT(cipher) \
95 SilcBool silc_##cipher##_encrypt(void *context, \
96 const unsigned char *src, \
100 #define SILC_CIPHER_API_DECRYPT(cipher) \
101 SilcBool silc_##cipher##_decrypt(void *context, \
102 const unsigned char *src, \
103 unsigned char *dst, \
106 #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \
107 SilcUInt32 silc_##cipher##_context_len()
108 #define SILC_CIPHER_API_SET_IV(cipher) \
109 SilcBool silc_##cipher##_set_iv(void *context, const unsigned char *iv)
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, SilcBool encryption);
275 * Sets the key for the cipher. The `keylen' is the key length in
276 * bits. If the `encryption' is TRUE the key is for encryption, if FALSE
277 * the key is for decryption.
280 SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
281 SilcUInt32 keylen, SilcBool encryption);
283 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
287 * void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
291 * Sets the IV (initial vector) for the cipher. The `iv' must be
292 * the size of the block size of the cipher.
295 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
297 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
301 * unsigned char *silc_cipher_get_iv(SilcCipher cipher);
305 * Returns the IV (initial vector) of the cipher. The returned
306 * pointer must not be freed by the caller.
309 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
311 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
315 * SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
319 * Returns the key length of the cipher in bits.
322 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
324 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
328 * SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
332 * Returns the block size of the cipher in bytes.
335 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
337 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv_len
341 * SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
345 * Returns the IV length of the cipher in bytes.
348 SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
350 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
354 * const char *silc_cipher_get_name(SilcCipher cipher);
358 * Returns the name of the cipher.
361 const char *silc_cipher_get_name(SilcCipher cipher);
363 #endif /* SILCCIPHER_H */