1141cc406Sopenharmony_ci/* Functions to compute MD5 message digest of files or memory blocks.
2141cc406Sopenharmony_ci   according to the definition of MD5 in RFC 1321 from April 1992.
3141cc406Sopenharmony_ci   Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
4141cc406Sopenharmony_ci   This file is part of the GNU C Library.
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   The GNU C Library is free software; you can redistribute it and/or
7141cc406Sopenharmony_ci   modify it under the terms of the GNU Lesser General Public
8141cc406Sopenharmony_ci   License as published by the Free Software Foundation; either
9141cc406Sopenharmony_ci   version 2.1 of the License, or (at your option) any later version.
10141cc406Sopenharmony_ci
11141cc406Sopenharmony_ci   The GNU C Library is distributed in the hope that it will be useful,
12141cc406Sopenharmony_ci   but WITHOUT ANY WARRANTY; without even the implied warranty of
13141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14141cc406Sopenharmony_ci   Lesser General Public License for more details.
15141cc406Sopenharmony_ci
16141cc406Sopenharmony_ci   You should have received a copy of the GNU Lesser General Public
17141cc406Sopenharmony_ci   License along with the GNU C Library.
18141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.  */
19141cc406Sopenharmony_ci
20141cc406Sopenharmony_ci/* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.  */
21141cc406Sopenharmony_ci
22141cc406Sopenharmony_ci#ifdef HAVE_CONFIG_H
23141cc406Sopenharmony_ci# include <config.h>
24141cc406Sopenharmony_ci#endif
25141cc406Sopenharmony_ci
26141cc406Sopenharmony_ci#include <sys/types.h>
27141cc406Sopenharmony_ci
28141cc406Sopenharmony_ci#include <stdlib.h>
29141cc406Sopenharmony_ci#include <string.h>
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci#include "md5.h"
32141cc406Sopenharmony_ci
33141cc406Sopenharmony_ci#ifdef _LIBC
34141cc406Sopenharmony_ci# include <endian.h>
35141cc406Sopenharmony_ci# if __BYTE_ORDER == __BIG_ENDIAN
36141cc406Sopenharmony_ci#  define WORDS_BIGENDIAN 1
37141cc406Sopenharmony_ci# endif
38141cc406Sopenharmony_ci/* We need to keep the namespace clean so define the MD5 function
39141cc406Sopenharmony_ci   protected using leading __ .  */
40141cc406Sopenharmony_ci# define md5_init_ctx __md5_init_ctx
41141cc406Sopenharmony_ci# define md5_process_block __md5_process_block
42141cc406Sopenharmony_ci# define md5_process_bytes __md5_process_bytes
43141cc406Sopenharmony_ci# define md5_finish_ctx __md5_finish_ctx
44141cc406Sopenharmony_ci# define md5_read_ctx __md5_read_ctx
45141cc406Sopenharmony_ci# define md5_stream __md5_stream
46141cc406Sopenharmony_ci# define md5_buffer __md5_buffer
47141cc406Sopenharmony_ci#endif
48141cc406Sopenharmony_ci
49141cc406Sopenharmony_ci#ifdef WORDS_BIGENDIAN
50141cc406Sopenharmony_ci# define SWAP(n)							\
51141cc406Sopenharmony_ci    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
52141cc406Sopenharmony_ci#else
53141cc406Sopenharmony_ci# define SWAP(n) (n)
54141cc406Sopenharmony_ci#endif
55141cc406Sopenharmony_ci
56141cc406Sopenharmony_ci
57141cc406Sopenharmony_ci/* This array contains the bytes used to pad the buffer to the next
58141cc406Sopenharmony_ci   64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
59141cc406Sopenharmony_cistatic const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
60141cc406Sopenharmony_ci
61141cc406Sopenharmony_ci
62141cc406Sopenharmony_civoid
63141cc406Sopenharmony_cimd5_init_ctx (struct md5_ctx *ctx);
64141cc406Sopenharmony_civoid *
65141cc406Sopenharmony_cimd5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
66141cc406Sopenharmony_civoid *
67141cc406Sopenharmony_cimd5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
68141cc406Sopenharmony_ciint
69141cc406Sopenharmony_cimd5_stream (FILE *stream, void *resblock);
70141cc406Sopenharmony_civoid *
71141cc406Sopenharmony_cimd5_buffer (const char *buffer, size_t len, void *resblock);
72141cc406Sopenharmony_civoid
73141cc406Sopenharmony_cimd5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx);
74141cc406Sopenharmony_civoid
75141cc406Sopenharmony_cimd5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx);
76141cc406Sopenharmony_ci
77141cc406Sopenharmony_ci
78141cc406Sopenharmony_ci/* Initialize structure containing state of computation.
79141cc406Sopenharmony_ci   (RFC 1321, 3.3: Step 3)  */
80141cc406Sopenharmony_civoid
81141cc406Sopenharmony_cimd5_init_ctx (struct md5_ctx *ctx)
82141cc406Sopenharmony_ci{
83141cc406Sopenharmony_ci  ctx->A = 0x67452301;
84141cc406Sopenharmony_ci  ctx->B = 0xefcdab89;
85141cc406Sopenharmony_ci  ctx->C = 0x98badcfe;
86141cc406Sopenharmony_ci  ctx->D = 0x10325476;
87141cc406Sopenharmony_ci
88141cc406Sopenharmony_ci  ctx->total[0] = ctx->total[1] = 0;
89141cc406Sopenharmony_ci  ctx->buflen = 0;
90141cc406Sopenharmony_ci}
91141cc406Sopenharmony_ci
92141cc406Sopenharmony_ci/* Put result from CTX in first 16 bytes following RESBUF.  The result
93141cc406Sopenharmony_ci   must be in little endian byte order.
94141cc406Sopenharmony_ci
95141cc406Sopenharmony_ci   IMPORTANT: On some systems it is required that RESBUF is correctly
96141cc406Sopenharmony_ci   aligned for a 32 bits value.  */
97141cc406Sopenharmony_civoid *
98141cc406Sopenharmony_cimd5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
99141cc406Sopenharmony_ci{
100141cc406Sopenharmony_ci  ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
101141cc406Sopenharmony_ci  ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
102141cc406Sopenharmony_ci  ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
103141cc406Sopenharmony_ci  ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
104141cc406Sopenharmony_ci
105141cc406Sopenharmony_ci  return resbuf;
106141cc406Sopenharmony_ci}
107141cc406Sopenharmony_ci
108141cc406Sopenharmony_ci/* Process the remaining bytes in the internal buffer and the usual
109141cc406Sopenharmony_ci   prolog according to the standard and write the result to RESBUF.
110141cc406Sopenharmony_ci
111141cc406Sopenharmony_ci   IMPORTANT: On some systems it is required that RESBUF is correctly
112141cc406Sopenharmony_ci   aligned for a 32 bits value.  */
113141cc406Sopenharmony_civoid *
114141cc406Sopenharmony_cimd5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
115141cc406Sopenharmony_ci{
116141cc406Sopenharmony_ci  /* Take yet unprocessed bytes into account.  */
117141cc406Sopenharmony_ci  md5_uint32 bytes = ctx->buflen;
118141cc406Sopenharmony_ci  size_t pad;
119141cc406Sopenharmony_ci  size_t offset;
120141cc406Sopenharmony_ci
121141cc406Sopenharmony_ci  /* Now count remaining bytes.  */
122141cc406Sopenharmony_ci  ctx->total[0] += bytes;
123141cc406Sopenharmony_ci  if (ctx->total[0] < bytes)
124141cc406Sopenharmony_ci    ++ctx->total[1];
125141cc406Sopenharmony_ci
126141cc406Sopenharmony_ci  pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
127141cc406Sopenharmony_ci  memcpy (&ctx->buffer[bytes], fillbuf, pad);
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_ci  /* Put the 64-bit file length in *bits* at the end of the buffer.  */
130141cc406Sopenharmony_ci  offset = (bytes + pad) / sizeof (md5_uint32);
131141cc406Sopenharmony_ci  ((md5_uint32 *) ctx->buffer)[offset] = SWAP (ctx->total[0] << 3);
132141cc406Sopenharmony_ci  offset = (bytes + pad + 4) / sizeof (md5_uint32);
133141cc406Sopenharmony_ci  ((md5_uint32 *) ctx->buffer)[offset] = SWAP ((ctx->total[1] << 3) |
134141cc406Sopenharmony_ci					       (ctx->total[0] >> 29));
135141cc406Sopenharmony_ci
136141cc406Sopenharmony_ci  /* Process last bytes.  */
137141cc406Sopenharmony_ci  md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
138141cc406Sopenharmony_ci
139141cc406Sopenharmony_ci  return md5_read_ctx (ctx, resbuf);
140141cc406Sopenharmony_ci}
141141cc406Sopenharmony_ci
142141cc406Sopenharmony_ci/* Compute MD5 message digest for bytes read from STREAM.  The
143141cc406Sopenharmony_ci   resulting message digest number will be written into the 16 bytes
144141cc406Sopenharmony_ci   beginning at RESBLOCK.  */
145141cc406Sopenharmony_ciint
146141cc406Sopenharmony_cimd5_stream (FILE *stream, void *resblock)
147141cc406Sopenharmony_ci{
148141cc406Sopenharmony_ci  /* Important: BLOCKSIZE must be a multiple of 64.  */
149141cc406Sopenharmony_ci#define BLOCKSIZE 4096
150141cc406Sopenharmony_ci  struct md5_ctx ctx;
151141cc406Sopenharmony_ci  char buffer[BLOCKSIZE + 72];
152141cc406Sopenharmony_ci  size_t sum;
153141cc406Sopenharmony_ci
154141cc406Sopenharmony_ci  /* Initialize the computation context.  */
155141cc406Sopenharmony_ci  md5_init_ctx (&ctx);
156141cc406Sopenharmony_ci
157141cc406Sopenharmony_ci  /* Iterate over full file contents.  */
158141cc406Sopenharmony_ci  while (1)
159141cc406Sopenharmony_ci    {
160141cc406Sopenharmony_ci      /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
161141cc406Sopenharmony_ci	 computation function processes the whole buffer so that with the
162141cc406Sopenharmony_ci	 next round of the loop another block can be read.  */
163141cc406Sopenharmony_ci      size_t n;
164141cc406Sopenharmony_ci      sum = 0;
165141cc406Sopenharmony_ci
166141cc406Sopenharmony_ci      /* Read block.  Take care for partial reads.  */
167141cc406Sopenharmony_ci      do
168141cc406Sopenharmony_ci	{
169141cc406Sopenharmony_ci	  n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
170141cc406Sopenharmony_ci
171141cc406Sopenharmony_ci	  sum += n;
172141cc406Sopenharmony_ci	}
173141cc406Sopenharmony_ci      while (sum < BLOCKSIZE && n != 0);
174141cc406Sopenharmony_ci      if (n == 0 && ferror (stream))
175141cc406Sopenharmony_ci        return 1;
176141cc406Sopenharmony_ci
177141cc406Sopenharmony_ci      /* If end of file is reached, end the loop.  */
178141cc406Sopenharmony_ci      if (n == 0)
179141cc406Sopenharmony_ci	break;
180141cc406Sopenharmony_ci
181141cc406Sopenharmony_ci      /* Process buffer with BLOCKSIZE bytes.  Note that
182141cc406Sopenharmony_ci			BLOCKSIZE % 64 == 0
183141cc406Sopenharmony_ci       */
184141cc406Sopenharmony_ci      md5_process_block (buffer, BLOCKSIZE, &ctx);
185141cc406Sopenharmony_ci    }
186141cc406Sopenharmony_ci
187141cc406Sopenharmony_ci  /* Add the last bytes if necessary.  */
188141cc406Sopenharmony_ci  if (sum > 0)
189141cc406Sopenharmony_ci    md5_process_bytes (buffer, sum, &ctx);
190141cc406Sopenharmony_ci
191141cc406Sopenharmony_ci  /* Construct result in desired memory.  */
192141cc406Sopenharmony_ci  md5_finish_ctx (&ctx, resblock);
193141cc406Sopenharmony_ci  return 0;
194141cc406Sopenharmony_ci}
195141cc406Sopenharmony_ci
196141cc406Sopenharmony_ci/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
197141cc406Sopenharmony_ci   result is always in little endian byte order, so that a byte-wise
198141cc406Sopenharmony_ci   output yields to the wanted ASCII representation of the message
199141cc406Sopenharmony_ci   digest.  */
200141cc406Sopenharmony_civoid *
201141cc406Sopenharmony_cimd5_buffer (const char *buffer, size_t len, void *resblock)
202141cc406Sopenharmony_ci{
203141cc406Sopenharmony_ci  struct md5_ctx ctx;
204141cc406Sopenharmony_ci
205141cc406Sopenharmony_ci  /* Initialize the computation context.  */
206141cc406Sopenharmony_ci  md5_init_ctx (&ctx);
207141cc406Sopenharmony_ci
208141cc406Sopenharmony_ci  /* Process whole buffer but last len % 64 bytes.  */
209141cc406Sopenharmony_ci  md5_process_bytes (buffer, len, &ctx);
210141cc406Sopenharmony_ci
211141cc406Sopenharmony_ci  /* Put result in desired memory area.  */
212141cc406Sopenharmony_ci  return md5_finish_ctx (&ctx, resblock);
213141cc406Sopenharmony_ci}
214141cc406Sopenharmony_ci
215141cc406Sopenharmony_ci
216141cc406Sopenharmony_civoid
217141cc406Sopenharmony_cimd5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
218141cc406Sopenharmony_ci{
219141cc406Sopenharmony_ci  /* When we already have some bits in our internal buffer concatenate
220141cc406Sopenharmony_ci     both inputs first.  */
221141cc406Sopenharmony_ci  if (ctx->buflen != 0)
222141cc406Sopenharmony_ci    {
223141cc406Sopenharmony_ci      size_t left_over = ctx->buflen;
224141cc406Sopenharmony_ci      size_t add = 128 - left_over > len ? len : 128 - left_over;
225141cc406Sopenharmony_ci
226141cc406Sopenharmony_ci      memcpy (&ctx->buffer[left_over], buffer, add);
227141cc406Sopenharmony_ci      ctx->buflen += add;
228141cc406Sopenharmony_ci
229141cc406Sopenharmony_ci      if (ctx->buflen > 64)
230141cc406Sopenharmony_ci	{
231141cc406Sopenharmony_ci	  md5_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
232141cc406Sopenharmony_ci
233141cc406Sopenharmony_ci	  ctx->buflen &= 63;
234141cc406Sopenharmony_ci	  /* The regions in the following copy operation cannot overlap.  */
235141cc406Sopenharmony_ci	  memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
236141cc406Sopenharmony_ci		  ctx->buflen);
237141cc406Sopenharmony_ci	}
238141cc406Sopenharmony_ci
239141cc406Sopenharmony_ci      buffer = (const char *) buffer + add;
240141cc406Sopenharmony_ci      len -= add;
241141cc406Sopenharmony_ci    }
242141cc406Sopenharmony_ci
243141cc406Sopenharmony_ci  /* Process available complete blocks.  */
244141cc406Sopenharmony_ci  if (len >= 64)
245141cc406Sopenharmony_ci    {
246141cc406Sopenharmony_ci#if !_STRING_ARCH_unaligned
247141cc406Sopenharmony_ci/* To check alignment gcc has an appropriate operator.  Other
248141cc406Sopenharmony_ci   compilers don't.  */
249141cc406Sopenharmony_ci# if __GNUC__ >= 2
250141cc406Sopenharmony_ci#  define UNALIGNED_P(p) (((md5_uintptr) p) % __alignof__ (md5_uint32) != 0)
251141cc406Sopenharmony_ci# else
252141cc406Sopenharmony_ci#  define UNALIGNED_P(p) (((md5_uintptr) p) % sizeof (md5_uint32) != 0)
253141cc406Sopenharmony_ci# endif
254141cc406Sopenharmony_ci      if (UNALIGNED_P (buffer))
255141cc406Sopenharmony_ci	while (len > 64)
256141cc406Sopenharmony_ci	  {
257141cc406Sopenharmony_ci	    md5_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
258141cc406Sopenharmony_ci	    buffer = (const char *) buffer + 64;
259141cc406Sopenharmony_ci	    len -= 64;
260141cc406Sopenharmony_ci	  }
261141cc406Sopenharmony_ci      else
262141cc406Sopenharmony_ci#endif
263141cc406Sopenharmony_ci	{
264141cc406Sopenharmony_ci	  md5_process_block (buffer, len & ~63, ctx);
265141cc406Sopenharmony_ci	  buffer = (const char *) buffer + (len & ~63);
266141cc406Sopenharmony_ci	  len &= 63;
267141cc406Sopenharmony_ci	}
268141cc406Sopenharmony_ci    }
269141cc406Sopenharmony_ci
270141cc406Sopenharmony_ci  /* Move remaining bytes in internal buffer.  */
271141cc406Sopenharmony_ci  if (len > 0)
272141cc406Sopenharmony_ci    {
273141cc406Sopenharmony_ci      size_t left_over = ctx->buflen;
274141cc406Sopenharmony_ci
275141cc406Sopenharmony_ci      memcpy (&ctx->buffer[left_over], buffer, len);
276141cc406Sopenharmony_ci      left_over += len;
277141cc406Sopenharmony_ci      if (left_over >= 64)
278141cc406Sopenharmony_ci	{
279141cc406Sopenharmony_ci	  md5_process_block (ctx->buffer, 64, ctx);
280141cc406Sopenharmony_ci	  left_over -= 64;
281141cc406Sopenharmony_ci	  memcpy (ctx->buffer, &ctx->buffer[64], left_over);
282141cc406Sopenharmony_ci	}
283141cc406Sopenharmony_ci      ctx->buflen = left_over;
284141cc406Sopenharmony_ci    }
285141cc406Sopenharmony_ci}
286141cc406Sopenharmony_ci
287141cc406Sopenharmony_ci
288141cc406Sopenharmony_ci/* These are the four functions used in the four steps of the MD5 algorithm
289141cc406Sopenharmony_ci   and defined in the RFC 1321.  The first function is a little bit optimized
290141cc406Sopenharmony_ci   (as found in Colin Plumbs public domain implementation).  */
291141cc406Sopenharmony_ci/* #define FF(b, c, d) ((b & c) | (~b & d)) */
292141cc406Sopenharmony_ci#define FF(b, c, d) (d ^ (b & (c ^ d)))
293141cc406Sopenharmony_ci#define FG(b, c, d) FF (d, b, c)
294141cc406Sopenharmony_ci#define FH(b, c, d) (b ^ c ^ d)
295141cc406Sopenharmony_ci#define FI(b, c, d) (c ^ (b | ~d))
296141cc406Sopenharmony_ci
297141cc406Sopenharmony_ci/* Process LEN bytes of BUFFER, accumulating context into CTX.
298141cc406Sopenharmony_ci   It is assumed that LEN % 64 == 0.  */
299141cc406Sopenharmony_ci
300141cc406Sopenharmony_civoid
301141cc406Sopenharmony_cimd5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
302141cc406Sopenharmony_ci{
303141cc406Sopenharmony_ci  md5_uint32 correct_words[16];
304141cc406Sopenharmony_ci  const md5_uint32 *words = buffer;
305141cc406Sopenharmony_ci  size_t nwords = len / sizeof (md5_uint32);
306141cc406Sopenharmony_ci  const md5_uint32 *endp = words + nwords;
307141cc406Sopenharmony_ci  md5_uint32 A = ctx->A;
308141cc406Sopenharmony_ci  md5_uint32 B = ctx->B;
309141cc406Sopenharmony_ci  md5_uint32 C = ctx->C;
310141cc406Sopenharmony_ci  md5_uint32 D = ctx->D;
311141cc406Sopenharmony_ci
312141cc406Sopenharmony_ci  /* First increment the byte count.  RFC 1321 specifies the possible
313141cc406Sopenharmony_ci     length of the file up to 2^64 bits.  Here we only compute the
314141cc406Sopenharmony_ci     number of bytes.  Do a double word increment.  */
315141cc406Sopenharmony_ci  ctx->total[0] += len;
316141cc406Sopenharmony_ci  if (ctx->total[0] < len)
317141cc406Sopenharmony_ci    ++ctx->total[1];
318141cc406Sopenharmony_ci
319141cc406Sopenharmony_ci  /* Process all bytes in the buffer with 64 bytes in each round of
320141cc406Sopenharmony_ci     the loop.  */
321141cc406Sopenharmony_ci  while (words < endp)
322141cc406Sopenharmony_ci    {
323141cc406Sopenharmony_ci      md5_uint32 *cwp = correct_words;
324141cc406Sopenharmony_ci      md5_uint32 A_save = A;
325141cc406Sopenharmony_ci      md5_uint32 B_save = B;
326141cc406Sopenharmony_ci      md5_uint32 C_save = C;
327141cc406Sopenharmony_ci      md5_uint32 D_save = D;
328141cc406Sopenharmony_ci
329141cc406Sopenharmony_ci      /* First round: using the given function, the context and a constant
330141cc406Sopenharmony_ci	 the next context is computed.  Because the algorithms processing
331141cc406Sopenharmony_ci	 unit is a 32-bit word and it is determined to work on words in
332141cc406Sopenharmony_ci	 little endian byte order we perhaps have to change the byte order
333141cc406Sopenharmony_ci	 before the computation.  To reduce the work for the next steps
334141cc406Sopenharmony_ci	 we store the swapped words in the array CORRECT_WORDS.  */
335141cc406Sopenharmony_ci
336141cc406Sopenharmony_ci#define OP(a, b, c, d, s, T)						\
337141cc406Sopenharmony_ci      do								\
338141cc406Sopenharmony_ci        {								\
339141cc406Sopenharmony_ci	  a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T;		\
340141cc406Sopenharmony_ci	  ++words;							\
341141cc406Sopenharmony_ci	  CYCLIC (a, s);						\
342141cc406Sopenharmony_ci	  a += b;							\
343141cc406Sopenharmony_ci        }								\
344141cc406Sopenharmony_ci      while (0)
345141cc406Sopenharmony_ci
346141cc406Sopenharmony_ci      /* It is unfortunate that C does not provide an operator for
347141cc406Sopenharmony_ci	 cyclic rotation.  Hope the C compiler is smart enough.  */
348141cc406Sopenharmony_ci#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
349141cc406Sopenharmony_ci
350141cc406Sopenharmony_ci      /* Before we start, one word to the strange constants.
351141cc406Sopenharmony_ci	 They are defined in RFC 1321 as
352141cc406Sopenharmony_ci
353141cc406Sopenharmony_ci	 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
354141cc406Sopenharmony_ci       */
355141cc406Sopenharmony_ci
356141cc406Sopenharmony_ci      /* Round 1.  */
357141cc406Sopenharmony_ci      OP (A, B, C, D,  7, 0xd76aa478);
358141cc406Sopenharmony_ci      OP (D, A, B, C, 12, 0xe8c7b756);
359141cc406Sopenharmony_ci      OP (C, D, A, B, 17, 0x242070db);
360141cc406Sopenharmony_ci      OP (B, C, D, A, 22, 0xc1bdceee);
361141cc406Sopenharmony_ci      OP (A, B, C, D,  7, 0xf57c0faf);
362141cc406Sopenharmony_ci      OP (D, A, B, C, 12, 0x4787c62a);
363141cc406Sopenharmony_ci      OP (C, D, A, B, 17, 0xa8304613);
364141cc406Sopenharmony_ci      OP (B, C, D, A, 22, 0xfd469501);
365141cc406Sopenharmony_ci      OP (A, B, C, D,  7, 0x698098d8);
366141cc406Sopenharmony_ci      OP (D, A, B, C, 12, 0x8b44f7af);
367141cc406Sopenharmony_ci      OP (C, D, A, B, 17, 0xffff5bb1);
368141cc406Sopenharmony_ci      OP (B, C, D, A, 22, 0x895cd7be);
369141cc406Sopenharmony_ci      OP (A, B, C, D,  7, 0x6b901122);
370141cc406Sopenharmony_ci      OP (D, A, B, C, 12, 0xfd987193);
371141cc406Sopenharmony_ci      OP (C, D, A, B, 17, 0xa679438e);
372141cc406Sopenharmony_ci      OP (B, C, D, A, 22, 0x49b40821);
373141cc406Sopenharmony_ci
374141cc406Sopenharmony_ci      /* For the second to fourth round we have the possibly swapped words
375141cc406Sopenharmony_ci	 in CORRECT_WORDS.  Redefine the macro to take an additional first
376141cc406Sopenharmony_ci	 argument specifying the function to use.  */
377141cc406Sopenharmony_ci#undef OP
378141cc406Sopenharmony_ci#define OP(f, a, b, c, d, k, s, T)					\
379141cc406Sopenharmony_ci      do 								\
380141cc406Sopenharmony_ci	{								\
381141cc406Sopenharmony_ci	  a += f (b, c, d) + correct_words[k] + T;			\
382141cc406Sopenharmony_ci	  CYCLIC (a, s);						\
383141cc406Sopenharmony_ci	  a += b;							\
384141cc406Sopenharmony_ci	}								\
385141cc406Sopenharmony_ci      while (0)
386141cc406Sopenharmony_ci
387141cc406Sopenharmony_ci      /* Round 2.  */
388141cc406Sopenharmony_ci      OP (FG, A, B, C, D,  1,  5, 0xf61e2562);
389141cc406Sopenharmony_ci      OP (FG, D, A, B, C,  6,  9, 0xc040b340);
390141cc406Sopenharmony_ci      OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
391141cc406Sopenharmony_ci      OP (FG, B, C, D, A,  0, 20, 0xe9b6c7aa);
392141cc406Sopenharmony_ci      OP (FG, A, B, C, D,  5,  5, 0xd62f105d);
393141cc406Sopenharmony_ci      OP (FG, D, A, B, C, 10,  9, 0x02441453);
394141cc406Sopenharmony_ci      OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
395141cc406Sopenharmony_ci      OP (FG, B, C, D, A,  4, 20, 0xe7d3fbc8);
396141cc406Sopenharmony_ci      OP (FG, A, B, C, D,  9,  5, 0x21e1cde6);
397141cc406Sopenharmony_ci      OP (FG, D, A, B, C, 14,  9, 0xc33707d6);
398141cc406Sopenharmony_ci      OP (FG, C, D, A, B,  3, 14, 0xf4d50d87);
399141cc406Sopenharmony_ci      OP (FG, B, C, D, A,  8, 20, 0x455a14ed);
400141cc406Sopenharmony_ci      OP (FG, A, B, C, D, 13,  5, 0xa9e3e905);
401141cc406Sopenharmony_ci      OP (FG, D, A, B, C,  2,  9, 0xfcefa3f8);
402141cc406Sopenharmony_ci      OP (FG, C, D, A, B,  7, 14, 0x676f02d9);
403141cc406Sopenharmony_ci      OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
404141cc406Sopenharmony_ci
405141cc406Sopenharmony_ci      /* Round 3.  */
406141cc406Sopenharmony_ci      OP (FH, A, B, C, D,  5,  4, 0xfffa3942);
407141cc406Sopenharmony_ci      OP (FH, D, A, B, C,  8, 11, 0x8771f681);
408141cc406Sopenharmony_ci      OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
409141cc406Sopenharmony_ci      OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
410141cc406Sopenharmony_ci      OP (FH, A, B, C, D,  1,  4, 0xa4beea44);
411141cc406Sopenharmony_ci      OP (FH, D, A, B, C,  4, 11, 0x4bdecfa9);
412141cc406Sopenharmony_ci      OP (FH, C, D, A, B,  7, 16, 0xf6bb4b60);
413141cc406Sopenharmony_ci      OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
414141cc406Sopenharmony_ci      OP (FH, A, B, C, D, 13,  4, 0x289b7ec6);
415141cc406Sopenharmony_ci      OP (FH, D, A, B, C,  0, 11, 0xeaa127fa);
416141cc406Sopenharmony_ci      OP (FH, C, D, A, B,  3, 16, 0xd4ef3085);
417141cc406Sopenharmony_ci      OP (FH, B, C, D, A,  6, 23, 0x04881d05);
418141cc406Sopenharmony_ci      OP (FH, A, B, C, D,  9,  4, 0xd9d4d039);
419141cc406Sopenharmony_ci      OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
420141cc406Sopenharmony_ci      OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
421141cc406Sopenharmony_ci      OP (FH, B, C, D, A,  2, 23, 0xc4ac5665);
422141cc406Sopenharmony_ci
423141cc406Sopenharmony_ci      /* Round 4.  */
424141cc406Sopenharmony_ci      OP (FI, A, B, C, D,  0,  6, 0xf4292244);
425141cc406Sopenharmony_ci      OP (FI, D, A, B, C,  7, 10, 0x432aff97);
426141cc406Sopenharmony_ci      OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
427141cc406Sopenharmony_ci      OP (FI, B, C, D, A,  5, 21, 0xfc93a039);
428141cc406Sopenharmony_ci      OP (FI, A, B, C, D, 12,  6, 0x655b59c3);
429141cc406Sopenharmony_ci      OP (FI, D, A, B, C,  3, 10, 0x8f0ccc92);
430141cc406Sopenharmony_ci      OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
431141cc406Sopenharmony_ci      OP (FI, B, C, D, A,  1, 21, 0x85845dd1);
432141cc406Sopenharmony_ci      OP (FI, A, B, C, D,  8,  6, 0x6fa87e4f);
433141cc406Sopenharmony_ci      OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
434141cc406Sopenharmony_ci      OP (FI, C, D, A, B,  6, 15, 0xa3014314);
435141cc406Sopenharmony_ci      OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
436141cc406Sopenharmony_ci      OP (FI, A, B, C, D,  4,  6, 0xf7537e82);
437141cc406Sopenharmony_ci      OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
438141cc406Sopenharmony_ci      OP (FI, C, D, A, B,  2, 15, 0x2ad7d2bb);
439141cc406Sopenharmony_ci      OP (FI, B, C, D, A,  9, 21, 0xeb86d391);
440141cc406Sopenharmony_ci
441141cc406Sopenharmony_ci      /* Add the starting values of the context.  */
442141cc406Sopenharmony_ci      A += A_save;
443141cc406Sopenharmony_ci      B += B_save;
444141cc406Sopenharmony_ci      C += C_save;
445141cc406Sopenharmony_ci      D += D_save;
446141cc406Sopenharmony_ci    }
447141cc406Sopenharmony_ci
448141cc406Sopenharmony_ci  /* Put checksum in context given as argument.  */
449141cc406Sopenharmony_ci  ctx->A = A;
450141cc406Sopenharmony_ci  ctx->B = B;
451141cc406Sopenharmony_ci  ctx->C = C;
452141cc406Sopenharmony_ci  ctx->D = D;
453141cc406Sopenharmony_ci}
454