UFO: Alien Invasion
Loading...
Searching...
No Matches
md4.cpp
Go to the documentation of this file.
1
7
8/*
9 Copyright (C) 1997-1998 Andrew Tridgell
10
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
20 See the GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to:
24
25 Free Software Foundation, Inc.
26 59 Temple Place - Suite 330
27 Boston, MA 02111-1307, USA
28
29 $Id$
30*/
31
32#include "../shared/shared.h"
33#include "md4.h"
34
35static struct mdfour *m;
36
37#define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
38#define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
39#define H(X,Y,Z) ((X)^(Y)^(Z))
40#ifdef LARGE_INT32
41#define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
42#else
43#define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
44#endif
45
46#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
47#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
48#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
49
50/* this applies md4 to 64 byte chunks */
51static void mdfour64 (uint32_t* M)
52{
53 uint32_t X[16];
54
55 for (int j = 0; j < 16; j++)
56 X[j] = M[j];
57
58 uint32_t A = m->A, B = m->B, C = m->C, D = m->D;
59 uint32_t 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 (int 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
101static void copy64 (uint32_t* M, const unsigned char* in)
102{
103 for (int i = 0; i < 16; i++)
104 M[i] = (in[i * 4 + 3] << 24) | (in[i * 4 + 2] << 16) | (in[i * 4 + 1] << 8) | (in[i * 4 + 0] << 0);
105}
106
107static void copy4 (unsigned char* out, uint32_t x)
108{
109 out[0] = x&0xFF;
110 out[1] = (x>>8)&0xFF;
111 out[2] = (x>>16)&0xFF;
112 out[3] = (x>>24)&0xFF;
113}
114
115static void mdfour_begin (struct mdfour *md)
116{
117 md->A = 0x67452301;
118 md->B = 0xefcdab89;
119 md->C = 0x98badcfe;
120 md->D = 0x10325476;
121 md->totalN = 0;
122}
123
124
125static void mdfour_tail (const unsigned char* in, int n)
126{
127 unsigned char buf[128];
128 uint32_t M[16];
129
130 m->totalN += n;
131
132 const uint32_t b = m->totalN * 8;
133
134 OBJZERO(buf);
135 if (n)
136 memcpy(buf, in, n);
137 buf[n] = 0x80;
138
139 if (n <= 55) {
140 copy4(buf + 56, b);
141 copy64(M, buf);
142 mdfour64(M);
143 } else {
144 copy4(buf + 120, b);
145 copy64(M, buf);
146 mdfour64(M);
147 copy64(M, buf + 64);
148 mdfour64(M);
149 }
150}
151
152static void mdfour_update (struct mdfour *md, const unsigned char* in, int n)
153{
154 uint32_t M[16];
155
161
162 m = md;
163
164 while (n >= 64) {
165 copy64(M, in);
166 mdfour64(M);
167 in += 64;
168 n -= 64;
169 m->totalN += 64;
170 }
171
172 mdfour_tail(in, n);
173}
174
175
176static void mdfour_result (struct mdfour *md, unsigned char* out)
177{
178 m = md;
179
180 copy4(out, m->A);
181 copy4(out + 4, m->B);
182 copy4(out + 8, m->C);
183 copy4(out + 12, m->D);
184}
185
186
187static void mdfour (unsigned char* out, const unsigned char* in, int n)
188{
189 struct mdfour md;
190 mdfour_begin(&md);
191 mdfour_update(&md, in, n);
192 mdfour_result(&md, out);
193}
194
201
202unsigned Com_BlockChecksum (const void* buffer, int length)
203{
204 int digest[4];
205
206 mdfour((unsigned char*) digest, (const unsigned char*) buffer, length);
207
208 return digest[0] ^ digest[1] ^ digest[2] ^ digest[3];
209}
#define M(x)
Definition cl_keys.cpp:74
voidpf void * buf
Definition ioapi.h:42
static void mdfour_update(struct mdfour *md, const unsigned char *in, int n)
Definition md4.cpp:152
#define ROUND3(a, b, c, d, k, s)
Definition md4.cpp:48
static void mdfour_tail(const unsigned char *in, int n)
Definition md4.cpp:125
static void mdfour64(uint32_t *M)
Definition md4.cpp:51
static void mdfour(unsigned char *out, const unsigned char *in, int n)
Definition md4.cpp:187
static struct mdfour * m
Definition md4.cpp:35
static void mdfour_result(struct mdfour *md, unsigned char *out)
Definition md4.cpp:176
static void copy64(uint32_t *M, const unsigned char *in)
Definition md4.cpp:101
#define ROUND2(a, b, c, d, k, s)
Definition md4.cpp:47
static void mdfour_begin(struct mdfour *md)
Definition md4.cpp:115
unsigned Com_BlockChecksum(const void *buffer, int length)
Definition md4.cpp:202
#define ROUND1(a, b, c, d, k, s)
Definition md4.cpp:46
static void copy4(unsigned char *out, uint32_t x)
Definition md4.cpp:107
QGL_EXTERN GLuint GLsizei GLsizei * length
Definition r_gl.h:110
QGL_EXTERN GLint i
Definition r_gl.h:113
#define OBJZERO(obj)
Definition shared.h:178
Definition md4.h:36
uint32_t B
Definition md4.h:37
uint32_t totalN
Definition md4.h:38
uint32_t D
Definition md4.h:37
uint32_t C
Definition md4.h:37
uint32_t A
Definition md4.h:37