1*b5677b36Schristos /* $NetBSD: md5_dgst.c,v 1.1.1.1 2009/04/12 15:33:31 christos Exp $ */
2*b5677b36Schristos
3*b5677b36Schristos /* crypto/md/md5_dgst.c */
4*b5677b36Schristos /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
5*b5677b36Schristos * All rights reserved.
6*b5677b36Schristos *
7*b5677b36Schristos * This package is an SSL implementation written
8*b5677b36Schristos * by Eric Young (eay@cryptsoft.com).
9*b5677b36Schristos * The implementation was written so as to conform with Netscapes SSL.
10*b5677b36Schristos *
11*b5677b36Schristos * This library is free for commercial and non-commercial use as long as
12*b5677b36Schristos * the following conditions are aheared to. The following conditions
13*b5677b36Schristos * apply to all code found in this distribution, be it the RC4, RSA,
14*b5677b36Schristos * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15*b5677b36Schristos * included with this distribution is covered by the same copyright terms
16*b5677b36Schristos * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17*b5677b36Schristos *
18*b5677b36Schristos * Copyright remains Eric Young's, and as such any Copyright notices in
19*b5677b36Schristos * the code are not to be removed.
20*b5677b36Schristos * If this package is used in a product, Eric Young should be given attribution
21*b5677b36Schristos * as the author of the parts of the library used.
22*b5677b36Schristos * This can be in the form of a textual message at program startup or
23*b5677b36Schristos * in documentation (online or textual) provided with the package.
24*b5677b36Schristos *
25*b5677b36Schristos * Redistribution and use in source and binary forms, with or without
26*b5677b36Schristos * modification, are permitted provided that the following conditions
27*b5677b36Schristos * are met:
28*b5677b36Schristos * 1. Redistributions of source code must retain the copyright
29*b5677b36Schristos * notice, this list of conditions and the following disclaimer.
30*b5677b36Schristos * 2. Redistributions in binary form must reproduce the above copyright
31*b5677b36Schristos * notice, this list of conditions and the following disclaimer in the
32*b5677b36Schristos * documentation and/or other materials provided with the distribution.
33*b5677b36Schristos * 3. All advertising materials mentioning features or use of this software
34*b5677b36Schristos * must display the following acknowledgement:
35*b5677b36Schristos * "This product includes cryptographic software written by
36*b5677b36Schristos * Eric Young (eay@cryptsoft.com)"
37*b5677b36Schristos * The word 'cryptographic' can be left out if the rouines from the library
38*b5677b36Schristos * being used are not cryptographic related :-).
39*b5677b36Schristos * 4. If you include any Windows specific code (or a derivative thereof) from
40*b5677b36Schristos * the apps directory (application code) you must include an acknowledgement:
41*b5677b36Schristos * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42*b5677b36Schristos *
43*b5677b36Schristos * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44*b5677b36Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45*b5677b36Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46*b5677b36Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47*b5677b36Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48*b5677b36Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49*b5677b36Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50*b5677b36Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51*b5677b36Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52*b5677b36Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53*b5677b36Schristos * SUCH DAMAGE.
54*b5677b36Schristos *
55*b5677b36Schristos * The licence and distribution terms for any publically available version or
56*b5677b36Schristos * derivative of this code cannot be changed. i.e. this code cannot simply be
57*b5677b36Schristos * copied and put under another distribution licence
58*b5677b36Schristos * [including the GNU Public Licence.]
59*b5677b36Schristos */
60*b5677b36Schristos
61*b5677b36Schristos #ifdef USE_MD5 /*%< Added by ogud@tis.com 1998/1/26 */
62*b5677b36Schristos #include <port_before.h>
63*b5677b36Schristos #ifndef HAVE_MD5
64*b5677b36Schristos #include <stdio.h>
65*b5677b36Schristos #include "md5_locl.h"
66*b5677b36Schristos #include <port_after.h>
67*b5677b36Schristos
68*b5677b36Schristos const char *MD5_version="MD5 part of SSLeay 0.8.1 19-Jul-1997";
69*b5677b36Schristos
70*b5677b36Schristos /*! \file
71*b5677b36Schristos * \brief
72*b5677b36Schristos * Implemented from RFC1321 The MD5 Message-Digest Algorithm
73*b5677b36Schristos */
74*b5677b36Schristos
75*b5677b36Schristos #define INIT_DATA_A (unsigned long)0x67452301L
76*b5677b36Schristos #define INIT_DATA_B (unsigned long)0xefcdab89L
77*b5677b36Schristos #define INIT_DATA_C (unsigned long)0x98badcfeL
78*b5677b36Schristos #define INIT_DATA_D (unsigned long)0x10325476L
79*b5677b36Schristos
80*b5677b36Schristos #ifndef NOPROTO
81*b5677b36Schristos static void md5_block(MD5_CTX *c, unsigned long *p);
82*b5677b36Schristos #else
83*b5677b36Schristos static void md5_block();
84*b5677b36Schristos #endif
85*b5677b36Schristos
MD5_Init(c)86*b5677b36Schristos void MD5_Init(c)
87*b5677b36Schristos MD5_CTX *c;
88*b5677b36Schristos {
89*b5677b36Schristos c->A=INIT_DATA_A;
90*b5677b36Schristos c->B=INIT_DATA_B;
91*b5677b36Schristos c->C=INIT_DATA_C;
92*b5677b36Schristos c->D=INIT_DATA_D;
93*b5677b36Schristos c->Nl=0;
94*b5677b36Schristos c->Nh=0;
95*b5677b36Schristos c->num=0;
96*b5677b36Schristos }
97*b5677b36Schristos
MD5_Update(c,data,len)98*b5677b36Schristos void MD5_Update(c, data, len)
99*b5677b36Schristos MD5_CTX *c;
100*b5677b36Schristos register const unsigned char *data;
101*b5677b36Schristos unsigned long len;
102*b5677b36Schristos {
103*b5677b36Schristos register ULONG *p;
104*b5677b36Schristos int sw,sc;
105*b5677b36Schristos ULONG l;
106*b5677b36Schristos
107*b5677b36Schristos if (len == 0U) return;
108*b5677b36Schristos
109*b5677b36Schristos l=(c->Nl+(len<<3))&0xffffffffL;
110*b5677b36Schristos /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
111*b5677b36Schristos * Wei Dai <weidai@eskimo.com> for pointing it out. */
112*b5677b36Schristos if (l < c->Nl) /*%< overflow */
113*b5677b36Schristos c->Nh++;
114*b5677b36Schristos c->Nh+=(len>>29);
115*b5677b36Schristos c->Nl=l;
116*b5677b36Schristos
117*b5677b36Schristos if (c->num != 0)
118*b5677b36Schristos {
119*b5677b36Schristos p=c->data;
120*b5677b36Schristos sw=c->num>>2;
121*b5677b36Schristos sc=c->num&0x03;
122*b5677b36Schristos
123*b5677b36Schristos if ((c->num+len) >= (size_t)MD5_CBLOCK)
124*b5677b36Schristos {
125*b5677b36Schristos l= p[sw];
126*b5677b36Schristos p_c2l(data,l,sc);
127*b5677b36Schristos p[sw++]=l;
128*b5677b36Schristos for (; sw<MD5_LBLOCK; sw++)
129*b5677b36Schristos {
130*b5677b36Schristos c2l(data,l);
131*b5677b36Schristos p[sw]=l;
132*b5677b36Schristos }
133*b5677b36Schristos len-=(MD5_CBLOCK-c->num);
134*b5677b36Schristos
135*b5677b36Schristos md5_block(c,p);
136*b5677b36Schristos c->num=0;
137*b5677b36Schristos /* drop through and do the rest */
138*b5677b36Schristos }
139*b5677b36Schristos else
140*b5677b36Schristos {
141*b5677b36Schristos int ew,ec;
142*b5677b36Schristos
143*b5677b36Schristos c->num+=(int)len;
144*b5677b36Schristos if ((sc+len) < 4U) /*%< ugly, add char's to a word */
145*b5677b36Schristos {
146*b5677b36Schristos l= p[sw];
147*b5677b36Schristos p_c2l_p(data,l,sc,len);
148*b5677b36Schristos p[sw]=l;
149*b5677b36Schristos }
150*b5677b36Schristos else
151*b5677b36Schristos {
152*b5677b36Schristos ew=(c->num>>2);
153*b5677b36Schristos ec=(c->num&0x03);
154*b5677b36Schristos l= p[sw];
155*b5677b36Schristos p_c2l(data,l,sc);
156*b5677b36Schristos p[sw++]=l;
157*b5677b36Schristos for (; sw < ew; sw++)
158*b5677b36Schristos { c2l(data,l); p[sw]=l; }
159*b5677b36Schristos if (ec)
160*b5677b36Schristos {
161*b5677b36Schristos c2l_p(data,l,ec);
162*b5677b36Schristos p[sw]=l;
163*b5677b36Schristos }
164*b5677b36Schristos }
165*b5677b36Schristos return;
166*b5677b36Schristos }
167*b5677b36Schristos }
168*b5677b36Schristos /* we now can process the input data in blocks of MD5_CBLOCK
169*b5677b36Schristos * chars and save the leftovers to c->data. */
170*b5677b36Schristos p=c->data;
171*b5677b36Schristos while (len >= (size_t)MD5_CBLOCK)
172*b5677b36Schristos {
173*b5677b36Schristos #if defined(L_ENDIAN) || defined(B_ENDIAN)
174*b5677b36Schristos memcpy(p,data,MD5_CBLOCK);
175*b5677b36Schristos data+=MD5_CBLOCK;
176*b5677b36Schristos #ifdef B_ENDIAN
177*b5677b36Schristos for (sw=(MD5_LBLOCK/4); sw; sw--)
178*b5677b36Schristos {
179*b5677b36Schristos Endian_Reverse32(p[0]);
180*b5677b36Schristos Endian_Reverse32(p[1]);
181*b5677b36Schristos Endian_Reverse32(p[2]);
182*b5677b36Schristos Endian_Reverse32(p[3]);
183*b5677b36Schristos p+=4;
184*b5677b36Schristos }
185*b5677b36Schristos #endif
186*b5677b36Schristos #else
187*b5677b36Schristos for (sw=(MD5_LBLOCK/4); sw; sw--)
188*b5677b36Schristos {
189*b5677b36Schristos c2l(data,l); *(p++)=l;
190*b5677b36Schristos c2l(data,l); *(p++)=l;
191*b5677b36Schristos c2l(data,l); *(p++)=l;
192*b5677b36Schristos c2l(data,l); *(p++)=l;
193*b5677b36Schristos }
194*b5677b36Schristos #endif
195*b5677b36Schristos p=c->data;
196*b5677b36Schristos md5_block(c,p);
197*b5677b36Schristos len-=MD5_CBLOCK;
198*b5677b36Schristos }
199*b5677b36Schristos sc=(int)len;
200*b5677b36Schristos c->num=sc;
201*b5677b36Schristos if (sc)
202*b5677b36Schristos {
203*b5677b36Schristos sw=sc>>2; /*%< words to copy */
204*b5677b36Schristos #ifdef L_ENDIAN
205*b5677b36Schristos p[sw]=0;
206*b5677b36Schristos memcpy(p,data,sc);
207*b5677b36Schristos #else
208*b5677b36Schristos sc&=0x03;
209*b5677b36Schristos for ( ; sw; sw--)
210*b5677b36Schristos { c2l(data,l); *(p++)=l; }
211*b5677b36Schristos c2l_p(data,l,sc);
212*b5677b36Schristos *p=l;
213*b5677b36Schristos #endif
214*b5677b36Schristos }
215*b5677b36Schristos }
216*b5677b36Schristos
md5_block(c,X)217*b5677b36Schristos static void md5_block(c, X)
218*b5677b36Schristos MD5_CTX *c;
219*b5677b36Schristos register ULONG *X;
220*b5677b36Schristos {
221*b5677b36Schristos register ULONG A,B,C,D;
222*b5677b36Schristos
223*b5677b36Schristos A=c->A;
224*b5677b36Schristos B=c->B;
225*b5677b36Schristos C=c->C;
226*b5677b36Schristos D=c->D;
227*b5677b36Schristos
228*b5677b36Schristos /* Round 0 */
229*b5677b36Schristos R0(A,B,C,D,X[ 0], 7,0xd76aa478L);
230*b5677b36Schristos R0(D,A,B,C,X[ 1],12,0xe8c7b756L);
231*b5677b36Schristos R0(C,D,A,B,X[ 2],17,0x242070dbL);
232*b5677b36Schristos R0(B,C,D,A,X[ 3],22,0xc1bdceeeL);
233*b5677b36Schristos R0(A,B,C,D,X[ 4], 7,0xf57c0fafL);
234*b5677b36Schristos R0(D,A,B,C,X[ 5],12,0x4787c62aL);
235*b5677b36Schristos R0(C,D,A,B,X[ 6],17,0xa8304613L);
236*b5677b36Schristos R0(B,C,D,A,X[ 7],22,0xfd469501L);
237*b5677b36Schristos R0(A,B,C,D,X[ 8], 7,0x698098d8L);
238*b5677b36Schristos R0(D,A,B,C,X[ 9],12,0x8b44f7afL);
239*b5677b36Schristos R0(C,D,A,B,X[10],17,0xffff5bb1L);
240*b5677b36Schristos R0(B,C,D,A,X[11],22,0x895cd7beL);
241*b5677b36Schristos R0(A,B,C,D,X[12], 7,0x6b901122L);
242*b5677b36Schristos R0(D,A,B,C,X[13],12,0xfd987193L);
243*b5677b36Schristos R0(C,D,A,B,X[14],17,0xa679438eL);
244*b5677b36Schristos R0(B,C,D,A,X[15],22,0x49b40821L);
245*b5677b36Schristos /* Round 1 */
246*b5677b36Schristos R1(A,B,C,D,X[ 1], 5,0xf61e2562L);
247*b5677b36Schristos R1(D,A,B,C,X[ 6], 9,0xc040b340L);
248*b5677b36Schristos R1(C,D,A,B,X[11],14,0x265e5a51L);
249*b5677b36Schristos R1(B,C,D,A,X[ 0],20,0xe9b6c7aaL);
250*b5677b36Schristos R1(A,B,C,D,X[ 5], 5,0xd62f105dL);
251*b5677b36Schristos R1(D,A,B,C,X[10], 9,0x02441453L);
252*b5677b36Schristos R1(C,D,A,B,X[15],14,0xd8a1e681L);
253*b5677b36Schristos R1(B,C,D,A,X[ 4],20,0xe7d3fbc8L);
254*b5677b36Schristos R1(A,B,C,D,X[ 9], 5,0x21e1cde6L);
255*b5677b36Schristos R1(D,A,B,C,X[14], 9,0xc33707d6L);
256*b5677b36Schristos R1(C,D,A,B,X[ 3],14,0xf4d50d87L);
257*b5677b36Schristos R1(B,C,D,A,X[ 8],20,0x455a14edL);
258*b5677b36Schristos R1(A,B,C,D,X[13], 5,0xa9e3e905L);
259*b5677b36Schristos R1(D,A,B,C,X[ 2], 9,0xfcefa3f8L);
260*b5677b36Schristos R1(C,D,A,B,X[ 7],14,0x676f02d9L);
261*b5677b36Schristos R1(B,C,D,A,X[12],20,0x8d2a4c8aL);
262*b5677b36Schristos /* Round 2 */
263*b5677b36Schristos R2(A,B,C,D,X[ 5], 4,0xfffa3942L);
264*b5677b36Schristos R2(D,A,B,C,X[ 8],11,0x8771f681L);
265*b5677b36Schristos R2(C,D,A,B,X[11],16,0x6d9d6122L);
266*b5677b36Schristos R2(B,C,D,A,X[14],23,0xfde5380cL);
267*b5677b36Schristos R2(A,B,C,D,X[ 1], 4,0xa4beea44L);
268*b5677b36Schristos R2(D,A,B,C,X[ 4],11,0x4bdecfa9L);
269*b5677b36Schristos R2(C,D,A,B,X[ 7],16,0xf6bb4b60L);
270*b5677b36Schristos R2(B,C,D,A,X[10],23,0xbebfbc70L);
271*b5677b36Schristos R2(A,B,C,D,X[13], 4,0x289b7ec6L);
272*b5677b36Schristos R2(D,A,B,C,X[ 0],11,0xeaa127faL);
273*b5677b36Schristos R2(C,D,A,B,X[ 3],16,0xd4ef3085L);
274*b5677b36Schristos R2(B,C,D,A,X[ 6],23,0x04881d05L);
275*b5677b36Schristos R2(A,B,C,D,X[ 9], 4,0xd9d4d039L);
276*b5677b36Schristos R2(D,A,B,C,X[12],11,0xe6db99e5L);
277*b5677b36Schristos R2(C,D,A,B,X[15],16,0x1fa27cf8L);
278*b5677b36Schristos R2(B,C,D,A,X[ 2],23,0xc4ac5665L);
279*b5677b36Schristos /* Round 3 */
280*b5677b36Schristos R3(A,B,C,D,X[ 0], 6,0xf4292244L);
281*b5677b36Schristos R3(D,A,B,C,X[ 7],10,0x432aff97L);
282*b5677b36Schristos R3(C,D,A,B,X[14],15,0xab9423a7L);
283*b5677b36Schristos R3(B,C,D,A,X[ 5],21,0xfc93a039L);
284*b5677b36Schristos R3(A,B,C,D,X[12], 6,0x655b59c3L);
285*b5677b36Schristos R3(D,A,B,C,X[ 3],10,0x8f0ccc92L);
286*b5677b36Schristos R3(C,D,A,B,X[10],15,0xffeff47dL);
287*b5677b36Schristos R3(B,C,D,A,X[ 1],21,0x85845dd1L);
288*b5677b36Schristos R3(A,B,C,D,X[ 8], 6,0x6fa87e4fL);
289*b5677b36Schristos R3(D,A,B,C,X[15],10,0xfe2ce6e0L);
290*b5677b36Schristos R3(C,D,A,B,X[ 6],15,0xa3014314L);
291*b5677b36Schristos R3(B,C,D,A,X[13],21,0x4e0811a1L);
292*b5677b36Schristos R3(A,B,C,D,X[ 4], 6,0xf7537e82L);
293*b5677b36Schristos R3(D,A,B,C,X[11],10,0xbd3af235L);
294*b5677b36Schristos R3(C,D,A,B,X[ 2],15,0x2ad7d2bbL);
295*b5677b36Schristos R3(B,C,D,A,X[ 9],21,0xeb86d391L);
296*b5677b36Schristos
297*b5677b36Schristos c->A+=A&0xffffffffL;
298*b5677b36Schristos c->B+=B&0xffffffffL;
299*b5677b36Schristos c->C+=C&0xffffffffL;
300*b5677b36Schristos c->D+=D&0xffffffffL;
301*b5677b36Schristos }
302*b5677b36Schristos
MD5_Final(md,c)303*b5677b36Schristos void MD5_Final(md, c)
304*b5677b36Schristos unsigned char *md;
305*b5677b36Schristos MD5_CTX *c;
306*b5677b36Schristos {
307*b5677b36Schristos register int i,j;
308*b5677b36Schristos register ULONG l;
309*b5677b36Schristos register ULONG *p;
310*b5677b36Schristos static unsigned char end[4]={0x80,0x00,0x00,0x00};
311*b5677b36Schristos unsigned char *cp=end;
312*b5677b36Schristos
313*b5677b36Schristos /* c->num should definitly have room for at least one more byte. */
314*b5677b36Schristos p=c->data;
315*b5677b36Schristos j=c->num;
316*b5677b36Schristos i=j>>2;
317*b5677b36Schristos
318*b5677b36Schristos /* purify often complains about the following line as an
319*b5677b36Schristos * Uninitialized Memory Read. While this can be true, the
320*b5677b36Schristos * following p_c2l macro will reset l when that case is true.
321*b5677b36Schristos * This is because j&0x03 contains the number of 'valid' bytes
322*b5677b36Schristos * already in p[i]. If and only if j&0x03 == 0, the UMR will
323*b5677b36Schristos * occur but this is also the only time p_c2l will do
324*b5677b36Schristos * l= *(cp++) instead of l|= *(cp++)
325*b5677b36Schristos * Many thanks to Alex Tang <altitude@cic.net> for pickup this
326*b5677b36Schristos * 'potential bug' */
327*b5677b36Schristos #ifdef PURIFY
328*b5677b36Schristos if ((j&0x03) == 0) p[i]=0;
329*b5677b36Schristos #endif
330*b5677b36Schristos l=p[i];
331*b5677b36Schristos p_c2l(cp,l,j&0x03);
332*b5677b36Schristos p[i]=l;
333*b5677b36Schristos i++;
334*b5677b36Schristos /* i is the next 'undefined word' */
335*b5677b36Schristos if (c->num >= MD5_LAST_BLOCK)
336*b5677b36Schristos {
337*b5677b36Schristos for (; i<MD5_LBLOCK; i++)
338*b5677b36Schristos p[i]=0;
339*b5677b36Schristos md5_block(c,p);
340*b5677b36Schristos i=0;
341*b5677b36Schristos }
342*b5677b36Schristos for (; i<(MD5_LBLOCK-2); i++)
343*b5677b36Schristos p[i]=0;
344*b5677b36Schristos p[MD5_LBLOCK-2]=c->Nl;
345*b5677b36Schristos p[MD5_LBLOCK-1]=c->Nh;
346*b5677b36Schristos md5_block(c,p);
347*b5677b36Schristos cp=md;
348*b5677b36Schristos l=c->A; l2c(l,cp);
349*b5677b36Schristos l=c->B; l2c(l,cp);
350*b5677b36Schristos l=c->C; l2c(l,cp);
351*b5677b36Schristos l=c->D; l2c(l,cp);
352*b5677b36Schristos
353*b5677b36Schristos /* clear stuff, md5_block may be leaving some stuff on the stack
354*b5677b36Schristos * but I'm not worried :-) */
355*b5677b36Schristos c->num=0;
356*b5677b36Schristos /* memset((char *)&c,0,sizeof(c));*/
357*b5677b36Schristos }
358*b5677b36Schristos
359*b5677b36Schristos #ifdef undef
printit(l)360*b5677b36Schristos int printit(l)
361*b5677b36Schristos unsigned long *l;
362*b5677b36Schristos {
363*b5677b36Schristos int i,ii;
364*b5677b36Schristos
365*b5677b36Schristos for (i=0; i<2; i++)
366*b5677b36Schristos {
367*b5677b36Schristos for (ii=0; ii<8; ii++)
368*b5677b36Schristos {
369*b5677b36Schristos fprintf(stderr,"%08lx ",l[i*8+ii]);
370*b5677b36Schristos }
371*b5677b36Schristos fprintf(stderr,"\n");
372*b5677b36Schristos }
373*b5677b36Schristos }
374*b5677b36Schristos #endif
375*b5677b36Schristos #endif /* HAVE_MD5 */
376*b5677b36Schristos #endif /* USE_MD5 */
377