add MD5 implementation
[platform/upstream/flac.git] / src / libFLAC / md5.c
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  * Changed so as no longer to depend on Colin Plumb's `usual.h' header
18  * definitions; now uses stuff from dpkg's config.h.
19  *  - Ian Jackson <ijackson@nyx.cs.du.edu>.
20  * Still in the public domain.
21  *
22  * Josh Coalson: made some changes to integrate with libFLAC.
23  * Still in the public domain.
24  */
25
26 #include <assert.h>             /* for assert() */
27 #include <stdlib.h>             /* for malloc() */
28 #include <string.h>             /* for memcpy() */
29
30 #include "private/md5.h"
31
32 static bool is_big_endian_host_;
33
34 void
35 byteSwap(uint32 *buf, unsigned words)
36 {
37         md5byte *p = (md5byte *)buf;
38
39         if(!is_big_endian_host_)
40                 return;
41         do {
42                 *buf++ = (uint32)((unsigned)p[3] << 8 | p[2]) << 16 | ((unsigned)p[1] << 8 | p[0]);
43                 p += 4;
44         } while (--words);
45 }
46
47 /*
48  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
49  * initialization constants.
50  */
51 void
52 MD5Init(struct MD5Context *ctx)
53 {
54     uint32 test = 1;
55
56     is_big_endian_host_ = (*((byte*)(&test)))? false : true;
57
58         ctx->buf[0] = 0x67452301;
59         ctx->buf[1] = 0xefcdab89;
60         ctx->buf[2] = 0x98badcfe;
61         ctx->buf[3] = 0x10325476;
62
63         ctx->bytes[0] = 0;
64         ctx->bytes[1] = 0;
65
66         ctx->internal_buf = 0;
67         ctx->capacity = 0;
68 }
69
70 /*
71  * Update context to reflect the concatenation of another buffer full
72  * of bytes.
73  */
74 void
75 MD5Update(struct MD5Context *ctx, md5byte const *buf, unsigned len)
76 {
77         uint32 t;
78
79         /* Update byte count */
80
81         t = ctx->bytes[0];
82         if ((ctx->bytes[0] = t + len) < t)
83                 ctx->bytes[1]++;        /* Carry from low to high */
84
85         t = 64 - (t & 0x3f);    /* Space available in ctx->in (at least 1) */
86         if (t > len) {
87                 memcpy((md5byte *)ctx->in + 64 - t, buf, len);
88                 return;
89         }
90         /* First chunk is an odd size */
91         memcpy((md5byte *)ctx->in + 64 - t, buf, t);
92         byteSwap(ctx->in, 16);
93         MD5Transform(ctx->buf, ctx->in);
94         buf += t;
95         len -= t;
96
97         /* Process data in 64-byte chunks */
98         while (len >= 64) {
99                 memcpy(ctx->in, buf, 64);
100                 byteSwap(ctx->in, 16);
101                 MD5Transform(ctx->buf, ctx->in);
102                 buf += 64;
103                 len -= 64;
104         }
105
106         /* Handle any remaining bytes of data. */
107         memcpy(ctx->in, buf, len);
108 }
109
110 /*
111  * Convert the incoming audio signal to a byte stream and MD5Update it.
112  */
113 bool
114 FLAC__MD5Accumulate(struct MD5Context *ctx, const int32 *signal[], unsigned channels, unsigned samples, unsigned bytes_per_sample)
115 {
116         unsigned channel, sample, a_byte;
117         int32 a_word;
118         byte *buf_;
119         const unsigned bytes_needed = channels * samples * bytes_per_sample;
120
121         if(ctx->capacity < bytes_needed) {
122                 byte *tmp = realloc(ctx->internal_buf, bytes_needed);
123                 if(0 == tmp) {
124                         free(ctx->internal_buf);
125                         if(0 == (ctx->internal_buf = malloc(bytes_needed)))
126                                 return false;
127                 }
128                 ctx->internal_buf = tmp;
129                 ctx->capacity = bytes_needed;
130         }
131
132         buf_ = ctx->internal_buf;
133
134         for(sample = 0; sample < samples; sample++) {
135                 for(channel = 0; channel < channels; channel++) {
136                         a_word = signal[channel][sample];
137                         for(a_byte = 0; a_byte < bytes_per_sample; a_byte++) {
138                                 *buf_++ = (byte)(a_word & 0xff);
139                                 a_word >>= 8;
140                         }
141                 }
142         }
143
144         MD5Update(ctx, ctx->internal_buf, bytes_needed);
145
146         return true;
147 }
148
149 /*
150  * Final wrapup - pad to 64-byte boundary with the bit pattern 
151  * 1 0* (64-bit count of bits processed, MSB-first)
152  */
153 void
154 MD5Final(md5byte digest[16], struct MD5Context *ctx)
155 {
156         int count = ctx->bytes[0] & 0x3f;       /* Number of bytes in ctx->in */
157         md5byte *p = (md5byte *)ctx->in + count;
158
159         /* Set the first char of padding to 0x80.  There is always room. */
160         *p++ = 0x80;
161
162         /* Bytes of padding needed to make 56 bytes (-8..55) */
163         count = 56 - 1 - count;
164
165         if (count < 0) {        /* Padding forces an extra block */
166                 memset(p, 0, count + 8);
167                 byteSwap(ctx->in, 16);
168                 MD5Transform(ctx->buf, ctx->in);
169                 p = (md5byte *)ctx->in;
170                 count = 56;
171         }
172         memset(p, 0, count);
173         byteSwap(ctx->in, 14);
174
175         /* Append length in bits and transform */
176         ctx->in[14] = ctx->bytes[0] << 3;
177         ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
178         MD5Transform(ctx->buf, ctx->in);
179
180         byteSwap(ctx->buf, 4);
181         memcpy(digest, ctx->buf, 16);
182         memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
183         if(0 != ctx->internal_buf) {
184                 free(ctx->internal_buf);
185                 ctx->internal_buf = 0;
186                 ctx->capacity = 0;
187         }
188 }
189
190 #ifndef ASM_MD5
191
192 /* The four core functions - F1 is optimized somewhat */
193
194 /* #define F1(x, y, z) (x & y | ~x & z) */
195 #define F1(x, y, z) (z ^ (x & (y ^ z)))
196 #define F2(x, y, z) F1(z, x, y)
197 #define F3(x, y, z) (x ^ y ^ z)
198 #define F4(x, y, z) (y ^ (x | ~z))
199
200 /* This is the central step in the MD5 algorithm. */
201 #define MD5STEP(f,w,x,y,z,in,s) \
202          (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
203
204 /*
205  * The core of the MD5 algorithm, this alters an existing MD5 hash to
206  * reflect the addition of 16 longwords of new data.  MD5Update blocks
207  * the data and converts bytes into longwords for this routine.
208  */
209 void
210 MD5Transform(uint32 buf[4], uint32 const in[16])
211 {
212         register uint32 a, b, c, d;
213
214         a = buf[0];
215         b = buf[1];
216         c = buf[2];
217         d = buf[3];
218
219         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
220         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
221         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
222         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
223         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
224         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
225         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
226         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
227         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
228         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
229         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
230         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
231         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
232         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
233         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
234         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
235
236         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
237         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
238         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
239         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
240         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
241         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
242         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
243         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
244         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
245         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
246         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
247         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
248         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
249         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
250         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
251         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
252
253         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
254         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
255         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
256         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
257         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
258         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
259         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
260         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
261         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
262         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
263         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
264         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
265         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
266         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
267         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
268         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
269
270         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
271         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
272         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
273         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
274         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
275         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
276         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
277         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
278         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
279         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
280         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
281         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
282         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
283         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
284         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
285         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
286
287         buf[0] += a;
288         buf[1] += b;
289         buf[2] += c;
290         buf[3] += d;
291 }
292
293 #endif