Split lines with many copyright years
[gromacs.git] / src / gromacs / fileio / md5.cpp
blobeb99ff7f38e8f4a1900c43299903ecf141c85604
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2009,2010,2011,2012,2014 by the GROMACS development team.
5 * Copyright (c) 2015,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /* This software has been altered by GROMACS for its use, including
37 * the use of GMX_INTEGER_BIG_ENDIAN, and the renaming of the
38 * functions md5_init, md5_append and md5_finish to have a gmx_ prefix
39 * (to avoid name clashes). */
40 #include "gmxpre.h"
42 #include "config.h"
44 #include <cstring>
46 #if GMX_INTEGER_BIG_ENDIAN
47 # define ARCH_IS_BIG_ENDIAN 1
48 #else
49 # define ARCH_IS_BIG_ENDIAN 0
50 #endif
53 Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
55 This software is provided 'as-is', without any express or implied
56 warranty. In no event will the authors be held liable for any damages
57 arising from the use of this software.
59 Permission is granted to anyone to use this software for any purpose,
60 including commercial applications, and to alter it and redistribute it
61 freely, subject to the following restrictions:
63 1. The origin of this software must not be misrepresented; you must not
64 claim that you wrote the original software. If you use this software
65 in a product, an acknowledgment in the product documentation would be
66 appreciated but is not required.
67 2. Altered source versions must be plainly marked as such, and must not be
68 misrepresented as being the original software.
69 3. This notice may not be removed or altered from any source distribution.
71 L. Peter Deutsch
72 ghost@aladdin.com
76 Independent implementation of MD5 (RFC 1321).
78 This code implements the MD5 Algorithm defined in RFC 1321, whose
79 text is available at
80 http://www.ietf.org/rfc/rfc1321.txt
81 The code is derived from the text of the RFC, including the test suite
82 (section A.5) but excluding the rest of Appendix A. It does not include
83 any code or documentation that is identified in the RFC as being
84 copyrighted.
86 The original and principal author of md5.c is L. Peter Deutsch
87 <ghost@aladdin.com>. Other authors are noted in the change history
88 that follows (in reverse chronological order):
90 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
91 either statically or dynamically; added missing #include <string.h>
92 in library.
93 2002-03-11 lpd Corrected argument list for main(), and added int return
94 type, in test program and T value program.
95 2002-02-21 lpd Added missing #include <stdio.h> in test program.
96 2000-07-03 lpd Patched to eliminate warnings about "constant is
97 unsigned in ANSI C, signed in traditional"; made test program
98 self-checking.
99 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
100 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
101 1999-05-03 lpd Original version.
104 #include "md5.h"
106 #undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
107 #ifdef ARCH_IS_BIG_ENDIAN
108 # define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
109 #else
110 # define BYTE_ORDER 0
111 #endif
113 #define T_MASK (static_cast<md5_word_t>(~0))
114 #define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
115 #define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
116 #define T3 0x242070db
117 #define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
118 #define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
119 #define T6 0x4787c62a
120 #define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
121 #define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
122 #define T9 0x698098d8
123 #define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
124 #define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
125 #define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
126 #define T13 0x6b901122
127 #define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
128 #define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
129 #define T16 0x49b40821
130 #define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
131 #define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
132 #define T19 0x265e5a51
133 #define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
134 #define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
135 #define T22 0x02441453
136 #define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
137 #define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
138 #define T25 0x21e1cde6
139 #define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
140 #define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
141 #define T28 0x455a14ed
142 #define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
143 #define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
144 #define T31 0x676f02d9
145 #define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
146 #define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
147 #define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
148 #define T35 0x6d9d6122
149 #define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
150 #define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
151 #define T38 0x4bdecfa9
152 #define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
153 #define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
154 #define T41 0x289b7ec6
155 #define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
156 #define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
157 #define T44 0x04881d05
158 #define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
159 #define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
160 #define T47 0x1fa27cf8
161 #define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
162 #define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
163 #define T50 0x432aff97
164 #define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
165 #define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
166 #define T53 0x655b59c3
167 #define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
168 #define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
169 #define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
170 #define T57 0x6fa87e4f
171 #define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
172 #define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
173 #define T60 0x4e0811a1
174 #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
175 #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
176 #define T63 0x2ad7d2bb
177 #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
180 static void md5_process(md5_state_t* pms, const md5_byte_t* data /*[64]*/)
182 md5_word_t a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3];
183 md5_word_t t;
184 #if BYTE_ORDER > 0
185 /* Define storage only for big-endian CPUs. */
186 md5_word_t X[16];
187 #else
188 /* Define storage for little-endian or both types of CPUs. */
189 md5_word_t xbuf[16];
190 const md5_word_t* X;
191 #endif
194 #if BYTE_ORDER == 0
196 * Determine dynamically whether this is a big-endian or
197 * little-endian machine, since we can use a more efficient
198 * algorithm on the latter.
200 static const int w = 1;
202 if (*(reinterpret_cast<const md5_byte_t*>(&w))) /* dynamic little-endian */
203 #endif
204 #if BYTE_ORDER <= 0 /* little-endian */
207 * On little-endian machines, we can process properly aligned
208 * data without copying it.
210 if (!((data - reinterpret_cast<const md5_byte_t*>(0)) & 3))
212 /* data are properly aligned */
213 X = reinterpret_cast<const md5_word_t*>(data);
215 else
217 /* not aligned */
218 std::memcpy(xbuf, data, 64);
219 X = xbuf;
222 #endif
223 #if BYTE_ORDER == 0
224 else /* dynamic big-endian */
225 #endif
226 #if BYTE_ORDER >= 0 /* big-endian */
229 * On big-endian machines, we must arrange the bytes in the
230 * right order.
232 const md5_byte_t* xp = data;
233 int i;
235 # if BYTE_ORDER == 0
236 X = xbuf; /* (dynamic only) */
237 # else
238 # define xbuf X /* (static only) */
239 # endif
240 for (i = 0; i < 16; ++i, xp += 4)
242 xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
245 #endif
248 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
250 /* Round 1. */
251 /* Let [abcd k s i] denote the operation
252 a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
253 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
254 #define SET(a, b, c, d, k, s, Ti) \
255 t = (a) + F(b, c, d) + X[k] + (Ti); \
256 (a) = ROTATE_LEFT(t, s) + b
257 /* Do the following 16 operations. */
258 SET(a, b, c, d, 0, 7, T1);
259 SET(d, a, b, c, 1, 12, T2);
260 SET(c, d, a, b, 2, 17, T3);
261 SET(b, c, d, a, 3, 22, T4);
262 SET(a, b, c, d, 4, 7, T5);
263 SET(d, a, b, c, 5, 12, T6);
264 SET(c, d, a, b, 6, 17, T7);
265 SET(b, c, d, a, 7, 22, T8);
266 SET(a, b, c, d, 8, 7, T9);
267 SET(d, a, b, c, 9, 12, T10);
268 SET(c, d, a, b, 10, 17, T11);
269 SET(b, c, d, a, 11, 22, T12);
270 SET(a, b, c, d, 12, 7, T13);
271 SET(d, a, b, c, 13, 12, T14);
272 SET(c, d, a, b, 14, 17, T15);
273 SET(b, c, d, a, 15, 22, T16);
274 #undef SET
276 /* Round 2. */
277 /* Let [abcd k s i] denote the operation
278 a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
279 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
280 #define SET(a, b, c, d, k, s, Ti) \
281 t = (a) + G(b, c, d) + X[k] + (Ti); \
282 (a) = ROTATE_LEFT(t, s) + b
283 /* Do the following 16 operations. */
284 SET(a, b, c, d, 1, 5, T17);
285 SET(d, a, b, c, 6, 9, T18);
286 SET(c, d, a, b, 11, 14, T19);
287 SET(b, c, d, a, 0, 20, T20);
288 SET(a, b, c, d, 5, 5, T21);
289 SET(d, a, b, c, 10, 9, T22);
290 SET(c, d, a, b, 15, 14, T23);
291 SET(b, c, d, a, 4, 20, T24);
292 SET(a, b, c, d, 9, 5, T25);
293 SET(d, a, b, c, 14, 9, T26);
294 SET(c, d, a, b, 3, 14, T27);
295 SET(b, c, d, a, 8, 20, T28);
296 SET(a, b, c, d, 13, 5, T29);
297 SET(d, a, b, c, 2, 9, T30);
298 SET(c, d, a, b, 7, 14, T31);
299 SET(b, c, d, a, 12, 20, T32);
300 #undef SET
302 /* Round 3. */
303 /* Let [abcd k s t] denote the operation
304 a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
305 #define H(x, y, z) ((x) ^ (y) ^ (z))
306 #define SET(a, b, c, d, k, s, Ti) \
307 t = (a) + H(b, c, d) + X[k] + (Ti); \
308 (a) = ROTATE_LEFT(t, s) + b
309 /* Do the following 16 operations. */
310 SET(a, b, c, d, 5, 4, T33);
311 SET(d, a, b, c, 8, 11, T34);
312 SET(c, d, a, b, 11, 16, T35);
313 SET(b, c, d, a, 14, 23, T36);
314 SET(a, b, c, d, 1, 4, T37);
315 SET(d, a, b, c, 4, 11, T38);
316 SET(c, d, a, b, 7, 16, T39);
317 SET(b, c, d, a, 10, 23, T40);
318 SET(a, b, c, d, 13, 4, T41);
319 SET(d, a, b, c, 0, 11, T42);
320 SET(c, d, a, b, 3, 16, T43);
321 SET(b, c, d, a, 6, 23, T44);
322 SET(a, b, c, d, 9, 4, T45);
323 SET(d, a, b, c, 12, 11, T46);
324 SET(c, d, a, b, 15, 16, T47);
325 SET(b, c, d, a, 2, 23, T48);
326 #undef SET
328 /* Round 4. */
329 /* Let [abcd k s t] denote the operation
330 a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
331 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
332 #define SET(a, b, c, d, k, s, Ti) \
333 t = (a) + I(b, c, d) + X[k] + (Ti); \
334 (a) = ROTATE_LEFT(t, s) + b
335 /* Do the following 16 operations. */
336 SET(a, b, c, d, 0, 6, T49);
337 SET(d, a, b, c, 7, 10, T50);
338 SET(c, d, a, b, 14, 15, T51);
339 SET(b, c, d, a, 5, 21, T52);
340 SET(a, b, c, d, 12, 6, T53);
341 SET(d, a, b, c, 3, 10, T54);
342 SET(c, d, a, b, 10, 15, T55);
343 SET(b, c, d, a, 1, 21, T56);
344 SET(a, b, c, d, 8, 6, T57);
345 SET(d, a, b, c, 15, 10, T58);
346 SET(c, d, a, b, 6, 15, T59);
347 SET(b, c, d, a, 13, 21, T60);
348 SET(a, b, c, d, 4, 6, T61);
349 SET(d, a, b, c, 11, 10, T62);
350 SET(c, d, a, b, 2, 15, T63);
351 SET(b, c, d, a, 9, 21, T64);
352 #undef SET
354 /* Then perform the following additions. (That is increment each
355 of the four registers by the value it had before this block
356 was started.) */
357 pms->abcd[0] += a;
358 pms->abcd[1] += b;
359 pms->abcd[2] += c;
360 pms->abcd[3] += d;
363 void gmx_md5_init(md5_state_t* pms)
365 pms->count[0] = pms->count[1] = 0;
366 pms->abcd[0] = 0x67452301;
367 pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
368 pms->abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
369 pms->abcd[3] = 0x10325476;
372 void gmx_md5_append(md5_state_t* pms, const md5_byte_t* data, int nbytes)
374 const md5_byte_t* p = data;
375 int left = nbytes;
376 int offset = (pms->count[0] >> 3) & 63;
377 md5_word_t nbits = static_cast<md5_word_t>(nbytes << 3);
379 if (nbytes <= 0)
381 return;
384 /* Update the message length. */
385 pms->count[1] += nbytes >> 29;
386 pms->count[0] += nbits;
387 if (pms->count[0] < nbits)
389 pms->count[1]++;
392 /* Process an initial partial block. */
393 if (offset)
395 int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
397 std::memcpy(pms->buf + offset, p, copy);
398 if (offset + copy < 64)
400 return;
402 p += copy;
403 left -= copy;
404 md5_process(pms, pms->buf);
407 /* Process full blocks. */
408 for (; left >= 64; p += 64, left -= 64)
410 md5_process(pms, p);
413 /* Process a final partial block. */
414 if (left)
416 memcpy(pms->buf, p, left);
420 std::array<unsigned char, 16> gmx_md5_finish(md5_state_t* pms)
422 static const md5_byte_t pad[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
423 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
424 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
426 md5_byte_t data[8];
427 int i;
429 /* Save the length before padding. */
430 for (i = 0; i < 8; ++i)
432 data[i] = static_cast<md5_byte_t>(pms->count[i >> 2] >> ((i & 3) << 3));
434 /* Pad to 56 bytes mod 64. */
435 gmx_md5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
436 /* Append the length. */
437 gmx_md5_append(pms, data, 8);
438 std::array<unsigned char, 16> digest;
439 for (size_t i = 0; i < digest.size(); ++i)
441 digest[i] = static_cast<unsigned char>(pms->abcd[i >> 2] >> ((i & 3) << 3));
443 return digest;