annotate ezBAMQC/src/htslib/cram/md5.c @ 20:9de3bbec2479 draft default tip

Uploaded
author youngkim
date Thu, 31 Mar 2016 10:10:37 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 * MD5 Message-Digest Algorithm (RFC 1321).
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 * Homepage:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 * Author:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 * This software was written by Alexander Peslyak in 2001. No copyright is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 * claimed, and the software is hereby placed in the public domain.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 * In case this attempt to disclaim copyright and place the software in the
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14 * public domain is deemed null and void, then the software is
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 * general public under the following terms:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18 * Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 * modification, are permitted.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 * There's ABSOLUTELY NO WARRANTY, express or implied.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 * (This is a heavily cut-down "BSD license".)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 * This differs from Colin Plumb's older public domain implementation in that
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 * no exactly 32-bit integer data type is required (any 32-bit or wider
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 * unsigned integer data type will do), there's no compile-time endianness
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 * configuration, and the function prototypes match OpenSSL's. No code from
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 * Colin Plumb's implementation has been reused; this comment merely compares
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30 * the properties of the two independent implementations.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 * The primary goals of this implementation are portability and ease of use.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 * It is meant to be fast, but not as fast as possible. Some known
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34 * optimizations are not included to reduce source code size and avoid
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 * compile-time configuration.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 #ifndef HAVE_OPENSSL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 #include "md5.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45 * The basic MD5 functions.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 * F and G are optimized compared to their RFC 1321 definitions for
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 * implementation.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 #define H(x, y, z) ((x) ^ (y) ^ (z))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57 * The MD5 transformation for all four rounds.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 #define STEP(f, a, b, c, d, x, t, s) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 (a) += f((b), (c), (d)) + (x) + (t); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 (a) += (b);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 * SET reads 4 input bytes in little-endian byte order and stores them
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 * in a properly aligned word in host byte order.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 * The check for little-endian architectures that tolerate unaligned
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 * memory accesses is just an optimization. Nothing will break if it
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 * doesn't work.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 #define SET(n) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 (*(MD5_u32plus *)&ptr[(n) * 4])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 #define GET(n) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 SET(n)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77 #else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 #define SET(n) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 (ctx->block[(n)] = \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80 (MD5_u32plus)ptr[(n) * 4] | \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 #define GET(n) \
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 (ctx->block[(n)])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 * This processes one or more 64-byte data blocks, but does NOT update
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 * the bit counters. There are no alignment requirements.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 static void *body(MD5_CTX *ctx, void *data, unsigned long size)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 unsigned char *ptr;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 MD5_u32plus a, b, c, d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 ptr = data;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 a = ctx->a;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101 b = ctx->b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 c = ctx->c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 d = ctx->d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 do {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 saved_a = a;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 saved_b = b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 saved_c = c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 saved_d = d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 /* Round 1 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 /* Round 2 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 /* Round 3 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 /* Round 4 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 a += saved_a;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 b += saved_b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185 c += saved_c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 d += saved_d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 ptr += 64;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 } while (size -= 64);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 ctx->a = a;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 ctx->b = b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 ctx->c = c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 ctx->d = d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 return ptr;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 void MD5_Init(MD5_CTX *ctx)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 ctx->a = 0x67452301;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202 ctx->b = 0xefcdab89;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 ctx->c = 0x98badcfe;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 ctx->d = 0x10325476;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 ctx->lo = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 ctx->hi = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 MD5_u32plus saved_lo;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 unsigned long used, free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215 saved_lo = ctx->lo;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 ctx->hi++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 ctx->hi += size >> 29;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 used = saved_lo & 0x3f;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 if (used) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 free = 64 - used;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 if (size < free) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226 memcpy(&ctx->buffer[used], data, size);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 return;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 memcpy(&ctx->buffer[used], data, free);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 data = (unsigned char *)data + free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 size -= free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 body(ctx, ctx->buffer, 64);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 if (size >= 64) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 data = body(ctx, data, size & ~(unsigned long)0x3f);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 size &= 0x3f;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241 memcpy(ctx->buffer, data, size);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 void MD5_Final(unsigned char *result, MD5_CTX *ctx)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 unsigned long used, free;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248 used = ctx->lo & 0x3f;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250 ctx->buffer[used++] = 0x80;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 free = 64 - used;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 if (free < 8) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
255 memset(&ctx->buffer[used], 0, free);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
256 body(ctx, ctx->buffer, 64);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
257 used = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
258 free = 64;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
259 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
260
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
261 memset(&ctx->buffer[used], 0, free - 8);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
262
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
263 ctx->lo <<= 3;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
264 ctx->buffer[56] = ctx->lo;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
265 ctx->buffer[57] = ctx->lo >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
266 ctx->buffer[58] = ctx->lo >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
267 ctx->buffer[59] = ctx->lo >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
268 ctx->buffer[60] = ctx->hi;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
269 ctx->buffer[61] = ctx->hi >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
270 ctx->buffer[62] = ctx->hi >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
271 ctx->buffer[63] = ctx->hi >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
272
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
273 body(ctx, ctx->buffer, 64);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
274
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
275 result[0] = ctx->a;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
276 result[1] = ctx->a >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
277 result[2] = ctx->a >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
278 result[3] = ctx->a >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
279 result[4] = ctx->b;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
280 result[5] = ctx->b >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
281 result[6] = ctx->b >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
282 result[7] = ctx->b >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
283 result[8] = ctx->c;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
284 result[9] = ctx->c >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
285 result[10] = ctx->c >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
286 result[11] = ctx->c >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
287 result[12] = ctx->d;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
288 result[13] = ctx->d >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
289 result[14] = ctx->d >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
290 result[15] = ctx->d >> 24;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
291
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
292 memset(ctx, 0, sizeof(*ctx));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
293 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
294
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
295 #endif