| 1 /* purple |
|
| 2 * |
|
| 3 * Purple is the legal property of its developers, whose names are too numerous |
|
| 4 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 5 * source distribution. |
|
| 6 * |
|
| 7 * This program is free software; you can redistribute it and/or modify |
|
| 8 * it under the terms of the GNU General Public License as published by |
|
| 9 * the Free Software Foundation; either version 2 of the License, or |
|
| 10 * (at your option) any later version. |
|
| 11 * |
|
| 12 * This program is distributed in the hope that it will be useful, |
|
| 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 15 * GNU General Public License for more details. |
|
| 16 * |
|
| 17 * You should have received a copy of the GNU General Public License |
|
| 18 * along with this program; if not, write to the Free Software |
|
| 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 20 */ |
|
| 21 #include "internal.h" |
|
| 22 #include "cipher.h" |
|
| 23 #include "debug.h" |
|
| 24 |
|
| 25 /****************************************************************************** |
|
| 26 * PurpleCipher API |
|
| 27 *****************************************************************************/ |
|
| 28 GType |
|
| 29 purple_cipher_get_type(void) { |
|
| 30 static GType type = 0; |
|
| 31 |
|
| 32 if(type == 0) { |
|
| 33 static const GTypeInfo info = { |
|
| 34 sizeof(PurpleCipherClass), |
|
| 35 NULL, |
|
| 36 NULL, |
|
| 37 NULL, |
|
| 38 NULL, |
|
| 39 NULL, |
|
| 40 sizeof(PurpleCipher), |
|
| 41 0, |
|
| 42 NULL, |
|
| 43 NULL |
|
| 44 }; |
|
| 45 |
|
| 46 type = g_type_register_static(G_TYPE_OBJECT, |
|
| 47 "PurpleCipher", |
|
| 48 &info, G_TYPE_FLAG_ABSTRACT); |
|
| 49 } |
|
| 50 |
|
| 51 return type; |
|
| 52 } |
|
| 53 |
|
| 54 static const gchar * |
|
| 55 purple_cipher_get_name(PurpleCipher *cipher) |
|
| 56 { |
|
| 57 PurpleCipherClass *klass; |
|
| 58 const gchar *name; |
|
| 59 |
|
| 60 if (!PURPLE_IS_CIPHER(cipher)) |
|
| 61 return "(error: not a cipher)"; |
|
| 62 |
|
| 63 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 64 if (!klass) |
|
| 65 return "(error: unknown cipher class)"; |
|
| 66 |
|
| 67 name = g_type_name(G_TYPE_FROM_CLASS(klass)); |
|
| 68 if (!name) |
|
| 69 return "(error: unknown cipher name)"; |
|
| 70 |
|
| 71 return name; |
|
| 72 } |
|
| 73 |
|
| 74 static const gchar * |
|
| 75 purple_hash_get_name(PurpleHash *hash) |
|
| 76 { |
|
| 77 PurpleHashClass *klass; |
|
| 78 const gchar *name; |
|
| 79 |
|
| 80 if (!PURPLE_IS_HASH(hash)) |
|
| 81 return "(error: not a hash)"; |
|
| 82 |
|
| 83 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 84 if (!klass) |
|
| 85 return "(error: unknown hash class)"; |
|
| 86 |
|
| 87 name = g_type_name(G_TYPE_FROM_CLASS(klass)); |
|
| 88 if (!name) |
|
| 89 return "(error: unknown hash name)"; |
|
| 90 |
|
| 91 return name; |
|
| 92 } |
|
| 93 |
|
| 94 void |
|
| 95 purple_cipher_reset(PurpleCipher *cipher) { |
|
| 96 PurpleCipherClass *klass = NULL; |
|
| 97 |
|
| 98 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 99 |
|
| 100 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 101 |
|
| 102 if (klass && klass->reset) |
|
| 103 klass->reset(cipher); |
|
| 104 else { |
|
| 105 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 106 "implement the reset method", |
|
| 107 purple_cipher_get_name(cipher)); |
|
| 108 } |
|
| 109 } |
|
| 110 |
|
| 111 void |
|
| 112 purple_cipher_reset_state(PurpleCipher *cipher) { |
|
| 113 PurpleCipherClass *klass = NULL; |
|
| 114 |
|
| 115 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 116 |
|
| 117 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 118 |
|
| 119 if (klass && klass->reset_state) |
|
| 120 klass->reset_state(cipher); |
|
| 121 else { |
|
| 122 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 123 "implement the reset_state method", |
|
| 124 purple_cipher_get_name(cipher)); |
|
| 125 } |
|
| 126 } |
|
| 127 |
|
| 128 void |
|
| 129 purple_cipher_set_iv(PurpleCipher *cipher, guchar *iv, size_t len) |
|
| 130 { |
|
| 131 PurpleCipherClass *klass = NULL; |
|
| 132 |
|
| 133 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 134 g_return_if_fail(iv); |
|
| 135 |
|
| 136 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 137 |
|
| 138 if (klass && klass->set_iv) |
|
| 139 klass->set_iv(cipher, iv, len); |
|
| 140 else { |
|
| 141 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 142 "implement the set_iv method", |
|
| 143 purple_cipher_get_name(cipher)); |
|
| 144 } |
|
| 145 } |
|
| 146 |
|
| 147 void |
|
| 148 purple_cipher_append(PurpleCipher *cipher, const guchar *data, |
|
| 149 size_t len) |
|
| 150 { |
|
| 151 PurpleCipherClass *klass = NULL; |
|
| 152 |
|
| 153 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 154 |
|
| 155 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 156 |
|
| 157 if (klass && klass->append) |
|
| 158 klass->append(cipher, data, len); |
|
| 159 else { |
|
| 160 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 161 "implement the append method", |
|
| 162 purple_cipher_get_name(cipher)); |
|
| 163 } |
|
| 164 } |
|
| 165 |
|
| 166 gboolean |
|
| 167 purple_cipher_digest(PurpleCipher *cipher, guchar digest[], size_t len) |
|
| 168 { |
|
| 169 PurpleCipherClass *klass = NULL; |
|
| 170 |
|
| 171 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), FALSE); |
|
| 172 |
|
| 173 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 174 |
|
| 175 if (klass && klass->digest) |
|
| 176 return klass->digest(cipher, digest, len); |
|
| 177 else { |
|
| 178 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 179 "implement the digest method", |
|
| 180 purple_cipher_get_name(cipher)); |
|
| 181 } |
|
| 182 |
|
| 183 return FALSE; |
|
| 184 } |
|
| 185 |
|
| 186 gboolean |
|
| 187 purple_cipher_digest_to_str(PurpleCipher *cipher, gchar digest_s[], size_t len) |
|
| 188 { |
|
| 189 /* 8k is a bit excessive, will tweak later. */ |
|
| 190 guchar digest[BUF_LEN * 4]; |
|
| 191 size_t digest_size, n; |
|
| 192 |
|
| 193 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), FALSE); |
|
| 194 g_return_val_if_fail(digest_s, FALSE); |
|
| 195 |
|
| 196 digest_size = purple_cipher_get_digest_size(cipher); |
|
| 197 |
|
| 198 g_return_val_if_fail(digest_size <= BUF_LEN * 4, FALSE); |
|
| 199 |
|
| 200 if(!purple_cipher_digest(cipher, digest, sizeof(digest))) |
|
| 201 return FALSE; |
|
| 202 |
|
| 203 /* Every digest byte occupies 2 chars + the NUL at the end. */ |
|
| 204 g_return_val_if_fail(digest_size * 2 + 1 <= len, FALSE); |
|
| 205 |
|
| 206 for(n = 0; n < digest_size; n++) |
|
| 207 sprintf(digest_s + (n * 2), "%02x", digest[n]); |
|
| 208 |
|
| 209 digest_s[n * 2] = '\0'; |
|
| 210 |
|
| 211 return TRUE; |
|
| 212 } |
|
| 213 |
|
| 214 size_t |
|
| 215 purple_cipher_get_digest_size(PurpleCipher *cipher) |
|
| 216 { |
|
| 217 PurpleCipherClass *klass = NULL; |
|
| 218 |
|
| 219 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), FALSE); |
|
| 220 |
|
| 221 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 222 |
|
| 223 if (klass && klass->get_digest_size) |
|
| 224 return klass->get_digest_size(cipher); |
|
| 225 else { |
|
| 226 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 227 "implement the get_digest_size method", |
|
| 228 purple_cipher_get_name(cipher)); |
|
| 229 } |
|
| 230 |
|
| 231 return FALSE; |
|
| 232 } |
|
| 233 |
|
| 234 ssize_t |
|
| 235 purple_cipher_encrypt(PurpleCipher *cipher, const guchar input[], |
|
| 236 size_t in_len, guchar output[], size_t out_size) |
|
| 237 { |
|
| 238 PurpleCipherClass *klass = NULL; |
|
| 239 |
|
| 240 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1); |
|
| 241 g_return_val_if_fail(input != NULL, -1); |
|
| 242 g_return_val_if_fail(output != NULL, -1); |
|
| 243 g_return_val_if_fail(out_size >= in_len, -1); |
|
| 244 |
|
| 245 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 246 |
|
| 247 if (klass && klass->encrypt) |
|
| 248 return klass->encrypt(cipher, input, in_len, output, out_size); |
|
| 249 else { |
|
| 250 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 251 "implement the encrypt method", |
|
| 252 purple_cipher_get_name(cipher)); |
|
| 253 } |
|
| 254 |
|
| 255 return -1; |
|
| 256 } |
|
| 257 |
|
| 258 ssize_t |
|
| 259 purple_cipher_decrypt(PurpleCipher *cipher, const guchar input[], |
|
| 260 size_t in_len, guchar output[], size_t out_size) |
|
| 261 { |
|
| 262 PurpleCipherClass *klass = NULL; |
|
| 263 |
|
| 264 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1); |
|
| 265 g_return_val_if_fail(input != NULL, -1); |
|
| 266 g_return_val_if_fail(output != NULL, -1); |
|
| 267 |
|
| 268 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 269 |
|
| 270 if (klass && klass->decrypt) |
|
| 271 return klass->decrypt(cipher, input, in_len, output, out_size); |
|
| 272 else { |
|
| 273 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 274 "implement the decrypt method", |
|
| 275 purple_cipher_get_name(cipher)); |
|
| 276 } |
|
| 277 |
|
| 278 return -1; |
|
| 279 } |
|
| 280 |
|
| 281 void |
|
| 282 purple_cipher_set_salt(PurpleCipher *cipher, const guchar *salt, size_t len) { |
|
| 283 PurpleCipherClass *klass = NULL; |
|
| 284 |
|
| 285 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 286 |
|
| 287 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 288 |
|
| 289 if (klass && klass->set_salt) |
|
| 290 klass->set_salt(cipher, salt, len); |
|
| 291 else { |
|
| 292 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 293 "implement the set_salt method", |
|
| 294 purple_cipher_get_name(cipher)); |
|
| 295 } |
|
| 296 } |
|
| 297 |
|
| 298 void |
|
| 299 purple_cipher_set_key(PurpleCipher *cipher, const guchar *key, size_t len) { |
|
| 300 PurpleCipherClass *klass = NULL; |
|
| 301 |
|
| 302 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 303 |
|
| 304 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 305 |
|
| 306 if (klass && klass->set_key) |
|
| 307 klass->set_key(cipher, key, len); |
|
| 308 else { |
|
| 309 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 310 "implement the set_key method", |
|
| 311 purple_cipher_get_name(cipher)); |
|
| 312 } |
|
| 313 } |
|
| 314 |
|
| 315 size_t |
|
| 316 purple_cipher_get_key_size(PurpleCipher *cipher) { |
|
| 317 PurpleCipherClass *klass = NULL; |
|
| 318 |
|
| 319 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1); |
|
| 320 |
|
| 321 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 322 |
|
| 323 if (klass && klass->get_key_size) |
|
| 324 return klass->get_key_size(cipher); |
|
| 325 else { |
|
| 326 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 327 "implement the get_key_size method", |
|
| 328 purple_cipher_get_name(cipher)); |
|
| 329 } |
|
| 330 |
|
| 331 return -1; |
|
| 332 } |
|
| 333 |
|
| 334 void |
|
| 335 purple_cipher_set_batch_mode(PurpleCipher *cipher, |
|
| 336 PurpleCipherBatchMode mode) |
|
| 337 { |
|
| 338 PurpleCipherClass *klass = NULL; |
|
| 339 |
|
| 340 g_return_if_fail(PURPLE_IS_CIPHER(cipher)); |
|
| 341 |
|
| 342 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 343 |
|
| 344 if (klass && klass->set_batch_mode) |
|
| 345 klass->set_batch_mode(cipher, mode); |
|
| 346 else { |
|
| 347 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 348 "implement the set_batch_mode method", |
|
| 349 purple_cipher_get_name(cipher)); |
|
| 350 } |
|
| 351 } |
|
| 352 |
|
| 353 PurpleCipherBatchMode |
|
| 354 purple_cipher_get_batch_mode(PurpleCipher *cipher) |
|
| 355 { |
|
| 356 PurpleCipherClass *klass = NULL; |
|
| 357 |
|
| 358 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1); |
|
| 359 |
|
| 360 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 361 |
|
| 362 if (klass && klass->get_batch_mode) |
|
| 363 return klass->get_batch_mode(cipher); |
|
| 364 else { |
|
| 365 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 366 "implement the get_batch_mode method", |
|
| 367 purple_cipher_get_name(cipher)); |
|
| 368 } |
|
| 369 |
|
| 370 return -1; |
|
| 371 } |
|
| 372 |
|
| 373 size_t |
|
| 374 purple_cipher_get_block_size(PurpleCipher *cipher) |
|
| 375 { |
|
| 376 PurpleCipherClass *klass = NULL; |
|
| 377 |
|
| 378 g_return_val_if_fail(PURPLE_IS_CIPHER(cipher), -1); |
|
| 379 |
|
| 380 klass = PURPLE_CIPHER_GET_CLASS(cipher); |
|
| 381 |
|
| 382 if (klass && klass->get_block_size) |
|
| 383 return klass->get_block_size(cipher); |
|
| 384 else { |
|
| 385 purple_debug_warning("cipher", "the %s cipher does not " |
|
| 386 "implement the get_block_size method", |
|
| 387 purple_cipher_get_name(cipher)); |
|
| 388 } |
|
| 389 |
|
| 390 return -1; |
|
| 391 } |
|
| 392 |
|
| 393 /****************************************************************************** |
|
| 394 * PurpleHash API |
|
| 395 *****************************************************************************/ |
|
| 396 GType |
|
| 397 purple_hash_get_type(void) { |
|
| 398 static GType type = 0; |
|
| 399 |
|
| 400 if(type == 0) { |
|
| 401 static const GTypeInfo info = { |
|
| 402 sizeof(PurpleHashClass), |
|
| 403 NULL, |
|
| 404 NULL, |
|
| 405 NULL, |
|
| 406 NULL, |
|
| 407 NULL, |
|
| 408 sizeof(PurpleHash), |
|
| 409 0, |
|
| 410 NULL, |
|
| 411 NULL |
|
| 412 }; |
|
| 413 |
|
| 414 type = g_type_register_static(G_TYPE_OBJECT, |
|
| 415 "PurpleHash", |
|
| 416 &info, G_TYPE_FLAG_ABSTRACT); |
|
| 417 } |
|
| 418 |
|
| 419 return type; |
|
| 420 } |
|
| 421 |
|
| 422 void |
|
| 423 purple_hash_reset(PurpleHash *hash) { |
|
| 424 PurpleHashClass *klass = NULL; |
|
| 425 |
|
| 426 g_return_if_fail(PURPLE_IS_HASH(hash)); |
|
| 427 |
|
| 428 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 429 |
|
| 430 if (klass && klass->reset) |
|
| 431 klass->reset(hash); |
|
| 432 else { |
|
| 433 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 434 "the reset method", purple_hash_get_name(hash)); |
|
| 435 } |
|
| 436 } |
|
| 437 |
|
| 438 void |
|
| 439 purple_hash_reset_state(PurpleHash *hash) { |
|
| 440 PurpleHashClass *klass = NULL; |
|
| 441 |
|
| 442 g_return_if_fail(PURPLE_IS_HASH(hash)); |
|
| 443 |
|
| 444 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 445 |
|
| 446 if (klass && klass->reset_state) |
|
| 447 klass->reset_state(hash); |
|
| 448 else { |
|
| 449 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 450 "the reset_state method", purple_hash_get_name(hash)); |
|
| 451 } |
|
| 452 } |
|
| 453 |
|
| 454 void |
|
| 455 purple_hash_append(PurpleHash *hash, const guchar *data, |
|
| 456 size_t len) |
|
| 457 { |
|
| 458 PurpleHashClass *klass = NULL; |
|
| 459 |
|
| 460 g_return_if_fail(PURPLE_IS_HASH(hash)); |
|
| 461 |
|
| 462 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 463 |
|
| 464 if (klass && klass->append) |
|
| 465 klass->append(hash, data, len); |
|
| 466 else { |
|
| 467 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 468 "the append method", purple_hash_get_name(hash)); |
|
| 469 } |
|
| 470 } |
|
| 471 |
|
| 472 gboolean |
|
| 473 purple_hash_digest(PurpleHash *hash, guchar digest[], size_t len) |
|
| 474 { |
|
| 475 PurpleHashClass *klass = NULL; |
|
| 476 |
|
| 477 g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE); |
|
| 478 |
|
| 479 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 480 |
|
| 481 if (klass && klass->digest) |
|
| 482 return klass->digest(hash, digest, len); |
|
| 483 else { |
|
| 484 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 485 "the digest method", purple_hash_get_name(hash)); |
|
| 486 } |
|
| 487 |
|
| 488 return FALSE; |
|
| 489 } |
|
| 490 |
|
| 491 gboolean |
|
| 492 purple_hash_digest_to_str(PurpleHash *hash, gchar digest_s[], size_t len) |
|
| 493 { |
|
| 494 /* 8k is a bit excessive, will tweak later. */ |
|
| 495 guchar digest[BUF_LEN * 4]; |
|
| 496 size_t digest_size, n; |
|
| 497 |
|
| 498 g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE); |
|
| 499 g_return_val_if_fail(digest_s, FALSE); |
|
| 500 |
|
| 501 digest_size = purple_hash_get_digest_size(hash); |
|
| 502 |
|
| 503 g_return_val_if_fail(digest_size <= BUF_LEN * 4, FALSE); |
|
| 504 |
|
| 505 if(!purple_hash_digest(hash, digest, sizeof(digest))) |
|
| 506 return FALSE; |
|
| 507 |
|
| 508 /* Every digest byte occupies 2 chars + the NUL at the end. */ |
|
| 509 g_return_val_if_fail(digest_size * 2 + 1 <= len, FALSE); |
|
| 510 |
|
| 511 for(n = 0; n < digest_size; n++) |
|
| 512 sprintf(digest_s + (n * 2), "%02x", digest[n]); |
|
| 513 |
|
| 514 digest_s[n * 2] = '\0'; |
|
| 515 |
|
| 516 return TRUE; |
|
| 517 } |
|
| 518 |
|
| 519 size_t |
|
| 520 purple_hash_get_digest_size(PurpleHash *hash) |
|
| 521 { |
|
| 522 PurpleHashClass *klass = NULL; |
|
| 523 |
|
| 524 g_return_val_if_fail(PURPLE_IS_HASH(hash), FALSE); |
|
| 525 |
|
| 526 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 527 |
|
| 528 if (klass && klass->get_digest_size) |
|
| 529 return klass->get_digest_size(hash); |
|
| 530 else { |
|
| 531 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 532 "the get_digest_size method", purple_hash_get_name(hash)); |
|
| 533 } |
|
| 534 |
|
| 535 return FALSE; |
|
| 536 } |
|
| 537 |
|
| 538 size_t |
|
| 539 purple_hash_get_block_size(PurpleHash *hash) |
|
| 540 { |
|
| 541 PurpleHashClass *klass = NULL; |
|
| 542 |
|
| 543 g_return_val_if_fail(PURPLE_IS_HASH(hash), -1); |
|
| 544 |
|
| 545 klass = PURPLE_HASH_GET_CLASS(hash); |
|
| 546 |
|
| 547 if (klass && klass->get_block_size) |
|
| 548 return klass->get_block_size(hash); |
|
| 549 else { |
|
| 550 purple_debug_warning("hash", "the %s hash does not implement " |
|
| 551 "the get_block_size method", purple_hash_get_name(hash)); |
|
| 552 } |
|
| 553 |
|
| 554 return -1; |
|
| 555 } |
|