Support changing players' science box in the editor.
[freeciv.git] / utility / md5.c
bloba5d13beeddca1e5b02a83bca707122d26a8e67a0
1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
15 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
16 * MD5 Message-Digest Algorithm (RFC 1321).
18 * Homepage:
19 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
21 * Author:
22 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
24 * This software was written by Alexander Peslyak in 2001. No copyright is
25 * claimed, and the software is hereby placed in the public domain.
26 * In case this attempt to disclaim copyright and place the software in the
27 * public domain is deemed null and void, then the software is
28 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
29 * general public under the following terms:
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted.
34 * There's ABSOLUTELY NO WARRANTY, express or implied.
36 * (This is a heavily cut-down "BSD license".)
38 * This differs from Colin Plumb's older public domain implementation in that
39 * no exactly 32-bit integer data type is required (any 32-bit or wider
40 * unsigned integer data type will do), there's no compile-time endianness
41 * configuration, and the function prototypes match OpenSSL's. No code from
42 * Colin Plumb's implementation has been reused; this comment merely compares
43 * the properties of the two independent implementations.
45 * The primary goals of this implementation are portability and ease of use.
46 * It is meant to be fast, but not as fast as possible. Some known
47 * optimizations are not included to reduce source code size and avoid
48 * compile-time configuration.
52 * Freeciv changes relative to upstream:
53 * - Reformatting and other cosmetic changes to fit in with Freeciv coding
54 * conventions.
55 * - Don't expose the init/update/final interface to the rest of Freeciv
56 * in md5.h; just expose a single high-level function which calculates
57 * the checksum on a buffer (with the result as an ASCII string).
59 * Upstream version is revision 1.13 from:
60 * http://cvsweb.openwall.com/cgi/cvsweb.cgi/Owl/packages/popa3d/popa3d/md5/md5.c
63 #ifdef HAVE_CONFIG_H
64 #include <fc_config.h>
65 #endif
67 #include <stdio.h>
68 #include <string.h>
70 #include "md5.h"
72 /* Any 32-bit or wider unsigned integer data type will do */
73 typedef unsigned int MD5_u32plus;
75 typedef struct {
76 MD5_u32plus lo, hi;
77 MD5_u32plus a, b, c, d;
78 unsigned char buffer[64];
79 MD5_u32plus block[16];
80 } MD5_CTX;
83 * The basic MD5 functions.
85 * F and G are optimized compared to their RFC 1321 definitions for
86 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
87 * implementation.
89 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
90 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
91 #define H(x, y, z) (((x) ^ (y)) ^ (z))
92 #define H2(x, y, z) ((x) ^ ((y) ^ (z)))
93 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
96 * The MD5 transformation for all four rounds.
98 #define STEP(f, a, b, c, d, x, t, s) \
99 (a) += f((b), (c), (d)) + (x) + (t); \
100 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
101 (a) += (b);
104 * SET reads 4 input bytes in little-endian byte order and stores them
105 * in a properly aligned word in host byte order.
107 #define SET(n) \
108 (ctx->block[(n)] = \
109 (MD5_u32plus)ptr[(n) * 4] | \
110 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
111 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
112 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
113 #define GET(n) \
114 (ctx->block[(n)])
117 * This processes one or more 64-byte data blocks, but does NOT update
118 * the bit counters. There are no alignment requirements.
120 static const void *body(MD5_CTX *ctx, const void *data, unsigned long size)
122 const unsigned char *ptr;
123 MD5_u32plus a, b, c, d;
124 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
126 ptr = (const unsigned char *)data;
128 a = ctx->a;
129 b = ctx->b;
130 c = ctx->c;
131 d = ctx->d;
133 do {
134 saved_a = a;
135 saved_b = b;
136 saved_c = c;
137 saved_d = d;
139 /* Round 1 */
140 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
141 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
142 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
143 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
144 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
145 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
146 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
147 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
148 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
149 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
150 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
151 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
152 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
153 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
154 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
155 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
157 /* Round 2 */
158 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
159 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
160 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
161 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
162 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
163 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
164 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
165 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
166 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
167 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
168 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
169 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
170 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
171 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
172 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
173 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
175 /* Round 3 */
176 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
177 STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
178 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
179 STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
180 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
181 STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
182 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
183 STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
184 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
185 STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
186 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
187 STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
188 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
189 STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
190 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
191 STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
193 /* Round 4 */
194 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
195 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
196 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
197 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
198 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
199 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
200 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
201 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
202 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
203 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
204 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
205 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
206 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
207 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
208 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
209 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
211 a += saved_a;
212 b += saved_b;
213 c += saved_c;
214 d += saved_d;
216 ptr += 64;
217 } while (size -= 64);
219 ctx->a = a;
220 ctx->b = b;
221 ctx->c = c;
222 ctx->d = d;
224 return ptr;
227 static void MD5_Init(MD5_CTX *ctx)
229 ctx->a = 0x67452301;
230 ctx->b = 0xefcdab89;
231 ctx->c = 0x98badcfe;
232 ctx->d = 0x10325476;
234 ctx->lo = 0;
235 ctx->hi = 0;
238 static void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size)
240 MD5_u32plus saved_lo;
241 unsigned long used, available;
243 saved_lo = ctx->lo;
244 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
245 ctx->hi++;
246 ctx->hi += size >> 29;
248 used = saved_lo & 0x3f;
250 if (used) {
251 available = 64 - used;
253 if (size < available) {
254 memcpy(&ctx->buffer[used], data, size);
255 return;
258 memcpy(&ctx->buffer[used], data, available);
259 data = (unsigned char *)data + available;
260 size -= available;
261 body(ctx, ctx->buffer, 64);
264 if (size >= 64) {
265 data = body(ctx, data, size & ~(unsigned long)0x3f);
266 size &= 0x3f;
269 memcpy(ctx->buffer, data, size);
272 static void MD5_Final(unsigned char *result, MD5_CTX *ctx)
274 unsigned long used, available;
276 used = ctx->lo & 0x3f;
278 ctx->buffer[used++] = 0x80;
280 available = 64 - used;
282 if (available < 8) {
283 memset(&ctx->buffer[used], 0, available);
284 body(ctx, ctx->buffer, 64);
285 used = 0;
286 available = 64;
289 memset(&ctx->buffer[used], 0, available - 8);
291 ctx->lo <<= 3;
292 ctx->buffer[56] = ctx->lo;
293 ctx->buffer[57] = ctx->lo >> 8;
294 ctx->buffer[58] = ctx->lo >> 16;
295 ctx->buffer[59] = ctx->lo >> 24;
296 ctx->buffer[60] = ctx->hi;
297 ctx->buffer[61] = ctx->hi >> 8;
298 ctx->buffer[62] = ctx->hi >> 16;
299 ctx->buffer[63] = ctx->hi >> 24;
301 body(ctx, ctx->buffer, 64);
303 result[0] = ctx->a;
304 result[1] = ctx->a >> 8;
305 result[2] = ctx->a >> 16;
306 result[3] = ctx->a >> 24;
307 result[4] = ctx->b;
308 result[5] = ctx->b >> 8;
309 result[6] = ctx->b >> 16;
310 result[7] = ctx->b >> 24;
311 result[8] = ctx->c;
312 result[9] = ctx->c >> 8;
313 result[10] = ctx->c >> 16;
314 result[11] = ctx->c >> 24;
315 result[12] = ctx->d;
316 result[13] = ctx->d >> 8;
317 result[14] = ctx->d >> 16;
318 result[15] = ctx->d >> 24;
320 memset(ctx, 0, sizeof(*ctx));
323 /**************************************************************************
324 Compute MD5 message digest for LEN octets beginning at BUFFER. The
325 result is always in little endian octet order, so that an octet-wise
326 output yields to the wanted ASCII representation of the message
327 digest.
328 **************************************************************************/
329 static void md5_buffer(const unsigned char *buffer, size_t len,
330 unsigned char *resblock)
332 MD5_CTX ctx;
334 /* Initialize the computation context. */
335 MD5_Init(&ctx);
337 /* Process whole buffer but last len % 64 bytes. */
338 MD5_Update(&ctx, buffer, len);
340 /* Put result in desired memory area. */
341 MD5_Final(resblock, &ctx);
344 /**************************************************************************
345 From a string, create an md5sum and store it in output in hex form.
346 **************************************************************************/
347 void create_md5sum(const unsigned char *input, int len,
348 char output[MD5_HEX_BYTES + 1])
350 unsigned char bin_buffer[MAX_MD5_BIN_BYTES];
351 size_t cnt;
352 char *ptr = output;
354 md5_buffer(input, len, bin_buffer);
356 for (cnt = 0; cnt < (MD5_HEX_BYTES / 2); cnt++, ptr += 2) {
357 sprintf(ptr, "%02x", bin_buffer[cnt]);