Coverage Report

Created: 2023-05-19 06:16

/src/ntp-dev/lib/isc/md5.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3
 * Copyright (C) 2000, 2001  Internet Software Consortium.
4
 *
5
 * Permission to use, copy, modify, and/or distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
 * PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
/* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */
19
20
/*! \file
21
 * This code implements the MD5 message-digest algorithm.
22
 * The algorithm is due to Ron Rivest.  This code was
23
 * written by Colin Plumb in 1993, no copyright is claimed.
24
 * This code is in the public domain; do with it what you wish.
25
 *
26
 * Equivalent code is available from RSA Data Security, Inc.
27
 * This code has been tested against that, and is equivalent,
28
 * except that you don't need to include two pages of legalese
29
 * with every copy.
30
 *
31
 * To compute the message digest of a chunk of bytes, declare an
32
 * MD5Context structure, pass it to MD5Init, call MD5Update as
33
 * needed on buffers full of bytes, and then call MD5Final, which
34
 * will fill a supplied 16-byte array with the digest.
35
 */
36
37
#include "config.h"
38
39
#include <isc/assertions.h>
40
#include <isc/md5.h>
41
#include <isc/platform.h>
42
#include <isc/string.h>
43
#include <isc/types.h>
44
#include <isc/util.h>
45
46
#ifdef ISC_PLATFORM_OPENSSLHASH
47
48
void
49
isc_md5_init(isc_md5_t *ctx) {
50
  EVP_DigestInit(ctx, EVP_md5());
51
}
52
53
void
54
isc_md5_invalidate(isc_md5_t *ctx) {
55
  EVP_MD_CTX_cleanup(ctx);
56
}
57
58
void
59
isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
60
  EVP_DigestUpdate(ctx, (const void *) buf, (size_t) len);
61
}
62
63
void
64
isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
65
  EVP_DigestFinal(ctx, digest, NULL);
66
}
67
68
#else
69
70
static void
71
byteSwap(isc_uint32_t *buf, unsigned words)
72
373
{
73
373
  unsigned char *p = (unsigned char *)buf;
74
75
4.21k
  do {
76
4.21k
    *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
77
4.21k
      ((unsigned)p[1] << 8 | p[0]);
78
4.21k
    p += 4;
79
4.21k
  } while (--words);
80
373
}
81
82
/*!
83
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
84
 * initialization constants.
85
 */
86
void
87
125
isc_md5_init(isc_md5_t *ctx) {
88
125
  ctx->buf[0] = 0x67452301;
89
125
  ctx->buf[1] = 0xefcdab89;
90
125
  ctx->buf[2] = 0x98badcfe;
91
125
  ctx->buf[3] = 0x10325476;
92
93
125
  ctx->bytes[0] = 0;
94
125
  ctx->bytes[1] = 0;
95
125
}
96
97
void
98
0
isc_md5_invalidate(isc_md5_t *ctx) {
99
0
  memset(ctx, 0, sizeof(isc_md5_t));
100
0
}
101
102
/*@{*/
103
/*! The four core functions - F1 is optimized somewhat */
104
105
/* #define F1(x, y, z) (x & y | ~x & z) */
106
7.93k
#define F1(x, y, z) (z ^ (x & (y ^ z)))
107
3.96k
#define F2(x, y, z) F1(z, x, y)
108
3.96k
#define F3(x, y, z) (x ^ y ^ z)
109
3.96k
#define F4(x, y, z) (y ^ (x | ~z))
110
/*@}*/
111
112
/*! This is the central step in the MD5 algorithm. */
113
#define MD5STEP(f,w,x,y,z,in,s) \
114
15.8k
   (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
115
116
/*!
117
 * The core of the MD5 algorithm, this alters an existing MD5 hash to
118
 * reflect the addition of 16 longwords of new data.  MD5Update blocks
119
 * the data and converts bytes into longwords for this routine.
120
 */
121
static void
122
248
transform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
123
248
  register isc_uint32_t a, b, c, d;
124
125
248
  a = buf[0];
126
248
  b = buf[1];
127
248
  c = buf[2];
128
248
  d = buf[3];
129
130
248
  MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
131
248
  MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
132
248
  MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
133
248
  MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
134
248
  MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
135
248
  MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
136
248
  MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
137
248
  MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
138
248
  MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
139
248
  MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
140
248
  MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
141
248
  MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
142
248
  MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
143
248
  MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
144
248
  MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
145
248
  MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
146
147
248
  MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
148
248
  MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
149
248
  MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
150
248
  MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
151
248
  MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
152
248
  MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
153
248
  MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
154
248
  MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
155
248
  MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
156
248
  MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
157
248
  MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
158
248
  MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
159
248
  MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
160
248
  MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
161
248
  MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
162
248
  MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
163
164
248
  MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
165
248
  MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
166
248
  MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
167
248
  MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
168
248
  MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
169
248
  MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
170
248
  MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
171
248
  MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
172
248
  MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
173
248
  MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
174
248
  MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
175
248
  MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
176
248
  MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
177
248
  MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
178
248
  MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
179
248
  MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
180
181
248
  MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
182
248
  MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
183
248
  MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
184
248
  MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
185
248
  MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
186
248
  MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
187
248
  MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
188
248
  MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
189
248
  MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
190
248
  MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
191
248
  MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
192
248
  MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
193
248
  MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
194
248
  MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
195
248
  MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
196
248
  MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
197
198
248
  buf[0] += a;
199
248
  buf[1] += b;
200
248
  buf[2] += c;
201
248
  buf[3] += d;
202
248
}
203
204
/*!
205
 * Update context to reflect the concatenation of another buffer full
206
 * of bytes.
207
 */
208
void
209
745
isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
210
745
  isc_uint32_t t;
211
212
  /* Update byte count */
213
214
745
  t = ctx->bytes[0];
215
745
  if ((ctx->bytes[0] = t + len) < t)
216
0
    ctx->bytes[1]++; /* Carry from low to high */
217
218
745
  t = 64 - (t & 0x3f);  /* Space available in ctx->in (at least 1) */
219
745
  if (t > len) {
220
745
    memcpy((unsigned char *)ctx->in + 64 - t, buf, len);
221
745
    return;
222
745
  }
223
  /* First chunk is an odd size */
224
0
  memcpy((unsigned char *)ctx->in + 64 - t, buf, t);
225
0
  byteSwap(ctx->in, 16);
226
0
  transform(ctx->buf, ctx->in);
227
0
  buf += t;
228
0
  len -= t;
229
230
  /* Process data in 64-byte chunks */
231
0
  while (len >= 64) {
232
0
    memcpy(ctx->in, buf, 64);
233
0
    byteSwap(ctx->in, 16);
234
0
    transform(ctx->buf, ctx->in);
235
0
    buf += 64;
236
0
    len -= 64;
237
0
  }
238
239
  /* Handle any remaining bytes of data. */
240
0
  memcpy(ctx->in, buf, len);
241
0
}
242
243
/*!
244
 * Final wrapup - pad to 64-byte boundary with the bit pattern
245
 * 1 0* (64-bit count of bits processed, MSB-first)
246
 */
247
void
248
125
isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
249
125
  int count = ctx->bytes[0] & 0x3f;    /* Number of bytes in ctx->in */
250
125
  unsigned char *p = (unsigned char *)ctx->in + count;
251
252
  /* Set the first char of padding to 0x80.  There is always room. */
253
125
  *p++ = 0x80;
254
255
  /* Bytes of padding needed to make 56 bytes (-8..55) */
256
125
  count = 56 - 1 - count;
257
258
125
  if (count < 0) { /* Padding forces an extra block */
259
123
    memset(p, 0, count + 8);
260
123
    byteSwap(ctx->in, 16);
261
123
    transform(ctx->buf, ctx->in);
262
123
    p = (unsigned char *)ctx->in;
263
123
    count = 56;
264
123
  }
265
125
  memset(p, 0, count);
266
125
  byteSwap(ctx->in, 14);
267
268
  /* Append length in bits and transform */
269
125
  ctx->in[14] = ctx->bytes[0] << 3;
270
125
  ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
271
125
  transform(ctx->buf, ctx->in);
272
273
125
  byteSwap(ctx->buf, 4);
274
125
  memcpy(digest, ctx->buf, 16);
275
125
  memset(ctx, 0, sizeof(isc_md5_t));  /* In case it's sensitive */
276
125
}
277
#endif