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 d0078d4d
authored
Jul 03, 2019
by
kevin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add des encrypt and limit num
1 parent
c994deb0
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
148 additions
and
105 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/main.cpp
compare_verify/CZ_LK_CHECKIT.h
View file @
d0078d4
...
...
@@ -32,7 +32,7 @@ void CZ_LK_CHECKIT()
verify
.
get_chip_id
(
chip_id
,
len
);
printf
(
"
\n
"
);
verify
.
read_chip_id_from_nvm
(
chip_id_from_nvm
,
len
);
verify
.
read_chip_id_
limit_num_
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
)
...
...
@@ -50,8 +50,8 @@ void CZ_LK_CHECKIT()
}
}
void
CZ_LK_CHECKIT_
WITH_STEP
()
{
//check
with each step
void
CZ_LK_CHECKIT_
time_to_time
()
{
//check
it from time to time
srand
((
unsigned
int
)
time
(
NULL
));
const
int
MAX
=
10000
;
const
int
step
=
3600
;
...
...
compare_verify/FucBase.h
deleted
100644 → 0
View file @
c994deb
#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 @
d0078d4
...
...
@@ -4,7 +4,9 @@
#include <iostream>
#include <WinBase.h>
#include "FucBase.h"
#include "des.h"
//#include "FucBase.h"
/*
void LK_SEETA_VERIFY::lk_seeta_establish_context()
...
...
@@ -348,83 +350,83 @@ int Hex2Byte(std::string strIn, BYTE *datOut)
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
,
"command execute error:%d
\n
"
,
res
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
BYTE
inoutdata
[
128
];
BYTE
keyStr
[
128
];
memset
(
inoutdata
,
0
,
0x20
);
memcpy
(
inoutdata
,
"
\x08\x11\x22\x33\x44\x55\x66\x77\x88
"
,
0x09
);
//注意第一个字节为加密明文长度
memcpy
(
keyStr
,
"
\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00
"
,
0x10
);
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
);
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
)
{
char
buf
[
100
];
sprintf
(
buf
,
"authorize failed!
\n
"
);
std
::
string
msg
(
buf
);
std
::
cout
<<
msg
.
c_str
()
<<
std
::
endl
;
throw
msg
.
c_str
();
}
}
//
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, "command execute error:%d\n", res);
//
//
std::string msg(buf);
//
std::cout << msg.c_str() << std::endl;
//
throw msg.c_str();
//
}
//
//
BYTE inoutdata[128];
//
BYTE keyStr[128];
//
memset(inoutdata, 0, 0x20);
//
memcpy(inoutdata, "\x08\x11\x22\x33\x44\x55\x66\x77\x88", 0x09); //注意第一个字节为加密明文长度
//
memcpy(keyStr, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x00\x00", 0x10);
//
//
//
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);
//
//
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)
//
{
//
char buf[100];
//
sprintf(buf, "authorize failed!\n");
//
//
std::string msg(buf);
//
std::cout << msg.c_str() << std::endl;
//
throw msg.c_str();
//
}
//
}
void
LK_SEETA_VERIFY
::
get_chip_id
(
unsigned
char
chip_id
[],
int
&
lenRcv
)
{
...
...
@@ -445,23 +447,49 @@ void LK_SEETA_VERIFY::get_chip_id(unsigned char chip_id[], int &lenRcv)
}
}
void
LK_SEETA_VERIFY
::
write_chip_id_to_nvm
()
void
LK_SEETA_VERIFY
::
write_chip_id_
limit_num_
to_nvm
()
{
uint8_t
DES_ENCRYPT_DECRYPT_KEY
[
128
];
memcpy
(
DES_ENCRYPT_DECRYPT_KEY
,
"
\x01\x09\x09\01\x00\x07\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00
"
,
0x10
);
unsigned
char
chip_id
[
300
];
int
len
;
get_chip_id
(
chip_id
,
len
);
//for (int i = 0; i < len; ++i)
//{
// printf("%d:%x ", i, chip_id[i]);
//}
unsigned
char
cmd_write_chip
[
300
];
unsigned
char
chip_id_and_limit_num
[
300
];
//mingwen
unsigned
char
chip_id_and_limit_num_des
[
300
];
//des chip id and limit num
memset
(
cmd_write_chip
,
0
,
300
);
memcpy
(
cmd_write_chip
,
"
\x80\x08\x00\x00\x09\x04
"
,
6
);
memcpy
(
cmd_write_chip
,
chip_id
,
8
);
memset
(
chip_id_and_limit_num
,
0
,
300
);
memset
(
chip_id_and_limit_num_des
,
0
,
300
);
memcpy
(
cmd_write_chip
,
"
\x80\x08\x00\x00\x11\x04
"
,
6
);
memcpy
(
chip_id_and_limit_num
,
"
\x10
"
,
1
);
memcpy
(
chip_id_and_limit_num
+
1
,
chip_id
,
8
);
memcpy
(
chip_id_and_limit_num
+
9
,
"
\x05\x00\x00\x01\x00\x00\x00\x00
"
,
8
);
// limit==10000
printf
(
"write into:
\n
"
);
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
printf
(
"%d:%x "
,
i
,
chip_id_and_limit_num
[
i
]);
}
encrypt_3des
(
chip_id_and_limit_num
,
DES_ENCRYPT_DECRYPT_KEY
);
memcpy
(
chip_id_and_limit_num_des
,
chip_id_and_limit_num
+
17
,
16
);
memcpy
(
cmd_write_chip
+
6
,
chip_id_and_limit_num_des
,
16
);
unsigned
char
rec
[
300
];
int
rec_len
;
int
res
=
SendAPDU
(
14
,
cmd_write_chip
,
&
rec_len
,
rec
);
//write to nvm
int
res
=
SendAPDU
(
22
,
cmd_write_chip
,
&
rec_len
,
rec
);
//write to nvm
//for (int i = 0; i < 14; ++i)
//{
// printf("%
d:%x ", i,
cmd_write_chip[i]);
// printf("%
2x ",
cmd_write_chip[i]);
//}
if
(
0
!=
res
)
...
...
@@ -477,15 +505,32 @@ void LK_SEETA_VERIFY::write_chip_id_to_nvm()
printf
(
"write id to nvm succeed!
\n
"
);
}
void
LK_SEETA_VERIFY
::
read_chip_id_from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
)
void
LK_SEETA_VERIFY
::
read_chip_id_
limit_num_
from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
)
{
uint8_t
DES_ENCRYPT_DECRYPT_KEY
[
128
];
memcpy
(
DES_ENCRYPT_DECRYPT_KEY
,
"
\x01\x09\x09\01\x00\x07\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00
"
,
0x10
);
unsigned
char
cmd_read_chip_id
[
300
];
unsigned
char
rec
[
300
];
unsigned
char
in_out_data
[
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
memset
(
in_out_data
,
0
,
300
);
memcpy
(
in_out_data
,
"
\x10
"
,
1
);
memcpy
(
in_out_data
+
1
,
rec
,
16
);
decrypt_3des
(
in_out_data
,
DES_ENCRYPT_DECRYPT_KEY
);
printf
(
"read from:
\n
"
);
for
(
int
i
=
0
;
i
<
17
;
++
i
)
{
printf
(
"%d:%x "
,
i
,
in_out_data
[
i
]);
}
if
(
0
!=
res
)
{
char
buf
[
100
];
...
...
@@ -502,7 +547,7 @@ void LK_SEETA_VERIFY::read_chip_id_from_nvm(unsigned char chip_id_from_nvm[], in
//}
lenRcv
=
8
;
memmove
(
chip_id_from_nvm
,
rec
,
8
);
memmove
(
chip_id_from_nvm
,
in_out_data
+
17
,
8
);
return
;
}
\ No newline at end of file
compare_verify/LK_seeta_verify.h
View file @
d0078d4
...
...
@@ -22,9 +22,9 @@ typedef int(__stdcall *pSendAPDU)(int, unsigned char*, int*, unsigned char*);
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
write_chip_id_
limit_num_to_nvm
();
//get chip id and limit num, then
write to nvm
void
read_chip_id_from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
);
void
read_chip_id_
limit_num_
from_nvm
(
unsigned
char
chip_id_from_nvm
[],
int
&
lenRcv
);
private
:
HMODULE
g_hDll
;
//̬
pConnect
ConnectUkey
;
...
...
compare_verify/compare_verify.vcxproj
View file @
d0078d4
...
...
@@ -56,15 +56,19 @@
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="ORZ.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
...
...
compare_verify/compare_verify.vcxproj.filters
View file @
d0078d4
...
...
@@ -29,10 +29,10 @@
<ClInclude Include="LK_seeta_verify.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="
des
.h">
<ClInclude Include="
CZ_LK_CHECKIT
.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="
CZ_LK_CHECKIT
.h">
<ClInclude Include="
des
.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
...
...
compare_verify/main.cpp
View file @
d0078d4
...
...
@@ -5,10 +5,14 @@
int
main
()
{
LK_SEETA_VERIFY
verify
;
verify
.
write_chip_id_limit_num_to_nvm
();
while
(
true
)
{
//CZ_LK_CHECKIT();
CZ_LK_CHECKIT_RANDOM
();
CZ_LK_CHECKIT
();
//CZ_LK_CHECKIT_time_to_time();
}
while
(
1
);
...
...
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