1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Implementation of DAAP (iTunes Music Sharing) hashing, parsing, connection
5 * Copyright (C) 2004,2005 Charles Schmidt <cschmidt2@emich.edu>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
35 #include "rb-daap-hash.h"
37 /* hashing - based on/copied from libopendaap
38 * Copyright (c) 2004 David Hammerton
49 * This code implements the MD5 message-digest algorithm.
50 * The algorithm is due to Ron Rivest. This code was
51 * written by Colin Plumb in 1993, no copyright is claimed.
52 * This code is in the public domain; do with it what you wish.
54 * Equivalent code is available from RSA Data Security, Inc.
55 * This code has been tested against that, and is equivalent,
56 * except that you don't need to include two pages of legalese
59 * To compute the message digest of a chunk of bytes, declare an MD5Context
60 * structure, pass it to OpenDaap_MD5Init, call OpenDaap_MD5Update as needed
61 * on buffers full of bytes, and then call OpenDaap_MD5Final, which will fill
62 * a supplied 16-byte array with the digest.
65 MD5Transform (guint32 buf
[4],
68 /* for some reason we still have to reverse bytes on bigendian machines
69 * I don't really know why... but otherwise it fails..
70 * Any MD5 gurus out there know why???
72 #if 0 //ndef WORDS_BIGENDIAN /* was: HIGHFIRST */
73 #define byteReverse(buf, len) /* Nothing */
76 byteReverse (unsigned char *buf
,
81 * Note: this code is harmless on little-endian machines.
84 byteReverse (unsigned char *buf
,
89 t
= (guint32
) ((unsigned) buf
[3] << 8 | buf
[2]) << 16 |
90 ((unsigned) buf
[1] << 8 | buf
[0]);
95 #endif /* ! ASM_MD5 */
99 OpenDaap_MD5Init (MD5_CTX
*ctx
,
102 memset (ctx
, 0, sizeof (MD5_CTX
));
103 ctx
->buf
[0] = 0x67452301;
104 ctx
->buf
[1] = 0xefcdab89;
105 ctx
->buf
[2] = 0x98badcfe;
106 ctx
->buf
[3] = 0x10325476;
111 ctx
->version
= version
;
115 OpenDaap_MD5Update (MD5_CTX
*ctx
,
116 unsigned char const *buf
,
121 /* Update bitcount */
124 if ((ctx
->bits
[0] = t
+ ((guint32
) len
<< 3)) < t
)
125 ctx
->bits
[1]++; /* Carry from low to high */
126 ctx
->bits
[1] += len
>> 29;
128 t
= (t
>> 3) & 0x3f; /* Bytes already in shsInfo->data */
130 /* Handle any leading odd-sized chunks */
133 unsigned char *p
= (unsigned char *) ctx
->in
+ t
;
137 memcpy (p
, buf
, len
);
141 byteReverse (ctx
->in
, 16);
142 MD5Transform (ctx
->buf
, (guint32
*) ctx
->in
, ctx
->version
);
146 /* Process data in 64-byte chunks */
149 memcpy (ctx
->in
, buf
, 64);
150 byteReverse (ctx
->in
, 16);
151 MD5Transform (ctx
->buf
, (guint32
*) ctx
->in
, ctx
->version
);
156 /* Handle any remaining bytes of data. */
158 memcpy (ctx
->in
, buf
, len
);
163 OpenDaap_MD5Final (MD5_CTX
*ctx
,
164 unsigned char digest
[16])
169 /* Compute number of bytes mod 64 */
170 count
= (ctx
->bits
[0] >> 3) & 0x3F;
172 /* Set the first char of padding to 0x80. This is safe since there is
173 always at least one byte free */
177 /* Bytes of padding needed to make 64 bytes */
178 count
= 64 - 1 - count
;
180 /* Pad out to 56 mod 64 */
182 /* Two lots of padding: Pad the first block to 64 bytes */
183 memset (p
, 0, count
);
184 byteReverse (ctx
->in
, 16);
185 MD5Transform (ctx
->buf
, (guint32
*) ctx
->in
, ctx
->version
);
187 /* Now fill the next block with 56 bytes */
188 memset (ctx
->in
, 0, 56);
190 /* Pad block to 56 bytes */
191 memset (p
, 0, count
- 8);
193 byteReverse (ctx
->in
, 14);
195 /* Append length in bits and transform */
196 ((guint32
*) ctx
->in
)[14] = ctx
->bits
[0];
197 ((guint32
*) ctx
->in
)[15] = ctx
->bits
[1];
199 MD5Transform (ctx
->buf
, (guint32
*) ctx
->in
, ctx
->version
);
200 byteReverse ((unsigned char *) ctx
->buf
, 4);
201 memcpy (digest
, ctx
->buf
, 16);
202 memset (ctx
, 0, sizeof(ctx
)); /* In case it's sensitive */
209 /* The four core functions - F1 is optimized somewhat */
211 /* #define F1(x, y, z) (x & y | ~x & z) */
212 #define F1(x, y, z) (z ^ (x & (y ^ z)))
213 #define F2(x, y, z) F1(z, x, y)
214 #define F3(x, y, z) (x ^ y ^ z)
215 #define F4(x, y, z) (y ^ (x | ~z))
217 /* This is the central step in the MD5 algorithm. */
218 #define MD5STEP(f, w, x, y, z, data, s) \
219 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
222 * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
223 * the addition of 16 longwords of new data. OpenDaap_MD5Update blocks the
224 * data and converts bytes into longwords for this routine.
227 MD5Transform (guint32 buf
[4],
228 guint32
const in
[16],
238 MD5STEP(F1
, a
, b
, c
, d
, in
[0] + 0xd76aa478, 7);
239 MD5STEP(F1
, d
, a
, b
, c
, in
[1] + 0xe8c7b756, 12);
240 MD5STEP(F1
, c
, d
, a
, b
, in
[2] + 0x242070db, 17);
241 MD5STEP(F1
, b
, c
, d
, a
, in
[3] + 0xc1bdceee, 22);
242 MD5STEP(F1
, a
, b
, c
, d
, in
[4] + 0xf57c0faf, 7);
243 MD5STEP(F1
, d
, a
, b
, c
, in
[5] + 0x4787c62a, 12);
244 MD5STEP(F1
, c
, d
, a
, b
, in
[6] + 0xa8304613, 17);
245 MD5STEP(F1
, b
, c
, d
, a
, in
[7] + 0xfd469501, 22);
246 MD5STEP(F1
, a
, b
, c
, d
, in
[8] + 0x698098d8, 7);
247 MD5STEP(F1
, d
, a
, b
, c
, in
[9] + 0x8b44f7af, 12);
248 MD5STEP(F1
, c
, d
, a
, b
, in
[10] + 0xffff5bb1, 17);
249 MD5STEP(F1
, b
, c
, d
, a
, in
[11] + 0x895cd7be, 22);
250 MD5STEP(F1
, a
, b
, c
, d
, in
[12] + 0x6b901122, 7);
251 MD5STEP(F1
, d
, a
, b
, c
, in
[13] + 0xfd987193, 12);
252 MD5STEP(F1
, c
, d
, a
, b
, in
[14] + 0xa679438e, 17);
253 MD5STEP(F1
, b
, c
, d
, a
, in
[15] + 0x49b40821, 22);
255 MD5STEP(F2
, a
, b
, c
, d
, in
[1] + 0xf61e2562, 5);
256 MD5STEP(F2
, d
, a
, b
, c
, in
[6] + 0xc040b340, 9);
257 MD5STEP(F2
, c
, d
, a
, b
, in
[11] + 0x265e5a51, 14);
258 MD5STEP(F2
, b
, c
, d
, a
, in
[0] + 0xe9b6c7aa, 20);
259 MD5STEP(F2
, a
, b
, c
, d
, in
[5] + 0xd62f105d, 5);
260 MD5STEP(F2
, d
, a
, b
, c
, in
[10] + 0x02441453, 9);
261 MD5STEP(F2
, c
, d
, a
, b
, in
[15] + 0xd8a1e681, 14);
262 MD5STEP(F2
, b
, c
, d
, a
, in
[4] + 0xe7d3fbc8, 20);
263 MD5STEP(F2
, a
, b
, c
, d
, in
[9] + 0x21e1cde6, 5);
264 MD5STEP(F2
, d
, a
, b
, c
, in
[14] + 0xc33707d6, 9);
265 MD5STEP(F2
, c
, d
, a
, b
, in
[3] + 0xf4d50d87, 14);
269 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x445a14ed, 20);
273 MD5STEP(F2
, b
, c
, d
, a
, in
[8] + 0x455a14ed, 20);
275 MD5STEP(F2
, a
, b
, c
, d
, in
[13] + 0xa9e3e905, 5);
276 MD5STEP(F2
, d
, a
, b
, c
, in
[2] + 0xfcefa3f8, 9);
277 MD5STEP(F2
, c
, d
, a
, b
, in
[7] + 0x676f02d9, 14);
278 MD5STEP(F2
, b
, c
, d
, a
, in
[12] + 0x8d2a4c8a, 20);
280 MD5STEP(F3
, a
, b
, c
, d
, in
[5] + 0xfffa3942, 4);
281 MD5STEP(F3
, d
, a
, b
, c
, in
[8] + 0x8771f681, 11);
282 MD5STEP(F3
, c
, d
, a
, b
, in
[11] + 0x6d9d6122, 16);
283 MD5STEP(F3
, b
, c
, d
, a
, in
[14] + 0xfde5380c, 23);
284 MD5STEP(F3
, a
, b
, c
, d
, in
[1] + 0xa4beea44, 4);
285 MD5STEP(F3
, d
, a
, b
, c
, in
[4] + 0x4bdecfa9, 11);
286 MD5STEP(F3
, c
, d
, a
, b
, in
[7] + 0xf6bb4b60, 16);
287 MD5STEP(F3
, b
, c
, d
, a
, in
[10] + 0xbebfbc70, 23);
288 MD5STEP(F3
, a
, b
, c
, d
, in
[13] + 0x289b7ec6, 4);
289 MD5STEP(F3
, d
, a
, b
, c
, in
[0] + 0xeaa127fa, 11);
290 MD5STEP(F3
, c
, d
, a
, b
, in
[3] + 0xd4ef3085, 16);
291 MD5STEP(F3
, b
, c
, d
, a
, in
[6] + 0x04881d05, 23);
292 MD5STEP(F3
, a
, b
, c
, d
, in
[9] + 0xd9d4d039, 4);
293 MD5STEP(F3
, d
, a
, b
, c
, in
[12] + 0xe6db99e5, 11);
294 MD5STEP(F3
, c
, d
, a
, b
, in
[15] + 0x1fa27cf8, 16);
295 MD5STEP(F3
, b
, c
, d
, a
, in
[2] + 0xc4ac5665, 23);
297 MD5STEP(F4
, a
, b
, c
, d
, in
[0] + 0xf4292244, 6);
298 MD5STEP(F4
, d
, a
, b
, c
, in
[7] + 0x432aff97, 10);
299 MD5STEP(F4
, c
, d
, a
, b
, in
[14] + 0xab9423a7, 15);
300 MD5STEP(F4
, b
, c
, d
, a
, in
[5] + 0xfc93a039, 21);
301 MD5STEP(F4
, a
, b
, c
, d
, in
[12] + 0x655b59c3, 6);
302 MD5STEP(F4
, d
, a
, b
, c
, in
[3] + 0x8f0ccc92, 10);
303 MD5STEP(F4
, c
, d
, a
, b
, in
[10] + 0xffeff47d, 15);
304 MD5STEP(F4
, b
, c
, d
, a
, in
[1] + 0x85845dd1, 21);
305 MD5STEP(F4
, a
, b
, c
, d
, in
[8] + 0x6fa87e4f, 6);
306 MD5STEP(F4
, d
, a
, b
, c
, in
[15] + 0xfe2ce6e0, 10);
307 MD5STEP(F4
, c
, d
, a
, b
, in
[6] + 0xa3014314, 15);
308 MD5STEP(F4
, b
, c
, d
, a
, in
[13] + 0x4e0811a1, 21);
309 MD5STEP(F4
, a
, b
, c
, d
, in
[4] + 0xf7537e82, 6);
310 MD5STEP(F4
, d
, a
, b
, c
, in
[11] + 0xbd3af235, 10);
311 MD5STEP(F4
, c
, d
, a
, b
, in
[2] + 0x2ad7d2bb, 15);
312 MD5STEP(F4
, b
, c
, d
, a
, in
[9] + 0xeb86d391, 21);
322 static int staticHashDone
= 0;
323 static unsigned char staticHash_42
[256*65] = {0};
324 static unsigned char staticHash_45
[256*65] = {0};
326 static const char hexchars
[] = "0123456789ABCDEF";
327 static char ac
[] = "Dpqzsjhiu!3114!Bqqmf!Dpnqvufs-!Jod/"; /* +1 */
328 static gboolean ac_unfudged
= FALSE
;
331 DigestToString (const unsigned char *digest
,
335 for (i
= 0; i
< 16; i
++)
337 unsigned char tmp
= digest
[i
];
338 string
[i
*2+1] = hexchars
[tmp
& 0x0f];
339 string
[i
*2] = hexchars
[(tmp
>> 4) & 0x0f];
347 unsigned char *p
= staticHash_42
;
349 unsigned char buf
[16];
351 for (i
= 0; i
< 256; i
++)
353 OpenDaap_MD5Init (&ctx
, 0);
355 #define MD5_STRUPDATE(str) OpenDaap_MD5Update(&ctx, (unsigned char const *)str, strlen(str))
358 MD5_STRUPDATE("Accept-Language");
360 MD5_STRUPDATE("user-agent");
363 MD5_STRUPDATE("max-age");
365 MD5_STRUPDATE("Authorization");
368 MD5_STRUPDATE("Client-DAAP-Version");
370 MD5_STRUPDATE("Accept-Encoding");
373 MD5_STRUPDATE("daap.protocolversion");
375 MD5_STRUPDATE("daap.songartist");
378 MD5_STRUPDATE("daap.songcomposer");
380 MD5_STRUPDATE("daap.songdatemodified");
383 MD5_STRUPDATE("daap.songdiscnumber");
385 MD5_STRUPDATE("daap.songdisabled");
388 MD5_STRUPDATE("playlist-item-spec");
390 MD5_STRUPDATE("revision-number");
393 MD5_STRUPDATE("session-id");
395 MD5_STRUPDATE("content-codes");
398 OpenDaap_MD5Final (&ctx
, buf
);
399 DigestToString (buf
, (char *)p
);
404 static void GenerateStatic_45()
407 unsigned char *p
= staticHash_45
;
409 unsigned char buf
[16];
411 for (i
= 0; i
< 256; i
++)
413 OpenDaap_MD5Init (&ctx
, 1);
415 #define MD5_STRUPDATE(str) OpenDaap_MD5Update(&ctx, (unsigned char const *)str, strlen(str))
418 MD5_STRUPDATE("eqwsdxcqwesdc");
420 MD5_STRUPDATE("op[;lm,piojkmn");
423 MD5_STRUPDATE("876trfvb 34rtgbvc");
425 MD5_STRUPDATE("=-0ol.,m3ewrdfv");
428 MD5_STRUPDATE("87654323e4rgbv ");
430 MD5_STRUPDATE("1535753690868867974342659792");
433 MD5_STRUPDATE("Song Name");
435 MD5_STRUPDATE("DAAP-CLIENT-ID:");
438 MD5_STRUPDATE("111222333444555");
440 MD5_STRUPDATE("4089961010");
443 MD5_STRUPDATE("playlist-item-spec");
445 MD5_STRUPDATE("revision-number");
448 MD5_STRUPDATE("session-id");
450 MD5_STRUPDATE("content-codes");
453 MD5_STRUPDATE("IUYHGFDCXWEDFGHN");
455 MD5_STRUPDATE("iuytgfdxwerfghjm");
459 OpenDaap_MD5Final (&ctx
, buf
);
460 DigestToString (buf
, (char *)p
);
466 rb_daap_hash_generate (short version_major
,
472 unsigned char buf
[16];
476 unsigned char *hashTable
= (version_major
== 3) ?
477 staticHash_45
: staticHash_42
;
481 GenerateStatic_42 ();
482 GenerateStatic_45 ();
486 OpenDaap_MD5Init (&ctx
, (version_major
== 3) ? 1 : 0);
488 OpenDaap_MD5Update (&ctx
, url
, strlen ((const gchar
*)url
));
489 if (ac_unfudged
== FALSE
) {
490 for (i
= 0; i
< strlen (ac
); i
++) {
495 OpenDaap_MD5Update (&ctx
, (const guchar
*)ac
, strlen (ac
));
497 OpenDaap_MD5Update (&ctx
, &hashTable
[hash_select
* 65], 32);
499 if (request_id
&& version_major
== 3)
502 sprintf (scribble
, "%u", request_id
);
503 OpenDaap_MD5Update (&ctx
, (const guchar
*)scribble
, strlen (scribble
));
506 OpenDaap_MD5Final (&ctx
, buf
);
507 DigestToString (buf
, (char *)out
);