summaryrefslogtreecommitdiff
path: root/lib/libalpm/md5.c
blob: 8d6fe049fea7a2f65a56bcb927606d02d5b3dd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 *  RFC 1321 compliant MD5 implementation
 *
 *  Copyright (C) 2006-2007  Christophe Devine
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License, version 2.1 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */
/*
 *  The MD5 algorithm was designed by Ron Rivest in 1991.
 *
 *  http://www.ietf.org/rfc/rfc1321.txt
 */
/*
 *  Pacman Notes:
 *
 *  Taken from the XySSL project at www.xyssl.org under terms of the
 *  LGPL. This is from version 0.7 of the library, and has been modified
 *  as following, which may be helpful for future updates:
 *  * change include from "xyssl/md5.h" to "md5.h"
 *  * removal of HMAC code
 *  * removal of SELF_TEST code
 *  * removal of ipad and opad from the md5_context struct in md5.h
 *  * change of md5_file prototype from
 *        int md5_file( char *path, unsigned char *output )
 *      to
 *        int md5_file( const char *path, unsigned char *output )
 */

#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif

#include <string.h>
#include <stdio.h>

#include "md5.h"

/*
 * 32-bit integer manipulation macros (little endian)
 */
#ifndef GET_UINT32_LE
#define GET_UINT32_LE(n,b,i)                            \
{                                                       \
    (n) = ( (unsigned long) (b)[(i)    ]       )        \
        | ( (unsigned long) (b)[(i) + 1] <<  8 )        \
        | ( (unsigned long) (b)[(i) + 2] << 16 )        \
        | ( (unsigned long) (b)[(i) + 3] << 24 );       \
}
#endif

#ifndef PUT_UINT32_LE
#define PUT_UINT32_LE(n,b,i)                            \
{                                                       \
    (b)[(i)    ] = (unsigned char) ( (n)       );       \
    (b)[(i) + 1] = (unsigned char) ( (n) >>  8 );       \
    (b)[(i) + 2] = (unsigned char) ( (n) >> 16 );       \
    (b)[(i) + 3] = (unsigned char) ( (n) >> 24 );       \
}
#endif

/*
 * MD5 context setup
 */
void md5_starts( md5_context *ctx )
{
    ctx->total[0] = 0;
    ctx->total[1] = 0;

    ctx->state[0] = 0x67452301;
    ctx->state[1] = 0xEFCDAB89;
    ctx->state[2] = 0x98BADCFE;
    ctx->state[3] = 0x10325476;
}

static void md5_process( md5_context *ctx, unsigned char data[64] )
{
    unsigned long X[16], A, B, C, D;

    GET_UINT32_LE( X[ 0], data,  0 );
    GET_UINT32_LE( X[ 1], data,  4 );
    GET_UINT32_LE( X[ 2], data,  8 );
    GET_UINT32_LE( X[ 3], data, 12 );
    GET_UINT32_LE( X[ 4], data, 16 );
    GET_UINT32_LE( X[ 5], data, 20 );
    GET_UINT32_LE( X[ 6], data, 24 );
    GET_UINT32_LE( X[ 7], data, 28 );
    GET_UINT32_LE( X[ 8], data, 32 );
    GET_UINT32_LE( X[ 9], data, 36 );
    GET_UINT32_LE( X[10], data, 40 );
    GET_UINT32_LE( X[11], data, 44 );
    GET_UINT32_LE( X[12], data, 48 );
    GET_UINT32_LE( X[13], data, 52 );
    GET_UINT32_LE( X[14], data, 56 );
    GET_UINT32_LE( X[15], data, 60 );

#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))

#define P(a,b,c,d,k,s,t)                                \
{                                                       \
    a += F(b,c,d) + X[k] + t; a = S(a,s) + b;           \
}

    A = ctx->state[0];
    B = ctx->state[1];
    C = ctx->state[2];
    D = ctx->state[3];

#define F(x,y,z) (z ^ (x & (y ^ z)))

    P( A, B, C, D,  0,  70xD76AA478 );
    P( D, A, B, C,  1120xE8C7B756 );
    P( C, D, A, B,  2170x242070DB );
    P( B, C, D, A,  3220xC1BDCEEE );
    P( A, B, C, D,  4,  70xF57C0FAF );
    P( D, A, B, C,  5120x4787C62A );
    P( C, D, A, B,  6170xA8304613 );
    P( B, C, D, A,  7220xFD469501 );
    P( A, B, C, D,  8,  70x698098D8 );
    P( D, A, B, C,  9120x8B44F7AF );
    P( C, D, A, B, 10170xFFFF5BB1 );
    P( B, C, D, A, 11220x895CD7BE );
    P( A, B, C, D, 12,  70x6B901122 );
    P( D, A, B, C, 13120xFD987193 );
    P( C, D, A, B, 14170xA679438E );
    P( B, C, D, A, 15220x49B40821 );

#undef F

#define F(x,y,z) (y ^ (z & (x ^ y)))

    P( A, B, C, D,  1,  50xF61E2562 );
    P( D, A, B, C,  6,  90xC040B340 );
    P( C, D, A, B, 11140x265E5A51 );
    P( B, C, D, A,  0200xE9B6C7AA );
    P( A, B, C, D,  5,  50xD62F105D );
    P( D, A, B, C, 10,  90x02441453 );
    P( C, D, A, B, 15140xD8A1E681 );
    P( B, C, D, A,  4200xE7D3FBC8 );
    P( A, B, C, D,  9,  50x21E1CDE6 );
    P( D, A, B, C, 14,  90xC33707D6 );
    P( C, D, A, B,  3140xF4D50D87 );
    P( B, C, D, A,  8200x455A14ED );
    P( A, B, C, D, 13,  50xA9E3E905 );
    P( D, A, B, C,  2,  90xFCEFA3F8 );
    P( C, D, A, B,  7140x676F02D9 );
    P( B, C, D, A, 12200x8D2A4C8A );

#undef F

#define F(x,y,z) (x ^ y ^ z)

    P( A, B, C, D,  5,  40xFFFA3942 );
    P( D, A, B, C,  8110x8771F681 );
    P( C, D, A, B, 11160x6D9D6122 );
    P( B, C, D, A, 14230xFDE5380C );
    P( A, B, C, D,  1,  40xA4BEEA44 );
    P( D, A, B, C,  4110x4BDECFA9 );
    P( C, D, A, B,  7160xF6BB4B60 );
    P( B, C, D, A, 10230xBEBFBC70 );
    P( A, B, C, D, 13,  40x289B7EC6 );
    P( D, A, B, C,  0110xEAA127FA );
    P( C, D, A, B,  3160xD4EF3085 );
    P( B, C, D, A,  6230x04881D05 );
    P( A, B, C, D,  9,  40xD9D4D039 );
    P( D, A, B, C, 12110xE6DB99E5 );
    P( C, D, A, B, 15160x1FA27CF8 );
    P( B, C, D, A,  2230xC4AC5665 );

#undef F

#define F(x,y,z) (y ^ (x | ~z))

    P( A, B, C, D,  0,  60xF4292244 );
    P( D, A, B, C,  7100x432AFF97 );
    P( C, D, A, B, 14150xAB9423A7 );
    P( B, C, D, A,  5210xFC93A039 );
    P( A, B, C, D, 12,  60x655B59C3 );
    P( D, A, B, C,  3100x8F0CCC92 );
    P( C, D, A, B, 10150xFFEFF47D );
    P( B, C, D, A,  1210x85845DD1 );
    P( A, B, C, D,  8,  60x6FA87E4F );
    P( D, A, B, C, 15100xFE2CE6E0 );
    P( C, D, A, B,  6150xA3014314 );
    P( B, C, D, A, 13210x4E0811A1 );
    P( A, B, C, D,  4,  60xF7537E82 );
    P( D, A, B, C, 11100xBD3AF235 );
    P( C, D, A, B,  2150x2AD7D2BB );
    P( B, C, D, A,  9210xEB86D391 );

#undef F

    ctx->state[0] += A;
    ctx->state[1] += B;
    ctx->state[2] += C;
    ctx->state[3] += D;
}

/*
 * MD5 process buffer
 */
void md5_update( md5_context *ctx, unsigned char *input, int ilen )
{
    int fill;
    unsigned long left;

    if( ilen <= 0 )
        return;

    left = ctx->total[0] & 0x3F;
    fill = 64 - left;

    ctx->total[0] += ilen;
    ctx->total[0] &= 0xFFFFFFFF;

    if( ctx->total[0] < (unsigned long) ilen )
        ctx->total[1]++;

    if( left && ilen >= fill )
    {
        memcpy( (void *) (ctx->buffer + left),
                (void *) input, fill );
        md5_process( ctx, ctx->buffer );
        input += fill;
        ilen  -= fill;
        left = 0;
    }

    while( ilen >= 64 )
    {
        md5_process( ctx, input );
        input += 64;
        ilen  -= 64;
    }

    if( ilen > 0 )
    {
        memcpy( (void *) (ctx->buffer + left),
                (void *) input, ilen );
    }
}

static const unsigned char md5_padding[64] =
{
 0x80000000000000000,
    0000000000000000,
    0000000000000000,
    0000000000000000
};

/*
 * MD5 final digest
 */
void md5_finish( md5_context *ctx, unsigned char *output )
{
    unsigned long last, padn;
    unsigned long high, low;
    unsigned char msglen[8];

    high = ( ctx->total[0] >> 29 )
         | ( ctx->total[1] <<  3 );
    low  = ( ctx->total[0] <<  3 );

    PUT_UINT32_LE( low,  msglen, 0 );
    PUT_UINT32_LE( high, msglen, 4 );

    last = ctx->total[0] & 0x3F;
    padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );

    md5_update( ctx, (unsigned char *) md5_padding, padn );
    md5_update( ctx, msglen, 8 );

    PUT_UINT32_LE( ctx->state[0], output,  0 );
    PUT_UINT32_LE( ctx->state[1], output,  4 );
    PUT_UINT32_LE( ctx->state[2], output,  8 );
    PUT_UINT32_LE( ctx->state[3], output, 12 );
}

/*
 * Output = MD5( input buffer )
 */
void md5( unsigned char *input, int ilen,
          unsigned char *output )
{
    md5_context ctx;

    md5_starts( &ctx );
    md5_update( &ctx, input, ilen );
    md5_finish( &ctx, output );

    memset( &ctx, 0sizeof( md5_context ) );
}

/*
 * Output = MD5( file contents )
 */
int md5_file( const char *path, unsigned char *output )
{
    FILE *f;
    size_t n;
    md5_context ctx;
    unsigned char buf[1024];

    if( ( f = fopen( path, "rb" ) ) == NULL )
        return1 );

    md5_starts( &ctx );

    while( ( n = fread( buf, 1sizeof( buf ), f ) ) > 0 )
        md5_update( &ctx, buf, (int) n );

    md5_finish( &ctx, output );

    memset( &ctx, 0sizeof( md5_context ) );

    if( ferror( f ) != 0 )
    {
        fclose( f );
        return2 );
    }

    fclose( f );
    return0 );
}