GADGET-4
Md5.cc
Go to the documentation of this file.
1#include "gadgetconfig.h"
2
8#ifdef DEBUG_MD5
9
10#include <mpi.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14#include "../data/allvars.h"
15#include "../data/dtypes.h"
16
17/*
18 **********************************************************************
19 ** md5.c **
20 ** RSA Data Security, Inc. MD5 Message Digest Algorithm **
21 ** Created: 2/17/90 RLR **
22 ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version **
23 ** Revised: 09/2018 by V. Springel to allow C++ compilation **
24 **********************************************************************
25 */
26
27/*
28 **********************************************************************
29 ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
30 ** **
31 ** License to copy and use this software is granted provided that **
32 ** it is identified as the "RSA Data Security, Inc. MD5 Message **
33 ** Digest Algorithm" in all material mentioning or referencing this **
34 ** software or this function. **
35 ** **
36 ** License is also granted to make and use derivative works **
37 ** provided that such works are identified as "derived from the RSA **
38 ** Data Security, Inc. MD5 Message Digest Algorithm" in all **
39 ** material mentioning or referencing the derived work. **
40 ** **
41 ** RSA Data Security, Inc. makes no representations concerning **
42 ** either the merchantability of this software or the suitability **
43 ** of this software for any particular purpose. It is provided "as **
44 ** is" without express or implied warranty of any kind. **
45 ** **
46 ** These notices must be retained in any copies of any part of this **
47 ** documentation and/or software. **
48 **********************************************************************
49 */
50
51/* -- include the following line if the md5.h header file is separate -- */
52#include "Md5.h"
53
54/* forward declaration */
55static void Transform(UINT4 *buf, UINT4 *in);
56static void MD5Update(MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen);
57
58static unsigned char PADDING[64] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
62
63/* F, G and H are basic MD5 functions: selection, majority, parity */
64#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
65#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
66#define H(x, y, z) ((x) ^ (y) ^ (z))
67#define I(x, y, z) ((y) ^ ((x) | (~z)))
68
69/* ROTATE_LEFT rotates x left n bits */
70#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
71
72/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
73/* Rotation is separate from addition to prevent recomputation */
74#define FF(a, b, c, d, x, s, ac) \
75 { \
76 (a) += F((b), (c), (d)) + (x) + (UINT4)(ac); \
77 (a) = ROTATE_LEFT((a), (s)); \
78 (a) += (b); \
79 }
80#define GG(a, b, c, d, x, s, ac) \
81 { \
82 (a) += G((b), (c), (d)) + (x) + (UINT4)(ac); \
83 (a) = ROTATE_LEFT((a), (s)); \
84 (a) += (b); \
85 }
86#define HH(a, b, c, d, x, s, ac) \
87 { \
88 (a) += H((b), (c), (d)) + (x) + (UINT4)(ac); \
89 (a) = ROTATE_LEFT((a), (s)); \
90 (a) += (b); \
91 }
92#define II(a, b, c, d, x, s, ac) \
93 { \
94 (a) += I((b), (c), (d)) + (x) + (UINT4)(ac); \
95 (a) = ROTATE_LEFT((a), (s)); \
96 (a) += (b); \
97 }
98
99void MD5Init(MD5_CTX *mdContext)
100{
101 mdContext->i[0] = mdContext->i[1] = (UINT4)0;
102
103 /* Load magic initialization constants.
104 */
105 mdContext->buf[0] = (UINT4)0x67452301;
106 mdContext->buf[1] = (UINT4)0xefcdab89;
107 mdContext->buf[2] = (UINT4)0x98badcfe;
108 mdContext->buf[3] = (UINT4)0x10325476;
109}
110
111void MD5UpdateLong(MD5_CTX *mdContext, unsigned char *inBuf, unsigned long long inLenLong)
112{
113 while(inLenLong > 0)
114 {
115 unsigned int inLen = 0x10000000;
116 if(inLen > inLenLong)
117 inLen = inLenLong;
118 MD5Update(mdContext, inBuf, inLen);
119 inBuf += inLen;
120 inLenLong -= inLen;
121 }
122}
123
124void MD5Update(MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen)
125{
126 UINT4 in[16];
127 int mdi;
128 unsigned int i, ii;
129
130 /* compute number of bytes mod 64 */
131 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
132
133 /* update number of bits */
134 if((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0])
135 mdContext->i[1]++;
136 mdContext->i[0] += ((UINT4)inLen << 3);
137 mdContext->i[1] += ((UINT4)inLen >> 29);
138
139 while(inLen--)
140 {
141 /* add new character to buffer, increment mdi */
142 mdContext->in[mdi++] = *inBuf++;
143
144 /* transform if necessary */
145 if(mdi == 0x40)
146 {
147 for(i = 0, ii = 0; i < 16; i++, ii += 4)
148 in[i] = (((UINT4)mdContext->in[ii + 3]) << 24) | (((UINT4)mdContext->in[ii + 2]) << 16) |
149 (((UINT4)mdContext->in[ii + 1]) << 8) | ((UINT4)mdContext->in[ii]);
150 Transform(mdContext->buf, in);
151 mdi = 0;
152 }
153 }
154}
155
156void MD5Final(MD5_CTX *mdContext)
157{
158 UINT4 in[16];
159 int mdi;
160 unsigned int i, ii;
161 unsigned int padLen;
162
163 /* save number of bits */
164 in[14] = mdContext->i[0];
165 in[15] = mdContext->i[1];
166
167 /* compute number of bytes mod 64 */
168 mdi = (int)((mdContext->i[0] >> 3) & 0x3F);
169
170 /* pad out to 56 mod 64 */
171 padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi);
172 MD5Update(mdContext, PADDING, padLen);
173
174 /* append length in bits and transform */
175 for(i = 0, ii = 0; i < 14; i++, ii += 4)
176 in[i] = (((UINT4)mdContext->in[ii + 3]) << 24) | (((UINT4)mdContext->in[ii + 2]) << 16) | (((UINT4)mdContext->in[ii + 1]) << 8) |
177 ((UINT4)mdContext->in[ii]);
178 Transform(mdContext->buf, in);
179
180 /* store buffer in digest */
181 for(i = 0, ii = 0; i < 4; i++, ii += 4)
182 {
183 mdContext->digest[ii] = (unsigned char)(mdContext->buf[i] & 0xFF);
184 mdContext->digest[ii + 1] = (unsigned char)((mdContext->buf[i] >> 8) & 0xFF);
185 mdContext->digest[ii + 2] = (unsigned char)((mdContext->buf[i] >> 16) & 0xFF);
186 mdContext->digest[ii + 3] = (unsigned char)((mdContext->buf[i] >> 24) & 0xFF);
187 }
188}
189
190/* Basic MD5 step. Transform buf based on in.
191 */
192static void Transform(UINT4 *buf, UINT4 *in)
193{
194 UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
195
196 /* Round 1 */
197#define S11 7
198#define S12 12
199#define S13 17
200#define S14 22
201 FF(a, b, c, d, in[0], S11, 3614090360); /* 1 */
202 FF(d, a, b, c, in[1], S12, 3905402710); /* 2 */
203 FF(c, d, a, b, in[2], S13, 606105819); /* 3 */
204 FF(b, c, d, a, in[3], S14, 3250441966); /* 4 */
205 FF(a, b, c, d, in[4], S11, 4118548399); /* 5 */
206 FF(d, a, b, c, in[5], S12, 1200080426); /* 6 */
207 FF(c, d, a, b, in[6], S13, 2821735955); /* 7 */
208 FF(b, c, d, a, in[7], S14, 4249261313); /* 8 */
209 FF(a, b, c, d, in[8], S11, 1770035416); /* 9 */
210 FF(d, a, b, c, in[9], S12, 2336552879); /* 10 */
211 FF(c, d, a, b, in[10], S13, 4294925233); /* 11 */
212 FF(b, c, d, a, in[11], S14, 2304563134); /* 12 */
213 FF(a, b, c, d, in[12], S11, 1804603682); /* 13 */
214 FF(d, a, b, c, in[13], S12, 4254626195); /* 14 */
215 FF(c, d, a, b, in[14], S13, 2792965006); /* 15 */
216 FF(b, c, d, a, in[15], S14, 1236535329); /* 16 */
217
218 /* Round 2 */
219#define S21 5
220#define S22 9
221#define S23 14
222#define S24 20
223 GG(a, b, c, d, in[1], S21, 4129170786); /* 17 */
224 GG(d, a, b, c, in[6], S22, 3225465664); /* 18 */
225 GG(c, d, a, b, in[11], S23, 643717713); /* 19 */
226 GG(b, c, d, a, in[0], S24, 3921069994); /* 20 */
227 GG(a, b, c, d, in[5], S21, 3593408605); /* 21 */
228 GG(d, a, b, c, in[10], S22, 38016083); /* 22 */
229 GG(c, d, a, b, in[15], S23, 3634488961); /* 23 */
230 GG(b, c, d, a, in[4], S24, 3889429448); /* 24 */
231 GG(a, b, c, d, in[9], S21, 568446438); /* 25 */
232 GG(d, a, b, c, in[14], S22, 3275163606); /* 26 */
233 GG(c, d, a, b, in[3], S23, 4107603335); /* 27 */
234 GG(b, c, d, a, in[8], S24, 1163531501); /* 28 */
235 GG(a, b, c, d, in[13], S21, 2850285829); /* 29 */
236 GG(d, a, b, c, in[2], S22, 4243563512); /* 30 */
237 GG(c, d, a, b, in[7], S23, 1735328473); /* 31 */
238 GG(b, c, d, a, in[12], S24, 2368359562); /* 32 */
239
240 /* Round 3 */
241#define S31 4
242#define S32 11
243#define S33 16
244#define S34 23
245 HH(a, b, c, d, in[5], S31, 4294588738); /* 33 */
246 HH(d, a, b, c, in[8], S32, 2272392833); /* 34 */
247 HH(c, d, a, b, in[11], S33, 1839030562); /* 35 */
248 HH(b, c, d, a, in[14], S34, 4259657740); /* 36 */
249 HH(a, b, c, d, in[1], S31, 2763975236); /* 37 */
250 HH(d, a, b, c, in[4], S32, 1272893353); /* 38 */
251 HH(c, d, a, b, in[7], S33, 4139469664); /* 39 */
252 HH(b, c, d, a, in[10], S34, 3200236656); /* 40 */
253 HH(a, b, c, d, in[13], S31, 681279174); /* 41 */
254 HH(d, a, b, c, in[0], S32, 3936430074); /* 42 */
255 HH(c, d, a, b, in[3], S33, 3572445317); /* 43 */
256 HH(b, c, d, a, in[6], S34, 76029189); /* 44 */
257 HH(a, b, c, d, in[9], S31, 3654602809); /* 45 */
258 HH(d, a, b, c, in[12], S32, 3873151461); /* 46 */
259 HH(c, d, a, b, in[15], S33, 530742520); /* 47 */
260 HH(b, c, d, a, in[2], S34, 3299628645); /* 48 */
261
262 /* Round 4 */
263#define S41 6
264#define S42 10
265#define S43 15
266#define S44 21
267 II(a, b, c, d, in[0], S41, 4096336452); /* 49 */
268 II(d, a, b, c, in[7], S42, 1126891415); /* 50 */
269 II(c, d, a, b, in[14], S43, 2878612391); /* 51 */
270 II(b, c, d, a, in[5], S44, 4237533241); /* 52 */
271 II(a, b, c, d, in[12], S41, 1700485571); /* 53 */
272 II(d, a, b, c, in[3], S42, 2399980690); /* 54 */
273 II(c, d, a, b, in[10], S43, 4293915773); /* 55 */
274 II(b, c, d, a, in[1], S44, 2240044497); /* 56 */
275 II(a, b, c, d, in[8], S41, 1873313359); /* 57 */
276 II(d, a, b, c, in[15], S42, 4264355552); /* 58 */
277 II(c, d, a, b, in[6], S43, 2734768916); /* 59 */
278 II(b, c, d, a, in[13], S44, 1309151649); /* 60 */
279 II(a, b, c, d, in[4], S41, 4149444226); /* 61 */
280 II(d, a, b, c, in[11], S42, 3174756917); /* 62 */
281 II(c, d, a, b, in[2], S43, 718787259); /* 63 */
282 II(b, c, d, a, in[9], S44, 3951481745); /* 64 */
283
284 buf[0] += a;
285 buf[1] += b;
286 buf[2] += c;
287 buf[3] += d;
288}
289
290/*
291 **********************************************************************
292 ** End of md5.c **
293 ******************************* (cut) ********************************
294 */
295
296#endif
definition and prototypes for MD5 routines
void MD5UpdateLong(MD5_CTX *mdContext, unsigned char *inBuf, unsigned long long inLenLong)
void MD5Init(MD5_CTX *mdContext)
unsigned long int UINT4
Definition: Md5.h:47
void MD5Final(MD5_CTX *mdContext)
Definition: Md5.h:51
unsigned char in[64]
Definition: Md5.h:54
UINT4 buf[4]
Definition: Md5.h:53
unsigned char digest[16]
Definition: Md5.h:55
UINT4 i[2]
Definition: Md5.h:52