[Mixmaster-devel] CVS: Mix/Src pgp.c,1.9,1.10
Brought to you by:
weaselp
From: Disastry <dis...@us...> - 2002-09-25 16:14:38
|
Update of /cvsroot/mixmaster/Mix/Src In directory usw-pr-cvs1:/tmp/cvs-serv19971/Mix/Src Modified Files: pgp.c Log Message: Check PGP armor checksum Index: pgp.c =================================================================== RCS file: /cvsroot/mixmaster/Mix/Src/pgp.c,v retrieving revision 1.9 retrieving revision 1.10 diff -u -d -r1.9 -r1.10 --- pgp.c 18 Sep 2002 23:26:16 -0000 1.9 +++ pgp.c 25 Sep 2002 16:14:31 -0000 1.10 @@ -274,6 +274,47 @@ return (err); } +#define POLY 0X1864CFB + +unsigned long crc24(BUFFER * in) +{ + unsigned long crc = 0xB704CE; + long p; + int i; + +#if 0 + /* CRC algorithm from RFC 2440 */ + for (p = 0; p < in->length; p++) { + crc ^= in->data[p] << 16; + for (i = 0; i < 8; i++) { + crc <<= 1; + if (crc & 0x1000000) + crc ^= POLY; + } + } +#else + /* pre-computed CRC table -- much faster */ + unsigned long table[256]; + unsigned long t; + int q = 0; + + table[0] = 0; + for (i = 0; i < 128; i++) { + t = table[i] << 1; + if (t & 0x1000000) { + table[q++] = t ^ POLY; + table[q++] = t; + } else { + table[q++] = t; + table[q++] = t ^ POLY; + } + } + for (p = 0; p < in->length; p++) + crc = crc << 8 ^ table[(in->data[p] ^ crc >> 16) & 255]; +#endif + return crc; +} + /* ASCII armor */ int pgp_dearmor(BUFFER *in, BUFFER *out) @@ -281,6 +322,7 @@ BUFFER *line, *temp; int err = 0; int tempbuf = 0; + unsigned long crc1, crc2; line = buf_new(); temp = buf_new(); @@ -299,9 +341,18 @@ while (buf_getheader(in, temp, line) == 0) ; /* scan for empty line */ err = decode(in, out); + crc1 = crc24(out); err = buf_getline(in, line); - if (line->length == 5 && line->data[0] == '=') /* skip CRC */ - err = buf_getline(in, line); + if (line->length == 5 && line->data[0] == '=') { /* CRC */ + line->ptr = 1; + err = decode(line, temp); + crc2 = (((unsigned long)temp->data[0])<<16) | (((unsigned long)temp->data[1])<<8) | temp->data[2]; + if (crc1 == crc2) + err = buf_getline(in, line); + else + err = -1; + } else + err = -1; if (err == 0 && bufleft(line, end_pgp)) err = 0; else @@ -318,8 +369,6 @@ return (err); } -#define POLY 0X1864CFB - int pgp_armor(BUFFER *in, int mode) /* mode = 1: remailer message @@ -331,40 +380,9 @@ { BUFFER *out; - unsigned long crc = 0xB704CE; - long p; - int i; - -#if 0 - /* CRC algorithm from RFC 2440 */ - for (p = 0; p < in->length; p++) { - crc ^= in->data[p] << 16; - for (i = 0; i < 8; i++) { - crc <<= 1; - if (crc & 0x1000000) - crc ^= POLY; - } - } -#else /* end of 0 */ - /* pre-computed CRC table -- much faster */ - unsigned long table[256]; - unsigned long t; - int q = 0; + unsigned long crc; - table[0] = 0; - for (i = 0; i < 128; i++) { - t = table[i] << 1; - if (t & 0x1000000) { - table[q++] = t ^ POLY; - table[q++] = t; - } else { - table[q++] = t; - table[q++] = t ^ POLY; - } - } - for (p = 0; p < in->length; p++) - crc = crc << 8 ^ table[(in->data[p] ^ crc >> 16) & 255]; -#endif /* end of not 0 */ + crc = crc24(in); encode(in, 64); out = buf_new(); |