5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
24 /* The default SILC PKCS (Public Key Cryptosystem) object to represent
26 typedef struct SilcPKCSObjectStruct {
28 int (*init)(void *, uint32, SilcRng);
29 void (*clear_keys)(void *);
30 unsigned char *(*get_public_key)(void *, uint32 *);
31 unsigned char *(*get_private_key)(void *, uint32 *);
32 uint32 (*set_public_key)(void *, unsigned char *, uint32);
33 int (*set_private_key)(void *, unsigned char *, uint32);
34 uint32 (*context_len)();
35 int (*encrypt)(void *, unsigned char *, uint32,
36 unsigned char *, uint32 *);
37 int (*decrypt)(void *, unsigned char *, uint32,
38 unsigned char *, uint32 *);
39 int (*sign)(void *, unsigned char *, uint32,
40 unsigned char *, uint32 *);
41 int (*verify)(void *, unsigned char *, uint32,
42 unsigned char *, uint32);
45 /* The main SILC PKCS structure. Use SilcPKCS instead of SilcPKCSStruct.
46 Also remember that SilcPKCS is a pointer. */
47 typedef struct SilcPKCSStruct {
52 uint32 (*get_key_len)(struct SilcPKCSStruct *);
55 /* SILC style public key object. Public key is read from file to this
56 object. Public keys received from network must be in this format as
66 /* SILC style private key object. Private key is read from file to this
74 /* Decoded SILC Public Key identifier. Note that some of the fields
83 } *SilcPublicKeyIdentifier;
85 /* Public and private key file headers */
86 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
87 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
88 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
89 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
91 /* Public and private key file encoding types */
92 #define SILC_PKCS_FILE_BIN 0
93 #define SILC_PKCS_FILE_PEM 1
95 /* Marks for all PKCS in silc. This can be used in silc_pkcs_unregister
96 to unregister all PKCS at once. */
97 #define SILC_ALL_PKCS ((SilcPKCSObject *)1)
99 /* Static list of PKCS for silc_pkcs_register_default(). */
100 extern SilcPKCSObject silc_default_pkcs[];
104 /* Macros used to implement the SILC PKCS API */
106 /* XXX: This needs slight redesigning. These needs to be made even
107 more generic. I don't like that the actual prime generation is done
108 in PKCS_API_INIT. The primes used in key generation should be sent
109 as argument to the init function. By doing this we would achieve
110 that PKCS could be used as SIM's. The only requirement would be
111 that they are compiled against GMP (well, actually even that would
112 not be a requirement, but the most generic case anyway). The new init
113 would look something like this:
115 #define SILC_PKCS_API_INIT(pkcs) \
116 inline int silc_##pkcs##_init(void *context, uint32 keylen, \
119 Now we wouldn't have to send the SilcRng object since the primes are
120 provided as arguments. To send them as void * they could actually be
121 used as in anyway for real (MP_INT (SilcMPInt) or even something else
122 (the pointer could be kludged to be something else in the module))
123 (Plus, the SilcRng object management in prime generation would be
124 simpler and better what it is now (in silcprimegen.c, that is)).
127 #define SILC_PKCS_API_INIT(pkcs) \
128 int silc_##pkcs##_init(void *context, uint32 keylen, \
130 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
131 void silc_##pkcs##_clear_keys(void *context)
132 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
133 unsigned char *silc_##pkcs##_get_public_key(void *context, \
135 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
136 unsigned char *silc_##pkcs##_get_private_key(void *context, \
138 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
139 uint32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
141 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
142 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
144 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
145 uint32 silc_##pkcs##_context_len()
146 #define SILC_PKCS_API_ENCRYPT(pkcs) \
147 int silc_##pkcs##_encrypt(void *context, \
148 unsigned char *src, \
150 unsigned char *dst, \
152 #define SILC_PKCS_API_DECRYPT(pkcs) \
153 int silc_##pkcs##_decrypt(void *context, \
154 unsigned char *src, \
156 unsigned char *dst, \
158 #define SILC_PKCS_API_SIGN(pkcs) \
159 int silc_##pkcs##_sign(void *context, \
160 unsigned char *src, \
162 unsigned char *dst, \
164 #define SILC_PKCS_API_VERIFY(pkcs) \
165 int silc_##pkcs##_verify(void *context, \
166 unsigned char *signature, \
167 uint32 signature_len, \
168 unsigned char *data, \
172 bool silc_pkcs_register(SilcPKCSObject *pkcs);
173 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
174 bool silc_pkcs_register_default(void);
175 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
176 void silc_pkcs_free(SilcPKCS pkcs);
177 int silc_pkcs_is_supported(const unsigned char *name);
178 char *silc_pkcs_get_supported(void);
179 uint32 silc_pkcs_get_key_len(SilcPKCS self);
180 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, uint32 *len);
181 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, uint32 *len);
182 uint32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
183 uint32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
185 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
186 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
188 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
189 unsigned char *dst, uint32 *dst_len);
190 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
191 unsigned char *dst, uint32 *dst_len);
192 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
193 unsigned char *dst, uint32 *dst_len);
194 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
195 uint32 signature_len, unsigned char *data,
197 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
198 unsigned char *src, uint32 src_len,
199 unsigned char *dst, uint32 *dst_len);
200 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
201 unsigned char *signature,
202 uint32 signature_len,
205 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
206 char *email, char *org, char *country);
207 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
208 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
209 SilcPublicKey silc_pkcs_public_key_alloc(char *name, char *identifier,
212 void silc_pkcs_public_key_free(SilcPublicKey public_key);
213 SilcPrivateKey silc_pkcs_private_key_alloc(char *name, unsigned char *prv,
215 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
217 silc_pkcs_public_key_encode(SilcPublicKey public_key, uint32 *len);
219 silc_pkcs_public_key_data_encode(unsigned char *pk, uint32 pk_len,
220 char *pkcs, char *identifier,
222 int silc_pkcs_public_key_decode(unsigned char *data, uint32 data_len,
223 SilcPublicKey *public_key);
225 silc_pkcs_private_key_encode(SilcPrivateKey private_key, uint32 *len);
227 silc_pkcs_private_key_data_encode(unsigned char *prv, uint32 prv_len,
228 char *pkcs, uint32 *len);
229 int silc_pkcs_private_key_decode(unsigned char *data, uint32 data_len,
230 SilcPrivateKey *private_key);
231 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
233 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
236 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key,
237 unsigned char *passphrase,
239 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data,
241 unsigned char *passphrase,
243 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
245 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,