干貨分享 | 一文了解TSMaster中Seed&key的兩種處理方法
在UDS診斷過(guò)程中,會(huì)涉及到安全訪問的問題,也就是常說(shuō)的Seed&Key。TSMaster中提供了兩種 Seed&Key 的處理方法:第一種是直接加載DLL文件;第二種是直接在TSMaster的編譯器中直接添加安全算法。
一、加載外部 Seed&Key DLL
TSMaster 診斷模塊支持通過(guò) dll 載入 Seed&Key 算法,該算法 dll 跟主流工具的計(jì)算接口兼容,接口定義如下圖所示:
DLL 加載界面如下圖所示:
【1】 加載 DLL
【2】 刪除 DLL
【3】 DLL 校驗(yàn)器,通過(guò)此按鈕,用戶可以判斷自己加載的 dll 接口是否正確,算法是否符合設(shè)計(jì)要求。如下圖所示:
如上圖所示界面,用戶選擇 Seed 的 Level 過(guò)后,輸入 Demo Seed 值,點(diǎn)擊 GenKey 進(jìn)行判斷。如果該 DLL 接口跟模板定義接口統(tǒng)一,則會(huì)輸出提示信息:Generate Key Success,然后用戶根據(jù) Key 值跟目標(biāo)值對(duì)比,進(jìn)一步確認(rèn)DLL 中的算法是否符合設(shè)計(jì)要求。
【4】 打開 TSMaster 安裝目錄下 Seed&Key 接口工程所在的路徑。用戶可以拷貝該工程添加自己的 Seed&Key 算法。
“
默認(rèn)SeedKey函數(shù)接口
目前,要想被 TSMaster 的診斷模塊直接加載,該 DLL 必須實(shí)現(xiàn)如下三種函數(shù)接口中的一種:
【1】 接口 1:
unsigned int GenerateKeyEx(
const unsigned char* ipSeedArray, /* Array for the seed [in] */
unsigned int iSeedArraySize, /* Length of the array for the seed [in] */
const unsigned int iSecurityLevel, /* Security level [in] */
const char* ipVariant, /* Name of the active variant [in] */
unsigned char* iopKeyArray, /* Array for the key [in, out] */
unsigned int iMaxKeyArraySize, /* Maximum length of the array for the key [in] */
unsigned int& oActualKeyArraySize); /* Length of the key [out] */
【2】 接口 2:
unsigned int GenerateKeyExOpt(
const unsigned char* ipSeedArray, /* Array for the seed [in] */
unsigned int iSeedArraySize, /* Length of the array for the seed [in] */
const unsigned int iSecurityLevel, /* Security level [in] */
const char* ipVariant, /* Name of the active variant [in] */
const char* iPara, /* */
unsigned char* iopKeyArray, /* Array for the key [in, out] */
unsigned int iMaxKeyArraySize, /* Maximum length of the array for the key [in] */
unsigned int& oActualKeyArraySize) /* Length of the key [out] */
【3】 接口 3:
bool ASAP1A_CCP_ComputeKeyFromSeed(
const unsigned char* ipSeedArray, /* Array for the seed [in] */
unsigned short iSeedArraySize, /* Length of the array for the seed [in] */
unsigned char* iopKeyArray, /* Array for the key [in, out] */
unsigned short iMaxKeyArraySize, /* Maximum length of the array for the key [in] */
unsigned short* opSizeKey) /* Length of the key [out] */
用戶的 DLL 只要實(shí)現(xiàn)了上述任意一種函數(shù)接口,即可直接加載到 TP 層模塊中。如果出現(xiàn)加載失敗,主要檢查如下情況:
1. 是否用 Release 模式發(fā)布,如果是 Debug 模式,常常會(huì)有以上失敗的情況出現(xiàn)。
2. 是否采用 x86 平臺(tái)發(fā)布,目前 TSMaster 為支持 X86 的版本,用來(lái)調(diào)試的 DLL 也必須為X86 模式。
“
如何兼容其他函數(shù)接口
日常使用中,經(jīng)常出現(xiàn)用戶已經(jīng)開發(fā)好了 dll,如果該 dll的接口不是上述三種中的任何一種,就無(wú)法直接加載到 TSMaster 的診斷模塊中。對(duì)于這種情況,推薦采用如下方案來(lái)解決此問題:
下面以一個(gè)實(shí)際的實(shí)例來(lái)講解如何兼容用戶現(xiàn)有的 DLL 文件。
1. 用戶現(xiàn)有的 DLL,名稱為 UserSeedKey.dll。該函數(shù)內(nèi)部的 API 函數(shù)有:
? Seed 等級(jí)為 1 的時(shí)候,調(diào)用函數(shù) void GetKeyFromSeed01(byte* ASeed, byte* AKey);
? Seed 等級(jí)為 3 的時(shí)候,調(diào)用函數(shù) void GetKeyFromSeed03(byte* ASeed, byte* AKey);
? Seed 等級(jí)為 11 的時(shí)候,調(diào)用函數(shù) void GetKeyFromSeed11(byte* ASeed, byte* AKey);
該 dll 不支持上述默認(rèn)加載接口,無(wú)法直接加載到 TSMaster 中使用。因此,需要把這些 DLL 再包裝一層,才能載入到 TSMaster 的診斷模塊中。
2. 選擇 TSMaster 安裝目錄中提供的 GenerateKeyEx 的模板工程,在該工程中調(diào)用上述 DLL的函數(shù)接口?;舅悸肥牵?/span>
? 采用 Loadlibrary 動(dòng)態(tài)用戶現(xiàn)有的 dll。
? 根據(jù)傳入的 Level 參數(shù),采用 GetProcAddress 函數(shù)動(dòng)態(tài)獲取實(shí)際的用于計(jì)算 Key 的函數(shù)指針。
? 如果獲取函數(shù)指針成功,則使用該函數(shù)指針傳輸 Seed 值,并計(jì)算對(duì)應(yīng)的 Key 值。
詳細(xì)調(diào)用示例函數(shù)如下圖所示:
3. 該 GenerateKeyEx 工程開發(fā)結(jié)束后,TSMaster 直接加載 GenerateKeyEx 所在的 dll。需要注意的是,用戶需要把現(xiàn)有的UserSeedKey.dll 拷貝到TSMaster 根目錄或者GenerateKeyEx.dll 所在的目錄。如果不拷貝過(guò)去,GenerateKeyEx.dll 執(zhí)行的時(shí)候會(huì)出現(xiàn)找不到對(duì)應(yīng)依賴 dll 的情況,解鎖失敗。
總結(jié):
在 TSMaster 安裝目錄中,提供了封裝 Seed&Key 算法的模板工程。如 GenerateKeyEx,GenerateKeyExOpt ASAP1A_CCP_ComputeKeyFromSeed,用戶基于此模板工程開發(fā)即可得到能夠直接加載的 dll 函數(shù)。
同時(shí),也提供了二次封裝的 dll 的工程,比如 GenerateKeyEx_Wrapper_Demo,該工程演示了如何基于已經(jīng)存在的 SeedKey 算法庫(kù)進(jìn)行包裝,生成可以直接加載到 TSMaster 診斷模塊中的 dll 的過(guò)程。
二、采用內(nèi)置的算法編輯器
基本步驟如下所示:
注意事項(xiàng):
【1】 算法函數(shù)的接口,TSMaster 目前提供了最常用的接口形式,如果用戶有自己特殊的接口形式,無(wú)法覆蓋住,請(qǐng)聯(lián)系上海同星把此接口增加到選項(xiàng)中。
【2】 所有的接口函數(shù)都定義了返回值 s32。增加此約束,主要是增加函數(shù)的嚴(yán)謹(jǐn)性。返回值為 0 表示成功,為其他值則有對(duì)應(yīng)的錯(cuò)誤碼。用戶在編輯代碼的時(shí)候,最后一行一定不要忘了輸入返回值,否則系統(tǒng)執(zhí)行函數(shù)過(guò)后,會(huì)認(rèn)為算法執(zhí)行失敗,不予往后面執(zhí)行。如下所示:
【3】 添加算法過(guò)后,點(diǎn)擊 OK 退出。TSMaster 內(nèi)置編譯器會(huì)自動(dòng)解釋該算法,并準(zhǔn)備好在執(zhí)行診斷的過(guò)程中使用。
*博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。