10Sstevel@tonic-gate /* crypto/md4/md4_dgst.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "md4_locl.h"
610Sstevel@tonic-gate #include <openssl/opensslv.h>
620Sstevel@tonic-gate
630Sstevel@tonic-gate const char *MD4_version="MD4" OPENSSL_VERSION_PTEXT;
640Sstevel@tonic-gate
650Sstevel@tonic-gate /* Implemented from RFC1186 The MD4 Message-Digest Algorithm
660Sstevel@tonic-gate */
670Sstevel@tonic-gate
680Sstevel@tonic-gate #define INIT_DATA_A (unsigned long)0x67452301L
690Sstevel@tonic-gate #define INIT_DATA_B (unsigned long)0xefcdab89L
700Sstevel@tonic-gate #define INIT_DATA_C (unsigned long)0x98badcfeL
710Sstevel@tonic-gate #define INIT_DATA_D (unsigned long)0x10325476L
720Sstevel@tonic-gate
MD4_Init(MD4_CTX * c)730Sstevel@tonic-gate int MD4_Init(MD4_CTX *c)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate c->A=INIT_DATA_A;
760Sstevel@tonic-gate c->B=INIT_DATA_B;
770Sstevel@tonic-gate c->C=INIT_DATA_C;
780Sstevel@tonic-gate c->D=INIT_DATA_D;
790Sstevel@tonic-gate c->Nl=0;
800Sstevel@tonic-gate c->Nh=0;
810Sstevel@tonic-gate c->num=0;
820Sstevel@tonic-gate return 1;
830Sstevel@tonic-gate }
840Sstevel@tonic-gate
850Sstevel@tonic-gate #ifndef md4_block_host_order
md4_block_host_order(MD4_CTX * c,const void * data,size_t num)86*2139Sjp161948 void md4_block_host_order (MD4_CTX *c, const void *data, size_t num)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate const MD4_LONG *X=data;
890Sstevel@tonic-gate register unsigned MD32_REG_T A,B,C,D;
900Sstevel@tonic-gate
910Sstevel@tonic-gate A=c->A;
920Sstevel@tonic-gate B=c->B;
930Sstevel@tonic-gate C=c->C;
940Sstevel@tonic-gate D=c->D;
950Sstevel@tonic-gate
960Sstevel@tonic-gate for (;num--;X+=HASH_LBLOCK)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate /* Round 0 */
990Sstevel@tonic-gate R0(A,B,C,D,X[ 0], 3,0);
1000Sstevel@tonic-gate R0(D,A,B,C,X[ 1], 7,0);
1010Sstevel@tonic-gate R0(C,D,A,B,X[ 2],11,0);
1020Sstevel@tonic-gate R0(B,C,D,A,X[ 3],19,0);
1030Sstevel@tonic-gate R0(A,B,C,D,X[ 4], 3,0);
1040Sstevel@tonic-gate R0(D,A,B,C,X[ 5], 7,0);
1050Sstevel@tonic-gate R0(C,D,A,B,X[ 6],11,0);
1060Sstevel@tonic-gate R0(B,C,D,A,X[ 7],19,0);
1070Sstevel@tonic-gate R0(A,B,C,D,X[ 8], 3,0);
1080Sstevel@tonic-gate R0(D,A,B,C,X[ 9], 7,0);
1090Sstevel@tonic-gate R0(C,D,A,B,X[10],11,0);
1100Sstevel@tonic-gate R0(B,C,D,A,X[11],19,0);
1110Sstevel@tonic-gate R0(A,B,C,D,X[12], 3,0);
1120Sstevel@tonic-gate R0(D,A,B,C,X[13], 7,0);
1130Sstevel@tonic-gate R0(C,D,A,B,X[14],11,0);
1140Sstevel@tonic-gate R0(B,C,D,A,X[15],19,0);
1150Sstevel@tonic-gate /* Round 1 */
1160Sstevel@tonic-gate R1(A,B,C,D,X[ 0], 3,0x5A827999L);
1170Sstevel@tonic-gate R1(D,A,B,C,X[ 4], 5,0x5A827999L);
1180Sstevel@tonic-gate R1(C,D,A,B,X[ 8], 9,0x5A827999L);
1190Sstevel@tonic-gate R1(B,C,D,A,X[12],13,0x5A827999L);
1200Sstevel@tonic-gate R1(A,B,C,D,X[ 1], 3,0x5A827999L);
1210Sstevel@tonic-gate R1(D,A,B,C,X[ 5], 5,0x5A827999L);
1220Sstevel@tonic-gate R1(C,D,A,B,X[ 9], 9,0x5A827999L);
1230Sstevel@tonic-gate R1(B,C,D,A,X[13],13,0x5A827999L);
1240Sstevel@tonic-gate R1(A,B,C,D,X[ 2], 3,0x5A827999L);
1250Sstevel@tonic-gate R1(D,A,B,C,X[ 6], 5,0x5A827999L);
1260Sstevel@tonic-gate R1(C,D,A,B,X[10], 9,0x5A827999L);
1270Sstevel@tonic-gate R1(B,C,D,A,X[14],13,0x5A827999L);
1280Sstevel@tonic-gate R1(A,B,C,D,X[ 3], 3,0x5A827999L);
1290Sstevel@tonic-gate R1(D,A,B,C,X[ 7], 5,0x5A827999L);
1300Sstevel@tonic-gate R1(C,D,A,B,X[11], 9,0x5A827999L);
1310Sstevel@tonic-gate R1(B,C,D,A,X[15],13,0x5A827999L);
1320Sstevel@tonic-gate /* Round 2 */
1330Sstevel@tonic-gate R2(A,B,C,D,X[ 0], 3,0x6ED9EBA1);
1340Sstevel@tonic-gate R2(D,A,B,C,X[ 8], 9,0x6ED9EBA1);
1350Sstevel@tonic-gate R2(C,D,A,B,X[ 4],11,0x6ED9EBA1);
1360Sstevel@tonic-gate R2(B,C,D,A,X[12],15,0x6ED9EBA1);
1370Sstevel@tonic-gate R2(A,B,C,D,X[ 2], 3,0x6ED9EBA1);
1380Sstevel@tonic-gate R2(D,A,B,C,X[10], 9,0x6ED9EBA1);
1390Sstevel@tonic-gate R2(C,D,A,B,X[ 6],11,0x6ED9EBA1);
1400Sstevel@tonic-gate R2(B,C,D,A,X[14],15,0x6ED9EBA1);
1410Sstevel@tonic-gate R2(A,B,C,D,X[ 1], 3,0x6ED9EBA1);
1420Sstevel@tonic-gate R2(D,A,B,C,X[ 9], 9,0x6ED9EBA1);
1430Sstevel@tonic-gate R2(C,D,A,B,X[ 5],11,0x6ED9EBA1);
1440Sstevel@tonic-gate R2(B,C,D,A,X[13],15,0x6ED9EBA1);
1450Sstevel@tonic-gate R2(A,B,C,D,X[ 3], 3,0x6ED9EBA1);
1460Sstevel@tonic-gate R2(D,A,B,C,X[11], 9,0x6ED9EBA1);
1470Sstevel@tonic-gate R2(C,D,A,B,X[ 7],11,0x6ED9EBA1);
1480Sstevel@tonic-gate R2(B,C,D,A,X[15],15,0x6ED9EBA1);
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate A = c->A += A;
1510Sstevel@tonic-gate B = c->B += B;
1520Sstevel@tonic-gate C = c->C += C;
1530Sstevel@tonic-gate D = c->D += D;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate #endif
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate #ifndef md4_block_data_order
1590Sstevel@tonic-gate #ifdef X
1600Sstevel@tonic-gate #undef X
1610Sstevel@tonic-gate #endif
md4_block_data_order(MD4_CTX * c,const void * data_,size_t num)162*2139Sjp161948 void md4_block_data_order (MD4_CTX *c, const void *data_, size_t num)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate const unsigned char *data=data_;
1650Sstevel@tonic-gate register unsigned MD32_REG_T A,B,C,D,l;
1660Sstevel@tonic-gate #ifndef MD32_XARRAY
1670Sstevel@tonic-gate /* See comment in crypto/sha/sha_locl.h for details. */
1680Sstevel@tonic-gate unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7,
1690Sstevel@tonic-gate XX8, XX9,XX10,XX11,XX12,XX13,XX14,XX15;
1700Sstevel@tonic-gate # define X(i) XX##i
1710Sstevel@tonic-gate #else
1720Sstevel@tonic-gate MD4_LONG XX[MD4_LBLOCK];
1730Sstevel@tonic-gate # define X(i) XX[i]
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate A=c->A;
1770Sstevel@tonic-gate B=c->B;
1780Sstevel@tonic-gate C=c->C;
1790Sstevel@tonic-gate D=c->D;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate for (;num--;)
1820Sstevel@tonic-gate {
1830Sstevel@tonic-gate HOST_c2l(data,l); X( 0)=l; HOST_c2l(data,l); X( 1)=l;
1840Sstevel@tonic-gate /* Round 0 */
1850Sstevel@tonic-gate R0(A,B,C,D,X( 0), 3,0); HOST_c2l(data,l); X( 2)=l;
1860Sstevel@tonic-gate R0(D,A,B,C,X( 1), 7,0); HOST_c2l(data,l); X( 3)=l;
1870Sstevel@tonic-gate R0(C,D,A,B,X( 2),11,0); HOST_c2l(data,l); X( 4)=l;
1880Sstevel@tonic-gate R0(B,C,D,A,X( 3),19,0); HOST_c2l(data,l); X( 5)=l;
1890Sstevel@tonic-gate R0(A,B,C,D,X( 4), 3,0); HOST_c2l(data,l); X( 6)=l;
1900Sstevel@tonic-gate R0(D,A,B,C,X( 5), 7,0); HOST_c2l(data,l); X( 7)=l;
1910Sstevel@tonic-gate R0(C,D,A,B,X( 6),11,0); HOST_c2l(data,l); X( 8)=l;
1920Sstevel@tonic-gate R0(B,C,D,A,X( 7),19,0); HOST_c2l(data,l); X( 9)=l;
1930Sstevel@tonic-gate R0(A,B,C,D,X( 8), 3,0); HOST_c2l(data,l); X(10)=l;
1940Sstevel@tonic-gate R0(D,A,B,C,X( 9), 7,0); HOST_c2l(data,l); X(11)=l;
1950Sstevel@tonic-gate R0(C,D,A,B,X(10),11,0); HOST_c2l(data,l); X(12)=l;
1960Sstevel@tonic-gate R0(B,C,D,A,X(11),19,0); HOST_c2l(data,l); X(13)=l;
1970Sstevel@tonic-gate R0(A,B,C,D,X(12), 3,0); HOST_c2l(data,l); X(14)=l;
1980Sstevel@tonic-gate R0(D,A,B,C,X(13), 7,0); HOST_c2l(data,l); X(15)=l;
1990Sstevel@tonic-gate R0(C,D,A,B,X(14),11,0);
2000Sstevel@tonic-gate R0(B,C,D,A,X(15),19,0);
2010Sstevel@tonic-gate /* Round 1 */
2020Sstevel@tonic-gate R1(A,B,C,D,X( 0), 3,0x5A827999L);
2030Sstevel@tonic-gate R1(D,A,B,C,X( 4), 5,0x5A827999L);
2040Sstevel@tonic-gate R1(C,D,A,B,X( 8), 9,0x5A827999L);
2050Sstevel@tonic-gate R1(B,C,D,A,X(12),13,0x5A827999L);
2060Sstevel@tonic-gate R1(A,B,C,D,X( 1), 3,0x5A827999L);
2070Sstevel@tonic-gate R1(D,A,B,C,X( 5), 5,0x5A827999L);
2080Sstevel@tonic-gate R1(C,D,A,B,X( 9), 9,0x5A827999L);
2090Sstevel@tonic-gate R1(B,C,D,A,X(13),13,0x5A827999L);
2100Sstevel@tonic-gate R1(A,B,C,D,X( 2), 3,0x5A827999L);
2110Sstevel@tonic-gate R1(D,A,B,C,X( 6), 5,0x5A827999L);
2120Sstevel@tonic-gate R1(C,D,A,B,X(10), 9,0x5A827999L);
2130Sstevel@tonic-gate R1(B,C,D,A,X(14),13,0x5A827999L);
2140Sstevel@tonic-gate R1(A,B,C,D,X( 3), 3,0x5A827999L);
2150Sstevel@tonic-gate R1(D,A,B,C,X( 7), 5,0x5A827999L);
2160Sstevel@tonic-gate R1(C,D,A,B,X(11), 9,0x5A827999L);
2170Sstevel@tonic-gate R1(B,C,D,A,X(15),13,0x5A827999L);
2180Sstevel@tonic-gate /* Round 2 */
2190Sstevel@tonic-gate R2(A,B,C,D,X( 0), 3,0x6ED9EBA1L);
2200Sstevel@tonic-gate R2(D,A,B,C,X( 8), 9,0x6ED9EBA1L);
2210Sstevel@tonic-gate R2(C,D,A,B,X( 4),11,0x6ED9EBA1L);
2220Sstevel@tonic-gate R2(B,C,D,A,X(12),15,0x6ED9EBA1L);
2230Sstevel@tonic-gate R2(A,B,C,D,X( 2), 3,0x6ED9EBA1L);
2240Sstevel@tonic-gate R2(D,A,B,C,X(10), 9,0x6ED9EBA1L);
2250Sstevel@tonic-gate R2(C,D,A,B,X( 6),11,0x6ED9EBA1L);
2260Sstevel@tonic-gate R2(B,C,D,A,X(14),15,0x6ED9EBA1L);
2270Sstevel@tonic-gate R2(A,B,C,D,X( 1), 3,0x6ED9EBA1L);
2280Sstevel@tonic-gate R2(D,A,B,C,X( 9), 9,0x6ED9EBA1L);
2290Sstevel@tonic-gate R2(C,D,A,B,X( 5),11,0x6ED9EBA1L);
2300Sstevel@tonic-gate R2(B,C,D,A,X(13),15,0x6ED9EBA1L);
2310Sstevel@tonic-gate R2(A,B,C,D,X( 3), 3,0x6ED9EBA1L);
2320Sstevel@tonic-gate R2(D,A,B,C,X(11), 9,0x6ED9EBA1L);
2330Sstevel@tonic-gate R2(C,D,A,B,X( 7),11,0x6ED9EBA1L);
2340Sstevel@tonic-gate R2(B,C,D,A,X(15),15,0x6ED9EBA1L);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate A = c->A += A;
2370Sstevel@tonic-gate B = c->B += B;
2380Sstevel@tonic-gate C = c->C += C;
2390Sstevel@tonic-gate D = c->D += D;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate #ifdef undef
printit(unsigned long * l)2450Sstevel@tonic-gate int printit(unsigned long *l)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate int i,ii;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate for (i=0; i<2; i++)
2500Sstevel@tonic-gate {
2510Sstevel@tonic-gate for (ii=0; ii<8; ii++)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate fprintf(stderr,"%08lx ",l[i*8+ii]);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate fprintf(stderr,"\n");
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate #endif
259