Coverage Report

Created: 2026-10-17 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/crypto/md5.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * This file was transplanted with slight modifications from Linux sources
4
 * (fs/cifs/md5.c) into U-Boot by Bartlomiej Sieka <tur@semihalf.com>.
5
 */
6
7
/*
8
 * This code implements the MD5 message-digest algorithm.
9
 * The algorithm is due to Ron Rivest.  This code was
10
 * written by Colin Plumb in 1993, no copyright is claimed.
11
 * This code is in the public domain; do with it what you wish.
12
 *
13
 * Equivalent code is available from RSA Data Security, Inc.
14
 * This code has been tested against that, and is equivalent,
15
 * except that you don't need to include two pages of legalese
16
 * with every copy.
17
 *
18
 * To compute the message digest of a chunk of bytes, declare an
19
 * MD5Context structure, pass it to MD5Init, call MD5Update as
20
 * needed on buffers full of bytes, and then call MD5Final, which
21
 * will fill a supplied 16-byte array with the digest.
22
 */
23
24
/* This code slightly modified to fit into Samba by
25
   abartlet@samba.org Jun 2001
26
   and to fit the cifs vfs by
27
   Steve French sfrench@us.ibm.com */
28
29
#include <common.h>
30
#include <digest.h>
31
#include <init.h>
32
#include <crypto/internal.h>
33
34
struct MD5Context {
35
  __u32 buf[4];
36
  __u32 bits[2];
37
  unsigned char in[64];
38
};
39
40
static void
41
MD5Transform(__u32 buf[4], __u32 const in[16]);
42
43
/*
44
 * Note: this code is harmless on little-endian machines.
45
 */
46
static void
47
byteReverse(unsigned char *buf, unsigned longs)
48
8
{
49
8
  __u32 t;
50
72
  do {
51
72
    t = (__u32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
52
72
        ((unsigned) buf[1] << 8 | buf[0]);
53
72
    *(__u32 *) buf = t;
54
72
    buf += 4;
55
72
  } while (--longs);
56
8
}
57
58
/*
59
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
60
 * initialization constants.
61
 */
62
static void
63
MD5Init(struct MD5Context *ctx)
64
4
{
65
4
  ctx->buf[0] = 0x67452301;
66
4
  ctx->buf[1] = 0xefcdab89;
67
4
  ctx->buf[2] = 0x98badcfe;
68
4
  ctx->buf[3] = 0x10325476;
69
70
4
  ctx->bits[0] = 0;
71
4
  ctx->bits[1] = 0;
72
4
}
73
74
/*
75
 * Update context to reflect the concatenation of another buffer full
76
 * of bytes.
77
 */
78
static void
79
MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
80
4
{
81
4
  register __u32 t;
82
83
  /* Update bitcount */
84
85
4
  t = ctx->bits[0];
86
4
  if ((ctx->bits[0] = t + ((__u32) len << 3)) < t)
87
0
    ctx->bits[1]++; /* Carry from low to high */
88
4
  ctx->bits[1] += len >> 29;
89
90
4
  t = (t >> 3) & 0x3f;  /* Bytes already in shsInfo->data */
91
92
  /* Handle any leading odd-sized chunks */
93
94
4
  if (t) {
95
0
    unsigned char *p = (unsigned char *) ctx->in + t;
96
97
0
    t = 64 - t;
98
0
    if (len < t) {
99
0
      memmove(p, buf, len);
100
0
      return;
101
0
    }
102
0
    memmove(p, buf, t);
103
0
    byteReverse(ctx->in, 16);
104
0
    MD5Transform(ctx->buf, (__u32 *) ctx->in);
105
0
    buf += t;
106
0
    len -= t;
107
0
  }
108
  /* Process data in 64-byte chunks */
109
110
4
  while (len >= 64) {
111
0
    memmove(ctx->in, buf, 64);
112
0
    byteReverse(ctx->in, 16);
113
0
    MD5Transform(ctx->buf, (__u32 *) ctx->in);
114
0
    buf += 64;
115
0
    len -= 64;
116
0
  }
117
118
  /* Handle any remaining bytes of data. */
119
120
4
  memmove(ctx->in, buf, len);
121
4
}
122
123
/*
124
 * Final wrapup - pad to 64-byte boundary with the bit pattern
125
 * 1 0* (64-bit count of bits processed, MSB-first)
126
 */
127
static void
128
MD5Final(unsigned char digest[16], struct MD5Context *ctx)
129
4
{
130
4
  unsigned int count;
131
4
  unsigned char *p;
132
133
  /* Compute number of bytes mod 64 */
134
4
  count = (ctx->bits[0] >> 3) & 0x3F;
135
136
  /* Set the first char of padding to 0x80.  This is safe since there is
137
     always at least one byte free */
138
4
  p = ctx->in + count;
139
4
  *p++ = 0x80;
140
141
  /* Bytes of padding needed to make 64 bytes */
142
4
  count = 64 - 1 - count;
143
144
  /* Pad out to 56 mod 64 */
145
4
  if (count < 8) {
146
    /* Two lots of padding:  Pad the first block to 64 bytes */
147
0
    memset(p, 0, count);
148
0
    byteReverse(ctx->in, 16);
149
0
    MD5Transform(ctx->buf, (__u32 *) ctx->in);
150
151
    /* Now fill the next block with 56 bytes */
152
0
    memset(ctx->in, 0, 56);
153
4
  } else {
154
    /* Pad block to 56 bytes */
155
4
    memset(p, 0, count - 8);
156
4
  }
157
4
  byteReverse(ctx->in, 14);
158
159
  /* Append length in bits and transform */
160
4
  ((__u32 *) ctx->in)[14] = ctx->bits[0];
161
4
  ((__u32 *) ctx->in)[15] = ctx->bits[1];
162
163
4
  MD5Transform(ctx->buf, (__u32 *) ctx->in);
164
4
  byteReverse((unsigned char *) ctx->buf, 4);
165
4
  memmove(digest, ctx->buf, 16);
166
4
  memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
167
4
}
168
169
/* The four core functions - F1 is optimized somewhat */
170
171
/* #define F1(x, y, z) (x & y | ~x & z) */
172
128
#define F1(x, y, z) (z ^ (x & (y ^ z)))
173
64
#define F2(x, y, z) F1(z, x, y)
174
64
#define F3(x, y, z) (x ^ y ^ z)
175
64
#define F4(x, y, z) (y ^ (x | ~z))
176
177
/* This is the central step in the MD5 algorithm. */
178
#define MD5STEP(f, w, x, y, z, data, s) \
179
256
  ( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
180
181
/*
182
 * The core of the MD5 algorithm, this alters an existing MD5 hash to
183
 * reflect the addition of 16 longwords of new data.  MD5Update blocks
184
 * the data and converts bytes into longwords for this routine.
185
 */
186
static void
187
MD5Transform(__u32 buf[4], __u32 const in[16])
188
4
{
189
4
  register __u32 a, b, c, d;
190
191
4
  a = buf[0];
192
4
  b = buf[1];
193
4
  c = buf[2];
194
4
  d = buf[3];
195
196
4
  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
197
4
  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
198
4
  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
199
4
  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
200
4
  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
201
4
  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
202
4
  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
203
4
  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
204
4
  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
205
4
  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
206
4
  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
207
4
  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
208
4
  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
209
4
  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
210
4
  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
211
4
  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
212
213
4
  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
214
4
  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
215
4
  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
216
4
  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
217
4
  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
218
4
  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
219
4
  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
220
4
  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
221
4
  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
222
4
  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
223
4
  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
224
4
  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
225
4
  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
226
4
  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
227
4
  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
228
4
  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
229
230
4
  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
231
4
  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
232
4
  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
233
4
  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
234
4
  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
235
4
  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
236
4
  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
237
4
  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
238
4
  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
239
4
  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
240
4
  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
241
4
  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
242
4
  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
243
4
  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
244
4
  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
245
4
  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
246
247
4
  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
248
4
  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
249
4
  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
250
4
  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
251
4
  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
252
4
  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
253
4
  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
254
4
  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
255
4
  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
256
4
  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
257
4
  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
258
4
  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
259
4
  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
260
4
  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
261
4
  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
262
4
  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
263
264
4
  buf[0] += a;
265
4
  buf[1] += b;
266
4
  buf[2] += c;
267
4
  buf[3] += d;
268
4
}
269
270
static int digest_md5_init(struct digest *d)
271
4
{
272
4
  MD5Init(d->ctx);
273
274
4
  return 0;
275
4
}
276
277
static int digest_md5_update(struct digest *d, const void *data,
278
           unsigned long len)
279
4
{
280
4
  MD5Update(d->ctx, data, len);
281
282
4
  return 0;
283
4
}
284
285
static int digest_md5_final(struct digest *d, unsigned char *md)
286
4
{
287
4
  MD5Final(md, d->ctx);
288
289
4
  return 0;
290
4
}
291
292
static struct digest_algo md5 = {
293
  .base = {
294
    .name   = "md5",
295
    .driver_name  = "md5-generic",
296
    .priority = 0,
297
    .algo   = HASH_ALGO_MD5,
298
  },
299
  .init = digest_md5_init,
300
  .update = digest_md5_update,
301
  .final = digest_md5_final,
302
  .digest = digest_generic_digest,
303
  .verify = digest_generic_verify,
304
  .length = 16,
305
  .ctx_length = sizeof(struct MD5Context),
306
};
307
308
static int md5_digest_register(void)
309
26
{
310
26
  return digest_algo_register(&md5);
311
26
}
312
device_initcall(md5_digest_register);