5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 - 2008 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.
20 #ifndef SILCCIPHER_I_H
21 #define SILCCIPHER_I_H
24 #error "Do not include this header directly"
27 /* The SilcCipher context. This is not visible to application programmer.
28 It is accessible from the algorithm implementations. */
29 struct SilcCipherStruct {
30 SilcCipherObject *cipher; /* Cipher operations */
31 void *context; /* Algorithm context */
32 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]; /* IV */
33 unsigned char block[SILC_CIPHER_MAX_IV_SIZE]; /* Extra block for free use */
36 /* These macros can be used to implement the SILC Crypto API and to avoid
37 errors in the API these macros should be used always. */
38 #define SILC_CIPHER_API_SET_KEY(name) \
39 SilcBool silc_##name##_set_key(SilcCipher cipher, \
40 struct SilcCipherObjectStruct *ops, \
41 void *context, void *key, \
44 #define SILC_CIPHER_API_SET_IV(name) \
45 void silc_##name##_set_iv(SilcCipher cipher, \
46 struct SilcCipherObjectStruct *ops, \
49 #define SILC_CIPHER_API_ENCRYPT(name) \
50 SilcBool silc_##name##_encrypt(SilcCipher cipher, \
51 struct SilcCipherObjectStruct *ops, \
53 const unsigned char *src, \
57 #define SILC_CIPHER_API_DECRYPT(name) \
58 SilcBool silc_##name##_decrypt(SilcCipher cipher, \
59 struct SilcCipherObjectStruct *ops, \
61 const unsigned char *src, \
65 #define SILC_CIPHER_API_INIT(name) \
66 void *silc_##name##_init(struct SilcCipherObjectStruct *ops)
67 #define SILC_CIPHER_API_UNINIT(name) \
68 void silc_##name##_uninit(struct SilcCipherObjectStruct *ops, \
71 /* Cipher object to represent a cipher algorithm. */
72 struct SilcCipherObjectStruct {
77 /* Set new key. If `encryption' is TRUE the key is for encryption,
78 FALSE for decryption. The `keylen' is in bits. */
79 SilcBool (*set_key)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
80 void *context, void *key, SilcUInt32 keylen,
83 /* Set IV. The upper layer (SilcCipher) maintains the IV. If the algorithm
84 needs to set the IV itself, this should be implemented. */
85 void (*set_iv)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
86 void *context, unsigned char *iv);
88 /* Encrypt. The `src' and `dst' may be same pointer. The `iv' may be
89 edited inside this function. */
90 SilcBool (*encrypt)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
91 void *context, const unsigned char *src,
92 unsigned char *dst, SilcUInt32 len,
95 /* Decrypt. The `src' and `dst' may be same pointer. The `iv' may be
96 edited inside this function. */
97 SilcBool (*decrypt)(SilcCipher cipher, struct SilcCipherObjectStruct *ops,
98 void *context, const unsigned char *src,
99 unsigned char *dst, SilcUInt32 len,
102 /* Initializes the cipher. Returns internal cipher context. The uninit()
103 will be called in silc_cipher_free to uninitialize the cipher and free
105 void *(*init)(struct SilcCipherObjectStruct *ops);
107 /* Uninitialize cipher. */
108 void (*uninit)(struct SilcCipherObjectStruct *ops, void *context);
110 unsigned int key_len : 10; /* Key length in bits */
111 unsigned int block_len : 8; /* Block size in bytes */
112 unsigned int iv_len : 8; /* IV length in bytes */
113 unsigned int mode : 6; /* SilcCipherMode */
116 #endif /* SILCCIPHER_I_H */