Coverage Report

Created: 2024-11-21 07:03

/src/libgcrypt/cipher/md4.c
Line
Count
Source (jump to first uncovered line)
1
/* md4.c - MD4 Message-Digest Algorithm
2
 * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3
 *
4
 * This file is part of Libgcrypt.
5
 *
6
 * Libgcrypt is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * Libgcrypt is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18
 * SPDX-License-Identifier: LGPL-2.1-or-later
19
 *
20
 * Based on md5.c in libgcrypt, but rewritten to compute md4 checksums
21
 * using a public domain md4 implementation with the following comments:
22
 *
23
 * Modified by Wei Dai from Andrew M. Kuchling's md4.c
24
 * The original code and all modifications are in the public domain.
25
 *
26
 * This is the original introductory comment:
27
 *
28
 *  md4.c : MD4 hash algorithm.
29
 *
30
 * Part of the Python Cryptography Toolkit, version 1.1
31
 *
32
 * Distribute and use freely; there are no restrictions on further
33
 * dissemination and usage except those imposed by the laws of your
34
 * country of residence.
35
 *
36
 */
37
38
/* MD4 test suite:
39
 * MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
40
 * MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
41
 * MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
42
 * MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
43
 * MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
44
 * MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
45
 * 043f8582f241db351ce627e153e7f0e4
46
 * MD4 ("123456789012345678901234567890123456789012345678901234567890123456
47
 * 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
48
 */
49
50
#include <config.h>
51
#include <stdio.h>
52
#include <stdlib.h>
53
#include <string.h>
54
55
#include "g10lib.h"
56
#include "cipher.h"
57
58
#include "bithelp.h"
59
#include "bufhelp.h"
60
#include "hash-common.h"
61
62
63
typedef struct {
64
    gcry_md_block_ctx_t bctx;
65
    u32 A,B,C,D;    /* chaining variables */
66
} MD4_CONTEXT;
67
68
static unsigned int
69
transform ( void *c, const unsigned char *data, size_t nblks );
70
71
static void
72
md4_init (void *context, unsigned int flags)
73
1.07k
{
74
1.07k
  MD4_CONTEXT *ctx = context;
75
76
1.07k
  (void)flags;
77
78
1.07k
  ctx->A = 0x67452301;
79
1.07k
  ctx->B = 0xefcdab89;
80
1.07k
  ctx->C = 0x98badcfe;
81
1.07k
  ctx->D = 0x10325476;
82
83
1.07k
  ctx->bctx.nblocks = 0;
84
1.07k
  ctx->bctx.nblocks_high = 0;
85
1.07k
  ctx->bctx.count = 0;
86
1.07k
  ctx->bctx.blocksize_shift = _gcry_ctz(64);
87
1.07k
  ctx->bctx.bwrite = transform;
88
1.07k
}
89
90
8.09M
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
91
8.09M
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
92
8.09M
#define H(x, y, z) ((x) ^ (y) ^ (z))
93
94
95
/****************
96
 * transform 64 bytes
97
 */
98
static unsigned int
99
transform_blk ( void *c, const unsigned char *data )
100
506k
{
101
506k
  MD4_CONTEXT *ctx = c;
102
506k
  u32 in[16];
103
506k
  register u32 A = ctx->A;
104
506k
  register u32 B = ctx->B;
105
506k
  register u32 C = ctx->C;
106
506k
  register u32 D = ctx->D;
107
506k
  int i;
108
109
8.60M
  for ( i = 0; i < 16; i++ )
110
8.09M
    in[i] = buf_get_le32(data + i * 4);
111
112
  /* Round 1.  */
113
8.09M
#define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+in[k],s);
114
506k
  function(A,B,C,D, 0, 3);
115
506k
  function(D,A,B,C, 1, 7);
116
506k
  function(C,D,A,B, 2,11);
117
506k
  function(B,C,D,A, 3,19);
118
506k
  function(A,B,C,D, 4, 3);
119
506k
  function(D,A,B,C, 5, 7);
120
506k
  function(C,D,A,B, 6,11);
121
506k
  function(B,C,D,A, 7,19);
122
506k
  function(A,B,C,D, 8, 3);
123
506k
  function(D,A,B,C, 9, 7);
124
506k
  function(C,D,A,B,10,11);
125
506k
  function(B,C,D,A,11,19);
126
506k
  function(A,B,C,D,12, 3);
127
506k
  function(D,A,B,C,13, 7);
128
506k
  function(C,D,A,B,14,11);
129
506k
  function(B,C,D,A,15,19);
130
131
506k
#undef function
132
133
  /* Round 2.  */
134
8.09M
#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
135
136
506k
  function(A,B,C,D, 0, 3);
137
506k
  function(D,A,B,C, 4, 5);
138
506k
  function(C,D,A,B, 8, 9);
139
506k
  function(B,C,D,A,12,13);
140
506k
  function(A,B,C,D, 1, 3);
141
506k
  function(D,A,B,C, 5, 5);
142
506k
  function(C,D,A,B, 9, 9);
143
506k
  function(B,C,D,A,13,13);
144
506k
  function(A,B,C,D, 2, 3);
145
506k
  function(D,A,B,C, 6, 5);
146
506k
  function(C,D,A,B,10, 9);
147
506k
  function(B,C,D,A,14,13);
148
506k
  function(A,B,C,D, 3, 3);
149
506k
  function(D,A,B,C, 7, 5);
150
506k
  function(C,D,A,B,11, 9);
151
506k
  function(B,C,D,A,15,13);
152
153
506k
#undef function
154
155
  /* Round 3.  */
156
8.09M
#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
157
158
506k
  function(A,B,C,D, 0, 3);
159
506k
  function(D,A,B,C, 8, 9);
160
506k
  function(C,D,A,B, 4,11);
161
506k
  function(B,C,D,A,12,15);
162
506k
  function(A,B,C,D, 2, 3);
163
506k
  function(D,A,B,C,10, 9);
164
506k
  function(C,D,A,B, 6,11);
165
506k
  function(B,C,D,A,14,15);
166
506k
  function(A,B,C,D, 1, 3);
167
506k
  function(D,A,B,C, 9, 9);
168
506k
  function(C,D,A,B, 5,11);
169
506k
  function(B,C,D,A,13,15);
170
506k
  function(A,B,C,D, 3, 3);
171
506k
  function(D,A,B,C,11, 9);
172
506k
  function(C,D,A,B, 7,11);
173
506k
  function(B,C,D,A,15,15);
174
175
176
  /* Put checksum in context given as argument.  */
177
506k
  ctx->A += A;
178
506k
  ctx->B += B;
179
506k
  ctx->C += C;
180
506k
  ctx->D += D;
181
182
506k
  return /*burn_stack*/ 80+6*sizeof(void*);
183
506k
}
184
185
186
static unsigned int
187
transform ( void *c, const unsigned char *data, size_t nblks )
188
14.2k
{
189
14.2k
  unsigned int burn;
190
191
14.2k
  do
192
506k
    {
193
506k
      burn = transform_blk (c, data);
194
506k
      data += 64;
195
506k
    }
196
506k
  while (--nblks);
197
198
14.2k
  return burn;
199
14.2k
}
200
201
202
/* The routine final terminates the message-digest computation and
203
 * ends with the desired message digest in mdContext->digest[0...15].
204
 * The handle is prepared for a new MD4 cycle.
205
 * Returns 16 bytes representing the digest.
206
 */
207
208
static void
209
md4_final( void *context )
210
8.75k
{
211
8.75k
  MD4_CONTEXT *hd = context;
212
8.75k
  u32 t, th, msb, lsb;
213
8.75k
  byte *p;
214
8.75k
  unsigned int burn;
215
216
8.75k
  t = hd->bctx.nblocks;
217
8.75k
  if (sizeof t == sizeof hd->bctx.nblocks)
218
0
    th = hd->bctx.nblocks_high;
219
8.75k
  else
220
8.75k
    th = hd->bctx.nblocks >> 32;
221
222
  /* multiply by 64 to make a byte count */
223
8.75k
  lsb = t << 6;
224
8.75k
  msb = (th << 6) | (t >> 26);
225
  /* add the count */
226
8.75k
  t = lsb;
227
8.75k
  if( (lsb += hd->bctx.count) < t )
228
0
    msb++;
229
  /* multiply by 8 to make a bit count */
230
8.75k
  t = lsb;
231
8.75k
  lsb <<= 3;
232
8.75k
  msb <<= 3;
233
8.75k
  msb |= t >> 29;
234
235
8.75k
  if (hd->bctx.count < 56)  /* enough room */
236
8.35k
    {
237
8.35k
      hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
238
8.35k
      if (hd->bctx.count < 56)
239
8.26k
  memset (&hd->bctx.buf[hd->bctx.count], 0, 56 - hd->bctx.count);
240
241
      /* append the 64 bit count */
242
8.35k
      buf_put_le32(hd->bctx.buf + 56, lsb);
243
8.35k
      buf_put_le32(hd->bctx.buf + 60, msb);
244
8.35k
      burn = transform (hd, hd->bctx.buf, 1);
245
8.35k
    }
246
395
  else /* need one extra block */
247
395
    {
248
395
      hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
249
      /* fill pad and next block with zeroes */
250
395
      memset (&hd->bctx.buf[hd->bctx.count], 0, 64 - hd->bctx.count + 56);
251
252
      /* append the 64 bit count */
253
395
      buf_put_le32(hd->bctx.buf + 64 + 56, lsb);
254
395
      buf_put_le32(hd->bctx.buf + 64 + 60, msb);
255
395
      burn = transform (hd, hd->bctx.buf, 2);
256
395
    }
257
258
8.75k
  p = hd->bctx.buf;
259
35.0k
#define X(a) do { buf_put_le32(p, hd->a); p += 4; } while(0)
260
8.75k
  X(A);
261
8.75k
  X(B);
262
8.75k
  X(C);
263
8.75k
  X(D);
264
8.75k
#undef X
265
266
8.75k
  hd->bctx.count = 0;
267
268
8.75k
  _gcry_burn_stack (burn);
269
8.75k
}
270
271
static byte *
272
md4_read (void *context)
273
8.75k
{
274
8.75k
  MD4_CONTEXT *hd = context;
275
8.75k
  return hd->bctx.buf;
276
8.75k
}
277
278
static const byte asn[18] = /* Object ID is 1.2.840.113549.2.4 */
279
  { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
280
    0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 };
281
282
static const gcry_md_oid_spec_t oid_spec_md4[] =
283
  {
284
    /* iso.member-body.us.rsadsi.digestAlgorithm.md4 */
285
    { "1.2.840.113549.2.4" },
286
    { NULL },
287
  };
288
289
const gcry_md_spec_t _gcry_digest_spec_md4 =
290
  {
291
    GCRY_MD_MD4, {0, 0},
292
    "MD4", asn, DIM (asn), oid_spec_md4,16,
293
    md4_init, _gcry_md_block_write, md4_final, md4_read, NULL,
294
    NULL,
295
    sizeof (MD4_CONTEXT)
296
  };