~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-security

« back to all changes in this revision

Viewing changes to cipher/md5.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* md5.c - MD5 Message-Digest Algorithm
2
 
 * Copyright (C) 1995, 1996, 1998, 1999,
3
 
 *               2000, 2001 Free Software Foundation, Inc.
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify it
6
 
 * under the terms of the GNU General Public License as published by the
7
 
 * Free Software Foundation; either version 2, or (at your option) any
8
 
 * later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software Foundation,
17
 
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 
 *
19
 
 * According to the definition of MD5 in RFC 1321 from April 1992.
20
 
 * NOTE: This is *not* the same file as the one from glibc.
21
 
 */
22
 
/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
23
 
/* Heavily modified for GnuPG by <wk@gnupg.org> */
24
 
 
25
 
/* Test values:
26
 
 * ""                  D4 1D 8C D9 8F 00 B2 04  E9 80 09 98 EC F8 42 7E
27
 
 * "a"                 0C C1 75 B9 C0 F1 B6 A8  31 C3 99 E2 69 77 26 61
28
 
 * "abc                90 01 50 98 3C D2 4F B0  D6 96 3F 7D 28 E1 7F 72
29
 
 * "message digest"    F9 6B 69 7D 7C B7 93 8D  52 5A 2F 31 AA F1 61 D0
30
 
 */
31
 
 
32
 
#include <config.h>
33
 
#include <stdio.h>
34
 
#include <stdlib.h>
35
 
#include <string.h>
36
 
#include <assert.h>
37
 
#include "util.h"
38
 
#include "memory.h"
39
 
#include "algorithms.h"
40
 
 
41
 
#include "bithelp.h"
42
 
 
43
 
 
44
 
typedef struct {
45
 
    u32 A,B,C,D;          /* chaining variables */
46
 
    u32  nblocks;
47
 
    byte buf[64];
48
 
    int  count;
49
 
} MD5_CONTEXT;
50
 
 
51
 
 
52
 
static void
53
 
md5_init( MD5_CONTEXT *ctx )
54
 
{
55
 
    ctx->A = 0x67452301;
56
 
    ctx->B = 0xefcdab89;
57
 
    ctx->C = 0x98badcfe;
58
 
    ctx->D = 0x10325476;
59
 
 
60
 
    ctx->nblocks = 0;
61
 
    ctx->count = 0;
62
 
}
63
 
 
64
 
 
65
 
 
66
 
 
67
 
/* These are the four functions used in the four steps of the MD5 algorithm
68
 
   and defined in the RFC 1321.  The first function is a little bit optimized
69
 
   (as found in Colin Plumbs public domain implementation).  */
70
 
/* #define FF(b, c, d) ((b & c) | (~b & d)) */
71
 
#define FF(b, c, d) (d ^ (b & (c ^ d)))
72
 
#define FG(b, c, d) FF (d, b, c)
73
 
#define FH(b, c, d) (b ^ c ^ d)
74
 
#define FI(b, c, d) (c ^ (b | ~d))
75
 
 
76
 
static void
77
 
burn_stack (int bytes)
78
 
{
79
 
    char buf[128];
80
 
    
81
 
    memset (buf, 0, sizeof buf);
82
 
    bytes -= sizeof buf;
83
 
    if (bytes > 0)
84
 
        burn_stack (bytes);
85
 
}
86
 
 
87
 
 
88
 
 
89
 
/****************
90
 
 * transform n*64 bytes
91
 
 */
92
 
static void
93
 
/*transform( MD5_CONTEXT *ctx, const void *buffer, size_t len )*/
94
 
transform( MD5_CONTEXT *ctx, byte *data )
95
 
{
96
 
    u32 correct_words[16];
97
 
    u32 A = ctx->A;
98
 
    u32 B = ctx->B;
99
 
    u32 C = ctx->C;
100
 
    u32 D = ctx->D;
101
 
    u32 *cwp = correct_words;
102
 
 
103
 
  #ifdef BIG_ENDIAN_HOST
104
 
    { int i;
105
 
      byte *p2, *p1;
106
 
      for(i=0, p1=data, p2=(byte*)correct_words; i < 16; i++, p2 += 4 ) {
107
 
        p2[3] = *p1++;
108
 
        p2[2] = *p1++;
109
 
        p2[1] = *p1++;
110
 
        p2[0] = *p1++;
111
 
      }
112
 
    }
113
 
  #else
114
 
    memcpy( correct_words, data, 64 );
115
 
  #endif
116
 
 
117
 
 
118
 
#define OP(a, b, c, d, s, T)                                        \
119
 
  do                                                                \
120
 
    {                                                               \
121
 
      a += FF (b, c, d) + (*cwp++) + T;             \
122
 
      a = rol(a, s);                                                \
123
 
      a += b;                                                       \
124
 
    }                                                               \
125
 
  while (0)
126
 
 
127
 
    /* Before we start, one word about the strange constants.
128
 
       They are defined in RFC 1321 as
129
 
 
130
 
       T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
131
 
     */
132
 
 
133
 
    /* Round 1.  */
134
 
    OP (A, B, C, D,  7, 0xd76aa478);
135
 
    OP (D, A, B, C, 12, 0xe8c7b756);
136
 
    OP (C, D, A, B, 17, 0x242070db);
137
 
    OP (B, C, D, A, 22, 0xc1bdceee);
138
 
    OP (A, B, C, D,  7, 0xf57c0faf);
139
 
    OP (D, A, B, C, 12, 0x4787c62a);
140
 
    OP (C, D, A, B, 17, 0xa8304613);
141
 
    OP (B, C, D, A, 22, 0xfd469501);
142
 
    OP (A, B, C, D,  7, 0x698098d8);
143
 
    OP (D, A, B, C, 12, 0x8b44f7af);
144
 
    OP (C, D, A, B, 17, 0xffff5bb1);
145
 
    OP (B, C, D, A, 22, 0x895cd7be);
146
 
    OP (A, B, C, D,  7, 0x6b901122);
147
 
    OP (D, A, B, C, 12, 0xfd987193);
148
 
    OP (C, D, A, B, 17, 0xa679438e);
149
 
    OP (B, C, D, A, 22, 0x49b40821);
150
 
 
151
 
#undef OP
152
 
#define OP(f, a, b, c, d, k, s, T)  \
153
 
    do                                                                \
154
 
      {                                                               \
155
 
        a += f (b, c, d) + correct_words[k] + T;                      \
156
 
        a = rol(a, s);                                                \
157
 
        a += b;                                                       \
158
 
      }                                                               \
159
 
    while (0)
160
 
 
161
 
    /* Round 2.  */
162
 
    OP (FG, A, B, C, D,  1,  5, 0xf61e2562);
163
 
    OP (FG, D, A, B, C,  6,  9, 0xc040b340);
164
 
    OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
165
 
    OP (FG, B, C, D, A,  0, 20, 0xe9b6c7aa);
166
 
    OP (FG, A, B, C, D,  5,  5, 0xd62f105d);
167
 
    OP (FG, D, A, B, C, 10,  9, 0x02441453);
168
 
    OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
169
 
    OP (FG, B, C, D, A,  4, 20, 0xe7d3fbc8);
170
 
    OP (FG, A, B, C, D,  9,  5, 0x21e1cde6);
171
 
    OP (FG, D, A, B, C, 14,  9, 0xc33707d6);
172
 
    OP (FG, C, D, A, B,  3, 14, 0xf4d50d87);
173
 
    OP (FG, B, C, D, A,  8, 20, 0x455a14ed);
174
 
    OP (FG, A, B, C, D, 13,  5, 0xa9e3e905);
175
 
    OP (FG, D, A, B, C,  2,  9, 0xfcefa3f8);
176
 
    OP (FG, C, D, A, B,  7, 14, 0x676f02d9);
177
 
    OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
178
 
 
179
 
    /* Round 3.  */
180
 
    OP (FH, A, B, C, D,  5,  4, 0xfffa3942);
181
 
    OP (FH, D, A, B, C,  8, 11, 0x8771f681);
182
 
    OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
183
 
    OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
184
 
    OP (FH, A, B, C, D,  1,  4, 0xa4beea44);
185
 
    OP (FH, D, A, B, C,  4, 11, 0x4bdecfa9);
186
 
    OP (FH, C, D, A, B,  7, 16, 0xf6bb4b60);
187
 
    OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
188
 
    OP (FH, A, B, C, D, 13,  4, 0x289b7ec6);
189
 
    OP (FH, D, A, B, C,  0, 11, 0xeaa127fa);
190
 
    OP (FH, C, D, A, B,  3, 16, 0xd4ef3085);
191
 
    OP (FH, B, C, D, A,  6, 23, 0x04881d05);
192
 
    OP (FH, A, B, C, D,  9,  4, 0xd9d4d039);
193
 
    OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
194
 
    OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
195
 
    OP (FH, B, C, D, A,  2, 23, 0xc4ac5665);
196
 
 
197
 
    /* Round 4.  */
198
 
    OP (FI, A, B, C, D,  0,  6, 0xf4292244);
199
 
    OP (FI, D, A, B, C,  7, 10, 0x432aff97);
200
 
    OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
201
 
    OP (FI, B, C, D, A,  5, 21, 0xfc93a039);
202
 
    OP (FI, A, B, C, D, 12,  6, 0x655b59c3);
203
 
    OP (FI, D, A, B, C,  3, 10, 0x8f0ccc92);
204
 
    OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
205
 
    OP (FI, B, C, D, A,  1, 21, 0x85845dd1);
206
 
    OP (FI, A, B, C, D,  8,  6, 0x6fa87e4f);
207
 
    OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
208
 
    OP (FI, C, D, A, B,  6, 15, 0xa3014314);
209
 
    OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
210
 
    OP (FI, A, B, C, D,  4,  6, 0xf7537e82);
211
 
    OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
212
 
    OP (FI, C, D, A, B,  2, 15, 0x2ad7d2bb);
213
 
    OP (FI, B, C, D, A,  9, 21, 0xeb86d391);
214
 
 
215
 
    /* Put checksum in context given as argument.  */
216
 
    ctx->A += A;
217
 
    ctx->B += B;
218
 
    ctx->C += C;
219
 
    ctx->D += D;
220
 
}
221
 
 
222
 
 
223
 
 
224
 
/* The routine updates the message-digest context to
225
 
 * account for the presence of each of the characters inBuf[0..inLen-1]
226
 
 * in the message whose digest is being computed.
227
 
 */
228
 
static void
229
 
md5_write( MD5_CONTEXT *hd, byte *inbuf, size_t inlen)
230
 
{
231
 
    if( hd->count == 64 ) { /* flush the buffer */
232
 
        transform( hd, hd->buf );
233
 
        burn_stack (80+6*sizeof(void*));
234
 
        hd->count = 0;
235
 
        hd->nblocks++;
236
 
    }
237
 
    if( !inbuf )
238
 
        return;
239
 
    if( hd->count ) {
240
 
        for( ; inlen && hd->count < 64; inlen-- )
241
 
            hd->buf[hd->count++] = *inbuf++;
242
 
        md5_write( hd, NULL, 0 );
243
 
        if( !inlen )
244
 
            return;
245
 
    }
246
 
 
247
 
    while( inlen >= 64 ) {
248
 
        transform( hd, inbuf );
249
 
        hd->count = 0;
250
 
        hd->nblocks++;
251
 
        inlen -= 64;
252
 
        inbuf += 64;
253
 
    }
254
 
    burn_stack (80+6*sizeof(void*));
255
 
    for( ; inlen && hd->count < 64; inlen-- )
256
 
        hd->buf[hd->count++] = *inbuf++;
257
 
}
258
 
 
259
 
 
260
 
 
261
 
/* The routine final terminates the message-digest computation and
262
 
 * ends with the desired message digest in mdContext->digest[0...15].
263
 
 * The handle is prepared for a new MD5 cycle.
264
 
 * Returns 16 bytes representing the digest.
265
 
 */
266
 
 
267
 
static void
268
 
md5_final( MD5_CONTEXT *hd )
269
 
{
270
 
    u32 t, msb, lsb;
271
 
    byte *p;
272
 
 
273
 
    md5_write(hd, NULL, 0); /* flush */;
274
 
 
275
 
    t = hd->nblocks;
276
 
    /* multiply by 64 to make a byte count */
277
 
    lsb = t << 6;
278
 
    msb = t >> 26;
279
 
    /* add the count */
280
 
    t = lsb;
281
 
    if( (lsb += hd->count) < t )
282
 
        msb++;
283
 
    /* multiply by 8 to make a bit count */
284
 
    t = lsb;
285
 
    lsb <<= 3;
286
 
    msb <<= 3;
287
 
    msb |= t >> 29;
288
 
 
289
 
    if( hd->count < 56 ) { /* enough room */
290
 
        hd->buf[hd->count++] = 0x80; /* pad */
291
 
        while( hd->count < 56 )
292
 
            hd->buf[hd->count++] = 0;  /* pad */
293
 
    }
294
 
    else { /* need one extra block */
295
 
        hd->buf[hd->count++] = 0x80; /* pad character */
296
 
        while( hd->count < 64 )
297
 
            hd->buf[hd->count++] = 0;
298
 
        md5_write(hd, NULL, 0);  /* flush */;
299
 
        memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
300
 
    }
301
 
    /* append the 64 bit count */
302
 
    hd->buf[56] = lsb      ;
303
 
    hd->buf[57] = lsb >>  8;
304
 
    hd->buf[58] = lsb >> 16;
305
 
    hd->buf[59] = lsb >> 24;
306
 
    hd->buf[60] = msb      ;
307
 
    hd->buf[61] = msb >>  8;
308
 
    hd->buf[62] = msb >> 16;
309
 
    hd->buf[63] = msb >> 24;
310
 
    transform( hd, hd->buf );
311
 
    burn_stack (80+6*sizeof(void*));
312
 
 
313
 
    p = hd->buf;
314
 
  #ifdef BIG_ENDIAN_HOST
315
 
    #define X(a) do { *p++ = hd-> a      ; *p++ = hd-> a >> 8;      \
316
 
                      *p++ = hd-> a >> 16; *p++ = hd-> a >> 24; } while(0)
317
 
  #else /* little endian */
318
 
    #define X(a) do { *(u32*)p = hd-> a ; p += 4; } while(0)
319
 
  #endif
320
 
    X(A);
321
 
    X(B);
322
 
    X(C);
323
 
    X(D);
324
 
  #undef X
325
 
 
326
 
}
327
 
 
328
 
static byte *
329
 
md5_read( MD5_CONTEXT *hd )
330
 
{
331
 
    return hd->buf;
332
 
}
333
 
 
334
 
/****************
335
 
 * Return some information about the algorithm.  We need algo here to
336
 
 * distinguish different flavors of the algorithm.
337
 
 * Returns: A pointer to string describing the algorithm or NULL if
338
 
 *          the ALGO is invalid.
339
 
 */
340
 
const char *
341
 
md5_get_info( int algo, size_t *contextsize,
342
 
               byte **r_asnoid, int *r_asnlen, int *r_mdlen,
343
 
               void (**r_init)( void *c ),
344
 
               void (**r_write)( void *c, byte *buf, size_t nbytes ),
345
 
               void (**r_final)( void *c ),
346
 
               byte *(**r_read)( void *c )
347
 
             )
348
 
{
349
 
    static byte asn[18] = /* Object ID is 1.2.840.113549.2.5 */
350
 
                    { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
351
 
                      0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
352
 
 
353
 
    if( algo != 1 )
354
 
        return NULL;
355
 
 
356
 
    *contextsize = sizeof(MD5_CONTEXT);
357
 
    *r_asnoid = asn;
358
 
    *r_asnlen = DIM(asn);
359
 
    *r_mdlen = 16;
360
 
    *(void  (**)(MD5_CONTEXT *))r_init                 = md5_init;
361
 
    *(void  (**)(MD5_CONTEXT *, byte*, size_t))r_write = md5_write;
362
 
    *(void  (**)(MD5_CONTEXT *))r_final                = md5_final;
363
 
    *(byte *(**)(MD5_CONTEXT *))r_read                 = md5_read;
364
 
 
365
 
    return "MD5";
366
 
}
367