Quake II RTX doxygen  1.0 dev
mdfour.c File Reference
#include "shared/shared.h"
#include "common/mdfour.h"

Go to the source code of this file.

Macros

#define F(X, Y, Z)   (((X)&(Y)) | ((~(X))&(Z)))
 
#define G(X, Y, Z)   (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
 
#define H(X, Y, Z)   ((X)^(Y)^(Z))
 
#define lshift(x, s)   (((x)<<(s)) | ((x)>>(32-(s))))
 
#define ROUND1(a, b, c, d, k, s)   a = lshift(a + F(b,c,d) + X[k], s)
 
#define ROUND2(a, b, c, d, k, s)   a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
 
#define ROUND3(a, b, c, d, k, s)   a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)
 

Functions

static void mdfour64 (uint32_t *M)
 
static void copy64 (uint32_t *M, uint8_t *in)
 
static void copy4 (uint8_t *out, uint32_t x)
 
void mdfour_begin (struct mdfour *md)
 
static void mdfour_tail (uint8_t *in, size_t n)
 
void mdfour_update (struct mdfour *md, uint8_t *in, size_t n)
 
void mdfour_result (struct mdfour *md, uint8_t *out)
 
uint32_t Com_BlockChecksum (void *buffer, size_t len)
 

Variables

static struct mdfour * m
 

Macro Definition Documentation

◆ F

#define F (   X,
  Y,
 
)    (((X)&(Y)) | ((~(X))&(Z)))

Definition at line 34 of file mdfour.c.

◆ G

#define G (   X,
  Y,
 
)    (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))

Definition at line 35 of file mdfour.c.

◆ H

#define H (   X,
  Y,
 
)    ((X)^(Y)^(Z))

Definition at line 36 of file mdfour.c.

◆ lshift

#define lshift (   x,
 
)    (((x)<<(s)) | ((x)>>(32-(s))))

Definition at line 40 of file mdfour.c.

◆ ROUND1

#define ROUND1 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + F(b,c,d) + X[k], s)

Definition at line 43 of file mdfour.c.

◆ ROUND2

#define ROUND2 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)

Definition at line 44 of file mdfour.c.

◆ ROUND3

#define ROUND3 (   a,
  b,
  c,
  d,
  k,
 
)    a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)

Definition at line 45 of file mdfour.c.

Function Documentation

◆ Com_BlockChecksum()

uint32_t Com_BlockChecksum ( void buffer,
size_t  len 
)

Definition at line 187 of file mdfour.c.

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 }

Referenced by BSP_Load().

◆ copy4()

static void copy4 ( uint8_t *  out,
uint32_t  x 
)
static

Definition at line 110 of file mdfour.c.

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 }

Referenced by mdfour_result(), and mdfour_tail().

◆ copy64()

static void copy64 ( uint32_t *  M,
uint8_t *  in 
)
static

Definition at line 101 of file mdfour.c.

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 }

Referenced by mdfour_tail(), and mdfour_update().

◆ mdfour64()

static void mdfour64 ( uint32_t *  M)
static

Definition at line 48 of file mdfour.c.

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 }

Referenced by mdfour_tail(), and mdfour_update().

◆ mdfour_begin()

void mdfour_begin ( struct mdfour *  md)

Definition at line 118 of file mdfour.c.

119 {
120  md->A = 0x67452301;
121  md->B = 0xefcdab89;
122  md->C = 0x98badcfe;
123  md->D = 0x10325476;
124  md->totalN = 0;
125 }

Referenced by CalcHash(), and Com_BlockChecksum().

◆ mdfour_result()

void mdfour_result ( struct mdfour *  md,
uint8_t *  out 
)

Definition at line 175 of file mdfour.c.

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 }

Referenced by CalcHash(), and Com_BlockChecksum().

◆ mdfour_tail()

static void mdfour_tail ( uint8_t *  in,
size_t  n 
)
static

Definition at line 128 of file mdfour.c.

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 }

Referenced by mdfour_update().

◆ mdfour_update()

void mdfour_update ( struct mdfour *  md,
uint8_t *  in,
size_t  n 
)

Definition at line 155 of file mdfour.c.

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 }

Referenced by CalcHash(), and Com_BlockChecksum().

Variable Documentation

◆ m

ROUND2
#define ROUND2(a, b, c, d, k, s)
Definition: mdfour.c:44
mdfour64
static void mdfour64(uint32_t *M)
Definition: mdfour.c:48
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