Commit becab5a2 becab5a25cdcc2c01ea4a9510e2a10b00fe6cd61 by Sergey Poznyakoff

Replaced RSA code with one from public domain

1 parent 6bd00726
1 /* MD5.H - header file for MD5C.C
2 */
3
4 /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
5 rights reserved.
6
7 License to copy and use this software is granted provided that it
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
9 Algorithm" in all material mentioning or referencing this software
10 or this function.
11
12 License is also granted to make and use derivative works provided
13 that such works are identified as "derived from the RSA Data
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
15 mentioning or referencing the derived work.
16
17 RSA Data Security, Inc. makes no representations concerning either
18 the merchantability of this software or the suitability of this
19 software for any particular purpose. It is provided "as is"
20 without express or implied warranty of any kind.
21 These notices must be retained in any copies of any part of this
22 documentation and/or software.
23 */
24
25 /* GLOBAL.H - RSAREF types and constants
26 */
27
28 /* PROTOTYPES should be set to one if and only if the compiler supports
29 function argument prototyping.
30 The following makes PROTOTYPES default to 0 if it has not already
31 been defined with C compiler flags.
32 */
33 #ifndef PROTOTYPES
34 #define PROTOTYPES 0
35 #endif
36
37 /* POINTER defines a generic pointer type */
38 typedef unsigned char *POINTER;
39
40 /* UINT2 defines a two byte word */
41 typedef unsigned short int UINT2;
42
43 /* UINT4 defines a four byte word */
44 typedef unsigned long int UINT4;
45
46 /* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
47 If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
48 returns an empty list.
49 */
50 #if __STDC__
51 #define PROTO_LIST(list) list
52 #else
53 #define PROTO_LIST(list) ()
54 #endif
55
56 /* MD5 context. */
57 typedef struct {
58 UINT4 state[4]; /* state (ABCD) */
59 UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
60 unsigned char buffer[64]; /* input buffer */
61 } MD5_CTX;
62
63 void MD5Init PROTO_LIST ((MD5_CTX *));
64 void MD5Update PROTO_LIST ((MD5_CTX *, unsigned char *, unsigned int));
65 void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *));
66
1 /*
2 * This code implements the MD5 message-digest algorithm.
3 * The algorithm is due to Ron Rivest. This code was
4 * written by Colin Plumb in 1993, no copyright is claimed.
5 * This code is in the public domain; do with it what you wish.
6 *
7 * Equivalent code is available from RSA Data Security, Inc.
8 * This code has been tested against that, and is equivalent,
9 * except that you don't need to include two pages of legalese
10 * with every copy.
11 *
12 * To compute the message digest of a chunk of bytes, declare an
13 * MD5Context structure, pass it to MD5Init, call MD5Update as
14 * needed on buffers full of bytes, and then call MD5Final, which
15 * will fill a supplied 16-byte array with the digest.
16 */
17 /*
18 * Modified (2001-01-31) to work on Sparcs <gray@Mirddin.farlep.net>
19 */
20 #if defined(HAVE_CONFIG_H)
21 # include <config.h>
22 #endif
23
24 #define MD5_CRYPT
25
26 #ifdef MD5_CRYPT
27 #include <string.h> /* for memcpy() */
28 #include <md5.h>
29
30 void
31 md5_calc(unsigned char *output, unsigned char *input, unsigned int inlen)
32 {
33 MD5_CTX context;
34
35 MD5Init(&context);
36 MD5Update(&context, input, inlen);
37 MD5Final(output, &context);
38 }
39
40
41 static void
42 bytes_encode(unsigned char *output, uint32 *input, unsigned int len)
43 {
44 unsigned int i, j;
45
46 for (i = 0, j = 0; j < len; i++, j += 4) {
47 output[j] = (unsigned char)(input[i] & 0xff);
48 output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
49 output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
50 output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
51 }
52 }
53
54 static void
55 bytes_decode(uint32 *output, unsigned char *input, unsigned int len)
56 {
57 unsigned int i, j;
58
59 for (i = 0, j = 0; j < len; i++, j += 4)
60 output[i] = ((uint32)input[j]) |
61 (((uint32)input[j+1]) << 8) |
62 (((uint32)input[j+2]) << 16) |
63 (((uint32)input[j+3]) << 24);
64 }
65
66 /*
67 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
68 * initialization constants.
69 */
70 void
71 MD5Init(struct MD5Context *ctx)
72 {
73 ctx->buf[0] = 0x67452301;
74 ctx->buf[1] = 0xefcdab89;
75 ctx->buf[2] = 0x98badcfe;
76 ctx->buf[3] = 0x10325476;
77
78 ctx->bits[0] = 0;
79 ctx->bits[1] = 0;
80 }
81
82 /*
83 * Update context to reflect the concatenation of another buffer full
84 * of bytes.
85 */
86 void
87 MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
88 {
89 uint32 t;
90
91 /* Update bitcount */
92
93 t = ctx->bits[0];
94 if ((ctx->bits[0] = t + ((uint32) len << 3)) < t)
95 ctx->bits[1]++; /* Carry from low to high */
96 ctx->bits[1] += len >> 29;
97
98 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
99 /* Handle any leading odd-sized chunks */
100
101 if (t) {
102 unsigned char *p = (unsigned char *) ctx->in + t;
103 t = 64 - t;
104 if (len < t) {
105 memcpy(p, buf, len);
106 return;
107 }
108 memcpy(p, buf, t);
109 MD5Transform(ctx->buf, (uint32 *) ctx->in);
110 buf += t;
111 len -= t;
112 }
113 /* Process data in 64-byte chunks */
114
115 while (len >= 64) {
116 memcpy(ctx->in, buf, 64);
117 MD5Transform(ctx->buf, (uint32 const *) buf);
118 buf += 64;
119 len -= 64;
120 }
121
122 /* Handle any remaining bytes of data. */
123
124 memcpy(ctx->in, buf, len);
125 }
126
127 /*
128 * Final wrapup - pad to 64-byte boundary with the bit pattern
129 * 1 0* (64-bit count of bits processed, MSB-first)
130 */
131 void
132 MD5Final(unsigned char digest[16], struct MD5Context *ctx)
133 {
134 unsigned count;
135 unsigned char *p;
136
137 /* Compute number of bytes mod 64 */
138 count = (ctx->bits[0] >> 3) & 0x3F;
139
140 /* Set the first char of padding to 0x80. This is safe since there is
141 always at least one byte free */
142 p = ctx->in + count;
143 *p++ = 0x80;
144
145 /* Bytes of padding needed to make 64 bytes */
146 count = 64 - 1 - count;
147
148 /* Pad out to 56 mod 64 */
149 if (count < 8) {
150 /* Two lots of padding: Pad the first block to 64 bytes */
151 memset(p, 0, count);
152 MD5Transform(ctx->buf, (uint32 *) ctx->in);
153
154 /* Now fill the next block with 56 bytes */
155 memset(ctx->in, 0, 56);
156 } else {
157 /* Pad block to 56 bytes */
158 memset(p, 0, count - 8);
159 }
160
161 /* Append length in bits and transform */
162 bytes_encode((unsigned char*)((uint32 *) ctx->in + 14), ctx->bits, 8);
163 MD5Transform(ctx->buf, (uint32 *) ctx->in);
164 bytes_encode(digest,ctx->buf,16);
165 memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */
166 }
167
168 #ifndef ASM_MD5
169
170 /* The four core functions - F1 is optimized somewhat */
171
172 #define F1(x, y, z) (x & y | ~x & z)
173 /*#define F1(x, y, z) (z ^ (x & (y ^ z))) */
174 #define F2(x, y, z) F1(z, x, y)
175 #define F3(x, y, z) (x ^ y ^ z)
176 #define F4(x, y, z) (y ^ (x | ~z))
177
178 /* This is the central step in the MD5 algorithm. */
179 #define MD5STEP(f, w, x, y, z, data, s) \
180 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x );
181
182 #if 0
183 dump(char *label,unsigned char *p, int len)
184 {
185 int i;
186 return;
187 printf("dump: %s\n", label);
188 for (i=0; i<len; i++)
189 printf("%x\n", p[i]);
190 printf("--\n");
191
192 }
193 #endif
194
195 /*
196 * The core of the MD5 algorithm, this alters an existing MD5 hash to
197 * reflect the addition of 16 longwords of new data. MD5Update blocks
198 * the data and converts bytes into longwords for this routine.
199 */
200 void
201 MD5Transform(uint32 buf[4], uint32 const cin[16])
202 {
203 register uint32 a, b, c, d;
204 uint32 in[16];
205
206 bytes_decode(in, (unsigned char *) cin, 64);
207
208 a = buf[0];
209 b = buf[1];
210 c = buf[2];
211 d = buf[3];
212
213 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
214 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
215 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
216 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
217 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
218 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
219 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
220 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
221 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
222 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
223 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
224 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
225 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
226 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
227 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
228 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
229
230 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
231 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
232 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
233 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
234 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
235 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
236 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
237 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
238 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
239 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
240 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
241 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
242 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
243 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
244 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
245 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
246
247 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
248 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
249 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
250 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
251 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
252 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
253 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
254 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
255 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
256 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
257 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
258 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
259 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
260 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
261 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
262 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
263
264 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
265 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
266 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
267 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
268 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
269 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
270 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
271 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
272 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
273 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
274 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
275 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
276 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
277 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
278 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
279 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
280
281 buf[0] += a;
282 buf[1] += b;
283 buf[2] += c;
284 buf[3] += d;
285 }
286
287 #endif
288 #endif /* MD5_CRYPT */
1 #ifndef MD5_H
2 #define MD5_H
3
4 #ifdef __alpha
5 typedef unsigned int uint32;
6 #else
7 typedef unsigned long uint32;
8 #endif
9
10 struct MD5Context {
11 uint32 buf[4];
12 uint32 bits[2];
13 unsigned char in[64];
14 };
15
16 void MD5Init(struct MD5Context *context);
17 void MD5Update(struct MD5Context *context, unsigned char const *buf,
18 unsigned len);
19 void MD5Final(unsigned char digest[16], struct MD5Context *context);
20 void MD5Transform(uint32 buf[4], uint32 const in[16]);
21
22 typedef struct MD5Context MD5_CTX;
23
24 #endif /* !MD5_H */