Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
zhangbo
/
CZ_LTK_CHIP_VERIFY
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 2894641a
authored
Jul 03, 2019
by
kevin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add chip id get and set in mingwen
1 parent
685958c1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
397 additions
and
40 deletions
compare_verify/CZ_LK_CHECKIT.h
compare_verify/FucBase.h
compare_verify/LK_seeta_verify.cpp
compare_verify/LK_seeta_verify.h
compare_verify/compare_verify.vcxproj
compare_verify/compare_verify.vcxproj.filters
compare_verify/CZ_LK_CHECKIT.h
0 → 100644
View file @
2894641
#pragma once
#include "lk_seeta_verify.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static
LK_SEETA_VERIFY
verify
;
//
static
int
compare_chip_id_hardware_nvm
(
unsigned
char
chip_id
[],
unsigned
char
chip_id_from_nvm
[],
int
len
)
{
int
is_sim
=
1
;
for
(
int
i
=
0
;
i
<
len
;
++
i
)
{
if
(
chip_id
[
i
]
!=
chip_id_from_nvm
[
i
])
{
is_sim
=
0
;
break
;
}
}
return
is_sim
;
}
void
CZ_LK_CHECKIT
()
{
unsigned
char
chip_id
[
300
];
unsigned
char
chip_id_from_nvm
[
300
];
int
len
;
verify
.
get_chip_id
(
chip_id
,
len
);
printf
(
"
\n
"
);
verify
.
read_chip_id_from_nvm
(
chip_id_from_nvm
,
len
);
int
is_sim
=
compare_chip_id_hardware_nvm
(
chip_id
,
chip_id_from_nvm
,
8
);
if
(
is_sim
>
0
)
{
//verify success
printf
(
"chip verification succeed
\n
"
);
}
else
{
//verify failed
char
buf
[
100
];
sprintf
(
buf
,
"Using unauthorized chip, please chekc with technicians.
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
}
void
CZ_LK_CHECKIT_WITH_STEP
()
{
//check with each step
srand
((
unsigned
int
)
time
(
NULL
));
const
int
MAX
=
10000
;
const
int
step
=
3600
;
int
random
=
rand
()
%
MAX
;
//std::cout << random << std::endl;
if
(
random
%
step
<=
10
)
{
//std::cout << " inner random:" << random << std::endl;
CZ_LK_CHECKIT
();
}
}
\ No newline at end of file
compare_verify/FucBase.h
0 → 100644
View file @
2894641
#ifndef __FUCBASE_H__
#define __FUCBASE_H__
#define UKEY_RIGHT 0
#define UKEY_WRONG -1
CString
DelStr
(
CString
strIn
);
int
Hex2Byte
(
CString
strIn
,
BYTE
*
datOut
);
#endif
\ No newline at end of file
compare_verify/LK_seeta_verify.cpp
View file @
2894641
...
...
@@ -2,7 +2,11 @@
#include <exception>
#include <string>
#include <iostream>
#include <WinBase.h>
#include "FucBase.h"
/*
void LK_SEETA_VERIFY::lk_seeta_establish_context()
{
LONG result = SCardEstablishContext(dwScope,
...
...
@@ -145,24 +149,91 @@ void LK_SEETA_VERIFY::lk_seeta_sdcard_release_context()
throw msg.c_str();
}
}
*/
LK_SEETA_VERIFY
::
LK_SEETA_VERIFY
()
{
//create context and connects
lk_seeta_establish_context
();
{
const
char
*
path_dll
=
"./LKT4302AHID_H.dll"
;
g_hDll
=
::
LoadLibraryA
(
path_dll
);;
//create handle
if
(
g_hDll
==
NULL
)
{
char
buf
[
100
];
sprintf
(
buf
,
"can't found LKT4302AHID_H.dll.
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
//get func
ConnectUkey
=
(
pConnect
)
::
GetProcAddress
(
g_hDll
,
"EK_Open"
);
if
(
ConnectUkey
==
NULL
)
{
char
buf
[
100
];
sprintf
(
buf
,
"EK_open not found in LKT4302AHID_H.dll.
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
GetUkeyAtr
=
(
pAtr
)
::
GetProcAddress
(
g_hDll
,
"EK_Reset"
);
if
(
GetUkeyAtr
==
NULL
)
{
char
buf
[
100
];
sprintf
(
buf
,
"EK_Reset not found in LKT4302AHID_H.dll.
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
DisConnectUkey
=
(
pDisConnect
)
::
GetProcAddress
(
g_hDll
,
"EK_Close"
);
if
(
DisConnectUkey
==
NULL
)
{
char
buf
[
100
];
sprintf
(
buf
,
"EK_Close not found in LKT4302AHID_H.dll.
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
SendAPDU
=
(
pSendAPDU
)
::
GetProcAddress
(
g_hDll
,
"EK_Exchange_APDU"
);
if
(
SendAPDU
==
NULL
)
{
char
buf
[
100
];
sprintf
(
buf
,
"EK_Exchange_APDU not found in LKT4302AHID_H.dll.
\n
"
);
lk_seeta_sdcard_connect
();
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
//connect
UINT
res
=
ConnectUkey
(
1
);
if
(
res
!=
1
)
{
char
buf
[
100
];
sprintf
(
buf
,
"connected error:%d
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
}
LK_SEETA_VERIFY
::~
LK_SEETA_VERIFY
()
{
//disconnect and release context
lk_seeta_scard_disconnect
();
INT
res
,
len
;
res
=
DisConnectUkey
();
lk_seeta_sdcard_release_context
(
);
FreeLibrary
(
g_hDll
);
}
DWORD
get_command_length
(
char
*
command
)
int
get_command_length
(
char
*
command
)
{
DWORD
len
=
0
;
int
len
=
0
;
char
*
temp
=
command
;
while
(
*
temp
!=
'\0'
)
{
...
...
@@ -173,9 +244,9 @@ DWORD get_command_length(char* command)
return
len
;
}
int
compare_des_inside_outside
(
BYTE
*
des_outside
,
BYTE
*
des_inside
,
DWORD
len
)
INT
compare_des_inside_outside
(
BYTE
*
des_outside
,
BYTE
*
des_inside
,
INT
len
)
{
//check if inside equal to outsize
int
result
=
0
;
INT
result
=
0
;
for
(
int
i
=
0
;
i
<
len
;
++
i
)
{
if
(
des_outside
[
i
]
!=
des_inside
[
i
])
...
...
@@ -187,31 +258,118 @@ int compare_des_inside_outside(BYTE* des_outside, BYTE* des_inside, DWORD len)
return
result
;
}
void
LK_SEETA_VERIFY
::
contrast_authenticate
()
std
::
string
DelStr
(
std
::
string
strIn
)
{
std
::
string
strOut
=
""
;
for
(
size_t
i
=
0
;
i
<
strIn
.
length
();
++
i
)
{
if
(
strIn
[
i
]
!=
' '
&&
strIn
[
i
]
!=
','
&&
strIn
[
i
]
!=
'h'
&&
strIn
[
i
]
!=
'H'
&&
strIn
[
i
]
!=
';'
&&
strIn
[
i
]
!=
'S'
&&
strIn
[
i
]
!=
'W'
&&
strIn
[
i
]
!=
'='
)
{
strOut
.
append
(
1
,
strIn
[
i
]);
}
}
return
strOut
;
}
/*************************************************
函数名称: Hex2Byte
功能: 将字符串转换为16进制
调用函数: NULL
被调用函数:
输入参数: strIn
输出参数: datOut
返回值: lenth, 输入字符串的长度
其他:
*************************************************/
int
Hex2Byte
(
std
::
string
strIn
,
BYTE
*
datOut
)
{
std
::
string
prefix_str
=
"80080000"
;
std
::
string
in_data_len
=
"09"
;
std
::
string
commpand_type
=
"03"
;
std
::
string
in_data
=
"1122334455667788"
;
char
*
command
=
const_cast
<
char
*>
(
std
::
string
(
""
).
append
(
prefix_str
).
append
(
in_data_len
).
append
(
commpand_type
).
append
(
in_data
).
c_str
());
//8008 0000 09 03 1122334455667788
DWORD
len
=
get_command_length
(
command
);
int
i
,
j
,
k
,
leth
;
char
charEven
,
charOdd
;
BYTE
out_buf_info
[
10
];
DWORD
dwLenr
;
strIn
=
DelStr
(
strIn
);
lk_seeta_sdcard_transmit
(
command
,
len
,
out_buf_info
,
dwLenr
);
//get "61XX"
leth
=
strIn
.
length
();
if
((
leth
>>=
1
)
==
0x00
)
{
return
0
;
}
BYTE
outBuf
[
1000
];
//8 random 8 des result
std
::
string
get_content_str
=
std
::
string
(
"00C00000"
).
append
(
1
,
out_buf_info
[
2
]).
append
(
1
,
out_buf_info
[
3
]);
char
*
command_get_content
=
const_cast
<
char
*>
(
get_content_str
.
c_str
())
;
len
=
get_command_length
(
command_get_content
);
if
((
strIn
[
0
]
==
'F'
)
&&
((
strIn
[
1
]
==
'0'
)
||
(
strIn
[
1
]
==
'4'
)))
{
leth
=
leth
-
2
;
}
lk_seeta_sdcard_transmit
(
command_get_content
,
len
,
outBuf
,
dwLenr
);
//actual result
for
(
k
=
0
;
k
<
leth
;
k
++
)
{
charEven
=
strIn
.
at
(
2
*
k
);
charOdd
=
strIn
.
at
(
2
*
k
+
1
);
i
=
16
;
j
=
16
;
if
(
dwLenr
%
2
!=
0
)
{
//length must 2 times
if
((
charEven
<=
'9'
)
&&
(
charEven
>=
'0'
))
{
i
=
(
charEven
-
'0'
);
}
else
if
((
charEven
<=
'F'
)
&&
(
charEven
>=
'A'
))
{
i
=
(
charEven
-
'A'
+
10
);
}
else
{
return
0
;
}
if
((
charOdd
<=
'9'
)
&&
(
charOdd
>=
'0'
))
{
j
=
(
charOdd
-
'0'
);
}
else
if
((
charOdd
<=
'F'
)
&&
(
charOdd
>=
'A'
))
{
j
=
(
charOdd
-
'A'
+
10
);
}
else
{
return
0
;
}
datOut
[
k
]
=
(
i
<<
4
)
+
j
;
}
//end of for
return
(
leth
);
}
void
LK_SEETA_VERIFY
::
contrast_authenticate
()
{
// not work
char
*
command
=
"8008 0000 09 03 1122334455667788"
;
//8008 0000 09 03 1122334455667788
std
::
cout
<<
"command:"
<<
command
<<
std
::
endl
;
INT
lenSnd
,
lenRcv
,
res
,
len
;
BYTE
cmdAPDU
[
0x200
],
outBuf
[
0x200
];
lenSnd
=
Hex2Byte
(
command
,
cmdAPDU
);
res
=
SendAPDU
(
lenSnd
,
cmdAPDU
,
&
lenRcv
,
outBuf
);
printf
(
"outBuf:"
);
for
(
int
i
=
0
;
i
<
lenRcv
;
++
i
)
{
//printf("%d:%x,%d ", i, outBuf[i], outBuf[i]);
printf
(
"%d:%x "
,
i
,
outBuf
[
i
]);
}
if
(
0
!=
res
)
{
char
buf
[
100
];
sprintf
(
buf
,
"
outBuf length error
\n
"
);
sprintf
(
buf
,
"
command execute error:%d
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
...
...
@@ -222,18 +380,40 @@ void LK_SEETA_VERIFY::contrast_authenticate()
BYTE
keyStr
[
128
];
memset
(
inoutdata
,
0
,
0x20
);
memcpy
(
inoutdata
,
"
\x08\x11\x22\x33\x44\x55\x66\x77\x88
"
,
0x09
);
//注意第一个字节为加密明文长度
memcpy
(
keyStr
,
"
\x00\x0
0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
\x00\x00
"
,
0x10
);
memcpy
(
keyStr
,
"
\x00\x0
1\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F
\x00\x00
"
,
0x10
);
//xor with random inside
for
(
int
i
=
0
;
i
<
dwLenr
/
2
;
++
i
)
printf
(
"
\n
"
);
printf
(
"inoutdata:"
);
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
printf
(
"%d:%x "
,
i
,
inoutdata
[
i
]);
}
printf
(
"
\n
"
);
printf
(
"keyStr:"
);
for
(
int
i
=
0
;
i
<
16
;
++
i
)
{
printf
(
"%d:%x "
,
i
,
keyStr
[
i
]);
}
printf
(
"
\n
"
);
//xor with random inside
for
(
int
i
=
1
;
i
<
9
;
++
i
)
{
//index 0 length of plaintext
inoutdata
[
i
]
^=
outBuf
[
i
];
}
//3DES加密
encrypt_3des
(
inoutdata
,
keyStr
);
int
shift_and_length
=
dwLenr
/
2
;
int
result
=
compare_des_inside_outside
(
inoutdata
+
shift_and_length
+
1
,
outBuf
+
shift_and_length
,
shift_and_length
);
printf
(
"
\n
"
);
printf
(
"des result:"
);
for
(
int
i
=
9
;
i
<
17
;
++
i
)
{
printf
(
"%d:%x "
,
i
,
inoutdata
[
i
]);
}
int
result
=
compare_des_inside_outside
(
inoutdata
+
9
,
outBuf
+
9
,
8
);
if
(
result
<
0
)
{
...
...
@@ -245,3 +425,84 @@ void LK_SEETA_VERIFY::contrast_authenticate()
throw
msg
.
c_str
();
}
}
void
LK_SEETA_VERIFY
::
get_chip_id
(
unsigned
char
chip_id
[],
int
&
lenRcv
)
{
unsigned
char
cmdAPDU
[
300
];
memset
(
cmdAPDU
,
0
,
300
);
memcpy
(
cmdAPDU
,
"
\x80\x08\x00\x00\x01\x07
"
,
6
);
int
res
=
SendAPDU
(
6
,
cmdAPDU
,
&
lenRcv
,
chip_id
);
if
(
0
!=
res
)
{
char
buf
[
100
];
sprintf
(
buf
,
"command execute error:%x
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
}
void
LK_SEETA_VERIFY
::
write_chip_id_to_nvm
()
{
unsigned
char
chip_id
[
300
];
int
len
;
get_chip_id
(
chip_id
,
len
);
unsigned
char
cmd_write_chip
[
300
];
memset
(
cmd_write_chip
,
0
,
300
);
memcpy
(
cmd_write_chip
,
"
\x80\x08\x00\x00\x09\x04
"
,
6
);
memcpy
(
cmd_write_chip
,
chip_id
,
8
);
unsigned
char
rec
[
300
];
int
rec_len
;
int
res
=
SendAPDU
(
14
,
cmd_write_chip
,
&
rec_len
,
rec
);
//write to nvm
//for (int i = 0; i < 14; ++i)
//{
// printf("%d:%x ", i, cmd_write_chip[i]);
//}
if
(
0
!=
res
)
{
char
buf
[
100
];
sprintf
(
buf
,
"command execute error:%x
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
printf
(
"write id to nvm succeed!
\n
"
);
}
void
LK_SEETA_VERIFY
::
read_chip_id_from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
)
{
unsigned
char
cmd_read_chip_id
[
300
];
unsigned
char
rec
[
300
];
int
rec_len
;
memset
(
cmd_read_chip_id
,
0
,
300
);
memcpy
(
cmd_read_chip_id
,
"
\x80\x08\x00\x00\x01\x05
"
,
6
);
int
res
=
SendAPDU
(
14
,
cmd_read_chip_id
,
&
rec_len
,
rec
);
//read from nvm
if
(
0
!=
res
)
{
char
buf
[
100
];
sprintf
(
buf
,
"command execute error:%x
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
//for (int i = 0; i < rec_len; ++i)
//{
// printf("%d:%x ", i, rec[i]);
//}
lenRcv
=
8
;
memmove
(
chip_id_from_nvm
,
rec
,
8
);
return
;
}
\ No newline at end of file
compare_verify/LK_seeta_verify.h
View file @
2894641
...
...
@@ -5,8 +5,13 @@
#ifdef __cplusplus
extern
"C"
{
#endif
#include "winscard.h"
#include "des.h"
#include <windows.h>
typedef
int
(
__stdcall
*
pConnect
)(
int
);
typedef
int
(
__stdcall
*
pAtr
)(
int
*
,
unsigned
char
*
);
typedef
int
(
__stdcall
*
pDisConnect
)(
void
);
typedef
int
(
__stdcall
*
pSendAPDU
)(
int
,
unsigned
char
*
,
int
*
,
unsigned
char
*
);
class
LK_SEETA_VERIFY
{
...
...
@@ -15,6 +20,18 @@ extern "C" {
~
LK_SEETA_VERIFY
();
void
contrast_authenticate
();
void
get_chip_id
(
unsigned
char
chip_id
[],
int
&
lenRcv
);
//get chip id from hardware
void
write_chip_id_to_nvm
();
//get chip id and write to nvm
void
read_chip_id_from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
);
private
:
HMODULE
g_hDll
;
//̬
pConnect
ConnectUkey
;
pDisConnect
DisConnectUkey
;
pAtr
GetUkeyAtr
;
pSendAPDU
SendAPDU
;
/*
private:
void lk_seeta_establish_context();//create context resource
void lk_seeta_sdcard_connect();//connect sdcard
...
...
@@ -34,6 +51,7 @@ extern "C" {
BYTE bAttr[32];
DWORD dwProtocol;
*/
private
:
LK_SEETA_VERIFY
&
operator
=
(
const
LK_SEETA_VERIFY
&
)
=
delete
;
};
...
...
compare_verify/compare_verify.vcxproj
View file @
2894641
...
...
@@ -88,9 +88,9 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>
Windows
</SubSystem>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>
winscard.lib;
%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
...
...
@@ -103,7 +103,7 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>
winscard.lib;
%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
...
...
@@ -117,7 +117,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>
Windows
</SubSystem>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
...
...
@@ -133,7 +133,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<SubSystem>
Windows
</SubSystem>
<SubSystem>
Console
</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
...
...
@@ -145,7 +145,7 @@
<ClCompile Include="LK_seeta_verify.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="
DEF_Type
.h" />
<ClInclude Include="
CZ_LK_CHECKIT
.h" />
<ClInclude Include="des.h" />
<ClInclude Include="LK_seeta_verify.h" />
</ItemGroup>
...
...
compare_verify/compare_verify.vcxproj.filters
View file @
2894641
...
...
@@ -32,7 +32,7 @@
<ClInclude Include="des.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="
DEF_Type
.h">
<ClInclude Include="
CZ_LK_CHECKIT
.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment