Back to index

texmacs  1.0.7.15
dpxcrypt.c
Go to the documentation of this file.
00001 /*  $Header: /home/cvsroot/dvipdfmx/src/dpxcrypt.c,v 1.2 2004/07/27 12:08:46 hirata Exp $
00002  
00003     This is DVIPDFMx, an eXtended version of DVIPDFM by Mark A. Wicks.
00004 
00005     Copyright (C) 2003 by Jin-Hwan Cho and Shunsaku Hirata,
00006     the DVIPDFMx project team <dvipdfmx@project.ktug.or.kr>
00007     
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012     
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017     
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00021 */
00022 
00023 #if HAVE_CONFIG_H
00024 #include "config.h"
00025 #endif
00026 
00027 #include <string.h>
00028 
00029 #include "dpxcrypt.h"
00030 
00031 static void _gcry_burn_stack (int bytes)
00032 {
00033   char buf[64];
00034     
00035   memset(buf, 0, sizeof buf);
00036   bytes -= sizeof buf;
00037   if (bytes > 0) _gcry_burn_stack(bytes);
00038 }
00039 
00040 /* Rotate a 32 bit integer by n bytes */
00041 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
00042 
00043 /*
00044  * The following codes for MD5 Message-Digest Algorithm were modified
00045  * by Jin-Hwan Cho on August 5, 2003 based on libgrypt-1.1.42.
00046  *
00047  * Copyright (C) 1995,1996,1998,1999,2001,2002,2003 Free Software Foundation, Inc.
00048  *
00049  * Libgcrypt is free software; you can redistribute it and/or modify
00050  * it under the terms of the GNU Lesser General Public License as
00051  * published by the Free Software Foundation; either version 2.1 of
00052  * the License, or (at your option) any later version.
00053  *
00054  * Libgcrypt is distributed in the hope that it will be useful,
00055  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00056  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00057  * GNU Lesser General Public License for more details.
00058  *
00059  * You should have received a copy of the GNU Lesser General Public
00060  * License along with this program; if not, write to the Free Software
00061  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00062  *
00063  * According to the definition of MD5 in RFC 1321 from April 1992.
00064  * NOTE: This is *not* the same file as the one from glibc.
00065  * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. 
00066  * heavily modified for GnuPG by Werner Koch <wk@gnupg.org> 
00067  */
00068 
00069 void MD5_init (MD5_CONTEXT *ctx)
00070 {
00071   ctx->A = 0x67452301;
00072   ctx->B = 0xefcdab89;
00073   ctx->C = 0x98badcfe;
00074   ctx->D = 0x10325476;
00075 
00076   ctx->nblocks = 0;
00077   ctx->count = 0;
00078 }
00079 
00080 /* These are the four functions used in the four steps of the MD5 algorithm
00081  * and defined in the RFC 1321. The first function is a little bit optimized
00082  * (as found in Colin Plumbs public domain implementation). */
00083 /* #define FF(b, c, d) ((b & c) | (~b & d)) */
00084 #define FF(b, c, d) (d ^ (b & (c ^ d)))
00085 #define FG(b, c, d) FF(d, b, c)
00086 #define FH(b, c, d) (b ^ c ^ d)
00087 #define FI(b, c, d) (c ^ (b | ~d))
00088 
00089 /* transform n*64 bytes */
00090 static void transform (MD5_CONTEXT *ctx, unsigned char *data)
00091 {
00092   unsigned long correct_words[16];
00093   register unsigned long A = ctx->A;
00094   register unsigned long B = ctx->B;
00095   register unsigned long C = ctx->C;
00096   register unsigned long D = ctx->D;
00097   unsigned long *cwp = correct_words;
00098 
00099 #ifdef WORDS_BIGENDIAN
00100   { int i; unsigned char *p1, *p2;
00101     for (i = 0, p1 = data, p2 = (unsigned char *)correct_words; i < 16; i++, p2 += 4 ) {
00102       p2[3] = *p1++; p2[2] = *p1++; p2[1] = *p1++; p2[0] = *p1++;
00103     }
00104   }
00105 #else
00106   memcpy(correct_words, data, 64);
00107 #endif
00108 
00109 #define OP(a, b, c, d, s, T) \
00110   do { a += FF(b, c, d) + (*cwp++) + T; a = rol(a, s); a += b; } while (0)
00111 
00112   /* Before we start, one word about the strange constants.
00113    * They are defined in RFC 1321 as
00114    *
00115    *   T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
00116    */
00117 
00118   /* Round 1. */
00119   OP(A, B, C, D,  7, 0xd76aa478);
00120   OP(D, A, B, C, 12, 0xe8c7b756);
00121   OP(C, D, A, B, 17, 0x242070db);
00122   OP(B, C, D, A, 22, 0xc1bdceee);
00123   OP(A, B, C, D,  7, 0xf57c0faf);
00124   OP(D, A, B, C, 12, 0x4787c62a);
00125   OP(C, D, A, B, 17, 0xa8304613);
00126   OP(B, C, D, A, 22, 0xfd469501);
00127   OP(A, B, C, D,  7, 0x698098d8);
00128   OP(D, A, B, C, 12, 0x8b44f7af);
00129   OP(C, D, A, B, 17, 0xffff5bb1);
00130   OP(B, C, D, A, 22, 0x895cd7be);
00131   OP(A, B, C, D,  7, 0x6b901122);
00132   OP(D, A, B, C, 12, 0xfd987193);
00133   OP(C, D, A, B, 17, 0xa679438e);
00134   OP(B, C, D, A, 22, 0x49b40821);
00135 
00136 #undef OP
00137 #define OP(f, a, b, c, d, k, s, T) \
00138   do { a += f(b, c, d) + correct_words[k] + T; a = rol(a, s); a += b; } while (0)
00139 
00140   /* Round 2. */
00141   OP(FG, A, B, C, D,  1,  5, 0xf61e2562);
00142   OP(FG, D, A, B, C,  6,  9, 0xc040b340);
00143   OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
00144   OP(FG, B, C, D, A,  0, 20, 0xe9b6c7aa);
00145   OP(FG, A, B, C, D,  5,  5, 0xd62f105d);
00146   OP(FG, D, A, B, C, 10,  9, 0x02441453);
00147   OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
00148   OP(FG, B, C, D, A,  4, 20, 0xe7d3fbc8);
00149   OP(FG, A, B, C, D,  9,  5, 0x21e1cde6);
00150   OP(FG, D, A, B, C, 14,  9, 0xc33707d6);
00151   OP(FG, C, D, A, B,  3, 14, 0xf4d50d87);
00152   OP(FG, B, C, D, A,  8, 20, 0x455a14ed);
00153   OP(FG, A, B, C, D, 13,  5, 0xa9e3e905);
00154   OP(FG, D, A, B, C,  2,  9, 0xfcefa3f8);
00155   OP(FG, C, D, A, B,  7, 14, 0x676f02d9);
00156   OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
00157 
00158   /* Round 3. */
00159   OP(FH, A, B, C, D,  5,  4, 0xfffa3942);
00160   OP(FH, D, A, B, C,  8, 11, 0x8771f681);
00161   OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
00162   OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
00163   OP(FH, A, B, C, D,  1,  4, 0xa4beea44);
00164   OP(FH, D, A, B, C,  4, 11, 0x4bdecfa9);
00165   OP(FH, C, D, A, B,  7, 16, 0xf6bb4b60);
00166   OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
00167   OP(FH, A, B, C, D, 13,  4, 0x289b7ec6);
00168   OP(FH, D, A, B, C,  0, 11, 0xeaa127fa);
00169   OP(FH, C, D, A, B,  3, 16, 0xd4ef3085);
00170   OP(FH, B, C, D, A,  6, 23, 0x04881d05);
00171   OP(FH, A, B, C, D,  9,  4, 0xd9d4d039);
00172   OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
00173   OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
00174   OP(FH, B, C, D, A,  2, 23, 0xc4ac5665);
00175 
00176   /* Round 4.  */
00177   OP(FI, A, B, C, D,  0,  6, 0xf4292244);
00178   OP(FI, D, A, B, C,  7, 10, 0x432aff97);
00179   OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
00180   OP(FI, B, C, D, A,  5, 21, 0xfc93a039);
00181   OP(FI, A, B, C, D, 12,  6, 0x655b59c3);
00182   OP(FI, D, A, B, C,  3, 10, 0x8f0ccc92);
00183   OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
00184   OP(FI, B, C, D, A,  1, 21, 0x85845dd1);
00185   OP(FI, A, B, C, D,  8,  6, 0x6fa87e4f);
00186   OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
00187   OP(FI, C, D, A, B,  6, 15, 0xa3014314);
00188   OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
00189   OP(FI, A, B, C, D,  4,  6, 0xf7537e82);
00190   OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
00191   OP(FI, C, D, A, B,  2, 15, 0x2ad7d2bb);
00192   OP(FI, B, C, D, A,  9, 21, 0xeb86d391);
00193 
00194   /* Put checksum in context given as argument. */
00195   ctx->A += A;
00196   ctx->B += B;
00197   ctx->C += C;
00198   ctx->D += D;
00199 }
00200 
00201 /* The routine updates the message-digest context to
00202  * account for the presence of each of the characters inBuf[0..inLen-1]
00203  * in the message whose digest is being computed. */
00204 void MD5_write (MD5_CONTEXT *hd, unsigned char *inbuf, unsigned long inlen)
00205 {
00206   if (hd->count == 64) { /* flush the buffer */
00207     transform(hd, hd->buf);
00208     _gcry_burn_stack(80+6*sizeof(void*));
00209     hd->count = 0;
00210     hd->nblocks++;
00211   }
00212   if (!inbuf) return;
00213   if (hd->count) {
00214     for (; inlen && hd->count < 64; inlen--)
00215       hd->buf[hd->count++] = *inbuf++;
00216     MD5_write(hd, NULL, 0);
00217     if (!inlen) return;
00218   }
00219   _gcry_burn_stack(80+6*sizeof(void*));
00220 
00221   while (inlen >= 64) {
00222     transform(hd, inbuf);
00223     hd->count = 0;
00224     hd->nblocks++;
00225     inlen -= 64;
00226     inbuf += 64;
00227   }
00228   for (; inlen && hd->count < 64; inlen--)
00229     hd->buf[hd->count++] = *inbuf++;
00230 }
00231 
00232 /* The routine final terminates the message-digest computation and
00233  * ends with the desired message digest in mdContext->digest[0...15].
00234  * The handle is prepared for a new MD5 cycle.
00235  * Returns 16 bytes representing the digest. */
00236 
00237 void MD5_final (unsigned char *outbuf, MD5_CONTEXT *hd)
00238 {
00239   unsigned long t, msb, lsb;
00240   unsigned char *p;
00241 
00242   MD5_write(hd, NULL, 0); /* flush */
00243 
00244   t = hd->nblocks;
00245   /* multiply by 64 to make a byte count */
00246   lsb = t << 6;
00247   msb = t >> 26;
00248   /* add the count */
00249   t = lsb;
00250   if ((lsb += hd->count) < t) msb++;
00251   /* multiply by 8 to make a bit count */
00252   t = lsb;
00253   lsb <<= 3;
00254   msb <<= 3;
00255   msb |= t >> 29;
00256 
00257   if (hd->count < 56) { /* enough room */
00258     hd->buf[hd->count++] = 0x80; /* pad */
00259     while (hd->count < 56) hd->buf[hd->count++] = 0; /* pad */
00260   } else { /* need one extra block */
00261     hd->buf[hd->count++] = 0x80; /* pad character */
00262     while (hd->count < 64) hd->buf[hd->count++] = 0;
00263     MD5_write(hd, NULL, 0); /* flush */
00264     memset(hd->buf, 0, 56); /* fill next block with zeroes */
00265   }
00266   /* append the 64 bit count */
00267   hd->buf[56] = lsb;
00268   hd->buf[57] = lsb >> 8;
00269   hd->buf[58] = lsb >> 16;
00270   hd->buf[59] = lsb >> 24;
00271   hd->buf[60] = msb;
00272   hd->buf[61] = msb >> 8;
00273   hd->buf[62] = msb >> 16;
00274   hd->buf[63] = msb >> 24;
00275   transform(hd, hd->buf);
00276   _gcry_burn_stack(80+6*sizeof(void*));
00277 
00278   p = outbuf; /* p = hd->buf; */
00279 #ifdef WORDS_BIGENDIAN
00280 #define X(a) do { *p++ = hd->a; *p++ = hd->a >> 8; \
00281                  *p++ = hd->a >> 16; *p++ = hd->a >> 24; } while (0)
00282 #else /* little endian */
00283 #define X(a) do { *(unsigned long *)p = (*hd).a ; p += 4; } while (0)
00284 #endif
00285   X(A);
00286   X(B);
00287   X(C);
00288   X(D);
00289 #undef X
00290 }
00291 
00292 /*
00293  * The following codes for the arcfour stream cipher were modified
00294  * by Jin-Hwan Cho on August 5, 2003 based on libgrypt-1.1.42.
00295  *
00296  * Copyright (C) 2000,2001,2002,2003 Free Software Foundation, Inc.
00297  *
00298  * Libgcrypt is free software; you can redistribute it and/or modify
00299  * it under the terms of the GNU Lesser General Public License as
00300  * published by the Free Software Foundation; either version 2.1 of
00301  * the License, or (at your option) any later version.
00302  *
00303  * Libgcrypt is distributed in the hope that it will be useful,
00304  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00305  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00306  * GNU Lesser General Public License for more details.
00307  *
00308  * You should have received a copy of the GNU Lesser General Public
00309  * License along with this program; if not, write to the Free Software
00310  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00311  *
00312  * According to the definition of MD5 in RFC 1321 from April 1992.
00313  * NOTE: This is *not* the same file as the one from glibc.
00314  * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995. 
00315  * heavily modified for GnuPG by Werner Koch <wk@gnupg.org> 
00316  */
00317 
00318 static void do_encrypt_stream (ARC4_KEY *ctx, unsigned char *outbuf, const unsigned char *inbuf, unsigned long len)
00319 {
00320   register int i = ctx->idx_i;
00321   register int j = ctx->idx_j;
00322   register unsigned char *sbox = ctx->sbox;
00323   register int t;  
00324 
00325   while (len--) {
00326     i++;
00327     i = i & 255; /* and seems to be faster than mod */
00328     j += sbox[i];
00329     j &= 255;
00330     t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
00331     *outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255];
00332   }
00333   
00334   ctx->idx_i = i;
00335   ctx->idx_j = j;
00336 }
00337 
00338 void ARC4 (ARC4_KEY *ctx, unsigned long len, const unsigned char *inbuf, unsigned char *outbuf)
00339 {
00340   do_encrypt_stream(ctx, outbuf, inbuf, len);
00341   _gcry_burn_stack(64);
00342 }
00343 
00344 static void do_arcfour_setkey (ARC4_KEY *ctx, const unsigned char *key, unsigned int keylen)
00345 {
00346   int i, j;
00347   unsigned char karr[256];
00348 
00349   ctx->idx_i = ctx->idx_j = 0;
00350   for (i = 0; i < 256; i++) ctx->sbox[i] = i;
00351   for (i = 0; i < 256; i++) karr[i] = key[i%keylen];
00352   for (i = j = 0; i < 256; i++) {
00353     int t;
00354     j = (j + ctx->sbox[i] + karr[i]) % 256;
00355     t = ctx->sbox[i];
00356     ctx->sbox[i] = ctx->sbox[j];
00357     ctx->sbox[j] = t;
00358   } 
00359   memset(karr, 0, 256);
00360 }
00361 
00362 void ARC4_set_key (ARC4_KEY *ctx, unsigned int keylen, const unsigned char *key)
00363 {
00364   do_arcfour_setkey(ctx, key, keylen);
00365   _gcry_burn_stack(300);
00366 }