home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 720 / PDF090B4-SorceCode / pdf / md5.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  12.3 KB  |  433 lines

  1. // MD5.CC - source code for the C++/object oriented translation and
  2. //          modification of MD5.
  3.  
  4. // Ported to EPOC.
  5. // Copyright (c) 2000 by Sander van der wal.
  6. // This port is provides "as is", without express or implied warranty of any kind.
  7. //
  8. // $Id: md5.cpp 1.1 2000-09-17 13:39:37+02 svdwal Exp svdwal $
  9. //
  10. // $Log
  11.  
  12. // Translation and modification (c) 1995 by Mordechai T. Abzug
  13.  
  14. // This translation/ modification is provided "as is," without express or
  15. // implied warranty of any kind.
  16.  
  17. // The translator/ modifier does not claim (1) that MD5 will do what you think
  18. // it does; (2) that this translation/ modification is accurate; or (3) that
  19. // this software is "merchantible."  (Language for this disclaimer partially
  20. // copied from the disclaimer below).
  21.  
  22. /* based on:
  23.  
  24.    MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  25.    MDDRIVER.C - test driver for MD2, MD4 and MD5
  26.  
  27.  
  28.    Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  29. rights reserved.
  30.  
  31. License to copy and use this software is granted provided that it
  32. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  33. Algorithm" in all material mentioning or referencing this software
  34. or this function.
  35.  
  36. License is also granted to make and use derivative works provided
  37. that such works are identified as "derived from the RSA Data
  38. Security, Inc. MD5 Message-Digest Algorithm" in all material
  39. mentioning or referencing the derived work.
  40.  
  41. RSA Data Security, Inc. makes no representations concerning either
  42. the merchantability of this software or the suitability of this
  43. software for any particular purpose. It is provided "as is"
  44. without express or implied warranty of any kind.
  45.  
  46. These notices must be retained in any copies of any part of this
  47. documentation and/or software.
  48.  
  49.  */
  50.  
  51. #include "md5.h"
  52.  
  53. #ifndef __E32STD_H__
  54. #include <e32std.h>
  55. #endif
  56.  
  57. static inline void assert(int aCond) { __ASSERT_ALWAYS(aCond, User::Panic(_L("MD5"), 1)); }
  58.  
  59. #include <stdio.h>
  60.  
  61. // MD5 simple initialization method
  62.  
  63. MD5::MD5(){
  64.  
  65.   init();
  66.  
  67. }
  68.  
  69.  
  70. // MD5 block update operation. Continues an MD5 message-digest
  71. // operation, processing another message block, and updating the
  72. // context.
  73.  
  74. void MD5::update (uint1 *input, uint4 input_length) {
  75.  
  76.   uint4 input_index, buffer_index;
  77.   uint4 buffer_space;                // how much space is left in buffer
  78.  
  79.   if (finalized){  // so we can't update!
  80.     // should be an error
  81.     // cerr << "MD5::update:  Can't update a finalized digest!" << endl;
  82.     return;
  83.   }
  84.  
  85.   // Compute number of bytes mod 64
  86.   buffer_index = (unsigned int)((count[0] >> 3) & 0x3F);
  87.  
  88.   // Update number of bits
  89.   if (  (count[0] += ((uint4) input_length << 3))<((uint4) input_length << 3) )
  90.     count[1]++;
  91.  
  92.   count[1] += ((uint4)input_length >> 29);
  93.  
  94.  
  95.   buffer_space = 64 - buffer_index;  // how much space is left in buffer
  96.  
  97.   // Transform as many times as possible.
  98.   if (input_length >= buffer_space) { // ie. we have enough to fill the buffer
  99.     // fill the rest of the buffer and transform
  100.     MemCpy (buffer + buffer_index, input, buffer_space);
  101.     transform (buffer);
  102.  
  103.     // now, transform each 64-byte piece of the input, bypassing the buffer
  104.     for (input_index = buffer_space; input_index + 63 < input_length;
  105.      input_index += 64)
  106.       transform (input+input_index);
  107.  
  108.     buffer_index = 0;  // so we can buffer remaining
  109.   }
  110.   else
  111.     input_index=0;     // so we can buffer the whole input
  112.  
  113.  
  114.   // and here we do the buffering:
  115.   MemCpy(buffer+buffer_index, input+input_index, input_length-input_index);
  116. }
  117.  
  118.  
  119. // MD5 finalization. Ends an MD5 message-digest operation, writing the
  120. // the message digest and zeroizing the context.
  121.  
  122. void MD5::finalize (){
  123.  
  124.   unsigned char bits[8];
  125.   unsigned int index, padLen;
  126.   uint1 PADDING[64]={
  127.     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  128.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  130.     };
  131.  
  132.   if (finalized){
  133.     // should be an error
  134.     //cerr << "MD5::finalize:  Already finalized this digest!" << endl;
  135.     return;
  136.   }
  137.  
  138.   // Save number of bits
  139.   encode (bits, count, 8);
  140.  
  141.   // Pad out to 56 mod 64.
  142.   index = (uint4) ((count[0] >> 3) & 0x3f);
  143.   padLen = (index < 56) ? (56 - index) : (120 - index);
  144.   update ((unsigned char*)PADDING, padLen);
  145.  
  146.   // Append length (before padding)
  147.   update (bits, 8);
  148.  
  149.   // Store state in digest
  150.   encode (digest, state, 16);
  151.  
  152.   // Zeroize sensitive information
  153.   MemSet (buffer, 0, sizeof(*buffer));
  154.  
  155.   finalized=1;
  156.  
  157. }
  158.  
  159. #if 0
  160. MD5::MD5(FILE *file){
  161.  
  162.   init();  // must be called be all constructors
  163.   update(file);
  164.   finalize ();
  165. }
  166. #endif
  167.  
  168. unsigned char *MD5::raw_digestL(){
  169.  
  170.   uint1 *s = new(ELeave) uint1[16];
  171.  
  172.   if (!finalized){
  173. //    cerr << "MD5::raw_digest:  Can't get digest if you haven't "<<
  174. //      "finalized the digest!" <<endl;
  175.     return ( (unsigned char*) "");
  176.   }
  177.  
  178.   MemCpy(s, digest, 16);
  179.   return s;
  180. }
  181.  
  182.  
  183. char *MD5::hex_digestL(){
  184.  
  185.   int i;
  186.   char *s= new(ELeave) char[33];
  187.  
  188.   if (!finalized){
  189. //    cerr << "MD5::hex_digest:  Can't get digest if you haven't "<<
  190. //      "finalized the digest!" <<endl;
  191.     return "";
  192.   }
  193.  
  194.   for (i=0; i<16; i++)
  195.     sprintf(s+i*2, "%02x", digest[i]);
  196.  
  197.   s[32]='\0';
  198.  
  199.   return s;
  200. }
  201.  
  202.  
  203. // PRIVATE METHODS:
  204.  
  205. void MD5::init(){
  206.   finalized=0;  // we just started!
  207.  
  208.   // Nothing counted, so count=0
  209.   count[0] = 0;
  210.   count[1] = 0;
  211.  
  212.   // Load magic initialization constants.
  213.   state[0] = 0x67452301;
  214.   state[1] = 0xefcdab89;
  215.   state[2] = 0x98badcfe;
  216.   state[3] = 0x10325476;
  217. }
  218.  
  219.  
  220.  
  221. // Constants for MD5Transform routine.
  222. // Although we could use C++ style constants, defines are actually better,
  223. // since they let us easily evade scope clashes.
  224.  
  225. #define S11 7
  226. #define S12 12
  227. #define S13 17
  228. #define S14 22
  229. #define S21 5
  230. #define S22 9
  231. #define S23 14
  232. #define S24 20
  233. #define S31 4
  234. #define S32 11
  235. #define S33 16
  236. #define S34 23
  237. #define S41 6
  238. #define S42 10
  239. #define S43 15
  240. #define S44 21
  241.  
  242.  
  243. // MD5 basic transformation. Transforms state based on block.
  244. void MD5::transform (uint1 block[64]){
  245.  
  246.   uint4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  247.  
  248.   decode (x, block, 64);
  249.  
  250.   assert(!finalized);  // not just a user error, since the method is private
  251.  
  252.   /* Round 1 */
  253.   FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  254.   FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  255.   FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  256.   FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  257.   FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  258.   FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  259.   FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  260.   FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  261.   FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  262.   FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  263.   FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  264.   FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  265.   FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  266.   FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  267.   FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  268.   FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  269.  
  270.  /* Round 2 */
  271.   GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  272.   GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  273.   GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  274.   GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  275.   GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  276.   GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
  277.   GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  278.   GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  279.   GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  280.   GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  281.   GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  282.   GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  283.   GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  284.   GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  285.   GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  286.   GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  287.  
  288.   /* Round 3 */
  289.   HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  290.   HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  291.   HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  292.   HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  293.   HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  294.   HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  295.   HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  296.   HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  297.   HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  298.   HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  299.   HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  300.   HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
  301.   HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  302.   HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  303.   HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  304.   HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  305.  
  306.   /* Round 4 */
  307.   II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  308.   II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  309.   II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  310.   II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  311.   II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  312.   II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  313.   II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  314.   II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  315.   II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  316.   II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  317.   II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  318.   II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  319.   II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  320.   II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  321.   II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  322.   II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  323.  
  324.   state[0] += a;
  325.   state[1] += b;
  326.   state[2] += c;
  327.   state[3] += d;
  328.  
  329.   // Zeroize sensitive information.
  330.   MemSet ( (uint1 *) x, 0, sizeof(x));
  331.  
  332. }
  333.  
  334.  
  335. // Encodes input (UINT4) into output (unsigned char). Assumes len is
  336. // a multiple of 4.
  337. void MD5::encode (uint1 *output, uint4 *input, uint4 len) {
  338.  
  339.   unsigned int i, j;
  340.  
  341.   for (i = 0, j = 0; j < len; i++, j += 4) {
  342.     output[j]   = (uint1)  (input[i] & 0xff);
  343.     output[j+1] = (uint1) ((input[i] >> 8) & 0xff);
  344.     output[j+2] = (uint1) ((input[i] >> 16) & 0xff);
  345.     output[j+3] = (uint1) ((input[i] >> 24) & 0xff);
  346.   }
  347. }
  348.  
  349.  
  350. // Decodes input (unsigned char) into output (UINT4). Assumes len is
  351. // a multiple of 4.
  352. void MD5::decode (uint4 *output, uint1 *input, uint4 len){
  353.  
  354.   unsigned int i, j;
  355.  
  356.   for (i = 0, j = 0; j < len; i++, j += 4)
  357.     output[i] = ((uint4)input[j]) | (((uint4)input[j+1]) << 8) |
  358.       (((uint4)input[j+2]) << 16) | (((uint4)input[j+3]) << 24);
  359. }
  360.  
  361.  
  362. // Note: Replace "for loop" with standard memcpy if possible.
  363. void MD5::MemCpy (uint1 *output, uint1 *input, uint4 len){
  364.  
  365.   unsigned int i;
  366.  
  367.   for (i = 0; i < len; i++)
  368.     output[i] = input[i];
  369. }
  370.  
  371.  
  372. // Note: Replace "for loop" with standard memset if possible.
  373. void MD5::MemSet (uint1 *output, uint1 value, uint4 len){
  374.  
  375.   unsigned int i;
  376.  
  377.   for (i = 0; i < len; i++)
  378.     output[i] = value;
  379. }
  380.  
  381.  
  382. // ROTATE_LEFT rotates x left n bits.
  383. inline unsigned int MD5::rotate_left  (uint4 x, uint4 n){
  384.   return (x << n) | (x >> (32-n))  ;
  385. }
  386.  
  387.  
  388. // F, G, H and I are basic MD5 functions.
  389.  
  390. inline unsigned int MD5::F            (uint4 x, uint4 y, uint4 z){
  391.   return (x & y) | (~x & z);
  392. }
  393.  
  394. inline unsigned int MD5::G            (uint4 x, uint4 y, uint4 z){
  395.   return (x & z) | (y & ~z);
  396. }
  397.  
  398. inline unsigned int MD5::H            (uint4 x, uint4 y, uint4 z){
  399.   return x ^ y ^ z;
  400. }
  401.  
  402. inline unsigned int MD5::I            (uint4 x, uint4 y, uint4 z){
  403.   return y ^ (x | ~z);
  404. }
  405.  
  406.  
  407. // FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  408. // Rotation is separate from addition to prevent recomputation.
  409.  
  410. inline void MD5::FF(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
  411.             uint4  s, uint4 ac){
  412.  a += F(b, c, d) + x + ac;
  413.  a = rotate_left (a, s) +b;
  414. }
  415.  
  416. inline void MD5::GG(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
  417.             uint4 s, uint4 ac){
  418.  a += G(b, c, d) + x + ac;
  419.  a = rotate_left (a, s) +b;
  420. }
  421.  
  422. inline void MD5::HH(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
  423.             uint4 s, uint4 ac){
  424.  a += H(b, c, d) + x + ac;
  425.  a = rotate_left (a, s) +b;
  426. }
  427.  
  428. inline void MD5::II(uint4& a, uint4 b, uint4 c, uint4 d, uint4 x,
  429.                  uint4 s, uint4 ac){
  430.  a += I(b, c, d) + x + ac;
  431.  a = rotate_left (a, s) +b;
  432. }
  433.