Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

md2.cpp

00001 // md2.cpp - modified by Wei Dai from Sun Microsystems's md2.c
00002 
00003 /*
00004 SKIP Source Code License Statement:
00005 ------------------------------------------------------------------
00006   Copyright
00007   Sun Microsystems, Inc.
00008  
00009  
00010   Copyright (C) 1994, 1995 Sun Microsystems, Inc.  All Rights
00011   Reserved.
00012  
00013   Permission is hereby granted, free of charge, to any person
00014   obtaining a copy of this software and associated documentation
00015   files (the "Software"), to deal in the Software without
00016   restriction, including without limitation the rights to use,
00017   copy, modify, merge, publish, distribute, sublicense, and/or sell
00018   copies of the Software or derivatives of the Software, and to 
00019   permit persons to whom the Software or its derivatives is furnished 
00020   to do so, subject to the following conditions:
00021  
00022   The above copyright notice and this permission notice shall be
00023   included in all copies or substantial portions of the Software.
00024  
00025   The Software must not be transferred to persons who are not US
00026   citizens or permanent residents of the US or exported outside
00027   the US (except Canada) in any form (including by electronic
00028   transmission) without prior written approval from the US
00029   Government. Non-compliance with these restrictions constitutes
00030   a violation of the U.S. Export Control Laws.
00031  
00032   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00034   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035   NONINFRINGEMENT.  IN NO EVENT SHALL SUN MICROSYSTEMS, INC., BE LIABLE
00036   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00038   CONNECTION WITH THE SOFTWARE OR DERIVATES OF THIS SOFTWARE OR 
00039   THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00040  
00041   Except as contained in this notice, the name of Sun Microsystems, Inc.
00042   shall not be used in advertising or otherwise to promote
00043   the sale, use or other dealings in this Software or its derivatives 
00044   without prior written authorization from Sun Microsystems, Inc.
00045 */
00046 
00047 #include "pch.h"
00048 #include "md2.h"
00049 
00050 NAMESPACE_BEGIN(CryptoPP)
00051 
00052 MD2::MD2()
00053         : buf(64)
00054 {
00055         Init();
00056 }
00057 
00058 void MD2::Init()
00059 {
00060         memset(buf, 0, 64);
00061         len = 0;
00062 }
00063 
00064 void MD2::Update(const byte *input, unsigned int length)
00065 {
00066         while (length)
00067         {
00068                 unsigned int lenInc = STDMIN(length, 16-len);
00069                 memcpy(buf+len+16, input, lenInc);
00070                 input += lenInc;
00071                 length -= lenInc;
00072                 len += lenInc;
00073 
00074                 if (len == 16)
00075                 {
00076                         Transform();
00077                         len = 0;
00078                 }
00079         }
00080 }
00081 
00082 void MD2::Final(byte *hash)
00083 {
00084         byte space = 16 - len; // Amount of padding
00085 
00086         // Pad with "space" bytes of value "space"
00087         memset(buf+16+len, space, space);
00088         Transform();
00089 
00090         // Append checksum
00091         memcpy(buf+16, buf+48, 16);
00092         // The transform redundantly updates the checksum, but it's not worth optimizing away.
00093         Transform();
00094 
00095         // Copy hash out
00096         memcpy(hash, buf, 16);
00097 
00098         Init();
00099 }
00100 
00101 void MD2::Transform()
00102 {
00103         static const byte permutation[256] = {
00104                  41, 46, 67,201,162,216,124,  1, 61, 54, 84,161,236,240,  6, 19,
00105                  98,167,  5,243,192,199,115,140,152,147, 43,217,188, 76,130,202,
00106                  30,155, 87, 60,253,212,224, 22,103, 66,111, 24,138, 23,229, 18,
00107                 190, 78,196,214,218,158,222, 73,160,251,245,142,187, 47,238,122,
00108                 169,104,121,145, 21,178,  7, 63,148,194, 16,137, 11, 34, 95, 33,
00109                 128,127, 93,154, 90,144, 50, 39, 53, 62,204,231,191,247,151,  3,
00110                 255, 25, 48,179, 72,165,181,209,215, 94,146, 42,172, 86,170,198,
00111                  79,184, 56,210,150,164,125,182,118,252,107,226,156,116,  4,241,
00112                  69,157,112, 89,100,113,135, 32,134, 91,207,101,230, 45,168,  2,
00113                  27, 96, 37,173,174,176,185,246, 28, 70, 97,105, 52, 64,126, 15,
00114                  85, 71,163, 35,221, 81,175, 58,195, 92,249,206,186,197,234, 38,
00115                  44, 83, 13,110,133, 40,132,  9,211,223,205,244, 65,129, 77, 82,
00116                 106,220, 55,200,108,193,171,250, 36,225,123,  8, 12,189,177, 74,
00117                 120,136,149,139,227, 99,232,109,233,203,213,254, 59,  0, 29, 57,
00118                 242,239,183, 14,102, 88,208,228,166,119,114,248,235,117, 75, 10,
00119                  49, 68, 80,180,143,237, 31, 26,219,153,141, 51,159, 17,131, 20
00120         };
00121 
00122         // Fill in the temp buf
00123         unsigned int i;
00124         for (i = 0; i < 16; i++)
00125                         buf[i+32] = buf[i+16] ^ buf[i];
00126 
00127         // Update the checksum in the last 16 bytes of the buf
00128         byte t = buf[63];
00129         for (i = 0; i < 16; i++)
00130                 t = buf[48+i] ^= permutation[buf[16+i] ^ t];
00131 
00132         // 18 passes of encryption over the first 48 bytes of the buf
00133         t = 0;
00134         for (i = 0; i < 18; i++)
00135         {
00136                 for (unsigned int j = 0; j < 48; j++)
00137                         t = buf[j] ^= permutation[t];
00138                 t += i;
00139         }
00140 }
00141 
00142 NAMESPACE_END

Generated at Mon Jan 15 01:16:33 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000