wok view syslinux/stuff/extra/md5sum.c @ rev 21820

syslinux/kbd: check kbd malloc pointer
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Aug 25 11:41:26 2019 +0200 (14 months ago)
parents 1fdfb6902d41
children 54edd52e6caa
line source
1 /*
2 * Based on busybox code.
3 *
4 * Compute MD5 checksum of strings according to the
5 * definition of MD5 in RFC 1321 from April 1992.
6 *
7 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
8 *
9 * Copyright (C) 1995-1999 Free Software Foundation, Inc.
10 * Copyright (C) 2001 Manuel Novoa III
11 * Copyright (C) 2003 Glenn L. McGrath
12 * Copyright (C) 2003 Erik Andersen
13 * Copyright (C) 2010 Denys Vlasenko
14 * Copyright (C) 2012 Pascal Bellard
15 *
16 * Licensed under GPLv2 or later
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <console.h>
25 #include <com32.h>
26 #include <syslinux/config.h>
27 #include <syslinux/disk.h>
29 #define ALIGN1
31 static uint8_t wbuffer[64]; /* always correctly aligned for uint64_t */
32 static uint64_t total64; /* must be directly before hash[] */
33 static uint32_t hash[8]; /* 4 elements for md5, 5 for sha1, 8 for sha256 */
35 /* Emit a string of hex representation of bytes */
36 static char* bin2hex(char *p)
37 {
38 static const char bb_hexdigits_upcase[] ALIGN1 = "0123456789abcdef";
39 int count = 16;
40 const char *cp = (const char *) hash;
41 while (count) {
42 unsigned char c = *cp++;
43 /* put lowercase hex digits */
44 *p++ = bb_hexdigits_upcase[c >> 4];
45 *p++ = bb_hexdigits_upcase[c & 0xf];
46 count--;
47 }
48 return p;
49 }
51 //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n))))
52 static uint32_t rotl32(uint32_t x, unsigned n)
53 {
54 return (x << n) | (x >> (32 - n));
55 }
57 static void md5_process_block64(void);
59 /* Feed data through a temporary buffer.
60 * The internal buffer remembers previous data until it has 64
61 * bytes worth to pass on.
62 */
63 static void common64_hash(const void *buffer, size_t len)
64 {
65 unsigned bufpos = total64 & 63;
67 total64 += len;
69 while (1) {
70 unsigned remaining = 64 - bufpos;
71 if (remaining > len)
72 remaining = len;
73 /* Copy data into aligned buffer */
74 memcpy(wbuffer + bufpos, buffer, remaining);
75 len -= remaining;
76 buffer = (const char *)buffer + remaining;
77 bufpos += remaining;
78 /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */
79 bufpos -= 64;
80 if (bufpos != 0)
81 break;
82 /* Buffer is filled up, process it */
83 md5_process_block64();
84 /*bufpos = 0; - already is */
85 }
86 }
88 /* Process the remaining bytes in the buffer */
89 static void common64_end(void)
90 {
91 unsigned bufpos = total64 & 63;
92 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
93 wbuffer[bufpos++] = 0x80;
95 /* This loop iterates either once or twice, no more, no less */
96 while (1) {
97 unsigned remaining = 64 - bufpos;
98 memset(wbuffer + bufpos, 0, remaining);
99 /* Do we have enough space for the length count? */
100 if (remaining >= 8) {
101 /* Store the 64-bit counter of bits in the buffer */
102 uint64_t t = total64 << 3;
103 /* wbuffer is suitably aligned for this */
104 *(uint64_t *) (&wbuffer[64 - 8]) = t;
105 }
106 md5_process_block64();
107 if (remaining >= 8)
108 break;
109 bufpos = 0;
110 }
111 }
113 /* These are the four functions used in the four steps of the MD5 algorithm
114 * and defined in the RFC 1321. The first function is a little bit optimized
115 * (as found in Colin Plumbs public domain implementation).
116 * #define FF(b, c, d) ((b & c) | (~b & d))
117 */
118 #undef FF
119 #undef FG
120 #undef FH
121 #undef FI
122 #define FF(b, c, d) (d ^ (b & (c ^ d)))
123 #define FG(b, c, d) FF(d, b, c)
124 #define FH(b, c, d) (b ^ c ^ d)
125 #define FI(b, c, d) (c ^ (b | ~d))
127 /* Hash a single block, 64 bytes long and 4-byte aligned */
128 static void md5_process_block64(void)
129 {
130 /* Before we start, one word to the strange constants.
131 They are defined in RFC 1321 as
132 T[i] = (int)(4294967296.0 * fabs(sin(i))), i=1..64
133 */
134 static const uint32_t C_array[] = {
135 /* round 1 */
136 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
137 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
138 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
139 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
140 /* round 2 */
141 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
142 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
143 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
144 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
145 /* round 3 */
146 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
147 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
148 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05,
149 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
150 /* round 4 */
151 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
152 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
153 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
154 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
155 };
156 static const char P_array[] ALIGN1 = {
157 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* 1 */
158 1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, /* 2 */
159 5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2, /* 3 */
160 0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9 /* 4 */
161 };
162 uint32_t *words = (void*) wbuffer;
163 uint32_t A = hash[0];
164 uint32_t B = hash[1];
165 uint32_t C = hash[2];
166 uint32_t D = hash[3];
168 static const char S_array[] ALIGN1 = {
169 7, 12, 17, 22,
170 5, 9, 14, 20,
171 4, 11, 16, 23,
172 6, 10, 15, 21
173 };
174 const uint32_t *pc;
175 const char *pp;
176 const char *ps;
177 int i;
178 uint32_t temp;
181 pc = C_array;
182 pp = P_array;
183 ps = S_array - 4;
185 for (i = 0; i < 64; i++) {
186 if ((i & 0x0f) == 0)
187 ps += 4;
188 temp = A;
189 switch (i >> 4) {
190 case 0:
191 temp += FF(B, C, D);
192 break;
193 case 1:
194 temp += FG(B, C, D);
195 break;
196 case 2:
197 temp += FH(B, C, D);
198 break;
199 case 3:
200 temp += FI(B, C, D);
201 }
202 temp += words[(int) (*pp++)] + *pc++;
203 temp = rotl32(temp, ps[i & 3]);
204 temp += B;
205 A = D;
206 D = C;
207 C = B;
208 B = temp;
209 }
210 /* Add checksum to the starting values */
211 hash[0] += A;
212 hash[1] += B;
213 hash[2] += C;
214 hash[3] += D;
216 }
217 #undef FF
218 #undef FG
219 #undef FH
220 #undef FI
222 /* Initialize structure containing state of computation.
223 * (RFC 1321, 3.3: Step 3)
224 */
225 static void md5_begin(void)
226 {
227 hash[0] = 0x67452301;
228 hash[1] = 0xefcdab89;
229 hash[2] = 0x98badcfe;
230 hash[3] = 0x10325476;
231 total64 = 0;
232 }
234 /* Used also for sha1 and sha256 */
235 #define md5_hash common64_hash
237 /* Process the remaining bytes in the buffer and put result from CTX
238 * in first 16 bytes following RESBUF. The result is always in little
239 * endian byte order, so that a byte-wise output yields to the wanted
240 * ASCII representation of the message digest.
241 */
242 #define md5_end common64_end
244 /*
245 * Copyright (C) 2003 Glenn L. McGrath
246 * Copyright (C) 2003-2004 Erik Andersen
247 *
248 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
249 */
251 #ifdef EXTRA
252 #define WITH_UNROCKRIDGE
253 #endif
254 #ifdef WITH_UNROCKRIDGE
255 static char *unrockridge(const char *name)
256 {
257 static char buffer[256];
258 int i = 0, j = 8;
259 while (*name && i < 255) {
260 char c = *name++;
261 //if (c == '\\') c = '/';
262 if (c == '.') {
263 for (j = i; --j >= 0 && buffer[j] != '/';)
264 if (buffer[j] == '.') buffer[j] = '_';
265 if (i - j > 9) i = j + 9;
266 j = i + 4;
267 }
268 else if (c == '/') j = i + 9;
269 else if (i >= j) continue;
270 else if (c >= 'a' && c <= 'z') c += 'A' - 'a';
271 else if ((c < 'A' || c > 'Z') && (c < '0' || c > '9')) c = '_';
272 buffer[i++] = c;
273 }
274 buffer[i] = 0;
275 return buffer;
276 }
277 #endif
279 static uint8_t *hash_file(const char *filename)
280 {
281 int src_fd, count;
282 uint8_t in_buf[4096];
283 static uint8_t hash_value[16*2+1];
285 src_fd = open(filename, O_RDONLY);
286 #ifdef WITH_UNROCKRIDGE
287 if (src_fd < 0) {
288 src_fd = open(unrockridge(filename), O_RDONLY);
289 }
290 #endif
292 if (src_fd < 0) {
293 return NULL;
294 }