Integer type name change.
[silc.git] / lib / silccrypt / silcpkcs.h
1 /*
2
3   silcpkcs.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
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.
18
19 */
20
21 #ifndef SILCPKCS_H
22 #define SILCPKCS_H
23
24 /* The default SILC PKCS (Public Key Cryptosystem) object to represent
25    any PKCS in SILC. */
26 typedef struct SilcPKCSObjectStruct {
27   char *name;
28   int (*init)(void *, SilcUInt32, SilcRng);
29   void (*clear_keys)(void *);
30   unsigned char *(*get_public_key)(void *, SilcUInt32 *);
31   unsigned char *(*get_private_key)(void *, SilcUInt32 *);
32   SilcUInt32 (*set_public_key)(void *, unsigned char *, SilcUInt32);
33   int (*set_private_key)(void *, unsigned char *, SilcUInt32);
34   SilcUInt32 (*context_len)();
35   int (*encrypt)(void *, unsigned char *, SilcUInt32,
36                  unsigned char *, SilcUInt32 *);
37   int (*decrypt)(void *, unsigned char *, SilcUInt32,
38                  unsigned char *, SilcUInt32 *);
39   int (*sign)(void *, unsigned char *, SilcUInt32,
40               unsigned char *, SilcUInt32 *);
41   int (*verify)(void *, unsigned char *, SilcUInt32,
42                 unsigned char *, SilcUInt32);
43 } SilcPKCSObject;
44
45 /* The main SILC PKCS structure. Use SilcPKCS instead of SilcPKCSStruct.
46    Also remember that SilcPKCS is a pointer. */
47 typedef struct SilcPKCSStruct {
48   void *context;
49   SilcPKCSObject *pkcs;
50   SilcUInt32 key_len;
51
52   SilcUInt32 (*get_key_len)(struct SilcPKCSStruct *);
53 } *SilcPKCS;
54
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 
57    well. */
58 typedef struct {
59   SilcUInt32 len;
60   char *name;
61   char *identifier;
62   unsigned char *pk;
63   SilcUInt32 pk_len;
64 } *SilcPublicKey;
65
66 /* SILC style private key object. Private key is read from file to this
67    object. */
68 typedef struct {
69   char *name;
70   unsigned char *prv;
71   SilcUInt32 prv_len;
72 } *SilcPrivateKey;
73
74 /* Decoded SILC Public Key identifier. Note that some of the fields 
75    may be NULL. */
76 typedef struct {
77   char *username;
78   char *host;
79   char *realname;
80   char *email;
81   char *org;
82   char *country;
83 } *SilcPublicKeyIdentifier;
84
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"
90
91 /* Public and private key file encoding types */
92 #define SILC_PKCS_FILE_BIN 0
93 #define SILC_PKCS_FILE_PEM 1
94
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)
98
99 /* Static list of PKCS for silc_pkcs_register_default(). */
100 extern DLLAPI SilcPKCSObject silc_default_pkcs[];
101
102 /* Default PKXS in the SILC protocol */
103 #define SILC_DEFAULT_PKCS "rsa"
104
105 /* Macros */
106
107 /* Macros used to implement the SILC PKCS API */
108
109 /* XXX: This needs slight redesigning. These needs to be made even
110    more generic. I don't like that the actual prime generation is done
111    in PKCS_API_INIT. The primes used in key generation should be sent
112    as argument to the init function. By doing this we would achieve
113    that PKCS could be used as SIM's. The only requirement would be
114    that they are compiled against GMP (well, actually even that would
115    not be a requirement, but the most generic case anyway). The new init 
116    would look something like this:
117
118    #define SILC_PKCS_API_INIT(pkcs) \
119    inline int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
120                                  void *p1, void *p2)
121
122    Now we wouldn't have to send the SilcRng object since the primes are 
123    provided as arguments. To send them as void * they could actually be 
124    used as in anyway for real (MP_INT (SilcMPInt) or even something else 
125    (the pointer could be kludged to be something else in the module))
126    (Plus, the SilcRng object management in prime generation would be
127    simpler and better what it is now (in silcprimegen.c, that is)).
128 */
129
130 #define SILC_PKCS_API_INIT(pkcs) \
131 int silc_##pkcs##_init(void *context, SilcUInt32 keylen, \
132                        SilcRng rng)
133 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
134 void silc_##pkcs##_clear_keys(void *context)
135 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
136 unsigned char *silc_##pkcs##_get_public_key(void *context, \
137                                             SilcUInt32 *ret_len)
138 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
139 unsigned char *silc_##pkcs##_get_private_key(void *context, \
140                                              SilcUInt32 *ret_len)
141 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
142 SilcUInt32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
143                                     SilcUInt32 key_len)
144 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
145 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
146                                   SilcUInt32 key_len)
147 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
148 SilcUInt32 silc_##pkcs##_context_len()
149 #define SILC_PKCS_API_ENCRYPT(pkcs) \
150 int silc_##pkcs##_encrypt(void *context, \
151                           unsigned char *src, \
152                           SilcUInt32 src_len, \
153                           unsigned char *dst, \
154                           SilcUInt32 *dst_len)
155 #define SILC_PKCS_API_DECRYPT(pkcs) \
156 int silc_##pkcs##_decrypt(void *context, \
157                           unsigned char *src, \
158                           SilcUInt32 src_len, \
159                           unsigned char *dst, \
160                           SilcUInt32 *dst_len)
161 #define SILC_PKCS_API_SIGN(pkcs) \
162 int silc_##pkcs##_sign(void *context, \
163                        unsigned char *src, \
164                        SilcUInt32 src_len, \
165                        unsigned char *dst, \
166                        SilcUInt32 *dst_len)
167 #define SILC_PKCS_API_VERIFY(pkcs) \
168 int silc_##pkcs##_verify(void *context, \
169                          unsigned char *signature, \
170                          SilcUInt32 signature_len, \
171                          unsigned char *data, \
172                          SilcUInt32 data_len)
173
174 /* Prototypes */
175 bool silc_pkcs_register(SilcPKCSObject *pkcs);
176 bool silc_pkcs_unregister(SilcPKCSObject *pkcs);
177 bool silc_pkcs_register_default(void);
178 bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
179 void silc_pkcs_free(SilcPKCS pkcs);
180 int silc_pkcs_is_supported(const unsigned char *name);
181 char *silc_pkcs_get_supported(void);
182 SilcUInt32 silc_pkcs_get_key_len(SilcPKCS self);
183 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len);
184 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, SilcUInt32 *len);
185 SilcUInt32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
186 SilcUInt32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
187                                      SilcUInt32 pk_len);
188 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
189 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
190                                    SilcUInt32 prv_len);
191 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
192                       unsigned char *dst, SilcUInt32 *dst_len);
193 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
194                       unsigned char *dst, SilcUInt32 *dst_len);
195 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, SilcUInt32 src_len,
196                    unsigned char *dst, SilcUInt32 *dst_len);
197 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature, 
198                      SilcUInt32 signature_len, unsigned char *data, 
199                      SilcUInt32 data_len);
200 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
201                              unsigned char *src, SilcUInt32 src_len,
202                              unsigned char *dst, SilcUInt32 *dst_len);
203 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash, 
204                                unsigned char *signature, 
205                                SilcUInt32 signature_len, 
206                                unsigned char *data, 
207                                SilcUInt32 data_len);
208 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
209                                   char *email, char *org, char *country);
210 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
211 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
212 SilcPublicKey silc_pkcs_public_key_alloc(char *name, char *identifier,
213                                          unsigned char *pk, 
214                                          SilcUInt32 pk_len);
215 void silc_pkcs_public_key_free(SilcPublicKey public_key);
216 SilcPrivateKey silc_pkcs_private_key_alloc(char *name, unsigned char *prv,
217                                            SilcUInt32 prv_len);
218 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
219 unsigned char *
220 silc_pkcs_public_key_encode(SilcPublicKey public_key, SilcUInt32 *len);
221 unsigned char *
222 silc_pkcs_public_key_data_encode(unsigned char *pk, SilcUInt32 pk_len,
223                                  char *pkcs, char *identifier, 
224                                  SilcUInt32 *len);
225 int silc_pkcs_public_key_decode(unsigned char *data, SilcUInt32 data_len,
226                                 SilcPublicKey *public_key);
227 bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2);
228 unsigned char *
229 silc_pkcs_private_key_encode(SilcPrivateKey private_key, SilcUInt32 *len);
230 unsigned char *
231 silc_pkcs_private_key_data_encode(unsigned char *prv, SilcUInt32 prv_len,
232                                   char *pkcs, SilcUInt32 *len);
233 int silc_pkcs_private_key_decode(unsigned char *data, SilcUInt32 data_len,
234                                  SilcPrivateKey *private_key);
235 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
236                               SilcUInt32 encoding);
237 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
238                                    SilcUInt32 data_len,
239                                    SilcUInt32 encoding);
240 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key, 
241                                unsigned char *passphrase,
242                                SilcUInt32 encoding);
243 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data, 
244                                     SilcUInt32 data_len,
245                                     unsigned char *passphrase,
246                                     SilcUInt32 encoding);
247 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
248                               SilcUInt32 encoding);
249 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
250                                SilcUInt32 encoding);
251
252 #endif