Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmlibrhash/librhash/md5.c
Line
Count
Source
1
/* md5.c - an implementation of the MD5 algorithm, based on RFC 1321.
2
 *
3
 * Copyright (c) 2007, Aleksey Kravchenko <rhash.admin@gmail.com>
4
 *
5
 * Permission to use, copy, modify, and/or distribute this software for any
6
 * purpose with or without fee is hereby granted.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9
 * REGARD TO THIS SOFTWARE  INCLUDING ALL IMPLIED WARRANTIES OF  MERCHANTABILITY
10
 * AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11
 * INDIRECT,  OR CONSEQUENTIAL DAMAGES  OR ANY DAMAGES WHATSOEVER RESULTING FROM
12
 * LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION OF CONTRACT,  NEGLIGENCE
13
 * OR OTHER TORTIOUS ACTION,  ARISING OUT OF  OR IN CONNECTION  WITH THE USE  OR
14
 * PERFORMANCE OF THIS SOFTWARE.
15
 */
16
17
#include <string.h>
18
#include "byte_order.h"
19
#include "md5.h"
20
21
/**
22
 * Initialize context before calculating hash.
23
 *
24
 * @param ctx context to initialize
25
 */
26
void rhash_md5_init(md5_ctx* ctx)
27
0
{
28
0
  memset(ctx, 0, sizeof(*ctx));
29
30
  /* initialize state */
31
0
  ctx->hash[0] = 0x67452301;
32
0
  ctx->hash[1] = 0xefcdab89;
33
0
  ctx->hash[2] = 0x98badcfe;
34
0
  ctx->hash[3] = 0x10325476;
35
0
}
36
37
/* First, define four auxiliary functions that each take as input
38
 * three 32-bit words and returns a 32-bit word.*/
39
40
/* F(x,y,z) = ((y XOR z) AND x) XOR z - is faster then original version */
41
0
#define MD5_F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z))
42
0
#define MD5_G(x, y, z) (((x) & (z)) | ((y) & (~z)))
43
0
#define MD5_H(x, y, z) ((x) ^ (y) ^ (z))
44
0
#define MD5_I(x, y, z) ((y) ^ ((x) | (~z)))
45
46
/* transformations for rounds 1, 2, 3, and 4. */
47
0
#define MD5_ROUND1(a, b, c, d, x, s, ac) { \
48
0
  (a) += MD5_F((b), (c), (d)) + (x) + (ac); \
49
0
  (a) = ROTL32((a), (s)); \
50
0
  (a) += (b); \
51
0
}
52
0
#define MD5_ROUND2(a, b, c, d, x, s, ac) { \
53
0
  (a) += MD5_G((b), (c), (d)) + (x) + (ac); \
54
0
  (a) = ROTL32((a), (s)); \
55
0
  (a) += (b); \
56
0
}
57
0
#define MD5_ROUND3(a, b, c, d, x, s, ac) { \
58
0
  (a) += MD5_H((b), (c), (d)) + (x) + (ac); \
59
0
  (a) = ROTL32((a), (s)); \
60
0
  (a) += (b); \
61
0
}
62
0
#define MD5_ROUND4(a, b, c, d, x, s, ac) { \
63
0
  (a) += MD5_I((b), (c), (d)) + (x) + (ac); \
64
0
  (a) = ROTL32((a), (s)); \
65
0
  (a) += (b); \
66
0
}
67
68
/**
69
 * The core transformation. Process a 512-bit block.
70
 * The function has been taken from RFC 1321 with little changes.
71
 *
72
 * @param state algorithm state
73
 * @param x the message block to process
74
 */
75
static void rhash_md5_process_block(unsigned state[4], const unsigned* x)
76
0
{
77
0
  register unsigned a, b, c, d;
78
0
  a = state[0];
79
0
  b = state[1];
80
0
  c = state[2];
81
0
  d = state[3];
82
83
0
  MD5_ROUND1(a, b, c, d, x[ 0],  7, 0xd76aa478);
84
0
  MD5_ROUND1(d, a, b, c, x[ 1], 12, 0xe8c7b756);
85
0
  MD5_ROUND1(c, d, a, b, x[ 2], 17, 0x242070db);
86
0
  MD5_ROUND1(b, c, d, a, x[ 3], 22, 0xc1bdceee);
87
0
  MD5_ROUND1(a, b, c, d, x[ 4],  7, 0xf57c0faf);
88
0
  MD5_ROUND1(d, a, b, c, x[ 5], 12, 0x4787c62a);
89
0
  MD5_ROUND1(c, d, a, b, x[ 6], 17, 0xa8304613);
90
0
  MD5_ROUND1(b, c, d, a, x[ 7], 22, 0xfd469501);
91
0
  MD5_ROUND1(a, b, c, d, x[ 8],  7, 0x698098d8);
92
0
  MD5_ROUND1(d, a, b, c, x[ 9], 12, 0x8b44f7af);
93
0
  MD5_ROUND1(c, d, a, b, x[10], 17, 0xffff5bb1);
94
0
  MD5_ROUND1(b, c, d, a, x[11], 22, 0x895cd7be);
95
0
  MD5_ROUND1(a, b, c, d, x[12],  7, 0x6b901122);
96
0
  MD5_ROUND1(d, a, b, c, x[13], 12, 0xfd987193);
97
0
  MD5_ROUND1(c, d, a, b, x[14], 17, 0xa679438e);
98
0
  MD5_ROUND1(b, c, d, a, x[15], 22, 0x49b40821);
99
100
0
  MD5_ROUND2(a, b, c, d, x[ 1],  5, 0xf61e2562);
101
0
  MD5_ROUND2(d, a, b, c, x[ 6],  9, 0xc040b340);
102
0
  MD5_ROUND2(c, d, a, b, x[11], 14, 0x265e5a51);
103
0
  MD5_ROUND2(b, c, d, a, x[ 0], 20, 0xe9b6c7aa);
104
0
  MD5_ROUND2(a, b, c, d, x[ 5],  5, 0xd62f105d);
105
0
  MD5_ROUND2(d, a, b, c, x[10],  9,  0x2441453);
106
0
  MD5_ROUND2(c, d, a, b, x[15], 14, 0xd8a1e681);
107
0
  MD5_ROUND2(b, c, d, a, x[ 4], 20, 0xe7d3fbc8);
108
0
  MD5_ROUND2(a, b, c, d, x[ 9],  5, 0x21e1cde6);
109
0
  MD5_ROUND2(d, a, b, c, x[14],  9, 0xc33707d6);
110
0
  MD5_ROUND2(c, d, a, b, x[ 3], 14, 0xf4d50d87);
111
0
  MD5_ROUND2(b, c, d, a, x[ 8], 20, 0x455a14ed);
112
0
  MD5_ROUND2(a, b, c, d, x[13],  5, 0xa9e3e905);
113
0
  MD5_ROUND2(d, a, b, c, x[ 2],  9, 0xfcefa3f8);
114
0
  MD5_ROUND2(c, d, a, b, x[ 7], 14, 0x676f02d9);
115
0
  MD5_ROUND2(b, c, d, a, x[12], 20, 0x8d2a4c8a);
116
117
0
  MD5_ROUND3(a, b, c, d, x[ 5],  4, 0xfffa3942);
118
0
  MD5_ROUND3(d, a, b, c, x[ 8], 11, 0x8771f681);
119
0
  MD5_ROUND3(c, d, a, b, x[11], 16, 0x6d9d6122);
120
0
  MD5_ROUND3(b, c, d, a, x[14], 23, 0xfde5380c);
121
0
  MD5_ROUND3(a, b, c, d, x[ 1],  4, 0xa4beea44);
122
0
  MD5_ROUND3(d, a, b, c, x[ 4], 11, 0x4bdecfa9);
123
0
  MD5_ROUND3(c, d, a, b, x[ 7], 16, 0xf6bb4b60);
124
0
  MD5_ROUND3(b, c, d, a, x[10], 23, 0xbebfbc70);
125
0
  MD5_ROUND3(a, b, c, d, x[13],  4, 0x289b7ec6);
126
0
  MD5_ROUND3(d, a, b, c, x[ 0], 11, 0xeaa127fa);
127
0
  MD5_ROUND3(c, d, a, b, x[ 3], 16, 0xd4ef3085);
128
0
  MD5_ROUND3(b, c, d, a, x[ 6], 23,  0x4881d05);
129
0
  MD5_ROUND3(a, b, c, d, x[ 9],  4, 0xd9d4d039);
130
0
  MD5_ROUND3(d, a, b, c, x[12], 11, 0xe6db99e5);
131
0
  MD5_ROUND3(c, d, a, b, x[15], 16, 0x1fa27cf8);
132
0
  MD5_ROUND3(b, c, d, a, x[ 2], 23, 0xc4ac5665);
133
134
0
  MD5_ROUND4(a, b, c, d, x[ 0],  6, 0xf4292244);
135
0
  MD5_ROUND4(d, a, b, c, x[ 7], 10, 0x432aff97);
136
0
  MD5_ROUND4(c, d, a, b, x[14], 15, 0xab9423a7);
137
0
  MD5_ROUND4(b, c, d, a, x[ 5], 21, 0xfc93a039);
138
0
  MD5_ROUND4(a, b, c, d, x[12],  6, 0x655b59c3);
139
0
  MD5_ROUND4(d, a, b, c, x[ 3], 10, 0x8f0ccc92);
140
0
  MD5_ROUND4(c, d, a, b, x[10], 15, 0xffeff47d);
141
0
  MD5_ROUND4(b, c, d, a, x[ 1], 21, 0x85845dd1);
142
0
  MD5_ROUND4(a, b, c, d, x[ 8],  6, 0x6fa87e4f);
143
0
  MD5_ROUND4(d, a, b, c, x[15], 10, 0xfe2ce6e0);
144
0
  MD5_ROUND4(c, d, a, b, x[ 6], 15, 0xa3014314);
145
0
  MD5_ROUND4(b, c, d, a, x[13], 21, 0x4e0811a1);
146
0
  MD5_ROUND4(a, b, c, d, x[ 4],  6, 0xf7537e82);
147
0
  MD5_ROUND4(d, a, b, c, x[11], 10, 0xbd3af235);
148
0
  MD5_ROUND4(c, d, a, b, x[ 2], 15, 0x2ad7d2bb);
149
0
  MD5_ROUND4(b, c, d, a, x[ 9], 21, 0xeb86d391);
150
151
0
  state[0] += a;
152
0
  state[1] += b;
153
0
  state[2] += c;
154
0
  state[3] += d;
155
0
}
156
157
/**
158
 * Calculate message hash.
159
 * Can be called repeatedly with chunks of the message to be hashed.
160
 *
161
 * @param ctx the algorithm context containing current hashing state
162
 * @param msg message chunk
163
 * @param size length of the message chunk
164
 */
165
void rhash_md5_update(md5_ctx* ctx, const unsigned char* msg, size_t size)
166
0
{
167
0
  unsigned index = (unsigned)ctx->length & 63;
168
0
  ctx->length += size;
169
170
  /* fill partial block */
171
0
  if (index) {
172
0
    unsigned left = md5_block_size - index;
173
0
    le32_copy(ctx->message, index, msg, (size < left ? size : left));
174
0
    if (size < left) return;
175
176
    /* process partial block */
177
0
    rhash_md5_process_block(ctx->hash, ctx->message);
178
0
    msg  += left;
179
0
    size -= left;
180
0
  }
181
0
  while (size >= md5_block_size) {
182
0
    unsigned* aligned_message_block;
183
0
    if (IS_LITTLE_ENDIAN && IS_ALIGNED_32(msg)) {
184
      /* the most common case is processing a 32-bit aligned message
185
      on a little-endian CPU without copying it */
186
0
      aligned_message_block = (unsigned*)msg;
187
0
    } else {
188
0
      le32_copy(ctx->message, 0, msg, md5_block_size);
189
0
      aligned_message_block = ctx->message;
190
0
    }
191
192
0
    rhash_md5_process_block(ctx->hash, aligned_message_block);
193
0
    msg  += md5_block_size;
194
0
    size -= md5_block_size;
195
0
  }
196
0
  if (size) {
197
    /* save leftovers */
198
0
    le32_copy(ctx->message, 0, msg, size);
199
0
  }
200
0
}
201
202
/**
203
 * Store calculated hash into the given array.
204
 *
205
 * @param ctx the algorithm context containing current hashing state
206
 * @param result calculated hash in binary form
207
 */
208
void rhash_md5_final(md5_ctx* ctx, unsigned char* result)
209
0
{
210
0
  unsigned index = ((unsigned)ctx->length & 63) >> 2;
211
0
  unsigned shift = ((unsigned)ctx->length & 3) * 8;
212
213
  /* pad message and run for last block */
214
215
  /* append the byte 0x80 to the message */
216
0
  ctx->message[index]   &= ~(0xFFFFFFFFu << shift);
217
0
  ctx->message[index++] ^= 0x80u << shift;
218
219
  /* if no room left in the message to store 64-bit message length */
220
0
  if (index > 14) {
221
    /* then fill the rest with zeros and process it */
222
0
    while (index < 16) {
223
0
      ctx->message[index++] = 0;
224
0
    }
225
0
    rhash_md5_process_block(ctx->hash, ctx->message);
226
0
    index = 0;
227
0
  }
228
0
  while (index < 14) {
229
0
    ctx->message[index++] = 0;
230
0
  }
231
0
  ctx->message[14] = (unsigned)(ctx->length << 3);
232
0
  ctx->message[15] = (unsigned)(ctx->length >> 29);
233
0
  rhash_md5_process_block(ctx->hash, ctx->message);
234
235
0
  if (result) le32_copy(result, 0, &ctx->hash, 16);
236
0
}