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 {
30 int (*init)(void *, uint32, SilcRng);
31 void (*clear_keys)(void *);
32 unsigned char *(*get_public_key)(void *, uint32 *);
33 unsigned char *(*get_private_key)(void *, uint32 *);
34 int (*set_public_key)(void *, unsigned char *, uint32);
35 int (*set_private_key)(void *, unsigned char *, uint32);
36 uint32 (*context_len)();
37 uint32 (*data_context_len)();
38 int (*set_arg)(void *, void *, int, SilcInt);
39 int (*encrypt)(void *, unsigned char *, uint32,
40 unsigned char *, uint32 *);
41 int (*decrypt)(void *, unsigned char *, uint32,
42 unsigned char *, uint32 *);
43 int (*sign)(void *, unsigned char *, uint32,
44 unsigned char *, uint32 *);
45 int (*verify)(void *, unsigned char *, uint32,
46 unsigned char *, uint32);
49 /* The main SILC PKCS structure. Use SilcPKCS instead of SilcPKCSStruct.
50 Also remember that SilcPKCS is a pointer. */
51 typedef struct SilcPKCSStruct {
56 uint32 (*get_key_len)(struct SilcPKCSStruct *);
59 /* List of all PKCS in SILC. */
60 extern SilcPKCSObject silc_pkcs_list[];
62 /* SILC style public key object. Public key is read from file to this
63 object. Public keys received from network must be in this format as
73 /* SILC style private key object. Private key is read from file to this
81 /* Public and private key file headers */
82 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
83 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
84 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
85 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
87 /* Public and private key file encoding types */
88 #define SILC_PKCS_FILE_BIN 0
89 #define SILC_PKCS_FILE_PEM 1
93 /* Macros used to implement the SILC PKCS API */
95 /* XXX: This needs slight redesigning. These needs to be made even
96 more generic. I don't like that the actual prime generation is done
97 in PKCS_API_INIT. The primes used in key generation should be sent
98 as argument to the init function. By doing this we would achieve
99 that PKCS could be used as SIM's. The only requirement would be
100 that they are compiled against GMP (well, actually even that would
101 not be a requirement, but the most generic case anyway). The new init
102 would look something like this:
104 #define SILC_PKCS_API_INIT(pkcs) \
105 inline int silc_##pkcs##_init(void *context, uint32 keylen, \
108 Now we wouldn't have to send the SilcRng object since the primes are
109 provided as arguments. To send them as void * they could actually be
110 used as in anyway for real (MP_INT (SilcInt) or even something else
111 (the pointer could be kludged to be something else in the module))
112 (Plus, the SilcRng object management in prime generation would be
113 simpler and better what it is now (in silcprimegen.c, that is)).
116 #define SILC_PKCS_API_INIT(pkcs) \
117 int silc_##pkcs##_init(void *context, uint32 keylen, \
119 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
120 void silc_##pkcs##_clear_keys(void *context)
121 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
122 unsigned char *silc_##pkcs##_get_public_key(void *context, \
124 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
125 unsigned char *silc_##pkcs##_get_private_key(void *context, \
127 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
128 int silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
130 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
131 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
133 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
134 uint32 silc_##pkcs##_context_len()
135 #define SILC_PKCS_API_DATA_CONTEXT_LEN(pkcs) \
136 uint32 silc_##pkcs##_data_context_len()
137 #define SILC_PKCS_API_SET_ARG(pkcs) \
138 int silc_##pkcs##_set_arg(void *context, \
139 void *data_context, \
142 #define SILC_PKCS_API_ENCRYPT(pkcs) \
143 int silc_##pkcs##_encrypt(void *context, \
144 unsigned char *src, \
146 unsigned char *dst, \
148 #define SILC_PKCS_API_DECRYPT(pkcs) \
149 int silc_##pkcs##_decrypt(void *context, \
150 unsigned char *src, \
152 unsigned char *dst, \
154 #define SILC_PKCS_API_SIGN(pkcs) \
155 int silc_##pkcs##_sign(void *context, \
156 unsigned char *src, \
158 unsigned char *dst, \
160 #define SILC_PKCS_API_VERIFY(pkcs) \
161 int silc_##pkcs##_verify(void *context, \
162 unsigned char *signature, \
163 uint32 signature_len, \
164 unsigned char *data, \
168 int silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
169 void silc_pkcs_free(SilcPKCS pkcs);
170 int silc_pkcs_is_supported(const unsigned char *name);
171 char *silc_pkcs_get_supported();
172 uint32 silc_pkcs_get_key_len(SilcPKCS self);
173 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, uint32 *len);
174 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, uint32 *len);
175 int silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
176 int silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
178 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
179 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
181 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
182 unsigned char *dst, uint32 *dst_len);
183 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
184 unsigned char *dst, uint32 *dst_len);
185 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
186 unsigned char *dst, uint32 *dst_len);
187 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature,
188 uint32 signature_len, unsigned char *data,
190 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
191 unsigned char *src, uint32 src_len,
192 unsigned char *dst, uint32 *dst_len);
193 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
194 unsigned char *signature,
195 uint32 signature_len,
198 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
199 char *email, char *org, char *country);
200 SilcPublicKey silc_pkcs_public_key_alloc(char *name, char *identifier,
203 void silc_pkcs_public_key_free(SilcPublicKey public_key);
204 SilcPrivateKey silc_pkcs_private_key_alloc(char *name, unsigned char *prv,
206 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
208 silc_pkcs_public_key_encode(SilcPublicKey public_key, uint32 *len);
210 silc_pkcs_public_key_data_encode(unsigned char *pk, uint32 pk_len,
211 char *pkcs, char *identifier,
213 int silc_pkcs_public_key_decode(unsigned char *data, uint32 data_len,
214 SilcPublicKey *public_key);
216 silc_pkcs_private_key_encode(SilcPrivateKey private_key, uint32 *len);
218 silc_pkcs_private_key_data_encode(unsigned char *prv, uint32 prv_len,
219 char *pkcs, uint32 *len);
220 int silc_pkcs_private_key_decode(unsigned char *data, uint32 data_len,
221 SilcPrivateKey *private_key);
222 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
224 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
227 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key,
228 unsigned char *passphrase,
230 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data,
232 unsigned char *passphrase,
234 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
236 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,