Diffstat (limited to 'gammu/emb/common/misc/coding/md5.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | gammu/emb/common/misc/coding/md5.c | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/gammu/emb/common/misc/coding/md5.c b/gammu/emb/common/misc/coding/md5.c new file mode 100644 index 0000000..30fe33f --- a/dev/null +++ b/gammu/emb/common/misc/coding/md5.c | |||
@@ -0,0 +1,298 @@ | |||
1 | /* Taken from ReHash (see http://www.reichlsoft.de.vu/) and released | ||
2 | * under GPL/LGPL with permission from ReHash author | ||
3 | * Dominik Reichl <dominik.reichl@t-online.de>, Germany | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | ********************************************************************** | ||
8 | ** MD5.cpp ** | ||
9 | ** ** | ||
10 | ** - Style modified by Tony Ray, January 2001 ** | ||
11 | ** Added support for randomizing initialization constants ** | ||
12 | ** - Style modified by Dominik Reichl, April 2003 ** | ||
13 | ** Optimized code ** | ||
14 | ** ** | ||
15 | ** MD5.c ** | ||
16 | ** RSA Data Security, Inc. MD5 Message Digest Algorithm ** | ||
17 | ** Created: 2/17/90 RLR ** | ||
18 | ** Revised: 1/91 SRD,AJ,BSK,JT Reference C Version ** | ||
19 | ********************************************************************** | ||
20 | */ | ||
21 | |||
22 | /* | ||
23 | ********************************************************************** | ||
24 | ** MD5.h -- Header file for implementation of MD5 ** | ||
25 | ** RSA Data Security, Inc. MD5 Message Digest Algorithm ** | ||
26 | ** Created: 2/17/90 RLR ** | ||
27 | ** Revised: 12/27/90 SRD,AJ,BSK,JT Reference C version ** | ||
28 | ** Revised (for MD5): RLR 4/27/91 ** | ||
29 | ** -- G modified to have y&~z instead of y&z ** | ||
30 | ** -- FF, GG, HH modified to add in last register done ** | ||
31 | ** -- Access pattern: round 2 works mod 5, round 3 works mod 3 ** | ||
32 | ** -- distinct additive constant for each step ** | ||
33 | ** -- round 4 added, working mod 7 ** | ||
34 | ********************************************************************** | ||
35 | */ | ||
36 | |||
37 | /* | ||
38 | ********************************************************************** | ||
39 | ** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. ** | ||
40 | ** ** | ||
41 | ** License to copy and use this software is granted provided that ** | ||
42 | ** it is identified as the "RSA Data Security, Inc. MD5 Message ** | ||
43 | ** Digest Algorithm" in all material mentioning or referencing this ** | ||
44 | ** software or this function. ** | ||
45 | ** ** | ||
46 | ** License is also granted to make and use derivative works ** | ||
47 | ** provided that such works are identified as "derived from the RSA ** | ||
48 | ** Data Security, Inc. MD5 Message Digest Algorithm" in all ** | ||
49 | ** material mentioning or referencing the derived work. ** | ||
50 | ** ** | ||
51 | ** RSA Data Security, Inc. makes no representations concerning ** | ||
52 | ** either the merchantability of this software or the suitability ** | ||
53 | ** of this software for any particular purpose. It is provided "as ** | ||
54 | ** is" without express or implied warranty of any kind. ** | ||
55 | ** ** | ||
56 | ** These notices must be retained in any copies of any part of this ** | ||
57 | ** documentation and/or software. ** | ||
58 | ********************************************************************** | ||
59 | */ | ||
60 | |||
61 | #include <stdio.h> | ||
62 | #include <stdlib.h> | ||
63 | |||
64 | #include "md5.h" | ||
65 | |||
66 | /* Typedef a 32 bit type */ | ||
67 | #ifndef UINT4 | ||
68 | typedef unsigned long int UINT4; | ||
69 | #endif | ||
70 | |||
71 | /* Data structure for MD5 (Message Digest) computation */ | ||
72 | typedef struct { | ||
73 | UINT4 i[2]; /* Number of _bits_ handled mod 2^64 */ | ||
74 | UINT4 buf[4]; /* Scratch buffer */ | ||
75 | unsigned char in[64]; /* Input buffer */ | ||
76 | unsigned char digest[16]; /* Actual digest after MD5Final call */ | ||
77 | } MD5_CTX; | ||
78 | |||
79 | /* Padding */ | ||
80 | static unsigned char MD5_PADDING[64] = { | ||
81 | 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
82 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
83 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
84 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
87 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
88 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | ||
89 | }; | ||
90 | |||
91 | /* MD5_F, MD5_G and MD5_H are basic MD5 functions: selection, majority, parity */ | ||
92 | #define MD5_F(x, y, z) (((x) & (y)) | ((~x) & (z))) | ||
93 | #define MD5_G(x, y, z) (((x) & (z)) | ((y) & (~z))) | ||
94 | #define MD5_H(x, y, z) ((x) ^ (y) ^ (z)) | ||
95 | #define MD5_I(x, y, z) ((y) ^ ((x) | (~z))) | ||
96 | |||
97 | /* ROTATE_LEFT rotates x left n bits */ | ||
98 | #ifndef ROTATE_LEFT | ||
99 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) | ||
100 | #endif | ||
101 | |||
102 | /* MD5_FF, MD5_GG, MD5_HH, and MD5_II transformations for rounds 1, 2, 3, and 4 */ | ||
103 | /* Rotation is separate from addition to prevent recomputation */ | ||
104 | #define MD5_FF(a, b, c, d, x, s, ac) {(a) += MD5_F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
105 | #define MD5_GG(a, b, c, d, x, s, ac) {(a) += MD5_G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
106 | #define MD5_HH(a, b, c, d, x, s, ac) {(a) += MD5_H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
107 | #define MD5_II(a, b, c, d, x, s, ac) {(a) += MD5_I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
108 | |||
109 | /* Constants for transformation */ | ||
110 | #define MD5_S11 7 /* Round 1 */ | ||
111 | #define MD5_S12 12 | ||
112 | #define MD5_S13 17 | ||
113 | #define MD5_S14 22 | ||
114 | #define MD5_S21 5 /* Round 2 */ | ||
115 | #define MD5_S22 9 | ||
116 | #define MD5_S23 14 | ||
117 | #define MD5_S24 20 | ||
118 | #define MD5_S31 4 /* Round 3 */ | ||
119 | #define MD5_S32 11 | ||
120 | #define MD5_S33 16 | ||
121 | #define MD5_S34 23 | ||
122 | #define MD5_S41 6 /* Round 4 */ | ||
123 | #define MD5_S42 10 | ||
124 | #define MD5_S43 15 | ||
125 | #define MD5_S44 21 | ||
126 | |||
127 | /* Basic MD5 step. MD5_Transform buf based on in */ | ||
128 | static void MD5_Transform (UINT4 *buf, UINT4 *in) | ||
129 | { | ||
130 | UINT4 a = buf[0], b = buf[1], c = buf[2], d = buf[3]; | ||
131 | |||
132 | /* Round 1 */ | ||
133 | MD5_FF ( a, b, c, d, in[ 0], MD5_S11, (UINT4) 3614090360u); /* 1 */ | ||
134 | MD5_FF ( d, a, b, c, in[ 1], MD5_S12, (UINT4) 3905402710u); /* 2 */ | ||
135 | MD5_FF ( c, d, a, b, in[ 2], MD5_S13, (UINT4) 606105819u); /* 3 */ | ||
136 | MD5_FF ( b, c, d, a, in[ 3], MD5_S14, (UINT4) 3250441966u); /* 4 */ | ||
137 | MD5_FF ( a, b, c, d, in[ 4], MD5_S11, (UINT4) 4118548399u); /* 5 */ | ||
138 | MD5_FF ( d, a, b, c, in[ 5], MD5_S12, (UINT4) 1200080426u); /* 6 */ | ||
139 | MD5_FF ( c, d, a, b, in[ 6], MD5_S13, (UINT4) 2821735955u); /* 7 */ | ||
140 | MD5_FF ( b, c, d, a, in[ 7], MD5_S14, (UINT4) 4249261313u); /* 8 */ | ||
141 | MD5_FF ( a, b, c, d, in[ 8], MD5_S11, (UINT4) 1770035416u); /* 9 */ | ||
142 | MD5_FF ( d, a, b, c, in[ 9], MD5_S12, (UINT4) 2336552879u); /* 10 */ | ||
143 | MD5_FF ( c, d, a, b, in[10], MD5_S13, (UINT4) 4294925233u); /* 11 */ | ||
144 | MD5_FF ( b, c, d, a, in[11], MD5_S14, (UINT4) 2304563134u); /* 12 */ | ||
145 | MD5_FF ( a, b, c, d, in[12], MD5_S11, (UINT4) 1804603682u); /* 13 */ | ||
146 | MD5_FF ( d, a, b, c, in[13], MD5_S12, (UINT4) 4254626195u); /* 14 */ | ||
147 | MD5_FF ( c, d, a, b, in[14], MD5_S13, (UINT4) 2792965006u); /* 15 */ | ||
148 | MD5_FF ( b, c, d, a, in[15], MD5_S14, (UINT4) 1236535329u); /* 16 */ | ||
149 | |||
150 | /* Round 2 */ | ||
151 | MD5_GG ( a, b, c, d, in[ 1], MD5_S21, (UINT4) 4129170786u); /* 17 */ | ||
152 | MD5_GG ( d, a, b, c, in[ 6], MD5_S22, (UINT4) 3225465664u); /* 18 */ | ||
153 | MD5_GG ( c, d, a, b, in[11], MD5_S23, (UINT4) 643717713u); /* 19 */ | ||
154 | MD5_GG ( b, c, d, a, in[ 0], MD5_S24, (UINT4) 3921069994u); /* 20 */ | ||
155 | MD5_GG ( a, b, c, d, in[ 5], MD5_S21, (UINT4) 3593408605u); /* 21 */ | ||
156 | MD5_GG ( d, a, b, c, in[10], MD5_S22, (UINT4) 38016083u); /* 22 */ | ||
157 | MD5_GG ( c, d, a, b, in[15], MD5_S23, (UINT4) 3634488961u); /* 23 */ | ||
158 | MD5_GG ( b, c, d, a, in[ 4], MD5_S24, (UINT4) 3889429448u); /* 24 */ | ||
159 | MD5_GG ( a, b, c, d, in[ 9], MD5_S21, (UINT4) 568446438u); /* 25 */ | ||
160 | MD5_GG ( d, a, b, c, in[14], MD5_S22, (UINT4) 3275163606u); /* 26 */ | ||
161 | MD5_GG ( c, d, a, b, in[ 3], MD5_S23, (UINT4) 4107603335u); /* 27 */ | ||
162 | MD5_GG ( b, c, d, a, in[ 8], MD5_S24, (UINT4) 1163531501u); /* 28 */ | ||
163 | MD5_GG ( a, b, c, d, in[13], MD5_S21, (UINT4) 2850285829u); /* 29 */ | ||
164 | MD5_GG ( d, a, b, c, in[ 2], MD5_S22, (UINT4) 4243563512u); /* 30 */ | ||
165 | MD5_GG ( c, d, a, b, in[ 7], MD5_S23, (UINT4) 1735328473u); /* 31 */ | ||
166 | MD5_GG ( b, c, d, a, in[12], MD5_S24, (UINT4) 2368359562u); /* 32 */ | ||
167 | |||
168 | /* Round 3 */ | ||
169 | MD5_HH ( a, b, c, d, in[ 5], MD5_S31, (UINT4) 4294588738u); /* 33 */ | ||
170 | MD5_HH ( d, a, b, c, in[ 8], MD5_S32, (UINT4) 2272392833u); /* 34 */ | ||
171 | MD5_HH ( c, d, a, b, in[11], MD5_S33, (UINT4) 1839030562u); /* 35 */ | ||
172 | MD5_HH ( b, c, d, a, in[14], MD5_S34, (UINT4) 4259657740u); /* 36 */ | ||
173 | MD5_HH ( a, b, c, d, in[ 1], MD5_S31, (UINT4) 2763975236u); /* 37 */ | ||
174 | MD5_HH ( d, a, b, c, in[ 4], MD5_S32, (UINT4) 1272893353u); /* 38 */ | ||
175 | MD5_HH ( c, d, a, b, in[ 7], MD5_S33, (UINT4) 4139469664u); /* 39 */ | ||
176 | MD5_HH ( b, c, d, a, in[10], MD5_S34, (UINT4) 3200236656u); /* 40 */ | ||
177 | MD5_HH ( a, b, c, d, in[13], MD5_S31, (UINT4) 681279174u); /* 41 */ | ||
178 | MD5_HH ( d, a, b, c, in[ 0], MD5_S32, (UINT4) 3936430074u); /* 42 */ | ||
179 | MD5_HH ( c, d, a, b, in[ 3], MD5_S33, (UINT4) 3572445317u); /* 43 */ | ||
180 | MD5_HH ( b, c, d, a, in[ 6], MD5_S34, (UINT4) 76029189u); /* 44 */ | ||
181 | MD5_HH ( a, b, c, d, in[ 9], MD5_S31, (UINT4) 3654602809u); /* 45 */ | ||
182 | MD5_HH ( d, a, b, c, in[12], MD5_S32, (UINT4) 3873151461u); /* 46 */ | ||
183 | MD5_HH ( c, d, a, b, in[15], MD5_S33, (UINT4) 530742520u); /* 47 */ | ||
184 | MD5_HH ( b, c, d, a, in[ 2], MD5_S34, (UINT4) 3299628645u); /* 48 */ | ||
185 | |||
186 | /* Round 4 */ | ||
187 | MD5_II ( a, b, c, d, in[ 0], MD5_S41, (UINT4) 4096336452u); /* 49 */ | ||
188 | MD5_II ( d, a, b, c, in[ 7], MD5_S42, (UINT4) 1126891415u); /* 50 */ | ||
189 | MD5_II ( c, d, a, b, in[14], MD5_S43, (UINT4) 2878612391u); /* 51 */ | ||
190 | MD5_II ( b, c, d, a, in[ 5], MD5_S44, (UINT4) 4237533241u); /* 52 */ | ||
191 | MD5_II ( a, b, c, d, in[12], MD5_S41, (UINT4) 1700485571u); /* 53 */ | ||
192 | MD5_II ( d, a, b, c, in[ 3], MD5_S42, (UINT4) 2399980690u); /* 54 */ | ||
193 | MD5_II ( c, d, a, b, in[10], MD5_S43, (UINT4) 4293915773u); /* 55 */ | ||
194 | MD5_II ( b, c, d, a, in[ 1], MD5_S44, (UINT4) 2240044497u); /* 56 */ | ||
195 | MD5_II ( a, b, c, d, in[ 8], MD5_S41, (UINT4) 1873313359u); /* 57 */ | ||
196 | MD5_II ( d, a, b, c, in[15], MD5_S42, (UINT4) 4264355552u); /* 58 */ | ||
197 | MD5_II ( c, d, a, b, in[ 6], MD5_S43, (UINT4) 2734768916u); /* 59 */ | ||
198 | MD5_II ( b, c, d, a, in[13], MD5_S44, (UINT4) 1309151649u); /* 60 */ | ||
199 | MD5_II ( a, b, c, d, in[ 4], MD5_S41, (UINT4) 4149444226u); /* 61 */ | ||
200 | MD5_II ( d, a, b, c, in[11], MD5_S42, (UINT4) 3174756917u); /* 62 */ | ||
201 | MD5_II ( c, d, a, b, in[ 2], MD5_S43, (UINT4) 718787259u); /* 63 */ | ||
202 | MD5_II ( b, c, d, a, in[ 9], MD5_S44, (UINT4) 3951481745u); /* 64 */ | ||
203 | |||
204 | buf[0] += a; | ||
205 | buf[1] += b; | ||
206 | buf[2] += c; | ||
207 | buf[3] += d; | ||
208 | } | ||
209 | |||
210 | // Set pseudoRandomNumber to zero for RFC MD5 implementation | ||
211 | void MD5Init (MD5_CTX *mdContext, unsigned long pseudoRandomNumber) | ||
212 | { | ||
213 | mdContext->i[0] = mdContext->i[1] = (UINT4)0; | ||
214 | |||
215 | /* Load magic initialization constants */ | ||
216 | mdContext->buf[0] = (UINT4)0x67452301 + (pseudoRandomNumber * 11); | ||
217 | mdContext->buf[1] = (UINT4)0xefcdab89 + (pseudoRandomNumber * 71); | ||
218 | mdContext->buf[2] = (UINT4)0x98badcfe + (pseudoRandomNumber * 37); | ||
219 | mdContext->buf[3] = (UINT4)0x10325476 + (pseudoRandomNumber * 97); | ||
220 | } | ||
221 | |||
222 | void MD5Update (MD5_CTX *mdContext, unsigned char *inBuf, unsigned int inLen) | ||
223 | { | ||
224 | UINT4 in[16]; | ||
225 | int mdi = 0; | ||
226 | unsigned int i = 0, ii = 0; | ||
227 | |||
228 | /* Compute number of bytes mod 64 */ | ||
229 | mdi = (int)((mdContext->i[0] >> 3) & 0x3F); | ||
230 | |||
231 | /* Update number of bits */ | ||
232 | if ((mdContext->i[0] + ((UINT4)inLen << 3)) < mdContext->i[0]) mdContext->i[1]++; | ||
233 | mdContext->i[0] += ((UINT4)inLen << 3); | ||
234 | mdContext->i[1] += ((UINT4)inLen >> 29); | ||
235 | |||
236 | while (inLen--) { | ||
237 | /* Add new character to buffer, increment mdi */ | ||
238 | mdContext->in[mdi++] = *inBuf++; | ||
239 | |||
240 | /* Transform if necessary */ | ||
241 | if (mdi == 0x40) { | ||
242 | for (i = 0, ii = 0; i < 16; i++, ii += 4) | ||
243 | in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | | ||
244 | (((UINT4)mdContext->in[ii+2]) << 16) | | ||
245 | (((UINT4)mdContext->in[ii+1]) << 8) | | ||
246 | ((UINT4)mdContext->in[ii]); | ||
247 | |||
248 | MD5_Transform (mdContext->buf, in); | ||
249 | mdi = 0; | ||
250 | } | ||
251 | } | ||
252 | } | ||
253 | |||
254 | void MD5Final (MD5_CTX *mdContext) | ||
255 | { | ||
256 | UINT4 in[16]; | ||
257 | int mdi = 0; | ||
258 | unsigned int i = 0, ii = 0, padLen = 0; | ||
259 | |||
260 | /* Save number of bits */ | ||
261 | in[14] = mdContext->i[0]; | ||
262 | in[15] = mdContext->i[1]; | ||
263 | |||
264 | /* Compute number of bytes mod 64 */ | ||
265 | mdi = (int)((mdContext->i[0] >> 3) & 0x3F); | ||
266 | |||
267 | /* Pad out to 56 mod 64 */ | ||
268 | padLen = (mdi < 56) ? (56 - mdi) : (120 - mdi); | ||
269 | MD5Update (mdContext, MD5_PADDING, padLen); | ||
270 | |||
271 | /* Append length in bits and transform */ | ||
272 | for (i = 0, ii = 0; i < 14; i++, ii += 4) | ||
273 | in[i] = (((UINT4)mdContext->in[ii+3]) << 24) | | ||
274 | (((UINT4)mdContext->in[ii+2]) << 16) | | ||
275 | (((UINT4)mdContext->in[ii+1]) << 8) | | ||
276 | ((UINT4)mdContext->in[ii]); | ||
277 | MD5_Transform (mdContext->buf, in); | ||
278 | |||
279 | /* Store buffer in digest */ | ||
280 | for (i = 0, ii = 0; i < 4; i++, ii += 4) { | ||
281 | mdContext->digest[ii] = (unsigned char)( mdContext->buf[i] & 0xFF); | ||
282 | mdContext->digest[ii+1] = (unsigned char)((mdContext->buf[i] >> 8) & 0xFF); | ||
283 | mdContext->digest[ii+2] = (unsigned char)((mdContext->buf[i] >> 16) & 0xFF); | ||
284 | mdContext->digest[ii+3] = (unsigned char)((mdContext->buf[i] >> 24) & 0xFF); | ||
285 | } | ||
286 | } | ||
287 | |||
288 | void CalculateMD5(unsigned char *buffer, int length, unsigned char *checksum) | ||
289 | { | ||
290 | int i; | ||
291 | MD5_CTX m_md5; | ||
292 | |||
293 | MD5Init(&m_md5, 0); | ||
294 | MD5Update(&m_md5, buffer, length); | ||
295 | MD5Final(&m_md5); | ||
296 | |||
297 | for (i = 0; i < 16; i++) sprintf(checksum+i*2,"%02X", m_md5.digest[i]); | ||
298 | } | ||