BSOne.SFC/Tocsg.Module/KessDRM/Tocsg.Kess.pas

174 lines
6.2 KiB
Plaintext

{*******************************************************}
{ }
{ Tocsg.Kess }
{ }
{ Copyright (C) 2024 kku }
{ }
{*******************************************************}
unit Tocsg.Kess;
interface
uses
System.SysUtils, Winapi.Windows;
const
{$IFDEF WIN64}
DLL_KESS = 'KESS_Crypto_x64.dll';
{$ELSE}
DLL_KESS = 'KESS_Crypto.dll';
{$ENDIF}
//------------------------------------------------------------------
// Error Code
//------------------------------------------------------------------
RESULT_SUCCESS = $00000000;
RESULT_FAIL = $FFFFFFFF;
FAIL_NEED_CALL_INIT = $C0000099;
//-- Init 100
FAIL_INIT_KC_INIT = $C0000100;
FAIL_INIT_SIZE = $C0000101;
FAIL_INIT_PATH = $C0000102;
FAIL_INIT_EXIST = $C0000103;
FAIL_INIT_LOAD = $C0000104;
FAIL_INIT_TEMPDIR = $C0000105;
FAIL_INIT_LICENSE = $C0000106;
FAIL_INIT_JNI = $C0000107;
FAIL_INIT_THIRD_PARTY_LICENSE = $C0000108;
//-- IsEnc 300
FAIL_ISENC_NOT = $C0000300;
FAIL_ISENC_PATH = $C0000301;
FAIL_ISENC_SEQSIZE = $C0000302;
FAIL_ISENC_FILESIZE = $C0000303;
FAIL_ISENC_OPEN = $C0000304;
FAIL_ISENC_MEMCMP = $C0000305;
FAIL_ISENC_WRITEOPEN = $C0000306;
FAIL_ISENC_JNI = $C0000307;
//-- DoEnc 400
FAIL_DOENC_PATH = $C0000400;
FAIL_DOENC_SIZE = $C0000401;
FAIL_DOENC_ISENC = $C0000402;
FAIL_DOENC_DOHASH = $C0000403;
FAIL_DOENC_CREATE_DIRECTORY = $C0000404;
FAIL_DOENC_CREATE_FILE = $C0000405;
FAIL_DOENC_KC_SK_KEYGEN = $C0000406;
FAIL_DOENC_OPEN_FILE = $C0000407;
FAIL_DOENC_READ_FILE = $C0000408;
FAIL_DOENC_COPY_FILE = $C0000409;
FAIL_DOENC_WRITE_FILE = $C0000410;
FAIL_DOENC_ALREADY_FILE_ENCRYPTED = $C0000411;
FAIL_DOENC_JNI = $C0000412;
//-- DoDec 500
FAIL_DODEC_PATH = $C0000501;
FAIL_DODEC_SIZE = $C0000502;
FAIL_DODEC_KC_SK_DEC = $C0000503;
FAIL_DODEC_CREATE_DIRECTORY = $C0000504;
FAIL_DODEC_CREATE_FILE = $C0000505;
FAIL_DODEC_ISENC = $C0000506;
FAIL_DODEC_GETENCINFO = $C0000507;
FAIL_DODEC_HASH_VERIFY = $C0000508;
FAIL_DODEC_DOHASH = $C0000509;
FAIL_DODEC_OPEN_FILE = $C0000510;
FAIL_DODEC_READ_FILE = $C0000511;
FAIL_DODEC_LICENSE_VERIFY = $C0000512;
FAIL_DODEC_TEMPPATH = $C0000513;
FAIL_DODEC_FILESIZE = $C0000514;
FAIL_DODEC_COPY_FILE = $C0000515;
FAIL_DODEC_WRONG_OPTION = $C0000516;
FAIL_DODEC_WRITE_FILE = $C0000517;
FAIL_DODEC_JNI = $C0000518;
type
TKCT_Init = function(sInitPath, sLicense: PChar): DWORD; stdcall;
TKCT_Clear = procedure; stdcall;
TKCT_IsEncrypt = function(sPath: PChar): DWORD; stdcall;
TKCT_Encrypt = function(sSrcPath: PChar; nOption: DWORD; sDstPath: PChar; nDstPathLen: DWORD; pCallback: Pointer): DWORD; stdcall;
TKCT_Decrypt = function(sSrcPath: PChar; nOption: DWORD; sDstPath: PChar; nDstPathLen: DWORD; pCallback: Pointer): DWORD; stdcall;
function KCT_Init(sInitPath, sLicense: String): DWORD;
procedure KCT_Clear;
function KCT_IsEncrypt(sPath: String): DWORD;
function KCT_Encrypt(sSrcPath: String; dwOption: DWORD; sDstPath: String): DWORD;
function KCT_Decrypt(sSrcPath: String; dwOption: DWORD; sDstPath: String): DWORD;
implementation
var
_hKess: THandle = 0;
_bInit: Boolean = false;
_fnKCT_Init: TKCT_Init = nil;
_fnKCT_Clear: TKCT_Clear = nil;
_fnKCT_IsEncrypt: TKCT_IsEncrypt = nil;
_fnKCT_Encrypt: TKCT_Encrypt = nil;
_fnKCT_Decrypt: TKCT_Decrypt = nil;
function InitKessProcedure(sDllPath: String): Boolean;
begin
if _hKess = 0 then
begin
_hKess := LoadLibrary(PChar(sDllPath));
if _hKess <> 0 then
begin
@_fnKCT_Init := GetProcAddress(_hKess, 'KCT_Init');
@_fnKCT_Clear := GetProcAddress(_hKess, 'KCT_Clear');
@_fnKCT_IsEncrypt := GetProcAddress(_hKess, 'KCT_IsEncrypt');
@_fnKCT_Encrypt := GetProcAddress(_hKess, 'KCT_Encrypt');
@_fnKCT_Decrypt := GetProcAddress(_hKess, 'KCT_Decrypt');
end;
end;
Result := _hKess <> 0;
end;
function KCT_Init(sInitPath, sLicense: String): DWORD;
begin
Result := 9797;
if _bInit then
Result := 0
else
if not _bInit and
InitKessProcedure(IncludeTrailingPathDelimiter(sInitPath) + DLL_KESS) and
Assigned(_fnKCT_Init) then
begin
Result := _fnKCT_Init(PChar(sInitPath), PChar(sLicense));
_bInit := Result = RESULT_SUCCESS;
end;
end;
procedure KCT_Clear;
begin
if _bInit and Assigned(_fnKCT_Clear) then
_fnKCT_Clear;
end;
function KCT_IsEncrypt(sPath: String): DWORD;
begin
Result := 9797;
if _bInit and Assigned(_fnKCT_IsEncrypt) then
Result := _fnKCT_IsEncrypt(PChar(sPath));
end;
// nOption : 0 - 대상 파일을 암호화할 경우, 2 - 암호화된 파일을 지정한 경로에 생성할 경우
function KCT_Encrypt(sSrcPath: String; dwOption: DWORD; sDstPath: String): DWORD;
begin
Result := 9797;
if _bInit and Assigned(_fnKCT_Encrypt) then
Result := _fnKCT_Encrypt(PChar(sSrcPath), dwOption, PChar(sDstPath), Length(sDstPath), nil);
end;
// dwOption : 0 - 대상 파일을 복호화할 경우, 1 - 복호화된 파일을 대상 파일와 동일한 경로에 생성할 경우 ex> " k_new_[기존파일 명].kenc" 확장자로 생성
// 2 - 복호화된 파일을 지정한 경로에 생성할 경우
function KCT_Decrypt(sSrcPath: String; dwOption: DWORD; sDstPath: String): DWORD;
begin
Result := 9797;
if _bInit and Assigned(_fnKCT_Decrypt) then
Result := _fnKCT_Decrypt(PChar(sSrcPath), dwOption, PChar(sDstPath), Length(sDstPath), nil);
end;
end.