ReactOS  0.4.15-dev-1392-g3014417
util.c File Reference
#include "precomp.h"
#include <wine/debug.h>
Include dependency graph for util.c:

Go to the source code of this file.

Classes

struct  MD4_CTX
 
struct  MD5_CTX
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (ntlm)
 
VOID WINAPI MD4Init (MD4_CTX *ctx)
 
VOID WINAPI MD4Update (MD4_CTX *ctx, const unsigned char *buf, unsigned int len)
 
VOID WINAPI MD4Final (MD4_CTX *ctx)
 
VOID WINAPI MD5Init (MD5_CTX *ctx)
 
VOID WINAPI MD5Update (MD5_CTX *ctx, const unsigned char *buf, unsigned int len)
 
VOID WINAPI MD5Final (MD5_CTX *ctx)
 
ULONG ComputeCrc32 (const BYTE *pData, INT iLen, ULONG initial_crc)
 
SECURITY_STATUS SECUR32_CreateNTLM1SessionKey (PBYTE password, int len, PBYTE session_key)
 
static void SECUR32_CalcNTLM2Subkey (const BYTE *session_key, const char *magic, PBYTE subkey)
 
SECURITY_STATUS SECUR32_CreateNTLM2SubKeys (PNegoHelper helper)
 
arc4_infoSECUR32_arc4Alloc (void)
 
void SECUR32_arc4Init (arc4_info *a4i, const BYTE *key, unsigned int keyLen)
 
void SECUR32_arc4Process (arc4_info *a4i, BYTE *inoutString, unsigned int length)
 
void SECUR32_arc4Cleanup (arc4_info *a4i)
 

Variables

static const ULONG CRC_table [256]
 
static const char client_to_server_sign_constant [] = "session key to client-to-server signing key magic constant"
 
static const char client_to_server_seal_constant [] = "session key to client-to-server sealing key magic constant"
 
static const char server_to_client_sign_constant [] = "session key to server-to-client signing key magic constant"
 
static const char server_to_client_seal_constant [] = "session key to server-to-client sealing key magic constant"
 

Function Documentation

◆ ComputeCrc32()

ULONG ComputeCrc32 ( const BYTE pData,
INT  iLen,
ULONG  initial_crc 
)

Definition at line 109 of file util.c.

110 {
111  ULONG crc = ~initial_crc;
112 
113  while (iLen > 0)
114  {
115  crc = CRC_table[(crc ^ *pData) & 0xff] ^ (crc >> 8);
116  pData++;
117  iLen--;
118  }
119  return ~crc;
120 }
static const ULONG CRC_table[256]
Definition: util.c:29
unsigned int ULONG
Definition: retypes.h:1
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1827

Referenced by ntlm_CreateSignature().

◆ MD4Final()

VOID WINAPI MD4Final ( MD4_CTX ctx)

Definition at line 113 of file md4.c.

114 {
115  unsigned int count;
116  unsigned char *p;
117 
118  /* Compute number of bytes mod 64 */
119  count = (ctx->i[0] >> 3) & 0x3F;
120 
121  /* Set the first char of padding to 0x80. This is safe since there is
122  always at least one byte free */
123  p = ctx->in + count;
124  *p++ = 0x80;
125 
126  /* Bytes of padding needed to make 64 bytes */
127  count = 64 - 1 - count;
128 
129  /* Pad out to 56 mod 64 */
130  if (count < 8)
131  {
132  /* Two lots of padding: Pad the first block to 64 bytes */
133  memset( p, 0, count );
134  byteReverse( ctx->in, 16 );
135  MD4Transform( ctx->buf, (unsigned int *)ctx->in );
136 
137  /* Now fill the next block with 56 bytes */
138  memset( ctx->in, 0, 56 );
139  }
140  else
141  {
142  /* Pad block to 56 bytes */
143  memset( p, 0, count - 8 );
144  }
145 
146  byteReverse( ctx->in, 14 );
147 
148  /* Append length in bits and transform */
149  ((unsigned int *)ctx->in)[14] = ctx->i[0];
150  ((unsigned int *)ctx->in)[15] = ctx->i[1];
151 
152  MD4Transform( ctx->buf, (unsigned int *)ctx->in );
153  byteReverse( (unsigned char *)ctx->buf, 4 );
154  memcpy( ctx->digest, ctx->buf, 16 );
155 }
static void MD4Transform(unsigned int buf[4], unsigned int const in[16])
Definition: md4.c:181
GLuint GLuint GLsizei count
Definition: gl.h:1545
void byteReverse(unsigned char *buf, unsigned longs)
Definition: util.c:8
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLfloat GLfloat p
Definition: glext.h:8902
#define memset(x, y, z)
Definition: compat.h:39

Referenced by SECUR32_CreateNTLM1SessionKey().

◆ MD4Init()

VOID WINAPI MD4Init ( MD4_CTX ctx)

Definition at line 45 of file md4.c.

46 {
47  ctx->buf[0] = 0x67452301;
48  ctx->buf[1] = 0xefcdab89;
49  ctx->buf[2] = 0x98badcfe;
50  ctx->buf[3] = 0x10325476;
51 
52  ctx->i[0] = ctx->i[1] = 0;
53 }

Referenced by SECUR32_CreateNTLM1SessionKey().

◆ MD4Update()

VOID WINAPI MD4Update ( MD4_CTX ctx,
const unsigned char buf,
unsigned int  len 
)

Definition at line 59 of file md4.c.

60 {
61  register unsigned int t;
62 
63  /* Update bitcount */
64  t = ctx->i[0];
65 
66  if ((ctx->i[0] = t + (len << 3)) < t)
67  ctx->i[1]++; /* Carry from low to high */
68 
69  ctx->i[1] += len >> 29;
70  t = (t >> 3) & 0x3f;
71 
72  /* Handle any leading odd-sized chunks */
73  if (t)
74  {
75  unsigned char *p = (unsigned char *)ctx->in + t;
76  t = 64 - t;
77 
78  if (len < t)
79  {
80  memcpy( p, buf, len );
81  return;
82  }
83 
84  memcpy( p, buf, t );
85  byteReverse( ctx->in, 16 );
86 
87  MD4Transform( ctx->buf, (unsigned int *)ctx->in );
88 
89  buf += t;
90  len -= t;
91  }
92 
93  /* Process data in 64-byte chunks */
94  while (len >= 64)
95  {
96  memcpy( ctx->in, buf, 64 );
97  byteReverse( ctx->in, 16 );
98 
99  MD4Transform( ctx->buf, (unsigned int *)ctx->in );
100 
101  buf += 64;
102  len -= 64;
103  }
104 
105  /* Handle any remaining bytes of data. */
106  memcpy( ctx->in, buf, len );
107 }
static void MD4Transform(unsigned int buf[4], unsigned int const in[16])
Definition: md4.c:181
GLdouble GLdouble t
Definition: gl.h:2047
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
void byteReverse(unsigned char *buf, unsigned longs)
Definition: util.c:8
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by SECUR32_CreateNTLM1SessionKey().

◆ MD5Final()

VOID WINAPI MD5Final ( MD5_CTX ctx)

Definition at line 113 of file md5.c.

114 {
115  unsigned int count;
116  unsigned char *p;
117 
118  /* Compute number of bytes mod 64 */
119  count = (ctx->i[0] >> 3) & 0x3F;
120 
121  /* Set the first char of padding to 0x80. This is safe since there is
122  always at least one byte free */
123  p = ctx->in + count;
124  *p++ = 0x80;
125 
126  /* Bytes of padding needed to make 64 bytes */
127  count = 64 - 1 - count;
128 
129  /* Pad out to 56 mod 64 */
130  if (count < 8)
131  {
132  /* Two lots of padding: Pad the first block to 64 bytes */
133  memset( p, 0, count );
134  byteReverse( ctx->in, 16 );
135  MD5Transform( ctx->buf, (unsigned int *)ctx->in );
136 
137  /* Now fill the next block with 56 bytes */
138  memset( ctx->in, 0, 56 );
139  }
140  else
141  {
142  /* Pad block to 56 bytes */
143  memset( p, 0, count - 8 );
144  }
145 
146  byteReverse( ctx->in, 14 );
147 
148  /* Append length in bits and transform */
149  ((unsigned int *)ctx->in)[14] = ctx->i[0];
150  ((unsigned int *)ctx->in)[15] = ctx->i[1];
151 
152  MD5Transform( ctx->buf, (unsigned int *)ctx->in );
153  byteReverse( (unsigned char *)ctx->buf, 4 );
154  memcpy( ctx->digest, ctx->buf, 16 );
155 }
GLuint GLuint GLsizei count
Definition: gl.h:1545
void byteReverse(unsigned char *buf, unsigned longs)
Definition: util.c:8
static void MD5Transform(unsigned int buf[4], const unsigned int in[16])
Definition: md5.c:174
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLfloat GLfloat p
Definition: glext.h:8902
#define memset(x, y, z)
Definition: compat.h:39

Referenced by SECUR32_CalcNTLM2Subkey().

◆ MD5Init()

VOID WINAPI MD5Init ( MD5_CTX ctx)

Definition at line 45 of file md5.c.

46 {
47  ctx->buf[0] = 0x67452301;
48  ctx->buf[1] = 0xefcdab89;
49  ctx->buf[2] = 0x98badcfe;
50  ctx->buf[3] = 0x10325476;
51 
52  ctx->i[0] = ctx->i[1] = 0;
53 }

Referenced by SECUR32_CalcNTLM2Subkey().

◆ MD5Update()

VOID WINAPI MD5Update ( MD5_CTX ctx,
const unsigned char buf,
unsigned int  len 
)

Definition at line 59 of file md5.c.

60 {
61  register unsigned int t;
62 
63  /* Update bitcount */
64  t = ctx->i[0];
65 
66  if ((ctx->i[0] = t + (len << 3)) < t)
67  ctx->i[1]++; /* Carry from low to high */
68 
69  ctx->i[1] += len >> 29;
70  t = (t >> 3) & 0x3f;
71 
72  /* Handle any leading odd-sized chunks */
73  if (t)
74  {
75  unsigned char *p = (unsigned char *)ctx->in + t;
76  t = 64 - t;
77 
78  if (len < t)
79  {
80  memcpy( p, buf, len );
81  return;
82  }
83 
84  memcpy( p, buf, t );
85  byteReverse( ctx->in, 16 );
86 
87  MD5Transform( ctx->buf, (unsigned int *)ctx->in );
88 
89  buf += t;
90  len -= t;
91  }
92 
93  /* Process data in 64-byte chunks */
94  while (len >= 64)
95  {
96  memcpy( ctx->in, buf, 64 );
97  byteReverse( ctx->in, 16 );
98 
99  MD5Transform( ctx->buf, (unsigned int *)ctx->in );
100 
101  buf += 64;
102  len -= 64;
103  }
104 
105  /* Handle any remaining bytes of data. */
106  memcpy( ctx->in, buf, len );
107 }
GLdouble GLdouble t
Definition: gl.h:2047
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
void byteReverse(unsigned char *buf, unsigned longs)
Definition: util.c:8
static void MD5Transform(unsigned int buf[4], const unsigned int in[16])
Definition: md5.c:174
if(!(yy_init))
Definition: macro.lex.yy.c:714
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
GLfloat GLfloat p
Definition: glext.h:8902

Referenced by SECUR32_CalcNTLM2Subkey().

◆ SECUR32_arc4Alloc()

arc4_info* SECUR32_arc4Alloc ( void  )

Definition at line 189 of file util.c.

190 {
191  arc4_info *a4i = HeapAlloc(GetProcessHeap(), 0, sizeof(arc4_info));
192  return a4i;
193 }
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)

Referenced by ntlm_AcceptSecurityContext(), and ntlm_InitializeSecurityContextW().

◆ SECUR32_arc4Cleanup()

void SECUR32_arc4Cleanup ( arc4_info a4i)

Definition at line 246 of file util.c.

247 {
248  HeapFree(GetProcessHeap(), 0, a4i);
249 }
#define GetProcessHeap()
Definition: compat.h:595
#define HeapFree(x, y, z)
Definition: compat.h:594

Referenced by ntlm_DeleteSecurityContext().

◆ SECUR32_arc4Init()

void SECUR32_arc4Init ( arc4_info a4i,
const BYTE key,
unsigned int  keyLen 
)

Definition at line 199 of file util.c.

200 {
201  unsigned int keyIndex = 0, stateIndex = 0;
202  unsigned int i, a;
203 
204  TRACE("(%p, %p, %d)\n", a4i, key, keyLen);
205 
206  a4i->x = a4i->y = 0;
207 
208  for (i=0; i<256; i++)
209  a4i->state[i] = i;
210 
211  for (i=0; i<256; i++)
212  {
213  a = a4i->state[i];
214  stateIndex += key[keyIndex] + a;
215  stateIndex &= 0xff;
216  a4i->state[i] = a4i->state[stateIndex];
217  a4i->state[stateIndex] = a;
218  if (++keyIndex >= keyLen)
219  keyIndex = 0;
220  }
221 
222 }
unsigned char x
Definition: ntlm.h:17
unsigned char state[256]
Definition: ntlm.h:18
#define a
Definition: ke_i.h:78
#define TRACE(s)
Definition: solgame.cpp:4
#define for
Definition: utility.h:88
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
unsigned char y
Definition: ntlm.h:17
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
Definition: path.c:41

Referenced by ntlm_AcceptSecurityContext(), and ntlm_InitializeSecurityContextW().

◆ SECUR32_arc4Process()

void SECUR32_arc4Process ( arc4_info a4i,
BYTE inoutString,
unsigned int  length 
)

Definition at line 224 of file util.c.

225 {
226  BYTE *const s=a4i->state;
227  unsigned int x = a4i->x;
228  unsigned int y = a4i->y;
229  unsigned int a, b;
230 
231  while(length--)
232  {
233  x = (x+1) & 0xff;
234  a = s[x];
235  y = (y+a) & 0xff;
236  b = s[y];
237  s[x] = b;
238  s[y] = a;
239  *inoutString++ ^= s[(a+b) & 0xff];
240  }
241 
242  a4i->x = x;
243  a4i->y = y;
244 }
unsigned char x
Definition: ntlm.h:17
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
unsigned char state[256]
Definition: ntlm.h:18
#define a
Definition: ke_i.h:78
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define b
Definition: ke_i.h:79
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLdouble s
Definition: gl.h:2039
unsigned char BYTE
Definition: xxhash.c:193
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
unsigned char y
Definition: ntlm.h:17
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204

Referenced by ntlm_CreateSignature(), ntlm_DecryptMessage(), and ntlm_EncryptMessage().

◆ SECUR32_CalcNTLM2Subkey()

static void SECUR32_CalcNTLM2Subkey ( const BYTE session_key,
const char magic,
PBYTE  subkey 
)
static

Definition at line 144 of file util.c.

145 {
146  MD5_CTX ctx;
147 
148  MD5Init(&ctx);
149  MD5Update(&ctx, session_key, 16);
150  MD5Update(&ctx, (const unsigned char*)magic, lstrlenA(magic)+1);
151  MD5Final(&ctx);
152  memcpy(subkey, ctx.digest, 16);
153 }
VOID WINAPI MD5Final(MD5_CTX *ctx)
Definition: md5.c:113
VOID WINAPI MD5Update(MD5_CTX *ctx, const unsigned char *buf, unsigned int len)
Definition: md5.c:59
u32_t magic(void)
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
int WINAPI lstrlenA(LPCSTR lpString)
Definition: lstring.c:145
VOID WINAPI MD5Init(MD5_CTX *ctx)
Definition: md5.c:45
Definition: msi.c:4078

Referenced by SECUR32_CreateNTLM2SubKeys().

◆ SECUR32_CreateNTLM1SessionKey()

SECURITY_STATUS SECUR32_CreateNTLM1SessionKey ( PBYTE  password,
int  len,
PBYTE  session_key 
)

Definition at line 122 of file util.c.

123 {
124  MD4_CTX ctx;
125  BYTE ntlm_hash[16];
126 
127  TRACE("(%p, %p)\n", password, session_key);
128 
129  MD4Init(&ctx);
131  MD4Final(&ctx);
132 
133  memcpy(ntlm_hash, ctx.digest, 0x10);
134 
135  MD4Init(&ctx);
136  MD4Update(&ctx, ntlm_hash, 0x10u);
137  MD4Final(&ctx);
138 
139  memcpy(session_key, ctx.digest, 0x10);
140 
141  return SEC_E_OK;
142 }
VOID WINAPI MD4Final(MD4_CTX *ctx)
Definition: md4.c:113
VOID WINAPI MD4Init(MD4_CTX *ctx)
Definition: md4.c:45
#define TRACE(s)
Definition: solgame.cpp:4
T1_FIELD_DICT_PRIVATE password
Definition: t1tokens.h:64
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei len
Definition: glext.h:6722
#define SEC_E_OK
Definition: winerror.h:2356
unsigned char BYTE
Definition: xxhash.c:193
Definition: util.c:81
VOID WINAPI MD4Update(MD4_CTX *ctx, const unsigned char *buf, unsigned int len)
Definition: md4.c:59

Referenced by ntlm_InitializeSecurityContextW().

◆ SECUR32_CreateNTLM2SubKeys()

SECURITY_STATUS SECUR32_CreateNTLM2SubKeys ( PNegoHelper  helper)

Definition at line 156 of file util.c.

157 {
158  helper->crypt.ntlm2.send_sign_key = HeapAlloc(GetProcessHeap(), 0, 16);
159  helper->crypt.ntlm2.send_seal_key = HeapAlloc(GetProcessHeap(), 0, 16);
160  helper->crypt.ntlm2.recv_sign_key = HeapAlloc(GetProcessHeap(), 0, 16);
161  helper->crypt.ntlm2.recv_seal_key = HeapAlloc(GetProcessHeap(), 0, 16);
162 
163  if(helper->mode == NTLM_CLIENT)
164  {
166  helper->crypt.ntlm2.send_sign_key);
168  helper->crypt.ntlm2.send_seal_key);
170  helper->crypt.ntlm2.recv_sign_key);
172  helper->crypt.ntlm2.recv_seal_key);
173  }
174  else
175  {
177  helper->crypt.ntlm2.send_sign_key);
179  helper->crypt.ntlm2.send_seal_key);
181  helper->crypt.ntlm2.recv_sign_key);
183  helper->crypt.ntlm2.recv_seal_key);
184  }
185 
186  return SEC_E_OK;
187 }
struct _NegoHelper::@539 crypt
HelperMode mode
Definition: ntlm.h:27
static const char server_to_client_sign_constant[]
Definition: util.c:78
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
static void SECUR32_CalcNTLM2Subkey(const BYTE *session_key, const char *magic, PBYTE subkey)
Definition: util.c:144
#define SEC_E_OK
Definition: winerror.h:2356
BYTE * session_key
Definition: ntlm.h:36
static const char client_to_server_seal_constant[]
Definition: util.c:77
static const char server_to_client_seal_constant[]
Definition: util.c:79
static const char client_to_server_sign_constant[]
Definition: util.c:76

Referenced by ntlm_InitializeSecurityContextW().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( ntlm  )

Variable Documentation

◆ client_to_server_seal_constant

const char client_to_server_seal_constant[] = "session key to client-to-server sealing key magic constant"
static

Definition at line 77 of file util.c.

Referenced by SECUR32_CreateNTLM2SubKeys().

◆ client_to_server_sign_constant

const char client_to_server_sign_constant[] = "session key to client-to-server signing key magic constant"
static

Definition at line 76 of file util.c.

Referenced by SECUR32_CreateNTLM2SubKeys().

◆ CRC_table

const ULONG CRC_table[256]
static

Definition at line 29 of file util.c.

Referenced by ComputeCrc32().

◆ server_to_client_seal_constant

const char server_to_client_seal_constant[] = "session key to server-to-client sealing key magic constant"
static

Definition at line 79 of file util.c.

Referenced by SECUR32_CreateNTLM2SubKeys().

◆ server_to_client_sign_constant

const char server_to_client_sign_constant[] = "session key to server-to-client signing key magic constant"
static

Definition at line 78 of file util.c.

Referenced by SECUR32_CreateNTLM2SubKeys().