source: S-port/trunk/Core/Src/MD5.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 3.7 KB
Line 
1#include "MD5.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include "string.h"
5
6// Constants are the integer part of the sines of integers (in radians) * 2^32.
7const uint32_t k[64] = {
80xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
90xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
100x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
110x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
120xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
130xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
140x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
150xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
160xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
170xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
180x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
190xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
200xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
210x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
220x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
230xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
24
25#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
26
27#define PUT_ULONG_LE(n,b,i) \
28{ \
29 (b)[(i) ] = (unsigned char) ( (n) ); \
30 (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \
31 (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \
32 (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \
33}
34
35
36
37void md5(uint8_t *initial_msg, size_t initial_len, uint8_t *hash) {
38
39 uint8_t *msg = NULL;
40 int new_len;
41 uint32_t bits_len;
42 int offset;
43 uint32_t *w;
44 uint32_t a, b, c, d, i, f, g, temp;
45
46 const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
47 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
48 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
49 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
50
51 uint32_t h0 = 0x67452301;
52 uint32_t h1 = 0xefcdab89;
53 uint32_t h2 = 0x98badcfe;
54 uint32_t h3 = 0x10325476;
55
56
57 for(new_len = initial_len * 8 + 1; new_len % 512 != 448; new_len++);
58 new_len /= 8;
59
60 msg = (uint8_t*)calloc(new_len + 64, 1); // also appends "0" bits
61 // (we alloc also 64 extra bytes...)
62 memcpy(msg, initial_msg, initial_len);
63 msg[initial_len] = 128; // write the "1" bit
64
65 bits_len = 8*initial_len; // note, we append the len
66 memcpy(msg + new_len, &bits_len, 4); // in bits at the end of the buffer
67
68 for(offset=0; offset<new_len; offset += (512/8)) {
69
70 // break chunk into sixteen 32-bit words w[j], 0 ? j ? 15
71 w = (uint32_t *) (msg + offset);
72
73 // Initialize hash value for this chunk:
74 a = h0;
75 b = h1;
76 c = h2;
77 d = h3;
78
79 // Main loop:
80 for(i = 0; i<64; i++) {
81
82 if (i < 16) {
83 f = (b & c) | ((~b) & d);
84 g = i;
85 } else if (i < 32) {
86 f = (d & b) | ((~d) & c);
87 g = (5*i + 1) % 16;
88 } else if (i < 48) {
89 f = b ^ c ^ d;
90 g = (3*i + 5) % 16;
91 } else {
92 f = c ^ (b | (~d));
93 g = (7*i) % 16;
94 }
95 temp = d;
96 d = c;
97 c = b;
98 b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
99 a = temp;
100
101 }
102 h0 += a;
103 h1 += b;
104 h2 += c;
105 h3 += d;
106 }
107
108 PUT_ULONG_LE(h0, hash, 0 );
109 PUT_ULONG_LE(h1, hash, 4 );
110 PUT_ULONG_LE(h2, hash, 8 );
111 PUT_ULONG_LE(h3, hash, 12 );
112
113 free(msg);
114}
Note: See TracBrowser for help on using the repository browser.