RFC 1321 - THE MD5 Message-Digest Algorithm
-------------------------------------------------- ------------------------------
NetWork Working Group R. RivesTrequest 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 Not Specify AN Internet Standard. Distribution of this Memo is Unlimited.
Acknowlegments
WE Would Like To Thank Don Coppersmith, Burt Kaliski, Ralph Merkle, David Chaum, And Noam Nisan For Numerous Helpful Comments and suggestions.
Table of contents
1. 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 Considerations 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 produces 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 produce 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 being encrypted with a private (secret) key under a public-key cryptosystem such as RSA.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 "conservative" 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 electric-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 (most significant) 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 ...............
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 (IE, MODULO-2 ^ 32 Additional). Let x <<< S Denote The 32-bit value obtained by circularly shifting (rotating) 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.
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. WE Imagine The Bits of The Message Written Down As Follows:
M_0 m_1 ... m_ {b-1}
THE FOLLOWING 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 then "0" bits are appended so that the length 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 representation of b (the length of the message before the padding bits were added) is appended to the result of the previous step. In the unlikely event that b is greater than 2 ^ 64, then only the low-order 64 Bits of B Are Used.
AT THIS POINT The Resulting Message (After Padding with Bits and With b) HAS a Length That 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 Used 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):
Word A: 01 23 45 67 Word B: 89 AB CD EF Word C: Fe DC Ba 98 Word D: 76 54 32 103.4 Step 4. Process Message In 16-Word Blocks
WORKET WORD INE 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 z i (x, Y, z) = y xor (x v not (z))
IN Each Bit Position F ACTS AS A CONDITIONAL: IF X THEEN Y Else Z. The Function F Could Have Been Defined USING INSTEAD OF V SINECE XY AND NOT (x) z WILL NEVER HAVE 1'S IS SAME BIT POSITION.) IT IS INTERESTING TO NOTE THAT INDEPENT AND UNBISED, 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, Each Bit of g (x, y, z), H (x, y, z), AND i (x, y, z) Will be independent and unbiased. Note That That Function h is the bit-wise "xor" or "parity" function of itction 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 Are Given in The appendix.
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 as aa, b as bb, c as cc, and d as dd. * / Aa = a bb = bcc = C DD = D
/ * ROUND 1. * / / / * let [abcd ksi] 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] [ABCD 8 7 9] [DABC 9 12 10] [BCDA 11 22 12] [ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
/ * ROUND 2. * / / * let [abcd ksi] 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 [BCDA 4 20 24] [CDAB 3 14 27] [BCDA 8 20 28] [ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
/ * ROUND 3. * / / / * let [abcd kst] denote the Operation A = B ((A H (B, C, D) X [K] T [I]) <<< s). * / / * Do the folload 16 Operations. * / [Abcd 5 4 34] [CDAB 11 16 35] [BCDA 14 23 36] [ABCD 1 4 37] [DABC 4 11 38] [CDAB [BCDA 10 23 40] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44] [ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
/ * ROUND 4. * / / / * let [abcd kst] 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] [DABC 3 10 54] [CDAB [BCDA 1 21 57] [DABC 15 10 59] [BCDA 13 21 60] [ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] / * Then Perform The Following Additions. (That INCREMENT EACH OF THE FOUR Registers by The Value It Had Before This Block Was Started.) * / A = A AA B = B BB C = C CC D = D DD
END / * OF LOOP ON i * /
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 weaknesses. 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 (XZ V y NOT (z)) to make g less symmetric.
4. Each Step Now Adds in The Result of the Previous Step. This Promotes A Faster "Avalanche Effect".
5. The Order in Which Input Words Areced in Rounds 2 and 3 is changed, to make eachse pattern less like each other.
6. The Shift Amounts in Each Round Have Been Approximately Optimized, To Yield A Faster "Avalanche Effect" The Shifts in Different Rounds Are Distinct.
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 Following 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 implementation 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" platforms where the lowest-addressed byte in A 32- BIT WORD IS The Least Significant and There No Alignment Restrictions, The Call To Decode In Md5Transform Can Be Replaced with a Typecast.a.1 Global.h
/ * Global.h - rsaref type and constants * /
/ * Prototypes shop be set to one if and only means
Been Defined with c compiler flags. * / # ifndef prototypes # define prototypes 0 # ENDIF
/ * Pointer Defines a generic pointer type * / typef unsigned char * POINTER;
/ * Uint2 defines a two byte word * / type inTEF UNSIGNED SHORT INT D;
/ * Uint4 defines a four byte word * / type inTEF 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 PROTOTYPES # 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. Allrights Reserved.
License to copy and use this software is granted provided that itis identified as the "RSA Data Security, Inc. MD5 Message-DigestAlgorithm" in all material mentioning or referencing this softwareor this function.
License is also granted to make and use derivative works providedthat such works are identified as "derived from the RSA DataSecurity, Inc. MD5 Message-Digest Algorithm" in all materialmentioning or referencing the derived work.RSA Data Security, Inc. makes no representations concerning Eitherthe Merchantability of this Software or the suitability of thissoftware for any particular purpose. it is provides "as" without express or implied warranty of any kind.
THESE NOTIES OF Any Part of thisDocumentation and / or software. * /
/ * Md5 context. * / Typef 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. Allrights Reserved.
License to copy and use this software is granted provided that itis identified as the "RSA Data Security, Inc. MD5 Message-DigestAlgorithm" in all material mentioning or referencing this softwareor this function.
License is also granted to make and use derivative works providedthat such works are identified as "derived from the RSA DataSecurity, Inc. MD5 Message-Digest Algorithm" in all materialmentioning or referencing the derived work.
RSA Data Security, Inc. makes no representations concerning eitherthe merchantability of this software or the suitability of thissoftware for any particular purpose. It is provided "as is" without express or implied warranty of any kind.These notices must be retained in any copies of Any Part of thisDocumentation and / or software. * /
#include "global.h" #include "md5.h"
/ * Constants for MD5Transform Routine. * /
#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
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};
/ * F, g, h and i are Basic MD5 functions. * / # Define f (x, y, z) ((x) & (y)) | (~ x) & (z))) # define G (X, Y, 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)); / (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)) (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)) <((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
INDEX = 0;} else i = 0;
/ * Buffer remaining infut * / 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 zeroizing 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);
/ * Store State in Digest * / Encode (Digest, Context-> State, 16);
/ * ZEROIZE SENSITIVE INFORMATION. * / MD5_MEMSET ((Pointer) Context, 0, SizeOf (* Context));
/ * MD5 Basic Transformation. * / 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, 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, 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 * /
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, 0xFFFFA3942) ; / * 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.
* / 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
. / * 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 / * 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 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 A.4 MDDRIVER.C / * MDDRIVER.C - TEST Driver for MD2, MD4 and MD5 * / / * CopyRight (C) 1990-2, RSA Data Security, Inc. Created 1990. Allrights Reserved. RSA Data Security, Inc. makes no representations concerning eitherthe merchantability of this software or the suitability of thissoftware for any particular purpose. It is provided "as is" without express or implied warranty of any kind. THESE NOTIES OF Any Part of thisDocumentation and / or software. * / / * The folload makes md default to md5 if it has not already been defined with c compiler flags. * / # IFNDef MD # define md MD5 # ENDIF #include #include "md4.h" # Endif # if md == 5 # include "md5.h" # Endif / * Length of test block, number of test block. * / # 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: -sstring - Digests String -t - Runs Time Trial-x - Runs Test Script FileName - Digests File (None) - Digests Standard Input * / Int Main (Argc, Argv) Int Argcc; Char * argv []; {INT i; IF (argc> 1) for (i = 1; i 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 the time to digest test_block_count test_block_len-byte blocks. * / static void mdtime () {md_ctx context; time_t endtime, starttime; unsigned char block [test_block_len], Digest [16]; unsigned int i; Printf ("MD% D Time Trial. Digesting% D% D-Byte Blocks ...", MD, Test_Block_len, Test_block_count; / * Initialize block * / for (i = 0; i / * START TIMER * / TIME (& StartTime); / * Digest block * / mdinit (& context); for (i = 0; i / * Stop Timer * / Time (& EndTime); Printf ("DONE / N"); Printf ("Digest ="); mdprint (Digest); Printf ("/ ntime =% ld seconds / n", (long); Printf ("speedf (" speted = % 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/1234567890123456789012345678901234567890") } / * Digests a File and Prints the result. * / 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; 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. * / 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]; { 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 ( "") = d41d8cd98f00b204e9800998ecf8427eMD5 ( "a") = 0cc175b9c0f1b6a831c399e269772661MD5 ( "abc") = 900150983cd24fb0d6963f7d28e17f72MD5 ( "message digest") = f96b697d7cb7938d525a2f31aaf161d0MD5 ( "abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13bMD5 ( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = d174ab98d277d9f5a5611c2c9f419d9fMD5 ( "12345678901234567890123456789012345678901234567890123456789012345678901234567890 ") = 57edf4a22be3c955ac49da2e2107b67a Security ConsiderationsThe 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, laboratory for Computer Science, MA 02139-1986 Phone: (617) 253-5880 Email: Rivest@theory.lcs.mit.edu Comment on RFC 1321