Fri, 11 Oct 2013 15:34:15 +0530
Removed g_object_notify calls from ciphers and hashes
| 34182 | 1 | /* |
| 2 | * purple | |
| 3 | * | |
| 4 | * Purple is the legal property of its developers, whose names are too numerous | |
| 5 | * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 6 | * source distribution. | |
| 7 | * | |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU General Public License as published by | |
| 10 | * the Free Software Foundation; either version 2 of the License, or | |
| 11 | * (at your option) any later version. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA | |
| 21 | * | |
| 22 | * Written by Tomek Wasilczyk <tomkiewicz@cpw.pidgin.im> | |
| 23 | */ | |
| 24 | ||
|
34566
e0f887dee077
Split PurpleCipher into PurpleCipher and PurpleHash. Hashes will subclass PurpleHash.
Ankit Vani <a@nevitus.org>
parents:
34547
diff
changeset
|
25 | #include "aescipher.h" |
| 34182 | 26 | #include "debug.h" |
|
34644
368d270dc0c3
Included enums.h in AES, DES and DES3 cipher to use the PURPLE_TYPE_BATCH_MODE GType for batch_mode property
Ankit Vani <a@nevitus.org>
parents:
34566
diff
changeset
|
27 | #include "enums.h" |
| 34182 | 28 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
29 | #include <string.h> |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
30 | |
| 34182 | 31 | #if defined(HAVE_GNUTLS) |
| 32 | # define PURPLE_AES_USE_GNUTLS 1 | |
| 33 | # include <gnutls/gnutls.h> | |
| 34 | # include <gnutls/crypto.h> | |
| 35 | #elif defined(HAVE_NSS) | |
| 36 | # define PURPLE_AES_USE_NSS 1 | |
| 37 | # include <nss.h> | |
| 38 | # include <pk11pub.h> | |
| 39 | # include <prerror.h> | |
| 40 | #else | |
| 41 | # error "No GnuTLS or NSS support" | |
| 42 | #endif | |
| 43 | ||
| 44 | /* 128bit */ | |
| 45 | #define PURPLE_AES_BLOCK_SIZE 16 | |
| 46 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
47 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
48 | * Structs |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
49 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
50 | #define PURPLE_AES_CIPHER_GET_PRIVATE(obj) \ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
51 | (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_AES_CIPHER, PurpleAESCipherPrivate)) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
52 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
53 | typedef struct { |
| 34182 | 54 | guchar iv[PURPLE_AES_BLOCK_SIZE]; |
| 55 | guchar key[32]; | |
| 56 | guint key_size; | |
| 57 | gboolean failure; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
58 | } PurpleAESCipherPrivate; |
| 34182 | 59 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
60 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
61 | * Enums |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
62 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
63 | enum { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
64 | PROP_NONE, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
65 | PROP_BATCH_MODE, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
66 | PROP_IV, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
67 | PROP_KEY, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
68 | PROP_LAST, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
69 | }; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
70 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
71 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
72 | * Cipher Stuff |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
73 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
74 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
75 | typedef gboolean (*purple_aes_cipher_crypt_func)( |
| 34182 | 76 | const guchar *input, guchar *output, size_t len, |
| 77 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size); | |
| 78 | ||
| 79 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
80 | purple_aes_cipher_reset(PurpleCipher *cipher) |
| 34182 | 81 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
82 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 83 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
84 | g_return_if_fail(priv != NULL); |
| 34182 | 85 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
86 | memset(priv->iv, 0, sizeof(priv->iv)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
87 | memset(priv->key, 0, sizeof(priv->key)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
88 | priv->key_size = 32; /* 256bit */ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
89 | priv->failure = FALSE; |
| 34182 | 90 | } |
| 91 | ||
| 92 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
93 | purple_aes_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len) |
| 34182 | 94 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
95 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 96 | |
| 97 | if ((len > 0 && iv == NULL) || | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
98 | (len != 0 && len != sizeof(priv->iv))) { |
| 34182 | 99 | purple_debug_error("cipher-aes", "invalid IV length\n"); |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
100 | priv->failure = TRUE; |
| 34182 | 101 | return; |
| 102 | } | |
| 103 | ||
| 104 | if (len == 0) | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
105 | memset(priv->iv, 0, sizeof(priv->iv)); |
| 34182 | 106 | else |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
107 | memcpy(priv->iv, iv, len); |
| 34182 | 108 | } |
| 109 | ||
| 110 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
111 | purple_aes_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len) |
| 34182 | 112 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
113 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 114 | |
| 115 | if ((len > 0 && key == NULL) || | |
| 116 | (len != 0 && len != 16 && len != 24 && len != 32)) { | |
| 117 | purple_debug_error("cipher-aes", "invalid key length\n"); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
118 | priv->failure = TRUE; |
| 34182 | 119 | return; |
| 120 | } | |
| 121 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
122 | priv->key_size = len; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
123 | memset(priv->key, 0, sizeof(priv->key)); |
| 34182 | 124 | if (len > 0) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
125 | memcpy(priv->key, key, len); |
| 34182 | 126 | } |
| 127 | ||
| 128 | static guchar * | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
129 | purple_aes_cipher_pad_pkcs7(const guchar input[], size_t in_len, size_t *out_len) |
| 34182 | 130 | { |
| 131 | int padding_len, total_len; | |
| 132 | guchar *padded; | |
| 133 | ||
| 134 | g_return_val_if_fail(input != NULL, NULL); | |
| 135 | g_return_val_if_fail(out_len != NULL, NULL); | |
| 136 | ||
| 137 | padding_len = PURPLE_AES_BLOCK_SIZE - (in_len % PURPLE_AES_BLOCK_SIZE); | |
| 138 | total_len = in_len + padding_len; | |
| 139 | g_assert((total_len % PURPLE_AES_BLOCK_SIZE) == 0); | |
| 140 | ||
| 141 | padded = g_new(guchar, total_len); | |
| 142 | *out_len = total_len; | |
| 143 | ||
| 144 | memcpy(padded, input, in_len); | |
| 145 | memset(padded + in_len, padding_len, padding_len); | |
| 146 | ||
| 147 | return padded; | |
| 148 | } | |
| 149 | ||
| 150 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
151 | purple_aes_cipher_unpad_pkcs7(guchar input[], size_t in_len) |
| 34182 | 152 | { |
|
34304
faf0414a8b51
Fix most of libpurple warnings about -Wsign-compare
Tomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>
parents:
34275
diff
changeset
|
153 | guchar padding_len, i; |
| 34182 | 154 | size_t out_len; |
| 155 | ||
| 156 | g_return_val_if_fail(input != NULL, -1); | |
| 157 | g_return_val_if_fail(in_len > 0, -1); | |
| 158 | ||
| 159 | padding_len = input[in_len - 1]; | |
|
34304
faf0414a8b51
Fix most of libpurple warnings about -Wsign-compare
Tomasz Wasilczyk <tomkiewicz@cpw.pidgin.im>
parents:
34275
diff
changeset
|
160 | if (padding_len == 0 || padding_len > PURPLE_AES_BLOCK_SIZE || |
| 34182 | 161 | padding_len > in_len) { |
| 162 | purple_debug_warning("cipher-aes", | |
|
34275
a6ddc53d1c84
Fix minor printf format warning.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents:
34250
diff
changeset
|
163 | "Invalid padding length: %d (total %" G_GSIZE_FORMAT ") - " |
| 34182 | 164 | "most probably, the key was invalid\n", |
| 165 | padding_len, in_len); | |
| 166 | return -1; | |
| 167 | } | |
| 168 | ||
| 169 | out_len = in_len - padding_len; | |
| 170 | for (i = 0; i < padding_len; i++) { | |
| 171 | if (input[out_len + i] != padding_len) { | |
| 172 | purple_debug_warning("cipher-aes", | |
| 173 | "Padding doesn't match at pos %d (found %02x, " | |
| 174 | "expected %02x) - " | |
| 175 | "most probably, the key was invalid\n", | |
| 176 | i, input[out_len + i], padding_len); | |
| 177 | return -1; | |
| 178 | } | |
| 179 | } | |
| 180 | ||
| 181 | memset(input + out_len, 0, padding_len); | |
| 182 | return out_len; | |
| 183 | } | |
| 184 | ||
| 185 | #ifdef PURPLE_AES_USE_GNUTLS | |
| 186 | ||
| 187 | static gnutls_cipher_hd_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
188 | purple_aes_cipher_gnutls_crypt_init(guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], |
| 34182 | 189 | guint key_size) |
| 190 | { | |
| 191 | gnutls_cipher_hd_t handle; | |
| 192 | gnutls_cipher_algorithm_t algorithm; | |
| 193 | gnutls_datum_t key_info, iv_info; | |
| 194 | int ret; | |
| 195 | ||
| 196 | if (key_size == 16) | |
| 197 | algorithm = GNUTLS_CIPHER_AES_128_CBC; | |
| 198 | else if (key_size == 24) | |
| 199 | algorithm = GNUTLS_CIPHER_AES_192_CBC; | |
| 200 | else if (key_size == 32) | |
| 201 | algorithm = GNUTLS_CIPHER_AES_256_CBC; | |
| 202 | else | |
| 203 | g_return_val_if_reached(NULL); | |
| 204 | ||
| 205 | key_info.data = key; | |
| 206 | key_info.size = key_size; | |
| 207 | ||
| 208 | iv_info.data = iv; | |
| 209 | iv_info.size = PURPLE_AES_BLOCK_SIZE; | |
| 210 | ||
| 211 | ret = gnutls_cipher_init(&handle, algorithm, &key_info, &iv_info); | |
| 212 | if (ret != 0) { | |
| 213 | purple_debug_error("cipher-aes", | |
| 214 | "gnutls_cipher_init failed: %d\n", ret); | |
| 215 | return NULL; | |
| 216 | } | |
| 217 | ||
| 218 | return handle; | |
| 219 | } | |
| 220 | ||
| 221 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
222 | purple_aes_cipher_gnutls_encrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 223 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 224 | { | |
| 225 | gnutls_cipher_hd_t handle; | |
| 226 | int ret; | |
| 227 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
228 | handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size); |
| 34182 | 229 | if (handle == NULL) |
| 230 | return FALSE; | |
| 231 | ||
| 34758 | 232 | ret = gnutls_cipher_encrypt2(handle, (guchar *)input, len, output, len); |
| 34182 | 233 | gnutls_cipher_deinit(handle); |
| 234 | ||
| 235 | if (ret != 0) { | |
| 236 | purple_debug_error("cipher-aes", | |
| 237 | "gnutls_cipher_encrypt2 failed: %d\n", ret); | |
| 238 | return FALSE; | |
| 239 | } | |
| 240 | ||
| 241 | return TRUE; | |
| 242 | } | |
| 243 | ||
| 244 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
245 | purple_aes_cipher_gnutls_decrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 246 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 247 | { | |
| 248 | gnutls_cipher_hd_t handle; | |
| 249 | int ret; | |
| 250 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
251 | handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size); |
| 34182 | 252 | if (handle == NULL) |
| 253 | return FALSE; | |
| 254 | ||
| 255 | ret = gnutls_cipher_decrypt2(handle, input, len, output, len); | |
| 256 | gnutls_cipher_deinit(handle); | |
| 257 | ||
| 258 | if (ret != 0) { | |
| 259 | purple_debug_error("cipher-aes", | |
| 260 | "gnutls_cipher_decrypt2 failed: %d\n", ret); | |
| 261 | return FALSE; | |
| 262 | } | |
| 263 | ||
| 264 | return TRUE; | |
| 265 | } | |
| 266 | ||
|
34250
1625de486023
Fix pre-processing directives to consistently only inclde code that will be used.
Daniel Atallah <datallah@pidgin.im>
parents:
34248
diff
changeset
|
267 | #elif defined(PURPLE_AES_USE_NSS) |
| 34182 | 268 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
269 | typedef struct { |
| 34182 | 270 | PK11SlotInfo *slot; |
| 271 | PK11SymKey *sym_key; | |
| 272 | SECItem *sec_param; | |
| 273 | PK11Context *enc_context; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
274 | } PurpleAESCipherNSSContext; |
| 34182 | 275 | |
| 276 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
277 | purple_aes_cipher_nss_cleanup(PurpleAESCipherNSSContext *context) |
| 34182 | 278 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
279 | g_return_if_fail(context != NULL); |
| 34182 | 280 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
281 | if (context->enc_context != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
282 | PK11_DestroyContext(context->enc_context, TRUE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
283 | if (context->sec_param != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
284 | SECITEM_FreeItem(context->sec_param, TRUE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
285 | if (context->sym_key != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
286 | PK11_FreeSymKey(context->sym_key); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
287 | if (context->slot != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
288 | PK11_FreeSlot(context->slot); |
| 34182 | 289 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
290 | memset(context, 0, sizeof(PurpleAESCipherNSSContext)); |
| 34182 | 291 | } |
| 292 | ||
| 293 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
294 | purple_aes_cipher_nss_crypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 295 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size, |
| 296 | CK_ATTRIBUTE_TYPE operation) | |
| 297 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
298 | PurpleAESCipherNSSContext context; |
| 34182 | 299 | CK_MECHANISM_TYPE cipher_mech = CKM_AES_CBC; |
| 300 | SECItem key_item, iv_item; | |
| 301 | SECStatus ret; | |
| 302 | int outlen = 0; | |
| 303 | unsigned int outlen_tmp = 0; | |
| 304 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
305 | memset(&context, 0, sizeof(PurpleAESCipherNSSContext)); |
| 34182 | 306 | |
| 307 | if (NSS_NoDB_Init(NULL) != SECSuccess) { | |
| 308 | purple_debug_error("cipher-aes", | |
| 309 | "NSS_NoDB_Init failed: %d\n", PR_GetError()); | |
| 310 | return FALSE; | |
| 311 | } | |
| 312 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
313 | context.slot = PK11_GetBestSlot(cipher_mech, NULL); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
314 | if (context.slot == NULL) { |
| 34182 | 315 | purple_debug_error("cipher-aes", |
| 316 | "PK11_GetBestSlot failed: %d\n", PR_GetError()); | |
| 317 | return FALSE; | |
| 318 | } | |
| 319 | ||
| 320 | key_item.type = siBuffer; | |
| 321 | key_item.data = key; | |
| 322 | key_item.len = key_size; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
323 | context.sym_key = PK11_ImportSymKey(context.slot, cipher_mech, |
| 34182 | 324 | PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, NULL); |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
325 | if (context.sym_key == NULL) { |
| 34182 | 326 | purple_debug_error("cipher-aes", |
| 327 | "PK11_ImportSymKey failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
328 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 329 | return FALSE; |
| 330 | } | |
| 331 | ||
| 332 | iv_item.type = siBuffer; | |
| 333 | iv_item.data = iv; | |
| 334 | iv_item.len = PURPLE_AES_BLOCK_SIZE; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
335 | context.sec_param = PK11_ParamFromIV(cipher_mech, &iv_item); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
336 | if (context.sec_param == NULL) { |
| 34182 | 337 | purple_debug_error("cipher-aes", |
| 338 | "PK11_ParamFromIV failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
339 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 340 | return FALSE; |
| 341 | } | |
| 342 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
343 | context.enc_context = PK11_CreateContextBySymKey(cipher_mech, operation, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
344 | context.sym_key, context.sec_param); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
345 | if (context.enc_context == NULL) { |
| 34182 | 346 | purple_debug_error("cipher-aes", |
| 347 | "PK11_CreateContextBySymKey failed: %d\n", | |
| 348 | PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
349 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 350 | return FALSE; |
| 351 | } | |
| 352 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
353 | ret = PK11_CipherOp(context.enc_context, output, &outlen, len, input, len); |
| 34182 | 354 | if (ret != SECSuccess) { |
| 355 | purple_debug_error("cipher-aes", | |
| 356 | "PK11_CipherOp failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
357 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 358 | return FALSE; |
| 359 | } | |
| 360 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
361 | ret = PK11_DigestFinal(context.enc_context, output + outlen, &outlen_tmp, |
| 34182 | 362 | len - outlen); |
| 363 | if (ret != SECSuccess) { | |
| 364 | purple_debug_error("cipher-aes", | |
| 365 | "PK11_DigestFinal failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
366 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 367 | return FALSE; |
| 368 | } | |
| 369 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
370 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 371 | |
| 372 | outlen += outlen_tmp; | |
| 373 | if (outlen != len) { | |
| 374 | purple_debug_error("cipher-aes", | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
375 | "resulting length doesn't match: %d (expected: %lu)\n", |
| 34182 | 376 | outlen, len); |
| 377 | return FALSE; | |
| 378 | } | |
| 379 | ||
| 380 | return TRUE; | |
| 381 | } | |
| 382 | ||
| 383 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
384 | purple_aes_cipher_nss_encrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 385 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 386 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
387 | return purple_aes_cipher_nss_crypt(input, output, len, iv, key, key_size, |
| 34182 | 388 | CKA_ENCRYPT); |
| 389 | } | |
| 390 | ||
| 391 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
392 | purple_aes_cipher_nss_decrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 393 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 394 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
395 | return purple_aes_cipher_nss_crypt(input, output, len, iv, key, key_size, |
| 34182 | 396 | CKA_DECRYPT); |
| 397 | } | |
| 398 | ||
| 399 | #endif /* PURPLE_AES_USE_NSS */ | |
| 400 | ||
| 401 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
402 | purple_aes_cipher_encrypt(PurpleCipher *cipher, const guchar input[], |
| 34182 | 403 | size_t in_len, guchar output[], size_t out_size) |
| 404 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
405 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
406 | purple_aes_cipher_crypt_func encrypt_func; |
| 34182 | 407 | guchar *input_padded; |
| 408 | size_t out_len = 0; | |
| 409 | gboolean succ; | |
| 410 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
411 | if (priv->failure) |
| 34182 | 412 | return -1; |
| 413 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
414 | input_padded = purple_aes_cipher_pad_pkcs7(input, in_len, &out_len); |
| 34182 | 415 | |
| 416 | if (out_len > out_size) { | |
| 417 | purple_debug_error("cipher-aes", "Output buffer too small\n"); | |
| 418 | memset(input_padded, 0, out_len); | |
| 419 | g_free(input_padded); | |
| 420 | return -1; | |
| 421 | } | |
| 422 | ||
| 423 | #if defined(PURPLE_AES_USE_GNUTLS) | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
424 | encrypt_func = purple_aes_cipher_gnutls_encrypt; |
| 34182 | 425 | #elif defined(PURPLE_AES_USE_NSS) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
426 | encrypt_func = purple_aes_cipher_nss_encrypt; |
| 34182 | 427 | #else |
| 428 | # error "No matching encrypt_func" | |
| 429 | #endif | |
| 430 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
431 | succ = encrypt_func(input_padded, output, out_len, priv->iv, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
432 | priv->key, priv->key_size); |
| 34182 | 433 | |
| 434 | memset(input_padded, 0, out_len); | |
| 435 | g_free(input_padded); | |
| 436 | ||
| 437 | if (!succ) { | |
| 438 | memset(output, 0, out_len); | |
| 439 | return -1; | |
| 440 | } | |
| 441 | ||
| 442 | return out_len; | |
| 443 | } | |
| 444 | ||
| 445 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
446 | purple_aes_cipher_decrypt(PurpleCipher *cipher, const guchar input[], |
| 34182 | 447 | size_t in_len, guchar output[], size_t out_size) |
| 448 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
449 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
450 | purple_aes_cipher_crypt_func decrypt_func; |
| 34182 | 451 | gboolean succ; |
| 452 | ssize_t out_len; | |
| 453 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
454 | if (priv->failure) |
| 34182 | 455 | return -1; |
| 456 | ||
| 457 | if (in_len > out_size) { | |
| 458 | purple_debug_error("cipher-aes", "Output buffer too small\n"); | |
| 459 | return -1; | |
| 460 | } | |
| 461 | ||
| 462 | if ((in_len % PURPLE_AES_BLOCK_SIZE) != 0 || in_len == 0) { | |
| 463 | purple_debug_error("cipher-aes", "Malformed data\n"); | |
| 464 | return -1; | |
| 465 | } | |
| 466 | ||
| 467 | #if defined(PURPLE_AES_USE_GNUTLS) | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
468 | decrypt_func = purple_aes_cipher_gnutls_decrypt; |
| 34182 | 469 | #elif defined(PURPLE_AES_USE_NSS) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
470 | decrypt_func = purple_aes_cipher_nss_decrypt; |
| 34182 | 471 | #else |
| 472 | # error "No matching encrypt_func" | |
| 473 | #endif | |
| 474 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
475 | succ = decrypt_func(input, output, in_len, priv->iv, priv->key, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
476 | priv->key_size); |
| 34182 | 477 | |
| 478 | if (!succ) { | |
| 479 | memset(output, 0, in_len); | |
| 480 | return -1; | |
| 481 | } | |
| 482 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
483 | out_len = purple_aes_cipher_unpad_pkcs7(output, in_len); |
| 34182 | 484 | if (out_len < 0) { |
| 485 | memset(output, 0, in_len); | |
| 486 | return -1; | |
| 487 | } | |
| 488 | ||
| 489 | return out_len; | |
| 490 | } | |
| 491 | ||
| 492 | static size_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
493 | purple_aes_cipher_get_key_size(PurpleCipher *cipher) |
| 34182 | 494 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
495 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 496 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
497 | return priv->key_size; |
| 34182 | 498 | } |
| 499 | ||
| 500 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
501 | purple_aes_cipher_set_batch_mode(PurpleCipher *cipher, |
| 34182 | 502 | PurpleCipherBatchMode mode) |
| 503 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
504 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 505 | |
|
35014
21864c2f543f
Removed g_object_notify calls from ciphers and hashes
Ankit Vani <a@nevitus.org>
parents:
34965
diff
changeset
|
506 | if (mode == PURPLE_CIPHER_BATCH_MODE_CBC) |
| 34182 | 507 | return; |
| 508 | ||
| 509 | purple_debug_error("cipher-aes", "unsupported batch mode\n"); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
510 | priv->failure = TRUE; |
| 34182 | 511 | } |
| 512 | ||
| 513 | static PurpleCipherBatchMode | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
514 | purple_aes_cipher_get_batch_mode(PurpleCipher *cipher) |
| 34182 | 515 | { |
| 516 | return PURPLE_CIPHER_BATCH_MODE_CBC; | |
| 517 | } | |
| 518 | ||
| 519 | static size_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
520 | purple_aes_cipher_get_block_size(PurpleCipher *cipher) |
| 34182 | 521 | { |
| 522 | return PURPLE_AES_BLOCK_SIZE; | |
| 523 | } | |
| 524 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
525 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
526 | * Object Stuff |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
527 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
528 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
529 | purple_aes_cipher_get_property(GObject *obj, guint param_id, GValue *value, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
530 | GParamSpec *pspec) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
531 | { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
532 | PurpleCipher *cipher = PURPLE_CIPHER(obj); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
533 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
534 | switch(param_id) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
535 | case PROP_BATCH_MODE: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
536 | g_value_set_enum(value, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
537 | purple_cipher_get_batch_mode(cipher)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
538 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
539 | default: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
540 | G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
541 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
542 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
543 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
544 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
545 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
546 | purple_aes_cipher_set_property(GObject *obj, guint param_id, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
547 | const GValue *value, GParamSpec *pspec) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
548 | { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
549 | PurpleCipher *cipher = PURPLE_CIPHER(obj); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
550 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
551 | switch(param_id) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
552 | case PROP_BATCH_MODE: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
553 | purple_cipher_set_batch_mode(cipher, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
554 | g_value_get_enum(value)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
555 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
556 | case PROP_IV: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
557 | { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
558 | guchar *iv = (guchar *)g_value_get_string(value); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
559 | purple_cipher_set_iv(cipher, iv, strlen((gchar*)iv)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
560 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
561 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
562 | case PROP_KEY: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
563 | purple_cipher_set_key(cipher, (guchar *)g_value_get_string(value), |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
564 | purple_aes_cipher_get_key_size(cipher)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
565 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
566 | default: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
567 | G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
568 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
569 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
570 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
571 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
572 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
573 | purple_aes_cipher_class_init(PurpleAESCipherClass *klass) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
574 | GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
575 | PurpleCipherClass *cipher_class = PURPLE_CIPHER_CLASS(klass); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
576 | GParamSpec *pspec; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
577 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
578 | obj_class->get_property = purple_aes_cipher_get_property; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
579 | obj_class->set_property = purple_aes_cipher_set_property; |
| 34182 | 580 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
581 | cipher_class->reset = purple_aes_cipher_reset; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
582 | cipher_class->set_iv = purple_aes_cipher_set_iv; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
583 | cipher_class->encrypt = purple_aes_cipher_encrypt; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
584 | cipher_class->decrypt = purple_aes_cipher_decrypt; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
585 | cipher_class->set_key = purple_aes_cipher_set_key; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
586 | cipher_class->get_key_size = purple_aes_cipher_get_key_size; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
587 | cipher_class->set_batch_mode = purple_aes_cipher_set_batch_mode; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
588 | cipher_class->get_batch_mode = purple_aes_cipher_get_batch_mode; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
589 | cipher_class->get_block_size = purple_aes_cipher_get_block_size; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
590 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
591 | pspec = g_param_spec_enum("batch_mode", "batch_mode", "batch_mode", |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
592 | PURPLE_TYPE_CIPHER_BATCH_MODE, 0, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
593 | G_PARAM_READWRITE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
594 | g_object_class_install_property(obj_class, PROP_BATCH_MODE, pspec); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
595 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
596 | pspec = g_param_spec_string("iv", "iv", "iv", NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
597 | G_PARAM_WRITABLE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
598 | g_object_class_install_property(obj_class, PROP_IV, pspec); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
599 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
600 | pspec = g_param_spec_string("key", "key", "key", NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
601 | G_PARAM_WRITABLE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
602 | g_object_class_install_property(obj_class, PROP_KEY, pspec); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
603 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
604 | g_type_class_add_private(klass, sizeof(PurpleAESCipherPrivate)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
605 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
606 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
607 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
608 | purple_aes_cipher_init(PurpleCipher *cipher) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
609 | purple_cipher_reset(cipher); |
| 34182 | 610 | } |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
611 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
612 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
613 | * API |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
614 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
615 | GType |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
616 | purple_aes_cipher_get_gtype(void) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
617 | static GType type = 0; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
618 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
619 | if(type == 0) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
620 | static const GTypeInfo info = { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
621 | sizeof(PurpleAESCipherClass), |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
622 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
623 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
624 | (GClassInitFunc)purple_aes_cipher_class_init, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
625 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
626 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
627 | sizeof(PurpleAESCipher), |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
628 | 0, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
629 | (GInstanceInitFunc)purple_aes_cipher_init, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
630 | NULL |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
631 | }; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
632 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
633 | type = g_type_register_static(PURPLE_TYPE_CIPHER, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
634 | "PurpleAESCipher", |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
635 | &info, 0); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
636 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
637 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
638 | return type; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
639 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
640 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
641 | PurpleCipher * |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
642 | purple_aes_cipher_new(void) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
643 | return g_object_new(PURPLE_TYPE_AES_CIPHER, NULL); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
644 | } |