Rewrote crypto library init/uninit. Added silc_crypto_init
[silc.git] / lib / silccrypt / silcpkcs_i.h
1 /*
2
3   silcpkcs_i.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 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 SILCPKCS_I_H
21 #define SILCPKCS_I_H
22
23 #ifndef SILCPKCS_H
24 #error "Do not include this header directly"
25 #endif
26
27 /* The PKCS Algorithm object to represent any PKCS algorithm.  This context
28    implements the PKCS algorithm, such as RSA, DSA, etc. */
29 struct SilcPKCSAlgorithmStruct {
30   /* Algorithm name and scheme */
31   char *name;                   /* Eg. rsa, dsa, etc. */
32   char *scheme;                 /* Eg. pkcs1, openpgp, etc. */
33
34   /* Supported hash functions, comma separated list */
35   char *hash;
36
37   /* Generate new key pair. Returns PKCS algorithm specific public key
38      and private key contexts. */
39   SilcBool (*generate_key)(const struct SilcPKCSAlgorithmStruct *pkcs,
40                            SilcUInt32 keylen,
41                            SilcRng rng,
42                            void **ret_public_key,
43                            void **ret_private_key);
44
45   /* Public key routines. */
46
47   /* Import/create new public key.  Returns the length of the data that was
48      imported from `key' or 0 on error.  Returns the PKCS algorithm specific
49      public key to `ret_public_key'. */
50   int (*import_public_key)(const struct SilcPKCSAlgorithmStruct *pkcs,
51                            void *key, SilcUInt32 key_len,
52                            void **ret_public_key);
53
54   /* Export/encode public key.  Returns the encoded public key buffer that
55      the caller must free. */
56   unsigned char *
57   (*export_public_key)(const struct SilcPKCSAlgorithmStruct *pkcs,
58                        SilcStack stack,
59                        void *public_key,
60                        SilcUInt32 *ret_len);
61
62   /* Returns the bit length of public key */
63   SilcUInt32 (*public_key_bitlen)(const struct SilcPKCSAlgorithmStruct *pkcs,
64                                   void *public_key);
65
66   /* Duplicated public key */
67   void *(*public_key_copy)(const struct SilcPKCSAlgorithmStruct *pkcs,
68                            void *public_key);
69
70   /* Compares two public keys.  Returns TRUE if they are identical. */
71   SilcBool (*public_key_compare)(const struct SilcPKCSAlgorithmStruct *pkcs,
72                                  void *key1, void *key2);
73
74   /* Free public key */
75   void (*public_key_free)(const struct SilcPKCSAlgorithmStruct *pkcs,
76                           void *public_key);
77
78   /* Private key routines. */
79
80   /* Import/create new private key.  Returns the length of the data that was
81      imported from `key' or 0 on error.  Returns the PKCS algorithm specific
82      private key to `ret_private_key'. */
83   int (*import_private_key)(const struct SilcPKCSAlgorithmStruct *pkcs,
84                             void *key,
85                             SilcUInt32 key_len,
86                             void **ret_private_key);
87
88   /* Export/encode private key.  Returns the encoded private key buffer that
89      the caller must free. */
90   unsigned char *
91   (*export_private_key)(const struct SilcPKCSAlgorithmStruct *pkcs,
92                         SilcStack stack,
93                         void *private_key,
94                         SilcUInt32 *ret_len);
95
96   /* Returns the bi length of private key */
97   SilcUInt32 (*private_key_bitlen)(const struct SilcPKCSAlgorithmStruct *pkcs,
98                                    void *public_key);
99
100   /* Free private key */
101   void (*private_key_free)(const struct SilcPKCSAlgorithmStruct *pkcs,
102                            void *private_key);
103
104   /* Encrypt and decrypt operations */
105   SilcAsyncOperation (*encrypt)(const struct SilcPKCSAlgorithmStruct *pkcs,
106                                 void *public_key,
107                                 unsigned char *src,
108                                 SilcUInt32 src_len,
109                                 SilcRng rng,
110                                 SilcPKCSEncryptCb encrypt_cb,
111                                 void *context);
112   SilcAsyncOperation (*decrypt)(const struct SilcPKCSAlgorithmStruct *pkcs,
113                                 void *private_key,
114                                 unsigned char *src,
115                                 SilcUInt32 src_len,
116                                 SilcPKCSDecryptCb decrypt_cb,
117                                 void *context);
118
119   /* Signature and verification operations */
120   SilcAsyncOperation (*sign)(const struct SilcPKCSAlgorithmStruct *pkcs,
121                              void *private_key,
122                              unsigned char *src,
123                              SilcUInt32 src_len,
124                              SilcBool compute_hash,
125                              SilcHash hash,
126                              SilcPKCSSignCb sign_cb,
127                              void *context);
128   SilcAsyncOperation (*verify)(const struct SilcPKCSAlgorithmStruct *pkcs,
129                                void *public_key,
130                                unsigned char *signature,
131                                SilcUInt32 signature_len,
132                                unsigned char *data,
133                                SilcUInt32 data_len,
134                                SilcHash hash,
135                                SilcPKCSVerifyCb verify_cb,
136                                void *context);
137 };
138
139 /* The PKCS (Public Key Cryptosystem) object to represent any PKCS.  This
140    context implements the PKCS, such as SILC public keys, X.509 certificates,
141    OpenPGP certificates, etc. under a common API. */
142 struct SilcPKCSObjectStruct {
143   /* PKCS type */
144   SilcPKCSType type;
145
146   /* Public key routines */
147
148   /* Returns PKCS algorithm context from public key */
149   const SilcPKCSAlgorithm *
150   (*get_algorithm)(const struct SilcPKCSObjectStruct *pkcs,
151                    void *public_key);
152
153   /* Imports from public key file */
154   SilcBool (*import_public_key_file)(const struct SilcPKCSObjectStruct *pkcs,
155                                      unsigned char *filedata,
156                                      SilcUInt32 filedata_len,
157                                      SilcPKCSFileEncoding encoding,
158                                      void **ret_public_key);
159
160   /* Imports from public key binary data.  Returns the amount of bytes
161      imported from `key' or 0 on error. */
162   int (*import_public_key)(const struct SilcPKCSObjectStruct *pkcs,
163                            void *key,
164                            SilcUInt32 key_len,
165                            void **ret_public_key);
166
167   /* Exports public key to file */
168   unsigned char *
169   (*export_public_key_file)(const struct SilcPKCSObjectStruct *pkcs,
170                             SilcStack stack,
171                             void *public_key,
172                             SilcPKCSFileEncoding encoding,
173                             SilcUInt32 *ret_len);
174
175   /* Export public key as binary data */
176   unsigned char *(*export_public_key)(const struct SilcPKCSObjectStruct *pkcs,
177                                       SilcStack stack,
178                                       void *public_key,
179                                       SilcUInt32 *ret_len);
180
181   /* Returns key length in bits */
182   SilcUInt32 (*public_key_bitlen)(const struct SilcPKCSObjectStruct *pkcs,
183                                   void *public_key);
184
185   /* Copy public key */
186   void *(*public_key_copy)(const struct SilcPKCSObjectStruct *pkcs,
187                            void *public_key);
188
189   /* Compares public keys */
190   SilcBool (*public_key_compare)(const struct SilcPKCSObjectStruct *pkcs,
191                                  void *key1, void *key2);
192
193   /* Free public key */
194   void (*public_key_free)(const struct SilcPKCSObjectStruct *pkcs,
195                           void *public_key);
196
197   /* Private key routines */
198
199   /* Imports from private key file */
200   SilcBool (*import_private_key_file)(const struct SilcPKCSObjectStruct *pkcs,
201                                       unsigned char *filedata,
202                                       SilcUInt32 filedata_len,
203                                       const char *passphrase,
204                                       SilcUInt32 passphrase_len,
205                                       SilcPKCSFileEncoding encoding,
206                                       void **ret_private_key);
207
208   /* Imports from private key binary data.  Returns the amount of bytes
209      imported from `key' or 0 on error. */
210   int (*import_private_key)(const struct SilcPKCSObjectStruct *pkcs,
211                             void *key,
212                             SilcUInt32 key_len,
213                             void **ret_private_key);
214
215   /* Exports private key to file */
216   unsigned char *
217   (*export_private_key_file)(const struct SilcPKCSObjectStruct *pkcs,
218                              SilcStack stack,
219                              void *private_key,
220                              const char *passphrase,
221                              SilcUInt32 passphrase_len,
222                              SilcPKCSFileEncoding encoding,
223                              SilcRng rng,
224                              SilcUInt32 *ret_len);
225
226   /* Export private key as binary data */
227   unsigned char *(*export_private_key)(const struct SilcPKCSObjectStruct *pkcs,
228                                        SilcStack stack,
229                                        void *private_key,
230                                        SilcUInt32 *ret_len);
231
232   /* Returns key length in bits */
233   SilcUInt32 (*private_key_bitlen)(const struct SilcPKCSObjectStruct *pkcs,
234                                    void *private_key);
235
236   /* Free private key */
237   void (*private_key_free)(const struct SilcPKCSObjectStruct *pkcs,
238                            void *private_key);
239
240   /* Encrypt and decrypt operations */
241   SilcAsyncOperation (*encrypt)(const struct SilcPKCSObjectStruct *pkcs,
242                                 void *public_key,
243                                 unsigned char *src,
244                                 SilcUInt32 src_len,
245                                 SilcRng rng,
246                                 SilcPKCSEncryptCb encrypt_cb,
247                                 void *context);
248   SilcAsyncOperation (*decrypt)(const struct SilcPKCSObjectStruct *pkcs,
249                                 void *private_key,
250                                 unsigned char *src,
251                                 SilcUInt32 src_len,
252                                 SilcPKCSDecryptCb decrypt_cb,
253                                 void *context);
254
255   /* Signature and verification operations */
256   SilcAsyncOperation (*sign)(const struct SilcPKCSObjectStruct *pkcs,
257                              void *private_key,
258                              unsigned char *src,
259                              SilcUInt32 src_len,
260                              SilcBool compute_hash,
261                              SilcHash hash,
262                              SilcPKCSSignCb sign_cb,
263                              void *context);
264   SilcAsyncOperation (*verify)(const struct SilcPKCSObjectStruct *pkcs,
265                                void *public_key,
266                                unsigned char *signature,
267                                SilcUInt32 signature_len,
268                                unsigned char *data,
269                                SilcUInt32 data_len,
270                                SilcHash hash,
271                                SilcPKCSVerifyCb verify_cb,
272                                void *context);
273 };
274
275 #endif /* SILCPKCS_I_H */