Added support for encrypted private key files. The passphrase
[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   SilcUInt32 (*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 SilcUInt32 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 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister
260  *
261  * SYNOPSIS
262  *
263  *    bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
264  *
265  * DESCRIPTION
266  *
267  *    Unregister a PKCS from the SILC. Returns FALSE on error.
268  *
269  ***/
270 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
271
272 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register_default
273  *
274  * SYNOPSIS
275  *
276  *    bool silc_pkcs_register_default(void);
277  *
278  * DESCRIPTION
279  *
280  *    Registers all the default PKCS (all builtin PKCS).  The application may
281  *    use this to register the default PKCS if specific PKCS in any specific
282  *    order is not wanted. Returns FALSE on error.
283  *
284  ***/
285 bool silc_pkcs_register_default(void);
286
287 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister_all
288  *
289  * SYNOPSIS
290  *
291  *    bool silc_pkcs_unregister_all(void);
292  *
293  * DESCRIPTION
294  *
295  *    Returns FALSE on error.
296  *
297  ***/
298 bool silc_pkcs_unregister_all(void);
299
300 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_alloc
301  *
302  * SYNOPSIS
303  *
304  *    bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
305  *
306  * DESCRIPTION
307  *
308  *    Allocates a new SilcPKCS object. The new allocated object is returned
309  *    to the 'new_pkcs' argument. Returns FALSE on error.
310  *
311  ***/
312 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
313
314 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_free
315  *
316  * SYNOPSIS
317  *
318  *    void silc_pkcs_free(SilcPKCS pkcs);
319  *
320  * DESCRIPTION
321  *
322  *    Frees the PKCS object.
323  *
324  ***/
325 void silc_pkcs_free(SilcPKCS pkcs);
326
327 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_is_supported
328  *
329  * SYNOPSIS
330  *
331  *    bool silc_pkcs_is_supported(const unsigned char *name);
332  *
333  * DESCRIPTION
334  *
335  *    Returns TRUE if PKCS algorithm `name' is supported.
336  *
337  ***/
338 bool silc_pkcs_is_supported(const unsigned char *name);
339
340 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_supported
341  *
342  * SYNOPSIS
343  *
344  *    char *silc_pkcs_get_supported(void);
345  *
346  * DESCRIPTION
347  *
348  *    Returns comma separated list of supported PKCS algorithms.
349  *
350  ***/
351 char *silc_pkcs_get_supported(void);
352
353 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_generate_key
354  *
355  * SYNOPSIS
356  *
357  *    bool silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
358  *                                SilcRng rng);
359  *
360  * DESCRIPTION
361  *
362  *    Generate new key pair into the `pkcs' context. Returns FALSE on error.
363  *
364  ***/
365 bool silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
366                             SilcRng rng);
367
368 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_key_len
369  *
370  * SYNOPSIS
371  *
372  *    SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
373  *
374  * DESCRIPTION
375  *
376  *    Returns the length of the key.
377  *
378  ***/
379 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
380
381 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_name
382  *
383  * SYNOPSIS
384  *
385  *    const char *silc_pkcs_get_name(SilcPKCS pkcs);
386  *
387  * DESCRIPTION
388  *
389  *    Returns PKCS name.
390  *
391  ***/
392 const char *silc_pkcs_get_name(SilcPKCS pkcs);
393
394 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_public_key
395  *
396  * SYNOPSIS
397  *
398  *    unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
399  *
400  * DESCRIPTION
401  *
402  *    Returns SILC style public key.  The caller must free the returned
403  *    data.
404  *
405  ***/
406 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
407
408 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_private_key
409  *
410  * SYNOPSIS
411  *
412  *    unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs,
413  *                                             SilcUInt32 *len);
414  *
415  * DESCRIPTION
416  *
417  *    Returns SILC style private key.  The caller must free the returned
418  *    data and SHOULD zero the memory area before freeing.
419  *
420  ***/
421 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len);
422
423 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_set
424  *
425  * SYNOPSIS
426  *
427  *    SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs,
428  *                                        SilcPublicKey public_key);
429  *
430  * DESCRIPTION
431  *
432  *    Sets public key from SilcPublicKey. Returns the length of the key.
433  *
434  ***/
435 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
436
437 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_data_set
438  *
439  * SYNOPSIS
440  *
441  *    SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs,
442  *                                             unsigned char *pk,
443  *                                             SilcUInt32 pk_len);
444  *
445  * DESCRIPTION
446  *
447  *    Sets public key from data. Returns the length of the key.
448  *
449  ***/
450 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
451                                          SilcUInt32 pk_len);
452
453 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_set
454  *
455  * SYNOPSIS
456  *
457  *    SilcUInt32 silc_pkcs_private_key_set(SilcPKCS pkcs,
458  *                                         SilcPrivateKey private_key);
459  *
460  * DESCRIPTION
461  *
462  *    Sets private key from SilcPrivateKey. Returns the length of the key.
463  *
464  ***/
465 SilcUInt32 silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
466
467 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_data_set
468  *
469  * SYNOPSIS
470  *
471  *    SilcUInt32 silc_pkcs_private_key_data_set(SilcPKCS pkcs,
472  *                                              unsigned char *prv,
473  *                                              SilcUInt32 prv_len);
474  *
475  * DESCRIPTION
476  *
477  *    Sets private key from data. Returns the length of the key.
478  *
479  ***/
480 SilcUInt32 silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
481                                           SilcUInt32 prv_len);
482
483 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encrypt
484  *
485  * SYNOPSIS
486  *
487  *    bool silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src,
488  *                           SilcUInt32 src_len, unsigned char *dst,
489  *                           SilcUInt32 *dst_len);
490  *
491  * DESCRIPTION
492  *
493  *    Encrypts. Returns FALSE on error.
494  *
495  ***/
496 bool silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
497                        unsigned char *dst, SilcUInt32 *dst_len);
498
499 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decrypt
500  *
501  * SYNOPSIS
502  *
503  *    bool silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src,
504  *                           SilcUInt32 src_len, unsigned char *dst,
505  *                           SilcUInt32 *dst_len);
506  *
507  * DESCRIPTION
508  *
509  *    Decrypts. Returns FALSE on error.
510  *
511  ***/
512 bool silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
513                        unsigned char *dst, SilcUInt32 *dst_len);
514
515 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign
516  *
517  * SYNOPSIS
518  *
519  *    bool silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src,
520  *                        SilcUInt32 src_len, unsigned char *dst,
521  *                        SilcUInt32 *dst_len);
522  *
523  * DESCRIPTION
524  *
525  *    Generates signature. Returns FALSE on error.
526  *
527  ***/
528 bool silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
529                     unsigned char *dst, SilcUInt32 *dst_len);
530
531 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify
532  *
533  * SYNOPSIS
534  *
535  *    bool silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
536  *                          SilcUInt32 signature_len, unsigned char *data,
537  *                          SilcUInt32 data_len);
538  *
539  * DESCRIPTION
540  *
541  *    Verifies signature. Returns FALSE on error.
542  *
543  ***/
544 bool silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
545                       SilcUInt32 signature_len, unsigned char *data,
546                       SilcUInt32 data_len);
547
548 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign_with_hash
549  *
550  * SYNOPSIS
551  *
552  *    bool silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
553  *                                  unsigned char *src, SilcUInt32 src_len,
554  *                                  unsigned char *dst, SilcUInt32 *dst_len);
555  *
556  * DESCRIPTION
557  *
558  *    Generates signature with hash. The hash is signed. Returns FALSE on
559  *    error.
560  *
561  ***/
562 bool silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
563                               unsigned char *src, SilcUInt32 src_len,
564                               unsigned char *dst, SilcUInt32 *dst_len);
565
566 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify_with_hash
567  *
568  * SYNOPSIS
569  *
570  *    bool silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
571  *                                    unsigned char *signature,
572  *                                    SilcUInt32 signature_len,
573  *                                    unsigned char *data,
574  *                                    SilcUInt32 data_len);
575  *
576  * DESCRIPTION
577  *
578  *    Verifies signature with hash. The `data' is hashed and verified against
579  *    the `signature'. Returns FALSE on error.
580  *
581  ***/
582 bool silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
583                                 unsigned char *signature,
584                                 SilcUInt32 signature_len,
585                                 unsigned char *data,
586                                 SilcUInt32 data_len);
587
588 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encode_identifier
589  *
590  * SYNOPSIS
591  *
592  *    char *silc_pkcs_encode_identifier(char *username, char *host,
593  *                                      char *realname, char *email,
594  *                                      char *org, char *country);
595  *
596  * DESCRIPTION
597  *
598  *    Encodes and returns SILC public key identifier. If some of the
599  *    arguments is NULL those are not encoded into the identifier string.
600  *    Protocol says that at least username and host must be provided.
601  *
602  ***/
603 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
604                                   char *email, char *org, char *country);
605
606 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decode_identifier
607  *
608  * SYNOPSIS
609  *
610  *    SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
611  *
612  * DESCRIPTION
613  *
614  *    Decodes the provided `identifier' and returns allocated context for
615  *    the identifier.
616  *
617  ***/
618 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
619
620 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_free_identifier
621  *
622  * SYNOPSIS
623  *
624  *    void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
625  *
626  * DESCRIPTION
627  *
628  *    Frees decoded public key identifier context. Call this to free the
629  *    context returned by the silc_pkcs_decode_identifier.
630  *
631  ***/
632 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
633
634 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_alloc
635  *
636  * SYNOPSIS
637  *
638  *    SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
639  *                                             const char *identifier,
640  *                                             const unsigned char *pk,
641  *                                             SilcUInt32 pk_len);
642  *
643  * DESCRIPTION
644  *
645  *    Allocates SILC style public key formed from sent arguments. All data
646  *    is duplicated.
647  *
648  ***/
649 SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
650                                          const char *identifier,
651                                          const unsigned char *pk,
652                                          SilcUInt32 pk_len);
653
654 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_free
655  *
656  * SYNOPSIS
657  *
658  *    void silc_pkcs_public_key_free(SilcPublicKey public_key);
659  *
660  * DESCRIPTION
661  *
662  *    Frees public key.
663  *
664  ***/
665 void silc_pkcs_public_key_free(SilcPublicKey public_key);
666
667 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_alloc
668  *
669  * SYNOPSIS
670  *
671  *    SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
672  *                                               const unsigned char *prv,
673  *                                               SilcUInt32 prv_len);
674  *
675  * DESCRIPTION
676  *
677  *    Allocates SILC private key formed from sent arguments. All data is
678  *    duplicated.
679  *
680  ***/
681 SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
682                                            const unsigned char *prv,
683                                            SilcUInt32 prv_len);
684
685 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_free
686  *
687  * SYNOPSIS
688  *
689  *    void silc_pkcs_private_key_free(SilcPrivateKey private_key);
690  *
691  * DESCRIPTION
692  *
693  *    Frees private key.
694  *
695  ***/
696 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
697
698 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_encode
699  *
700  * SYNOPSIS
701  *
702  *    unsigned char *
703  *    silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
704  *
705  * DESCRIPTION
706  *
707  *    Encodes SILC style public key from SilcPublicKey. Returns the encoded
708  *    data.
709  *
710  ***/
711 unsigned char *
712 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
713
714 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_data_encode
715  *
716  * SYNOPSIS
717  *
718  *    unsigned char *
719  *    silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
720  *                                     char *pkcs, char *identifier,
721  *                                     SilcUInt32 *len);
722  *
723  * DESCRIPTION
724  *
725  *    Encodes SILC style public key. Returns the encoded data.
726  *
727  ***/
728 unsigned char *
729 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
730                                  char *pkcs, char *identifier,
731                                  SilcUInt32 *len);
732
733 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_decode
734  *
735  * SYNOPSIS
736  *
737  *    bool silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
738  *                                     SilcPublicKey *public_key);
739  *
740  * DESCRIPTION
741  *
742  *    Decodes SILC style public key. Returns TRUE if the decoding was
743  *    successful. Allocates new public key as well.
744  *
745  ***/
746 bool silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
747                                  SilcPublicKey *public_key);
748
749 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_compare
750  *
751  * SYNOPSIS
752  *
753  *    bool silc_pkcs_public_key_compare(SilcPublicKey key1,
754  *                                      SilcPublicKey key2);
755  *
756  * DESCRIPTION
757  *
758  *    Compares two public keys and returns TRUE if they are same key, and
759  *    FALSE if they are not same.
760  *
761  ***/
762 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
763
764 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_copy
765  *
766  * SYNOPSIS
767  *
768  *    SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
769  *
770  * DESCRIPTION
771  *
772  *    Copies the public key indicated by `public_key' and returns new allocated
773  *    public key which is indentical to the `public_key'.
774  *
775  ***/
776 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
777
778 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_encode
779  *
780  * SYNOPSIS
781  *
782  *    unsigned char *
783  *    silc_pkcs_private_key_encode(SilcPrivateKey private_key,
784  *                                 SilcUInt32 *len);
785  *
786  * DESCRIPTION
787  *
788  *    Encodes SILC private key from SilcPrivateKey. Returns the encoded data.
789  *
790  ***/
791 unsigned char *
792 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len);
793
794 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_data_encode
795  *
796  * SYNOPSIS
797  *
798  *    unsigned char *
799  *    silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
800  *                                      char *pkcs, SilcUInt32 *len);
801  *
802  * DESCRIPTION
803  *
804  *    Encodes SILC private key. Returns the encoded data.
805  *
806  ***/
807 unsigned char *
808 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
809                                   char *pkcs, SilcUInt32 *len);
810
811 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_decode
812  *
813  * SYNOPSIS
814  *
815  *    bool silc_pkcs_private_key_decode(unsigned char *data,
816  *                                      SilcUInt32 data_len,
817  *                                      SilcPrivateKey *private_key);
818  *
819  * DESCRIPTION
820  *
821  *    Decodes SILC style private key. Returns TRUE if the decoding was
822  *    successful. Allocates new private key as well.
823  *
824  ***/
825 bool silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
826                                   SilcPrivateKey *private_key);
827
828 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key
829  *
830  * SYNOPSIS
831  *
832  *    bool silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
833  *                                   SilcUInt32 encoding);
834  *
835  * DESCRIPTION
836  *
837  *    Saves public key into file. Returns FALSE on error.
838  *
839  ***/
840 bool silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
841                                SilcUInt32 encoding);
842
843 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key_data
844  *
845  * SYNOPSIS
846  *
847  *    bool silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
848  *                                        SilcUInt32 data_len,
849  *                                        SilcUInt32 encoding);
850  *
851  * DESCRIPTION
852  *
853  *    Saves public key into file.  The public key is already encoded as
854  *    data when calling this function.  Returns FALSE on error.
855  *
856  ***/
857 bool silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
858                                     SilcUInt32 data_len,
859                                     SilcUInt32 encoding);
860
861 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_private_key
862  *
863  * SYNOPSIS
864  *
865  *    bool silc_pkcs_save_private_key(char *filename,
866  *                                    SilcPrivateKey private_key,
867  *                                    unsigned char *passphrase,
868  *                                    SilcUInt32 passphrase_len,
869  *                                    SilcUInt32 encoding);
870  *
871  * DESCRIPTION
872  *
873  *    Saves private key into file.  The private key is encrypted into
874  *    the file with the `passphrase' as a key.  The encryption algorithm
875  *    is AES with 256 bit key in CBC mode.  Returns FALSE on error.
876  *
877  ***/
878 bool silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key, 
879                                 unsigned char *passphrase,
880                                 SilcUInt32 passphrase_len,
881                                 SilcUInt32 encoding);
882
883 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_public_key
884  *
885  * SYNOPSIS
886  *
887  *    bool silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
888  *                                   SilcUInt32 encoding);
889  *
890  * DESCRIPTION
891  *
892  *    Loads public key from file and allocates new public key. Returns TRUE
893  *    if loading was successful.
894  *
895  ***/
896 bool silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
897                                SilcUInt32 encoding);
898
899 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_private_key
900  *
901  * SYNOPSIS
902  *
903  *    bool silc_pkcs_load_private_key(char *filename,
904  *                                    SilcPrivateKey *private_key,
905  *                                    unsigned char *passphrase,
906  *                                    SilcUInt32 passphrase_len,
907  *                                    SilcUInt32 encoding);
908  *
909  * DESCRIPTION
910  *
911  *    Load private key from file and allocates new private key. Returns TRUE
912  *    if loading was successful. The `passphrase' is used as decryption
913  *    key of the private key file.
914  *
915  ***/
916 bool silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
917                                 unsigned char *passphrase,
918                                 SilcUInt32 passphrase_len,
919                                 SilcUInt32 encoding);
920
921 #endif /* SILCPKCS_H */