3. Message Digest (MD5)
• MD5 was developed in 1991 by Ronald Rivest
• MD5 is a cryptographic hash function algorithm that
takes the message as input of any length and changes
it into a fixed-length message of 16 bytes.
• MD5 algorithm stands for the message-digest
algorithm.
• The output of MD5 (Digest size) is always 128 bits.
3
Plaintext = multiples of 512 bits
Message digest = 128 bits
4. Message Digest (MD5)
• Uses
It is used for file authentication.
In a web application, it is used for security
purposes. e.g. Secure password of users etc.
Using this algorithm, We can store our password in
128 bits format
4
5. Message Digest (MD5)
• Working of MD5 Algorithm
1. Append Padding Bits
2. Append Length Bits
3. Initialize MD buffer
4. Process Each 512-bit Block
5
7. Message Digest (MD5)
1. Append Padding Bits
In the first step, we add padding bits in the original
message in such a way that the total length of the
message is 64 bits less than the exact multiple of 512
7
Length(original message + padding bits) = 512 * i – 64 where i = 1,2,3 . . .
8. Message Digest (MD5)
Example:
• Plaintext Message: “ They are deterministic”
T 54 01010100
h 68 01101000
e 65 01100101
y 79 01111001
…
01010100 01101000 01100101 01111001 00100000 01100001
01110010 01100101 00100000 01100100 01100101 01110100
01100101 01110010 01101101 01101001 01101110 01101001
01110011 01110100 01101001 01100011
Total 22 letters including blank space 22*8 = 176 bits
ASCII
11. Message Digest (MD5)
2. Append Length Bits
Add length bit in the output of the first step in such a way
that the total number of the bits is the perfect multiple of
512.
Simply, here we add the 64-bit as a length bit in the output of
the first step.
i.e. output of first step = 512 * n – 64
length bits = 64.
After adding both, the length of the message = 512 * n 11
13. Message Digest (MD5)
3. Initialize MD buffer (– to store output)
Initialize four different buffers, namely A, B, C, and
D.
These buffers are 32 bits each and are initialized as
follows:
13
A = 01 23 45 67
B = 89 ab cd ef
C = fe dc ba 98
D = 76 54 32 10
Each buffers= 32bits
Need 4 buffers (A, B, C, D) –
Store 32*4=128 bits
14. Message Digest (MD5)
4. Process each block:
Each 512-bit block gets broken down further into 16 sub-
blocks of 32 bits each.
There are four rounds of operations, with each round
utilizing all the sub-blocks, the buffers, and a constant array
value
This constant array can be denoted as K[1] K[64].
Each of the sub-blocks are denoted as M[0] M[15]
14
Length of message = 512 * n
Each block= 512 bits
17. Message Digest (MD5)
• Each of these M inputs (512 bits)
are used in every single round,
they are added in different orders.
In second round:
M1, M6, M11, M0, M5, M10, M15, M4,
M9, M14, M3, M8, M13, M2, M7, M12
In third round
M5, M8, M11, M14, M1, M4, M7, M10,
M13, M0, M3, M6, M9, M12, M15, M2
In fourth round
M0, M7, M14, M5, M12, M3, M10, M1,
M8, M15, M6, M13, M4, M11, M2, M9
17
Operation carried out
on Every 512 bit block
21. Message Digest (MD5)
Each block 4 rounds of operation
• In each round 16 operations are performed
– Total 64 operations are performed in 4 rounds.
• 1st round 16 operations will be performed
• 2nd round 16 operations will be performed
• 3rd round 16 operations will be performed
• 4th round 16 operations will be performed.
• We apply a different function on each round i.e. for the
– 1st round we apply the F function,
– 2nd G function
– 3rd for the H function
– 4th for the I function.
21
22. Message Digest (MD5)
• Each round has 16 steps of the form:
b = b+((a+f(b,c,d)+M[i]+K[i])<<<s)
22
• Constant array : K[1] K[64].
• K[1-16]
• K[17-32]
• K[33-48]
• K[49-64]
• Each of the sub-blocks : M[0] M[15]
• M[0] 32bits
• M[1] 32bits
• …
• M[15] 32bits
g(b,c,d) is a different nonlinear
function in each round (F,G,H,I)
23. Message Digest (MD5)
• g(b,c,d) is a different nonlinear function in each round
(F,G,H,I)
• Perform OR, AND, XOR, and NOT (basically these are
logic gates) for calculating functions
• Use 3 buffers for each function
23
F(B,C,D) = (B AND C) OR (NOT B AND D)
G(B,C,D) = (B AND C) OR (C AND NOT D)
H(B,C,D) = B XOR C XOR D
I(B,C,D) = C XOR (B OR NOT D)
29. 29
MD5 F, G, H and I functions
F(B, C, D) = (B∧C)∨(¬B∧D)
G(B, C, D) = (B∧D)∨(C∧¬D)
H(B, C, D) =B⊕C⊕D
I(B, C, D) = C⊕(B∨¬D)
F (89abcdef, fedcba98, 76543210) = (89abcdef AND fedcba98) OR (NOT-89abcdef AND 76543210)
F(B, C, D) = 88888888 OR 76543210
= fedcba98
Modular Addition 2bd309f0
Left bit shift
0010 1011 1101 0011 0000 1001 1111 0000
30. 001 0101 1110 1001 1000 0100 1111 1000 0xxx xxxx
Shift 7 bits left
1110 1001 1000 0100 1111 1000 0001 0101
In hexa
e984f815
A – 01234567
B – 89abcdef
C – fedcba98
D – 76543210
(X + Y) mod Z
X B = 89abcdef
Y e984f815
Z100000000 (232)
(X + Y) mod Z
= (89abcdef + e984f895) mod 100000000
= 7330C604
A – 76543210
B – 7330c604
C – 89abcdef
D – fedcba98
2bd309f0
First operation in Round1 is completed
Similarly do other 63 operations and generate
the final Message Digest of 128 bits