50c1354a2693594f681f410104aba2ed3321c949
[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   void *data_context;
29
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   uint32 (*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);
47 } SilcPKCSObject;
48
49 /* The main SILC PKCS structure. Use SilcPKCS instead of SilcPKCSStruct.
50    Also remember that SilcPKCS is a pointer. */
51 typedef struct SilcPKCSStruct {
52   void *context;
53   SilcPKCSObject *pkcs;
54   uint32 key_len;
55
56   uint32 (*get_key_len)(struct SilcPKCSStruct *);
57 } *SilcPKCS;
58
59 /* List of all PKCS in SILC. */
60 extern SilcPKCSObject silc_pkcs_list[];
61
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 
64    well. */
65 typedef struct {
66   uint32 len;
67   char *name;
68   char *identifier;
69   unsigned char *pk;
70   uint32 pk_len;
71 } *SilcPublicKey;
72
73 /* SILC style private key object. Private key is read from file to this
74    object. */
75 typedef struct {
76   char *name;
77   unsigned char *prv;
78   uint32 prv_len;
79 } *SilcPrivateKey;
80
81 /* Decoded SILC Public Key identifier. Note that some of the fields 
82    may be NULL. */
83 typedef struct {
84   char *username;
85   char *host;
86   char *realname;
87   char *email;
88   char *org;
89   char *country;
90 } *SilcPublicKeyIdentifier;
91
92 /* Public and private key file headers */
93 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
94 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
95 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
96 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
97
98 /* Public and private key file encoding types */
99 #define SILC_PKCS_FILE_BIN 0
100 #define SILC_PKCS_FILE_PEM 1
101
102 /* Macros */
103
104 /* Macros used to implement the SILC PKCS API */
105
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:
114
115    #define SILC_PKCS_API_INIT(pkcs) \
116    inline int silc_##pkcs##_init(void *context, uint32 keylen, \
117                                  void *p1, void *p2)
118
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 (SilcInt) 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)).
125 */
126
127 #define SILC_PKCS_API_INIT(pkcs) \
128 int silc_##pkcs##_init(void *context, uint32 keylen, \
129                        SilcRng rng)
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, \
134                                             uint32 *ret_len)
135 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
136 unsigned char *silc_##pkcs##_get_private_key(void *context, \
137                                              uint32 *ret_len)
138 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
139 uint32 silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
140                                     uint32 key_len)
141 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
142 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
143                                   uint32 key_len)
144 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
145 uint32 silc_##pkcs##_context_len()
146 #define SILC_PKCS_API_DATA_CONTEXT_LEN(pkcs) \
147 uint32 silc_##pkcs##_data_context_len()
148 #define SILC_PKCS_API_SET_ARG(pkcs) \
149 int silc_##pkcs##_set_arg(void *context, \
150                           void *data_context, \
151                           int argnum, \
152                           SilcInt val)
153 #define SILC_PKCS_API_ENCRYPT(pkcs) \
154 int silc_##pkcs##_encrypt(void *context, \
155                           unsigned char *src, \
156                           uint32 src_len, \
157                           unsigned char *dst, \
158                           uint32 *dst_len)
159 #define SILC_PKCS_API_DECRYPT(pkcs) \
160 int silc_##pkcs##_decrypt(void *context, \
161                           unsigned char *src, \
162                           uint32 src_len, \
163                           unsigned char *dst, \
164                           uint32 *dst_len)
165 #define SILC_PKCS_API_SIGN(pkcs) \
166 int silc_##pkcs##_sign(void *context, \
167                        unsigned char *src, \
168                        uint32 src_len, \
169                        unsigned char *dst, \
170                        uint32 *dst_len)
171 #define SILC_PKCS_API_VERIFY(pkcs) \
172 int silc_##pkcs##_verify(void *context, \
173                          unsigned char *signature, \
174                          uint32 signature_len, \
175                          unsigned char *data, \
176                          uint32 data_len)
177
178 /* Prototypes */
179 int silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
180 void silc_pkcs_free(SilcPKCS pkcs);
181 int silc_pkcs_is_supported(const unsigned char *name);
182 char *silc_pkcs_get_supported();
183 uint32 silc_pkcs_get_key_len(SilcPKCS self);
184 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, uint32 *len);
185 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, uint32 *len);
186 uint32 silc_pkcs_public_key_set(SilcPKCS pkcs, SilcPublicKey public_key);
187 uint32 silc_pkcs_public_key_data_set(SilcPKCS pkcs, unsigned char *pk,
188                                      uint32 pk_len);
189 int silc_pkcs_private_key_set(SilcPKCS pkcs, SilcPrivateKey private_key);
190 int silc_pkcs_private_key_data_set(SilcPKCS pkcs, unsigned char *prv,
191                                    uint32 prv_len);
192 int silc_pkcs_encrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
193                       unsigned char *dst, uint32 *dst_len);
194 int silc_pkcs_decrypt(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
195                       unsigned char *dst, uint32 *dst_len);
196 int silc_pkcs_sign(SilcPKCS pkcs, unsigned char *src, uint32 src_len,
197                    unsigned char *dst, uint32 *dst_len);
198 int silc_pkcs_verify(SilcPKCS pkcs, unsigned char *signature, 
199                      uint32 signature_len, unsigned char *data, 
200                      uint32 data_len);
201 int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
202                              unsigned char *src, uint32 src_len,
203                              unsigned char *dst, uint32 *dst_len);
204 int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash, 
205                                unsigned char *signature, 
206                                uint32 signature_len, 
207                                unsigned char *data, 
208                                uint32 data_len);
209 char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
210                                   char *email, char *org, char *country);
211 SilcPublicKeyIdentifier silc_pkcs_decode_identifier(char *identifier);
212 void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier);
213 SilcPublicKey silc_pkcs_public_key_alloc(char *name, char *identifier,
214                                          unsigned char *pk, 
215                                          uint32 pk_len);
216 void silc_pkcs_public_key_free(SilcPublicKey public_key);
217 SilcPrivateKey silc_pkcs_private_key_alloc(char *name, unsigned char *prv,
218                                            uint32 prv_len);
219 void silc_pkcs_private_key_free(SilcPrivateKey private_key);
220 unsigned char *
221 silc_pkcs_public_key_encode(SilcPublicKey public_key, uint32 *len);
222 unsigned char *
223 silc_pkcs_public_key_data_encode(unsigned char *pk, uint32 pk_len,
224                                  char *pkcs, char *identifier, 
225                                  uint32 *len);
226 int silc_pkcs_public_key_decode(unsigned char *data, uint32 data_len,
227                                 SilcPublicKey *public_key);
228 unsigned char *
229 silc_pkcs_private_key_encode(SilcPrivateKey private_key, uint32 *len);
230 unsigned char *
231 silc_pkcs_private_key_data_encode(unsigned char *prv, uint32 prv_len,
232                                   char *pkcs, uint32 *len);
233 int silc_pkcs_private_key_decode(unsigned char *data, uint32 data_len,
234                                  SilcPrivateKey *private_key);
235 int silc_pkcs_save_public_key(char *filename, SilcPublicKey public_key,
236                               uint32 encoding);
237 int silc_pkcs_save_public_key_data(char *filename, unsigned char *data,
238                                    uint32 data_len,
239                                    uint32 encoding);
240 int silc_pkcs_save_private_key(char *filename, SilcPrivateKey private_key, 
241                                unsigned char *passphrase,
242                                uint32 encoding);
243 int silc_pkcs_save_private_key_data(char *filename, unsigned char *data, 
244                                     uint32 data_len,
245                                     unsigned char *passphrase,
246                                     uint32 encoding);
247 int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
248                               uint32 encoding);
249 int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
250                                uint32 encoding);
251
252 #endif