Added RNG to PKCS API and PKCS Alg API.
[silc.git] / lib / silccrypt / silcpkcs.h
1 /*
2
3   silcpkcs.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silccrypt/SILC PKCS Interface
21  *
22  * DESCRIPTION
23  *
24  * SILC PKCS API provides generic interface for performing various
25  * public key cryptography related operations with different types of
26  * public and private keys.  Support for loading and saving of different
27  * types of public key and private keys are also provided.
28  *
29  ***/
30
31 #ifndef SILCPKCS_H
32 #define SILCPKCS_H
33
34 /* Forward declarations */
35 typedef struct SilcPKCSAlgorithmStruct SilcPKCSAlgorithm;
36 typedef struct SilcPKCSObjectStruct SilcPKCSObject;
37
38 /****d* silccrypt/SilcPKCSAPI/SilcPKCSType
39  *
40  * NAME
41  *
42  *    typedef enum { ... } SilcPKCSType;
43  *
44  * DESCRIPTION
45  *
46  *    Supported public key cryptosystem types.
47  *
48  * SOURCE
49  */
50 typedef enum {
51   SILC_PKCS_SILC    = 1,        /* SILC PKCS */
52   SILC_PKCS_SSH2    = 2,        /* SSH2 PKCS */
53   SILC_PKCS_X509V3  = 3,        /* X.509v3 PKCS */
54   SILC_PKCS_OPENPGP = 4,        /* OpenPGP PKCS */
55   SILC_PKCS_SPKI    = 5,        /* SPKI PKCS (not supported) */
56   SILC_PKCS_ANY     = 0,
57 } SilcPKCSType;
58 /***/
59
60 /****s* silccrypt/SilcPKCSAPI/SilcPublicKey
61  *
62  * NAME
63  *
64  *    typedef struct { ... } *SilcPublicKey;
65  *
66  * DESCRIPTION
67  *
68  *    This context represents any kind of PKCS public key.  It can be
69  *    allocated by silc_pkcs_public_key_alloc and is freed by the
70  *    silc_pkcs_public_key_free.  The PKCS specific public key context
71  *    can be retrieved by calling silc_pkcs_public_key_get_pkcs.
72  *
73  * SOURCE
74  */
75 typedef struct SilcPublicKeyStruct {
76   SilcPKCSObject *pkcs;         /* PKCS */
77   const SilcPKCSAlgorithm *alg; /* PKCS algorithm */
78   void *public_key;             /* PKCS specific public key */
79 } *SilcPublicKey;
80 /***/
81
82 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
83  *
84  * NAME
85  *
86  *    typedef struct { ... } *SilcPrivateKey;
87  *
88  * DESCRIPTION
89  *
90  *    This context represents any kind of PKCS private key.  The PKCS specific
91  *    key context can be retrieved by calling silc_pkcs_private_key_get_pkcs.
92  *
93  * SOURCE
94  */
95 typedef struct SilcPrivateKeyStruct {
96   SilcPKCSObject *pkcs;         /* PKCS */
97   const SilcPKCSAlgorithm *alg; /* PKCS algorithm */
98   void *private_key;            /* PKCS specific private key */
99 } *SilcPrivateKey;
100 /***/
101
102 /****d* silccrypt/SilcPKCSAPI/SilcPKCSFileEncoding
103  *
104  * NAME
105  *
106  *    typedef enum { ... } SilcPKCSType
107  *
108  * DESCRIPTION
109  *
110  *    Public and private key file encoding types.
111  *
112  * SOURCE
113  */
114 typedef enum {
115   SILC_PKCS_FILE_BIN,           /* Binary encoding */
116   SILC_PKCS_FILE_BASE64         /* Base64 encoding */
117 } SilcPKCSFileEncoding;
118 /***/
119
120 /****f* silccrypt/SilcPKCSAPI/SilcPKCSEncryptCb
121  *
122  * SYNOPSIS
123  *
124  *    typedef void (*SilcPKCSEncryptCb)(SilcBool success,
125  *                                      const unsigned char *encrypted,
126  *                                      SilcUInt32 encrypted_len,
127  *                                      void *context);
128  *
129  * DESCRIPTION
130  *
131  *    Encryption callback.  This callback is given as argument to the
132  *    silc_pkcs_encrypt and the encrypted data is delivered to the caller
133  *    in this callback.  The `encrypted' is the encrypted data.  If the
134  *    `success' is FALSE the encryption operation failed.
135  *
136  ***/
137 typedef void (*SilcPKCSEncryptCb)(SilcBool success,
138                                   const unsigned char *encrypted,
139                                   SilcUInt32 encrypted_len,
140                                   void *context);
141
142 /****f* silccrypt/SilcPKCSAPI/SilcPKCSDecryptCb
143  *
144  * SYNOPSIS
145  *
146  *    typedef void (*SilcPKCSDecryptCb)(SilcBool success,
147  *                                      const unsigned char *decrypted,
148  *                                      SilcUInt32 decrypted_len,
149  *                                      void *context);
150  *
151  * DESCRIPTION
152  *
153  *    Decryption callback.  This callback is given as argument to the
154  *    silc_pkcs_decrypt and the decrypted data is delivered to the caller
155  *    in this callback.  The `decrypted' is the decrypted data.  If the
156  *    `success' is FALSE the decryption operation failed.
157  *
158  ***/
159 typedef void (*SilcPKCSDecryptCb)(SilcBool success,
160                                   const unsigned char *decrypted,
161                                   SilcUInt32 decrypted_len,
162                                   void *context);
163
164 /****f* silccrypt/SilcPKCSAPI/SilcPKCSSignCb
165  *
166  * SYNOPSIS
167  *
168  *    typedef void (*SilcPKCSSignCb)(SilcBool success,
169  *                                   const unsigned char *signature,
170  *                                   SilcUInt32 signature_len,
171  *                                   void *context);
172  *
173  * DESCRIPTION
174  *
175  *    Signature callback.  This callback is given as argument to the
176  *    silc_pkcs_sign and the digitally signed data is delivered to the caller
177  *    in this callback.  The `signature' is the signature data.  If the
178  *    `success' is FALSE the signature operation failed.
179  *
180  ***/
181 typedef void (*SilcPKCSSignCb)(SilcBool success,
182                                const unsigned char *signature,
183                                SilcUInt32 signature_len,
184                                void *context);
185
186 /****f* silccrypt/SilcPKCSAPI/SilcPKCSVerifyCb
187  *
188  * SYNOPSIS
189  *
190  *    typedef void (*SilcPKCSVerifyCb)(SilcBool success, void *context);
191  *
192  * DESCRIPTION
193  *
194  *    Verification callback.  This callback is given as argument to the
195  *    silc_pkcs_verify and the result of the signature verification is
196  *    deliver to the caller in this callback.  If the `success' is FALSE
197  *    the signature verification failed.
198  *
199  ***/
200 typedef void (*SilcPKCSVerifyCb)(SilcBool success, void *context);
201
202 #include "silcpkcs_i.h"
203
204 /* Marks for all PKCS in. This can be used in silc_pkcs_unregister to
205    unregister all PKCS at once. */
206 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
207 #define SILC_ALL_PKCS_ALG ((SilcPKCSAlgorithm *)1)
208
209 /* Static lists of PKCS and PKCS algorithms. */
210 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
211 extern DLLAPI const SilcPKCSAlgorithm silc_default_pkcs_alg[];
212
213 /* Prototypes */
214
215 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
216  *
217  * SYNOPSIS
218  *
219  *    SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
220  *
221  * DESCRIPTION
222  *
223  *    Registers a new PKCS into the crypto library.  This function is used
224  *    at the initialization of an application.  All registered PKCSs
225  *    should be unregistered with silc_pkcs_unregister.  The `pkcs' includes
226  *    the name of the PKCS and member functions for the algorithm.  Usually
227  *    this function is not called directly.  Instead, application can call
228  *    the silc_pkcs_register_default to register all PKCSs that are
229  *    builtin the sources.  Returns FALSE on error.
230  *
231  ***/
232 SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
233
234 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister
235  *
236  * SYNOPSIS
237  *
238  *    SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
239  *
240  * DESCRIPTION
241  *
242  *    Unregister a PKCS from the crypto library. Returns FALSE on error.
243  *
244  ***/
245 SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
246
247 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_register
248  *
249  * SYNOPSIS
250  *
251  *    SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
252  *
253  * DESCRIPTION
254  *
255  *    Registers a new PKCS Algorithm into crypto library.  This function
256  *    is used at the initialization of an application.  All registered PKCS
257 *     algorithms should be unregistered with silc_pkcs_unregister.
258  *
259  ***/
260 SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
261
262 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_unregister
263  *
264  * SYNOPSIS
265  *
266  *    SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
267  *
268  * DESCRIPTION
269  *
270  *    Unregister a PKCS from the crypto library. Returns FALSE on error.
271  *
272  ***/
273 SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
274
275 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register_default
276  *
277  * SYNOPSIS
278  *
279  *    SilcBool silc_pkcs_register_default(void);
280  *
281  * DESCRIPTION
282  *
283  *    Registers all the default PKCS (all builtin PKCS) and PKCS algorithms.
284  *    The application may use this to register the default PKCS if specific
285  *    PKCS in any specific order is not wanted.  Returns FALSE on error.
286  *
287  ***/
288 SilcBool silc_pkcs_register_default(void);
289
290 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister_all
291  *
292  * SYNOPSIS
293  *
294  *    SilcBool silc_pkcs_unregister_all(void);
295  *
296  * DESCRIPTION
297  *
298  *    Unregister all PKCS and PKCS algorithms. Returns FALSE on error.
299  *
300  ***/
301 SilcBool silc_pkcs_unregister_all(void);
302
303 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_supported
304  *
305  * SYNOPSIS
306  *
307  *    char *silc_pkcs_get_supported(void);
308  *
309  * DESCRIPTION
310  *
311  *    Returns comma separated list of supported PKCS algorithms.
312  *
313  ***/
314 char *silc_pkcs_get_supported(void);
315
316 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_pkcs
317  *
318  * SYNOPSIS
319  *
320  *    const SilcPKCSObject *silc_pkcs_get_pkcs(SilcPKCSType type);
321  *
322  * DESCRIPTION
323  *
324  *    Finds PKCS context by the PKCS type.
325  *
326  ***/
327 const SilcPKCSObject *silc_pkcs_find_pkcs(SilcPKCSType type);
328
329 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_algorithm
330  *
331  * SYNOPSIS
332  *
333  *    const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
334  *                                                      const char *scheme);
335  *
336  * DESCRIPTION
337  *
338  *    Finds PKCS algorithm context by the algorithm name `algorithm' and
339  *    the algorithm scheme `scheme'.  The `scheme' may be NULL.
340  *
341  ***/
342 const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
343                                                   const char *scheme);
344
345 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_pkcs
346  *
347  * SYNOPSIS
348  *
349  *    const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
350  *
351  * DESCRIPTION
352  *
353  *    Returns the PKCS object from `key', which may be SilcPublicKey or
354  *    SilcPrivateKey pointer.
355  *
356  ***/
357 const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
358
359 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_algorithm
360  *
361  * SYNOPSIS
362  *
363  *    const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
364  *
365  * DESCRIPTION
366  *
367  *    Returns the PKCS algorithm object from `key', which may be SilcPublicKey
368  *    or SilcPrivateKey pointer.
369  *
370  ***/
371 const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
372
373 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_name
374  *
375  * SYNOPSIS
376  *
377  *    const char *silc_pkcs_get_name(void *key);
378  *
379  * DESCRIPTION
380  *
381  *    Returns PKCS algorithm name from the `key', which may be SilcPublicKey
382  *    or SilcPrivateKey pointer.
383  *
384  ***/
385 const char *silc_pkcs_get_name(void *key);
386
387 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_type
388  *
389  * SYNOPSIS
390  *
391  *    SilcPKCSType silc_pkcs_get_type(void *key);
392  *
393  * DESCRIPTION
394  *
395  *    Returns PKCS type from the `key', which may be SilcPublicKey or
396  *    SilcPrivateKey pointer.
397  *
398  ***/
399 SilcPKCSType silc_pkcs_get_type(void *key);
400
401 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_get_pkcs
402  *
403  * SYNOPSIS
404  *
405  *    void *silc_pkcs_public_key_get_pkcs(SilcPKCSType type,
406  *                                        SilcPublicKey public_key);
407  *
408  * DESCRIPTION
409  *
410  *    Returns the internal PKCS `type' specific public key context from the
411  *    `public_key'.  The caller needs to explicitly type cast it to correct
412  *    type.  Returns NULL on error.
413  *
414  *    For SILC_PKCS_SILC the returned context is SilcSILCPublicKey.
415  *    For SILC_PKCS_SSH2 the returned context is SilcSshPublicKey.
416  *
417  ***/
418 void *silc_pkcs_public_key_get_pkcs(SilcPKCSType type,
419                                     SilcPublicKey public_key);
420
421 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_get_pkcs
422  *
423  * SYNOPSIS
424  *
425  *    void *silc_pkcs_private_key_get_pkcs(SilcPKCSType type,
426  *                                        SilcPublicKey public_key);
427  *
428  * DESCRIPTION
429  *
430  *    Returns the internal PKCS `type' specific private key context from the
431  *    `private_key'.  The caller needs to explicitly type cast it to correct
432  *    type.  Returns NULL on error.
433  *
434  *    For SILC_PKCS_SILC the returned context is SilcSILCPrivateKey.
435  *    For SILC_PKCS_SSH2 the returned context is SilcSshPrivateKey.
436  *
437  ***/
438 void *silc_pkcs_private_key_get_pkcs(SilcPKCSType type,
439                                      SilcPrivateKey private_key);
440
441 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_alloc
442  *
443  * SYNOPSIS
444  *
445  *    SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
446  *                                        unsigned char *key,
447  *                                        SilcUInt32 key_len
448  *                                        SilcPublicKey *ret_public_key);
449  *
450  * DESCRIPTION
451  *
452  *    Allocates SilcPublicKey of the type of `type' from the key data
453  *    `key' of length of `key_len' bytes.  Returns FALSE if the `key'
454  *    is malformed or unsupported public key type.  This function can be
455  *    used to create public key from any kind of PKCS public keys that
456  *    the implementation supports.
457  *
458  ***/
459 SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
460                                     unsigned char *key,
461                                     SilcUInt32 key_len,
462                                     SilcPublicKey *ret_public_key);
463
464 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_free
465  *
466  * SYNOPSIS
467  *
468  *    void silc_pkcs_public_key_free(SilcPublicKey public_key);
469  *
470  * DESCRIPTION
471  *
472  *    Frees the public key.
473  *
474  ***/
475 void silc_pkcs_public_key_free(SilcPublicKey public_key);
476
477 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_export
478  *
479  * SYNOPSIS
480  *
481  *    unsigned char *silc_pkcs_public_key_encode(SilcStack stack,
482  *                                               SilcPublicKey public_key,
483  *                                               SilcUInt32 *ret_len);
484  *
485  * DESCRIPTION
486  *
487  *    Encodes the `public_key' into a binary format and returns it.  Returns
488  *    NULL on error.  Caller must free the returned buffer.
489  *
490  *    If the `stack' is non-NULL the returned buffer is allocated from the
491  *    `stack'.  This call will consume `stack' so caller should push the stack
492  *    before calling and then later pop it.
493  *
494  ***/
495 unsigned char *silc_pkcs_public_key_encode(SilcStack stack,
496                                            SilcPublicKey public_key,
497                                            SilcUInt32 *ret_len);
498
499 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_get_len
500  *
501  * SYNOPSIS
502  *
503  *    SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
504  *
505  * DESCRIPTION
506  *
507  *    Returns the key length in bits from the public key.
508  *
509  ***/
510 SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
511
512 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_compare
513  *
514  * SYNOPSIS
515  *
516  *    SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1,
517  *                                          SilcPublicKey key2);
518  *
519  * DESCRIPTION
520  *
521  *    Compares two public keys and returns TRUE if they are same key, and
522  *    FALSE if they are not same.
523  *
524  ***/
525 SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
526
527 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_copy
528  *
529  * SYNOPSIS
530  *
531  *    SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
532  *
533  * DESCRIPTION
534  *
535  *    Copies the public key indicated by `public_key' and returns new
536  *    allocated public key which is indentical to the `public_key'.
537  *
538  ***/
539 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
540
541 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_alloc
542  *
543  * SYNOPSIS
544  *
545  *    SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
546  *                                         unsigned char *key,
547  *                                         SilcUInt32 key_len,
548  *                                         SilcPrivateKey *ret_private_key);
549  *
550  * DESCRIPTION
551  *
552  *    Allocates SilcPrivateKey of the type of `type' from the key data
553  *    `key' of length of `key_len' bytes.  Returns FALSE if the `key'
554  *    is malformed or unsupported private key type.
555  *
556  ***/
557 SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
558                                      unsigned char *key,
559                                      SilcUInt32 key_len,
560                                      SilcPrivateKey *ret_private_key);
561
562 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_get_len
563  *
564  * SYNOPSIS
565  *
566  *    SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
567  *
568  * DESCRIPTION
569  *
570  *    Returns the key length in bits from the private key.
571  *
572  ***/
573 SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
574
575 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_free
576  *
577  * SYNOPSIS
578  *
579  *    void silc_pkcs_private_key_free(SilcPrivateKey private_key;
580  *
581  * DESCRIPTION
582  *
583  *    Frees the private key.
584  *
585  ***/
586 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
587
588 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encrypt
589  *
590  * SYNOPSIS
591  *
592  *    SilcAsyncOperation silc_pkcs_encrypt(SilcPublicKey public_key,
593  *                                         unsigned char *src,
594  *                                         SilcUInt32 src_len, SilcRng rng,
595  *                                         SilcPKCSEncryptCb encrypt_cb,
596  *                                         void *context);
597  *
598  * DESCRIPTION
599  *
600  *    Encrypts with the public key.  The `encrypt_cb' will be called to
601  *    deliver the encrypted data.  The encryption operation may be asynchronous
602  *    if the `public_key' is accelerated public key.  If this returns NULL
603  *    the asynchronous operation cannot be controlled.
604  *
605  ***/
606 SilcAsyncOperation silc_pkcs_encrypt(SilcPublicKey public_key,
607                                      unsigned char *src,
608                                      SilcUInt32 src_len, SilcRng rng,
609                                      SilcPKCSEncryptCb encrypt_cb,
610                                      void *context);
611
612 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decrypt
613  *
614  * SYNOPSIS
615  *
616  *    SilcAsyncOperation silc_pkcs_decrypt(SilcPrivateKey private_key,
617  *                                         unsigned char *src,
618  *                                         SilcUInt32 src_len,
619  *                                         SilcPKCSDecryptCb decrypt_cb,
620  *                                         void *context);
621  *
622  * DESCRIPTION
623  *
624  *    Decrypts with the private key.  The `decrypt_cb' will be called to
625  *    deliver the decrypted data.  The decryption operation may be asynchronous
626  *    if the `private_key' is accelerated private key.  If this returns NULL
627  *    the asynchronous operation cannot be controlled.
628  *
629  ***/
630 SilcAsyncOperation silc_pkcs_decrypt(SilcPrivateKey private_key,
631                                      unsigned char *src, SilcUInt32 src_len,
632                                      SilcPKCSDecryptCb decrypt_cb,
633                                      void *context);
634
635 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign
636  *
637  * SYNOPSIS
638  *
639  *    SilcAsyncOperation silc_pkcs_sign(SilcPrivateKey private_key,
640  *                                      unsigned char *src,
641  *                                      SilcUInt32 src_len,
642  *                                      SilcBool compute_hash,
643  *                                      SilcHash hash,
644  *                                      SilcRng rng,
645  *                                      SilcPKCSSignCb sign_cb,
646  *                                      void *context);
647  *
648  * DESCRIPTION
649  *
650  *    Computes signature with the private key.  The `sign_cb' will be called
651  *    to deliver the signature data.  If `compute_hash' is TRUE the `hash'
652  *    will be used to compute a message digest over the `src'.  The `hash'
653  *    must always be valid.  The `rng' should always be provided.  The
654  *    signature operation may be asynchronous if the `private_key' is
655  *    accelerated private key.  If this returns NULL the asynchronous
656  *    operation cannot be controlled.
657  *
658  ***/
659 SilcAsyncOperation silc_pkcs_sign(SilcPrivateKey private_key,
660                                   unsigned char *src,
661                                   SilcUInt32 src_len,
662                                   SilcBool compute_hash,
663                                   SilcHash hash,
664                                   SilcRng rng,
665                                   SilcPKCSSignCb sign_cb,
666                                   void *context);
667
668 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify
669  *
670  * SYNOPSIS
671  *
672  *    SilcAsyncOperation silc_pkcs_verify(SilcPublicKey public_key,
673  *                                        unsigned char *signature,
674  *                                        SilcUInt32 signature_len,
675  *                                        unsigned char *data,
676  *                                        SilcUInt32 data_len,
677  *                                        SilcHash hash,
678  *                                        SilcRng rng,
679  *                                        SilcPKCSVerifyCb verify_cb,
680  *                                        void *context);
681  *
682  * DESCRIPTION
683  *
684  *    Verifies signature.  The `verify_cb' will be called to deliver the
685  *    result of the verification process.  The 'signature' is verified against
686  *    the 'data'.  If the `hash' is non-NULL then the `data' will hashed
687  *    before verification.  If the `hash' is NULL, then the hash algorithm
688  *    to be used is retrieved from the signature.  If it isn't present in the
689  *    signature the verification is done as is without hashing.  The `rng'
690  *    is usually not needed, however some algorithms might need it so it is
691  *    wise to provide it.  If this returns NULL the asynchronous operation
692  *    cannot be controlled.
693  *
694  ***/
695 SilcAsyncOperation silc_pkcs_verify(SilcPublicKey public_key,
696                                     unsigned char *signature,
697                                     SilcUInt32 signature_len,
698                                     unsigned char *data,
699                                     SilcUInt32 data_len,
700                                     SilcHash hash,
701                                     SilcRng rng,
702                                     SilcPKCSVerifyCb verify_cb,
703                                     void *context);
704
705 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_public_key
706  *
707  * SYNOPSIS
708  *
709  *    SilcBool silc_pkcs_load_public_key(const char *filename,
710  *                                       SilcPKCSType type,
711  *                                       SilcPublicKey *ret_public_key);
712  *
713  * DESCRIPTION
714  *
715  *    Loads public key from file and allocates new public key.  Returns TRUE
716  *    if loading was successful.  If `type' is SILC_PKSC_ANY this attempts
717  *    to automatically detect the public key type.  If `type' is some other
718  *    PKCS type, the key is expected to be of that type.
719  *
720  ***/
721 SilcBool silc_pkcs_load_public_key(const char *filename,
722                                    SilcPKCSType type,
723                                    SilcPublicKey *ret_public_key);
724
725 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key
726  *
727  * SYNOPSIS
728  *
729  *    SilcBool silc_pkcs_save_public_key(const char *filename,
730  *                                       SilcPublicKey public_key,
731  *                                       SilcPKCSFileEncoding encoding);
732  *
733  * DESCRIPTION
734  *
735  *    Saves public key into file with specified encoding.  Returns FALSE
736  *    on error.
737  *
738  ***/
739 SilcBool silc_pkcs_save_public_key(const char *filename,
740                                    SilcPublicKey public_key,
741                                    SilcPKCSFileEncoding encoding);
742
743 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_private_key
744  *
745  * SYNOPSIS
746  *
747  *    SilcBool silc_pkcs_load_private_key(const char *filename,
748  *                                        const unsigned char *passphrase,
749  *                                        SilcUInt32 passphrase_len,
750  *                                        SilcPKCSType type,
751  *                                        SilcPrivateKey *ret_private_key);
752  *
753  * DESCRIPTION
754  *
755  *    Loads private key from file and allocates new private key.  Returns TRUE
756  *    if loading was successful.  The `passphrase' is used as decryption
757  *    key of the private key file, in case it is encrypted.  If `type' is
758  *    SILC_PKSC_ANY this attempts to automatically detect the private key type.
759  *    If `type' is some other PKCS type, the key is expected to be of that
760  *    type.
761  *
762  ***/
763 SilcBool silc_pkcs_load_private_key(const char *filename,
764                                     const unsigned char *passphrase,
765                                     SilcUInt32 passphrase_len,
766                                     SilcPKCSType type,
767                                     SilcPrivateKey *ret_private_key);
768
769 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_private_key
770  *
771  * SYNOPSIS
772  *
773  *    SilcBool silc_pkcs_save_private_key(const char *filename,
774  *                                        SilcPrivateKey private_key,
775  *                                        const unsigned char *passphrase,
776  *                                        SilcUInt32 passphrase_len,
777  *                                        SilcPKCSFileEncoding encoding,
778  *                                        SilcRng rng);
779  *
780  * DESCRIPTION
781  *
782  *    Saves private key into file.  The private key is encrypted into
783  *    the file with the `passphrase' as a key, if PKCS supports encrypted
784  *    private keys.  Returns FALSE on error.
785  *
786  ***/
787 SilcBool silc_pkcs_save_private_key(const char *filename,
788                                     SilcPrivateKey private_key,
789                                     const unsigned char *passphrase,
790                                     SilcUInt32 passphrase_len,
791                                     SilcPKCSFileEncoding encoding,
792                                     SilcRng rng);
793
794 /****f* silccrypt/SilcPKCSAPI/silc_hash_public_key
795  *
796  * SYNOPSIS
797  *
798  *    SilcUInt32 silc_hash_public_key(void *key, void *user_context);
799  *
800  * DESCRIPTION
801  *
802  *    An utility function for hashing public key for SilcHashTable.  Give
803  *    this as argument as the hash function for SilcHashTable.
804  *
805  ***/
806 SilcUInt32 silc_hash_public_key(void *key, void *user_context);
807
808 /****f* silccrypt/SilcPKCSAPI/silc_hash_public_key_compare
809  *
810  * SYNOPSIS
811  *
812  *    SilcBool silc_hash_public_key_compare(void *key1, void *key2,
813  *                                          void *user_context);
814  *
815  * DESCRIPTION
816  *
817  *    An utility function for comparing public keys for SilcHashTable.  Give
818  *    this as argument as the compare function for SilcHashTable.
819  *
820  ***/
821 SilcBool silc_hash_public_key_compare(void *key1, void *key2,
822                                       void *user_context);
823
824 #endif  /* !SILCPKCS_H */