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