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