kascpu.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. {
  2. KAScrypt Cryptographic Component Library
  3. -------------------------------------------------------------------------
  4. Detect hardware features
  5. Copyright (C) 2018-2025 Alexander Koblov ([email protected])
  6. Permission is hereby granted, free of charge, to any person obtaining
  7. a copy of this software and associated documentation files (the
  8. "Software"), to deal in the Software without restriction, including
  9. without limitation the rights to use, copy, modify, merge, publish,
  10. distribute, sublicense, and/or sell copies of the Software, and to
  11. permit persons to whom the Software is furnished to do so, subject to
  12. the following conditions:
  13. The above copyright notice and this permission notice shall be included
  14. in all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. }
  23. unit KAScpu;
  24. interface
  25. {$IF DEFINED(CPUAARCH64)}
  26. function SHA256Support: Boolean;
  27. {$ELSEIF DEFINED(CPUX86_64)}
  28. function BMI2Support: LongBool;
  29. function SSSE3Support: LongBool;
  30. function SSE41Support: LongBool;
  31. {$ENDIF}
  32. implementation
  33. {$IF DEFINED(CPUAARCH64)}
  34. {$IF DEFINED(WIN64)}
  35. uses
  36. Windows;
  37. {$ELSEIF DEFINED(UNIX)}
  38. uses
  39. InitC, CTypes;
  40. {$ENDIF}
  41. const
  42. HWCAP_SHA2 = 64;
  43. var
  44. hwcaps: UInt64 = 0;
  45. {$IF DEFINED(LINUX) OR DEFINED(ANDROID)}
  46. const
  47. AT_HWCAP = 16;
  48. function getauxval(type_: culong): culong; cdecl; external clib;
  49. procedure Initialize;
  50. begin
  51. hwcaps:= getauxval(AT_HWCAP);
  52. end;
  53. {$ELSEIF DEFINED(DARWIN)}
  54. function sysctlbyname(const name: pansichar; oldp: pointer; oldlenp: pcsize_t;
  55. const newp: pointer; newlen: csize_t): cint; cdecl; external clib;
  56. procedure Initialize;
  57. var
  58. value: cint;
  59. size: csize_t;
  60. begin
  61. value:= 0;
  62. size:= SizeOf(value);
  63. if sysctlbyname('hw.optional.arm.FEAT_SHA256', @value, @size, nil, 0) = 0 then
  64. begin
  65. if (size > 0) and (value <> 0) then
  66. begin
  67. hwcaps:= hwcaps or HWCAP_SHA2;
  68. end;
  69. end;
  70. end;
  71. {$ELSEIF DEFINED(WIN64)}
  72. const
  73. PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE = 30;
  74. function IsProcessorFeaturePresent(ProcessorFeature: DWORD): BOOL; stdcall; external kernel32;
  75. procedure Initialize;
  76. begin
  77. if IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE) then
  78. hwcaps:= hwcaps or HWCAP_SHA2;
  79. end;
  80. {$ENDIF}
  81. function SHA256Support: Boolean;
  82. begin
  83. Result:= (hwcaps and HWCAP_SHA2) <> 0;
  84. end;
  85. initialization
  86. Initialize;
  87. {$ELSEIF DEFINED(CPUX86_64)}
  88. function BMI2Support: LongBool; assembler; nostackframe;
  89. asm
  90. pushq %rbx
  91. movl $7,%eax
  92. movl $0,%ecx
  93. cpuid
  94. andl $256,%ebx
  95. movl %ebx,%eax
  96. popq %rbx
  97. end;
  98. function SSSE3Support: LongBool; assembler; nostackframe;
  99. asm
  100. pushq %rbx
  101. movl $1,%eax
  102. cpuid
  103. andl $512,%ecx
  104. movl %ecx,%eax
  105. popq %rbx
  106. end;
  107. function SSE41Support: LongBool; assembler; nostackframe;
  108. asm
  109. pushq %rbx
  110. movl $1,%eax
  111. cpuid
  112. andl $0x80000,%ecx
  113. movl %ecx,%eax
  114. popq %rbx
  115. end;
  116. {$ENDIF}
  117. end.