diff --git a/pkgs/tools/security/hashcat/default.nix b/pkgs/tools/security/hashcat/default.nix index 55201b3d84835..5c471f3e2b2b0 100644 --- a/pkgs/tools/security/hashcat/default.nix +++ b/pkgs/tools/security/hashcat/default.nix @@ -1,53 +1,149 @@ -{ lib, stdenv -, addDriverRunpath -, config -, cudaPackages ? {} -, cudaSupport ? config.cudaSupport -, fetchurl -, makeWrapper -, opencl-headers -, ocl-icd -, xxHash -, Foundation, IOKit, Metal, OpenCL, libiconv +{ + lib, + stdenv, + + nix-update-script, + addDriverRunpath, + fetchFromGitHub, + makeWrapper, + xxHash, + autoAddDriverRunpath, + + # test foo + testers, + nixosTests, + hashcat, + # test foo stop + + # cuda foo + hashcat-cuda ? config.cudaSupport, + cudaSupport ? config.cudaSupport, + cudaPackages ? { }, + # cuda foo stop + + # ROCm foo + hashcat-rocm ? config.rocmSupport, + rocmSupport ? config.rocmSupport, + # buildEnv, + # linkFarm, + rocmPackages ? { }, + # ROCm foo stop + + # opencl foo + ocl-icd, + opencl-headers, + # opencl foo stop + + # Darwin / arm foo stop + OpenCL ? stdenv.hostPlatform.isDarwin, + Foundation ? stdenv.hostPlatform.isDarwin, + IOKit ? stdenv.hostPlatform.isDarwin, + Metal ? stdenv.hostPlatform.isDarwin, + libiconv ? stdenv.hostPlatform.isDarwin, + # Darwin / arm foo stop + + config, + # one of `"opencl" "rocm" "cuda" ]` + acceleration ? null, }: -stdenv.mkDerivation rec { - pname = "hashcat"; +assert builtins.elem acceleration [ + null + false + "rocm" + "cuda" +]; + +let + pname = "hashcat"; version = "6.2.6"; - src = fetchurl { - url = "https://hashcat.net/files/hashcat-${version}.tar.gz"; - sha256 = "sha256-sl4Qd7zzSQjMjxjBppouyYsEeyy88PURRNzzuh4Leyo="; + src = fetchFromGitHub { + owner = "hashcat"; + repo = "${pname}"; + rev = "6716447dfce969ddde42a9abe0681500bee0df48"; + hash = "sha256-adDVPpeVAw917IpFeOq2FT/VUaKHQh1k8ljgDEcWi/k="; }; - postPatch = '' - # MACOSX_DEPLOYMENT_TARGET is defined by the enviroment - # Remove hardcoded paths on darwin - substituteInPlace src/Makefile \ - --replace "export MACOSX_DEPLOYMENT_TARGET" "#export MACOSX_DEPLOYMENT_TARGET" \ - --replace "/usr/bin/ar" "ar" \ - --replace "/usr/bin/sed" "sed" \ - --replace '-i ""' '-i' - ''; + validateFallback = lib.warnIf (config.rocmSupport && config.cudaSupport) (lib.concatStrings [ + "both `nixpkgs.config.rocmSupport` and `nixpkgs.config.cudaSupport` are enabled, " + "but they are mutually exclusive; falling back to cpu" + ]) (!(config.rocmSupport && config.cudaSupport)); + + rocmRequested = shouldEnable "rocm" rocmSupport; + cudaRequested = shouldEnable "cuda" cudaSupport; + shouldEnable = + mode: fallback: (acceleration == mode) || (fallback && acceleration == null && validateFallback); - nativeBuildInputs = [ - makeWrapper - ] ++ lib.optionals cudaSupport [ - addDriverRunpath + enableRocm = rocmRequested && stdenv.hostPlatform.isLinux; + enableCuda = cudaRequested && stdenv.hostPlatform.isLinux; + + openclLibs = [ + ocl-icd + opencl-headers + ]; + + DarwinLibs = [ + OpenCL + Foundation + IOKit + Metal + libiconv ]; - buildInputs = [ opencl-headers xxHash ] - ++ lib.optionals stdenv.hostPlatform.isDarwin [ Foundation IOKit Metal OpenCL libiconv ]; - - makeFlags = [ - "PREFIX=${placeholder "out"}" - "COMPTIME=1337" - "VERSION_TAG=${version}" - "USE_SYSTEM_OPENCL=1" - "USE_SYSTEM_XXHASH=1" - ] ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform == stdenv.buildPlatform) [ - "IS_APPLE_SILICON='${if stdenv.hostPlatform.isAarch64 then "1" else "0"}'" + rocmLibs = [ + rocmPackages.clr + rocmPackages.hipblas + rocmPackages.rocblas + rocmPackages.rocsolver + rocmPackages.rocsparse + rocmPackages.rocm-device-libs + rocmPackages.rocm-smi ]; + # rocmClang = linkFarm "rocm-clang" { llvm = rocmPackages.llvm.clang; }; + # rocmPath = buildEnv { + # name = "rocm-path"; + # paths = rocmLibs ++ [ rocmClang ]; + # }; + + cudaLibs = [ addDriverRunpath ]; + +in +stdenv.mkDerivation { + inherit pname version src; + nativeBuildInputs = + [ + makeWrapper + autoAddDriverRunpath + ] + ++ [ openclLibs ] + ++ lib.optionals enableRocm [ rocmLibs ] + ++ lib.optionals enableCuda [ cudaLibs ]; + + buildInputs = + [ + xxHash + ] + ++ [ openclLibs ] + ++ lib.optionals stdenv.hostPlatform.isDarwin [ DarwinLibs ] + ++ lib.optionals rocmRequested [ rocmLibs ] + ++ lib.optionals cudaRequested [ cudaLibs ]; + + patches = lib.optionals enableRocm [ + ./rocm1.patch + ./rocm2.patch + ]; + makeFlags = + [ + "PREFIX=${placeholder "out"}" + "COMPTIME=1337" + "VERSION_TAG=${version}" + "USE_SYSTEM_OPENCL=1" + "USE_SYSTEM_XXHASH=1" + ] + ++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform == stdenv.buildPlatform) [ + "IS_APPLE_SILICON='${if stdenv.hostPlatform.isAarch64 then "1" else "0"}'" + ]; enableParallelBuilding = true; @@ -59,28 +155,68 @@ stdenv.mkDerivation rec { done ''; - postFixup = let - LD_LIBRARY_PATH = builtins.concatStringsSep ":" ([ - "${ocl-icd}/lib" - ] ++ lib.optionals cudaSupport [ - "${cudaPackages.cudatoolkit}/lib" - ]); - in '' - wrapProgram $out/bin/hashcat \ - --prefix LD_LIBRARY_PATH : ${lib.escapeShellArg LD_LIBRARY_PATH} - '' + lib.optionalString cudaSupport '' - for program in $out/bin/hashcat $out/bin/.hashcat-wrapped; do - isELF "$program" || continue - addDriverRunpath "$program" - done - ''; + postFixup = + let + LD_LIBRARY_PATH = builtins.concatStringsSep ":" ( + [ + "${ocl-icd}/lib" + ] + ++ lib.optionals cudaRequested [ + "${cudaPackages.cudatoolkit}/lib" + ] + ++ lib.optionals rocmRequested [ + "${rocmPackages.rocblas}/lib" + ] + ); + in + '' + wrapProgram $out/bin/hashcat \ + --prefix LD_LIBRARY_PATH : ${lib.escapeShellArg LD_LIBRARY_PATH} + '' + + lib.optionalString cudaRequested '' + for program in $out/bin/hashcat $out/bin/.hashcat-wrapped; do + isELF "$program" || continue + addDriverRunpath "$program" + done + '' + + lib.optionalString rocmRequested '' + for program in $out/bin/hashcat $out/bin/.hashcat-wrapped; do + isELF "$program" || continue + addDriverRunpath "$program" + done + ''; + + passthru = { + tests = + { + inherit hashcat; + version = testers.testVersion { + inherit version; + package = hashcat; + }; + } + // lib.optionalAttrs stdenv.hostPlatform.isLinux { + inherit hashcat-rocm hashcat-cuda; + service = nixosTests.hashcat; + service-cuda = nixosTests.hashcat-cuda; + service-rocm = nixosTests.hashcat-rocm; + }; + } // lib.optionalAttrs (!enableRocm && !enableCuda) { updateScript = nix-update-script { }; }; meta = with lib; { - description = "Fast password cracker"; + description = + "Fast password cracker" + + lib.optionalString rocmRequested ", usng ROCM for AMD GPU acceleration" + + lib.optionalString cudaRequested ", usng CUDA for NVIDIA GPU acceleration"; mainProgram = "hashcat"; - homepage = "https://hashcat.net/hashcat/"; - license = licenses.mit; - platforms = platforms.unix; - maintainers = with maintainers; [ felixalbrigtsen zimbatm ]; + changelog = "https://github.com/hashcat/hashcat/releases/tag/${version}"; + homepage = "https://hashcat.net/hashcat/"; + license = licenses.mit; + platforms = if (rocmRequested || cudaRequested) then platforms.linux else platforms.unix; + maintainers = with maintainers; [ + felixalbrigtsen + zimbatm + Makuru + ]; }; } diff --git a/pkgs/tools/security/hashcat/rocm1.patch b/pkgs/tools/security/hashcat/rocm1.patch new file mode 100644 index 0000000000000..e8a6bd82ca2e1 --- /dev/null +++ b/pkgs/tools/security/hashcat/rocm1.patch @@ -0,0 +1,83 @@ +From 08f6cf7e0af48cfb1ec01e42f0f072cf8455a63c Mon Sep 17 00:00:00 2001 +From: Mayank +Date: Thu, 8 Feb 2024 10:16:25 +0530 +Subject: [PATCH] Updated HIP SDK detection to use ENV variable on Windows + +--- + src/ext_hiprtc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++--- + 1 file changed, 48 insertions(+), 3 deletions(-) + +diff --git a/src/ext_hiprtc.c b/src/ext_hiprtc.c +index ad7d14be9a..8ca3431473 100644 +--- a/src/ext_hiprtc.c ++++ b/src/ext_hiprtc.c +@@ -11,6 +11,42 @@ + + #include "dynloader.h" + ++ ++char* hiprtcDllPath(char* hipSDKPath) ++{ ++ /* ++ AMD HIP RTC DLLs is stored at "C:\Program Files\ROCm\X.Y\bin\hiprtc0X0Y.dll" ++ Tried using regex to simplify, but had compilation issues on mingw64 (linker ++ had troubles with pcre.h) ++ ++ This function can return complete dll path based on major release version ++ X.Y parsed from the ENV variable HIP_PATH. ++ */ ++ ++ // Identifying major release version X.Y ++ char* majorVersion = malloc(strlen("X.Y")+1); ++ memcpy(majorVersion, hipSDKPath + (strlen(hipSDKPath) - 4), 3); ++ memcpy(majorVersion+0x3, "\0", 1); ++ ++ // Preparing DLL name "hiprtc0X0Y.dll" ++ char* hiprtcDllName = malloc(strlen("hiprtcXXXX.dll")+1); ++ memcpy(hiprtcDllName, "hiprtc0", strlen("hiprtc0")); ++ ++ memcpy(hiprtcDllName + 0x7, majorVersion, 1); ++ memcpy(hiprtcDllName + 0x8, "0", 1); ++ memcpy(hiprtcDllName + 0x9, majorVersion + 2, 1); ++ memcpy(hiprtcDllName + 0xa, ".dll\0", 5); ++ ++ // Preparing complete path as "C:\Program Files\ROCm\X.Y\bin\hiprtc0X0Y.dll" to ++ // return to the caller. ++ char* hiprtcDllPath = malloc(strlen(hipSDKPath) + strlen("bin/") + strlen("hiprtcXXXX.dll") + 1); ++ strcpy(hiprtcDllPath, hipSDKPath); ++ strcat(hiprtcDllPath, "bin\\"); ++ strcat(hiprtcDllPath, hiprtcDllName); ++ return(hiprtcDllPath); ++} ++ ++ + int hiprtc_make_options_array_from_string (char *string, char **options) + { + char *saveptr = NULL; +@@ -41,14 +77,23 @@ int hiprtc_init (void *hashcat_ctx) + #if defined (_WIN) + hiprtc->lib = hc_dlopen ("hiprtc.dll"); + +- if (hiprtc->lib == NULL) hiprtc->lib = hc_dlopen ("C:/Program Files/AMD/ROCm/5.5/bin/hiprtc0505.dll"); ++ // Check for HIP SDK installation from ENV ++ const char* hipSDKPath = getenv("HIP_PATH"); ++ if (hipSDKPath != NULL && hiprtc->lib == NULL) ++ { ++ hiprtc->lib = hc_dlopen (hiprtcDllPath(hipSDKPath)); ++ } + if (hiprtc->lib == NULL) hiprtc->lib = hc_dlopen ("amdhip64.dll"); + #elif defined (__APPLE__) + hiprtc->lib = hc_dlopen ("fixme.dylib"); + #elif defined (__CYGWIN__) + hiprtc->lib = hc_dlopen ("hiprtc.dll"); +- +- if (hiprtc->lib == NULL) hiprtc->lib = hc_dlopen ("C:/Program Files/AMD/ROCm/5.5/bin/hiprtc0505.dll"); ++ // Check for HIP SDK installation from ENV ++ const char* hipSDKPath = getenv("HIP_PATH"); ++ if (hipSDKPath != NULL && hiprtc->lib == NULL) ++ { ++ hiprtc->lib = hc_dlopen (hiprtcDllPath(hipSDKPath)); ++ } + if (hiprtc->lib == NULL) hiprtc->lib = hc_dlopen ("amdhip64.dll"); + #else + hiprtc->lib = hc_dlopen ("libhiprtc.so"); diff --git a/pkgs/tools/security/hashcat/rocm2.patch b/pkgs/tools/security/hashcat/rocm2.patch new file mode 100644 index 0000000000000..4f78d100d849e --- /dev/null +++ b/pkgs/tools/security/hashcat/rocm2.patch @@ -0,0 +1,23 @@ +From 992f1c13ba85c73fd4497b0c7cdeac85a7c01821 Mon Sep 17 00:00:00 2001 +From: Mayank +Date: Fri, 9 Feb 2024 23:15:54 +0530 +Subject: [PATCH] Removed -nocudalib from hiprtc_options to fix LLVMBitcode + compilation error when using HIP Backend + +--- + src/backend.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/src/backend.c b/src/backend.c +index f7c916e1db..d4cf54be45 100644 +--- a/src/backend.c ++++ b/src/backend.c +@@ -8755,7 +8755,7 @@ static bool load_kernel (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_p + */ + + hiprtc_options[1] = "-nocudainc"; +- hiprtc_options[2] = "-nocudalib"; ++ hiprtc_options[2] = ""; + hiprtc_options[3] = ""; + hiprtc_options[4] = ""; + diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 582bb41a74bc6..6e78f23419794 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -3737,8 +3737,12 @@ with pkgs; haguichi = callPackage ../tools/networking/haguichi { }; hashcat = callPackage ../tools/security/hashcat { - inherit (darwin.apple_sdk.frameworks) Foundation IOKit Metal OpenCL; + acceleration = null; + inherit (darwin.apple_sdk.frameworks) IOKit Metal OpenCL; }; + hashcat-rocm = callPackage ../tools/security/hashcat { acceleration = "rocm"; }; + hashcat-cuda = callPackage ../tools/security/hashcat { acceleration = "cuda"; }; + haskell-language-server = callPackage ../development/tools/haskell/haskell-language-server/withWrapper.nix { };