MD5 algorithm based on assembly language as core

xiaoxiao2021-03-06  121

MD5.c

#include "md5.h" #include void main () {md5_ctx context; md5init (& context); Md5Update (& Context, "Crazyd", Strlen ("Crazyd")); MD5Final (& Context); //// On the breakpoint of the above break, the memory at Context, the content of the 16-byte content is the result of encryption}

MD5.DSP

# Microsoft Developer Studio Project File - Name = "MD5" - Package Owner = <4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT Edit **

# TARGTYPE "WIN32 (x86) console application" 0x0103

CFG = md5 -! Win32 Debug MESSAGE This is not a valid makefile To build this project using NMAKE, MESSAGE use the Export Makefile command and run MESSAGE MESSAGE NMAKE / f "md5.mak" MESSAGE MESSAGE You can.!!!.!! specify a configuration when running NMAKE MESSAGE by defining the macro CFG on the command line for example:!.!!!! MESSAGE MESSAGE NMAKE / f "md5.mak" CFG = "md5 - Win32 Debug" MESSAGE MESSAGE Possible choices for configuration are :! Message! Message "MD5 - WIN32 RELEASE" (Based on "Win32 (x86) Console Application")! Message "MD5 - WIN32 Debug" (X86) Console Application ")! Message

# Begin Project # prop allowperconfigdependencies 0 # prop scc_projname "" #prop scc_localpath "" CPP = cl.exersc = rc.exe

! IF "$ (cfg) ==" MD5 - Win32 Release "

# PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # Prop target_dir "# add base cpp / nologo / w3 / gx / o2 / d" win32 "/ d" ndebug "/ d" _console "/ d" _mbcs "/ yx / fd / c # add CPP / NOLOGO / W3 / GX / O2 / D "WIN32" / D "ndebug" / d "_console" / d "_mbCS" / yx / fd / c # add base rsc / l 0x804 / d "ndebug" # add rsc / l 0x804 / d "ndebug "BSC32 = bscmake.exe # ADD BASE BSC32 / nologo # ADD BSC32 /nologoLINK32=link.exe# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32 .lib uuid.lib odbc32.lib odbccp32.lib / nologo / subsystem: console / machine: I386 # ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32. Lib uuid.lib odbc32.lib odbccp32.lib / NOLOGO / SUBSYSTEM: Console / Machine: i386! Elseif "$ (c FG) "==" MD5 - Win32 Debug "

# PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # Prop target_dir "# add base cpp / nologo / w3 / gm / gx / zi / od / d" win32 "/ d" _debug "/ d" _console "/ d" _mbcs "/ yx / fd / gz / c # add CPP / NOLO / W3 / GM / GX / ZI / OD / D "WIN32" / D "_debug" / d "_console" / d "_mbCS" / yx / fd / gz / c # add base rsc / l 0x804 / d " _DEBUG "# ADD RSC / l 0x804 / d" _DEBUG "BSC32 = bscmake.exe # ADD BASE BSC32 / nologo # ADD BSC32 /nologoLINK32=link.exe# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32 .lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib / nologo / subsystem: console / debug / machine: I386 / pdbtype: sept # ADD LINK32 kernel32.lib user32.lib gdi32. Lib Winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib / NOLOGO / SUBSYST EM: console / debug / machine: i386 / pdbtype: sept! Endif

# Begin Target

# Name "MD5 - Win32 Release" # name "MD5 - Win32 Debug" # begin Source File

Source =. / Md5.c # end source file # begin Source File

Source =. / Md5.h # end source file # end target # End Project

Md5.dsw

Microsoft Developer Studio Workspace File, Format Version 6.00 # Warning: Do Not Edit Or Delete this Workspace File!

######################################################################################################################################################################################################################################################################################################## ###########################################

Project: "MD5" =. / Md5.dsp - package Owner = <4>

Package = <5> {{{}}}}

Package = <4> {{{}}}

######################################################################################################################################################################################################################################################################################################## ###################### Global:

Package = <5> {{{}}}}

Package = <3> {{{}}}}

######################################################################################################################################################################################################################################################################################################## ###########################################

MD5.H

/ * Author: crazyd Date: 2002-12-29 Description: This code embedded with compiled languages, md5 the most central part written in assembly has a high operation speed, almost faster than C code Two times, the method of use is the three functions of MD5init, MD5UPDATE, MD5Final, followed by secondary sequence:

Standard code: #include "md5.h" void main () {md5_ctx context; md5init (& context); Md5Update (& Context, "Crazyd", Strlen ("Crazyd")); MD5Final (& Context);}

Special instructions: context.state [0] is 32-bit unsigned integers, and the back array element is also true, and they are especially paying attention to the direction of memory storage and display. * / # include

Typedef struct {dWord State [4]; // Encypted Message DWord Count [2]; // Bits of Plaintext Unsigned Char Buffer [64];} MD5_CTX;

Void md5init (MD5_CTX *); Void Md5Update (md5_ctx *, unsigned char *, unsigned int); // The plain text to be encrypted is the middle parameter void md5Final (MD5_CTX *);

#define s11 7 # define s12 12 # define s13 17 # define s21 5 # define s22 9 # define s22 9 # Define s23 14 # define s24 20 # define s31 4 # define s32 11 # define s33 16 # define s34 23 # define S41 6 # Define S42 10 # Define S43 15 # Define S44 21

#define a esi # define b Edi # define c Edx # Define D EBX # define Tmp1 EAX # define TMP2 ECX

#define x (i) [x 4 * i]

Static void Md5Transform (DWORD [4], UNSIGNED CHAR [64]);

Static unsigned charphar padding [64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

#define FF (A, B, C, D, X, S, AC) / __ASM MOV TMP1, B / __ASM AND TMP1, C / __ASM MOV TMP2, B / __ASM NOT TMP2 / __ASM AND TMP2, D / __ASM OR TMP2 , TMP1 / __ASM LEA A, [TMP2 A AC] / __ASM ADD A, X / __ASM ROL A, S / __ASM Add A, B / # Define GG (A, B, C, D, X, S, AC ) / __ASM MOV TMP1, D / __ASM MOV TMP2, D / __ASM NOT TMP2 / __ASM AND TMP2, C / __ASM OR TMP2, TMP1 / __ASM LEA A, [TMP2 A AC] / __ASM Add A, X / __ASM ROL A, S / __ASM ADD A, B

#define HH (A, B, C, D, X, S, AC) / __ASM MOV TMP2, B / __ASM XOR TMP2, C / __ASM XOR TMP2, D / __ASM LEA A, [TMP2 A AC] / __asm Add A, X / __ASM ROL A, S / __ASM ADD A, B

#define II (A, B, C, D, X, S, AC) / __ASM MOV TMP2, D / __ASM NOT TMP2 / __ASM OR TMP2, B / __ASM XOR TMP2, C / __ASM LEA A, [TMP2 A AC] / __ASM Add A, X / __ASM ROL A, S / __ASM ADD A, B

Void md5init (MD5_CTX * Context) {context-> count [0] = context-> count [1] = 0; context-> state [0] = 0x67452301; context-> state [1] = 0xEfcdab89; context-> State [2] = 0x98badcfe; context-> state [3] = 0x10325476;

Void Md5Update (MD5_CTX * Context, unsigned char * INPUT, UNSIGNED INT INPUT I, INDEX, Partle; Index = (unsigned int) ((CONTEXT-> COUNT [0] >> 3) & 0x3f);

IF ((Context-> Count [0] = (DWORD) Inputlen << 3)) <(DWORD) INPUTLEN << 3)) Context-> COUNT [1] ; context-> count [1] = ((DWORD) INPUTLEN >> 29); Partlen = 64 - INDEX;

IF (Inputlen> = Partle) {CopyMemory (& Contex); MD5TRANSFORM (Context-> State, Context-> Buffer); for (i = partlen; i 63 buffer [index], & input [i], inputlen-i);} void md5final (md5_ctx * context) {UNSIGNED Char Bits [8]; Unsigned Int Index, Padlen; CopyMemory (Bits, Context-> Count, 8); Index = (unsigned int) ((Context-> Count [0] >> 3) & 0x3f); Padlen = (INDEX <56): (56 - INDEX): (120 - index); Md5Update (Context, Padding, Padlen); MD5Update (Context, Bits, 8);

static void MD5Transform (DWORD state [4], unsigned char block [64]) {DWORD x [16]; __asm ​​{// initial mov a, 0x67452301 mov b, 0xefcdab89 mov c, 0x98badcfe mov d, 0x10325476 // copy string from Block to State // Consider using the API will affect the register, so you can achieve this memory copy Push ESI Push EDI XOR ECX, ECX MOV ESI, DWORD PTR [Block] Lea EDI, [X] roll: MOV Eax, DWORD PTR [ ESI ECX] MOV DWORD PTR [EDI ECX], ES ADD ECX, 4 CMP ECX, 64 JB ROLL POP EDI POP ESI} / * Round 1 * / FF (A, B, C, D, X (0), S11, 0xD76AA478); / * 1 * / ff (D, A, B, C, X (1), S12, 0xE8C7B756); / * 2 * / ff (C, D, A, B, X (2), S13, 0x242070db); / * 3 * / ff (B, C, D, A, X (3), S14, 0XC1BDCEEE); / * 4 * / ff (A, B, C, D, X (4), S11, 0xF57c0FAF); / * 5 * / ff (D, A, B, C, X (5), S12, 0x4787C62A); / * 6 * / ff (C, D, A, B, X (6), S13, 0xA8304613); / * 7 * / ff (B, C, D, A, X (7), S14, 0xFD469501); / * 8 * / ff (A, B, C, D, X (8), S11, 0x698098d8); / * 9 * / ff (D, A, B, C, X (9), S12, 0x8B44F7AF); / * 10 * / ff (C, D, A, B, X (10), S13, 0xffff5bb1); / * 11 * / ff (B, C, D, A, X (11), S14, 0x895CD7BE); / * 12 * / ff (A, B, C, D, X (12), S11, 0x6b901122); / * 13 * / ff (D, A, B, C, X (13), S12, 0xFD987193); / * 14 * / ff (C, D, A, B, X (14), S13, 0xA679438E); / * 15 * / ff (B, C, D, A, X (15), S14, 0X49B40821); / * 16 * / / * ROUND 2 * / gg (A, B, C, D, X (1), S21, 0XF61E2562); / * 17 * / gg (D, A, B, C , x (6), s22, 0xc040b340); / * 18 * / gg (C, D, A, B, X (11), S23, 0X265E5A51); / * 19 * / gg (B, C, D, A , x (0), S24, 0xE9B6C7AA); / * 20 * / gg (A, B, C, D, X (5), S21, 0XD62F105D); / * 21 * / gg (D, A, B, C , x (10), S22, 0x24441453); / * 22 * ​​/ gg (C, D, A, B, X (15), S23, 0XD8A1E681); / * 23 * / gg (B, C, D, A , x (4), s24, 0xe7d3fbc8); / * 24 * / gg (A, B, C, D, X (9), S21, 0X21E1CDE6); / * 25 * / gg (D, A, B, C , x (14), s22, 0xc33707d6);

/ * 26 * / gg (C, D, A, B, X (3), S23, 0xF4D50D87); / * 27 * / gg (B, C, D, A, X (8), S24, 0X455A14ED); / * 28 * / gg (A, B, C, D, X (13), S21, 0XA9E3E905); / * 29 * / gg (D, A, B, C, X (2), S22, 0XFCEFA3F8); / * 30 * / gg (C, D, A, B, X (7), S23, 0X676F02D9); / * 31 * / gg (B, C, D, A, X (12), S24, 0x8D2A4C8A); / * 32 * / / * ROUND 3 * / HH (A, B, C, D, X (5), S31, 0xFFFA3942); / * 33 * / HH (D, A, B, C, X (8) S32, 0x8771F681); / * 34 * / HH (C, D, A, B, X (11), S33, 0x6D9D6122); / * 35 * / hh (B, C, D, A, X (14) , S34, 0xFDE5380C); / * 36 * / hh (A, B, C, D, X (1), S31, 0xA4Beea44); / * 37 * / HH (D, A, B, C, X (4) , S32, 0x4BDECFA9); / * 38 * / HH (C, D, A, B, X (7), S33, 0XF6BB4B60); / * 39 * / HH (B, C, D, A, X (10) , S34, 0XBEBFBC70); / * 40 * / HH (A, B, C, D, X (13), S31, 0x289B7EC6); / * 41 * / HH (D, A, B, C, X (0) S32, 0xEAA127FA); / * 42 * / hh (C, D, A, B, X (3), S33, 0xD4ef3085); / * 43 * / hh (B, C, D, A, X (6) , S34, 0X4881D05); / * 44 * / HH (A, B, C, D, X (9), S31, 0XD9D4D039); / * 45 * / HH (D, A, B, C, X (12) S32 0xE6DB99E5); / * 46 * / hh (C, D, A, B, X (15), S33, 0x1FA27CF8); / * 47 * / HH (B, C, D, A, X (2), S34 0xC4AC5665); / * 48 * / / * ROUND 4 * / II (A, B, C, D, X (0), S41, 0XF4292244); / * 49 * / II (D, A, B, C, X (7), S42, 0x432AFF97); / * 50 * / ii (C, D, A, B, X (14), S43, 0XAB9423A7); / * 51 * / II (B, C, D, A, X (5), S44, 0xFC93A039); / * 52 * / ii (A, B, C, D, X (12), S41, 0X655B59C3); / * 53 * / II (D, A, B, C, X (3), S42, 0x8F0CCC92); / * 54 * / ii (C, D, A, B, X (10), S43, 0xffeff47d); / * 55 * / II (B, C, D, A, X (1), S44, 0X85845DD1); / * 56 * / ii (A, B, C, D, X (8), S41, 0X6FA87E4F); / * 57 * / II (D, A, B, C, X (15), S42, 0XFE2CE6E0); / * 58 * / II (C, D, A, B, X (6), S43, 0XA3014314); / * 59 * / II (B, C, D, A, X (13), S44, 0X4E0811A1);

转载请注明原文地址:https://www.9cbs.com/read-126667.html

New Post(0)