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 : 10;
59 unsigned int block_len : 8;
60 unsigned int iv_len : 8;
61 unsigned int mode : 6;
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"
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 "encrypt"
84 #define SILC_CIPHER_SIM_DECRYPT "decrypt"
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, \
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()
109 /****d* silccrypt/SilcCipherAPI/SilcCipherMode
113 * typedef enum { ... } SilcCipherMode;
122 SILC_CIPHER_MODE_ECB = 1, /* ECB mode */
123 SILC_CIPHER_MODE_CBC = 2, /* CBC mode */
124 SILC_CIPHER_MODE_CTR = 3, /* CTR mode */
125 SILC_CIPHER_MODE_CFB = 4, /* CFB mode */
126 SILC_CIPHER_MODE_OFB = 5, /* OFB mode */
132 /****f* silccrypt/SilcCipherAPI/silc_cipher_register
136 * SilcBool silc_cipher_register(const SilcCipherObject *cipher);
140 * Register a new cipher into SILC. This is used at the initialization of
141 * the SILC. This function allocates a new object for the cipher to be
142 * registered. Therefore, if memory has been allocated for the object sent
143 * as argument it has to be free'd after this function returns succesfully.
146 SilcBool silc_cipher_register(const SilcCipherObject *cipher);
148 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister
152 * SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
156 * Unregister a cipher from the SILC.
159 SilcBool silc_cipher_unregister(SilcCipherObject *cipher);
161 /****f* silccrypt/SilcCipherAPI/silc_cipher_register_default
165 * SilcBool silc_cipher_register_default(void);
169 * Function that registers all the default ciphers (all builtin ciphers).
170 * The application may use this to register the default ciphers if specific
171 * ciphers in any specific order is not wanted.
174 SilcBool silc_cipher_register_default(void);
176 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister_all
180 * SilcBool silc_cipher_unregister_all(void);
184 * Unregisters all ciphers.
187 SilcBool silc_cipher_unregister_all(void);
189 /****f* silccrypt/SilcCipherAPI/silc_cipher_alloc
193 * SilcBool silc_cipher_alloc(const unsigned char *name,
194 * SilcCipher *new_cipher);
198 * Allocates a new SILC cipher object. Function returns 1 on succes and 0
199 * on error. The allocated cipher is returned in new_cipher argument. The
200 * caller must set the key to the cipher after this function has returned
201 * by calling the ciphers set_key function.
204 SilcBool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
206 /****f* silccrypt/SilcCipherAPI/silc_cipher_free
210 * void silc_cipher_free(SilcCipher cipher);
214 * Frees the given cipher.
217 void silc_cipher_free(SilcCipher cipher);
219 /****f* silccrypt/SilcCipherAPI/silc_cipher_is_supported
223 * SilcBool silc_cipher_is_supported(const unsigned char *name);
227 * Returns TRUE if cipher `name' is supported.
230 SilcBool silc_cipher_is_supported(const unsigned char *name);
232 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_supported
236 * char *silc_cipher_get_supported(void);
240 * Returns comma separated list of supported ciphers.
243 char *silc_cipher_get_supported(void);
245 /****f* silccrypt/SilcCipherAPI/silc_cipher_encrypt
249 * SilcBool silc_cipher_encrypt(SilcCipher cipher,
250 * const unsigned char *src,
251 * unsigned char *dst, SilcUInt32 len,
252 * unsigned char *iv);
256 * Encrypts data from `src' into `dst' with the specified cipher and
257 * Initial Vector (IV). If the `iv' is NULL then the cipher's internal
258 * IV is used. The `src' and `dst' maybe same buffer.
261 SilcBool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
262 unsigned char *dst, SilcUInt32 len,
265 /****f* silccrypt/SilcCipherAPI/silc_cipher_decrypt
269 * SilcBool silc_cipher_decrypt(SilcCipher cipher,
270 * const unsigned char *src,
271 * unsigned char *dst, SilcUInt32 len,
272 * unsigned char *iv);
276 * Decrypts data from `src' into `dst' with the specified cipher and
277 * Initial Vector (IV). If the `iv' is NULL then the cipher's internal
278 * IV is used. The `src' and `dst' maybe same buffer.
281 SilcBool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
282 unsigned char *dst, SilcUInt32 len,
285 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_key
289 * SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
290 * SilcUInt32 keylen, SilcBool encryption);
294 * Sets the key for the cipher. The `keylen' is the key length in
295 * bits. If the `encryption' is TRUE the key is for encryption, if FALSE
296 * the key is for decryption.
299 SilcBool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
300 SilcUInt32 keylen, SilcBool encryption);
302 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
306 * void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
310 * Sets the IV (initial vector) for the cipher. The `iv' must be
311 * the size of the block size of the cipher.
314 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
316 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
320 * unsigned char *silc_cipher_get_iv(SilcCipher cipher);
324 * Returns the IV (initial vector) of the cipher. The returned
325 * pointer must not be freed by the caller. If the caller modifies
326 * the returned pointer the IV inside cipher is also modified.
329 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
331 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
335 * SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
339 * Returns the key length of the cipher in bits.
342 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
344 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
348 * SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
352 * Returns the block size of the cipher in bytes.
355 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
357 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv_len
361 * SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
365 * Returns the IV length of the cipher in bytes.
368 SilcUInt32 silc_cipher_get_iv_len(SilcCipher cipher);
370 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
374 * const char *silc_cipher_get_name(SilcCipher cipher);
378 * Returns the name of the cipher.
381 const char *silc_cipher_get_name(SilcCipher cipher);
383 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_mode
387 * SilcCipherMode silc_cipher_get_mode(SilcCipher cipher);
391 * Returns the cipher mode.
394 SilcCipherMode silc_cipher_get_mode(SilcCipher cipher);
396 #endif /* SILCCIPHER_H */