comparison src/cipher.c @ 11183:8dca96cbcd64

[gaim-migrate @ 13295] I changed the cipher API to use guchar instead of guint8 This seems to be what gtk/glib uses for random bits of data I don't know what got into me committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Wed, 03 Aug 2005 02:57:00 +0000
parents 5c56223fa24f
children f93d434ee222
comparison
equal deleted inserted replaced
11182:5389d7d497ce 11183:8dca96cbcd64
36 * MD5 36 * MD5
37 ******************************************************************************/ 37 ******************************************************************************/
38 struct MD5Context { 38 struct MD5Context {
39 guint32 total[2]; 39 guint32 total[2];
40 guint32 state[4]; 40 guint32 state[4];
41 guint8 buffer[64]; 41 guchar buffer[64];
42 }; 42 };
43 43
44 #define MD5_GET_GUINT32(n,b,i) { \ 44 #define MD5_GET_GUINT32(n,b,i) { \
45 (n) = ((guint32)(b) [(i) ] ) \ 45 (n) = ((guint32)(b) [(i) ] ) \
46 | ((guint32)(b) [(i) + 1] << 8) \ 46 | ((guint32)(b) [(i) + 1] << 8) \
47 | ((guint32)(b) [(i) + 2] << 16) \ 47 | ((guint32)(b) [(i) + 2] << 16) \
48 | ((guint32)(b) [(i) + 3] << 24); \ 48 | ((guint32)(b) [(i) + 3] << 24); \
49 } 49 }
50 #define MD5_PUT_GUINT32(n,b,i) { \ 50 #define MD5_PUT_GUINT32(n,b,i) { \
51 (b)[(i) ] = (guint8)((n) ); \ 51 (b)[(i) ] = (guchar)((n) ); \
52 (b)[(i) + 1] = (guint8)((n) >> 8); \ 52 (b)[(i) + 1] = (guchar)((n) >> 8); \
53 (b)[(i) + 2] = (guint8)((n) >> 16); \ 53 (b)[(i) + 2] = (guchar)((n) >> 16); \
54 (b)[(i) + 3] = (guint8)((n) >> 24); \ 54 (b)[(i) + 3] = (guchar)((n) >> 24); \
55 } 55 }
56 56
57 static void 57 static void
58 md5_init(GaimCipherContext *context, gpointer extra) { 58 md5_init(GaimCipherContext *context, gpointer extra) {
59 struct MD5Context *md5_context; 59 struct MD5Context *md5_context;
94 g_free(md5_context); 94 g_free(md5_context);
95 md5_context = NULL; 95 md5_context = NULL;
96 } 96 }
97 97
98 static void 98 static void
99 md5_process(struct MD5Context *md5_context, const guint8 data[64]) { 99 md5_process(struct MD5Context *md5_context, const guchar data[64]) {
100 guint32 X[16], A, B, C, D; 100 guint32 X[16], A, B, C, D;
101 101
102 A = md5_context->state[0]; 102 A = md5_context->state[0];
103 B = md5_context->state[1]; 103 B = md5_context->state[1];
104 C = md5_context->state[2]; 104 C = md5_context->state[2];
164 P(A, B, C, D, 13, 5, 0xA9E3E905); 164 P(A, B, C, D, 13, 5, 0xA9E3E905);
165 P(D, A, B, C, 2, 9, 0xFCEFA3F8); 165 P(D, A, B, C, 2, 9, 0xFCEFA3F8);
166 P(C, D, A, B, 7, 14, 0x676F02D9); 166 P(C, D, A, B, 7, 14, 0x676F02D9);
167 P(B, C, D, A, 12, 20, 0x8D2A4C8A); 167 P(B, C, D, A, 12, 20, 0x8D2A4C8A);
168 #undef F 168 #undef F
169 169
170 /* third pass */ 170 /* third pass */
171 #define F(x,y,z) (x ^ y ^ z) 171 #define F(x,y,z) (x ^ y ^ z)
172 P(A, B, C, D, 5, 4, 0xFFFA3942); 172 P(A, B, C, D, 5, 4, 0xFFFA3942);
173 P(D, A, B, C, 8, 11, 0x8771F681); 173 P(D, A, B, C, 8, 11, 0x8771F681);
174 P(C, D, A, B, 11, 16, 0x6D9D6122); 174 P(C, D, A, B, 11, 16, 0x6D9D6122);
214 md5_context->state[2] += C; 214 md5_context->state[2] += C;
215 md5_context->state[3] += D; 215 md5_context->state[3] += D;
216 } 216 }
217 217
218 static void 218 static void
219 md5_append(GaimCipherContext *context, const guint8 *data, size_t len) { 219 md5_append(GaimCipherContext *context, const guchar *data, size_t len) {
220 struct MD5Context *md5_context = NULL; 220 struct MD5Context *md5_context = NULL;
221 guint32 left = 0, fill = 0; 221 guint32 left = 0, fill = 0;
222 222
223 g_return_if_fail(context != NULL); 223 g_return_if_fail(context != NULL);
224 224
252 memcpy((md5_context->buffer + left), data, len); 252 memcpy((md5_context->buffer + left), data, len);
253 } 253 }
254 } 254 }
255 255
256 static gboolean 256 static gboolean
257 md5_digest(GaimCipherContext *context, size_t in_len, guint8 digest[16], 257 md5_digest(GaimCipherContext *context, size_t in_len, guchar digest[16],
258 size_t *out_len) 258 size_t *out_len)
259 { 259 {
260 struct MD5Context *md5_context = NULL; 260 struct MD5Context *md5_context = NULL;
261 guint32 last, pad; 261 guint32 last, pad;
262 guint32 high, low; 262 guint32 high, low;
263 guint8 message[8]; 263 guchar message[8];
264 guint8 padding[64] = { 264 guchar padding[64] = {
265 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 265 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 266 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 267 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
269 }; 269 };
384 384
385 sha1_ctx->H[0] += A; 385 sha1_ctx->H[0] += A;
386 sha1_ctx->H[1] += B; 386 sha1_ctx->H[1] += B;
387 sha1_ctx->H[2] += C; 387 sha1_ctx->H[2] += C;
388 sha1_ctx->H[3] += D; 388 sha1_ctx->H[3] += D;
389 sha1_ctx->H[4] += E; 389 sha1_ctx->H[4] += E;
390 } 390 }
391 391
392 static void 392 static void
393 sha1_set_opt(GaimCipherContext *context, const gchar *name, void *value) { 393 sha1_set_opt(GaimCipherContext *context, const gchar *name, void *value) {
394 struct SHA1Context *ctx; 394 struct SHA1Context *ctx;
469 sha1_ctx = NULL; 469 sha1_ctx = NULL;
470 } 470 }
471 471
472 472
473 static void 473 static void
474 sha1_append(GaimCipherContext *context, const guint8 *data, size_t len) { 474 sha1_append(GaimCipherContext *context, const guchar *data, size_t len) {
475 struct SHA1Context *sha1_ctx; 475 struct SHA1Context *sha1_ctx;
476 gint i; 476 gint i;
477 477
478 sha1_ctx = gaim_cipher_context_get_data(context); 478 sha1_ctx = gaim_cipher_context_get_data(context);
479 479
492 sha1_ctx->sizeHi += (sha1_ctx->sizeLo < 8); 492 sha1_ctx->sizeHi += (sha1_ctx->sizeLo < 8);
493 } 493 }
494 } 494 }
495 495
496 static gboolean 496 static gboolean
497 sha1_digest(GaimCipherContext *context, size_t in_len, guint8 digest[20], 497 sha1_digest(GaimCipherContext *context, size_t in_len, guchar digest[20],
498 size_t *out_len) 498 size_t *out_len)
499 { 499 {
500 struct SHA1Context *sha1_ctx; 500 struct SHA1Context *sha1_ctx;
501 guint8 pad0x80 = 0x80, pad0x00 = 0x00; 501 guchar pad0x80 = 0x80, pad0x00 = 0x00;
502 guint8 padlen[8]; 502 guchar padlen[8];
503 gint i; 503 gint i;
504 504
505 g_return_val_if_fail(in_len >= 20, FALSE); 505 g_return_val_if_fail(in_len >= 20, FALSE);
506 506
507 sha1_ctx = gaim_cipher_context_get_data(context); 507 sha1_ctx = gaim_cipher_context_get_data(context);
508 508
509 g_return_val_if_fail(sha1_ctx, FALSE); 509 g_return_val_if_fail(sha1_ctx, FALSE);
510 510
511 padlen[0] = (guint8)((sha1_ctx->sizeHi >> 24) & 255); 511 padlen[0] = (guchar)((sha1_ctx->sizeHi >> 24) & 255);
512 padlen[1] = (guint8)((sha1_ctx->sizeHi >> 16) & 255); 512 padlen[1] = (guchar)((sha1_ctx->sizeHi >> 16) & 255);
513 padlen[2] = (guint8)((sha1_ctx->sizeHi >> 8) & 255); 513 padlen[2] = (guchar)((sha1_ctx->sizeHi >> 8) & 255);
514 padlen[3] = (guint8)((sha1_ctx->sizeHi >> 0) & 255); 514 padlen[3] = (guchar)((sha1_ctx->sizeHi >> 0) & 255);
515 padlen[4] = (guint8)((sha1_ctx->sizeLo >> 24) & 255); 515 padlen[4] = (guchar)((sha1_ctx->sizeLo >> 24) & 255);
516 padlen[5] = (guint8)((sha1_ctx->sizeLo >> 16) & 255); 516 padlen[5] = (guchar)((sha1_ctx->sizeLo >> 16) & 255);
517 padlen[6] = (guint8)((sha1_ctx->sizeLo >> 8) & 255); 517 padlen[6] = (guchar)((sha1_ctx->sizeLo >> 8) & 255);
518 padlen[7] = (guint8)((sha1_ctx->sizeLo >> 0) & 255); 518 padlen[7] = (guchar)((sha1_ctx->sizeLo >> 0) & 255);
519 519
520 /* pad with a 1, then zeroes, then length */ 520 /* pad with a 1, then zeroes, then length */
521 gaim_cipher_context_append(context, &pad0x80, 1); 521 gaim_cipher_context_append(context, &pad0x80, 1);
522 while(sha1_ctx->lenW != 56) 522 while(sha1_ctx->lenW != 56)
523 gaim_cipher_context_append(context, &pad0x00, 1); 523 gaim_cipher_context_append(context, &pad0x00, 1);
524 gaim_cipher_context_append(context, padlen, 8); 524 gaim_cipher_context_append(context, padlen, 8);
525 525
526 for(i = 0; i < 20; i++) { 526 for(i = 0; i < 20; i++) {
527 digest[i] = (guint8)(sha1_ctx->H[i / 4] >> 24); 527 digest[i] = (guchar)(sha1_ctx->H[i / 4] >> 24);
528 sha1_ctx->H[i / 4] <<= 8; 528 sha1_ctx->H[i / 4] <<= 8;
529 } 529 }
530 530
531 gaim_cipher_context_reset(context, NULL); 531 gaim_cipher_context_reset(context, NULL);
532 532
623 623
624 return caps; 624 return caps;
625 } 625 }
626 626
627 gboolean 627 gboolean
628 gaim_cipher_digest_region(const gchar *name, const guint8 *data, 628 gaim_cipher_digest_region(const gchar *name, const guchar *data,
629 size_t data_len, size_t in_len, 629 size_t data_len, size_t in_len,
630 guint8 digest[], size_t *out_len) 630 guchar digest[], size_t *out_len)
631 { 631 {
632 GaimCipher *cipher; 632 GaimCipher *cipher;
633 GaimCipherContext *context; 633 GaimCipherContext *context;
634 gboolean ret = FALSE; 634 gboolean ret = FALSE;
635 635
865 g_free(context); 865 g_free(context);
866 context = NULL; 866 context = NULL;
867 } 867 }
868 868
869 void 869 void
870 gaim_cipher_context_set_iv(GaimCipherContext *context, guint8 *iv, size_t len) 870 gaim_cipher_context_set_iv(GaimCipherContext *context, guchar *iv, size_t len)
871 { 871 {
872 GaimCipher *cipher = NULL; 872 GaimCipher *cipher = NULL;
873 873
874 g_return_if_fail(context); 874 g_return_if_fail(context);
875 g_return_if_fail(iv); 875 g_return_if_fail(iv);
883 gaim_debug_info("cipher", "the %s cipher does not support the set" 883 gaim_debug_info("cipher", "the %s cipher does not support the set"
884 "initialization vector operation\n", cipher->name); 884 "initialization vector operation\n", cipher->name);
885 } 885 }
886 886
887 void 887 void
888 gaim_cipher_context_append(GaimCipherContext *context, const guint8 *data, 888 gaim_cipher_context_append(GaimCipherContext *context, const guchar *data,
889 size_t len) 889 size_t len)
890 { 890 {
891 GaimCipher *cipher = NULL; 891 GaimCipher *cipher = NULL;
892 892
893 g_return_if_fail(context); 893 g_return_if_fail(context);
902 "operation\n", cipher->name); 902 "operation\n", cipher->name);
903 } 903 }
904 904
905 gboolean 905 gboolean
906 gaim_cipher_context_digest(GaimCipherContext *context, size_t in_len, 906 gaim_cipher_context_digest(GaimCipherContext *context, size_t in_len,
907 guint8 digest[], size_t *out_len) 907 guchar digest[], size_t *out_len)
908 { 908 {
909 GaimCipher *cipher = NULL; 909 GaimCipher *cipher = NULL;
910 910
911 g_return_val_if_fail(context, FALSE); 911 g_return_val_if_fail(context, FALSE);
912 912
925 gboolean 925 gboolean
926 gaim_cipher_context_digest_to_str(GaimCipherContext *context, size_t in_len, 926 gaim_cipher_context_digest_to_str(GaimCipherContext *context, size_t in_len,
927 gchar digest_s[], size_t *out_len) 927 gchar digest_s[], size_t *out_len)
928 { 928 {
929 /* 8k is a bit excessive, will tweak later. */ 929 /* 8k is a bit excessive, will tweak later. */
930 guint8 digest[BUF_LEN * 4]; 930 guchar digest[BUF_LEN * 4];
931 gint n = 0; 931 gint n = 0;
932 size_t dlen = 0; 932 size_t dlen = 0;
933 933
934 g_return_val_if_fail(context, FALSE); 934 g_return_val_if_fail(context, FALSE);
935 g_return_val_if_fail(digest_s, FALSE); 935 g_return_val_if_fail(digest_s, FALSE);
950 950
951 return TRUE; 951 return TRUE;
952 } 952 }
953 953
954 gint 954 gint
955 gaim_cipher_context_encrypt(GaimCipherContext *context, const guint8 data[], 955 gaim_cipher_context_encrypt(GaimCipherContext *context, const guchar data[],
956 size_t len, guint8 output[], size_t *outlen) 956 size_t len, guchar output[], size_t *outlen)
957 { 957 {
958 GaimCipher *cipher = NULL; 958 GaimCipher *cipher = NULL;
959 959
960 g_return_val_if_fail(context, -1); 960 g_return_val_if_fail(context, -1);
961 961
974 return -1; 974 return -1;
975 } 975 }
976 } 976 }
977 977
978 gint 978 gint
979 gaim_cipher_context_decrypt(GaimCipherContext *context, const guint8 data[], 979 gaim_cipher_context_decrypt(GaimCipherContext *context, const guchar data[],
980 size_t len, guint8 output[], size_t *outlen) 980 size_t len, guchar output[], size_t *outlen)
981 { 981 {
982 GaimCipher *cipher = NULL; 982 GaimCipher *cipher = NULL;
983 983
984 g_return_val_if_fail(context, -1); 984 g_return_val_if_fail(context, -1);
985 985
998 return -1; 998 return -1;
999 } 999 }
1000 } 1000 }
1001 1001
1002 void 1002 void
1003 gaim_cipher_context_set_salt(GaimCipherContext *context, guint8 *salt) { 1003 gaim_cipher_context_set_salt(GaimCipherContext *context, guchar *salt) {
1004 GaimCipher *cipher = NULL; 1004 GaimCipher *cipher = NULL;
1005 1005
1006 g_return_if_fail(context); 1006 g_return_if_fail(context);
1007 1007
1008 cipher = context->cipher; 1008 cipher = context->cipher;
1033 return -1; 1033 return -1;
1034 } 1034 }
1035 } 1035 }
1036 1036
1037 void 1037 void
1038 gaim_cipher_context_set_key(GaimCipherContext *context, guint8 *key) { 1038 gaim_cipher_context_set_key(GaimCipherContext *context, guchar *key) {
1039 GaimCipher *cipher = NULL; 1039 GaimCipher *cipher = NULL;
1040 1040
1041 g_return_if_fail(context); 1041 g_return_if_fail(context);
1042 1042
1043 cipher = context->cipher; 1043 cipher = context->cipher;