1 
2 /*
3   Meanwhile - Unofficial Lotus Sametime Community Client Library
4   Copyright (C) 2004  Christopher (siege) O'Brien
5 
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public
8   License as published by the Free Software Foundation; either
9   version 2 of the License, or (at your option) any later version.
10 
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15 
16   You should have received a copy of the GNU Library General Public
17   License along with this library; if not, write to the Free
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20 
21 #ifndef _MW_CIPHER_H
22 #define _MW_CIPHER_H
23 
24 
25 #include <glib.h>
26 #include "mw_common.h"
27 
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 /* place-holders */
35 struct mwChannel;
36 struct mwSession;
37 
38 
39 /** @enum mwCipherType
40     Common cipher types */
41 enum mwCipherType {
42   mwCipher_RC2_40   = 0x0000,
43   mwCipher_RC2_128  = 0x0001,
44 };
45 
46 
47 struct mwCipher;
48 struct mwCipherInstance;
49 
50 
51 /** Obtain an instance of a given cipher, which can be used for the
52     processing of a single channel. */
53 typedef struct mwCipherInstance *(*mwCipherInstantiator)
54      (struct mwCipher *cipher, struct mwChannel *chan);
55 
56 
57 /** Process (encrypt or decrypt, depending) the given data. The passed
58     buffer may be freed in processing and be replaced with a freshly
59     allocated buffer. The post-processed buffer must in turn be freed
60     after use */
61 typedef int (*mwCipherProcessor)
62      (struct mwCipherInstance *ci, struct mwOpaque *data);
63 
64 
65 /** A cipher. Ciphers are primarily used to provide cipher instances
66     for bi-directional encryption on channels, but some may be used
67     for other activities. Expand upon this structure to create a
68     custom encryption provider.
69     @see mwCipherInstance */
70 struct mwCipher {
71 
72   /** service this cipher is providing for
73       @see mwCipher_getSession */
74   struct mwSession *session;
75 
76   guint16 type;               /**< @see mwCipher_getType */
77   const char *(*get_name)();  /**< @see mwCipher_getName */
78   const char *(*get_desc)();  /**< @see mwCipher_getDesc */
79 
80   /** Generate a new Cipher Instance for use on a channel
81       @see mwCipher_newInstance */
82   mwCipherInstantiator new_instance;
83 
84   void (*offered)(struct mwCipherInstance *ci, struct mwEncryptItem *item);
85   struct mwEncryptItem *(*offer)(struct mwCipherInstance *ci);
86   void (*accepted)(struct mwCipherInstance *ci, struct mwEncryptItem *item);
87   struct mwEncryptItem *(*accept)(struct mwCipherInstance *ci);
88 
89   mwCipherProcessor encrypt; /**< @see mwCipherInstance_encrypt */
90   mwCipherProcessor decrypt; /**< @see mwCipherInstance_decrypt */
91 
92   /** prepare this cipher for being free'd
93       @see mwCipher_free */
94   void (*clear)(struct mwCipher *c);
95 
96   /** clean up a cipher instance before being free'd
97       @see mwCipherInstance_free */
98   void (*clear_instance)(struct mwCipherInstance *ci);
99 };
100 
101 
102 /** An instance of a cipher. Expand upon this structure to contain
103     necessary state data
104     @see mwCipher */
105 struct mwCipherInstance {
106 
107   /** the parent cipher.
108       @see mwCipherInstance_getCipher */
109   struct mwCipher *cipher;
110 
111   /** the channel this instances processes
112       @see mwCipherInstance_getChannel */
113   struct mwChannel *channel;
114 };
115 
116 
117 struct mwCipher *mwCipher_new_RC2_40(struct mwSession *s);
118 
119 
120 struct mwCipher *mwCipher_new_RC2_128(struct mwSession *s);
121 
122 
123 struct mwSession *mwCipher_getSession(struct mwCipher *cipher);
124 
125 
126 guint16 mwCipher_getType(struct mwCipher *cipher);
127 
128 
129 const char *mwCipher_getName(struct mwCipher *cipher);
130 
131 
132 const char *mwCipher_getDesc(struct mwCipher *cipher);
133 
134 
135 struct mwCipherInstance *mwCipher_newInstance(struct mwCipher *cipher,
136 					      struct mwChannel *channel);
137 
138 
139 /** destroy a cipher */
140 void mwCipher_free(struct mwCipher* cipher);
141 
142 
143 /** reference the parent cipher of an instance */
144 struct mwCipher *mwCipherInstance_getCipher(struct mwCipherInstance *ci);
145 
146 
147 /** reference the channel a cipher instance is attached to */
148 struct mwChannel *mwCipherInstance_getChannel(struct mwCipherInstance *ci);
149 
150 
151 /** Indicates a cipher has been offered to our channel */
152 void mwCipherInstance_offered(struct mwCipherInstance *ci,
153 			      struct mwEncryptItem *item);
154 
155 
156 /** Offer a cipher */
157 struct mwEncryptItem *
158 mwCipherInstance_offer(struct mwCipherInstance *ci);
159 
160 
161 /** Indicates an offered cipher has been accepted */
162 void mwCipherInstance_accepted(struct mwCipherInstance *ci,
163 			       struct mwEncryptItem *item);
164 
165 
166 /** Accept a cipher offered to our channel */
167 struct mwEncryptItem *
168 mwCipherInstance_accept(struct mwCipherInstance *ci);
169 
170 
171 /** encrypt data */
172 int mwCipherInstance_encrypt(struct mwCipherInstance *ci,
173 			     struct mwOpaque *data);
174 
175 
176 /** decrypt data */
177 int mwCipherInstance_decrypt(struct mwCipherInstance *ci,
178 			     struct mwOpaque *data);
179 
180 
181 /** destroy a cipher instance */
182 void mwCipherInstance_free(struct mwCipherInstance *ci);
183 
184 
185 /**
186   @section General Cipher Functions
187 
188   These functions are reused where encryption is necessary outside of
189   a channel (eg. session authentication)
190 */
191 /* @{ */
192 
193 
194 /** generate some pseudo-random bytes
195     @param keylen  count of bytes to write into key
196     @param key     buffer to write keys into
197 */
198 void mwKeyRandom(guchar *key, gsize keylen);
199 
200 
201 /** Setup an Initialization Vector. IV must be at least 8 bytes */
202 void mwIV_init(guchar *iv);
203 
204 
205 /** Expand a variable-length key into a 128-byte key (represented as
206     an an array of 64 ints) */
207 void mwKeyExpand(int *ekey, const guchar *key, gsize keylen);
208 
209 
210 /** Encrypt data using an already-expanded key */
211 void mwEncryptExpanded(const int *ekey, guchar *iv,
212 		       struct mwOpaque *in,
213 		       struct mwOpaque *out);
214 
215 
216 /** Encrypt data using an expanded form of the given key */
217 void mwEncrypt(const guchar *key, gsize keylen, guchar *iv,
218 	       struct mwOpaque *in, struct mwOpaque *out);
219 
220 
221 /** Decrypt data using an already expanded key */
222 void mwDecryptExpanded(const int *ekey, guchar *iv,
223 		       struct mwOpaque *in,
224 		       struct mwOpaque *out);
225 
226 
227 /** Decrypt data using an expanded form of the given key */
228 void mwDecrypt(const guchar *key, gsize keylen, guchar *iv,
229 	       struct mwOpaque *in, struct mwOpaque *out);
230 
231 
232 /* @} */
233 
234 
235 /**
236   @section Diffie-Hellman Functions
237 
238   These functions are reused where DH Key negotiation is necessary
239   outside of a channel (eg. session authentication). These are
240   wrapping a full multiple-precision integer math library, but most of
241   the functionality there-of is not exposed. Currently, the math is
242   provided by a copy of the public domain libmpi.
243 
244   for more information on the used MPI Library, visit
245   http://www.cs.dartmouth.edu/~sting/mpi/
246 */
247 /* @{ */
248 
249 
250 /** @struct mwMpi */
251 struct mwMpi;
252 
253 
254 /** prepare a new mpi value */
255 struct mwMpi *mwMpi_new();
256 
257 
258 /** destroy an mpi value */
259 void mwMpi_free(struct mwMpi *i);
260 
261 
262 /** Import a value from an opaque */
263 void mwMpi_import(struct mwMpi *i, struct mwOpaque *o);
264 
265 
266 /** Export a value into an opaque */
267 void mwMpi_export(struct mwMpi *i, struct mwOpaque *o);
268 
269 
270 /** set a big integer to the Sametime Prime value */
271 void mwMpi_setDHPrime(struct mwMpi *i);
272 
273 
274 /** set a big integer to the Sametime Base value */
275 void mwMpi_setDHBase(struct mwMpi *i);
276 
277 
278 /** sets private to a randomly generated value, and calculates public
279     using the Sametime Prime and Base */
280 void mwMpi_randDHKeypair(struct mwMpi *private_key, struct mwMpi *public_key);
281 
282 
283 /** sets the shared key value based on the remote and private keys,
284     using the Sametime Prime and Base */
285 void mwMpi_calculateDHShared(struct mwMpi *shared_key, struct mwMpi *remote_key,
286 			     struct mwMpi *private_key);
287 
288 
289 /* @} */
290 
291 
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 
297 #endif /* _MW_CIPHER_H */
298