From 7ab00af506b97f4bd2e5317615045efc3287ab68 Mon Sep 17 00:00:00 2001
From: Gabriele Gristina <matrix@users.noreply.github.com>
Date: Sun, 28 May 2023 21:21:20 +0200
Subject: [PATCH] Support building windows binaries on macOS using MinGW,
 updated documentation

---
 BUILD.md         |  6 +++++-
 BUILD_macOS.md   | 27 +++++++++++++++++++++++++++
 docs/changes.txt |  2 ++
 src/Makefile     | 47 +++++++++++++++++++++++++++++++++++++----------
 4 files changed, 71 insertions(+), 11 deletions(-)
 create mode 100644 BUILD_macOS.md

diff --git a/BUILD.md b/BUILD.md
index 333b8ec81..b552f48bc 100644
--- a/BUILD.md
+++ b/BUILD.md
@@ -3,7 +3,7 @@ hashcat build documentation
 
 ### Revision ###
 
-* 1.5
+* 1.6
 
 ### Author ###
 
@@ -61,6 +61,10 @@ Otherwise:
 - Cached kernels go to: $HOME/.cache/hashcat
 - Potfiles go to: $HOME/.local/share/hashcat/
 
+### Building hashcat for Windows (using macOS) ###
+
+Refer to [BUILD_macOS.md](BUILD_macOS.md)
+
 ### Building hashcat for Windows (using Windows Subsystem for Linux) ###
 
 Refer to [BUILD_WSL.md](BUILD_WSL.md)
diff --git a/BUILD_macOS.md b/BUILD_macOS.md
new file mode 100644
index 000000000..67d09a6bf
--- /dev/null
+++ b/BUILD_macOS.md
@@ -0,0 +1,27 @@
+# Compiling hashcat for Windows with macOS.
+
+Tested on macOS 12.6.6 M1.
+
+Make sure to have the HomeBrew upgraded.
+
+### Installation ###
+
+```
+brew install mingw-w64
+git clone https://github.com/hashcat/hashcat
+git clone https://github.com/win-iconv/win-iconv
+cd win-iconv/
+patch < ../hashcat/tools/win-iconv-64.diff
+sudo make install
+cd ../
+```
+
+### Building ###
+
+You've already cloned the latest master revision of hashcat repository above, so switch to the folder and type "make win" to start compiling hashcat
+```
+cd hashcat/
+make win
+```
+
+The process may take a while, please be patient.
diff --git a/docs/changes.txt b/docs/changes.txt
index f6be03171..907fa88f7 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -95,7 +95,9 @@
 - Apple Driver: Updated requirements to use Apple OpenCL API to macOS 13.0 - use
 - Backend Checks: Describe workaround in error message when detecting more than 64 backend devices
 - Brain: Added sanity check and corresponding error message for invalid --brain-port values
+- Building: Support building windows binaries on macOS using MinGW
 - Dependencies: Updated OpenCL-Headers to v2023.04.17
+- Documents: Updated BUILD.md and added BUILD_macOS.md (containing instructions for building windows binaries on macOS)
 - Modules: Added support for non-zero IVs for -m 6800 (Lastpass). Also added `tools/lastpass2hashcat.py`
 - Open Document Format: Added support for small documents with content length < 1024
 - Status Code: Add specific return code for self-test fail (-11)
diff --git a/src/Makefile b/src/Makefile
index 1e60bfdc7..9fc12a311 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -407,10 +407,6 @@ EMU_OBJS_ALL            += emu_inc_hash_base58
 
 OBJS_ALL                := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_hip ext_nvapi ext_nvml ext_nvrtc ext_hiprtc ext_OpenCL ext_sysfs_amdgpu ext_sysfs_cpu ext_iokit ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL)
 
-ifeq ($(UNAME),Darwin)
-OBJS_ALL                += ext_metal
-endif
-
 ifeq ($(ENABLE_BRAIN),1)
 OBJS_ALL                += brain
 endif
@@ -419,6 +415,12 @@ NATIVE_OBJS             := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.o)
 LINUX_OBJS              := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).LINUX.o)
 WIN_OBJS                := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).WIN.o)
 
+ifeq ($(UNAME),Darwin)
+OBJS_METAL              := ext_metal
+
+NATIVE_OBJS             += $(foreach OBJ,$(OBJS_METAL),obj/$(OBJ).METAL.NATIVE.o)
+endif
+
 ifeq ($(USE_SYSTEM_LZMA),0)
 OBJS_LZMA               := 7zCrc 7zCrcOpt 7zFile 7zStream Alloc Bra Bra86 BraIA64 CpuArch Delta LzmaDec Lzma2Dec Sha256 Sha256Opt Xz XzCrc64 XzCrc64Opt XzDec XzIn
 
@@ -618,8 +620,10 @@ uninstall:
 obj/%.NATIVE.o: src/%.c
 	$(CC) -c $(CCFLAGS) $(CFLAGS_NATIVE) $< -o $@ -fpic
 
-obj/%.NATIVE.o: src/%.m
+ifeq ($(UNAME),Darwin)
+obj/%.METAL.NATIVE.o: src/%.m
 	$(CC) -c $(CCFLAGS) $(CFLAGS_NATIVE) $< -o $@ -fpic
+endif
 
 ifeq ($(USE_SYSTEM_LZMA),0)
 obj/%.LZMA.NATIVE.o: $(DEPS_LZMA_PATH)/%.c
@@ -712,7 +716,7 @@ modules: $(MODULES_LIB)
 ## Cross Compilation (binary release version)
 ##
 
-ifeq ($(UNAME),Linux)
+ifneq (,$(filter $(UNAME),Linux Darwin))
 
 ##
 ## Compiler paths
@@ -758,10 +762,32 @@ LFLAGS_CROSS_WIN        += -lws2_32
 LFLAGS_CROSS_WIN        += -lpowrprof
 LFLAGS_CROSS_WIN        += -static -static-libgcc -static-libstdc++
 
+CFLAGS_LZMA_WIN         := $(CFLAGS_LZMA)
+CFLAGS_UNRAR_WIN        := $(CFLAGS_UNRAR)
+
+ifeq ($(UNAME),Darwin)
+CFLAGS_CROSS_WIN        := $(filter-out -Wno-typedef-redefinition,$(CFLAGS_CROSS_WIN))
+
+CFLAGS_LZMA_WIN         += -Wno-misleading-indentation
+
+CFLAGS_UNRAR_WIN        += -Wno-misleading-indentation
+CFLAGS_UNRAR_WIN        += -Wno-class-memaccess
+endif
+
 ##
 ## Targets
 ##
 
+ifeq ($(UNAME),Darwin)
+.PHONY: binaries
+binaries: win
+
+.PHONY: host_win
+host_win:   hashcat.exe
+
+.PHONY: win
+win:   host_win   modules_win
+else
 .PHONY: binaries
 binaries: linux win
 
@@ -772,6 +798,7 @@ host_win:   hashcat.exe
 .PHONY: linux win
 linux: host_linux modules_linux
 win:   host_win   modules_win
+endif
 
 ##
 ## cross compiled modules
@@ -802,10 +829,10 @@ obj/%.WIN.o:   src/%.c
 
 ifeq ($(USE_SYSTEM_LZMA),0)
 obj/%.LZMA.LINUX.o: $(DEPS_LZMA_PATH)/%.c
-	$(CC_LINUX) $(CCFLAGS) $(CFLAGS_CROSS_LINUX) $(CFLAGS_LZMA) -c -o $@ $<
+	$(CC_LINUX) $(CCFLAGS) $(CFLAGS_CROSS_LINUX) $(CFLAGS_LZMA)     -c -o $@ $<
 
 obj/%.LZMA.WIN.o:   $(DEPS_LZMA_PATH)/%.c
-	$(CC_WIN)   $(CCFLAGS) $(CFLAGS_CROSS_WIN)   $(CFLAGS_LZMA) -c -o $@ $<
+	$(CC_WIN)   $(CCFLAGS) $(CFLAGS_CROSS_WIN)   $(CFLAGS_LZMA_WIN) -c -o $@ $<
 endif
 
 ifeq ($(USE_SYSTEM_ZLIB),0)
@@ -829,10 +856,10 @@ endif
 ifeq ($(ENABLE_UNRAR),1)
 ifeq ($(USE_SYSTEM_UNRAR),0)
 obj/%.UNRAR.LINUX.o: $(DEPS_UNRAR_PATH)/%.cpp
-	$(CXX_LINUX) $(CXXFLAGS) $(CFLAGS_CROSS_LINUX) $(CFLAGS_UNRAR) -c -o $@ $<
+	$(CXX_LINUX) $(CXXFLAGS) $(CFLAGS_CROSS_LINUX) $(CFLAGS_UNRAR)     -c -o $@ $<
 
 obj/%.UNRAR.WIN.o:   $(DEPS_UNRAR_PATH)/%.cpp
-	$(CXX_WIN)   $(CXXFLAGS) $(CFLAGS_CROSS_WIN)   $(CFLAGS_UNRAR) -c -o $@ $<
+	$(CXX_WIN)   $(CXXFLAGS) $(CFLAGS_CROSS_WIN)   $(CFLAGS_UNRAR_WIN) -c -o $@ $<
 endif
 endif