5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2002 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
24 /****h* silccrypt/SILC Cipher Interface
28 * This is the interface for cipher functions. It provides cipher
29 * registering and unregistering routines, encryption and decryption
34 /****s* silccrypt/SilcCipherAPI/SilcCipher
38 * typedef struct { ... } SilcCipher;
42 * This context is the actual cipher context and is allocated
43 * by silc_cipher_alloc and given as argument usually to all
44 * silc_cipher _* functions. It is freed by the silc_cipher_free
48 typedef struct SilcCipherStruct *SilcCipher;
50 /* The default SILC Cipher object to represent any cipher in SILC. */
56 bool (*set_key)(void *, const unsigned char *, SilcUInt32);
57 bool (*set_key_with_string)(void *, const unsigned char *, SilcUInt32);
58 bool (*encrypt)(void *, const unsigned char *, unsigned char *,
59 SilcUInt32, unsigned char *);
60 bool (*decrypt)(void *, const unsigned char *, unsigned char *,
61 SilcUInt32, unsigned char *);
62 SilcUInt32 (*context_len)();
65 #define SILC_CIPHER_MAX_IV_SIZE 16
67 /* Marks for all ciphers in silc. This can be used in silc_cipher_unregister
68 to unregister all ciphers at once. */
69 #define SILC_ALL_CIPHERS ((SilcCipherObject *)1)
71 /* Static list of ciphers for silc_cipher_register_default(). */
72 extern DLLAPI const SilcCipherObject silc_default_ciphers[];
74 /* Default cipher in the SILC protocol */
75 #define SILC_DEFAULT_CIPHER "aes-256-cbc"
80 /* Function names in SILC Crypto modules. The name of the cipher
81 is appended into these names and used to the get correct symbol out
82 of the module. All SILC Crypto API compliant modules must support
83 these function names (use macros below to assure this). */
84 #define SILC_CIPHER_SIM_SET_KEY "set_key"
85 #define SILC_CIPHER_SIM_SET_KEY_WITH_STRING "set_key_with_string"
86 #define SILC_CIPHER_SIM_ENCRYPT_CBC "encrypt_cbc"
87 #define SILC_CIPHER_SIM_DECRYPT_CBC "decrypt_cbc"
88 #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len"
90 /* These macros can be used to implement the SILC Crypto API and to avoid
91 errors in the API these macros should be used always. */
92 #define SILC_CIPHER_API_SET_KEY(cipher) \
93 bool silc_##cipher##_set_key(void *context, \
94 const unsigned char *key, \
96 #define SILC_CIPHER_API_SET_KEY_WITH_STRING(cipher) \
97 bool silc_##cipher##_set_key_with_string(void *context, \
98 const unsigned char *string, \
100 #define SILC_CIPHER_API_ENCRYPT_CBC(cipher) \
101 bool silc_##cipher##_encrypt_cbc(void *context, \
102 const unsigned char *src, \
103 unsigned char *dst, \
106 #define SILC_CIPHER_API_DECRYPT_CBC(cipher) \
107 bool silc_##cipher##_decrypt_cbc(void *context, \
108 const unsigned char *src, \
109 unsigned char *dst, \
114 #define SILC_CIPHER_API_CONTEXT_LEN(cipher) \
115 SilcUInt32 silc_##cipher##_context_len()
120 /****f* silccrypt/SilcCipherAPI/silc_cipher_register
124 * bool silc_cipher_register(const SilcCipherObject *cipher);
128 * Register a new cipher into SILC. This is used at the initialization of
129 * the SILC. This function allocates a new object for the cipher to be
130 * registered. Therefore, if memory has been allocated for the object sent
131 * as argument it has to be free'd after this function returns succesfully.
134 bool silc_cipher_register(const SilcCipherObject *cipher);
136 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister
140 * bool silc_cipher_unregister(SilcCipherObject *cipher);
144 * Unregister a cipher from the SILC.
147 bool silc_cipher_unregister(SilcCipherObject *cipher);
149 /****f* silccrypt/SilcCipherAPI/silc_cipher_register_default
153 * bool silc_cipher_register_default(void);
157 * Function that registers all the default ciphers (all builtin ciphers).
158 * The application may use this to register the default ciphers if specific
159 * ciphers in any specific order is not wanted.
162 bool silc_cipher_register_default(void);
164 /****f* silccrypt/SilcCipherAPI/silc_cipher_unregister_all
168 * bool silc_cipher_unregister_all(void);
172 * Unregisters all ciphers.
175 bool silc_cipher_unregister_all(void);
177 /****f* silccrypt/SilcCipherAPI/silc_cipher_alloc
181 * bool silc_cipher_alloc(const unsigned char *name,
182 * SilcCipher *new_cipher);
186 * Allocates a new SILC cipher object. Function returns 1 on succes and 0
187 * on error. The allocated cipher is returned in new_cipher argument. The
188 * caller must set the key to the cipher after this function has returned
189 * by calling the ciphers set_key function.
192 bool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
194 /****f* silccrypt/SilcCipherAPI/silc_cipher_free
198 * void silc_cipher_free(SilcCipher cipher);
202 * Frees the given cipher.
205 void silc_cipher_free(SilcCipher cipher);
207 /****f* silccrypt/SilcCipherAPI/silc_cipher_is_supported
211 * bool silc_cipher_is_supported(const unsigned char *name);
215 * Returns TRUE if cipher `name' is supported.
218 bool silc_cipher_is_supported(const unsigned char *name);
220 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_supported
224 * char *silc_cipher_get_supported(void);
228 * Returns comma separated list of supported ciphers.
231 char *silc_cipher_get_supported(void);
233 /****f* silccrypt/SilcCipherAPI/silc_cipher_encrypt
237 * bool silc_cipher_encrypt(SilcCipher cipher, 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). The `src' and `dst' maybe same buffer.
247 bool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
248 unsigned char *dst, SilcUInt32 len,
251 /****f* silccrypt/SilcCipherAPI/silc_cipher_decrypt
255 * bool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
256 * unsigned char *dst, SilcUInt32 len,
257 * unsigned char *iv);
261 * Decrypts data from `src' into `dst' with the specified cipher and
262 * Initial Vector (IV). The `src' and `dst' maybe same buffer.
265 bool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
266 unsigned char *dst, SilcUInt32 len,
269 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_key
273 * bool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
274 * SilcUInt32 keylen);
278 * Sets the key for the cipher. The `keylen' is the key length in
282 bool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
285 /****f* silccrypt/SilcCipherAPI/silc_cipher_set_iv
289 * void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
293 * Sets the IV (initial vector) for the cipher. The `iv' must be
294 * the size of the block size of the cipher.
297 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
299 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_iv
303 * unsigned char *silc_cipher_get_iv(SilcCipher cipher);
307 * Returns the IV (initial vector) of the cipher. The returned
308 * pointer must not be freed by the caller.
311 unsigned char *silc_cipher_get_iv(SilcCipher cipher);
313 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_key_len
317 * SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
321 * Returns the key length of the cipher in bits.
324 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
326 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_block_len
330 * SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
334 * Returns the block size of the cipher in bytes.
337 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
339 /****f* silccrypt/SilcCipherAPI/silc_cipher_get_name
343 * const char *silc_cipher_get_name(SilcCipher cipher);
347 * Returns the name of the cipher.
350 const char *silc_cipher_get_name(SilcCipher cipher);