5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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.
23 /****h* silccrypt/SILC PKCS Interface
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.
40 /****s* silccrypt/SilcPKCSAPI/SilcPKCS
44 * typedef struct SilcPKCSStruct *SilcPKCS;
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
54 typedef struct SilcPKCSStruct *SilcPKCS;
56 /* The default SILC PKCS (Public Key Cryptosystem) object to represent
58 typedef struct SilcPKCSObjectStruct {
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 int (*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);
77 /****s* silccrypt/SilcPKCSAPI/SilcPublicKey
81 * typedef struct { ... } *SilcPublicKey;
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.
103 /****s* silccrypt/SilcPKCSAPI/SilcPublicKeyIdentifier
107 * typedef struct { ... } *SilcPublicKeyIdentifier;
111 * Decoded SILC Public Key identifier. Note that some of the fields
112 * may be NULL. This context is allcated by the function
113 * silc_pkcs_decode_identifier and freed by silc_pkcs_free_identifier.
114 * The identifier in SilcPublicKey is the 'identifier' field, which
115 * can be given as argument to silc_pkcs_decode_identifier.
126 } *SilcPublicKeyIdentifier;
129 /****s* silccrypt/SilcPKCSAPI/SilcPrivateKey
133 * typedef struct { ... } *SilcPrivateKey;
137 * SILC style private key object. Public key is read from file to this
138 * object. This object is allocated by silc_pkcs_private_key_alloc and
139 * freed by silc_pkcs_private_key_free. The object is given as argument
140 * to all silc_pkcs_private_key_* functions.
149 /* Public and private key file headers */
150 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
151 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
152 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
153 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
155 /* Public and private key file encoding types */
156 #define SILC_PKCS_FILE_BIN 0
157 #define SILC_PKCS_FILE_PEM 1
159 /* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister
160 to unregister all PKCS at once. */
161 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
163 /* Static list of PKCS for silc_pkcs_register_default(). */
164 extern DLLAPI const SilcPKCSObject silc_default_pkcs[];
166 /* Default PKXS in the SILC protocol */
167 #define SILC_DEFAULT_PKCS "rsa"
171 /* Macros used to implement the SILC PKCS API */
173 /* XXX: This needs slight redesigning. These needs to be made even
174 more generic. I don't like that the actual prime generation is done
175 in PKCS_API_INIT. The primes used in key generation should be sent
176 as argument to the init function. By doing this we would achieve
177 that PKCS could be used as SIM's. The only requirement would be
178 that they are compiled against GMP (well, actually even that would
179 not be a requirement, but the most generic case anyway). The new init
180 would look something like this:
182 #define SILC_PKCS_API_INIT(pkcs) \
183 inline int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
186 Now we wouldn't have to send the SilcRng object since the primes are
187 provided as arguments. To send them as void * they could actually be
188 used as in anyway for real (MP_INT (SilcMPInt) or even something else
189 (the pointer could be kludged to be something else in the module))
190 (Plus, the SilcRng object management in prime generation would be
191 simpler and better what it is now (in silcprimegen.c, that is)).
194 #define SILC_PKCS_API_INIT(pkcs) \
195 int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
197 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
198 void silc_##pkcs##_clear_keys(void *context)
199 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
200 unsigned char *silc_##pkcs##_get_public_key(void *context, \
202 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
203 unsigned char *silc_##pkcs##_get_private_key(void *context, \
205 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
206 SilcUInt32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
208 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
209 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
211 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
212 SilcUInt32 silc_##pkcs##_context_len()
213 #define SILC_PKCS_API_ENCRYPT(pkcs) \
214 int silc_##pkcs##_encrypt(void *context, \
215 unsigned char *src, \
216 SilcUInt32 src_len, \
217 unsigned char *dst, \
219 #define SILC_PKCS_API_DECRYPT(pkcs) \
220 int silc_##pkcs##_decrypt(void *context, \
221 unsigned char *src, \
222 SilcUInt32 src_len, \
223 unsigned char *dst, \
225 #define SILC_PKCS_API_SIGN(pkcs) \
226 int silc_##pkcs##_sign(void *context, \
227 unsigned char *src, \
228 SilcUInt32 src_len, \
229 unsigned char *dst, \
231 #define SILC_PKCS_API_VERIFY(pkcs) \
232 int silc_##pkcs##_verify(void *context, \
233 unsigned char *signature, \
234 SilcUInt32 signature_len, \
235 unsigned char *data, \
240 /****f* silccrypt/SilcPKCSAPI/silc_pkcs_register
244 * bool silc_pkcs_register(const SilcPKCSObject *pkcs);
248 * Registers a new PKCS into the SILC. This function is used
249 * at the initialization of the SILC. All registered PKCSs
250 * should be unregistered with silc_pkcs_unregister. The `pkcs' includes
251 * the name of the PKCS and member functions for the algorithm. Usually
252 * this function is not called directly. Instead, application can call
253 * the silc_pkcs_register_default to register all PKCSs that are
254 * builtin the sources. Returns FALSE on error.
257 bool silc_pkcs_register(const SilcPKCSObject *pkcs);
259 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
260 bool silc_pkcs_register_default(void);
261 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
262 void silc_pkcs_free(SilcPKCS pkcs);
263 int silc_pkcs_is_supported(const unsigned char *name);
264 char *silc_pkcs_get_supported(void);
265 int silc_pkcs_generate_key(SilcPKCS pkcs, SilcUInt32 bits_key_len,
267 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
268 const char *silc_pkcs_get_name(SilcPKCS pkcs);
269 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
270 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len);
271 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
272 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
274 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
275 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
277 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
278 unsigned char *dst, SilcUInt32 *dst_len);
279 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
280 unsigned char *dst, SilcUInt32 *dst_len);
281 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
282 unsigned char *dst, SilcUInt32 *dst_len);
283 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
284 SilcUInt32 signature_len, unsigned char *data,
285 SilcUInt32 data_len);
286 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
287 unsigned char *src, SilcUInt32 src_len,
288 unsigned char *dst, SilcUInt32 *dst_len);
289 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
290 unsigned char *signature,
291 SilcUInt32 signature_len,
293 SilcUInt32 data_len);
294 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
295 char *email, char *org, char *country);
296 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
297 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
298 SilcPublicKey silc_pkcs_public_key_alloc(const char *name,
299 const char *identifier,
300 const unsigned char *pk,
302 void silc_pkcs_public_key_free(SilcPublicKey public_key);
303 SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
304 const unsigned char *prv,
306 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
308 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
310 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
311 char *pkcs, char *identifier,
313 int silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
314 SilcPublicKey *public_key);
315 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
316 SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key);
318 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len);
320 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
321 char *pkcs, SilcUInt32 *len);
322 int silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
323 SilcPrivateKey *private_key);
324 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
325 SilcUInt32 encoding);
326 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
328 SilcUInt32 encoding);
329 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key,
330 unsigned char *passphrase,
331 SilcUInt32 encoding);
332 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data,
334 unsigned char *passphrase,
335 SilcUInt32 encoding);
336 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
337 SilcUInt32 encoding);
338 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
339 SilcUInt32 encoding);
341 #endif /* SILCPKCS_H */