168651Skris/* crypto/md4/md4_dgst.c */
268651Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
368651Skris * All rights reserved.
468651Skris *
568651Skris * This package is an SSL implementation written
668651Skris * by Eric Young (eay@cryptsoft.com).
768651Skris * The implementation was written so as to conform with Netscapes SSL.
8296341Sdelphij *
968651Skris * This library is free for commercial and non-commercial use as long as
1068651Skris * the following conditions are aheared to.  The following conditions
1168651Skris * apply to all code found in this distribution, be it the RC4, RSA,
1268651Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1368651Skris * included with this distribution is covered by the same copyright terms
1468651Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296341Sdelphij *
1668651Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1768651Skris * the code are not to be removed.
1868651Skris * If this package is used in a product, Eric Young should be given attribution
1968651Skris * as the author of the parts of the library used.
2068651Skris * This can be in the form of a textual message at program startup or
2168651Skris * in documentation (online or textual) provided with the package.
22296341Sdelphij *
2368651Skris * Redistribution and use in source and binary forms, with or without
2468651Skris * modification, are permitted provided that the following conditions
2568651Skris * are met:
2668651Skris * 1. Redistributions of source code must retain the copyright
2768651Skris *    notice, this list of conditions and the following disclaimer.
2868651Skris * 2. Redistributions in binary form must reproduce the above copyright
2968651Skris *    notice, this list of conditions and the following disclaimer in the
3068651Skris *    documentation and/or other materials provided with the distribution.
3168651Skris * 3. All advertising materials mentioning features or use of this software
3268651Skris *    must display the following acknowledgement:
3368651Skris *    "This product includes cryptographic software written by
3468651Skris *     Eric Young (eay@cryptsoft.com)"
3568651Skris *    The word 'cryptographic' can be left out if the rouines from the library
3668651Skris *    being used are not cryptographic related :-).
37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3868651Skris *    the apps directory (application code) you must include an acknowledgement:
3968651Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296341Sdelphij *
4168651Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4268651Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4368651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4468651Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4568651Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4668651Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4768651Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4868651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4968651Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5068651Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5168651Skris * SUCH DAMAGE.
52296341Sdelphij *
5368651Skris * The licence and distribution terms for any publically available version or
5468651Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5568651Skris * copied and put under another distribution licence
5668651Skris * [including the GNU Public Licence.]
5768651Skris */
5868651Skris
5968651Skris#include <stdio.h>
60238405Sjkim#include <openssl/opensslv.h>
61238405Sjkim#include <openssl/crypto.h>
6268651Skris#include "md4_locl.h"
6368651Skris
64296341Sdelphijconst char MD4_version[] = "MD4" OPENSSL_VERSION_PTEXT;
6568651Skris
66296341Sdelphij/*
67296341Sdelphij * Implemented from RFC1186 The MD4 Message-Digest Algorithm
6868651Skris */
6968651Skris
7068651Skris#define INIT_DATA_A (unsigned long)0x67452301L
7168651Skris#define INIT_DATA_B (unsigned long)0xefcdab89L
7268651Skris#define INIT_DATA_C (unsigned long)0x98badcfeL
7368651Skris#define INIT_DATA_D (unsigned long)0x10325476L
7468651Skris
75238405Sjkimfips_md_init(MD4)
76296341Sdelphij{
77296341Sdelphij    memset(c, 0, sizeof(*c));
78296341Sdelphij    c->A = INIT_DATA_A;
79296341Sdelphij    c->B = INIT_DATA_B;
80296341Sdelphij    c->C = INIT_DATA_C;
81296341Sdelphij    c->D = INIT_DATA_D;
82296341Sdelphij    return 1;
83296341Sdelphij}
8468651Skris
8568651Skris#ifndef md4_block_data_order
86296341Sdelphij# ifdef X
87296341Sdelphij#  undef X
88296341Sdelphij# endif
89296341Sdelphijvoid md4_block_data_order(MD4_CTX *c, const void *data_, size_t num)
90296341Sdelphij{
91296341Sdelphij    const unsigned char *data = data_;
92296341Sdelphij    register unsigned MD32_REG_T A, B, C, D, l;
93296341Sdelphij# ifndef MD32_XARRAY
94296341Sdelphij    /* See comment in crypto/sha/sha_locl.h for details. */
95296341Sdelphij    unsigned MD32_REG_T XX0, XX1, XX2, XX3, XX4, XX5, XX6, XX7,
96296341Sdelphij        XX8, XX9, XX10, XX11, XX12, XX13, XX14, XX15;
97296341Sdelphij#  define X(i)   XX##i
98296341Sdelphij# else
99296341Sdelphij    MD4_LONG XX[MD4_LBLOCK];
100296341Sdelphij#  define X(i)   XX[i]
101296341Sdelphij# endif
10268651Skris
103296341Sdelphij    A = c->A;
104296341Sdelphij    B = c->B;
105296341Sdelphij    C = c->C;
106296341Sdelphij    D = c->D;
10768651Skris
108296341Sdelphij    for (; num--;) {
109296341Sdelphij        (void)HOST_c2l(data, l);
110296341Sdelphij        X(0) = l;
111296341Sdelphij        (void)HOST_c2l(data, l);
112296341Sdelphij        X(1) = l;
113296341Sdelphij        /* Round 0 */
114296341Sdelphij        R0(A, B, C, D, X(0), 3, 0);
115296341Sdelphij        (void)HOST_c2l(data, l);
116296341Sdelphij        X(2) = l;
117296341Sdelphij        R0(D, A, B, C, X(1), 7, 0);
118296341Sdelphij        (void)HOST_c2l(data, l);
119296341Sdelphij        X(3) = l;
120296341Sdelphij        R0(C, D, A, B, X(2), 11, 0);
121296341Sdelphij        (void)HOST_c2l(data, l);
122296341Sdelphij        X(4) = l;
123296341Sdelphij        R0(B, C, D, A, X(3), 19, 0);
124296341Sdelphij        (void)HOST_c2l(data, l);
125296341Sdelphij        X(5) = l;
126296341Sdelphij        R0(A, B, C, D, X(4), 3, 0);
127296341Sdelphij        (void)HOST_c2l(data, l);
128296341Sdelphij        X(6) = l;
129296341Sdelphij        R0(D, A, B, C, X(5), 7, 0);
130296341Sdelphij        (void)HOST_c2l(data, l);
131296341Sdelphij        X(7) = l;
132296341Sdelphij        R0(C, D, A, B, X(6), 11, 0);
133296341Sdelphij        (void)HOST_c2l(data, l);
134296341Sdelphij        X(8) = l;
135296341Sdelphij        R0(B, C, D, A, X(7), 19, 0);
136296341Sdelphij        (void)HOST_c2l(data, l);
137296341Sdelphij        X(9) = l;
138296341Sdelphij        R0(A, B, C, D, X(8), 3, 0);
139296341Sdelphij        (void)HOST_c2l(data, l);
140296341Sdelphij        X(10) = l;
141296341Sdelphij        R0(D, A, B, C, X(9), 7, 0);
142296341Sdelphij        (void)HOST_c2l(data, l);
143296341Sdelphij        X(11) = l;
144296341Sdelphij        R0(C, D, A, B, X(10), 11, 0);
145296341Sdelphij        (void)HOST_c2l(data, l);
146296341Sdelphij        X(12) = l;
147296341Sdelphij        R0(B, C, D, A, X(11), 19, 0);
148296341Sdelphij        (void)HOST_c2l(data, l);
149296341Sdelphij        X(13) = l;
150296341Sdelphij        R0(A, B, C, D, X(12), 3, 0);
151296341Sdelphij        (void)HOST_c2l(data, l);
152296341Sdelphij        X(14) = l;
153296341Sdelphij        R0(D, A, B, C, X(13), 7, 0);
154296341Sdelphij        (void)HOST_c2l(data, l);
155296341Sdelphij        X(15) = l;
156296341Sdelphij        R0(C, D, A, B, X(14), 11, 0);
157296341Sdelphij        R0(B, C, D, A, X(15), 19, 0);
158296341Sdelphij        /* Round 1 */
159296341Sdelphij        R1(A, B, C, D, X(0), 3, 0x5A827999L);
160296341Sdelphij        R1(D, A, B, C, X(4), 5, 0x5A827999L);
161296341Sdelphij        R1(C, D, A, B, X(8), 9, 0x5A827999L);
162296341Sdelphij        R1(B, C, D, A, X(12), 13, 0x5A827999L);
163296341Sdelphij        R1(A, B, C, D, X(1), 3, 0x5A827999L);
164296341Sdelphij        R1(D, A, B, C, X(5), 5, 0x5A827999L);
165296341Sdelphij        R1(C, D, A, B, X(9), 9, 0x5A827999L);
166296341Sdelphij        R1(B, C, D, A, X(13), 13, 0x5A827999L);
167296341Sdelphij        R1(A, B, C, D, X(2), 3, 0x5A827999L);
168296341Sdelphij        R1(D, A, B, C, X(6), 5, 0x5A827999L);
169296341Sdelphij        R1(C, D, A, B, X(10), 9, 0x5A827999L);
170296341Sdelphij        R1(B, C, D, A, X(14), 13, 0x5A827999L);
171296341Sdelphij        R1(A, B, C, D, X(3), 3, 0x5A827999L);
172296341Sdelphij        R1(D, A, B, C, X(7), 5, 0x5A827999L);
173296341Sdelphij        R1(C, D, A, B, X(11), 9, 0x5A827999L);
174296341Sdelphij        R1(B, C, D, A, X(15), 13, 0x5A827999L);
175296341Sdelphij        /* Round 2 */
176296341Sdelphij        R2(A, B, C, D, X(0), 3, 0x6ED9EBA1L);
177296341Sdelphij        R2(D, A, B, C, X(8), 9, 0x6ED9EBA1L);
178296341Sdelphij        R2(C, D, A, B, X(4), 11, 0x6ED9EBA1L);
179296341Sdelphij        R2(B, C, D, A, X(12), 15, 0x6ED9EBA1L);
180296341Sdelphij        R2(A, B, C, D, X(2), 3, 0x6ED9EBA1L);
181296341Sdelphij        R2(D, A, B, C, X(10), 9, 0x6ED9EBA1L);
182296341Sdelphij        R2(C, D, A, B, X(6), 11, 0x6ED9EBA1L);
183296341Sdelphij        R2(B, C, D, A, X(14), 15, 0x6ED9EBA1L);
184296341Sdelphij        R2(A, B, C, D, X(1), 3, 0x6ED9EBA1L);
185296341Sdelphij        R2(D, A, B, C, X(9), 9, 0x6ED9EBA1L);
186296341Sdelphij        R2(C, D, A, B, X(5), 11, 0x6ED9EBA1L);
187296341Sdelphij        R2(B, C, D, A, X(13), 15, 0x6ED9EBA1L);
188296341Sdelphij        R2(A, B, C, D, X(3), 3, 0x6ED9EBA1L);
189296341Sdelphij        R2(D, A, B, C, X(11), 9, 0x6ED9EBA1L);
190296341Sdelphij        R2(C, D, A, B, X(7), 11, 0x6ED9EBA1L);
191296341Sdelphij        R2(B, C, D, A, X(15), 15, 0x6ED9EBA1L);
19268651Skris
193296341Sdelphij        A = c->A += A;
194296341Sdelphij        B = c->B += B;
195296341Sdelphij        C = c->C += C;
196296341Sdelphij        D = c->D += D;
197296341Sdelphij    }
198296341Sdelphij}
19968651Skris#endif
200