annotate nasmbuild/nasm-2.13rc9/nasmlib/md5c.c @ 10557:23e8673c32c3

<moonythedwarf> ` cd nasmbuild/nasm-2.13rc9; ./configure > confoutput
author HackBot
date Thu, 30 Mar 2017 21:05:13 +0000
parents 587a0a262d22
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10554
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
1 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
2 * This code implements the MD5 message-digest algorithm.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
3 * The algorithm is due to Ron Rivest. This code was
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
4 * written by Colin Plumb in 1993, no copyright is claimed.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
5 * This code is in the public domain; do with it what you wish.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
6 *
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
7 * Equivalent code is available from RSA Data Security, Inc.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
8 * This code has been tested against that, and is equivalent,
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
9 * except that you don't need to include two pages of legalese
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
10 * with every copy.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
11 *
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
12 * To compute the message digest of a chunk of bytes, declare an
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
13 * MD5Context structure, pass it to MD5Init, call MD5Update as
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
14 * needed on buffers full of bytes, and then call MD5Final, which
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
15 * will fill a supplied 16-byte array with the digest.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
16 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
17
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
18 #include "md5.h"
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
19 #include <string.h> /* for memcpy() */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
20
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
21 #ifdef WORDS_LITTEENDIAN
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
22 #define byteReverse(buf, len) /* Nothing */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
23 #else
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
24 static void byteReverse(unsigned char *buf, unsigned longs);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
25
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
26 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
27 * Note: this code is harmless on little-endian machines.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
28 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
29 static void byteReverse(unsigned char *buf, unsigned longs)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
30 {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
31 uint32_t t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
32 do {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
33 t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
34 ((unsigned) buf[1] << 8 | buf[0]);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
35 *(uint32_t *) buf = t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
36 buf += 4;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
37 } while (--longs);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
38 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
39 #endif
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
40
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
41 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
42 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
43 * initialization constants.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
44 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
45 void MD5Init(MD5_CTX *ctx)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
46 {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
47 ctx->buf[0] = 0x67452301;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
48 ctx->buf[1] = 0xefcdab89;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
49 ctx->buf[2] = 0x98badcfe;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
50 ctx->buf[3] = 0x10325476;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
51
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
52 ctx->bits[0] = 0;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
53 ctx->bits[1] = 0;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
54 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
55
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
56 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
57 * Update context to reflect the concatenation of another buffer full
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
58 * of bytes.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
59 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
60 void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
61 {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
62 uint32_t t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
63
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
64 /* Update bitcount */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
65
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
66 t = ctx->bits[0];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
67 if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
68 ctx->bits[1]++; /* Carry from low to high */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
69 ctx->bits[1] += len >> 29;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
70
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
71 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
72
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
73 /* Handle any leading odd-sized chunks */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
74
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
75 if (t) {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
76 unsigned char *p = (unsigned char *) ctx->in + t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
77
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
78 t = 64 - t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
79 if (len < t) {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
80 memcpy(p, buf, len);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
81 return;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
82 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
83 memcpy(p, buf, t);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
84 byteReverse(ctx->in, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
85 MD5Transform(ctx->buf, (uint32_t *) ctx->in);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
86 buf += t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
87 len -= t;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
88 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
89 /* Process data in 64-byte chunks */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
90
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
91 while (len >= 64) {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
92 memcpy(ctx->in, buf, 64);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
93 byteReverse(ctx->in, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
94 MD5Transform(ctx->buf, (uint32_t *) ctx->in);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
95 buf += 64;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
96 len -= 64;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
97 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
98
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
99 /* Handle any remaining bytes of data. */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
100
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
101 memcpy(ctx->in, buf, len);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
102 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
103
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
104 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
105 * Final wrapup - pad to 64-byte boundary with the bit pattern
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
106 * 1 0* (64-bit count of bits processed, MSB-first)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
107 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
108 void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
109 {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
110 unsigned count;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
111 unsigned char *p;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
112
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
113 /* Compute number of bytes mod 64 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
114 count = (ctx->bits[0] >> 3) & 0x3F;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
115
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
116 /* Set the first char of padding to 0x80. This is safe since there is
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
117 always at least one byte free */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
118 p = ctx->in + count;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
119 *p++ = 0x80;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
120
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
121 /* Bytes of padding needed to make 64 bytes */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
122 count = 64 - 1 - count;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
123
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
124 /* Pad out to 56 mod 64 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
125 if (count < 8) {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
126 /* Two lots of padding: Pad the first block to 64 bytes */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
127 memset(p, 0, count);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
128 byteReverse(ctx->in, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
129 MD5Transform(ctx->buf, (uint32_t *) ctx->in);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
130
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
131 /* Now fill the next block with 56 bytes */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
132 memset(ctx->in, 0, 56);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
133 } else {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
134 /* Pad block to 56 bytes */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
135 memset(p, 0, count - 8);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
136 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
137 byteReverse(ctx->in, 14);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
138
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
139 /* Append length in bits and transform */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
140 ((uint32_t *) ctx->in)[14] = ctx->bits[0];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
141 ((uint32_t *) ctx->in)[15] = ctx->bits[1];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
142
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
143 MD5Transform(ctx->buf, (uint32_t *) ctx->in);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
144 byteReverse((unsigned char *) ctx->buf, 4);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
145 memcpy(digest, ctx->buf, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
146 memset((char *) ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
147 }
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
148
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
149 /* The four core functions - F1 is optimized somewhat */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
150
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
151 /* #define F1(x, y, z) (x & y | ~x & z) */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
152 #define F1(x, y, z) (z ^ (x & (y ^ z)))
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
153 #define F2(x, y, z) F1(z, x, y)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
154 #define F3(x, y, z) (x ^ y ^ z)
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
155 #define F4(x, y, z) (y ^ (x | ~z))
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
156
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
157 /* This is the central step in the MD5 algorithm. */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
158 #define MD5STEP(f, w, x, y, z, data, s) \
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
159 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
160
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
161 /*
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
162 * The core of the MD5 algorithm, this alters an existing MD5 hash to
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
163 * reflect the addition of 16 longwords of new data. MD5Update blocks
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
164 * the data and converts bytes into longwords for this routine.
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
165 */
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
166 void MD5Transform(uint32_t buf[4], uint32_t const in[16])
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
167 {
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
168 register uint32_t a, b, c, d;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
169
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
170 a = buf[0];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
171 b = buf[1];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
172 c = buf[2];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
173 d = buf[3];
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
174
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
175 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
176 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
177 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
178 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
179 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
180 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
181 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
182 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
183 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
184 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
185 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
186 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
187 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
188 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
189 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
190 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
191
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
192 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
193 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
194 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
195 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
196 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
197 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
198 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
199 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
200 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
201 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
202 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
203 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
204 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
205 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
206 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
207 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
208
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
209 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
210 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
211 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
212 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
213 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
214 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
215 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
216 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
217 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
218 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
219 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
220 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
221 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
222 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
223 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
224 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
225
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
226 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
227 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
228 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
229 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
230 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
231 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
232 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
233 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
234 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
235 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
236 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
237 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
238 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
239 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
240 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
241 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
242
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
243 buf[0] += a;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
244 buf[1] += b;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
245 buf[2] += c;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
246 buf[3] += d;
587a0a262d22 <moonythedwarf> ` cd nasmbuild; tar -xf nasm.tar.gz
HackBot
parents:
diff changeset
247 }