Integer type name change.
[silc.git] / lib / silccrypt / silccipher.h
1 /*
2
3   silccipher.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
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.
18
19 */
20
21 #ifndef SILCCIPHER_H
22 #define SILCCIPHER_H
23
24 /* 
25    SILC Cipher object.
26
27    Default SILC cipher object to represent any cipher. The function
28    pointers are the stub functions for each implemented cipher. Following
29    short description of the fields:
30
31    char *name
32
33        Logical name of the cipher.
34
35    SilcUInt32 block_len
36
37        Block size of the cipher.
38
39    SilcUInt32 key_len
40
41        Length of the key of the cipher (in bits).
42
43 */
44 typedef struct {
45   char *name;
46   SilcUInt32 block_len;
47   SilcUInt32 key_len;
48
49   bool (*set_key)(void *, const unsigned char *, SilcUInt32);
50   bool (*set_key_with_string)(void *, const unsigned char *, SilcUInt32);
51   bool (*encrypt)(void *, const unsigned char *, unsigned char *,
52                   SilcUInt32, unsigned char *);
53   bool (*decrypt)(void *, const unsigned char *, unsigned char *, 
54                   SilcUInt32, unsigned char *);
55   SilcUInt32 (*context_len)();
56 } SilcCipherObject;
57
58 #define SILC_CIPHER_MAX_IV_SIZE 16
59
60 /* The main SilcCipher structure. Use SilcCipher instead of SilcCipherStruct.
61    Also remember that SilcCipher is a pointer. */
62 typedef struct SilcCipherStruct {
63   SilcCipherObject *cipher;
64   void *context;
65   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
66   void (*set_iv)(struct SilcCipherStruct *, const unsigned char *);
67   void (*get_iv)(struct SilcCipherStruct *, unsigned char *);
68   SilcUInt32 (*get_key_len)(struct SilcCipherStruct *);
69   SilcUInt32 (*get_block_len)(struct SilcCipherStruct *);
70 } *SilcCipher;
71
72 /* Marks for all ciphers in silc. This can be used in silc_cipher_unregister
73    to unregister all ciphers at once. */
74 #define SILC_ALL_CIPHERS ((SilcCipherObject *)1)
75
76 /* Static list of ciphers for silc_cipher_register_default(). */
77 extern DLLAPI SilcCipherObject silc_default_ciphers[];
78
79 /* Default cipher in the SILC protocol */
80 #define SILC_DEFAULT_CIPHER "aes-256-cbc"
81
82 /* Macros */
83
84 /* Function names in SILC Crypto modules. The name of the cipher
85    is appended into these names and used to the get correct symbol out
86    of the module. All SILC Crypto API compliant modules must support
87    these function names (use macros below to assure this). */
88 #define SILC_CIPHER_SIM_SET_KEY "set_key"
89 #define SILC_CIPHER_SIM_SET_KEY_WITH_STRING "set_key_with_string"
90 #define SILC_CIPHER_SIM_ENCRYPT_CBC "encrypt_cbc"
91 #define SILC_CIPHER_SIM_DECRYPT_CBC "decrypt_cbc"
92 #define SILC_CIPHER_SIM_CONTEXT_LEN "context_len"
93
94 /* These macros can be used to implement the SILC Crypto API and to avoid
95    errors in the API these macros should be used always. */
96 #define SILC_CIPHER_API_SET_KEY(cipher)                 \
97 bool silc_##cipher##_set_key(void *context,             \
98                              const unsigned char *key,  \
99                              SilcUInt32 keylen)
100 #define SILC_CIPHER_API_SET_KEY_WITH_STRING(cipher)                     \
101 bool silc_##cipher##_set_key_with_string(void *context,                 \
102                                          const unsigned char *string,   \
103                                          SilcUInt32 stringlen)
104 #define SILC_CIPHER_API_ENCRYPT_CBC(cipher)                     \
105 bool silc_##cipher##_encrypt_cbc(void *context,                 \
106                                  const unsigned char *src,      \
107                                  unsigned char *dst,            \
108                                  SilcUInt32 len,                \
109                                  unsigned char *iv)
110 #define SILC_CIPHER_API_DECRYPT_CBC(cipher)                     \
111 bool silc_##cipher##_decrypt_cbc(void *context,                 \
112                                  const unsigned char *src,      \
113                                  unsigned char *dst,            \
114                                  SilcUInt32 len,                \
115                                  unsigned char *iv)
116 #define SILC_CIPHER_API_CONTEXT_LEN(cipher)                     \
117 SilcUInt32 silc_##cipher##_context_len()
118
119 /* Prototypes */
120 bool silc_cipher_register(SilcCipherObject *cipher);
121 bool silc_cipher_unregister(SilcCipherObject *cipher);
122 bool silc_cipher_register_default(void);
123 bool silc_cipher_alloc(const unsigned char *name, SilcCipher *new_cipher);
124 void silc_cipher_free(SilcCipher cipher);
125 bool silc_cipher_is_supported(const unsigned char *name);
126 char *silc_cipher_get_supported(void);
127 bool silc_cipher_encrypt(SilcCipher cipher, const unsigned char *src,
128                          unsigned char *dst, SilcUInt32 len, 
129                          unsigned char *iv);
130 bool silc_cipher_decrypt(SilcCipher cipher, const unsigned char *src,
131                          unsigned char *dst, SilcUInt32 len, 
132                          unsigned char *iv);
133 bool silc_cipher_set_key(SilcCipher cipher, const unsigned char *key,
134                          SilcUInt32 keylen);
135 void silc_cipher_set_iv(SilcCipher cipher, const unsigned char *iv);
136 void silc_cipher_get_iv(SilcCipher cipher, unsigned char *iv);
137 SilcUInt32 silc_cipher_get_key_len(SilcCipher cipher);
138 SilcUInt32 silc_cipher_get_block_len(SilcCipher cipher);
139
140 #endif