annotate libfaim/md5.c @ 2062:caba1f59801e

[gaim-migrate @ 2072] What's up. Nothing to see here, move right along. :) committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Tue, 26 Jun 2001 21:53:09 +0000
parents 3fe5799b7823
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
741
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
1 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
2 Copyright (C) 1999 Aladdin Enterprises. All rights reserved.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
3
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
4 This software is provided 'as-is', without any express or implied
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
5 warranty. In no event will the authors be held liable for any damages
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
6 arising from the use of this software.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
7
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
8 Permission is granted to anyone to use this software for any purpose,
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
9 including commercial applications, and to alter it and redistribute it
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
10 freely, subject to the following restrictions:
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
11
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
12 1. The origin of this software must not be misrepresented; you must not
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
13 claim that you wrote the original software. If you use this software
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
14 in a product, an acknowledgment in the product documentation would be
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
15 appreciated but is not required.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
16 2. Altered source versions must be plainly marked as such, and must not be
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
17 misrepresented as being the original software.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
18 3. This notice may not be removed or altered from any source distribution.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
19
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
20 L. Peter Deutsch
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
21 ghost@aladdin.com
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
22
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
23 */
1649
3fe5799b7823 [gaim-migrate @ 1659]
Adam Fritzler <mid@auk.cx>
parents: 889
diff changeset
24 /*$Id: md5.c 1659 2001-03-27 00:51:09Z mid $ */
741
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
25 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
26 Independent implementation of MD5 (RFC 1321).
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
27
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
28 This code implements the MD5 Algorithm defined in RFC 1321.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
29 It is derived directly from the text of the RFC and not from the
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
30 reference implementation.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
31
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
32 The original and principal author of md5.c is L. Peter Deutsch
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
33 <ghost@aladdin.com>. Other authors are noted in the change history
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
34 that follows (in reverse chronological order):
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
35
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
36 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
37 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
38 1999-05-03 lpd Original version.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
39 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
40
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
41 #include "md5.h"
889
e1da6a6ec42b [gaim-migrate @ 899]
Eric Warmenhoven <eric@warmenhoven.org>
parents: 741
diff changeset
42 #include <string.h>
741
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
43
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
44 #ifdef TEST
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
45 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
46 * Compile with -DTEST to create a self-contained executable test program.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
47 * The test program should print out the same values as given in section
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
48 * A.5 of RFC 1321, reproduced below.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
49 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
50 #include <string.h>
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
51 main()
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
52 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
53 static const char *const test[7] = {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
54 "", /*d41d8cd98f00b204e9800998ecf8427e*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
55 "945399884.61923487334tuvga", /*0cc175b9c0f1b6a831c399e269772661*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
56 "abc", /*900150983cd24fb0d6963f7d28e17f72*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
57 "message digest", /*f96b697d7cb7938d525a2f31aaf161d0*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
58 "abcdefghijklmnopqrstuvwxyz", /*c3fcd3d76192e4007dfb496cca67e13b*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
60 /*d174ab98d277d9f5a5611c2c9f419d9f*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
61 "12345678901234567890123456789012345678901234567890123456789012345678901234567890" /*57edf4a22be3c955ac49da2e2107b67a*/
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
62 };
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
63 int i;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
64
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
65 for (i = 0; i < 7; ++i) {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
66 md5_state_t state;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
67 md5_byte_t digest[16];
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
68 int di;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
69
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
70 md5_init(&state);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
71 md5_append(&state, (const md5_byte_t *)test[i], strlen(test[i]));
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
72 md5_finish(&state, digest);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
73 printf("MD5 (\"%s\") = ", test[i]);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
74 for (di = 0; di < 16; ++di)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
75 printf("%02x", digest[di]);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
76 printf("\n");
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
77 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
78 return 0;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
79 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
80 #endif /* TEST */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
81
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
82
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
83 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
84 * For reference, here is the program that computed the T values.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
85 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
86 #if 0
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
87 #include <math.h>
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
88 main()
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
89 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
90 int i;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
91 for (i = 1; i <= 64; ++i) {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
92 unsigned long v = (unsigned long)(4294967296.0 * fabs(sin((double)i)));
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
93 printf("#define T%d 0x%08lx\n", i, v);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
94 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
95 return 0;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
96 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
97 #endif
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
98 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
99 * End of T computation program.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
100 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
101 #define T1 0xd76aa478
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
102 #define T2 0xe8c7b756
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
103 #define T3 0x242070db
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
104 #define T4 0xc1bdceee
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
105 #define T5 0xf57c0faf
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
106 #define T6 0x4787c62a
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
107 #define T7 0xa8304613
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
108 #define T8 0xfd469501
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
109 #define T9 0x698098d8
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
110 #define T10 0x8b44f7af
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
111 #define T11 0xffff5bb1
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
112 #define T12 0x895cd7be
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
113 #define T13 0x6b901122
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
114 #define T14 0xfd987193
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
115 #define T15 0xa679438e
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
116 #define T16 0x49b40821
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
117 #define T17 0xf61e2562
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
118 #define T18 0xc040b340
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
119 #define T19 0x265e5a51
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
120 #define T20 0xe9b6c7aa
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
121 #define T21 0xd62f105d
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
122 #define T22 0x02441453
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
123 #define T23 0xd8a1e681
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
124 #define T24 0xe7d3fbc8
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
125 #define T25 0x21e1cde6
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
126 #define T26 0xc33707d6
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
127 #define T27 0xf4d50d87
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
128 #define T28 0x455a14ed
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
129 #define T29 0xa9e3e905
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
130 #define T30 0xfcefa3f8
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
131 #define T31 0x676f02d9
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
132 #define T32 0x8d2a4c8a
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
133 #define T33 0xfffa3942
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
134 #define T34 0x8771f681
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
135 #define T35 0x6d9d6122
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
136 #define T36 0xfde5380c
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
137 #define T37 0xa4beea44
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
138 #define T38 0x4bdecfa9
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
139 #define T39 0xf6bb4b60
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
140 #define T40 0xbebfbc70
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
141 #define T41 0x289b7ec6
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
142 #define T42 0xeaa127fa
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
143 #define T43 0xd4ef3085
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
144 #define T44 0x04881d05
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
145 #define T45 0xd9d4d039
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
146 #define T46 0xe6db99e5
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
147 #define T47 0x1fa27cf8
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
148 #define T48 0xc4ac5665
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
149 #define T49 0xf4292244
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
150 #define T50 0x432aff97
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
151 #define T51 0xab9423a7
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
152 #define T52 0xfc93a039
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
153 #define T53 0x655b59c3
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
154 #define T54 0x8f0ccc92
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
155 #define T55 0xffeff47d
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
156 #define T56 0x85845dd1
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
157 #define T57 0x6fa87e4f
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
158 #define T58 0xfe2ce6e0
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
159 #define T59 0xa3014314
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
160 #define T60 0x4e0811a1
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
161 #define T61 0xf7537e82
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
162 #define T62 0xbd3af235
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
163 #define T63 0x2ad7d2bb
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
164 #define T64 0xeb86d391
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
165
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
166 static void
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
167 md5_process(md5_state_t *pms, const md5_byte_t *data /*[64]*/)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
168 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
169 md5_word_t
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
170 a = pms->abcd[0], b = pms->abcd[1],
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
171 c = pms->abcd[2], d = pms->abcd[3];
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
172 md5_word_t t;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
173
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
174 #ifndef ARCH_IS_BIG_ENDIAN
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
175 # define ARCH_IS_BIG_ENDIAN 1 /* slower, default implementation */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
176 #endif
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
177 #if ARCH_IS_BIG_ENDIAN
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
178
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
179 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
180 * On big-endian machines, we must arrange the bytes in the right
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
181 * order. (This also works on machines of unknown byte order.)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
182 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
183 md5_word_t X[16];
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
184 const md5_byte_t *xp = data;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
185 int i;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
186
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
187 for (i = 0; i < 16; ++i, xp += 4)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
188 X[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
189
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
190 #else /* !ARCH_IS_BIG_ENDIAN */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
191
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
192 /*
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
193 * On little-endian machines, we can process properly aligned data
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
194 * without copying it.
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
195 */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
196 md5_word_t xbuf[16];
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
197 const md5_word_t *X;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
198
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
199 if (!((data - (const md5_byte_t *)0) & 3)) {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
200 /* data are properly aligned */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
201 X = (const md5_word_t *)data;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
202 } else {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
203 /* not aligned */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
204 memcpy(xbuf, data, 64);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
205 X = xbuf;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
206 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
207 #endif
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
208
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
209 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
210
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
211 /* Round 1. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
212 /* Let [abcd k s i] denote the operation
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
213 a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
214 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
215 #define SET(a, b, c, d, k, s, Ti)\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
216 t = a + F(b,c,d) + X[k] + Ti;\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
217 a = ROTATE_LEFT(t, s) + b
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
218 /* Do the following 16 operations. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
219 SET(a, b, c, d, 0, 7, T1);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
220 SET(d, a, b, c, 1, 12, T2);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
221 SET(c, d, a, b, 2, 17, T3);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
222 SET(b, c, d, a, 3, 22, T4);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
223 SET(a, b, c, d, 4, 7, T5);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
224 SET(d, a, b, c, 5, 12, T6);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
225 SET(c, d, a, b, 6, 17, T7);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
226 SET(b, c, d, a, 7, 22, T8);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
227 SET(a, b, c, d, 8, 7, T9);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
228 SET(d, a, b, c, 9, 12, T10);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
229 SET(c, d, a, b, 10, 17, T11);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
230 SET(b, c, d, a, 11, 22, T12);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
231 SET(a, b, c, d, 12, 7, T13);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
232 SET(d, a, b, c, 13, 12, T14);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
233 SET(c, d, a, b, 14, 17, T15);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
234 SET(b, c, d, a, 15, 22, T16);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
235 #undef SET
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
236
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
237 /* Round 2. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
238 /* Let [abcd k s i] denote the operation
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
239 a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
240 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
241 #define SET(a, b, c, d, k, s, Ti)\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
242 t = a + G(b,c,d) + X[k] + Ti;\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
243 a = ROTATE_LEFT(t, s) + b
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
244 /* Do the following 16 operations. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
245 SET(a, b, c, d, 1, 5, T17);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
246 SET(d, a, b, c, 6, 9, T18);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
247 SET(c, d, a, b, 11, 14, T19);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
248 SET(b, c, d, a, 0, 20, T20);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
249 SET(a, b, c, d, 5, 5, T21);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
250 SET(d, a, b, c, 10, 9, T22);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
251 SET(c, d, a, b, 15, 14, T23);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
252 SET(b, c, d, a, 4, 20, T24);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
253 SET(a, b, c, d, 9, 5, T25);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
254 SET(d, a, b, c, 14, 9, T26);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
255 SET(c, d, a, b, 3, 14, T27);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
256 SET(b, c, d, a, 8, 20, T28);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
257 SET(a, b, c, d, 13, 5, T29);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
258 SET(d, a, b, c, 2, 9, T30);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
259 SET(c, d, a, b, 7, 14, T31);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
260 SET(b, c, d, a, 12, 20, T32);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
261 #undef SET
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
262
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
263 /* Round 3. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
264 /* Let [abcd k s t] denote the operation
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
265 a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
266 #define H(x, y, z) ((x) ^ (y) ^ (z))
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
267 #define SET(a, b, c, d, k, s, Ti)\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
268 t = a + H(b,c,d) + X[k] + Ti;\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
269 a = ROTATE_LEFT(t, s) + b
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
270 /* Do the following 16 operations. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
271 SET(a, b, c, d, 5, 4, T33);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
272 SET(d, a, b, c, 8, 11, T34);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
273 SET(c, d, a, b, 11, 16, T35);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
274 SET(b, c, d, a, 14, 23, T36);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
275 SET(a, b, c, d, 1, 4, T37);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
276 SET(d, a, b, c, 4, 11, T38);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
277 SET(c, d, a, b, 7, 16, T39);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
278 SET(b, c, d, a, 10, 23, T40);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
279 SET(a, b, c, d, 13, 4, T41);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
280 SET(d, a, b, c, 0, 11, T42);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
281 SET(c, d, a, b, 3, 16, T43);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
282 SET(b, c, d, a, 6, 23, T44);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
283 SET(a, b, c, d, 9, 4, T45);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
284 SET(d, a, b, c, 12, 11, T46);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
285 SET(c, d, a, b, 15, 16, T47);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
286 SET(b, c, d, a, 2, 23, T48);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
287 #undef SET
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
288
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
289 /* Round 4. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
290 /* Let [abcd k s t] denote the operation
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
291 a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
292 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
293 #define SET(a, b, c, d, k, s, Ti)\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
294 t = a + I(b,c,d) + X[k] + Ti;\
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
295 a = ROTATE_LEFT(t, s) + b
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
296 /* Do the following 16 operations. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
297 SET(a, b, c, d, 0, 6, T49);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
298 SET(d, a, b, c, 7, 10, T50);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
299 SET(c, d, a, b, 14, 15, T51);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
300 SET(b, c, d, a, 5, 21, T52);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
301 SET(a, b, c, d, 12, 6, T53);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
302 SET(d, a, b, c, 3, 10, T54);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
303 SET(c, d, a, b, 10, 15, T55);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
304 SET(b, c, d, a, 1, 21, T56);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
305 SET(a, b, c, d, 8, 6, T57);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
306 SET(d, a, b, c, 15, 10, T58);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
307 SET(c, d, a, b, 6, 15, T59);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
308 SET(b, c, d, a, 13, 21, T60);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
309 SET(a, b, c, d, 4, 6, T61);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
310 SET(d, a, b, c, 11, 10, T62);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
311 SET(c, d, a, b, 2, 15, T63);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
312 SET(b, c, d, a, 9, 21, T64);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
313 #undef SET
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
314
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
315 /* Then perform the following additions. (That is increment each
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
316 of the four registers by the value it had before this block
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
317 was started.) */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
318 pms->abcd[0] += a;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
319 pms->abcd[1] += b;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
320 pms->abcd[2] += c;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
321 pms->abcd[3] += d;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
322 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
323
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
324 void
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
325 md5_init(md5_state_t *pms)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
326 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
327 pms->count[0] = pms->count[1] = 0;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
328 pms->abcd[0] = 0x67452301;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
329 pms->abcd[1] = 0xefcdab89;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
330 pms->abcd[2] = 0x98badcfe;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
331 pms->abcd[3] = 0x10325476;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
332 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
333
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
334 void
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
335 md5_append(md5_state_t *pms, const md5_byte_t *data, int nbytes)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
336 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
337 const md5_byte_t *p = data;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
338 int left = nbytes;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
339 int offset = (pms->count[0] >> 3) & 63;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
340 md5_word_t nbits = (md5_word_t)(nbytes << 3);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
341
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
342 if (nbytes <= 0)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
343 return;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
344
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
345 /* Update the message length. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
346 pms->count[1] += nbytes >> 29;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
347 pms->count[0] += nbits;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
348 if (pms->count[0] < nbits)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
349 pms->count[1]++;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
350
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
351 /* Process an initial partial block. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
352 if (offset) {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
353 int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
354
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
355 memcpy(pms->buf + offset, p, copy);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
356 if (offset + copy < 64)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
357 return;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
358 p += copy;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
359 left -= copy;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
360 md5_process(pms, pms->buf);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
361 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
362
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
363 /* Process full blocks. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
364 for (; left >= 64; p += 64, left -= 64)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
365 md5_process(pms, p);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
366
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
367 /* Process a final partial block. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
368 if (left)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
369 memcpy(pms->buf, p, left);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
370 }
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
371
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
372 void
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
373 md5_finish(md5_state_t *pms, md5_byte_t digest[16])
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
374 {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
375 static const md5_byte_t pad[64] = {
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
376 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
377 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
378 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
379 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
380 };
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
381 md5_byte_t data[8];
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
382 int i;
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
383
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
384 /* Save the length before padding. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
385 for (i = 0; i < 8; ++i)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
386 data[i] = (md5_byte_t)(pms->count[i >> 2] >> ((i & 3) << 3));
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
387 /* Pad to 56 bytes mod 64. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
388 md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
389 /* Append the length. */
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
390 md5_append(pms, data, 8);
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
391 for (i = 0; i < 16; ++i)
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
392 digest[i] = (md5_byte_t)(pms->abcd[i >> 2] >> ((i & 3) << 3));
58a6e6e26695 [gaim-migrate @ 751]
Eric Warmenhoven <eric@warmenhoven.org>
parents:
diff changeset
393 }