20 #include <winpr/config.h>
22 #include <winpr/ntlm.h>
23 #include <winpr/assert.h>
25 #include <winpr/crt.h>
26 #include <winpr/crypto.h>
34 BOOL NTOWFv1W(LPWSTR Password, UINT32 PasswordLength, BYTE* NtHash)
36 if (!Password || !NtHash)
39 if (!winpr_Digest(WINPR_MD_MD4, (BYTE*)Password, (
size_t)PasswordLength, NtHash,
40 WINPR_MD4_DIGEST_LENGTH))
46 BOOL NTOWFv1A(LPSTR Password, UINT32 PasswordLength, BYTE* NtHash)
48 LPWSTR PasswordW = NULL;
50 size_t pwdCharLength = 0;
55 PasswordW = ConvertUtf8NToWCharAlloc(Password, PasswordLength, &pwdCharLength);
59 if (!NTOWFv1W(PasswordW, (UINT32)pwdCharLength *
sizeof(WCHAR), NtHash))
75 BOOL NTOWFv2W(LPWSTR Password, UINT32 PasswordLength, LPWSTR User, UINT32 UserLength, LPWSTR Domain,
76 UINT32 DomainLength, BYTE* NtHash)
78 BYTE NtHashV1[WINPR_MD5_DIGEST_LENGTH];
80 if ((!User) || (!Password) || (!NtHash))
83 if (!NTOWFv1W(Password, PasswordLength, NtHashV1))
86 return NTOWFv2FromHashW(NtHashV1, User, UserLength, Domain, DomainLength, NtHash);
89 BOOL NTOWFv2A(LPSTR Password, UINT32 PasswordLength, LPSTR User, UINT32 UserLength, LPSTR Domain,
90 UINT32 DomainLength, BYTE* NtHash)
93 LPWSTR DomainW = NULL;
94 LPWSTR PasswordW = NULL;
96 size_t userCharLength = 0;
97 size_t domainCharLength = 0;
98 size_t pwdCharLength = 0;
103 UserW = ConvertUtf8NToWCharAlloc(User, UserLength, &userCharLength);
104 DomainW = ConvertUtf8NToWCharAlloc(Domain, DomainLength, &domainCharLength);
105 PasswordW = ConvertUtf8NToWCharAlloc(Password, PasswordLength, &pwdCharLength);
107 if (!UserW || !DomainW || !PasswordW)
110 if (!NTOWFv2W(PasswordW, (UINT32)pwdCharLength *
sizeof(WCHAR), UserW,
111 (UINT32)userCharLength *
sizeof(WCHAR), DomainW,
112 (UINT32)domainCharLength *
sizeof(WCHAR), NtHash))
123 BOOL NTOWFv2FromHashW(BYTE* NtHashV1, LPWSTR User, UINT32 UserLength, LPWSTR Domain,
124 UINT32 DomainLength, BYTE* NtHash)
129 if (!User || !NtHash)
132 if (!(buffer = (BYTE*)malloc(UserLength + DomainLength)))
136 CopyMemory(buffer, User, UserLength);
137 CharUpperBuffW((LPWSTR)buffer, UserLength / 2);
139 if (DomainLength > 0)
141 CopyMemory(&buffer[UserLength], Domain, DomainLength);
146 if (!winpr_HMAC(WINPR_MD_MD5, NtHashV1, 16, buffer, UserLength + DomainLength, NtHash,
147 WINPR_MD5_DIGEST_LENGTH))
156 BOOL NTOWFv2FromHashA(BYTE* NtHashV1, LPSTR User, UINT32 UserLength, LPSTR Domain,
157 UINT32 DomainLength, BYTE* NtHash)
160 LPWSTR DomainW = NULL;
162 size_t userCharLength = 0;
163 size_t domainCharLength = 0;
167 UserW = ConvertUtf8NToWCharAlloc(User, UserLength, &userCharLength);
168 DomainW = ConvertUtf8NToWCharAlloc(Domain, DomainLength, &domainCharLength);
170 if (!UserW || !DomainW)
173 if (!NTOWFv2FromHashW(NtHashV1, UserW, (UINT32)userCharLength *
sizeof(WCHAR), DomainW,
174 (UINT32)domainCharLength *
sizeof(WCHAR), NtHash))