MD5 official algorithm

xiaoxiao2021-03-06  82

Network Working Group R. Rivest

Request for Comments: 1321 Mit Laboratory for Computer Science

And RSA Data Security, Inc.

April 1992

The MD5 Message-Digest Algorithm

STATUS OF this Memo

This Memo Provides Information for the Internet Community. It Does. IT Does

Not Specify ANTRNET STANDARD. Distribution of this Memo IS

Unlimited.

Acknowlegments

We would like to Than Thak Don Coppersmith, Burt Kaliski, Ralph Merkle,

David Chaum, And Noam Nisan For Numerous Helpful Comments and

Suggestions.

Table of contents

Executive Summary 1

2. Terminology and notation 2

3. MD5 Algorithm Description 3

4. Summary 6

5. Differences Between MD4 and MD5 6

References 7

Appendix A - Reference Implementation 7

Security considances 21

Author's address 21

Executive Summary

THIS Document Describes the MD5 Message-Digest Algorithm. The

Algorithm Takes As INPUT A Message of Arbitrary Length And Products

As Output A 128-bit "fingerprint" or "message digest" of the input.

IT IS Conjectured That IT IS Computationally Infeasible To Produce

Two Messages Having The Same Message Digest, Or To Product Any

Message Having a Given Prespecified Target Message Digest. THE MD5

Algorithm is intended for Digital Signature Applications, WHERE A

Large File Must Be "Compressed" in a Secure Manner Before BeinGencrypted with a private (Secret) Key under a public-key cryptosystem

Such as RSA.

Rivest [Page 1]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

The MD5 Algorithm Is Designed to Be Quite Fast On 32-Bit Machines. In

Addition, The MD5 Algorithm Does Not Require Any Large Substitution

Tables; The Algorithm Can Be Coded Quite Compactly.

The md5 algorithm is an extension of the md4 message-Digest Algorithm

1,2]. MD5 is Slightly Slower Than MD4, But Is More "Conservation" in

Design. MD5 WAS Designed Because It Was Felt That MD4 WAS Perhaps

Being adopted for use more Quickly Than Justified by the existing

Critical review; Because MD4 WAS Designed to Be Exceptionally Fast,

IT IS "At the Edge" in Terms of Risking Successful Cryptanalytic

Attack. MD5 BACKS OFF A BIT, GIVING UP A Little in Speed ​​for a Much

Greater Likelihood of Ultimate Security. It incorporates Some

Suggestions Made by Various Reviewers, And Contains Additional

Optimizations. The MD5 Algorithm is Being Placed in The Public Domain

For review and possible adoption as a standard.

For Osi-Based Applications, MD5's Object Identifier IS

MD5 Object Identifier :: =

Iso (1) Member-body (2) US (840) Rsadsi (113549) Digestalgorithm (2) 5}

In the X.509 Type Algorithmidentifier [3], The Parameters for MD5

SHOULD HAVE TYPE NULL.

2. Terminology and notament

In this document a "Word" IS A 32-BIT QUANTITY AND A "byte" is an

EIGHT-BIT QUANTITY. A SEQUENCE OF BITS CAN BE Interpreted in A

Natural Manner As a Sequence of Bytes, WHERE Each Connecutive Group

Of Eight Bits Is Interpreted As a Byte with The High-Order (MOSTSIGNIFICANT) BIT OF Each Byte Listed First. Similarly, A Sequence of

Bytes Can Be Interpreted As a Sequence of 32-Bit Words, WHERE EACH

Consecutive Group of Four bytes is interpreted as a word with the

Low-Order (Least Significant) Byte Given First.

Let X_i Denote "X Sub I". If The subscript is an expression, we

Surround IT IN BRACES, AS IN X_ {i 1}. Similarly, We Use ^ for

Superscripts (Exponentiation), SO That X ^ I Denotes x to the i-th

Power.

Let the symbol " " Denote Addition of Words (I.e., MODULO-2 ^ 32

Add x <<< S Denote the 32-bit value obtained by circularly

Shifting x left by s bit positions. let not (x) denote the

Bit-wise complement of x, and let x v y Denote the bit-wise or of x

And Y. let x xor y denote the bit-wise xor of x and y, and let xy

Denote the bit-wise and of x and y.

Rivest [Page 2]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

3. MD5 Algorithm Description

We Begin by Supposing That We Have A B-bit Message As Input, And That

We wish to find its message digest. here b is an arbitrary

Nonnegative Integer; B May Be Zero, IT NEED NOT BE A MULTIPLE OF

EIGHT, AND IT May Be Arbitrarily Large. Weimagine The Bits of The

Message Written Down As Follows:

M_0 m_1 ... m_ {b-1}

The Following Five Steps Are Performed to Compute The Message Digest

Of the message.

3.1 Step 1. Append Padding Bits

The Message IS "Padded" (Extended) SO That ITS Length (in Bits) IS

Congruent to 448, MODULO 512. That IS, The Message Is Extended SO

That it is Just 64 Bits Shy Of Being a Multiple of 512 Bits Long.padding Is Always Performed, Even IF The length of the message is

Already congruent to 448, modulo 512.

Padding is Performed As Follows: a Single "1" bit is appended to the

Message, and the "0" bits area it here, in bits of

The Padded Message Becomes Congruent To 448, Modulo 512. In All, AT

Least One Bit And at Most 512 Bits Are Appended.

3.2 Step 2. Append Length

A 64-Bit Reperesentation of B (The Length of the Message Before The Message Before

Padding bits were added) is appended to the result of the previous

Step. in the unlikely Event this b is Greater Than 2 ^ 64, THEN ONLY

The Low-Order 64 Bits of B Are Used. (THESE BITS Are Appended As Two

32-Bit Words and Appended Low-Order Word First in Accordance with The

Previous conventions.)

At this Point The Resulting Message (after Padding with Bits and with with with

b) HAS a length this is an exact multiple of 512 bits. Equivalently,

This Message Has a Length That Is An Exact Multiple Of 16 (32-bit)

Words. Let M [0 ... N-1] Denote the Words of the Resulting Message,

WHERE N IS A MULTIPLE OF 16.

3.3 Step 3. Initialize MD Buffer

A Four-Word Buffer (a, b, c, d) is buy to compute the message digest.

Here Each of A, B, C, D IS A 32-Bit Register. Thase Registers Are

Initialized to the Following Values ​​in Hexadecimal, Low-Order Bytes

First):

Rivest [Page 3]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Word a: 01 23 45 67

Word B: 89 AB CD EF

Word C: Fe DC BA 98

Word D: 76 54 32 10

3.4 Step 4. Process Message In 16-Word Blocks

We First Define Four Auxiliary Functions That Each Take As Input

Three 32-Bit Words and Produce As Output One 32-Bit Word.f (x, y, z) = xy v not (x) z

G (x, y, z) = xz v y NOT (z)

H (x, y, z) = x xor y xor

I (x, y, z) = y xor (x v not (z))

IN Each Bit Position f ACTS AS A CONDITIONAL: IF X THEN Y ELSE Z.

The Function F Could Have Been Defined Using Instead of V Since XY

And not (x) z Will Never Have 1's in The Same Bit Position.) IT IS

Interesting to NOTE THAT IF The Bits of X, Y, And Z Are INDEPENDENT

AND UNBIASED, THE Each Bit of F (x, y, z) Will Be Independent and

unbiad.

The Functions G, H, AND i are similar to the function f, in That the

ACT IN "Bitwise Parallel" to Produce Their Output from The Bits of X,

Y, and z, in Such a manner That if The Corresponding Bits of X, Y,

And z Are Independent and UNbiaD, THEN Each Bit of G (X, Y, Z),

H (x, y, z), AND i (x, y, z) will be independent and unbiad. Note That

The function h is the bit-wise "xor" or "parity" function of its

INPUTS.

THIS STEP Uses A 64-Element Table T [1 ... 64] Constructed from the

SINE FUNCTION. Let T [I] Denote The I-TH Element of the Table, Which

IS equal to the integer part of 4294967296 Times ABS (Sin (i)), WHERE I

IS in Radians. The elements of the table..............

Do The Following:

/ * Process Each 16-Word Block. * /

For i = 0 to n / 16-1 do

/ * Copy Block I Into X. * /

For J = 0 to 15 DO

Set x [j] to m [i * 16 j].

END / * OF LOOP ON J * /

/ * Save A AA, B AS BB, C AS CC, And D AS DD. * /

AA = a

BB = B

Rivest [Page 4]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

CC = C

DD = D

/ * ROUND 1. * /

/ * Let [Abcd K S i] Denote the Operation

A = B ((A F (B, C, D) x [K] T [I]) <<< S). * // * do the folload 16 Operations. * /

[ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]

[ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]

[ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]

[ABCD 12 7 13] [CDAB 14 17 15] [BCDA 15 22 16]

/ * Round 2. * /

/ * Let [Abcd K S i] Denote the Operation

A = B ((A G (B, C, D) X [K] T [I]) <<< s). * /

/ * Do the folload 16 Operations. * /

[ABCD 1 5 17] [CDAB 11 14 19] [BCDA 0 20 20]

[Abcd 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]

[ABCD 9 5 25] [CDAB 3 14 27] [BCDA 8 20 28]

[ABCD 13 5 29] [CDAB 7 14 31] [BCDA 12 20 32]

/ * ROUND 3. * /

/ * Let [Abcd K S] Denote the Operation

A = B ((A H (B, C, D) X [K] T [I]) <<< S). * /

/ * Do the folload 16 Operations. * /

[ABCD 5 4 33] [CDAB 11 16 35] [BCDA 14 23 36]

[ABCD 1 4 37] [CDAB 7 16 39] [BCDA 10 23 40]

[ABCD 13 4 41] [CDAB 3 16 43] [BCDA 6 23 44]

[ABCD 9 4 45] [CDAB 15 16 47] [BCDA 2 23 48]

/ * ROUND 4. * /

/ * Let [Abcd K S] Denote the Operation

A = B ((A I (B, C, D) X [K] T [I]) <<< s). * /

/ * Do the folload 16 Operations. * /

[ABCD 0 6 49] [CDAB 14 15 51] [BCDA 5 21 52]

[ABCD 12 6 53] [CDAB 10 15 55] [BCDA 1 21 56]

[ABCD 8 6 57] [CDAB 6 15 59] [BCDA 13 21 60]

[ABCD 4 6 61] [CDAB 2 15 63] [BCDA 9 21 64]

/ * THEN Perform The Following Additions. (That INCREMENT Eachof The Four Registers by The Value It Had Before this Block

WAS start.) * /

A = a aa

B = B BB

C = C CC

D = D DD

END / * OF LOOP ON i * /

Rivest [Page 5]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

3.5 Step 5. Output

The Message Digest Productued As Output Is A, B, C, D. That IS, WE

Begin with the low-order byte of a, and endte the high-order byte

Of D.

This Completes The Description of MD5. A Reference Implementation in

C is Given in the appendix.

4. Summary

THE MD5 Message-Digest Algorithm is Simple to Implement, and Provides

A "fingerprint" or Message Digest of a Message of Arbitrary Length.

IT IS Conjectured That The Difficulty of Coming Up with Two Messages

Having The Same Message Digest Is on The Order Of 2 ^ 64 Operations,

AND That The Difficulty of Coming Up with any Message Having a given

Message digest is on the order of 2 ^ 128 Operations. The MD5 Algorithm

HAS Been Carefully Scrutinized for Weaknesss. IT IS, HOWEVER, A

Relatively New Algorithm and Further Security Analysis is of course

Justified, as is the case with any new proposal of this sort.

5. Differences Between MD4 and MD5

The Following Are The Differences Between MD4 and MD5:

1. A Fourth Round Has Been Added.

2. Each Step Now Has A Unique Additive Constant.

3. The Function G in Round 2 Was Changed from (XY V xz v yz) to

D (z)) to make g less symmetric.

4. Each Step Now Adds in Result of the prep. This

Promotes a Faster "Avalanche Effect".

.

6. The Shift Amounts in Each Round Have Been ApproxIMately

Optimized, To Yield A Faster "Avalanche Effect." THE Shifts in

DIFFERENT ROUNDS Are DISTINCT.

Rivest [Page 6]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

References

[1] Rivest, R., "THE MD4 Message Digest Algorithm", RFC 1320, MIT AND

RSA Data Security, Inc., April 1992.

[2] Rivest, R., "THE MD4 Message Digest Algorithm", in A.j. Menezes

And S.A. Vanstone, Editors, Advances in cryptology - crypto '90

Proceedings, Pages 303-311, Springer-Verlag, 1991.

[3] ccitt recommendation X.509 (1988), "The Directory -

Authentication Framework. "

Appendix A - Reference Implementation

This appendix contains the folowing files taken from rsaref: a

Cryptographic Toolkit for Privacy-Enhanced Mail:

Global.h - Global Header File

Md5.h - Header File for MD5

MD5c.c - Source Code for MD5

For more information on rsaref, send email to .

The Appendix Also Includes The Following File:

MDDRIVER.C - TEST Driver for MD2, MD4 and MD5

The Driver Compiles for MD5 by Default But Can Compile for MD2 Or MD4

IF The Symbol Md Is Defined On The C Compiler Command Line As 2 OR 4.

The Implement IS Portable and Should Work ON Many Different

Plaforms. However, IT IS Not Difficult To Optimize The Implementation

On Particular Platforms, An Exercise Left to the Reader. for Example,

On "Little-Endian" platage-add-addressed byte in a 32-

Bit Word Is The Least Significant and There No AlignmentRestrictions, The Call To Decode In Md5Transform Can Be Replaced with

A TypeCast.

A.1 Global.h

/ * Global.h - Rsaref Types and constants

* /

/ * Prototypes SHOULD BE set to one if and only if the compiler supports

Function argument prototyping.

The Following Makes Prototypes Default To 0 if IT Has Not Already

Rivest [Page 7]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Been Defined with c compiler flags.

* /

#ifndef prototypes

#define prototypes 0

#ENDIF

/ * Pointer Defines a Generic Pointer Type * /

Typedef unsigned char * pointer;

/ * Uint2 defines a two byte word * /

Typedef unsigned short int uint2;

/ * Uint4 defines a four Byte Word * /

Typedef unsigned long int uint4;

/ * Proto_list is defined Depending On How prototypes is defined Above.

If Using Prototypes, Then Proto_List Returns the List, OtherWise IT

Returns an Empty List.

* /

#if protoypes

#define proto_list (list) List

#ELSE

#define proto_list (list) ()

#ENDIF

A.2 md5.h

/ * Md5.h - Header file for md5c.c

* /

/ * CopyRight (C) 1991-2, RSA Data Security, Inc. Created 1991. All

Rights reserved.

License to Copy and Use this Software Is Grand Provided That IT

Is Identified as The "RSA Data Security, Inc. MD5 Message-Digest

Algorithm "in All Material Menting or Referencing this Software

OR this function.

License is also granted to make and use derivative works provike

That Such Works Are Identified As "Derived from the RSA Data

Security, Inc. MD5 Message-Digest Algorithm "in All Material

Menting or referencing the derived work.

RSA Data Security, Inc. Makes no representations Concerning Eitherthe Merchantability of this Software or the suitability of this

Software for any particular purpose. it is provided "as IS"

WITHOUT EXPRESS or Implied Warranty of Any Kind.

Rivest [Page 8]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Thase Notices Must Be Retained in Any Copies of Any Part of this

Documentation and / or software.

* /

/ * MD5 context. * /

Typedef struct {

Uint4 state [4]; / ​​* state (abcd) * /

Uint4 count [2]; / * Number of Bits, MODULO 2 ^ 64 (LSB first) * /

UNSIGNED Char Buffer [64]; / ​​* Input Buffer * /

} MD5_CTX;

Void MD5init Proto_List ((MD5_CTX *));

Void MD5UPDATE PROTO_LIST

((MD5_CTX *, Unsigned CHAR *, UNSIGNED INT);

Void MD5Final Proto_List ((unsigned char [16), md5_ctx *));

A.3 md5c.c

/ * MD5C.c - RSA Data Security, Inc., MD5 Message-Digest Algorithm

* /

/ * CopyRight (C) 1991-2, RSA Data Security, Inc. Created 1991. All

Rights reserved.

License to Copy and Use this Software Is Grand Provided That IT

Is Identified as The "RSA Data Security, Inc. MD5 Message-Digest

Algorithm "in All Material Menting or Referencing this Software

OR this function.

License is also granted to make and use derivative works provike

That Such Works Are Identified As "Derived from the RSA Data

Security, Inc. MD5 Message-Digest Algorithm "in All Material

Menting or referencing the derived work.

RSA Data Security, Inc. makes no representations Concerning Either

The Merchantability of this Software or the suitability of this

Software for any particular purpose. It is provided "as" without express or implied warranty of any kind.

Thase Notices Must Be Retained in Any Copies of Any Part of this

Documentation and / or software.

* /

#include "global.h"

#include "md5.h"

/ * Constants for MD5Transform Routine.

* /

Rivest [Page 9]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

#define S11 7

#define s12 12

#define s13 17

#define s14 22

#define S21 5

#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

Static void MD5Transform Proto_List ((uint4 [4), unsigned char [64]));

Static void Encode Proto_List

((UNSIGNED CHAR *, UINT4 *, UNSIGNED INT);

Static Void Decode Proto_List

((Uint4 *, unsigned char *, unsigned int));

Static void MD5_Memcpy Proto_List ((Pointer, Pointer, Unsigned Int);

Static void MD5_MEMSET Proto_List ((Pointer, INT, Unsigned Int);

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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

}

/ * F, g, h and i are basic md5 functions.

* /

#define f (x, y, z) ((x) & (y)) | ((~ x) & (z)))))))

#define g (x, y, z) ((x) & (z)) | ((Y) & (~ z))))

#define h (x, y, z) ((x) ^ (y) ^ (z))

#define i (x, y, z) ((y) ^ ((x) | (~ z))))))

/ * Rotate_left Rotates x Left n bits.

* /

#define rotate_left (x, n) (((x) << (n)) | ((x) >> (32- (n)))))))

/ * FF, GG, HH, AND II TRANSFORMATIONS for rounds 1, 2, 3, and 4.

Rotation is Separate from addition to prevent recomputation. * /

#define FF (A, B, C, D, X, S, AC) {/

(a) = f ((b), (c), (d)) (x) (UINT4) (AC); /

(a) = rotate_left ((a), (s)); /

Rivest [Page 10]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

(a) = (b); /

}

#define GG (A, B, C, D, X, S, AC) {/

(a) = g ((b), (c), (d)) (x) (UINT4) (AC); /

(a) = rotate_left ((a), (s)); /

(a) = (b); /

}

#define HH (A, B, C, D, X, S, AC) {/

(a) = h ((b), (c), (d)) (x) (UINT4) (AC); /

(a) = rotate_left ((a), (s)); /

(a) = (b); /

}

#define II (A, B, C, D, X, S, AC) {/

(a) = I ((b), (c), (d)) (x) (UINT4) (AC); /

(a) = rotate_left ((a), (s)); /

(a) = (b); /

}

/ * MD5 Initialization. Begins AN MD5 Operation, Writing a new context.

* /

Void MD5Init (Context)

MD5_CTX * context; / * context * /

{

CONTEXT-> count [0] = context-> count [1] = 0;

/ * Load Magic Initialization Constants.

* /

Context-> State [0] = 0x67452301;

Context-> State [1] = 0xEfcdAb89;

CONTEXT-> State [2] = 0x98badcfe;

Context-> State [3] = 0x10325476;

}

/ * MD5 block update operation. Continues an MD5 Message-Digest

Operation, Processing Another Message Block, and Updating the

CONTEXT.

* /

Void Md5Update (Context, Input, Inputlen)

MD5_CTX * context; / * context * /

Unsigned char * input; / * input block * /

Unsigned int inputlen; / * Length of infut block * /

{

Unsigned Int I, Index, Partlen; / * Compute Number of Bytes MOD 64 * /

Index = (unsigned int) ((CONTEXT-> COUNT [0] >> 3) & 0x3F);

/ * Update Number of Bits * /

IF ((context-> count [0] = (uint4) Inputlen << 3))

Rivest [Page 11]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

<((Uint4) Inputlen << 3))

CONTEXT-> COUNT [1] ;

Context-> count [1] = ((uint4) Inputlen >> 29);

Partlen = 64 - INDEX;

/ * Transform As Many Times as Possible.

* /

IF (Inputlen> = Partle) {

MD5_MEMCPY

((Pointer) & context-> buffer [index], (Pointer) Input, Partle;

MD5Transform (context-> state, context-> buffer);

For (i = partlen; i 63

MD5Transform (Context-> State, & Input [i]);

INDEX = 0;

}

Else

i = 0;

/ * Buffer remaining input * /

MD5_MEMCPY

(Pointer & Context-> Buffer [Index], (Pointer) & INPUT [i],

Inputlen-i);

}

/ * MD5 Finalization. Ends an MD5 Message-Digest Operation, Writing the

The message digest and zeroining the context.

* /

Void Md5Final (Digest, Context)

Unsigned char digest [16]; / * message digest * /

MD5_CTX * context; / * context * /

{

UNSIGNED Char Bits [8];

Unsigned int index, padlen

/ * Save Number of Bits * /

ENCODE (Bits, Context-> Count, 8);

/ * Pad Out to 56 mod 64.

* /

Index = (unsigned int) ((CONTEXT-> COUNT [0] >> 3) & 0x3F);

Padlen = (INDEX <56)? (56 - INDEX): (120 - index);

MD5UPDATE (Context, Padding, Padlen);

/ * Append length (Before Padding) * /

MD5UPDATE (Context, Bits, 8);

Rivest [Page 12] RFC 1321 MD5 Message-Digest Algorithm April 1992

/ * Store State in Digest * /

ENCODE (Digest, Context-> State, 16);

/ * ZEROIZE SENSITIVE INFORMATION.

* /

MD5_MEMSET ((Pointer) Context, 0, SizeOf (* Context));

}

/ * MD5 Basic Transformation. Transforms State Based on Block.

* /

Static Void Md5Transform (State, Block)

Uint4 state [4];

UNSIGNED Char Block [64];

{

Uint4 a = state [0], b = state [1], c = state [2], d = state [3], x [16];

Decode (X, Block, 64);

/ * 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, 0xFFFFF5BB1); / * 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, 0x2441453); / * 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 * /

Rivest [Page 13]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

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); / * 60 * /

II (A, B, C, D, X [4], S41, 0XF7537E82); / * 61 * /

II (D, A, B, C, X [11], S42, 0XBD3AF235); / * 62 * /

II (C, D, A, B, X [2], S43, 0x2AD7D2BB); / * 63 * /

II (B, C, D, A, X [9], S44, 0XEB86D391); / * 64 * /

State [0] = a;

State [1] = B;

State [2] = C;

State [3] = D;

/ * ZEROIZE SENSITIVE INFORMATION.

Rivest [Page 14]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

* /

MD5_MEMSET ((Pointer) X, 0, SizeOf (x));

}

/ * ENCODES INPUT (UINT4) INTO OUTPUT (UNSIGNED Char). Assumes Len IS

A Multiple of 4.

* /

Static void Encode (Output, Input, Len)

Unsigned char * OUTPUT;

Uint4 * input;

Unsigned int Len;

{

UNSIGNED INT I, J;

For (i = 0, j = 0; j

Output [j] = (unsigned char) (INPUT [I] & 0xFF);

Output [J 1] = (Unsigned Char) ((INPUT [I] >> 8) & 0xFF);

OUTPUT [J 2] = (unsigned char) ((INPUT [I] >> 16) & 0xFF);

OUTPUT [J 3] = (Unsigned Char) ((INPUT [I] >> 24) & 0xFF);

}

}

/ * Decodes Input (unsigned char) INTO OUTPUT (uint4). Assumes Len IS

A Multiple of 4.

* /

Static void Decode (Output, Input, Len)

Uint4 * output;

Unsigned char * input;

Unsigned int Len;

{

UNSIGNED INT I, J;

For (i = 0, j = 0; j

((UINT4) INPUT [J 2]) << 16) | ((uint4) INPUT [J 3]) << 24);

}

/ * Note: Replace "for loop" with standard memcpy if Possible.

* /

Static void MD5_Memcpy (Output, Input, LEN)

Pointer Output;

Pointer INPUT;

Unsigned int Len;

{

Unsigned Int i;

For (i = 0; i

Rivest [Page 15]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Output [i] = INPUT [i];

}

/ * Note: Replace "for loop" with standard memset if Possible.

* /

Static void MD5_MEMSET (Output, Value, Len)

Pointer Output;

Int value;

Unsigned int Len;

{

Unsigned Int i;

For (i = 0; i

(CHAR *) OUTPUT [I] = (char) value;

}

A.4 MDDRIVER.C

/ * MDDRIVER.C - TEST Driver for MD2, MD4 and MD5

* /

/ * CopyRight (C) 1990-2, RSA Data Security, Inc. Created 1990. All

Rights reserved.

RSA Data Security, Inc. makes no representations Concerning Either

The Merchantability of this Software or the suitability of this

Software for any particular purpose. it is provided "as IS"

WITHOUT EXPRESS or Implied Warranty of Any Kind.

Thase Notices Must Be Retained in Any Copies of Any Part of this

Documentation and / or software.

* /

/ * The following makes md default to md5 if it has not already Been

Defined with c compiler flags.

* /

#ifndef md

#define MD MD5

#ENDIF

#include

#include

#include

#include "global.h"

#if md == 2

#include "md2.h"

#ENDIF

#if md == 4

Rivest [Page 16] RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

#include "md4.h"

#ENDIF

#if md == 5

#include "md5.h"

#ENDIF

/ * Length of test block, Number of Test Blocks.

* /

#define test_block_len 1000

#define test_block_count 1000

Static void mdstring proto_list ((char *));

Static void MDTIMETRIAL PROTO_LIST ((void));

Static void MDTestSuite Proto_List ((void));

Static void Mdfile Proto_List ((char *));

Static void Mdfilter Proto_List (Void));

Static void mdprint proto_list (unsigned char [16]));

#if md == 2

#define MD_CTX MD2_CTX

#define mdinit md2init

#define mdupdate md2update

#define mdfinal md2final

#ENDIF

#if md == 4

#define MD_CTX MD4_CTX

#define mdinit md4init

#define mdupdate md4update

#define mdfinal MD4Final

#ENDIF

#if md == 5

#define MD_CTX MD5_CTX

#define mdinit md5init

#define mdupdate md5Update

#define mdfinal MD5Final

#ENDIF

/ * Main driver.

ARGUMENTS (May Be Anyi):

-sstring - Digests String

-T - Runs Time Trial

-x - Runs Test Script

Filename - Digests file

(NONE) - Digests Standard Input

* /

Int main (argc, argv)

Int argc;

Rivest [Page 17]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Char * argv [];

{

INT I;

IF (Argc> 1)

For (i = 1; i

IF (Argv [i] [0] == '-' && Argv [i] [1] == 's')

MDString (Argv [i] 2);

Else IF (strcmp (argv [i], "-t") == 0)

MDTIMETRIAL ();

Else IF (strcmp (argv [i], "-x") == 0)

Mdtestsuite ();

Elsemdfile (Argv [I]);

Else

Mdfilter ();

Return (0);

}

/ * Digests a string andprints the result.

* /

Static void mdstring (String)

CHAR * STRING;

{

MD_CTX Context;

Unsigned char digest [16];

Unsigned int LEN = strlen (String);

MDINIT (& context);

MDUPDATE (& Context, String, Len);

Mdfinal (Digest, & Context);

Printf ("MD% D (/"% s / ") =", MD, String);

MDPrint (Digest);

Printf ("/ n");

}

/ * MEASURES TIME to Digest Test_block_count test_block_len-byte

BLOCKS.

* /

Static void mdtimetrial ()

{

MD_CTX Context;

Time_t endtime, starttime;

Unsigned char block [test_block_len], digest [16];

Unsigned Int i;

Rivest [Page 18]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

PRINTF

("MD% D Time Trial. Digesting% D% D-Byte Blocks ...", MD,

Test_block_len, test_block_count);

/ * Initialize Block * /

For (i = 0; i

Block [i] = (unsigned char) (i & 0xff);

/ * Start Timer * /

Time (& StartTime);

/ * Digest block * /

MDINIT (& context);

For (i = 0; i

MDUPDATE (& Context, Block, Test_block_len);

Mdfinal (Digest, & Context);

/ * Stop Timer * /

Time (& EndTime);

Printf ("DONE / N");

Printf ("Digest =");

MDPrint (Digest);

Printf ("/ NTIME =% ld seconds / n", (long);

PRINTF

("Speed ​​=% ld bytes / second / n",

(long) Test_block_len * (long) Test_block_count / (endtime-starttime);

}

/ * Digests a Reference Suite of strings and prints the results.

* /

Static void mdtestsuite ()

{

Printf ("MD% D Test Suite: / N", MD);

MDString (""); mdstring ("a");

MDString ("ABC");

MDString ("Message Digest");

MDString ("AbcDefghijklmnopqrStuvwxyz");

MDString

("Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789);

MDString

("1234567890123456789012345678901234567890 /

"12345678901234567890");

}

/ * Digests a File and Prints the result.

Rivest [Page 19]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

* /

Static void mdfile (filename)

Char * filename;

{

File * file;

MD_CTX Context;

Int Len;

Unsigned char buffer [1024], digest [16];

IF ((file = fopen (filename, "rb")) == NULL)

Printf ("% s can't be opened / n", filename);

Else {

MDINIT (& context);

While (Len = FREAD (Buffer, 1, 1024, File)

MDUPDATE (& Context, Buffer, Len);

Mdfinal (Digest, & Context);

Fclose (file);

Printf ("MD% D (% s) =", MD, FileName);

MDPrint (Digest);

Printf ("/ n");

}

}

/ * Digests The Standard Input and Prints The Result.

* /

Static void mdfilter ()

{

MD_CTX Context;

Int Len;

Unsigned char buffer [16], digest [16];

MDINIT (& context);

While (Len = Fread (buffer, 1, 16, stdin))

MDUPDATE (& Context, Buffer, Len);

Mdfinal (Digest, & Context);

MDPrint (Digest);

Printf ("/ n");

}

/ * Prints a message digest in Hexadecimal.

* /

Static void mdprint (Digest)

Unsigned char digest [16];

{

Rivest [Page 20]

RFC 1321 MD5 MESSAGE-Digest Algorithm April 1992

Unsigned Int i;

For (i = 0; i <16; i )

Printf ("% 02x", Digest [i]);

A.5 Test Suite

The MD5 Test Suite (Driver Option "-x") Should Print The Following

Results:

MD5 Test Suite:

MD5 ("") = D41D8CD98F00B204E9800998ECF8427E

MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661

MD5 ("ABC") = 900150983cd24fb0d6963f7d28e17f72

MD5 ("Message Digest") = F96B697D7CB7938D525A2F31AAF161D0

MD5 ("AbcdefghijklmnopqrStuvwxyz") = C3FCD3D76192E4007DFB496CCA67E13B

MD5 ("AbcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrStuvwxyz0123456789) =

D174AB98D277D9F5A5611C2C9F419D9F

MD5 ("123456789012345678901234567890123456789012345678901234567890123456

78901234567890 ") = 57EDF4A22BE3C955AC49DA2E2107B67A

Security considances

The Level of Security Discussed in this memo is considered to be

Sufficient for Implementing Very High Security Hybrid Digital-

Signature Schemes Based ON MD5 and a public-key cryptosystem.

Author's address

Ronald L. Rivest

Massachusetts Institute of Technology Of Technology

Laboratory for Computer Science

NE43-324

545 TECHNOLOGY SQUARE

Cambridge, MA 02139-1986

Phone: (617) 253-5880

Email: rivest@theory.lcs.mit.edu

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

New Post(0)