1195972f6Sopenharmony_ci/*
2195972f6Sopenharmony_ci *  RFC 1186/1320 compliant MD4 implementation
3195972f6Sopenharmony_ci *
4195972f6Sopenharmony_ci *  Based on XySSL: Copyright (C) 2006-2008  Christophe Devine
5195972f6Sopenharmony_ci *
6195972f6Sopenharmony_ci *  Copyright (C) 2009  Paul Bakker <polarssl_maintainer at polarssl dot org>
7195972f6Sopenharmony_ci *
8195972f6Sopenharmony_ci *  All rights reserved.
9195972f6Sopenharmony_ci *
10195972f6Sopenharmony_ci *  Redistribution and use in source and binary forms, with or without
11195972f6Sopenharmony_ci *  modification, are permitted provided that the following conditions
12195972f6Sopenharmony_ci *  are met:
13195972f6Sopenharmony_ci *
14195972f6Sopenharmony_ci *    * Redistributions of source code must retain the above copyright
15195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer.
16195972f6Sopenharmony_ci *    * Redistributions in binary form must reproduce the above copyright
17195972f6Sopenharmony_ci *      notice, this list of conditions and the following disclaimer in the
18195972f6Sopenharmony_ci *      documentation and/or other materials provided with the distribution.
19195972f6Sopenharmony_ci *    * Neither the names of PolarSSL or XySSL nor the names of its contributors
20195972f6Sopenharmony_ci *      may be used to endorse or promote products derived from this software
21195972f6Sopenharmony_ci *      without specific prior written permission.
22195972f6Sopenharmony_ci *
23195972f6Sopenharmony_ci *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24195972f6Sopenharmony_ci *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25195972f6Sopenharmony_ci *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26195972f6Sopenharmony_ci *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27195972f6Sopenharmony_ci *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28195972f6Sopenharmony_ci *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29195972f6Sopenharmony_ci *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30195972f6Sopenharmony_ci *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31195972f6Sopenharmony_ci *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32195972f6Sopenharmony_ci *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33195972f6Sopenharmony_ci *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34195972f6Sopenharmony_ci */
35195972f6Sopenharmony_ci/*
36195972f6Sopenharmony_ci *  The MD4 algorithm was designed by Ron Rivest in 1990.
37195972f6Sopenharmony_ci *
38195972f6Sopenharmony_ci *  http://www.ietf.org/rfc/rfc1186.txt
39195972f6Sopenharmony_ci *  http://www.ietf.org/rfc/rfc1320.txt
40195972f6Sopenharmony_ci */
41195972f6Sopenharmony_ci
42195972f6Sopenharmony_ci#include "netif/ppp/ppp_opts.h"
43195972f6Sopenharmony_ci#if PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_MD4
44195972f6Sopenharmony_ci
45195972f6Sopenharmony_ci#include "netif/ppp/polarssl/md4.h"
46195972f6Sopenharmony_ci
47195972f6Sopenharmony_ci#include <string.h>
48195972f6Sopenharmony_ci
49195972f6Sopenharmony_ci/*
50195972f6Sopenharmony_ci * 32-bit integer manipulation macros (little endian)
51195972f6Sopenharmony_ci */
52195972f6Sopenharmony_ci#ifndef GET_ULONG_LE
53195972f6Sopenharmony_ci#define GET_ULONG_LE(n,b,i)                             \
54195972f6Sopenharmony_ci{                                                       \
55195972f6Sopenharmony_ci    (n) = ( (unsigned long) (b)[(i)    ]       )        \
56195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 1] <<  8 )        \
57195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 2] << 16 )        \
58195972f6Sopenharmony_ci        | ( (unsigned long) (b)[(i) + 3] << 24 );       \
59195972f6Sopenharmony_ci}
60195972f6Sopenharmony_ci#endif
61195972f6Sopenharmony_ci
62195972f6Sopenharmony_ci#ifndef PUT_ULONG_LE
63195972f6Sopenharmony_ci#define PUT_ULONG_LE(n,b,i)                             \
64195972f6Sopenharmony_ci{                                                       \
65195972f6Sopenharmony_ci    (b)[(i)    ] = (unsigned char) ( (n)       );       \
66195972f6Sopenharmony_ci    (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );       \
67195972f6Sopenharmony_ci    (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );       \
68195972f6Sopenharmony_ci    (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );       \
69195972f6Sopenharmony_ci}
70195972f6Sopenharmony_ci#endif
71195972f6Sopenharmony_ci
72195972f6Sopenharmony_ci/*
73195972f6Sopenharmony_ci * MD4 context setup
74195972f6Sopenharmony_ci */
75195972f6Sopenharmony_civoid md4_starts( md4_context *ctx )
76195972f6Sopenharmony_ci{
77195972f6Sopenharmony_ci    ctx->total[0] = 0;
78195972f6Sopenharmony_ci    ctx->total[1] = 0;
79195972f6Sopenharmony_ci
80195972f6Sopenharmony_ci    ctx->state[0] = 0x67452301;
81195972f6Sopenharmony_ci    ctx->state[1] = 0xEFCDAB89;
82195972f6Sopenharmony_ci    ctx->state[2] = 0x98BADCFE;
83195972f6Sopenharmony_ci    ctx->state[3] = 0x10325476;
84195972f6Sopenharmony_ci}
85195972f6Sopenharmony_ci
86195972f6Sopenharmony_cistatic void md4_process( md4_context *ctx, const unsigned char data[64] )
87195972f6Sopenharmony_ci{
88195972f6Sopenharmony_ci    unsigned long X[16], A, B, C, D;
89195972f6Sopenharmony_ci
90195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 0], data,  0 );
91195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 1], data,  4 );
92195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 2], data,  8 );
93195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 3], data, 12 );
94195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 4], data, 16 );
95195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 5], data, 20 );
96195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 6], data, 24 );
97195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 7], data, 28 );
98195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 8], data, 32 );
99195972f6Sopenharmony_ci    GET_ULONG_LE( X[ 9], data, 36 );
100195972f6Sopenharmony_ci    GET_ULONG_LE( X[10], data, 40 );
101195972f6Sopenharmony_ci    GET_ULONG_LE( X[11], data, 44 );
102195972f6Sopenharmony_ci    GET_ULONG_LE( X[12], data, 48 );
103195972f6Sopenharmony_ci    GET_ULONG_LE( X[13], data, 52 );
104195972f6Sopenharmony_ci    GET_ULONG_LE( X[14], data, 56 );
105195972f6Sopenharmony_ci    GET_ULONG_LE( X[15], data, 60 );
106195972f6Sopenharmony_ci
107195972f6Sopenharmony_ci#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
108195972f6Sopenharmony_ci
109195972f6Sopenharmony_ci    A = ctx->state[0];
110195972f6Sopenharmony_ci    B = ctx->state[1];
111195972f6Sopenharmony_ci    C = ctx->state[2];
112195972f6Sopenharmony_ci    D = ctx->state[3];
113195972f6Sopenharmony_ci
114195972f6Sopenharmony_ci#define F(x, y, z) ((x & y) | ((~x) & z))
115195972f6Sopenharmony_ci#define P(a,b,c,d,x,s) { a += F(b,c,d) + x; a = S(a,s); }
116195972f6Sopenharmony_ci
117195972f6Sopenharmony_ci    P( A, B, C, D, X[ 0],  3 );
118195972f6Sopenharmony_ci    P( D, A, B, C, X[ 1],  7 );
119195972f6Sopenharmony_ci    P( C, D, A, B, X[ 2], 11 );
120195972f6Sopenharmony_ci    P( B, C, D, A, X[ 3], 19 );
121195972f6Sopenharmony_ci    P( A, B, C, D, X[ 4],  3 );
122195972f6Sopenharmony_ci    P( D, A, B, C, X[ 5],  7 );
123195972f6Sopenharmony_ci    P( C, D, A, B, X[ 6], 11 );
124195972f6Sopenharmony_ci    P( B, C, D, A, X[ 7], 19 );
125195972f6Sopenharmony_ci    P( A, B, C, D, X[ 8],  3 );
126195972f6Sopenharmony_ci    P( D, A, B, C, X[ 9],  7 );
127195972f6Sopenharmony_ci    P( C, D, A, B, X[10], 11 );
128195972f6Sopenharmony_ci    P( B, C, D, A, X[11], 19 );
129195972f6Sopenharmony_ci    P( A, B, C, D, X[12],  3 );
130195972f6Sopenharmony_ci    P( D, A, B, C, X[13],  7 );
131195972f6Sopenharmony_ci    P( C, D, A, B, X[14], 11 );
132195972f6Sopenharmony_ci    P( B, C, D, A, X[15], 19 );
133195972f6Sopenharmony_ci
134195972f6Sopenharmony_ci#undef P
135195972f6Sopenharmony_ci#undef F
136195972f6Sopenharmony_ci
137195972f6Sopenharmony_ci#define F(x,y,z) ((x & y) | (x & z) | (y & z))
138195972f6Sopenharmony_ci#define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x5A827999; a = S(a,s); }
139195972f6Sopenharmony_ci
140195972f6Sopenharmony_ci    P( A, B, C, D, X[ 0],  3 );
141195972f6Sopenharmony_ci    P( D, A, B, C, X[ 4],  5 );
142195972f6Sopenharmony_ci    P( C, D, A, B, X[ 8],  9 );
143195972f6Sopenharmony_ci    P( B, C, D, A, X[12], 13 );
144195972f6Sopenharmony_ci    P( A, B, C, D, X[ 1],  3 );
145195972f6Sopenharmony_ci    P( D, A, B, C, X[ 5],  5 );
146195972f6Sopenharmony_ci    P( C, D, A, B, X[ 9],  9 );
147195972f6Sopenharmony_ci    P( B, C, D, A, X[13], 13 );
148195972f6Sopenharmony_ci    P( A, B, C, D, X[ 2],  3 );
149195972f6Sopenharmony_ci    P( D, A, B, C, X[ 6],  5 );
150195972f6Sopenharmony_ci    P( C, D, A, B, X[10],  9 );
151195972f6Sopenharmony_ci    P( B, C, D, A, X[14], 13 );
152195972f6Sopenharmony_ci    P( A, B, C, D, X[ 3],  3 );
153195972f6Sopenharmony_ci    P( D, A, B, C, X[ 7],  5 );
154195972f6Sopenharmony_ci    P( C, D, A, B, X[11],  9 );
155195972f6Sopenharmony_ci    P( B, C, D, A, X[15], 13 );
156195972f6Sopenharmony_ci
157195972f6Sopenharmony_ci#undef P
158195972f6Sopenharmony_ci#undef F
159195972f6Sopenharmony_ci
160195972f6Sopenharmony_ci#define F(x,y,z) (x ^ y ^ z)
161195972f6Sopenharmony_ci#define P(a,b,c,d,x,s) { a += F(b,c,d) + x + 0x6ED9EBA1; a = S(a,s); }
162195972f6Sopenharmony_ci
163195972f6Sopenharmony_ci    P( A, B, C, D, X[ 0],  3 );
164195972f6Sopenharmony_ci    P( D, A, B, C, X[ 8],  9 );
165195972f6Sopenharmony_ci    P( C, D, A, B, X[ 4], 11 );
166195972f6Sopenharmony_ci    P( B, C, D, A, X[12], 15 );
167195972f6Sopenharmony_ci    P( A, B, C, D, X[ 2],  3 );
168195972f6Sopenharmony_ci    P( D, A, B, C, X[10],  9 );
169195972f6Sopenharmony_ci    P( C, D, A, B, X[ 6], 11 );
170195972f6Sopenharmony_ci    P( B, C, D, A, X[14], 15 );
171195972f6Sopenharmony_ci    P( A, B, C, D, X[ 1],  3 );
172195972f6Sopenharmony_ci    P( D, A, B, C, X[ 9],  9 );
173195972f6Sopenharmony_ci    P( C, D, A, B, X[ 5], 11 );
174195972f6Sopenharmony_ci    P( B, C, D, A, X[13], 15 );
175195972f6Sopenharmony_ci    P( A, B, C, D, X[ 3],  3 );
176195972f6Sopenharmony_ci    P( D, A, B, C, X[11],  9 );
177195972f6Sopenharmony_ci    P( C, D, A, B, X[ 7], 11 );
178195972f6Sopenharmony_ci    P( B, C, D, A, X[15], 15 );
179195972f6Sopenharmony_ci
180195972f6Sopenharmony_ci#undef F
181195972f6Sopenharmony_ci#undef P
182195972f6Sopenharmony_ci
183195972f6Sopenharmony_ci    ctx->state[0] += A;
184195972f6Sopenharmony_ci    ctx->state[1] += B;
185195972f6Sopenharmony_ci    ctx->state[2] += C;
186195972f6Sopenharmony_ci    ctx->state[3] += D;
187195972f6Sopenharmony_ci}
188195972f6Sopenharmony_ci
189195972f6Sopenharmony_ci/*
190195972f6Sopenharmony_ci * MD4 process buffer
191195972f6Sopenharmony_ci */
192195972f6Sopenharmony_civoid md4_update( md4_context *ctx, const unsigned char *input, int ilen )
193195972f6Sopenharmony_ci{
194195972f6Sopenharmony_ci    int fill;
195195972f6Sopenharmony_ci    unsigned long left;
196195972f6Sopenharmony_ci
197195972f6Sopenharmony_ci    if( ilen <= 0 )
198195972f6Sopenharmony_ci        return;
199195972f6Sopenharmony_ci
200195972f6Sopenharmony_ci    left = ctx->total[0] & 0x3F;
201195972f6Sopenharmony_ci    fill = 64 - left;
202195972f6Sopenharmony_ci
203195972f6Sopenharmony_ci    ctx->total[0] += ilen;
204195972f6Sopenharmony_ci    ctx->total[0] &= 0xFFFFFFFF;
205195972f6Sopenharmony_ci
206195972f6Sopenharmony_ci    if( ctx->total[0] < (unsigned long) ilen )
207195972f6Sopenharmony_ci        ctx->total[1]++;
208195972f6Sopenharmony_ci
209195972f6Sopenharmony_ci    if( left && ilen >= fill )
210195972f6Sopenharmony_ci    {
211195972f6Sopenharmony_ci        MEMCPY( (void *) (ctx->buffer + left),
212195972f6Sopenharmony_ci                input, fill );
213195972f6Sopenharmony_ci        md4_process( ctx, ctx->buffer );
214195972f6Sopenharmony_ci        input += fill;
215195972f6Sopenharmony_ci        ilen  -= fill;
216195972f6Sopenharmony_ci        left = 0;
217195972f6Sopenharmony_ci    }
218195972f6Sopenharmony_ci
219195972f6Sopenharmony_ci    while( ilen >= 64 )
220195972f6Sopenharmony_ci    {
221195972f6Sopenharmony_ci        md4_process( ctx, input );
222195972f6Sopenharmony_ci        input += 64;
223195972f6Sopenharmony_ci        ilen  -= 64;
224195972f6Sopenharmony_ci    }
225195972f6Sopenharmony_ci
226195972f6Sopenharmony_ci    if( ilen > 0 )
227195972f6Sopenharmony_ci    {
228195972f6Sopenharmony_ci        MEMCPY( (void *) (ctx->buffer + left),
229195972f6Sopenharmony_ci                input, ilen );
230195972f6Sopenharmony_ci    }
231195972f6Sopenharmony_ci}
232195972f6Sopenharmony_ci
233195972f6Sopenharmony_cistatic const unsigned char md4_padding[64] =
234195972f6Sopenharmony_ci{
235195972f6Sopenharmony_ci 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
236195972f6Sopenharmony_ci    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
237195972f6Sopenharmony_ci    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
238195972f6Sopenharmony_ci    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
239195972f6Sopenharmony_ci};
240195972f6Sopenharmony_ci
241195972f6Sopenharmony_ci/*
242195972f6Sopenharmony_ci * MD4 final digest
243195972f6Sopenharmony_ci */
244195972f6Sopenharmony_civoid md4_finish( md4_context *ctx, unsigned char output[16] )
245195972f6Sopenharmony_ci{
246195972f6Sopenharmony_ci    unsigned long last, padn;
247195972f6Sopenharmony_ci    unsigned long high, low;
248195972f6Sopenharmony_ci    unsigned char msglen[8];
249195972f6Sopenharmony_ci
250195972f6Sopenharmony_ci    high = ( ctx->total[0] >> 29 )
251195972f6Sopenharmony_ci         | ( ctx->total[1] <<  3 );
252195972f6Sopenharmony_ci    low  = ( ctx->total[0] <<  3 );
253195972f6Sopenharmony_ci
254195972f6Sopenharmony_ci    PUT_ULONG_LE( low,  msglen, 0 );
255195972f6Sopenharmony_ci    PUT_ULONG_LE( high, msglen, 4 );
256195972f6Sopenharmony_ci
257195972f6Sopenharmony_ci    last = ctx->total[0] & 0x3F;
258195972f6Sopenharmony_ci    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
259195972f6Sopenharmony_ci
260195972f6Sopenharmony_ci    md4_update( ctx, md4_padding, padn );
261195972f6Sopenharmony_ci    md4_update( ctx, msglen, 8 );
262195972f6Sopenharmony_ci
263195972f6Sopenharmony_ci    PUT_ULONG_LE( ctx->state[0], output,  0 );
264195972f6Sopenharmony_ci    PUT_ULONG_LE( ctx->state[1], output,  4 );
265195972f6Sopenharmony_ci    PUT_ULONG_LE( ctx->state[2], output,  8 );
266195972f6Sopenharmony_ci    PUT_ULONG_LE( ctx->state[3], output, 12 );
267195972f6Sopenharmony_ci}
268195972f6Sopenharmony_ci
269195972f6Sopenharmony_ci/*
270195972f6Sopenharmony_ci * output = MD4( input buffer )
271195972f6Sopenharmony_ci */
272195972f6Sopenharmony_civoid md4( unsigned char *input, int ilen, unsigned char output[16] )
273195972f6Sopenharmony_ci{
274195972f6Sopenharmony_ci    md4_context ctx;
275195972f6Sopenharmony_ci
276195972f6Sopenharmony_ci    md4_starts( &ctx );
277195972f6Sopenharmony_ci    md4_update( &ctx, input, ilen );
278195972f6Sopenharmony_ci    md4_finish( &ctx, output );
279195972f6Sopenharmony_ci}
280195972f6Sopenharmony_ci
281195972f6Sopenharmony_ci#endif /* PPP_SUPPORT && LWIP_INCLUDED_POLARSSL_MD4 */
282