Initial revision
[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 - 2000 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   unsigned char *name;
28   void *data_context;
29
30   int (*init)(void *, unsigned int, SilcRng);
31   void (*clear_keys)(void *);
32   unsigned char *(*get_public_key)(void *, unsigned int *);
33   unsigned char *(*get_private_key)(void *, unsigned int *);
34   int (*set_public_key)(void *, unsigned char *, unsigned int);
35   int (*set_private_key)(void *, unsigned char *, unsigned int);
36   unsigned int (*context_len)();
37   unsigned int (*data_context_len)();
38   int (*set_arg)(void *, void *, int, SilcInt);
39   int (*encrypt)(void *, unsigned char *, unsigned int,
40                  unsigned char *, unsigned int *);
41   int (*decrypt)(void *, unsigned char *, unsigned int,
42                  unsigned char *, unsigned int *);
43   int (*sign)(void *, unsigned char *, unsigned int,
44               unsigned char *, unsigned int *);
45   int (*verify)(void *, unsigned char *, unsigned int,
46                 unsigned char *, unsigned int);
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   unsigned int key_len;
55
56   unsigned int (*get_key_len)(struct SilcPKCSStruct *);
57 } *SilcPKCS;
58
59 /* List of all PKCS in SILC. */
60 extern SilcPKCSObject silc_pkcs_list[];
61
62 /* Public and private key file headers */
63 #define SILC_PKCS_PUBLIC_KEYFILE_BEGIN "-----BEGIN SILC PUBLIC KEY-----\n"
64 #define SILC_PKCS_PUBLIC_KEYFILE_END "\n-----END SILC PUBLIC KEY-----\n"
65 #define SILC_PKCS_PRIVATE_KEYFILE_BEGIN "-----BEGIN SILC PRIVATE KEY-----\n"
66 #define SILC_PKCS_PRIVATE_KEYFILE_END "\n-----END SILC PRIVATE KEY-----\n"
67
68 /* Macros */
69
70 /* Macros used to implement the SILC PKCS API */
71
72 /* XXX: This needs slight redesigning. These needs to be made even
73    more generic. I don't like that the actual prime generation is done
74    in PKCS_API_INIT. The primes used in key generation should be sent
75    as argument to the init function. By doing this we would achieve
76    that PKCS could be used as SIM's. The only requirement would be
77    that they are compiled against GMP (well, actually even that would
78    not be a requirement, but the most generic case anyway). The new init 
79    would look something like this:
80
81    #define SILC_PKCS_API_INIT(pkcs) \
82    inline int silc_##pkcs##_init(void *context, unsigned int keylen, \
83                                  void *p1, void *p2)
84
85    Now we wouldn't have to send the SilcRng object since the primes are 
86    provided as arguments. To send them as void * they could actually be 
87    used as in anyway for real (MP_INT (SilcInt) or even something else 
88    (the pointer could be kludged to be something else in the module))
89    (Plus, the SilcRng object management in prime generation would be
90    simpler and better what it is now (in silcprimegen.c, that is)).
91 */
92
93 #define SILC_PKCS_API_INIT(pkcs) \
94 int silc_##pkcs##_init(void *context, unsigned int keylen, \
95                        SilcRng rng)
96 #define SILC_PKCS_API_CLEAR_KEYS(pkcs) \
97 void silc_##pkcs##_clear_keys(void *context)
98 #define SILC_PKCS_API_GET_PUBLIC_KEY(pkcs) \
99 unsigned char *silc_##pkcs##_get_public_key(void *context, \
100                                             unsigned int *ret_len)
101 #define SILC_PKCS_API_GET_PRIVATE_KEY(pkcs) \
102 unsigned char *silc_##pkcs##_get_private_key(void *context, \
103                                              unsigned int *ret_len)
104 #define SILC_PKCS_API_SET_PUBLIC_KEY(pkcs) \
105 int silc_##pkcs##_set_public_key(void *context, unsigned char *key_data, \
106                                  unsigned int key_len)
107 #define SILC_PKCS_API_SET_PRIVATE_KEY(pkcs) \
108 int silc_##pkcs##_set_private_key(void *context, unsigned char *key_data, \
109                                   unsigned int key_len)
110 #define SILC_PKCS_API_CONTEXT_LEN(pkcs) \
111 unsigned int silc_##pkcs##_context_len()
112 #define SILC_PKCS_API_DATA_CONTEXT_LEN(pkcs) \
113 unsigned int silc_##pkcs##_data_context_len()
114 #define SILC_PKCS_API_SET_ARG(pkcs) \
115 int silc_##pkcs##_set_arg(void *context, \
116                           void *data_context, \
117                           int argnum, \
118                           SilcInt val)
119 #define SILC_PKCS_API_ENCRYPT(pkcs) \
120 int silc_##pkcs##_encrypt(void *context, \
121                           unsigned char *src, \
122                           unsigned int src_len, \
123                           unsigned char *dst, \
124                           unsigned int *dst_len)
125 #define SILC_PKCS_API_DECRYPT(pkcs) \
126 int silc_##pkcs##_decrypt(void *context, \
127                           unsigned char *src, \
128                           unsigned int src_len, \
129                           unsigned char *dst, \
130                           unsigned int *dst_len)
131 #define SILC_PKCS_API_SIGN(pkcs) \
132 int silc_##pkcs##_sign(void *context, \
133                        unsigned char *src, \
134                        unsigned int src_len, \
135                        unsigned char *dst, \
136                        unsigned int *dst_len)
137 #define SILC_PKCS_API_VERIFY(pkcs) \
138 int silc_##pkcs##_verify(void *context, \
139                          unsigned char *signature, \
140                          unsigned int signature_len, \
141                          unsigned char *data, \
142                          unsigned int data_len)
143
144 /* Prototypes */
145 int silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs);
146 void silc_pkcs_free(SilcPKCS pkcs);
147 int silc_pkcs_is_supported(const unsigned char *name);
148 char *silc_pkcs_get_supported();
149 unsigned int silc_pkcs_get_key_len(SilcPKCS self);
150 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, unsigned int *len);
151 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, unsigned int *len);
152 int silc_pkcs_set_public_key(SilcPKCS pkcs, unsigned char *pk, 
153                              unsigned int pk_len);
154 int silc_pkcs_set_private_key(SilcPKCS pkcs, unsigned char *prv, 
155                               unsigned int prv_len);
156 int silc_pkcs_save_public_key(SilcPKCS pkcs, char *filename,
157                               unsigned char *pk, unsigned int pk_len);
158 int silc_pkcs_save_private_key(SilcPKCS pkcs, char *filename,
159                                unsigned char *prv, unsigned int prv_len,
160                                char *passphrase);
161 int silc_pkcs_load_public_key(char *filename, SilcPKCS *ret_pkcs);
162 int silc_pkcs_load_private_key(char *filename, SilcPKCS *ret_pkcs);
163
164 #endif