md5_dgst.c revision 65c4736d9c0ebc6d9b1d991593b55566909da9cd
1443158c1101e58bf2fa0b9c785a40265b898a3bTinderbox User/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * All rights reserved.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * This package is an SSL implementation written
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * by Eric Young (eay@cryptsoft.com).
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * The implementation was written so as to conform with Netscapes SSL.
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * This library is free for commercial and non-commercial use as long as
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * the following conditions are aheared to. The following conditions
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * apply to all code found in this distribution, be it the RC4, RSA,
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * included with this distribution is covered by the same copyright terms
1fa26403d7679235a30fbf6289f68fed5872df30Mark Andrews * except that the holder is Tim Hudson (tjh@cryptsoft.com).
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Copyright remains Eric Young's, and as such any Copyright notices in
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * the code are not to be removed.
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews * If this package is used in a product, Eric Young should be given attribution
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * as the author of the parts of the library used.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * This can be in the form of a textual message at program startup or
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * in documentation (online or textual) provided with the package.
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * Redistribution and use in source and binary forms, with or without
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer * modification, are permitted provided that the following conditions
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * 1. Redistributions of source code must retain the copyright
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * notice, this list of conditions and the following disclaimer.
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * 2. Redistributions in binary form must reproduce the above copyright
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * notice, this list of conditions and the following disclaimer in the
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * documentation and/or other materials provided with the distribution.
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * 3. All advertising materials mentioning features or use of this software
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * must display the following acknowledgement:
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * "This product includes cryptographic software written by
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * Eric Young (eay@cryptsoft.com)"
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * The word 'cryptographic' can be left out if the rouines from the library
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * being used are not cryptographic related :-).
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * 4. If you include any Windows specific code (or a derivative thereof) from
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * the apps directory (application code) you must include an acknowledgement:
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
667c498942c0e605c1aab98bca6b90aac4cbbd71Mark Andrews * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * SUCH DAMAGE.
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * The licence and distribution terms for any publically available version or
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrews * derivative of this code cannot be changed. i.e. this code cannot simply be
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * copied and put under another distribution licence
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * [including the GNU Public Licence.]
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews/* Implemented from RFC1321 The MD5 Message-Digest Algorithm
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define INIT_DATA_B (unsigned long)0xefcdab89L
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define INIT_DATA_C (unsigned long)0x98badcfeL
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence#define INIT_DATA_D (unsigned long)0x10325476L
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrencevoid md5_block_host_order (MD5_CTX *c, const void *data, int num)
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence register unsigned long A,B,C,D;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * In case you wonder why A-D are declared as long and not
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * as MD5_LONG. Doing so results in slight performance
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * boost on LP64 architectures. The catch is we don't
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * really care if 32 MSBs of a 64-bit register get polluted
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * with eventual overflows as we *save* only 32 LSBs in
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * *either* case. Now declaring 'em long excuses the compiler
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * from keeping 32 MSBs zeroed resulting in 13% performance
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * Well, to be honest it should say that this *prevents*
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * performance degradation.
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence * <appro@fy.chalmers.se>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence /* Round 0 */
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer /* Round 1 */
422009fe5b15e31e7f5d09212bd1480121a1464eEvan Hunt /* Round 2 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer /* Round 3 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer A = c->A += A;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer B = c->B += B;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer C = c->C += C;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer D = c->D += D;
36e97eb661df71069df2d75f5fd3b5bb0dcf1efeMark Andrewsvoid md5_block_data_order (MD5_CTX *c, const void *data_, int num)
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer register unsigned long A,B,C,D,l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * In case you wonder why A-D are declared as long and not
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * as MD5_LONG. Doing so results in slight performance
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * boost on LP64 architectures. The catch is we don't
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * really care if 32 MSBs of a 64-bit register get polluted
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * with eventual overflows as we *save* only 32 LSBs in
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * *either* case. Now declaring 'em long excuses the compiler
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * from keeping 32 MSBs zeroed resulting in 13% performance
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * Well, to be honest it should say that this *prevents*
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * performance degradation.
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * <appro@fy.chalmers.se>
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * In case you wonder why don't I use c->data for this.
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * RISCs usually have a handful of registers and if X is
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer * declared as automatic array good optimizing compiler
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * shall accomodate at least part of it in register bank
c213258356638f9af1e1c68425df527b856567a2Francis Dupont * instead of memory.
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews * <appro@fy.chalmers.se>
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence HOST_c2l(data,l); X[ 0]=l; HOST_c2l(data,l); X[ 1]=l;
487e6abc16c1b2958d371b0d4e808953646b520aDavid Lawrence /* Round 0 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer R0(A,B,C,D,X[ 0], 7,0xd76aa478L); HOST_c2l(data,l); X[ 2]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(D,A,B,C,X[ 1],12,0xe8c7b756L); HOST_c2l(data,l); X[ 3]=l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer R0(C,D,A,B,X[ 2],17,0x242070dbL); HOST_c2l(data,l); X[ 4]=l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer R0(B,C,D,A,X[ 3],22,0xc1bdceeeL); HOST_c2l(data,l); X[ 5]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(A,B,C,D,X[ 4], 7,0xf57c0fafL); HOST_c2l(data,l); X[ 6]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(D,A,B,C,X[ 5],12,0x4787c62aL); HOST_c2l(data,l); X[ 7]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(C,D,A,B,X[ 6],17,0xa8304613L); HOST_c2l(data,l); X[ 8]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(B,C,D,A,X[ 7],22,0xfd469501L); HOST_c2l(data,l); X[ 9]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(A,B,C,D,X[ 8], 7,0x698098d8L); HOST_c2l(data,l); X[10]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(D,A,B,C,X[ 9],12,0x8b44f7afL); HOST_c2l(data,l); X[11]=l;
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews R0(C,D,A,B,X[10],17,0xffff5bb1L); HOST_c2l(data,l); X[12]=l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer R0(B,C,D,A,X[11],22,0x895cd7beL); HOST_c2l(data,l); X[13]=l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer R0(A,B,C,D,X[12], 7,0x6b901122L); HOST_c2l(data,l); X[14]=l;
6ceca14e5ea7972a9ba8bc1dca0de1f63f669cfdDanny Mayer R0(D,A,B,C,X[13],12,0xfd987193L); HOST_c2l(data,l); X[15]=l;
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer /* Round 1 */
d3243476c280d3a98cadbfd6343437fa2b29356bDanny Mayer /* Round 2 */
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews /* Round 3 */
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews A = c->A += A;
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt B = c->B += B;
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt C = c->C += C;
e7c38ca9635e73c9a928bbab9c73c2abbd499f8bEvan Hunt D = c->D += D;
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrewsint printit(unsigned long *l)
503e5aa8d2400e47c9617db10b57f2d4f3495fd2Mark Andrews for (i=0; i<2; i++)