luc115.c (7049B)
1 /* Obtain MD5 checksum of the following strings and check whether they are same: 2 "Six slippery snails slid slowly seaward." 3 "Six silppery snails slid slowly seaward." 4 */ 5 /* Let Us C, Chap- 23 (Security Programming), Qn No.: D(a) */ 6 7 /* This file is auto-generated by a bot. */ 8 /* This code is not compiled; it is for reference only. */ 9 10 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <stdint.h> 15 16 /* * Self-contained MD5 Implementation 17 * Based on the public domain reference implementation (RFC 1321) logic. 18 */ 19 20 // --- MD5 Implementation Start --- 21 typedef struct { 22 uint64_t size; // Size of input in bytes 23 uint32_t buffer[4]; // Current accumulation of hash 24 uint8_t input[64]; // Input to be processed 25 } MD5Context; 26 27 #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) 28 #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) 29 #define H(x, y, z) ((x) ^ (y) ^ (z)) 30 #define I(x, y, z) ((y) ^ ((x) | (~z))) 31 32 #define ROTL(x, s) (((x) << (s)) | ((x) >> (32 - (s)))) 33 34 #define STEP(f, a, b, c, d, x, t, s) \ 35 (a) += f((b), (c), (d)) + (x) + (t); \ 36 (a) = ROTL((a), (s)); \ 37 (a) += (b); 38 39 void md5Transform(uint32_t state[4], const uint8_t block[64]) { 40 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16]; 41 int i, j; 42 43 // Decode block into x 44 for (i = 0, j = 0; j < 64; i++, j += 4) 45 x[i] = ((uint32_t)block[j]) | (((uint32_t)block[j + 1]) << 8) | 46 (((uint32_t)block[j + 2]) << 16) | (((uint32_t)block[j + 3]) << 24); 47 48 // Round 1 49 STEP(F, a, b, c, d, x[0], 0xd76aa478, 7); 50 STEP(F, d, a, b, c, x[1], 0xe8c7b756, 12); 51 STEP(F, c, d, a, b, x[2], 0x242070db, 17); 52 STEP(F, b, c, d, a, x[3], 0xc1bdceee, 22); 53 STEP(F, a, b, c, d, x[4], 0xf57c0faf, 7); 54 STEP(F, d, a, b, c, x[5], 0x4787c62a, 12); 55 STEP(F, c, d, a, b, x[6], 0xa8304613, 17); 56 STEP(F, b, c, d, a, x[7], 0xfd469501, 22); 57 STEP(F, a, b, c, d, x[8], 0x698098d8, 7); 58 STEP(F, d, a, b, c, x[9], 0x8b44f7af, 12); 59 STEP(F, c, d, a, b, x[10], 0xffff5bb1, 17); 60 STEP(F, b, c, d, a, x[11], 0x895cd7be, 22); 61 STEP(F, a, b, c, d, x[12], 0x6b901122, 7); 62 STEP(F, d, a, b, c, x[13], 0xfd987193, 12); 63 STEP(F, c, d, a, b, x[14], 0xa679438e, 17); 64 STEP(F, b, c, d, a, x[15], 0x49b40821, 22); 65 66 // Round 2 67 STEP(G, a, b, c, d, x[1], 0xf61e2562, 5); 68 STEP(G, d, a, b, c, x[6], 0xc040b340, 9); 69 STEP(G, c, d, a, b, x[11], 0x265e5a51, 14); 70 STEP(G, b, c, d, a, x[0], 0xe9b6c7aa, 20); 71 STEP(G, a, b, c, d, x[5], 0xd62f105d, 5); 72 STEP(G, d, a, b, c, x[10], 0x02441453, 9); 73 STEP(G, c, d, a, b, x[15], 0xd8a1e681, 14); 74 STEP(G, b, c, d, a, x[4], 0xe7d3fbc8, 20); 75 STEP(G, a, b, c, d, x[9], 0x21e1cde6, 5); 76 STEP(G, d, a, b, c, x[14], 0xc33707d6, 9); 77 STEP(G, c, d, a, b, x[3], 0xf4d50d87, 14); 78 STEP(G, b, c, d, a, x[8], 0x455a14ed, 20); 79 STEP(G, a, b, c, d, x[13], 0xa9e3e905, 5); 80 STEP(G, d, a, b, c, x[2], 0xfcefa3f8, 9); 81 STEP(G, c, d, a, b, x[7], 0x676f02d9, 14); 82 STEP(G, b, c, d, a, x[12], 0x8d2a4c8a, 20); 83 84 // Round 3 85 STEP(H, a, b, c, d, x[5], 0xfffa3942, 4); 86 STEP(H, d, a, b, c, x[8], 0x8771f681, 11); 87 STEP(H, c, d, a, b, x[11], 0x6d9d6122, 16); 88 STEP(H, b, c, d, a, x[14], 0xfde5380c, 23); 89 STEP(H, a, b, c, d, x[1], 0xa4beea44, 4); 90 STEP(H, d, a, b, c, x[4], 0x4bdecfa9, 11); 91 STEP(H, c, d, a, b, x[7], 0xf6bb4b60, 16); 92 STEP(H, b, c, d, a, x[10], 0xbebfbc70, 23); 93 STEP(H, a, b, c, d, x[13], 0x289b7ec6, 4); 94 STEP(H, d, a, b, c, x[0], 0xeaa127fa, 11); 95 STEP(H, c, d, a, b, x[3], 0xd4ef3085, 16); 96 STEP(H, b, c, d, a, x[6], 0x04881d05, 23); 97 STEP(H, a, b, c, d, x[9], 0xd9d4d039, 4); 98 STEP(H, d, a, b, c, x[12], 0xe6db99e5, 11); 99 STEP(H, c, d, a, b, x[15], 0x1fa27cf8, 16); 100 STEP(H, b, c, d, a, x[2], 0xc4ac5665, 23); 101 102 // Round 4 103 STEP(I, a, b, c, d, x[0], 0xf4292244, 6); 104 STEP(I, d, a, b, c, x[7], 0x432aff97, 10); 105 STEP(I, c, d, a, b, x[14], 0xab9423a7, 15); 106 STEP(I, b, c, d, a, x[5], 0xfc93a039, 21); 107 STEP(I, a, b, c, d, x[12], 0x655b59c3, 6); 108 STEP(I, d, a, b, c, x[3], 0x8f0ccc92, 10); 109 STEP(I, c, d, a, b, x[10], 0xffeff47d, 15); 110 STEP(I, b, c, d, a, x[1], 0x85845dd1, 21); 111 STEP(I, a, b, c, d, x[8], 0x6fa87e4f, 6); 112 STEP(I, d, a, b, c, x[15], 0xfe2ce6e0, 10); 113 STEP(I, c, d, a, b, x[6], 0xa3014314, 15); 114 STEP(I, b, c, d, a, x[13], 0x4e0811a1, 21); 115 STEP(I, a, b, c, d, x[4], 0xf7537e82, 6); 116 STEP(I, d, a, b, c, x[11], 0xbd3af235, 10); 117 STEP(I, c, d, a, b, x[2], 0x2ad7d2bb, 15); 118 STEP(I, b, c, d, a, x[9], 0xeb86d391, 21); 119 120 state[0] += a; 121 state[1] += b; 122 state[2] += c; 123 state[3] += d; 124 } 125 126 void md5Init(MD5Context *ctx) { 127 ctx->size = 0; 128 ctx->buffer[0] = 0x67452301; 129 ctx->buffer[1] = 0xefcdab89; 130 ctx->buffer[2] = 0x98badcfe; 131 ctx->buffer[3] = 0x10325476; 132 } 133 134 void md5Update(MD5Context *ctx, const uint8_t *input, size_t inputLen) { 135 size_t i, index, partLen; 136 index = (size_t)((ctx->size >> 3) & 0x3f); 137 ctx->size += (uint64_t)inputLen * 8; 138 partLen = 64 - index; 139 140 if (inputLen >= partLen) { 141 memcpy(&ctx->input[index], input, partLen); 142 md5Transform(ctx->buffer, ctx->input); 143 for (i = partLen; i + 63 < inputLen; i += 64) 144 md5Transform(ctx->buffer, &input[i]); 145 index = 0; 146 } else { 147 i = 0; 148 } 149 memcpy(&ctx->input[index], &input[i], inputLen - i); 150 } 151 152 void md5Final(uint8_t digest[16], MD5Context *ctx) { 153 uint8_t bits[8]; 154 size_t index, padLen; 155 static const uint8_t PADDING[64] = {0x80}; // Remaining zeros implicit 156 157 // Save number of bits 158 for (int i = 0; i < 8; i++) 159 bits[i] = (uint8_t)((ctx->size >> (i * 8)) & 0xff); 160 161 index = (size_t)((ctx->size >> 3) & 0x3f); 162 padLen = (index < 56) ? (56 - index) : (120 - index); 163 md5Update(ctx, PADDING, padLen); 164 md5Update(ctx, bits, 8); 165 166 // Encode state into digest 167 for (int i = 0; i < 16; i++) 168 digest[i] = (uint8_t)((ctx->buffer[i >> 2] >> ((i & 3) * 8)) & 0xff); 169 } 170 // --- MD5 Implementation End --- 171 172 void print_hash(uint8_t *digest) { 173 for (int i = 0; i < 16; i++) printf("%02x", digest[i]); 174 printf("\n"); 175 } 176 177 int main() { 178 char str1[] = "Six slippery snails slid slowly seaward."; 179 char str2[] = "Six silppery snails slid slowly seaward."; // Typo 'silppery' 180 181 uint8_t hash1[16], hash2[16]; 182 MD5Context ctx; 183 184 printf("String 1: %s\n", str1); 185 md5Init(&ctx); 186 md5Update(&ctx, (uint8_t*)str1, strlen(str1)); 187 md5Final(hash1, &ctx); 188 printf("MD5: "); 189 print_hash(hash1); 190 191 printf("\nString 2: %s\n", str2); 192 md5Init(&ctx); 193 md5Update(&ctx, (uint8_t*)str2, strlen(str2)); 194 md5Final(hash2, &ctx); 195 printf("MD5: "); 196 print_hash(hash2); 197 198 // Compare 199 int same = 1; 200 for(int i=0; i<16; i++) if(hash1[i] != hash2[i]) same = 0; 201 202 printf("\nConclusion: The MD5 checksums are %s.\n", same ? "IDENTICAL" : "DIFFERENT"); 203 if (!same) printf("(Even a small change in input creates a completely different hash)\n"); 204 205 return 0; 206 }