summaryrefslogtreecommitdiff
path: root/libpurple/cipher.h
blob: 3e62ec81d54f9931c616355ab865e2ff83e64306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */
/**
 * SECTION:cipher
 * @section_id: libpurple-cipher
 * @short_description: <filename>cipher.h</filename>
 * @title: Cipher and Hash API
 */

#ifndef PURPLE_CIPHER_H
#define PURPLE_CIPHER_H

#include <glib.h>
#include <glib-object.h>
#include <string.h>

#define PURPLE_TYPE_CIPHER				(purple_cipher_get_type())
#define PURPLE_CIPHER(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_CIPHER, PurpleCipher))
#define PURPLE_CIPHER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_CIPHER, PurpleCipherClass))
#define PURPLE_IS_CIPHER(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_CIPHER))
#define PURPLE_IS_CIPHER_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_CIPHER))
#define PURPLE_CIPHER_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_CIPHER, PurpleCipherClass))

typedef struct _PurpleCipher       PurpleCipher;
typedef struct _PurpleCipherClass  PurpleCipherClass;

#define PURPLE_TYPE_HASH				(purple_hash_get_type())
#define PURPLE_HASH(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_HASH, PurpleHash))
#define PURPLE_HASH_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_HASH, PurpleHashClass))
#define PURPLE_IS_HASH(obj)				(G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_HASH))
#define PURPLE_IS_HASH_CLASS(klass)		(G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_HASH))
#define PURPLE_HASH_GET_CLASS(obj)		(G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_HASH, PurpleHashClass))

typedef struct _PurpleHash       PurpleHash;
typedef struct _PurpleHashClass  PurpleHashClass;

/**
 * PurpleCipherBatchMode:
 * @PURPLE_CIPHER_BATCH_MODE_ECB: Electronic Codebook Mode
 * @PURPLE_CIPHER_BATCH_MODE_CBC: Cipher Block Chaining Mode
 *
 * Modes for batch encrypters
 */
typedef enum {
	PURPLE_CIPHER_BATCH_MODE_ECB,
	PURPLE_CIPHER_BATCH_MODE_CBC
} PurpleCipherBatchMode;

/**
 * PurpleCipher:
 *
 * Purple Cipher is an opaque data structure and should not be used directly.
 */
struct _PurpleCipher {
	GObject gparent;
};

/**
 * PurpleCipherClass:
 *
 * The base class for all #PurpleCipher's.
 */
struct _PurpleCipherClass {
	GObjectClass parent_class;

	/** The reset function */
	void (*reset)(PurpleCipher *cipher);

	/** The reset state function */
	void (*reset_state)(PurpleCipher *cipher);

	/** The set initialization vector function */
	void (*set_iv)(PurpleCipher *cipher, guchar *iv, size_t len);

	/** The append data function */
	void (*append)(PurpleCipher *cipher, const guchar *data, size_t len);

	/** The digest function */
	gboolean (*digest)(PurpleCipher *cipher, guchar digest[], size_t len);

	/** The get digest size function */
	size_t (*get_digest_size)(PurpleCipher *cipher);

	/** The encrypt function */
	ssize_t (*encrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);

	/** The decrypt function */
	ssize_t (*decrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);

	/** The set salt function */
	void (*set_salt)(PurpleCipher *cipher, const guchar *salt, size_t len);

	/** The set key function */
	void (*set_key)(PurpleCipher *cipher, const guchar *key, size_t len);

	/** The get key size function */
	size_t (*get_key_size)(PurpleCipher *cipher);

	/** The set batch mode function */
	void (*set_batch_mode)(PurpleCipher *cipher, PurpleCipherBatchMode mode);

	/** The get batch mode function */
	PurpleCipherBatchMode (*get_batch_mode)(PurpleCipher *cipher);

	/** The get block size function */
	size_t (*get_block_size)(PurpleCipher *cipher);

	/*< private >*/
	void (*_purple_reserved1)(void);
	void (*_purple_reserved2)(void);
	void (*_purple_reserved3)(void);
	void (*_purple_reserved4)(void);
};

/**
 * PurpleHash:
 *
 * Purple Hash is an opaque data structure and should not be used directly.
 */
struct _PurpleHash {
	GObject gparent;
};

/**
 * PurpleHashClass:
 *
 * The base class for all #PurpleHash's.
 */
struct _PurpleHashClass {
	GObjectClass parent_class;

	/** The reset function */
	void (*reset)(PurpleHash *hash);

	/** The reset state function */
	void (*reset_state)(PurpleHash *hash);

	/** The append data function */
	void (*append)(PurpleHash *hash, const guchar *data, size_t len);

	/** The digest function */
	gboolean (*digest)(PurpleHash *hash, guchar digest[], size_t len);

	/** The get digest size function */
	size_t (*get_digest_size)(PurpleHash *hash);

	/** The get block size function */
	size_t (*get_block_size)(PurpleHash *hash);

	/*< private >*/
	void (*_purple_reserved1)(void);
	void (*_purple_reserved2)(void);
	void (*_purple_reserved3)(void);
	void (*_purple_reserved4)(void);
};

G_BEGIN_DECLS

/*****************************************************************************/
/* PurpleCipher API                                                          */
/*****************************************************************************/
/*@{*/

/**
 * purple_cipher_get_type:
 *
 * Returns: The #GType for the Cipher object.
 */
GType purple_cipher_get_type(void);

/**
 * purple_cipher_reset:
 * @cipher:  The cipher
 *
 * Resets a cipher to it's default value
 * Note: If you have set an IV you will have to set it after resetting
 */
void purple_cipher_reset(PurpleCipher *cipher);

/**
 * purple_cipher_reset_state:
 * @cipher:  The cipher
 *
 * Resets a cipher state to it's default value, but doesn't touch stateless
 * configuration.
 *
 * That means, IV and digest will be wiped out, but keys, ops or salt
 * will remain untouched.
 */
void purple_cipher_reset_state(PurpleCipher *cipher);

/**
 * purple_cipher_set_iv:
 * @cipher:  The cipher
 * @iv:      The initialization vector to set
 * @len:     The len of the IV
 *
 * Sets the initialization vector for a cipher
 * Note: This should only be called right after a cipher is created or reset
 */
void purple_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len);

/**
 * purple_cipher_append:
 * @cipher:  The cipher
 * @data:    The data to append
 * @len:     The length of the data
 *
 * Appends data to the cipher context
 */
void purple_cipher_append(PurpleCipher *cipher, const guchar *data, size_t len);

/**
 * purple_cipher_digest:
 * @cipher:  The cipher
 * @digest:  The return buffer for the digest
 * @len:     The length of the buffer
 *
 * Digests a cipher context
 */
gboolean purple_cipher_digest(PurpleCipher *cipher, guchar digest[], size_t len);

/**
 * purple_cipher_digest_to_str:
 * @cipher:   The cipher
 * @digest_s: The return buffer for the string digest
 * @len:      The length of the buffer
 *
 * Converts a guchar digest into a hex string
 */
gboolean purple_cipher_digest_to_str(PurpleCipher *cipher, gchar digest_s[], size_t len);

/**
 * purple_cipher_get_digest_size:
 * @cipher: The cipher whose digest size to get
 *
 * Gets the digest size of a cipher
 *
 * Returns: The digest size of the cipher
 */
size_t purple_cipher_get_digest_size(PurpleCipher *cipher);

/**
 * purple_cipher_encrypt:
 * @cipher:   The cipher
 * @input:    The data to encrypt
 * @in_len:   The length of the data
 * @output:   The output buffer
 * @out_size: The size of the output buffer
 *
 * Encrypts data using the cipher
 *
 * Returns: A length of data that was outputed or -1, if failed
 */
ssize_t purple_cipher_encrypt(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);

/**
 * purple_cipher_decrypt:
 * @cipher:   The cipher
 * @input:    The data to encrypt
 * @in_len:   The length of the returned value
 * @output:   The output buffer
 * @out_size: The size of the output buffer
 *
 * Decrypts data using the cipher
 *
 * Returns: A length of data that was outputed or -1, if failed
 */
ssize_t purple_cipher_decrypt(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);

/**
 * purple_cipher_set_salt:
 * @cipher:  The cipher whose salt to set
 * @salt:    The salt
 * @len:     The length of the salt
 *
 * Sets the salt on a cipher
 */
void purple_cipher_set_salt(PurpleCipher *cipher, const guchar *salt, size_t len);

/**
 * purple_cipher_set_key:
 * @cipher:  The cipher whose key to set
 * @key:     The key
 * @len:     The size of the key
 *
 * Sets the key on a cipher
 */
void purple_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len);

/**
 * purple_cipher_get_key_size:
 * @cipher: The cipher whose key size to get
 *
 * Gets the size of the key if the cipher supports it
 *
 * Returns: The size of the key
 */
size_t purple_cipher_get_key_size(PurpleCipher *cipher);

/**
 * purple_cipher_set_batch_mode:
 * @cipher:  The cipher whose batch mode to set
 * @mode:    The batch mode under which the cipher should operate
 *
 * Sets the batch mode of a cipher
 */
void purple_cipher_set_batch_mode(PurpleCipher *cipher, PurpleCipherBatchMode mode);

/**
 * purple_cipher_get_batch_mode:
 * @cipher: The cipher whose batch mode to get
 *
 * Gets the batch mode of a cipher
 *
 * Returns: The batch mode under which the cipher is operating
 */
PurpleCipherBatchMode purple_cipher_get_batch_mode(PurpleCipher *cipher);

/**
 * purple_cipher_get_block_size:
 * @cipher: The cipher whose block size to get
 *
 * Gets the block size of a cipher
 *
 * Returns: The block size of the cipher
 */
size_t purple_cipher_get_block_size(PurpleCipher *cipher);

/*@}*/

/*****************************************************************************/
/* PurpleHash API                                                            */
/*****************************************************************************/
/*@{*/

/**
 * purple_hash_get_type:
 *
 * Returns: The #GType for the Hash object.
 */
GType purple_hash_get_type(void);

/**
 * purple_hash_reset:
 * @hash:  The hash
 *
 * Resets a hash to it's default value
 * Note: If you have set an IV you will have to set it after resetting
 */
void purple_hash_reset(PurpleHash *hash);

/**
 * purple_hash_reset_state:
 * @hash:  The hash
 *
 * Resets a hash state to it's default value, but doesn't touch stateless
 * configuration.
 *
 * That means, IV and digest will be wiped out, but keys, ops or salt
 * will remain untouched.
 */
void purple_hash_reset_state(PurpleHash *hash);

/**
 * purple_hash_append:
 * @hash:    The hash
 * @data:    The data to append
 * @len:     The length of the data
 *
 * Appends data to the hash context
 */
void purple_hash_append(PurpleHash *hash, const guchar *data, size_t len);

/**
 * purple_hash_digest:
 * @hash:    The hash
 * @digest:  The return buffer for the digest
 * @len:     The length of the buffer
 *
 * Digests a hash context
 */
gboolean purple_hash_digest(PurpleHash *hash, guchar digest[], size_t len);

/**
 * purple_hash_digest_to_str:
 * @hash:     The hash
 * @digest_s: The return buffer for the string digest
 * @len:      The length of the buffer
 *
 * Converts a guchar digest into a hex string
 */
gboolean purple_hash_digest_to_str(PurpleHash *hash, gchar digest_s[], size_t len);

/**
 * purple_hash_get_digest_size:
 * @hash: The hash whose digest size to get
 *
 * Gets the digest size of a hash
 *
 * Returns: The digest size of the hash
 */
size_t purple_hash_get_digest_size(PurpleHash *hash);

/**
 * purple_hash_get_block_size:
 * @hash: The hash whose block size to get
 *
 * Gets the block size of a hash
 *
 * Returns: The block size of the hash
 */
size_t purple_hash_get_block_size(PurpleHash *hash);

/*@}*/

G_END_DECLS

#endif /* PURPLE_CIPHER_H */