3
Meanwhile - Unofficial Lotus Sametime Community Client Library
4
Copyright (C) 2004 Christopher (siege) O'Brien
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.
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.
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
26
#include "mw_common.h"
35
/** Common cipher types */
37
mwCipher_RC2_40 = 0x0000,
38
mwCipher_RC2_128 = 0x0001,
43
struct mwCipherInstance;
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);
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);
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
62
typedef int (*mwCipherProcessor)
63
(struct mwCipherInstance *ci, struct mwOpaque *data);
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 */
73
/** service this cipher is providing for
74
@see mwCipher_getSession */
75
struct mwSession *session;
77
guint16 type; /**< @see mwCipher_getType */
78
const char *(*get_name)(); /**< @see mwCipher_getName */
79
const char *(*get_desc)(); /**< @see mwCipher_getDesc */
81
/** Generate a new Cipher Instance for use on a channel
82
@see mwCipher_newInstance */
83
mwCipherInstantiator new_instance;
85
/** @see mwCipher_newItem */
86
mwCipherDescriptor new_item;
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);
93
mwCipherProcessor encrypt; /**< @see mwCipherInstance_encrypt */
94
mwCipherProcessor decrypt; /**< @see mwCipherInstance_decrypt */
96
/** prepare this cipher for being free'd
98
void (*clear)(struct mwCipher *c);
100
/** clean up a cipher instance before being free'd
101
@see mwCipherInstance_free */
102
void (*clear_instance)(struct mwCipherInstance *ci);
106
/** An instance of a cipher. Expand upon this structure to contain
109
struct mwCipherInstance {
111
/** the parent cipher.
112
@see mwCipherInstance_getCipher */
113
struct mwCipher *cipher;
115
/** the channel this instances processes
116
@see mwCipherInstance_getChannel */
117
struct mwChannel *channel;
121
struct mwCipher *mwCipher_new_RC2_40(struct mwSession *s);
125
/* @todo write this */
126
struct mwCipher *mwCipher_new_DH_RC2_128(struct mwSession *s);
130
struct mwSession *mwCipher_getSession(struct mwCipher *cipher);
133
guint16 mwCipher_getType(struct mwCipher *cipher);
136
const char *mwCipher_getName(struct mwCipher *cipher);
139
const char *mwCipher_getDesc(struct mwCipher *cipher);
142
struct mwCipherInstance *mwCipher_newInstance(struct mwCipher *cipher,
143
struct mwChannel *channel);
146
/** destroy a cipher */
147
void mwCipher_free(struct mwCipher* cipher);
150
/** reference the parent cipher of an instance */
151
struct mwCipher *mwCipherInstance_getCipher(struct mwCipherInstance *ci);
154
struct mwEncryptItem *mwCipherInstance_newItem(struct mwCipherInstance *ci);
157
/** Indicates a cipher has been offered to our channel */
158
void mwCipherInstance_offered(struct mwCipherInstance *ci,
159
struct mwEncryptItem *item);
162
/** Offer a cipher */
163
void mwCipherInstance_offer(struct mwCipherInstance *ci);
166
/** Indicates an offered cipher has been accepted */
167
void mwCipherInstance_accepted(struct mwCipherInstance *ci,
168
struct mwEncryptItem *item);
171
/** Accept a cipher offered to our channel */
172
void mwCipherInstance_accept(struct mwCipherInstance *ci);
176
int mwCipherInstance_encrypt(struct mwCipherInstance *ci,
177
struct mwOpaque *data);
181
int mwCipherInstance_decrypt(struct mwCipherInstance *ci,
182
struct mwOpaque *data);
185
/** destroy a cipher instance */
186
void mwCipherInstance_free(struct mwCipherInstance *ci);
190
@section General Cipher Functions
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.
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...
206
/** generate some pseudo-random bytes
207
@param keylen count of bytes to write into key
208
@param key buffer to write keys into
210
void rand_key(char *key, gsize keylen);
213
/** Setup an Initialization Vector */
214
void mwIV_init(char *iv);
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);
222
/** Encrypt data using an already-expanded key */
223
void mwEncryptExpanded(const int *ekey, char *iv,
225
struct mwOpaque *out);
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);
233
/** Decrypt data using an already expanded key */
234
void mwDecryptExpanded(const int *ekey, char *iv,
236
struct mwOpaque *out);
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);