Merged silc_1_0_branch to trunk.
[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, SilcPublicKeyStruct;
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   SilcUInt16 pk_type;           /* Public key type (SilcSKEPKType) */
96   SilcUInt32 len;
97   char *name;
98   char *identifier;
99   unsigned char *pk;
100   SilcUInt32 pk_len;
101 } *SilcPublicKey, SilcPublicKeyStruct;
102 /***/
103
104 /****s* silccrypt/SilcPKCSAPI/SilcPublicKeyIdentifier
105  *
106  * NAME
107  *
108  *    typedef struct { ... } *SilcPublicKeyIdentifier,
109  *                            SilcPublicKeyIdentifierStruct;
110  *
111  * DESCRIPTION
112  *
113  *    Decoded SILC Public Key identifier.  Note that some of the fields
114  *    may be NULL.  This context is allocated by the function
115  *    silc_pkcs_decode_identifier and freed by silc_pkcs_free_identifier.
116  *    The identifier in SilcPublicKey is the `identifier' field, which
117  *    can be given as argument to silc_pkcs_decode_identifier.
118  *
119  * SOURCE
120  */
121 typedef struct {
122   char *username;
123   char *host;
124   char *realname;
125   char *email;
126   char *org;
127   char *country;
128 } *SilcPublicKeyIdentifier, SilcPublicKeyIdentifierStruct;
129 /***/
130
131 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
132  *
133  * NAME
134  *
135  *    typedef struct { ... } *SilcPrivateKey, SilcPrivateKeyStruct;
136  *
137  * DESCRIPTION
138  *
139  *    SILC style private key object.  Public key is read from file to this
140  *    object.  This object is allocated by silc_pkcs_private_key_alloc and
141  *    freed by silc_pkcs_private_key_free.  The object is given as argument
142  *    to all silc_pkcs_private_key_* functions.
143  *
144  ***/
145 typedef struct {
146   char *name;
147   unsigned char *prv;
148   SilcUInt32 prv_len;
149 } *SilcPrivateKey, SilcPrivateKeyStruct;
150
151 /* Public and private key file headers */
152 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
153 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
154 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
155 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
156
157 /* Public and private key file encoding types */
158 #define SILC_PKCS_FILE_BIN 0
159 #define SILC_PKCS_FILE_PEM 1
160
161 /* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister
162    to unregister all PKCS at once. */
163 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
164
165 /* Static list of PKCS for silc_pkcs_register_default(). */
166 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
167
168 /* Default PKXS in the SILC protocol */
169 #define SILC_DEFAULT_PKCS "rsa"
170
171 /* Macros */
172
173 /* Macros used to implement the SILC PKCS API */
174
175 /* XXX: This needs slight redesigning. These needs to be made even
176    more generic. I don't like that the actual prime generation is done
177    in PKCS_API_INIT. The primes used in key generation should be sent
178    as argument to the init function. By doing this we would achieve
179    that PKCS could be used as SIM's. The only requirement would be
180    that they are compiled against GMP (well, actually even that would
181    not be a requirement, but the most generic case anyway). The new init
182    would look something like this:
183
184    #define SILC_PKCS_API_INIT(pkcs) \
185    inline int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
186                                  void *p1, void *p2)
187
188    Now we wouldn't have to send the SilcRng object since the primes are
189    provided as arguments. To send them as void * they could actually be
190    used as in anyway for real (MP_INT (SilcMPInt) or even something else
191    (the pointer could be kludged to be something else in the module))
192    (Plus, the SilcRng object management in prime generation would be
193    simpler and better what it is now (in silcprimegen.c, that is)).
194 */
195
196 #define SILC_PKCS_API_INIT(pkcs) \
197 int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
198                        SilcRng rng)
199 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
200 void silc_##pkcs##_clear_keys(void *context)
201 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
202 unsigned char *silc_##pkcs##_get_public_key(void *context, \
203                                             SilcUInt32 *ret_len)
204 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
205 unsigned char *silc_##pkcs##_get_private_key(void *context, \
206                                              SilcUInt32 *ret_len)
207 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
208 SilcUInt32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
209                                         SilcUInt32 key_len)
210 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
211 SilcUInt32 silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
212                                          SilcUInt32 key_len)
213 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
214 SilcUInt32 silc_##pkcs##_context_len()
215 #define SILC_PKCS_API_ENCRYPT(pkcs) \
216 int silc_##pkcs##_encrypt(void *context, \
217                           unsigned char *src, \
218                           SilcUInt32 src_len, \
219                           unsigned char *dst, \
220                           SilcUInt32 *dst_len)
221 #define SILC_PKCS_API_DECRYPT(pkcs) \
222 int silc_##pkcs##_decrypt(void *context, \
223                           unsigned char *src, \
224                           SilcUInt32 src_len, \
225                           unsigned char *dst, \
226                           SilcUInt32 *dst_len)
227 #define SILC_PKCS_API_SIGN(pkcs) \
228 int silc_##pkcs##_sign(void *context, \
229                        unsigned char *src, \
230                        SilcUInt32 src_len, \
231                        unsigned char *dst, \
232                        SilcUInt32 *dst_len)
233 #define SILC_PKCS_API_VERIFY(pkcs) \
234 int silc_##pkcs##_verify(void *context, \
235                          unsigned char *signature, \
236                          SilcUInt32 signature_len, \
237                          unsigned char *data, \
238                          SilcUInt32 data_len)
239
240 /* Prototypes */
241
242 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
243  *
244  * SYNOPSIS
245  *
246  *    bool silc_pkcs_register(const SilcPKCSObject *pkcs);
247  *
248  * DESCRIPTION
249  *
250  *    Registers a new PKCS into the SILC.  This function is used
251  *    at the initialization of the SILC.  All registered PKCSs
252  *    should be unregistered with silc_pkcs_unregister.  The `pkcs' includes
253  *    the name of the PKCS and member functions for the algorithm.  Usually
254  *    this function is not called directly.  Instead, application can call
255  *    the silc_pkcs_register_default to register all PKCSs that are
256  *    builtin the sources.  Returns FALSE on error.
257  *
258  ***/
259 bool silc_pkcs_register(const SilcPKCSObject *pkcs);
260
261 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister
262  *
263  * SYNOPSIS
264  *
265  *    bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
266  *
267  * DESCRIPTION
268  *
269  *    Unregister a PKCS from the SILC. Returns FALSE on error.
270  *
271  ***/
272 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
273
274 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register_default
275  *
276  * SYNOPSIS
277  *
278  *    bool silc_pkcs_register_default(void);
279  *
280  * DESCRIPTION
281  *
282  *    Registers all the default PKCS (all builtin PKCS).  The application may
283  *    use this to register the default PKCS if specific PKCS in any specific
284  *    order is not wanted. Returns FALSE on error.
285  *
286  ***/
287 bool silc_pkcs_register_default(void);
288
289 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister_all
290  *
291  * SYNOPSIS
292  *
293  *    bool silc_pkcs_unregister_all(void);
294  *
295  * DESCRIPTION
296  *
297  *    Returns FALSE on error.
298  *
299  ***/
300 bool silc_pkcs_unregister_all(void);
301
302 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_alloc
303  *
304  * SYNOPSIS
305  *
306  *    bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
307  *
308  * DESCRIPTION
309  *
310  *    Allocates a new SilcPKCS object.  The new allocated object is returned
311  *    to the 'new_pkcs' argument.  Returns FALSE on error.
312  *
313  ***/
314 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
315
316 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_free
317  *
318  * SYNOPSIS
319  *
320  *    void silc_pkcs_free(SilcPKCS pkcs);
321  *
322  * DESCRIPTION
323  *
324  *    Frees the PKCS object.
325  *
326  ***/
327 void silc_pkcs_free(SilcPKCS pkcs);
328
329 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_is_supported
330  *
331  * SYNOPSIS
332  *
333  *    bool silc_pkcs_is_supported(const unsigned char *name);
334  *
335  * DESCRIPTION
336  *
337  *    Returns TRUE if PKCS algorithm `name' is supported.
338  *
339  ***/
340 bool silc_pkcs_is_supported(const unsigned char *name);
341
342 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_supported
343  *
344  * SYNOPSIS
345  *
346  *    char *silc_pkcs_get_supported(void);
347  *
348  * DESCRIPTION
349  *
350  *    Returns comma separated list of supported PKCS algorithms.
351  *
352  ***/
353 char *silc_pkcs_get_supported(void);
354
355 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_generate_key
356  *
357  * SYNOPSIS
358  *
359  *    bool silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
360  *                                SilcRng rng);
361  *
362  * DESCRIPTION
363  *
364  *    Generate new key pair into the `pkcs' context. Returns FALSE on error.
365  *
366  ***/
367 bool silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
368                             SilcRng rng);
369
370 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_key_len
371  *
372  * SYNOPSIS
373  *
374  *    SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
375  *
376  * DESCRIPTION
377  *
378  *    Returns the length of the key.
379  *
380  ***/
381 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
382
383 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_name
384  *
385  * SYNOPSIS
386  *
387  *    const char *silc_pkcs_get_name(SilcPKCS pkcs);
388  *
389  * DESCRIPTION
390  *
391  *    Returns PKCS name.
392  *
393  ***/
394 const char *silc_pkcs_get_name(SilcPKCS pkcs);
395
396 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_public_key
397  *
398  * SYNOPSIS
399  *
400  *    unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
401  *
402  * DESCRIPTION
403  *
404  *    Returns SILC style public key.  The caller must free the returned
405  *    data.
406  *
407  ***/
408 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
409
410 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_private_key
411  *
412  * SYNOPSIS
413  *
414  *    unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs,
415  *                                             SilcUInt32 *len);
416  *
417  * DESCRIPTION
418  *
419  *    Returns SILC style private key.  The caller must free the returned
420  *    data and SHOULD zero the memory area before freeing.
421  *
422  ***/
423 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len);
424
425 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_set
426  *
427  * SYNOPSIS
428  *
429  *    SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs,
430  *                                        SilcPublicKey public_key);
431  *
432  * DESCRIPTION
433  *
434  *    Sets public key from SilcPublicKey. Returns the length of the key.
435  *
436  ***/
437 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
438
439 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_data_set
440  *
441  * SYNOPSIS
442  *
443  *    SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs,
444  *                                             unsigned char *pk,
445  *                                             SilcUInt32 pk_len);
446  *
447  * DESCRIPTION
448  *
449  *    Sets public key from data. Returns the length of the key.
450  *
451  ***/
452 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
453                                          SilcUInt32 pk_len);
454
455 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_set
456  *
457  * SYNOPSIS
458  *
459  *    SilcUInt32 silc_pkcs_private_key_set(SilcPKCS pkcs,
460  *                                         SilcPrivateKey private_key);
461  *
462  * DESCRIPTION
463  *
464  *    Sets private key from SilcPrivateKey. Returns the length of the key.
465  *
466  ***/
467 SilcUInt32 silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
468
469 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_data_set
470  *
471  * SYNOPSIS
472  *
473  *    SilcUInt32 silc_pkcs_private_key_data_set(SilcPKCS pkcs,
474  *                                              unsigned char *prv,
475  *                                              SilcUInt32 prv_len);
476  *
477  * DESCRIPTION
478  *
479  *    Sets private key from data. Returns the length of the key.
480  *
481  ***/
482 SilcUInt32 silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
483                                           SilcUInt32 prv_len);
484
485 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encrypt
486  *
487  * SYNOPSIS
488  *
489  *    bool silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src,
490  *                           SilcUInt32 src_len, unsigned char *dst,
491  *                           SilcUInt32 *dst_len);
492  *
493  * DESCRIPTION
494  *
495  *    Encrypts. Returns FALSE on error.
496  *
497  ***/
498 bool silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
499                        unsigned char *dst, SilcUInt32 *dst_len);
500
501 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decrypt
502  *
503  * SYNOPSIS
504  *
505  *    bool silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src,
506  *                           SilcUInt32 src_len, unsigned char *dst,
507  *                           SilcUInt32 *dst_len);
508  *
509  * DESCRIPTION
510  *
511  *    Decrypts.  Returns FALSE on error.
512  *
513  ***/
514 bool silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
515                        unsigned char *dst, SilcUInt32 *dst_len);
516
517 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign
518  *
519  * SYNOPSIS
520  *
521  *    bool silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src,
522  *                        SilcUInt32 src_len, unsigned char *dst,
523  *                        SilcUInt32 *dst_len);
524  *
525  * DESCRIPTION
526  *
527  *    Generates signature.  Returns FALSE on error.
528  *
529  ***/
530 bool silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
531                     unsigned char *dst, SilcUInt32 *dst_len);
532
533 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify
534  *
535  * SYNOPSIS
536  *
537  *    bool silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
538  *                          SilcUInt32 signature_len, unsigned char *data,
539  *                          SilcUInt32 data_len);
540  *
541  * DESCRIPTION
542  *
543  *    Verifies signature.  Returns FALSE on error.
544  *
545  ***/
546 bool silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
547                       SilcUInt32 signature_len, unsigned char *data,
548                       SilcUInt32 data_len);
549
550 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign_with_hash
551  *
552  * SYNOPSIS
553  *
554  *    bool silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
555  *                                  unsigned char *src, SilcUInt32 src_len,
556  *                                  unsigned char *dst, SilcUInt32 *dst_len);
557  *
558  * DESCRIPTION
559  *
560  *    Generates signature with hash.  The hash is signed.  Returns FALSE on
561  *    error.
562  *
563  ***/
564 bool silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
565                               unsigned char *src, SilcUInt32 src_len,
566                               unsigned char *dst, SilcUInt32 *dst_len);
567
568 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify_with_hash
569  *
570  * SYNOPSIS
571  *
572  *    bool silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
573  *                                    unsigned char *signature,
574  *                                    SilcUInt32 signature_len,
575  *                                    unsigned char *data,
576  *                                    SilcUInt32 data_len);
577  *
578  * DESCRIPTION
579  *
580  *    Verifies signature with hash.  The `data' is hashed and verified against
581  *    the `signature'.  Returns FALSE on error.
582  *
583  ***/
584 bool silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
585                                 unsigned char *signature,
586                                 SilcUInt32 signature_len,
587                                 unsigned char *data,
588                                 SilcUInt32 data_len);
589
590 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encode_identifier
591  *
592  * SYNOPSIS
593  *
594  *    char *silc_pkcs_encode_identifier(char *username, char *host,
595  *                                      char *realname, char *email,
596  *                                      char *org, char *country);
597  *
598  * DESCRIPTION
599  *
600  *    Encodes and returns SILC public key identifier. If some of the
601  *    arguments is NULL those are not encoded into the identifier string.
602  *    Protocol says that at least username and host must be provided.
603  *
604  ***/
605 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
606                                   char *email, char *org, char *country);
607
608 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decode_identifier
609  *
610  * SYNOPSIS
611  *
612  *    SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
613  *
614  * DESCRIPTION
615  *
616  *    Decodes the provided `identifier' and returns allocated context for
617  *    the identifier.
618  *
619  ***/
620 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
621
622 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_free_identifier
623  *
624  * SYNOPSIS
625  *
626  *    void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
627  *
628  * DESCRIPTION
629  *
630  *    Frees decoded public key identifier context.  Call this to free the
631  *    context returned by the silc_pkcs_decode_identifier.
632  *
633  ***/
634 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
635
636 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_alloc
637  *
638  * SYNOPSIS
639  *
640  *    SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
641  *                                             const char *identifier,
642  *                                             const unsigned char *pk,
643  *                                             SilcUInt32 pk_len);
644  *
645  * DESCRIPTION
646  *
647  *    Allocates SILC style public key formed from sent arguments.  All data
648  *    is duplicated.
649  *
650  ***/
651 SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
652                                          const char *identifier,
653                                          const unsigned char *pk,
654                                          SilcUInt32 pk_len);
655
656 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_free
657  *
658  * SYNOPSIS
659  *
660  *    void silc_pkcs_public_key_free(SilcPublicKey public_key);
661  *
662  * DESCRIPTION
663  *
664  *    Frees public key.
665  *
666  ***/
667 void silc_pkcs_public_key_free(SilcPublicKey public_key);
668
669 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_alloc
670  *
671  * SYNOPSIS
672  *
673  *    SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
674  *                                               const unsigned char *prv,
675  *                                               SilcUInt32 prv_len);
676  *
677  * DESCRIPTION
678  *
679  *    Allocates SILC private key formed from sent arguments.  All data is
680  *    duplicated.
681  *
682  ***/
683 SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
684                                            const unsigned char *prv,
685                                            SilcUInt32 prv_len);
686
687 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_free
688  *
689  * SYNOPSIS
690  *
691  *    void silc_pkcs_private_key_free(SilcPrivateKey private_key);
692  *
693  * DESCRIPTION
694  *
695  *    Frees private key.
696  *
697  ***/
698 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
699
700 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_encode
701  *
702  * SYNOPSIS
703  *
704  *    unsigned char *
705  *    silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
706  *
707  * DESCRIPTION
708  *
709  *    Encodes SILC style public key from SilcPublicKey.  Returns the encoded
710  *    data.
711  *
712  ***/
713 unsigned char *
714 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
715
716 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_data_encode
717  *
718  * SYNOPSIS
719  *
720  *    unsigned char *
721  *    silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
722  *                                     char *pkcs, char *identifier,
723  *                                     SilcUInt32 *len);
724  *
725  * DESCRIPTION
726  *
727  *    Encodes SILC style public key.  Returns the encoded data.
728  *
729  ***/
730 unsigned char *
731 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
732                                  char *pkcs, char *identifier,
733                                  SilcUInt32 *len);
734
735 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_decode
736  *
737  * SYNOPSIS
738  *
739  *    bool silc_pkcs_public_key_decode(unsigned char *data,
740  *                                     SilcUInt32 data_len,
741  *                                     SilcPublicKey *public_key);
742  *
743  * DESCRIPTION
744  *
745  *    Decodes SILC style public key. Returns TRUE if the decoding was
746  *    successful. Allocates new public key as well.
747  *
748  ***/
749 bool silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
750                                  SilcPublicKey *public_key);
751
752 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_payload_encode
753  *
754  * SYNOPSIS
755  *
756  *    bool silc_pkcs_public_key_payload_encode(SilcPublicKey public_key);
757  *
758  * DESCRIPTION
759  *
760  *    Encodes the Public Key Payload from the public key indicated by
761  *    `public_key' of type of `pk_type'.  The type is SilcSKEPKType.
762  *    Returns the encoded payload buffer.
763  *
764  ***/
765 SilcBuffer silc_pkcs_public_key_payload_encode(SilcPublicKey public_key);
766
767 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_payload_decode
768  *
769  * SYNOPSIS
770  *
771  *    bool silc_pkcs_public_key_payload_decode(unsigned char *data,
772  *                                             SilcUInt32 data_len,
773  *                                             SilcPublicKey *public_key);
774  *
775  * DESCRIPTION
776  *
777  *    Decodes Public Key Payload from `data' of `data_len' bytes in length
778  *    data buffer into `public_key' pointer.  Returns FALSE if the payload
779  *    cannot be decoded.
780  *
781  ***/
782 bool silc_pkcs_public_key_payload_decode(unsigned char *data,
783                                          SilcUInt32 data_len,
784                                          SilcPublicKey *public_key);
785
786 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_compare
787  *
788  * SYNOPSIS
789  *
790  *    bool silc_pkcs_public_key_compare(SilcPublicKey key1,
791  *                                      SilcPublicKey key2);
792  *
793  * DESCRIPTION
794  *
795  *    Compares two public keys and returns TRUE if they are same key, and
796  *    FALSE if they are not same.
797  *
798  ***/
799 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
800
801 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_copy
802  *
803  * SYNOPSIS
804  *
805  *    SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
806  *
807  * DESCRIPTION
808  *
809  *    Copies the public key indicated by `public_key' and returns new allocated
810  *    public key which is indentical to the `public_key'.
811  *
812  ***/
813 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
814
815 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_encode
816  *
817  * SYNOPSIS
818  *
819  *    unsigned char *
820  *    silc_pkcs_private_key_encode(SilcPrivateKey private_key,
821  *                                 SilcUInt32 *len);
822  *
823  * DESCRIPTION
824  *
825  *    Encodes SILC private key from SilcPrivateKey.  Returns the encoded data.
826  *
827  ***/
828 unsigned char *
829 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len);
830
831 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_data_encode
832  *
833  * SYNOPSIS
834  *
835  *    unsigned char *
836  *    silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
837  *                                      char *pkcs, SilcUInt32 *len);
838  *
839  * DESCRIPTION
840  *
841  *    Encodes SILC private key.  Returns the encoded data.
842  *
843  ***/
844 unsigned char *
845 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
846                                   char *pkcs, SilcUInt32 *len);
847
848 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_decode
849  *
850  * SYNOPSIS
851  *
852  *    bool silc_pkcs_private_key_decode(unsigned char *data,
853  *                                      SilcUInt32 data_len,
854  *                                      SilcPrivateKey *private_key);
855  *
856  * DESCRIPTION
857  *
858  *    Decodes SILC style private key.  Returns TRUE if the decoding was
859  *    successful.  Allocates new private key as well.
860  *
861  ***/
862 bool silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
863                                   SilcPrivateKey *private_key);
864
865 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key
866  *
867  * SYNOPSIS
868  *
869  *    bool silc_pkcs_save_public_key(const char *filename,
870  *                                   SilcPublicKey public_key,
871  *                                   SilcUInt32 encoding);
872  *
873  * DESCRIPTION
874  *
875  *    Saves public key into file.  Returns FALSE on error.
876  *
877  ***/
878 bool silc_pkcs_save_public_key(const char *filename, SilcPublicKey public_key,
879                                SilcUInt32 encoding);
880
881 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key_data
882  *
883  * SYNOPSIS
884  *
885  *    bool silc_pkcs_save_public_key_data(const char *filename,
886  *                                        unsigned char *data,
887  *                                        SilcUInt32 data_len,
888  *                                        SilcUInt32 encoding);
889  *
890  * DESCRIPTION
891  *
892  *    Saves public key into file.  The public key is already encoded as
893  *    data when calling this function.  Returns FALSE on error.
894  *
895  ***/
896 bool silc_pkcs_save_public_key_data(const char *filename, unsigned char *data,
897                                     SilcUInt32 data_len, SilcUInt32 encoding);
898
899 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_private_key
900  *
901  * SYNOPSIS
902  *
903  *    bool silc_pkcs_save_private_key(const char *filename,
904  *                                    SilcPrivateKey private_key,
905  *                                    unsigned char *passphrase,
906  *                                    SilcUInt32 passphrase_len,
907  *                                    SilcUInt32 encoding);
908  *
909  * DESCRIPTION
910  *
911  *    Saves private key into file.  The private key is encrypted into
912  *    the file with the `passphrase' as a key.  The encryption algorithm
913  *    is AES with 256 bit key in CBC mode.  Returns FALSE on error.
914  *
915  ***/
916 bool silc_pkcs_save_private_key(const char *filename,
917                                 SilcPrivateKey private_key,
918                                 unsigned char *passphrase,
919                                 SilcUInt32 passphrase_len,
920                                 SilcUInt32 encoding);
921
922 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_public_key
923  *
924  * SYNOPSIS
925  *
926  *    bool silc_pkcs_load_public_key(const char *filename,
927  *                                   SilcPublicKey *public_key,
928  *                                   SilcUInt32 encoding);
929  *
930  * DESCRIPTION
931  *
932  *    Loads public key from file and allocates new public key.  Returns TRUE
933  *    if loading was successful.
934  *
935  ***/
936 bool silc_pkcs_load_public_key(const char *filename, SilcPublicKey *public_key,
937                                SilcUInt32 encoding);
938
939 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_private_key
940  *
941  * SYNOPSIS
942  *
943  *    bool silc_pkcs_load_private_key(const char *filename,
944  *                                    SilcPrivateKey *private_key,
945  *                                    unsigned char *passphrase,
946  *                                    SilcUInt32 passphrase_len,
947  *                                    SilcUInt32 encoding);
948  *
949  * DESCRIPTION
950  *
951  *    Loads private key from file and allocates new private key.  Returns TRUE
952  *    if loading was successful.  The `passphrase' is used as decryption
953  *    key of the private key file.
954  *
955  ***/
956 bool silc_pkcs_load_private_key(const char *filename,
957                                 SilcPrivateKey *private_key,
958                                 unsigned char *passphrase,
959                                 SilcUInt32 passphrase_len,
960                                 SilcUInt32 encoding);
961
962 #endif  /* !SILCPKCS_H */