| |
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 /* place-holders */ |
| |
30 struct mwChannel; |
| |
31 struct mwSession; |
| |
32 |
| |
33 |
| |
34 |
| |
35 /** Common cipher types */ |
| |
36 enum mwCipherType { |
| |
37 mwCipher_RC2_40 = 0x0000, |
| |
38 mwCipher_RC2_128 = 0x0001, |
| |
39 }; |
| |
40 |
| |
41 |
| |
42 struct mwCipher; |
| |
43 struct mwCipherInstance; |
| |
44 |
| |
45 |
| |
46 /** Obtain an instance of a given cipher, which can be used for the |
| |
47 processing of a single channel. */ |
| |
48 typedef struct mwCipherInstance *(*mwCipherInstantiator) |
| |
49 (struct mwCipher *cipher, struct mwChannel *chan); |
| |
50 |
| |
51 |
| |
52 /** Generate a descriptor for use in a channel create message to |
| |
53 indicate the availability of this cipher */ |
| |
54 typedef struct mwEncryptItem *(*mwCipherDescriptor) |
| |
55 (struct mwCipherInstance *instance); |
| |
56 |
| |
57 |
| |
58 /** Process (encrypt or decrypt, depending) the given data. The passed |
| |
59 buffer may be freed in processing and be replaced with a freshly |
| |
60 allocated buffer. The post-processed buffer must in turn be freed |
| |
61 after use */ |
| |
62 typedef int (*mwCipherProcessor) |
| |
63 (struct mwCipherInstance *ci, struct mwOpaque *data); |
| |
64 |
| |
65 |
| |
66 /** A cipher. Ciphers are primarily used to provide cipher instances |
| |
67 for bi-directional encryption on channels, but some may be used |
| |
68 for other activities. Expand upon this structure to create a |
| |
69 custom encryption provider. |
| |
70 @see mwCipherInstance */ |
| |
71 struct mwCipher { |
| |
72 |
| |
73 /** service this cipher is providing for |
| |
74 @see mwCipher_getSession */ |
| |
75 struct mwSession *session; |
| |
76 |
| |
77 guint16 type; /**< @see mwCipher_getType */ |
| |
78 const char *(*get_name)(); /**< @see mwCipher_getName */ |
| |
79 const char *(*get_desc)(); /**< @see mwCipher_getDesc */ |
| |
80 |
| |
81 /** Generate a new Cipher Instance for use on a channel |
| |
82 @see mwCipher_newInstance */ |
| |
83 mwCipherInstantiator new_instance; |
| |
84 |
| |
85 /** @see mwCipher_newItem */ |
| |
86 mwCipherDescriptor new_item; |
| |
87 |
| |
88 void (*offered)(struct mwCipherInstance *ci, struct mwEncryptItem *item); |
| |
89 void (*offer)(struct mwCipherInstance *ci); |
| |
90 void (*accepted)(struct mwCipherInstance *ci, struct mwEncryptItem *item); |
| |
91 void (*accept)(struct mwCipherInstance *ci); |
| |
92 |
| |
93 mwCipherProcessor encrypt; /**< @see mwCipherInstance_encrypt */ |
| |
94 mwCipherProcessor decrypt; /**< @see mwCipherInstance_decrypt */ |
| |
95 |
| |
96 /** prepare this cipher for being free'd |
| |
97 @see mwCipher_free */ |
| |
98 void (*clear)(struct mwCipher *c); |
| |
99 |
| |
100 /** clean up a cipher instance before being free'd |
| |
101 @see mwCipherInstance_free */ |
| |
102 void (*clear_instance)(struct mwCipherInstance *ci); |
| |
103 }; |
| |
104 |
| |
105 |
| |
106 /** An instance of a cipher. Expand upon this structure to contain |
| |
107 necessary state data |
| |
108 @see mwCipher */ |
| |
109 struct mwCipherInstance { |
| |
110 |
| |
111 /** the parent cipher. |
| |
112 @see mwCipherInstance_getCipher */ |
| |
113 struct mwCipher *cipher; |
| |
114 |
| |
115 /** the channel this instances processes |
| |
116 @see mwCipherInstance_getChannel */ |
| |
117 struct mwChannel *channel; |
| |
118 }; |
| |
119 |
| |
120 |
| |
121 struct mwCipher *mwCipher_new_RC2_40(struct mwSession *s); |
| |
122 |
| |
123 |
| |
124 #if 0 |
| |
125 /* @todo write this */ |
| |
126 struct mwCipher *mwCipher_new_DH_RC2_128(struct mwSession *s); |
| |
127 #endif |
| |
128 |
| |
129 |
| |
130 struct mwSession *mwCipher_getSession(struct mwCipher *cipher); |
| |
131 |
| |
132 |
| |
133 guint16 mwCipher_getType(struct mwCipher *cipher); |
| |
134 |
| |
135 |
| |
136 const char *mwCipher_getName(struct mwCipher *cipher); |
| |
137 |
| |
138 |
| |
139 const char *mwCipher_getDesc(struct mwCipher *cipher); |
| |
140 |
| |
141 |
| |
142 struct mwCipherInstance *mwCipher_newInstance(struct mwCipher *cipher, |
| |
143 struct mwChannel *channel); |
| |
144 |
| |
145 |
| |
146 /** destroy a cipher */ |
| |
147 void mwCipher_free(struct mwCipher* cipher); |
| |
148 |
| |
149 |
| |
150 /** reference the parent cipher of an instance */ |
| |
151 struct mwCipher *mwCipherInstance_getCipher(struct mwCipherInstance *ci); |
| |
152 |
| |
153 |
| |
154 struct mwEncryptItem *mwCipherInstance_newItem(struct mwCipherInstance *ci); |
| |
155 |
| |
156 |
| |
157 /** Indicates a cipher has been offered to our channel */ |
| |
158 void mwCipherInstance_offered(struct mwCipherInstance *ci, |
| |
159 struct mwEncryptItem *item); |
| |
160 |
| |
161 |
| |
162 /** Offer a cipher */ |
| |
163 void mwCipherInstance_offer(struct mwCipherInstance *ci); |
| |
164 |
| |
165 |
| |
166 /** Indicates an offered cipher has been accepted */ |
| |
167 void mwCipherInstance_accepted(struct mwCipherInstance *ci, |
| |
168 struct mwEncryptItem *item); |
| |
169 |
| |
170 |
| |
171 /** Accept a cipher offered to our channel */ |
| |
172 void mwCipherInstance_accept(struct mwCipherInstance *ci); |
| |
173 |
| |
174 |
| |
175 /** encrypt data */ |
| |
176 int mwCipherInstance_encrypt(struct mwCipherInstance *ci, |
| |
177 struct mwOpaque *data); |
| |
178 |
| |
179 |
| |
180 /** decrypt data */ |
| |
181 int mwCipherInstance_decrypt(struct mwCipherInstance *ci, |
| |
182 struct mwOpaque *data); |
| |
183 |
| |
184 |
| |
185 /** destroy a cipher instance */ |
| |
186 void mwCipherInstance_free(struct mwCipherInstance *ci); |
| |
187 |
| |
188 |
| |
189 /** |
| |
190 @section General Cipher Functions |
| |
191 |
| |
192 This set of functions is a broken sort of RC2 implementation. But it |
| |
193 works with sametime, so we're all happy, right? Primary change to |
| |
194 functionality appears in the mwKeyExpand function. Hypothetically, |
| |
195 using a key expanded here (but breaking it into a 128-char array |
| |
196 rather than 64 ints), one could pass it at that length to openssl |
| |
197 and no further key expansion would occur. |
| |
198 |
| |
199 I'm not certain if replacing this with a wrapper for calls to some |
| |
200 other crypto library is a good idea or not. Proven software versus |
| |
201 added dependencies... |
| |
202 */ |
| |
203 /* @{ */ |
| |
204 |
| |
205 |
| |
206 /** generate some pseudo-random bytes |
| |
207 @param keylen count of bytes to write into key |
| |
208 @param key buffer to write keys into |
| |
209 */ |
| |
210 void rand_key(char *key, gsize keylen); |
| |
211 |
| |
212 |
| |
213 /** Setup an Initialization Vector */ |
| |
214 void mwIV_init(char *iv); |
| |
215 |
| |
216 |
| |
217 /** Expand a variable-length key into a 128-byte key (represented as |
| |
218 an an array of 64 ints) */ |
| |
219 void mwKeyExpand(int *ekey, const char *key, gsize keylen); |
| |
220 |
| |
221 |
| |
222 /** Encrypt data using an already-expanded key */ |
| |
223 void mwEncryptExpanded(const int *ekey, char *iv, |
| |
224 struct mwOpaque *in, |
| |
225 struct mwOpaque *out); |
| |
226 |
| |
227 |
| |
228 /** Encrypt data using an expanded form of the given key */ |
| |
229 void mwEncrypt(const char *key, gsize keylen, char *iv, |
| |
230 struct mwOpaque *in, struct mwOpaque *out); |
| |
231 |
| |
232 |
| |
233 /** Decrypt data using an already expanded key */ |
| |
234 void mwDecryptExpanded(const int *ekey, char *iv, |
| |
235 struct mwOpaque *in, |
| |
236 struct mwOpaque *out); |
| |
237 |
| |
238 |
| |
239 /** Decrypt data using an expanded form of the given key */ |
| |
240 void mwDecrypt(const char *key, gsize keylen, char *iv, |
| |
241 struct mwOpaque *in, struct mwOpaque *out); |
| |
242 |
| |
243 |
| |
244 /* @} */ |
| |
245 |
| |
246 |
| |
247 #endif |
| |
248 |
| |
249 |