1 |
/* md5.c - MD5 Message-Digest Algorithm |
/* md5.c - MD5 Message-Digest Algorithm |
2 |
* Copyright (C) 1995, 1996, 1998, 1999, |
* Copyright (C) 1995, 1996, 1998, 1999, |
3 |
* 2000, 2001 Free Software Foundation, Inc. |
* 2000, 2001 Free Software Foundation, Inc. |
4 |
* |
* |
5 |
* This program is free software; you can redistribute it and/or modify it |
* This program is free software; you can redistribute it and/or modify it |
6 |
* under the terms of the GNU General Public License as published by the |
* under the terms of the GNU General Public License as published by the |
7 |
* Free Software Foundation; either version 2, or (at your option) any |
* Free Software Foundation; either version 2, or (at your option) any |
8 |
* later version. |
* later version. |
9 |
* |
* |
10 |
* This program is distributed in the hope that it will be useful, |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
14 |
* |
* |
15 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software Foundation, |
* along with this program; if not, write to the Free Software Foundation, |
17 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
* |
* |
19 |
* According to the definition of MD5 in RFC 1321 from April 1992. |
* According to the definition of MD5 in RFC 1321 from April 1992. |
20 |
* NOTE: This is *not* the same file as the one from glibc. |
* NOTE: This is *not* the same file as the one from glibc. |
21 |
*/ |
*/ |
22 |
/* Written by Ulrich Drepper <[email protected]>, 1995. */ |
/* Written by Ulrich Drepper <[email protected]>, 1995. */ |
23 |
/* Heavily modified for GnuPG by <[email protected]> */ |
/* Heavily modified for GnuPG by <[email protected]> */ |
24 |
|
|
25 |
/* Test values: |
/* Test values: |
26 |
* "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E |
* "" D4 1D 8C D9 8F 00 B2 04 E9 80 09 98 EC F8 42 7E |
27 |
* "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61 |
* "a" 0C C1 75 B9 C0 F1 B6 A8 31 C3 99 E2 69 77 26 61 |
28 |
* "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72 |
* "abc 90 01 50 98 3C D2 4F B0 D6 96 3F 7D 28 E1 7F 72 |
29 |
* "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0 |
* "message digest" F9 6B 69 7D 7C B7 93 8D 52 5A 2F 31 AA F1 61 D0 |
30 |
*/ |
*/ |
31 |
|
|
32 |
#include <stdio.h> |
#ifdef HAVE_CONFIG_H |
33 |
#include <stdlib.h> |
#include <config.h> |
34 |
#include <string.h> |
#endif |
35 |
#include <assert.h> |
|
36 |
|
#include <stdio.h> |
37 |
#include "md.h" |
#include <stdio.h> |
38 |
|
#include <stdlib.h> |
39 |
|
#include <string.h> |
40 |
void |
#include <assert.h> |
41 |
md5_init( MD5_CONTEXT *ctx ) |
|
42 |
{ |
#include "md.h" |
43 |
ctx->A = 0x67452301; |
|
44 |
ctx->B = 0xefcdab89; |
|
45 |
ctx->C = 0x98badcfe; |
void |
46 |
ctx->D = 0x10325476; |
md5_init( MD5_CONTEXT *ctx ) |
47 |
|
{ |
48 |
ctx->nblocks = 0; |
ctx->A = 0x67452301; |
49 |
ctx->count = 0; |
ctx->B = 0xefcdab89; |
50 |
} |
ctx->C = 0x98badcfe; |
51 |
|
ctx->D = 0x10325476; |
52 |
|
|
53 |
|
ctx->nblocks = 0; |
54 |
|
ctx->count = 0; |
55 |
/* These are the four functions used in the four steps of the MD5 algorithm |
} |
56 |
and defined in the RFC 1321. The first function is a little bit optimized |
|
57 |
(as found in Colin Plumbs public domain implementation). */ |
|
58 |
/* #define FF(b, c, d) ((b & c) | (~b & d)) */ |
|
59 |
#define FF(b, c, d) (d ^ (b & (c ^ d))) |
|
60 |
#define FG(b, c, d) FF (d, b, c) |
/* These are the four functions used in the four steps of the MD5 algorithm |
61 |
#define FH(b, c, d) (b ^ c ^ d) |
and defined in the RFC 1321. The first function is a little bit optimized |
62 |
#define FI(b, c, d) (c ^ (b | ~d)) |
(as found in Colin Plumbs public domain implementation). */ |
63 |
|
/* #define FF(b, c, d) ((b & c) | (~b & d)) */ |
64 |
static void |
#define FF(b, c, d) (d ^ (b & (c ^ d))) |
65 |
burn_stack (int bytes) |
#define FG(b, c, d) FF (d, b, c) |
66 |
{ |
#define FH(b, c, d) (b ^ c ^ d) |
67 |
char buf[128]; |
#define FI(b, c, d) (c ^ (b | ~d)) |
68 |
|
|
69 |
memset(buf, 0, sizeof buf); |
static void |
70 |
bytes -= sizeof buf; |
burn_stack (int bytes) |
71 |
if (bytes > 0) |
{ |
72 |
burn_stack (bytes); |
char buf[128]; |
73 |
} |
|
74 |
|
memset(buf, 0, sizeof buf); |
75 |
|
bytes -= sizeof buf; |
76 |
|
if (bytes > 0) |
77 |
/**************** |
burn_stack (bytes); |
78 |
* transform n*64 bytes |
} |
79 |
*/ |
|
80 |
static void |
|
81 |
/*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/ |
|
82 |
transform( MD5_CONTEXT *ctx, unsigned char *data ) |
/**************** |
83 |
{ |
* transform n*64 bytes |
84 |
unsigned long correct_words[16]; |
*/ |
85 |
unsigned long A = ctx->A; |
static void |
86 |
unsigned long B = ctx->B; |
/*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/ |
87 |
unsigned long C = ctx->C; |
transform( MD5_CONTEXT *ctx, unsigned char *data ) |
88 |
unsigned long D = ctx->D; |
{ |
89 |
unsigned long *cwp = correct_words; |
unsigned long correct_words[16]; |
90 |
|
unsigned long A = ctx->A; |
91 |
#ifdef BIG_ENDIAN_HOST |
unsigned long B = ctx->B; |
92 |
{ int i; |
unsigned long C = ctx->C; |
93 |
unsigned char *p2, *p1; |
unsigned long D = ctx->D; |
94 |
for(i=0, p1=data, p2=(unsigned char*)correct_words; i < 16; i++, p2 += 4 ) { |
unsigned long *cwp = correct_words; |
95 |
p2[3] = *p1++; |
|
96 |
p2[2] = *p1++; |
#ifdef BIG_ENDIAN_HOST |
97 |
p2[1] = *p1++; |
{ int i; |
98 |
p2[0] = *p1++; |
unsigned char *p2, *p1; |
99 |
} |
for(i=0, p1=data, p2=(unsigned char*)correct_words; i < 16; i++, p2 += 4 ) { |
100 |
} |
p2[3] = *p1++; |
101 |
#else |
p2[2] = *p1++; |
102 |
memcpy( correct_words, data, 64 ); |
p2[1] = *p1++; |
103 |
#endif |
p2[0] = *p1++; |
104 |
|
} |
105 |
|
} |
106 |
#define OP(a, b, c, d, s, T) \ |
#else |
107 |
do \ |
memcpy( correct_words, data, 64 ); |
108 |
{ \ |
#endif |
109 |
a += FF (b, c, d) + (*cwp++) + T; \ |
|
110 |
a = rol(a, s); \ |
|
111 |
a += b; \ |
#define OP(a, b, c, d, s, T) \ |
112 |
} \ |
do \ |
113 |
while (0) |
{ \ |
114 |
|
a += FF (b, c, d) + (*cwp++) + T; \ |
115 |
/* Before we start, one word about the strange constants. |
a = rol(a, s); \ |
116 |
They are defined in RFC 1321 as |
a += b; \ |
117 |
|
} \ |
118 |
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 |
while (0) |
119 |
*/ |
|
120 |
|
/* Before we start, one word about the strange constants. |
121 |
/* Round 1. */ |
They are defined in RFC 1321 as |
122 |
OP (A, B, C, D, 7, 0xd76aa478); |
|
123 |
OP (D, A, B, C, 12, 0xe8c7b756); |
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 |
124 |
OP (C, D, A, B, 17, 0x242070db); |
*/ |
125 |
OP (B, C, D, A, 22, 0xc1bdceee); |
|
126 |
OP (A, B, C, D, 7, 0xf57c0faf); |
/* Round 1. */ |
127 |
OP (D, A, B, C, 12, 0x4787c62a); |
OP (A, B, C, D, 7, 0xd76aa478); |
128 |
OP (C, D, A, B, 17, 0xa8304613); |
OP (D, A, B, C, 12, 0xe8c7b756); |
129 |
OP (B, C, D, A, 22, 0xfd469501); |
OP (C, D, A, B, 17, 0x242070db); |
130 |
OP (A, B, C, D, 7, 0x698098d8); |
OP (B, C, D, A, 22, 0xc1bdceee); |
131 |
OP (D, A, B, C, 12, 0x8b44f7af); |
OP (A, B, C, D, 7, 0xf57c0faf); |
132 |
OP (C, D, A, B, 17, 0xffff5bb1); |
OP (D, A, B, C, 12, 0x4787c62a); |
133 |
OP (B, C, D, A, 22, 0x895cd7be); |
OP (C, D, A, B, 17, 0xa8304613); |
134 |
OP (A, B, C, D, 7, 0x6b901122); |
OP (B, C, D, A, 22, 0xfd469501); |
135 |
OP (D, A, B, C, 12, 0xfd987193); |
OP (A, B, C, D, 7, 0x698098d8); |
136 |
OP (C, D, A, B, 17, 0xa679438e); |
OP (D, A, B, C, 12, 0x8b44f7af); |
137 |
OP (B, C, D, A, 22, 0x49b40821); |
OP (C, D, A, B, 17, 0xffff5bb1); |
138 |
|
OP (B, C, D, A, 22, 0x895cd7be); |
139 |
#undef OP |
OP (A, B, C, D, 7, 0x6b901122); |
140 |
#define OP(f, a, b, c, d, k, s, T) \ |
OP (D, A, B, C, 12, 0xfd987193); |
141 |
do \ |
OP (C, D, A, B, 17, 0xa679438e); |
142 |
{ \ |
OP (B, C, D, A, 22, 0x49b40821); |
143 |
a += f (b, c, d) + correct_words[k] + T; \ |
|
144 |
a = rol(a, s); \ |
#undef OP |
145 |
a += b; \ |
#define OP(f, a, b, c, d, k, s, T) \ |
146 |
} \ |
do \ |
147 |
while (0) |
{ \ |
148 |
|
a += f (b, c, d) + correct_words[k] + T; \ |
149 |
/* Round 2. */ |
a = rol(a, s); \ |
150 |
OP (FG, A, B, C, D, 1, 5, 0xf61e2562); |
a += b; \ |
151 |
OP (FG, D, A, B, C, 6, 9, 0xc040b340); |
} \ |
152 |
OP (FG, C, D, A, B, 11, 14, 0x265e5a51); |
while (0) |
153 |
OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); |
|
154 |
OP (FG, A, B, C, D, 5, 5, 0xd62f105d); |
/* Round 2. */ |
155 |
OP (FG, D, A, B, C, 10, 9, 0x02441453); |
OP (FG, A, B, C, D, 1, 5, 0xf61e2562); |
156 |
OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); |
OP (FG, D, A, B, C, 6, 9, 0xc040b340); |
157 |
OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); |
OP (FG, C, D, A, B, 11, 14, 0x265e5a51); |
158 |
OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); |
OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); |
159 |
OP (FG, D, A, B, C, 14, 9, 0xc33707d6); |
OP (FG, A, B, C, D, 5, 5, 0xd62f105d); |
160 |
OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); |
OP (FG, D, A, B, C, 10, 9, 0x02441453); |
161 |
OP (FG, B, C, D, A, 8, 20, 0x455a14ed); |
OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); |
162 |
OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); |
OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); |
163 |
OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); |
OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); |
164 |
OP (FG, C, D, A, B, 7, 14, 0x676f02d9); |
OP (FG, D, A, B, C, 14, 9, 0xc33707d6); |
165 |
OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); |
OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); |
166 |
|
OP (FG, B, C, D, A, 8, 20, 0x455a14ed); |
167 |
/* Round 3. */ |
OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); |
168 |
OP (FH, A, B, C, D, 5, 4, 0xfffa3942); |
OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); |
169 |
OP (FH, D, A, B, C, 8, 11, 0x8771f681); |
OP (FG, C, D, A, B, 7, 14, 0x676f02d9); |
170 |
OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); |
OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); |
171 |
OP (FH, B, C, D, A, 14, 23, 0xfde5380c); |
|
172 |
OP (FH, A, B, C, D, 1, 4, 0xa4beea44); |
/* Round 3. */ |
173 |
OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); |
OP (FH, A, B, C, D, 5, 4, 0xfffa3942); |
174 |
OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); |
OP (FH, D, A, B, C, 8, 11, 0x8771f681); |
175 |
OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); |
OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); |
176 |
OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); |
OP (FH, B, C, D, A, 14, 23, 0xfde5380c); |
177 |
OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); |
OP (FH, A, B, C, D, 1, 4, 0xa4beea44); |
178 |
OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); |
OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); |
179 |
OP (FH, B, C, D, A, 6, 23, 0x04881d05); |
OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); |
180 |
OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); |
OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); |
181 |
OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); |
OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); |
182 |
OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); |
OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); |
183 |
OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); |
OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); |
184 |
|
OP (FH, B, C, D, A, 6, 23, 0x04881d05); |
185 |
/* Round 4. */ |
OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); |
186 |
OP (FI, A, B, C, D, 0, 6, 0xf4292244); |
OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); |
187 |
OP (FI, D, A, B, C, 7, 10, 0x432aff97); |
OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); |
188 |
OP (FI, C, D, A, B, 14, 15, 0xab9423a7); |
OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); |
189 |
OP (FI, B, C, D, A, 5, 21, 0xfc93a039); |
|
190 |
OP (FI, A, B, C, D, 12, 6, 0x655b59c3); |
/* Round 4. */ |
191 |
OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); |
OP (FI, A, B, C, D, 0, 6, 0xf4292244); |
192 |
OP (FI, C, D, A, B, 10, 15, 0xffeff47d); |
OP (FI, D, A, B, C, 7, 10, 0x432aff97); |
193 |
OP (FI, B, C, D, A, 1, 21, 0x85845dd1); |
OP (FI, C, D, A, B, 14, 15, 0xab9423a7); |
194 |
OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); |
OP (FI, B, C, D, A, 5, 21, 0xfc93a039); |
195 |
OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); |
OP (FI, A, B, C, D, 12, 6, 0x655b59c3); |
196 |
OP (FI, C, D, A, B, 6, 15, 0xa3014314); |
OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); |
197 |
OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); |
OP (FI, C, D, A, B, 10, 15, 0xffeff47d); |
198 |
OP (FI, A, B, C, D, 4, 6, 0xf7537e82); |
OP (FI, B, C, D, A, 1, 21, 0x85845dd1); |
199 |
OP (FI, D, A, B, C, 11, 10, 0xbd3af235); |
OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); |
200 |
OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); |
OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); |
201 |
OP (FI, B, C, D, A, 9, 21, 0xeb86d391); |
OP (FI, C, D, A, B, 6, 15, 0xa3014314); |
202 |
|
OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); |
203 |
/* Put checksum in context given as argument. */ |
OP (FI, A, B, C, D, 4, 6, 0xf7537e82); |
204 |
ctx->A += A; |
OP (FI, D, A, B, C, 11, 10, 0xbd3af235); |
205 |
ctx->B += B; |
OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); |
206 |
ctx->C += C; |
OP (FI, B, C, D, A, 9, 21, 0xeb86d391); |
207 |
ctx->D += D; |
|
208 |
} |
/* Put checksum in context given as argument. */ |
209 |
|
ctx->A += A; |
210 |
|
ctx->B += B; |
211 |
|
ctx->C += C; |
212 |
/* The routine updates the message-digest context to |
ctx->D += D; |
213 |
* account for the presence of each of the characters inBuf[0..inLen-1] |
} |
214 |
* in the message whose digest is being computed. |
|
215 |
*/ |
|
216 |
void |
|
217 |
md5_write( MD5_CONTEXT *hd, unsigned char *inbuf, size_t inlen) |
/* The routine updates the message-digest context to |
218 |
{ |
* account for the presence of each of the characters inBuf[0..inLen-1] |
219 |
if( hd->count == 64 ) { /* flush the buffer */ |
* in the message whose digest is being computed. |
220 |
transform( hd, hd->buf ); |
*/ |
221 |
burn_stack (80+6*sizeof(void*)); |
void |
222 |
hd->count = 0; |
md5_write( MD5_CONTEXT *hd, unsigned char *inbuf, size_t inlen) |
223 |
hd->nblocks++; |
{ |
224 |
} |
if( hd->count == 64 ) { /* flush the buffer */ |
225 |
if( !inbuf ) |
transform( hd, hd->buf ); |
226 |
return; |
burn_stack (80+6*sizeof(void*)); |
227 |
if( hd->count ) { |
hd->count = 0; |
228 |
for( ; inlen && hd->count < 64; inlen-- ) |
hd->nblocks++; |
229 |
hd->buf[hd->count++] = *inbuf++; |
} |
230 |
md5_write( hd, NULL, 0 ); |
if( !inbuf ) |
231 |
if( !inlen ) |
return; |
232 |
return; |
if( hd->count ) { |
233 |
} |
for( ; inlen && hd->count < 64; inlen-- ) |
234 |
|
hd->buf[hd->count++] = *inbuf++; |
235 |
while( inlen >= 64 ) { |
md5_write( hd, NULL, 0 ); |
236 |
transform( hd, inbuf ); |
if( !inlen ) |
237 |
hd->count = 0; |
return; |
238 |
hd->nblocks++; |
} |
239 |
inlen -= 64; |
|
240 |
inbuf += 64; |
while( inlen >= 64 ) { |
241 |
} |
transform( hd, inbuf ); |
242 |
burn_stack (80+6*sizeof(void*)); |
hd->count = 0; |
243 |
for( ; inlen && hd->count < 64; inlen-- ) |
hd->nblocks++; |
244 |
hd->buf[hd->count++] = *inbuf++; |
inlen -= 64; |
245 |
} |
inbuf += 64; |
246 |
|
} |
247 |
|
burn_stack (80+6*sizeof(void*)); |
248 |
|
for( ; inlen && hd->count < 64; inlen-- ) |
249 |
/* The routine final terminates the message-digest computation and |
hd->buf[hd->count++] = *inbuf++; |
250 |
* ends with the desired message digest in mdContext->digest[0...15]. |
} |
251 |
* The handle is prepared for a new MD5 cycle. |
|
252 |
* Returns 16 bytes representing the digest. |
|
253 |
*/ |
|
254 |
|
/* The routine final terminates the message-digest computation and |
255 |
void |
* ends with the desired message digest in mdContext->digest[0...15]. |
256 |
md5_final( MD5_CONTEXT *hd ) |
* The handle is prepared for a new MD5 cycle. |
257 |
{ |
* Returns 16 bytes representing the digest. |
258 |
unsigned long t, msb, lsb; |
*/ |
259 |
unsigned char *p; |
|
260 |
|
void |
261 |
md5_write(hd, NULL, 0); /* flush */; |
md5_final( MD5_CONTEXT *hd ) |
262 |
|
{ |
263 |
t = hd->nblocks; |
unsigned long t, msb, lsb; |
264 |
/* multiply by 64 to make a unsigned char count */ |
unsigned char *p; |
265 |
lsb = t << 6; |
|
266 |
msb = t >> 26; |
md5_write(hd, NULL, 0); /* flush */; |
267 |
/* add the count */ |
|
268 |
t = lsb; |
t = hd->nblocks; |
269 |
if( (lsb += hd->count) < t ) |
/* multiply by 64 to make a unsigned char count */ |
270 |
msb++; |
lsb = t << 6; |
271 |
/* multiply by 8 to make a bit count */ |
msb = t >> 26; |
272 |
t = lsb; |
/* add the count */ |
273 |
lsb <<= 3; |
t = lsb; |
274 |
msb <<= 3; |
if( (lsb += hd->count) < t ) |
275 |
msb |= t >> 29; |
msb++; |
276 |
|
/* multiply by 8 to make a bit count */ |
277 |
if( hd->count < 56 ) { /* enough room */ |
t = lsb; |
278 |
hd->buf[hd->count++] = 0x80; /* pad */ |
lsb <<= 3; |
279 |
while( hd->count < 56 ) |
msb <<= 3; |
280 |
hd->buf[hd->count++] = 0; /* pad */ |
msb |= t >> 29; |
281 |
} |
|
282 |
else { /* need one extra block */ |
if( hd->count < 56 ) { /* enough room */ |
283 |
hd->buf[hd->count++] = 0x80; /* pad character */ |
hd->buf[hd->count++] = 0x80; /* pad */ |
284 |
while( hd->count < 64 ) |
while( hd->count < 56 ) |
285 |
hd->buf[hd->count++] = 0; |
hd->buf[hd->count++] = 0; /* pad */ |
286 |
md5_write(hd, NULL, 0); /* flush */; |
} |
287 |
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */ |
else { /* need one extra block */ |
288 |
} |
hd->buf[hd->count++] = 0x80; /* pad character */ |
289 |
/* append the 64 bit count */ |
while( hd->count < 64 ) |
290 |
hd->buf[56] = lsb ; |
hd->buf[hd->count++] = 0; |
291 |
hd->buf[57] = lsb >> 8; |
md5_write(hd, NULL, 0); /* flush */; |
292 |
hd->buf[58] = lsb >> 16; |
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */ |
293 |
hd->buf[59] = lsb >> 24; |
} |
294 |
hd->buf[60] = msb ; |
/* append the 64 bit count */ |
295 |
hd->buf[61] = msb >> 8; |
hd->buf[56] = lsb ; |
296 |
hd->buf[62] = msb >> 16; |
hd->buf[57] = lsb >> 8; |
297 |
hd->buf[63] = msb >> 24; |
hd->buf[58] = lsb >> 16; |
298 |
transform( hd, hd->buf ); |
hd->buf[59] = lsb >> 24; |
299 |
burn_stack (80+6*sizeof(void*)); |
hd->buf[60] = msb ; |
300 |
|
hd->buf[61] = msb >> 8; |
301 |
p = hd->buf; |
hd->buf[62] = msb >> 16; |
302 |
#ifdef BIG_ENDIAN_HOST |
hd->buf[63] = msb >> 24; |
303 |
#define X(a) do { *p++ = hd-> a ; *p++ = hd-> a >> 8; \ |
transform( hd, hd->buf ); |
304 |
*p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0) |
burn_stack (80+6*sizeof(void*)); |
305 |
#else /* little endian */ |
|
306 |
#define X(a) do { *(unsigned long*)p = hd-> a ; p += 4; } while(0) |
p = hd->buf; |
307 |
#endif |
#ifdef BIG_ENDIAN_HOST |
308 |
X(A); |
#define X(a) do { *p++ = hd-> a ; *p++ = hd-> a >> 8; \ |
309 |
X(B); |
*p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0) |
310 |
X(C); |
#else /* little endian */ |
311 |
X(D); |
#define X(a) do { *(unsigned long*)p = hd-> a ; p += 4; } while(0) |
312 |
#undef X |
#endif |
313 |
|
X(A); |
314 |
} |
X(B); |
315 |
|
X(C); |
316 |
unsigned char * |
X(D); |
317 |
md5_read( MD5_CONTEXT *hd ) |
#undef X |
318 |
{ |
|
319 |
return hd->buf; |
} |
320 |
} |
|
321 |
|
unsigned char * |
322 |
|
md5_read( MD5_CONTEXT *hd ) |
323 |
|
{ |
324 |
|
return hd->buf; |
325 |
|
} |