Sat, 13 Jul 2013 02:30:09 +0530
Merged default branch
| 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); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
108 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
109 | g_object_notify(G_OBJECT(cipher), "iv"); |
| 34182 | 110 | } |
| 111 | ||
| 112 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
113 | purple_aes_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len) |
| 34182 | 114 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
115 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 116 | |
| 117 | if ((len > 0 && key == NULL) || | |
| 118 | (len != 0 && len != 16 && len != 24 && len != 32)) { | |
| 119 | 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
|
120 | priv->failure = TRUE; |
| 34182 | 121 | return; |
| 122 | } | |
| 123 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
124 | priv->key_size = len; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
125 | memset(priv->key, 0, sizeof(priv->key)); |
| 34182 | 126 | if (len > 0) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
127 | memcpy(priv->key, key, len); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
128 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
129 | g_object_notify(G_OBJECT(cipher), "key"); |
| 34182 | 130 | } |
| 131 | ||
| 132 | static guchar * | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
133 | purple_aes_cipher_pad_pkcs7(const guchar input[], size_t in_len, size_t *out_len) |
| 34182 | 134 | { |
| 135 | int padding_len, total_len; | |
| 136 | guchar *padded; | |
| 137 | ||
| 138 | g_return_val_if_fail(input != NULL, NULL); | |
| 139 | g_return_val_if_fail(out_len != NULL, NULL); | |
| 140 | ||
| 141 | padding_len = PURPLE_AES_BLOCK_SIZE - (in_len % PURPLE_AES_BLOCK_SIZE); | |
| 142 | total_len = in_len + padding_len; | |
| 143 | g_assert((total_len % PURPLE_AES_BLOCK_SIZE) == 0); | |
| 144 | ||
| 145 | padded = g_new(guchar, total_len); | |
| 146 | *out_len = total_len; | |
| 147 | ||
| 148 | memcpy(padded, input, in_len); | |
| 149 | memset(padded + in_len, padding_len, padding_len); | |
| 150 | ||
| 151 | return padded; | |
| 152 | } | |
| 153 | ||
| 154 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
155 | purple_aes_cipher_unpad_pkcs7(guchar input[], size_t in_len) |
| 34182 | 156 | { |
| 157 | int padding_len, i; | |
| 158 | size_t out_len; | |
| 159 | ||
| 160 | g_return_val_if_fail(input != NULL, -1); | |
| 161 | g_return_val_if_fail(in_len > 0, -1); | |
| 162 | ||
| 163 | padding_len = input[in_len - 1]; | |
| 164 | if (padding_len <= 0 || padding_len > PURPLE_AES_BLOCK_SIZE || | |
| 165 | padding_len > in_len) { | |
| 166 | purple_debug_warning("cipher-aes", | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
167 | "Invalid padding length: %d (total %lu) - " |
| 34182 | 168 | "most probably, the key was invalid\n", |
| 169 | padding_len, in_len); | |
| 170 | return -1; | |
| 171 | } | |
| 172 | ||
| 173 | out_len = in_len - padding_len; | |
| 174 | for (i = 0; i < padding_len; i++) { | |
| 175 | if (input[out_len + i] != padding_len) { | |
| 176 | purple_debug_warning("cipher-aes", | |
| 177 | "Padding doesn't match at pos %d (found %02x, " | |
| 178 | "expected %02x) - " | |
| 179 | "most probably, the key was invalid\n", | |
| 180 | i, input[out_len + i], padding_len); | |
| 181 | return -1; | |
| 182 | } | |
| 183 | } | |
| 184 | ||
| 185 | memset(input + out_len, 0, padding_len); | |
| 186 | return out_len; | |
| 187 | } | |
| 188 | ||
| 189 | #ifdef PURPLE_AES_USE_GNUTLS | |
| 190 | ||
| 191 | static gnutls_cipher_hd_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
192 | purple_aes_cipher_gnutls_crypt_init(guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], |
| 34182 | 193 | guint key_size) |
| 194 | { | |
| 195 | gnutls_cipher_hd_t handle; | |
| 196 | gnutls_cipher_algorithm_t algorithm; | |
| 197 | gnutls_datum_t key_info, iv_info; | |
| 198 | int ret; | |
| 199 | ||
| 200 | if (key_size == 16) | |
| 201 | algorithm = GNUTLS_CIPHER_AES_128_CBC; | |
| 202 | else if (key_size == 24) | |
| 203 | algorithm = GNUTLS_CIPHER_AES_192_CBC; | |
| 204 | else if (key_size == 32) | |
| 205 | algorithm = GNUTLS_CIPHER_AES_256_CBC; | |
| 206 | else | |
| 207 | g_return_val_if_reached(NULL); | |
| 208 | ||
| 209 | key_info.data = key; | |
| 210 | key_info.size = key_size; | |
| 211 | ||
| 212 | iv_info.data = iv; | |
| 213 | iv_info.size = PURPLE_AES_BLOCK_SIZE; | |
| 214 | ||
| 215 | ret = gnutls_cipher_init(&handle, algorithm, &key_info, &iv_info); | |
| 216 | if (ret != 0) { | |
| 217 | purple_debug_error("cipher-aes", | |
| 218 | "gnutls_cipher_init failed: %d\n", ret); | |
| 219 | return NULL; | |
| 220 | } | |
| 221 | ||
| 222 | return handle; | |
| 223 | } | |
| 224 | ||
| 225 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
226 | purple_aes_cipher_gnutls_encrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 227 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 228 | { | |
| 229 | gnutls_cipher_hd_t handle; | |
| 230 | int ret; | |
| 231 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
232 | handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size); |
| 34182 | 233 | if (handle == NULL) |
| 234 | return FALSE; | |
| 235 | ||
| 34758 | 236 | ret = gnutls_cipher_encrypt2(handle, (guchar *)input, len, output, len); |
| 34182 | 237 | gnutls_cipher_deinit(handle); |
| 238 | ||
| 239 | if (ret != 0) { | |
| 240 | purple_debug_error("cipher-aes", | |
| 241 | "gnutls_cipher_encrypt2 failed: %d\n", ret); | |
| 242 | return FALSE; | |
| 243 | } | |
| 244 | ||
| 245 | return TRUE; | |
| 246 | } | |
| 247 | ||
| 248 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
249 | purple_aes_cipher_gnutls_decrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 250 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 251 | { | |
| 252 | gnutls_cipher_hd_t handle; | |
| 253 | int ret; | |
| 254 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
255 | handle = purple_aes_cipher_gnutls_crypt_init(iv, key, key_size); |
| 34182 | 256 | if (handle == NULL) |
| 257 | return FALSE; | |
| 258 | ||
| 259 | ret = gnutls_cipher_decrypt2(handle, input, len, output, len); | |
| 260 | gnutls_cipher_deinit(handle); | |
| 261 | ||
| 262 | if (ret != 0) { | |
| 263 | purple_debug_error("cipher-aes", | |
| 264 | "gnutls_cipher_decrypt2 failed: %d\n", ret); | |
| 265 | return FALSE; | |
| 266 | } | |
| 267 | ||
| 268 | return TRUE; | |
| 269 | } | |
| 270 | ||
| 271 | #endif /* PURPLE_AES_USE_GNUTLS */ | |
| 272 | ||
| 273 | #ifdef PURPLE_AES_USE_NSS | |
| 274 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
275 | typedef struct { |
| 34182 | 276 | PK11SlotInfo *slot; |
| 277 | PK11SymKey *sym_key; | |
| 278 | SECItem *sec_param; | |
| 279 | PK11Context *enc_context; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
280 | } PurpleAESCipherNSSContext; |
| 34182 | 281 | |
| 282 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
283 | purple_aes_cipher_nss_cleanup(PurpleAESCipherNSSContext *context) |
| 34182 | 284 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
285 | g_return_if_fail(context != NULL); |
| 34182 | 286 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
287 | if (context->enc_context != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
288 | PK11_DestroyContext(context->enc_context, TRUE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
289 | if (context->sec_param != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
290 | SECITEM_FreeItem(context->sec_param, TRUE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
291 | if (context->sym_key != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
292 | PK11_FreeSymKey(context->sym_key); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
293 | if (context->slot != NULL) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
294 | PK11_FreeSlot(context->slot); |
| 34182 | 295 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
296 | memset(context, 0, sizeof(PurpleAESCipherNSSContext)); |
| 34182 | 297 | } |
| 298 | ||
| 299 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
300 | purple_aes_cipher_nss_crypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 301 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size, |
| 302 | CK_ATTRIBUTE_TYPE operation) | |
| 303 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
304 | PurpleAESCipherNSSContext context; |
| 34182 | 305 | CK_MECHANISM_TYPE cipher_mech = CKM_AES_CBC; |
| 306 | SECItem key_item, iv_item; | |
| 307 | SECStatus ret; | |
| 308 | int outlen = 0; | |
| 309 | unsigned int outlen_tmp = 0; | |
| 310 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
311 | memset(&context, 0, sizeof(PurpleAESCipherNSSContext)); |
| 34182 | 312 | |
| 313 | if (NSS_NoDB_Init(NULL) != SECSuccess) { | |
| 314 | purple_debug_error("cipher-aes", | |
| 315 | "NSS_NoDB_Init failed: %d\n", PR_GetError()); | |
| 316 | return FALSE; | |
| 317 | } | |
| 318 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
319 | context.slot = PK11_GetBestSlot(cipher_mech, NULL); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
320 | if (context.slot == NULL) { |
| 34182 | 321 | purple_debug_error("cipher-aes", |
| 322 | "PK11_GetBestSlot failed: %d\n", PR_GetError()); | |
| 323 | return FALSE; | |
| 324 | } | |
| 325 | ||
| 326 | key_item.type = siBuffer; | |
| 327 | key_item.data = key; | |
| 328 | key_item.len = key_size; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
329 | context.sym_key = PK11_ImportSymKey(context.slot, cipher_mech, |
| 34182 | 330 | PK11_OriginUnwrap, CKA_ENCRYPT, &key_item, NULL); |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
331 | if (context.sym_key == NULL) { |
| 34182 | 332 | purple_debug_error("cipher-aes", |
| 333 | "PK11_ImportSymKey failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
334 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 335 | return FALSE; |
| 336 | } | |
| 337 | ||
| 338 | iv_item.type = siBuffer; | |
| 339 | iv_item.data = iv; | |
| 340 | iv_item.len = PURPLE_AES_BLOCK_SIZE; | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
341 | 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
|
342 | if (context.sec_param == NULL) { |
| 34182 | 343 | purple_debug_error("cipher-aes", |
| 344 | "PK11_ParamFromIV failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
345 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 346 | return FALSE; |
| 347 | } | |
| 348 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
349 | context.enc_context = PK11_CreateContextBySymKey(cipher_mech, operation, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
350 | context.sym_key, context.sec_param); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
351 | if (context.enc_context == NULL) { |
| 34182 | 352 | purple_debug_error("cipher-aes", |
| 353 | "PK11_CreateContextBySymKey failed: %d\n", | |
| 354 | PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
355 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 356 | return FALSE; |
| 357 | } | |
| 358 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
359 | ret = PK11_CipherOp(context.enc_context, output, &outlen, len, input, len); |
| 34182 | 360 | if (ret != SECSuccess) { |
| 361 | purple_debug_error("cipher-aes", | |
| 362 | "PK11_CipherOp failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
363 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 364 | return FALSE; |
| 365 | } | |
| 366 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
367 | ret = PK11_DigestFinal(context.enc_context, output + outlen, &outlen_tmp, |
| 34182 | 368 | len - outlen); |
| 369 | if (ret != SECSuccess) { | |
| 370 | purple_debug_error("cipher-aes", | |
| 371 | "PK11_DigestFinal failed: %d\n", PR_GetError()); | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
372 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 373 | return FALSE; |
| 374 | } | |
| 375 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
376 | purple_aes_cipher_nss_cleanup(&context); |
| 34182 | 377 | |
| 378 | outlen += outlen_tmp; | |
| 379 | if (outlen != len) { | |
| 380 | purple_debug_error("cipher-aes", | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
381 | "resulting length doesn't match: %d (expected: %lu)\n", |
| 34182 | 382 | outlen, len); |
| 383 | return FALSE; | |
| 384 | } | |
| 385 | ||
| 386 | return TRUE; | |
| 387 | } | |
| 388 | ||
| 389 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
390 | purple_aes_cipher_nss_encrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 391 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 392 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
393 | return purple_aes_cipher_nss_crypt(input, output, len, iv, key, key_size, |
| 34182 | 394 | CKA_ENCRYPT); |
| 395 | } | |
| 396 | ||
| 397 | static gboolean | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
398 | purple_aes_cipher_nss_decrypt(const guchar *input, guchar *output, size_t len, |
| 34182 | 399 | guchar iv[PURPLE_AES_BLOCK_SIZE], guchar key[32], guint key_size) |
| 400 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
401 | return purple_aes_cipher_nss_crypt(input, output, len, iv, key, key_size, |
| 34182 | 402 | CKA_DECRYPT); |
| 403 | } | |
| 404 | ||
| 405 | #endif /* PURPLE_AES_USE_NSS */ | |
| 406 | ||
| 407 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
408 | purple_aes_cipher_encrypt(PurpleCipher *cipher, const guchar input[], |
| 34182 | 409 | size_t in_len, guchar output[], size_t out_size) |
| 410 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
411 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
412 | purple_aes_cipher_crypt_func encrypt_func; |
| 34182 | 413 | guchar *input_padded; |
| 414 | size_t out_len = 0; | |
| 415 | gboolean succ; | |
| 416 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
417 | if (priv->failure) |
| 34182 | 418 | return -1; |
| 419 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
420 | input_padded = purple_aes_cipher_pad_pkcs7(input, in_len, &out_len); |
| 34182 | 421 | |
| 422 | if (out_len > out_size) { | |
| 423 | purple_debug_error("cipher-aes", "Output buffer too small\n"); | |
| 424 | memset(input_padded, 0, out_len); | |
| 425 | g_free(input_padded); | |
| 426 | return -1; | |
| 427 | } | |
| 428 | ||
| 429 | #if defined(PURPLE_AES_USE_GNUTLS) | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
430 | encrypt_func = purple_aes_cipher_gnutls_encrypt; |
| 34182 | 431 | #elif defined(PURPLE_AES_USE_NSS) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
432 | encrypt_func = purple_aes_cipher_nss_encrypt; |
| 34182 | 433 | #else |
| 434 | # error "No matching encrypt_func" | |
| 435 | #endif | |
| 436 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
437 | 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
|
438 | priv->key, priv->key_size); |
| 34182 | 439 | |
| 440 | memset(input_padded, 0, out_len); | |
| 441 | g_free(input_padded); | |
| 442 | ||
| 443 | if (!succ) { | |
| 444 | memset(output, 0, out_len); | |
| 445 | return -1; | |
| 446 | } | |
| 447 | ||
| 448 | return out_len; | |
| 449 | } | |
| 450 | ||
| 451 | static ssize_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
452 | purple_aes_cipher_decrypt(PurpleCipher *cipher, const guchar input[], |
| 34182 | 453 | size_t in_len, guchar output[], size_t out_size) |
| 454 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
455 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
456 | purple_aes_cipher_crypt_func decrypt_func; |
| 34182 | 457 | gboolean succ; |
| 458 | ssize_t out_len; | |
| 459 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
460 | if (priv->failure) |
| 34182 | 461 | return -1; |
| 462 | ||
| 463 | if (in_len > out_size) { | |
| 464 | purple_debug_error("cipher-aes", "Output buffer too small\n"); | |
| 465 | return -1; | |
| 466 | } | |
| 467 | ||
| 468 | if ((in_len % PURPLE_AES_BLOCK_SIZE) != 0 || in_len == 0) { | |
| 469 | purple_debug_error("cipher-aes", "Malformed data\n"); | |
| 470 | return -1; | |
| 471 | } | |
| 472 | ||
| 473 | #if defined(PURPLE_AES_USE_GNUTLS) | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
474 | decrypt_func = purple_aes_cipher_gnutls_decrypt; |
| 34182 | 475 | #elif defined(PURPLE_AES_USE_NSS) |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
476 | decrypt_func = purple_aes_cipher_nss_decrypt; |
| 34182 | 477 | #else |
| 478 | # error "No matching encrypt_func" | |
| 479 | #endif | |
| 480 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
481 | 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
|
482 | priv->key_size); |
| 34182 | 483 | |
| 484 | if (!succ) { | |
| 485 | memset(output, 0, in_len); | |
| 486 | return -1; | |
| 487 | } | |
| 488 | ||
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
489 | out_len = purple_aes_cipher_unpad_pkcs7(output, in_len); |
| 34182 | 490 | if (out_len < 0) { |
| 491 | memset(output, 0, in_len); | |
| 492 | return -1; | |
| 493 | } | |
| 494 | ||
| 495 | return out_len; | |
| 496 | } | |
| 497 | ||
| 498 | static size_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
499 | purple_aes_cipher_get_key_size(PurpleCipher *cipher) |
| 34182 | 500 | { |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
501 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 502 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
503 | return priv->key_size; |
| 34182 | 504 | } |
| 505 | ||
| 506 | static void | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
507 | purple_aes_cipher_set_batch_mode(PurpleCipher *cipher, |
| 34182 | 508 | PurpleCipherBatchMode mode) |
| 509 | { | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
510 | PurpleAESCipherPrivate *priv = PURPLE_AES_CIPHER_GET_PRIVATE(cipher); |
| 34182 | 511 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
512 | if (mode == PURPLE_CIPHER_BATCH_MODE_CBC) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
513 | g_object_notify(G_OBJECT(cipher), "batch_mode"); |
| 34182 | 514 | return; |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
515 | } |
| 34182 | 516 | |
| 517 | 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
|
518 | priv->failure = TRUE; |
| 34182 | 519 | } |
| 520 | ||
| 521 | static PurpleCipherBatchMode | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
522 | purple_aes_cipher_get_batch_mode(PurpleCipher *cipher) |
| 34182 | 523 | { |
| 524 | return PURPLE_CIPHER_BATCH_MODE_CBC; | |
| 525 | } | |
| 526 | ||
| 527 | static size_t | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
528 | purple_aes_cipher_get_block_size(PurpleCipher *cipher) |
| 34182 | 529 | { |
| 530 | return PURPLE_AES_BLOCK_SIZE; | |
| 531 | } | |
| 532 | ||
|
34547
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
533 | static const gchar* |
|
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
534 | purple_aes_cipher_get_name(PurpleCipher *cipher) |
|
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
535 | { |
|
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
536 | return "aes"; |
|
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
537 | } |
|
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
538 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
539 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
540 | * Object Stuff |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
541 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
542 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
543 | 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
|
544 | GParamSpec *pspec) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
545 | { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
546 | PurpleCipher *cipher = PURPLE_CIPHER(obj); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
547 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
548 | switch(param_id) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
549 | case PROP_BATCH_MODE: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
550 | g_value_set_enum(value, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
551 | purple_cipher_get_batch_mode(cipher)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
552 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
553 | default: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
554 | 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
|
555 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
556 | } |
|
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 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
559 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
560 | 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
|
561 | const GValue *value, GParamSpec *pspec) |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
562 | { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
563 | PurpleCipher *cipher = PURPLE_CIPHER(obj); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
564 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
565 | switch(param_id) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
566 | case PROP_BATCH_MODE: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
567 | purple_cipher_set_batch_mode(cipher, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
568 | g_value_get_enum(value)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
569 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
570 | case PROP_IV: |
|
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 | guchar *iv = (guchar *)g_value_get_string(value); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
573 | 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
|
574 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
575 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
576 | case PROP_KEY: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
577 | 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
|
578 | purple_aes_cipher_get_key_size(cipher)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
579 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
580 | default: |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
581 | 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
|
582 | break; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
583 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
584 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
585 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
586 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
587 | purple_aes_cipher_class_init(PurpleAESCipherClass *klass) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
588 | GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
589 | PurpleCipherClass *cipher_class = PURPLE_CIPHER_CLASS(klass); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
590 | GParamSpec *pspec; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
591 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
592 | 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
|
593 | obj_class->set_property = purple_aes_cipher_set_property; |
| 34182 | 594 | |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
595 | cipher_class->reset = purple_aes_cipher_reset; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
596 | 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
|
597 | cipher_class->encrypt = purple_aes_cipher_encrypt; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
598 | cipher_class->decrypt = purple_aes_cipher_decrypt; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
599 | 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
|
600 | 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
|
601 | 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
|
602 | 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
|
603 | cipher_class->get_block_size = purple_aes_cipher_get_block_size; |
|
34547
45b72366ddde
Added get_name method to all PurpleCiphers
Ankit Vani <a@nevitus.org>
parents:
34546
diff
changeset
|
604 | cipher_class->get_name = purple_aes_cipher_get_name; |
|
34546
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 | 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
|
607 | PURPLE_TYPE_CIPHER_BATCH_MODE, 0, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
608 | G_PARAM_READWRITE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
609 | 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
|
610 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
611 | 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
|
612 | G_PARAM_WRITABLE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
613 | 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
|
614 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
615 | 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
|
616 | G_PARAM_WRITABLE); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
617 | 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
|
618 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
619 | g_type_class_add_private(klass, sizeof(PurpleAESCipherPrivate)); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
620 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
621 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
622 | static void |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
623 | purple_aes_cipher_init(PurpleCipher *cipher) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
624 | purple_cipher_reset(cipher); |
| 34182 | 625 | } |
|
34546
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
626 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
627 | /****************************************************************************** |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
628 | * API |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
629 | *****************************************************************************/ |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
630 | GType |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
631 | purple_aes_cipher_get_gtype(void) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
632 | static GType type = 0; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
633 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
634 | if(type == 0) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
635 | static const GTypeInfo info = { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
636 | sizeof(PurpleAESCipherClass), |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
637 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
638 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
639 | (GClassInitFunc)purple_aes_cipher_class_init, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
640 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
641 | NULL, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
642 | sizeof(PurpleAESCipher), |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
643 | 0, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
644 | (GInstanceInitFunc)purple_aes_cipher_init, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
645 | NULL |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
646 | }; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
647 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
648 | type = g_type_register_static(PURPLE_TYPE_CIPHER, |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
649 | "PurpleAESCipher", |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
650 | &info, 0); |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
651 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
652 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
653 | return type; |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
654 | } |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
655 | |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
656 | PurpleCipher * |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
657 | purple_aes_cipher_new(void) { |
|
5c2f894fe4d6
GObjectified the AES cipher as PurpleAESCipher
Ankit Vani <a@nevitus.org>
parents:
34182
diff
changeset
|
658 | 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
|
659 | } |