Quake II RTX doxygen  1.0 dev
mdfour.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 1997-1998 Andrew Tridgell
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "shared/shared.h"
20 #include "common/mdfour.h"
21 
22 #if SIZEOF_INT > 4
23 #define LARGE_INT32
24 #endif
25 
26 
27 /* NOTE: This code makes no attempt to be fast!
28 
29  It assumes that a int is at least 32 bits long
30 */
31 
32 static struct mdfour *m;
33 
34 #define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
35 #define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
36 #define H(X,Y,Z) ((X)^(Y)^(Z))
37 #ifdef LARGE_INT32
38 #define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
39 #else
40 #define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
41 #endif
42 
43 #define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
44 #define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
45 #define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
46 
47 /* this applies md4 to 64 byte chunks */
48 static void mdfour64(uint32_t *M)
49 {
50  int j;
51  uint32_t AA, BB, CC, DD;
52  uint32_t X[16];
53  uint32_t A, B, C, D;
54 
55  for (j = 0; j < 16; j++)
56  X[j] = M[j];
57 
58  A = m->A; B = m->B; C = m->C; D = m->D;
59  AA = A; BB = B; CC = C; DD = D;
60 
61  ROUND1(A, B, C, D, 0, 3); ROUND1(D, A, B, C, 1, 7);
62  ROUND1(C, D, A, B, 2, 11); ROUND1(B, C, D, A, 3, 19);
63  ROUND1(A, B, C, D, 4, 3); ROUND1(D, A, B, C, 5, 7);
64  ROUND1(C, D, A, B, 6, 11); ROUND1(B, C, D, A, 7, 19);
65  ROUND1(A, B, C, D, 8, 3); ROUND1(D, A, B, C, 9, 7);
66  ROUND1(C, D, A, B, 10, 11); ROUND1(B, C, D, A, 11, 19);
67  ROUND1(A, B, C, D, 12, 3); ROUND1(D, A, B, C, 13, 7);
68  ROUND1(C, D, A, B, 14, 11); ROUND1(B, C, D, A, 15, 19);
69 
70  ROUND2(A, B, C, D, 0, 3); ROUND2(D, A, B, C, 4, 5);
71  ROUND2(C, D, A, B, 8, 9); ROUND2(B, C, D, A, 12, 13);
72  ROUND2(A, B, C, D, 1, 3); ROUND2(D, A, B, C, 5, 5);
73  ROUND2(C, D, A, B, 9, 9); ROUND2(B, C, D, A, 13, 13);
74  ROUND2(A, B, C, D, 2, 3); ROUND2(D, A, B, C, 6, 5);
75  ROUND2(C, D, A, B, 10, 9); ROUND2(B, C, D, A, 14, 13);
76  ROUND2(A, B, C, D, 3, 3); ROUND2(D, A, B, C, 7, 5);
77  ROUND2(C, D, A, B, 11, 9); ROUND2(B, C, D, A, 15, 13);
78 
79  ROUND3(A, B, C, D, 0, 3); ROUND3(D, A, B, C, 8, 9);
80  ROUND3(C, D, A, B, 4, 11); ROUND3(B, C, D, A, 12, 15);
81  ROUND3(A, B, C, D, 2, 3); ROUND3(D, A, B, C, 10, 9);
82  ROUND3(C, D, A, B, 6, 11); ROUND3(B, C, D, A, 14, 15);
83  ROUND3(A, B, C, D, 1, 3); ROUND3(D, A, B, C, 9, 9);
84  ROUND3(C, D, A, B, 5, 11); ROUND3(B, C, D, A, 13, 15);
85  ROUND3(A, B, C, D, 3, 3); ROUND3(D, A, B, C, 11, 9);
86  ROUND3(C, D, A, B, 7, 11); ROUND3(B, C, D, A, 15, 15);
87 
88  A += AA; B += BB; C += CC; D += DD;
89 
90 #ifdef LARGE_INT32
91  A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
92  C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
93 #endif
94 
95  for (j = 0; j < 16; j++)
96  X[j] = 0;
97 
98  m->A = A; m->B = B; m->C = C; m->D = D;
99 }
100 
101 static void copy64(uint32_t *M, uint8_t *in)
102 {
103  int i;
104 
105  for (i = 0; i < 16; i++)
106  M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) |
107  (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
108 }
109 
110 static void copy4(uint8_t *out, uint32_t x)
111 {
112  out[0] = x & 0xFF;
113  out[1] = (x >> 8) & 0xFF;
114  out[2] = (x >> 16) & 0xFF;
115  out[3] = (x >> 24) & 0xFF;
116 }
117 
118 void mdfour_begin(struct mdfour *md)
119 {
120  md->A = 0x67452301;
121  md->B = 0xefcdab89;
122  md->C = 0x98badcfe;
123  md->D = 0x10325476;
124  md->totalN = 0;
125 }
126 
127 
128 static void mdfour_tail(uint8_t *in, size_t n)
129 {
130  uint8_t buf[128];
131  uint32_t M[16];
132  uint32_t b;
133 
134  m->totalN += n;
135 
136  b = m->totalN * 8;
137 
138  memset(buf, 0, 128);
139  if (n) memcpy(buf, in, n);
140  buf[n] = 0x80;
141 
142  if (n <= 55) {
143  copy4(buf + 56, b);
144  copy64(M, buf);
145  mdfour64(M);
146  } else {
147  copy4(buf + 120, b);
148  copy64(M, buf);
149  mdfour64(M);
150  copy64(M, buf + 64);
151  mdfour64(M);
152  }
153 }
154 
155 void mdfour_update(struct mdfour *md, uint8_t *in, size_t n)
156 {
157  uint32_t M[16];
158 
159  m = md;
160 
161  if (n == 0) mdfour_tail(in, n);
162 
163  while (n >= 64) {
164  copy64(M, in);
165  mdfour64(M);
166  in += 64;
167  n -= 64;
168  m->totalN += 64;
169  }
170 
171  mdfour_tail(in, n);
172 }
173 
174 
175 void mdfour_result(struct mdfour *md, uint8_t *out)
176 {
177  m = md;
178 
179  copy4(out, m->A);
180  copy4(out + 4, m->B);
181  copy4(out + 8, m->C);
182  copy4(out + 12, m->D);
183 }
184 
185 //===================================================================
186 
187 uint32_t Com_BlockChecksum(void *buffer, size_t len)
188 {
189  uint32_t digest[4];
190  uint32_t val;
191  struct mdfour md;
192 
193  mdfour_begin(&md);
194  mdfour_update(&md, (uint8_t *)buffer, len);
195  mdfour_result(&md, (uint8_t *)digest);
196 
197  val = digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
198 
199  return val;
200 }
201 
ROUND2
#define ROUND2(a, b, c, d, k, s)
Definition: mdfour.c:44
mdfour64
static void mdfour64(uint32_t *M)
Definition: mdfour.c:48
Com_BlockChecksum
uint32_t Com_BlockChecksum(void *buffer, size_t len)
Definition: mdfour.c:187
B
#define B(name)
Definition: g_save.c:40
copy64
static void copy64(uint32_t *M, uint8_t *in)
Definition: mdfour.c:101
m
static struct mdfour * m
Definition: mdfour.c:32
mdfour_result
void mdfour_result(struct mdfour *md, uint8_t *out)
Definition: mdfour.c:175
ROUND3
#define ROUND3(a, b, c, d, k, s)
Definition: mdfour.c:45
ROUND1
#define ROUND1(a, b, c, d, k, s)
Definition: mdfour.c:43
mdfour_tail
static void mdfour_tail(uint8_t *in, size_t n)
Definition: mdfour.c:128
copy4
static void copy4(uint8_t *out, uint32_t x)
Definition: mdfour.c:110
mdfour_update
void mdfour_update(struct mdfour *md, uint8_t *in, size_t n)
Definition: mdfour.c:155
mdfour_begin
void mdfour_begin(struct mdfour *md)
Definition: mdfour.c:118