Migrate xmpp-caps.xml to XDG cache dir
[pidgin-git.git] / libpurple / cipher.h
blob57b90d38be9fe1205d51e971cf9e0f949821974e
1 /* purple
3 * Purple is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #ifndef PURPLE_CIPHER_H
23 #define PURPLE_CIPHER_H
24 /**
25 * SECTION:cipher
26 * @section_id: libpurple-cipher
27 * @short_description: <filename>cipher.h</filename>
28 * @title: Cipher and Hash API
31 #include <glib.h>
32 #include <glib-object.h>
33 #include <string.h>
35 #define PURPLE_TYPE_CIPHER (purple_cipher_get_type())
36 #define PURPLE_CIPHER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_CIPHER, PurpleCipher))
37 #define PURPLE_CIPHER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_CIPHER, PurpleCipherClass))
38 #define PURPLE_IS_CIPHER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_CIPHER))
39 #define PURPLE_IS_CIPHER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_CIPHER))
40 #define PURPLE_CIPHER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_CIPHER, PurpleCipherClass))
42 typedef struct _PurpleCipher PurpleCipher;
43 typedef struct _PurpleCipherClass PurpleCipherClass;
45 #define PURPLE_TYPE_HASH (purple_hash_get_type())
46 #define PURPLE_HASH(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PURPLE_TYPE_HASH, PurpleHash))
47 #define PURPLE_HASH_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PURPLE_TYPE_HASH, PurpleHashClass))
48 #define PURPLE_IS_HASH(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PURPLE_TYPE_HASH))
49 #define PURPLE_IS_HASH_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PURPLE_TYPE_HASH))
50 #define PURPLE_HASH_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PURPLE_TYPE_HASH, PurpleHashClass))
52 typedef struct _PurpleHash PurpleHash;
53 typedef struct _PurpleHashClass PurpleHashClass;
55 /**
56 * PurpleCipherBatchMode:
57 * @PURPLE_CIPHER_BATCH_MODE_ECB: Electronic Codebook Mode
58 * @PURPLE_CIPHER_BATCH_MODE_CBC: Cipher Block Chaining Mode
60 * Modes for batch encrypters
62 typedef enum {
63 PURPLE_CIPHER_BATCH_MODE_ECB,
64 PURPLE_CIPHER_BATCH_MODE_CBC
65 } PurpleCipherBatchMode;
67 /**
68 * PurpleCipher:
70 * Purple Cipher is an opaque data structure and should not be used directly.
72 struct _PurpleCipher {
73 GObject gparent;
76 struct _PurpleCipherClass {
77 GObjectClass parent_class;
79 /** The reset function */
80 void (*reset)(PurpleCipher *cipher);
82 /** The reset state function */
83 void (*reset_state)(PurpleCipher *cipher);
85 /** The set initialization vector function */
86 void (*set_iv)(PurpleCipher *cipher, guchar *iv, size_t len);
88 /** The append data function */
89 void (*append)(PurpleCipher *cipher, const guchar *data, size_t len);
91 /** The digest function */
92 gboolean (*digest)(PurpleCipher *cipher, guchar digest[], size_t len);
94 /** The get digest size function */
95 size_t (*get_digest_size)(PurpleCipher *cipher);
97 /** The encrypt function */
98 ssize_t (*encrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
100 /** The decrypt function */
101 ssize_t (*decrypt)(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
103 /** The set salt function */
104 void (*set_salt)(PurpleCipher *cipher, const guchar *salt, size_t len);
106 /** The set key function */
107 void (*set_key)(PurpleCipher *cipher, const guchar *key, size_t len);
109 /** The get key size function */
110 size_t (*get_key_size)(PurpleCipher *cipher);
112 /** The set batch mode function */
113 void (*set_batch_mode)(PurpleCipher *cipher, PurpleCipherBatchMode mode);
115 /** The get batch mode function */
116 PurpleCipherBatchMode (*get_batch_mode)(PurpleCipher *cipher);
118 /** The get block size function */
119 size_t (*get_block_size)(PurpleCipher *cipher);
121 /*< private >*/
122 void (*_purple_reserved1)(void);
123 void (*_purple_reserved2)(void);
124 void (*_purple_reserved3)(void);
125 void (*_purple_reserved4)(void);
129 * PurpleHash:
131 * Purple Hash is an opaque data structure and should not be used directly.
133 struct _PurpleHash {
134 GObject gparent;
137 struct _PurpleHashClass {
138 GObjectClass parent_class;
140 /** The reset function */
141 void (*reset)(PurpleHash *hash);
143 /** The reset state function */
144 void (*reset_state)(PurpleHash *hash);
146 /** The append data function */
147 void (*append)(PurpleHash *hash, const guchar *data, size_t len);
149 /** The digest function */
150 gboolean (*digest)(PurpleHash *hash, guchar digest[], size_t len);
152 /** The get digest size function */
153 size_t (*get_digest_size)(PurpleHash *hash);
155 /** The get block size function */
156 size_t (*get_block_size)(PurpleHash *hash);
158 /*< private >*/
159 void (*_purple_reserved1)(void);
160 void (*_purple_reserved2)(void);
161 void (*_purple_reserved3)(void);
162 void (*_purple_reserved4)(void);
165 G_BEGIN_DECLS
167 /*****************************************************************************/
168 /* PurpleCipher API */
169 /*****************************************************************************/
172 * purple_cipher_get_type:
174 * Returns: The #GType for the Cipher object.
176 GType purple_cipher_get_type(void);
179 * purple_cipher_reset:
180 * @cipher: The cipher
182 * Resets a cipher to it's default value
183 * Note: If you have set an IV you will have to set it after resetting
185 void purple_cipher_reset(PurpleCipher *cipher);
188 * purple_cipher_reset_state:
189 * @cipher: The cipher
191 * Resets a cipher state to it's default value, but doesn't touch stateless
192 * configuration.
194 * That means, IV and digest will be wiped out, but keys, ops or salt
195 * will remain untouched.
197 void purple_cipher_reset_state(PurpleCipher *cipher);
200 * purple_cipher_set_iv:
201 * @cipher: The cipher
202 * @iv: The initialization vector to set
203 * @len: The len of the IV
205 * Sets the initialization vector for a cipher
206 * Note: This should only be called right after a cipher is created or reset
208 void purple_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len);
211 * purple_cipher_append:
212 * @cipher: The cipher
213 * @data: The data to append
214 * @len: The length of the data
216 * Appends data to the cipher context
218 void purple_cipher_append(PurpleCipher *cipher, const guchar *data, size_t len);
221 * purple_cipher_digest:
222 * @cipher: The cipher
223 * @digest: The return buffer for the digest
224 * @len: The length of the buffer
226 * Digests a cipher context
228 gboolean purple_cipher_digest(PurpleCipher *cipher, guchar digest[], size_t len);
231 * purple_cipher_digest_to_str:
232 * @cipher: The cipher
233 * @digest_s: The return buffer for the string digest
234 * @len: The length of the buffer
236 * Converts a guchar digest into a hex string
238 gboolean purple_cipher_digest_to_str(PurpleCipher *cipher, gchar digest_s[], size_t len);
241 * purple_cipher_get_digest_size:
242 * @cipher: The cipher whose digest size to get
244 * Gets the digest size of a cipher
246 * Returns: The digest size of the cipher
248 size_t purple_cipher_get_digest_size(PurpleCipher *cipher);
251 * purple_cipher_encrypt:
252 * @cipher: The cipher
253 * @input: The data to encrypt
254 * @in_len: The length of the data
255 * @output: The output buffer
256 * @out_size: The size of the output buffer
258 * Encrypts data using the cipher
260 * Returns: A length of data that was outputed or -1, if failed
262 ssize_t purple_cipher_encrypt(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
265 * purple_cipher_decrypt:
266 * @cipher: The cipher
267 * @input: The data to encrypt
268 * @in_len: The length of the returned value
269 * @output: The output buffer
270 * @out_size: The size of the output buffer
272 * Decrypts data using the cipher
274 * Returns: A length of data that was outputed or -1, if failed
276 ssize_t purple_cipher_decrypt(PurpleCipher *cipher, const guchar input[], size_t in_len, guchar output[], size_t out_size);
279 * purple_cipher_set_salt:
280 * @cipher: The cipher whose salt to set
281 * @salt: The salt
282 * @len: The length of the salt
284 * Sets the salt on a cipher
286 void purple_cipher_set_salt(PurpleCipher *cipher, const guchar *salt, size_t len);
289 * purple_cipher_set_key:
290 * @cipher: The cipher whose key to set
291 * @key: The key
292 * @len: The size of the key
294 * Sets the key on a cipher
296 void purple_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len);
299 * purple_cipher_get_key_size:
300 * @cipher: The cipher whose key size to get
302 * Gets the size of the key if the cipher supports it
304 * Returns: The size of the key
306 size_t purple_cipher_get_key_size(PurpleCipher *cipher);
309 * purple_cipher_set_batch_mode:
310 * @cipher: The cipher whose batch mode to set
311 * @mode: The batch mode under which the cipher should operate
313 * Sets the batch mode of a cipher
315 void purple_cipher_set_batch_mode(PurpleCipher *cipher, PurpleCipherBatchMode mode);
318 * purple_cipher_get_batch_mode:
319 * @cipher: The cipher whose batch mode to get
321 * Gets the batch mode of a cipher
323 * Returns: The batch mode under which the cipher is operating
325 PurpleCipherBatchMode purple_cipher_get_batch_mode(PurpleCipher *cipher);
328 * purple_cipher_get_block_size:
329 * @cipher: The cipher whose block size to get
331 * Gets the block size of a cipher
333 * Returns: The block size of the cipher
335 size_t purple_cipher_get_block_size(PurpleCipher *cipher);
337 /*****************************************************************************/
338 /* PurpleHash API */
339 /*****************************************************************************/
342 * purple_hash_get_type:
344 * Returns: The #GType for the Hash object.
346 GType purple_hash_get_type(void);
349 * purple_hash_reset:
350 * @hash: The hash
352 * Resets a hash to it's default value
353 * Note: If you have set an IV you will have to set it after resetting
355 void purple_hash_reset(PurpleHash *hash);
358 * purple_hash_reset_state:
359 * @hash: The hash
361 * Resets a hash state to it's default value, but doesn't touch stateless
362 * configuration.
364 * That means, IV and digest will be wiped out, but keys, ops or salt
365 * will remain untouched.
367 void purple_hash_reset_state(PurpleHash *hash);
370 * purple_hash_append:
371 * @hash: The hash
372 * @data: The data to append
373 * @len: The length of the data
375 * Appends data to the hash context
377 void purple_hash_append(PurpleHash *hash, const guchar *data, size_t len);
380 * purple_hash_digest:
381 * @hash: The hash
382 * @digest: The return buffer for the digest
383 * @len: The length of the buffer
385 * Digests a hash context
387 gboolean purple_hash_digest(PurpleHash *hash, guchar digest[], size_t len);
390 * purple_hash_digest_to_str:
391 * @hash: The hash
392 * @digest_s: The return buffer for the string digest
393 * @len: The length of the buffer
395 * Converts a guchar digest into a hex string
397 gboolean purple_hash_digest_to_str(PurpleHash *hash, gchar digest_s[], size_t len);
400 * purple_hash_get_digest_size:
401 * @hash: The hash whose digest size to get
403 * Gets the digest size of a hash
405 * Returns: The digest size of the hash
407 size_t purple_hash_get_digest_size(PurpleHash *hash);
410 * purple_hash_get_block_size:
411 * @hash: The hash whose block size to get
413 * Gets the block size of a hash
415 * Returns: The block size of the hash
417 size_t purple_hash_get_block_size(PurpleHash *hash);
419 G_END_DECLS
421 #endif /* PURPLE_CIPHER_H */