5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
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.
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.
20 /****h* silccrypt/SILC PKCS Interface
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.
34 /* Forward declarations */
35 typedef struct SilcPKCSObjectStruct SilcPKCSObject;
37 /****d* silccrypt/SilcPKCSAPI/SilcPKCSType
41 * typedef enum { ... } SilcPKCSType;
45 * Supported public key cryptosystem types.
50 SILC_PKCS_SILC = 1, /* SILC PKCS */
51 SILC_PKCS_SSH2 = 2, /* SSH2 PKCS (not supported) */
52 SILC_PKCS_X509V3 = 3, /* X.509v3 PKCS (not supported) */
53 SILC_PKCS_OPENPGP = 4, /* OpenPGP PKCS (not supported) */
54 SILC_PKCS_SPKI = 5, /* SPKI PKCS (not supported) */
58 /****s* silccrypt/SilcPKCSAPI/SilcPublicKey
62 * typedef struct { ... } *SilcPublicKey;
66 * This context represents any kind of PKCS public key. It can be
67 * allocated by silc_pkcs_public_key_alloc and is freed by the
68 * silc_pkcs_public_key_free. The PKCS specific public key context
69 * can be retrieved by calling silc_pkcs_get_context.
74 const SilcPKCSObject *pkcs; /* PKCS */
75 void *public_key; /* PKCS specific public key */
79 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
83 * typedef struct { ... } *SilcPrivateKey;
87 * This context represents any kind of PKCS private key.
92 const SilcPKCSObject *pkcs; /* PKCS */
93 void *private_key; /* PKCS specific private key */
97 /****d* silccrypt/SilcPKCSAPI/SilcPKCSFileEncoding
101 * typedef enum { ... } SilcPKCSType
105 * Public and private key file encoding types.
110 SILC_PKCS_FILE_BIN, /* Binary encoding */
111 SILC_PKCS_FILE_BASE64 /* Base64 encoding */
112 } SilcPKCSFileEncoding;
115 /* The PKCS Algorithm object to represent any PKCS algorithm. */
117 /* Algorithm name and scheme */
121 /* Supported hash functions, comma separated list */
124 /* Generate new key pair. Returns PKCS algorithm specific public key
125 and private key contexts. */
126 SilcBool (*generate_key)(SilcUInt32 keylen,
128 void **ret_public_key,
129 void **ret_private_key);
131 /* Public key routines. */
132 int (*import_public_key)(unsigned char *key,
134 void **ret_public_key);
135 unsigned char *(*export_public_key)(void *public_key,
136 SilcUInt32 *ret_len);
137 SilcUInt32 (*public_key_bitlen)(void *public_key);
138 void *(*public_key_copy)(void *public_key);
139 SilcBool (*public_key_compare)(void *key1, void *key2);
140 void (*public_key_free)(void *public_key);
142 /* Private key routines */
143 int (*import_private_key)(unsigned char *key,
145 void **ret_private_key);
146 unsigned char *(*export_private_key)(void *private_key,
147 SilcUInt32 *ret_len);
148 SilcUInt32 (*private_key_bitlen)(void *public_key);
149 void (*private_key_free)(void *private_key);
151 /* Encrypt and decrypt operations */
152 SilcBool (*encrypt)(void *public_key,
157 SilcUInt32 *ret_dst_len,
159 SilcBool (*decrypt)(void *private_key,
164 SilcUInt32 *ret_dst_len);
166 /* Signature and verification operations */
167 SilcBool (*sign)(void *private_key,
170 unsigned char *signature,
171 SilcUInt32 signature_size,
172 SilcUInt32 *ret_signature_len,
174 SilcBool (*verify)(void *public_key,
175 unsigned char *signature,
176 SilcUInt32 signature_len,
182 /* The PKCS (Public Key Cryptosystem) object to represent any PKCS. */
183 struct SilcPKCSObjectStruct {
187 /* Public key routines */
189 /* Returns PKCS algorithm context from public key */
190 const SilcPKCSAlgorithm *(*get_algorithm)(void *public_key);
192 /* Imports from public key file */
193 SilcBool (*import_public_key_file)(unsigned char *filedata,
194 SilcUInt32 filedata_len,
195 SilcPKCSFileEncoding encoding,
196 void **ret_public_key);
198 /* Imports from public key binary data. Returns the amount of bytes
199 imported from `key' or 0 on error. */
200 int (*import_public_key)(unsigned char *key,
202 void **ret_public_key);
204 /* Exports public key to file */
205 unsigned char *(*export_public_key_file)(void *public_key,
206 SilcPKCSFileEncoding encoding,
207 SilcUInt32 *ret_len);
209 /* Export public key as binary data */
210 unsigned char *(*export_public_key)(void *public_key,
211 SilcUInt32 *ret_len);
213 /* Returns key length in bits */
214 SilcUInt32 (*public_key_bitlen)(void *public_key);
216 /* Copy public key */
217 void *(*public_key_copy)(void *public_key);
219 /* Compares public keys */
220 SilcBool (*public_key_compare)(void *key1, void *key2);
222 /* Free public key */
223 void (*public_key_free)(void *public_key);
225 /* Private key routines */
227 /* Imports from private key file */
228 SilcBool (*import_private_key_file)(unsigned char *filedata,
229 SilcUInt32 filedata_len,
230 const char *passphrase,
231 SilcUInt32 passphrase_len,
232 SilcPKCSFileEncoding encoding,
233 void **ret_private_key);
235 /* Imports from private key binary data. Returns the amount of bytes
236 imported from `key' or 0 on error. */
237 int (*import_private_key)(unsigned char *key,
239 void **ret_private_key);
241 /* Exports private key to file */
242 unsigned char *(*export_private_key_file)(void *private_key,
243 const char *passphrase,
244 SilcUInt32 passphrase_len,
245 SilcPKCSFileEncoding encoding,
247 SilcUInt32 *ret_len);
249 /* Export private key as binary data */
250 unsigned char *(*export_private_key)(void *private_key,
251 SilcUInt32 *ret_len);
253 /* Returns key length in bits */
254 SilcUInt32 (*private_key_bitlen)(void *private_key);
256 /* Free private key */
257 void (*private_key_free)(void *private_key);
259 /* Encrypt and decrypt operations */
260 SilcBool (*encrypt)(void *public_key,
265 SilcUInt32 *ret_dst_len,
267 SilcBool (*decrypt)(void *private_key,
272 SilcUInt32 *ret_dst_len);
274 /* Signature and verification operations */
275 SilcBool (*sign)(void *private_key,
278 unsigned char *signature,
279 SilcUInt32 signature_size,
280 SilcUInt32 *ret_signature_len,
282 SilcBool (*verify)(void *public_key,
283 unsigned char *signature,
284 SilcUInt32 signature_len,
290 /* Marks for all PKCS in. This can be used in silc_pkcs_unregister to
291 unregister all PKCS at once. */
292 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
293 #define SILC_ALL_PKCS_ALG ((SilcPKCSAlgorithm *)1)
295 /* Static lists of PKCS and PKCS algorithms. */
296 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
297 extern DLLAPI const SilcPKCSAlgorithm silc_default_pkcs_alg[];
301 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
305 * SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
309 * Registers a new PKCS into the SILC. This function is used
310 * at the initialization of the SILC. All registered PKCSs
311 * should be unregistered with silc_pkcs_unregister. The `pkcs' includes
312 * the name of the PKCS and member functions for the algorithm. Usually
313 * this function is not called directly. Instead, application can call
314 * the silc_pkcs_register_default to register all PKCSs that are
315 * builtin the sources. Returns FALSE on error.
318 SilcBool silc_pkcs_register(const SilcPKCSObject *pkcs);
320 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister
324 * SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
328 * Unregister a PKCS from the SILC. Returns FALSE on error.
331 SilcBool silc_pkcs_unregister(SilcPKCSObject *pkcs);
333 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_register
337 * SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
341 * Registers a new PKCS Algorithm into the SILC. This function is used
342 * at the initialization of the SILC. All registered PKCS algorithms
343 * should be unregistered with silc_pkcs_unregister.
346 SilcBool silc_pkcs_algorithm_register(const SilcPKCSAlgorithm *pkcs);
348 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_algorithm_unregister
352 * SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
356 * Unregister a PKCS from the SILC. Returns FALSE on error.
359 SilcBool silc_pkcs_algorithm_unregister(SilcPKCSAlgorithm *pkcs);
361 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register_default
365 * SilcBool silc_pkcs_register_default(void);
369 * Registers all the default PKCS (all builtin PKCS) and PKCS algorithms.
370 * The application may use this to register the default PKCS if specific
371 * PKCS in any specific order is not wanted. Returns FALSE on error.
374 SilcBool silc_pkcs_register_default(void);
376 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_unregister_all
380 * SilcBool silc_pkcs_unregister_all(void);
384 * Unregister all PKCS and PKCS algorithms. Returns FALSE on error.
387 SilcBool silc_pkcs_unregister_all(void);
389 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_supported
393 * char *silc_pkcs_get_supported(void);
397 * Returns comma separated list of supported PKCS algorithms.
400 char *silc_pkcs_get_supported(void);
402 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_pkcs
406 * const SilcPKCSObject *silc_pkcs_get_pkcs(SilcPKCSType type);
410 * Finds PKCS context by the PKCS type.
413 const SilcPKCSObject *silc_pkcs_find_pkcs(SilcPKCSType type);
415 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_find_algorithm
419 * const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
420 * const char *scheme);
424 * Finds PKCS algorithm context by the algorithm name `algorithm' and
425 * the algorithm scheme `scheme'. The `scheme' may be NULL.
428 const SilcPKCSAlgorithm *silc_pkcs_find_algorithm(const char *algorithm,
431 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_pkcs
435 * const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
439 * Returns the PKCS object from `key', which may be SilcPublicKey or
440 * SilcPrivateKey pointer.
443 const SilcPKCSObject *silc_pkcs_get_pkcs(void *key);
445 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_algorithm
449 * const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
453 * Returns the PKCS algorithm object from `key', which may be SilcPublicKey
454 * or SilcPrivateKey pointer.
457 const SilcPKCSAlgorithm *silc_pkcs_get_algorithm(void *key);
459 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_name
463 * const char *silc_pkcs_get_name(void *key);
467 * Returns PKCS algorithm name from the `key', which may be SilcPublicKey
468 * or SilcPrivateKey pointer.
471 const char *silc_pkcs_get_name(void *key);
473 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_type
477 * SilcPKCSType silc_pkcs_get_type(void *key);
481 * Returns PKCS type from the `key', which may be SilcPublicKey or
482 * SilcPrivateKey pointer.
485 SilcPKCSType silc_pkcs_get_type(void *key);
487 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_get_context
491 * void *silc_pkcs_get_context(SilcPKCSType type, SilcPublicKey public_key);
495 * Returns the internal PKCS `type' specific public key context from the
496 * `public_key'. The caller needs to explicitly type cast it to correct
497 * type. Returns NULL on error.
499 * For SILC_PKCS_SILC the returned context is SilcSILCPublicKey.
502 void *silc_pkcs_get_context(SilcPKCSType type, SilcPublicKey public_key);
504 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_alloc
508 * SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
509 * unsigned char *key,
511 * SilcPublicKey *ret_public_key);
515 * Allocates SilcPublicKey of the type of `type' from the key data
516 * `key' of length of `key_len' bytes. Returns FALSE if the `key'
517 * is malformed or unsupported public key type. This function can be
518 * used to create public key from any kind of PKCS public keys that
519 * the implementation supports.
522 SilcBool silc_pkcs_public_key_alloc(SilcPKCSType type,
525 SilcPublicKey *ret_public_key);
527 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_free
531 * void silc_pkcs_public_key_free(SilcPublicKey public_key);
535 * Frees the public key.
538 void silc_pkcs_public_key_free(SilcPublicKey public_key);
540 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_export
544 * unsigned char *silc_pkcs_public_key_encode(SilcPublicKey public_key,
545 * SilcUInt32 *ret_len);
549 * Encodes the `public_key' into a binary format and returns it. Returns
550 * NULL on error. Caller must free the returned buffer.
553 unsigned char *silc_pkcs_public_key_encode(SilcPublicKey public_key,
554 SilcUInt32 *ret_len);
556 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_get_len
560 * SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
564 * Returns the key length in bits from the public key.
567 SilcUInt32 silc_pkcs_public_key_get_len(SilcPublicKey public_key);
569 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_compare
573 * SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1,
574 * SilcPublicKey key2);
578 * Compares two public keys and returns TRUE if they are same key, and
579 * FALSE if they are not same.
582 SilcBool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
584 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_public_key_copy
588 * SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
592 * Copies the public key indicated by `public_key' and returns new
593 * allocated public key which is indentical to the `public_key'.
596 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
598 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_alloc
602 * SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
603 * unsigned char *key,
604 * SilcUInt32 key_len,
605 * SilcPrivateKey *ret_private_key);
609 * Allocates SilcPrivateKey of the type of `type' from the key data
610 * `key' of length of `key_len' bytes. Returns FALSE if the `key'
611 * is malformed or unsupported private key type.
614 SilcBool silc_pkcs_private_key_alloc(SilcPKCSType type,
617 SilcPrivateKey *ret_private_key);
619 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_get_len
623 * SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
627 * Returns the key length in bits from the private key.
630 SilcUInt32 silc_pkcs_private_key_get_len(SilcPrivateKey private_key);
632 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_private_key_free
636 * void silc_pkcs_private_key_free(SilcPrivateKey private_key;
640 * Frees the private key.
643 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
645 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_encrypt
649 * SilcBool silc_pkcs_encrypt(SilcPublicKey public_key,
650 * unsigned char *src, SilcUInt32 src_len,
651 * unsigned char *dst, SilcUInt32 dst_size,
652 * SilcUInt32 *dst_len);
656 * Encrypts with the public key. Returns FALSE on error.
659 SilcBool silc_pkcs_encrypt(SilcPublicKey public_key,
660 unsigned char *src, SilcUInt32 src_len,
661 unsigned char *dst, SilcUInt32 dst_size,
662 SilcUInt32 *dst_len, SilcRng rng);
664 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_decrypt
668 * SilcBool silc_pkcs_decrypt(SilcPrivateKey private_key,
669 * unsigned char *src, SilcUInt32 src_len,
670 * unsigned char *dst, SilcUInt32 dst_size,
671 * SilcUInt32 *dst_len);
675 * Decrypts with the private key. Returns FALSE on error.
678 SilcBool silc_pkcs_decrypt(SilcPrivateKey private_key,
679 unsigned char *src, SilcUInt32 src_len,
680 unsigned char *dst, SilcUInt32 dst_size,
681 SilcUInt32 *dst_len);
683 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_sign
687 * SilcBool silc_pkcs_sign(SilcPrivateKey private_key,
688 * unsigned char *src, SilcUInt32 src_len,
689 * unsigned char *dst, SilcUInt32 dst_size,
690 * SilcUInt32 *dst_len, SilcHash hash);
694 * Generates signature with the private key. Returns FALSE on error.
695 * If `hash' is non-NULL the `src' will be hashed before signing.
698 SilcBool silc_pkcs_sign(SilcPrivateKey private_key,
699 unsigned char *src, SilcUInt32 src_len,
700 unsigned char *dst, SilcUInt32 dst_size,
701 SilcUInt32 *dst_len, SilcHash hash);
703 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_verify
707 * SilcBool silc_pkcs_verify(SilcPublicKey public_key,
708 * unsigned char *signature,
709 * SilcUInt32 signature_len,
710 * unsigned char *data,
711 * SilcUInt32 data_len, SilcHash hash);
715 * Verifies signature. Returns FALSE on error. The 'signature' is
716 * verified against the 'data'. If the `hash' is non-NULL then the `data'
717 * will hashed before verification. If the `hash' is NULL, then the
718 * hash algorithm to be used is retrieved from the signature. If it
719 * isn't present in the signature the verification is done as is without
723 SilcBool silc_pkcs_verify(SilcPublicKey public_key,
724 unsigned char *signature,
725 SilcUInt32 signature_len,
727 SilcUInt32 data_len, SilcHash hash);
729 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_public_key
733 * SilcBool silc_pkcs_load_public_key(const char *filename,
734 * SilcPublicKey *ret_public_key);
738 * Loads public key from file and allocates new public key. Returns TRUE
739 * if loading was successful.
742 SilcBool silc_pkcs_load_public_key(const char *filename,
743 SilcPublicKey *ret_public_key);
745 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_public_key
749 * SilcBool silc_pkcs_save_public_key(const char *filename,
750 * SilcPublicKey public_key,
751 * SilcPKCSFileEncoding encoding);
755 * Saves public key into file with specified encoding. Returns FALSE
759 SilcBool silc_pkcs_save_public_key(const char *filename,
760 SilcPublicKey public_key,
761 SilcPKCSFileEncoding encoding);
763 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_load_private_key
767 * SilcBool silc_pkcs_load_private_key(const char *filename,
768 * const unsigned char *passphrase,
769 * SilcUInt32 passphrase_len,
770 * SilcPrivateKey *ret_private_key);
774 * Loads private key from file and allocates new private key. Returns TRUE
775 * if loading was successful. The `passphrase' is used as decryption
776 * key of the private key file, in case it is encrypted.
779 SilcBool silc_pkcs_load_private_key(const char *filename,
780 const unsigned char *passphrase,
781 SilcUInt32 passphrase_len,
782 SilcPrivateKey *ret_private_key);
784 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_save_private_key
788 * SilcBool silc_pkcs_save_private_key(const char *filename,
789 * SilcPrivateKey private_key,
790 * const unsigned char *passphrase,
791 * SilcUInt32 passphrase_len,
792 * SilcPKCSFileEncoding encoding,
797 * Saves private key into file. The private key is encrypted into
798 * the file with the `passphrase' as a key, if PKCS supports encrypted
799 * private keys. Returns FALSE on error.
802 SilcBool silc_pkcs_save_private_key(const char *filename,
803 SilcPrivateKey private_key,
804 const unsigned char *passphrase,
805 SilcUInt32 passphrase_len,
806 SilcPKCSFileEncoding encoding,
809 #endif /* !SILCPKCS_H */