Added stacktrace support with --enable-stack-trace option.
[silc.git] / lib / silccrypt / silcpkcs.h
1 /*
2
3   silcpkcs.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 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_H
21 #define SILCPKCS_H
22
23 /****h* silccrypt/SILC PKCS Interface
24  *
25  * DESCRIPTION
26  *
27  *    This is the interface for public key cryptosystems, and various
28  *    utility functions related to public keys and private keys.  This
29  *    interface also defines the actual PKCS objects, public keys and
30  *    private keys.  The interface is generic PKCS interface, which has
31  *    capability of supporting any kind of public key algorithm.  This
32  *    interface also implements the SILC Public Key and routines for
33  *    encoding and decoding SILC Public Key (as defined by the SILC
34  *    protocol specification).  Interface or encrypting, decrypting,
35  *    producing digital signatures and verifying digital signatures are
36  *    also defined in this header.
37  *
38  ***/
39
40 /****s* silccrypt/SilcPKCSAPI/SilcPKCS
41  *
42  * NAME
43  * 
44  *    typedef struct SilcPKCSStruct *SilcPKCS;
45  *
46  * DESCRIPTION
47  *
48  *    This context is the actual PKCS context and is allocated
49  *    by silc_pkcs_alloc and given as argument usually to all
50  *    silc_pkcs _* functions.  It is freed by the silc_pkcs_free
51  *    function.
52  *
53  ***/
54 typedef struct SilcPKCSStruct *SilcPKCS;
55
56 /* The default SILC PKCS (Public Key Cryptosystem) object to represent
57    any PKCS in SILC. */
58 typedef struct SilcPKCSObjectStruct {
59   char *name;
60   int (*init)(void *, SilcUInt32, SilcRng);
61   void (*clear_keys)(void *);
62   unsigned char *(*get_public_key)(void *, SilcUInt32 *);
63   unsigned char *(*get_private_key)(void *, SilcUInt32 *);
64   SilcUInt32 (*set_public_key)(void *, unsigned char *, SilcUInt32);
65   int (*set_private_key)(void *, unsigned char *, SilcUInt32);
66   SilcUInt32 (*context_len)();
67   int (*encrypt)(void *, unsigned char *, SilcUInt32,
68                  unsigned char *, SilcUInt32 *);
69   int (*decrypt)(void *, unsigned char *, SilcUInt32,
70                  unsigned char *, SilcUInt32 *);
71   int (*sign)(void *, unsigned char *, SilcUInt32,
72               unsigned char *, SilcUInt32 *);
73   int (*verify)(void *, unsigned char *, SilcUInt32,
74                 unsigned char *, SilcUInt32);
75 } SilcPKCSObject;
76
77 /****s* silccrypt/SilcPKCSAPI/SilcPublicKey
78  *
79  * NAME
80  * 
81  *    typedef struct { ... } *SilcPublicKey;
82  *
83  * DESCRIPTION
84  *
85  *    SILC style public key object.  Public key is read from file to this
86  *    object.  Public keys received from network must be in this format as 
87  *    well.  The format is defined by the SILC protocol specification.
88  *    This object is allocated by silc_pkcs_public_key_alloc and freed
89  *    by silc_pkcs_public_key_free.  The object is given as argument to
90  *    all silc_pkcs_public_key_* functions.
91  *
92  * SOURCE
93  */
94 typedef struct {
95   SilcUInt32 len;
96   char *name;
97   char *identifier;
98   unsigned char *pk;
99   SilcUInt32 pk_len;
100 } *SilcPublicKey;
101 /***/
102
103 /****s* silccrypt/SilcPKCSAPI/SilcPublicKeyIdentifier
104  *
105  * NAME
106  * 
107  *    typedef struct { ... } *SilcPublicKeyIdentifier;
108  *
109  * DESCRIPTION
110  *
111  *    Decoded SILC Public Key identifier.  Note that some of the fields 
112  *    may be NULL.  This context is allcated by the function
113  *    silc_pkcs_decode_identifier and freed by silc_pkcs_free_identifier.
114  *    The identifier in SilcPublicKey is the 'identifier' field, which
115  *    can be given as argument to silc_pkcs_decode_identifier.
116  *
117  * SOURCE
118  */
119 typedef struct {
120   char *username;
121   char *host;
122   char *realname;
123   char *email;
124   char *org;
125   char *country;
126 } *SilcPublicKeyIdentifier;
127 /***/
128
129 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
130  *
131  * NAME
132  * 
133  *    typedef struct { ... } *SilcPrivateKey;
134  *
135  * DESCRIPTION
136  *
137  *    SILC style private key object.  Public key is read from file to this
138  *    object.  This object is allocated by silc_pkcs_private_key_alloc and
139  *    freed by silc_pkcs_private_key_free.  The object is given as argument
140  *    to all silc_pkcs_private_key_* functions.
141  *
142  ***/
143 typedef struct {
144   char *name;
145   unsigned char *prv;
146   SilcUInt32 prv_len;
147 } *SilcPrivateKey;
148
149 /* Public and private key file headers */
150 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
151 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
152 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
153 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
154
155 /* Public and private key file encoding types */
156 #define SILC_PKCS_FILE_BIN 0
157 #define SILC_PKCS_FILE_PEM 1
158
159 /* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister
160    to unregister all PKCS at once. */
161 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
162
163 /* Static list of PKCS for silc_pkcs_register_default(). */
164 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
165
166 /* Default PKXS in the SILC protocol */
167 #define SILC_DEFAULT_PKCS "rsa"
168
169 /* Macros */
170
171 /* Macros used to implement the SILC PKCS API */
172
173 /* XXX: This needs slight redesigning. These needs to be made even
174    more generic. I don't like that the actual prime generation is done
175    in PKCS_API_INIT. The primes used in key generation should be sent
176    as argument to the init function. By doing this we would achieve
177    that PKCS could be used as SIM's. The only requirement would be
178    that they are compiled against GMP (well, actually even that would
179    not be a requirement, but the most generic case anyway). The new init 
180    would look something like this:
181
182    #define SILC_PKCS_API_INIT(pkcs) \
183    inline int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
184                                  void *p1, void *p2)
185
186    Now we wouldn't have to send the SilcRng object since the primes are 
187    provided as arguments. To send them as void * they could actually be 
188    used as in anyway for real (MP_INT (SilcMPInt) or even something else 
189    (the pointer could be kludged to be something else in the module))
190    (Plus, the SilcRng object management in prime generation would be
191    simpler and better what it is now (in silcprimegen.c, that is)).
192 */
193
194 #define SILC_PKCS_API_INIT(pkcs) \
195 int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
196                        SilcRng rng)
197 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
198 void silc_##pkcs##_clear_keys(void *context)
199 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
200 unsigned char *silc_##pkcs##_get_public_key(void *context, \
201                                             SilcUInt32 *ret_len)
202 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
203 unsigned char *silc_##pkcs##_get_private_key(void *context, \
204                                              SilcUInt32 *ret_len)
205 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
206 SilcUInt32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
207                                     SilcUInt32 key_len)
208 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
209 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
210                                   SilcUInt32 key_len)
211 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
212 SilcUInt32 silc_##pkcs##_context_len()
213 #define SILC_PKCS_API_ENCRYPT(pkcs) \
214 int silc_##pkcs##_encrypt(void *context, \
215                           unsigned char *src, \
216                           SilcUInt32 src_len, \
217                           unsigned char *dst, \
218                           SilcUInt32 *dst_len)
219 #define SILC_PKCS_API_DECRYPT(pkcs) \
220 int silc_##pkcs##_decrypt(void *context, \
221                           unsigned char *src, \
222                           SilcUInt32 src_len, \
223                           unsigned char *dst, \
224                           SilcUInt32 *dst_len)
225 #define SILC_PKCS_API_SIGN(pkcs) \
226 int silc_##pkcs##_sign(void *context, \
227                        unsigned char *src, \
228                        SilcUInt32 src_len, \
229                        unsigned char *dst, \
230                        SilcUInt32 *dst_len)
231 #define SILC_PKCS_API_VERIFY(pkcs) \
232 int silc_##pkcs##_verify(void *context, \
233                          unsigned char *signature, \
234                          SilcUInt32 signature_len, \
235                          unsigned char *data, \
236                          SilcUInt32 data_len)
237
238 /* Prototypes */
239
240 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
241  *
242  * SYNOPSIS
243  *
244  *    bool silc_pkcs_register(const SilcPKCSObject *pkcs);
245  *
246  * DESCRIPTION
247  *
248  *    Registers a new PKCS into the SILC.  This function is used
249  *    at the initialization of the SILC.  All registered PKCSs
250  *    should be unregistered with silc_pkcs_unregister.  The `pkcs' includes
251  *    the name of the PKCS and member functions for the algorithm.  Usually
252  *    this function is not called directly.  Instead, application can call
253  *    the silc_pkcs_register_default to register all PKCSs that are
254  *    builtin the sources.  Returns FALSE on error.
255  *
256  ***/
257 bool silc_pkcs_register(const SilcPKCSObject *pkcs);
258
259 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
260 bool silc_pkcs_register_default(void);
261 bool silc_pkcs_unregister_all(void);
262 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
263 void silc_pkcs_free(SilcPKCS pkcs);
264 int silc_pkcs_is_supported(const unsigned char *name);
265 char *silc_pkcs_get_supported(void);
266 int silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
267                            SilcRng rng);
268 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
269 const char *silc_pkcs_get_name(SilcPKCS pkcs);
270 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
271 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len);
272 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
273 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
274                                      SilcUInt32 pk_len);
275 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
276 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
277                                    SilcUInt32 prv_len);
278 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
279                       unsigned char *dst, SilcUInt32 *dst_len);
280 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
281                       unsigned char *dst, SilcUInt32 *dst_len);
282 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
283                    unsigned char *dst, SilcUInt32 *dst_len);
284 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature, 
285                      SilcUInt32 signature_len, unsigned char *data, 
286                      SilcUInt32 data_len);
287 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
288                              unsigned char *src, SilcUInt32 src_len,
289                              unsigned char *dst, SilcUInt32 *dst_len);
290 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash, 
291                                unsigned char *signature, 
292                                SilcUInt32 signature_len, 
293                                unsigned char *data, 
294                                SilcUInt32 data_len);
295 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
296                                   char *email, char *org, char *country);
297 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
298 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
299 SilcPublicKey silc_pkcs_public_key_alloc(const char *name, 
300                                          const char *identifier,
301                                          const unsigned char *pk, 
302                                          SilcUInt32 pk_len);
303 void silc_pkcs_public_key_free(SilcPublicKey public_key);
304 SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
305                                            const unsigned char *prv,
306                                            SilcUInt32 prv_len);
307 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
308 unsigned char *
309 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
310 unsigned char *
311 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
312                                  char *pkcs, char *identifier, 
313                                  SilcUInt32 *len);
314 int silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
315                                 SilcPublicKey *public_key);
316 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
317 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
318 unsigned char *
319 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len);
320 unsigned char *
321 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
322                                   char *pkcs, SilcUInt32 *len);
323 int silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
324                                  SilcPrivateKey *private_key);
325 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
326                               SilcUInt32 encoding);
327 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
328                                    SilcUInt32 data_len,
329                                    SilcUInt32 encoding);
330 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key, 
331                                unsigned char *passphrase,
332                                SilcUInt32 encoding);
333 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data, 
334                                     SilcUInt32 data_len,
335                                     unsigned char *passphrase,
336                                     SilcUInt32 encoding);
337 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
338                               SilcUInt32 encoding);
339 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
340                                SilcUInt32 encoding);
341
342 #endif /* SILCPKCS_H */