Major restructuring of the internals of SILC Cipher API
[crypto.git] / lib / silccrypt / silccipher_i.h
1 /*
2
3   silccipher_i.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 - 2008 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 #ifndef SILCCIPHER_I_H
21 #define SILCCIPHER_I_H
22
23 #ifndef SILCCIPHER_H
24 #error "Do not include this header directly"
25 #endif
26
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 */
34 };
35
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,              \
42                                  SilcUInt32 keylen,                     \
43                                  SilcBool encryption)
44 #define SILC_CIPHER_API_SET_IV(name)                                    \
45   void silc_##name##_set_iv(SilcCipher cipher,                          \
46                             struct SilcCipherObjectStruct *ops,         \
47                             void *context,                              \
48                             unsigned char *iv)
49 #define SILC_CIPHER_API_ENCRYPT(name)                                   \
50   SilcBool silc_##name##_encrypt(SilcCipher cipher,                     \
51                                  struct SilcCipherObjectStruct *ops,    \
52                                  void *context,                         \
53                                  const unsigned char *src,              \
54                                  unsigned char *dst,                    \
55                                  SilcUInt32 len,                        \
56                                  unsigned char *iv)
57 #define SILC_CIPHER_API_DECRYPT(name)                                   \
58   SilcBool silc_##name##_decrypt(SilcCipher cipher,                     \
59                                  struct SilcCipherObjectStruct *ops,    \
60                                  void *context,                         \
61                                  const unsigned char *src,              \
62                                  unsigned char *dst,                    \
63                                  SilcUInt32 len,                        \
64                                  unsigned char *iv)
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,         \
69                             void *context)
70
71 /* Cipher object to represent a cipher algorithm. */
72 struct SilcCipherObjectStruct {
73   /* Cipher name */
74   char *name;
75   char *alg_name;
76
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,
81                       SilcBool encryption);
82
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);
87
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,
93                       unsigned char *iv);
94
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,
100                       unsigned char *iv);
101
102   /* Initializes the cipher.  Returns internal cipher context.  The uninit()
103      will be called in silc_cipher_free to uninitialize the cipher and free
104      the context. */
105   void *(*init)(struct SilcCipherObjectStruct *ops);
106
107   /* Uninitialize cipher. */
108   void (*uninit)(struct SilcCipherObjectStruct *ops, void *context);
109
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 */
114 };
115
116 #endif /* SILCCIPHER_I_H */