Commit 685958c1 by kevin

first commit for pcsc api

0 parents
/*
LKT4302A
*/
/**
* @file DEF_Macro.h
*
* $Revision$
* $Date$
* $Author$
*/
/*** Header define ************************************************************/
#ifndef __DEF_TYPE_H__
#define __DEF_TYPE_H__
/*** Macro definitions ********************************************************/
#define SIZE_BYTE 1
#define SIZE_HALFWORD 2
#define SIZE_WORD 4
/*** Type definitions *********************************************************/
typedef unsigned char u8;
typedef signed char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned long u32;
typedef signed long s32;
typedef unsigned char BYTE;
typedef signed short SHORT;
typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef signed long LONG;
typedef unsigned long ULONG;
typedef unsigned long DWORD;
#endif ///< __DEF_TYPE_H__
#include "lk_seeta_verify.h"
#include <exception>
#include <string>
#include <iostream>
void LK_SEETA_VERIFY::lk_seeta_establish_context()
{
LONG result = SCardEstablishContext(dwScope,
NULL,
NULL,
&hContext);
if (SCARD_S_SUCCESS != result)
{
std::string msg = "scard establish context failed!";
std::cout<< msg.c_str() << std::endl;
throw msg.c_str();
}
DWORD cch = SCARD_AUTOALLOCATE;
result = SCardListReaders(hContext,
NULL,
(LPTSTR)&readers,
&cch);
if (SCARD_S_SUCCESS != result)
{
std::string msg = "scard list reader failed!";
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_connect()
{
LONG result = SCardConnect(hContext,
(LPTSTR)readers,
dwShareMode,
SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1,
&hCardHandle[index],
&dwProtocols[index]);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "Connect Failed,ErrorCode:%X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_reconnect()
{
LONG result = SCardReconnect(hCardHandle[index],
1,
3,
2,
&dwProtocols[index]);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "Reconnect Failed,ErrorCode:%X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
DWORD dwState, cch, cByte;
result = SCardStatus(hCardHandle[index],
NULL,
&cch,
&dwState,
&dwProtocol,
bAttr,
&cByte);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "status:%08X\n", dwState);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void command_convert(char* command, DWORD len, BYTE* inBuf)
{//convert to hex
len = len / 2;
for (int i = 0; i<1000; i++)
{
sscanf(command + 2 * i, "%02x", inBuf + i); //research the send data structure,x -hex
if (i >= (len - 1))
break;
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_transmit(char* command, DWORD len, BYTE* outBuf, DWORD &dwLenr)
{
BYTE inBuf[1000];
DWORD inBufLength = len / 2;
command_convert(command, len, inBuf);
LONG result = SCardTransmit(hCardHandle[index],
(dwProtocols[index] == SCARD_PROTOCOL_T0 ? SCARD_PCI_T0 : SCARD_PCI_T1),
inBuf,
inBufLength,
NULL,
outBuf,
&dwLenr);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "The Command Execute error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_scard_disconnect()
{
LONG result = SCardDisconnect(hCardHandle[index], 0);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "disconnect error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
void LK_SEETA_VERIFY::lk_seeta_sdcard_release_context()
{
LONG result = SCardReleaseContext(hContext);
if (SCARD_S_SUCCESS != result)
{
char buf[100];
sprintf(buf, "release context error:%08X\n", result);
std::string msg(buf);
std::cout << msg.c_str() << std::endl;
throw msg.c_str();
}
}
LK_SEETA_VERIFY::LK_SEETA_VERIFY()
{//create context and connects
lk_seeta_establish_context();
lk_seeta_sdcard_connect();
}
LK_SEETA_VERIFY::~LK_SEETA_VERIFY()
{//disconnect and release context
lk_seeta_scard_disconnect();
lk_seeta_sdcard_release_context();
}
DWORD get_command_length(char* command)
{
DWORD len = 0;
char* temp = command;
while (*temp != '\0')
{
++len;
temp = temp + 1;
}
return len;
}
int compare_des_inside_outside(BYTE* des_outside, BYTE* des_inside, DWORD len)
{//check if inside equal to outsize
int result = 0;
for (int i = 0; i < len; ++i)
{
if (des_outside[i] != des_inside[i])
{
result = -1;
break;
}
}
return result;
}
void LK_SEETA_VERIFY::contrast_authenticate()
{
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);
BYTE out_buf_info[10];
DWORD dwLenr;
lk_seeta_sdcard_transmit(command, len, out_buf_info, dwLenr);//get "61XX"
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);
lk_seeta_sdcard_transmit(command_get_content, len, outBuf, dwLenr);//actual result
if (dwLenr % 2 != 0)
{//length must 2 times
char buf[100];
sprintf(buf, "outBuf length error\n");
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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0x10);
//xor with random inside
for (int i = 0; i < dwLenr / 2; ++i)
{
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);
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();
}
}
#pragma once
#ifndef LK_SEETA_VERIFY_H
#define LK_SEETA_VERIFY_H
#ifdef __cplusplus
extern "C" {
#endif
#include "winscard.h"
#include "des.h"
class LK_SEETA_VERIFY
{
public:
LK_SEETA_VERIFY();
~LK_SEETA_VERIFY();
void contrast_authenticate();
private:
void lk_seeta_establish_context();//create context resource
void lk_seeta_sdcard_connect();//connect sdcard
void lk_seeta_sdcard_reconnect(); //active sdcard
void lk_seeta_sdcard_transmit(char* commond, DWORD len, BYTE* outBuf, DWORD &dwLenr);//transmit command and get result
void lk_seeta_scard_disconnect();//disconnect
void lk_seeta_sdcard_release_context();//release context resource
private:
DWORD dwScope = SCARD_SCOPE_USER;
SCARDCONTEXT hContext;
SCARDHANDLE hCardHandle[4];
DWORD dwShareMode = SCARD_SHARE_SHARED;
DWORD dwProtocols[4];//
LPTSTR readers = NULL;//
int index = 0;
BYTE bAttr[32];
DWORD dwProtocol;
private:
LK_SEETA_VERIFY& operator=(const LK_SEETA_VERIFY&) = delete;
};
#ifdef __cplusplus
}
#endif
#endif // !LK_SEETA_VERIFY_H
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{60BA1C11-DF1F-4EA5-A7BA-15712662B7BE}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>compare_verify</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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" />
</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" />
</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" />
</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" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>winscard.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>winscard.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;COMPARE_VERIFY_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DES.c" />
<ClCompile Include="main.cpp" />
<ClCompile Include="LK_seeta_verify.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DEF_Type.h" />
<ClInclude Include="des.h" />
<ClInclude Include="LK_seeta_verify.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LK_seeta_verify.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DES.c">
<Filter>Header Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LK_seeta_verify.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="des.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DEF_Type.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#ifndef __DES_h
#define __DES_h
#endif
#ifdef __DES_h
#include <stdint.h>
unsigned char Char8ToBit64(unsigned char *ch,unsigned char *bit_a);
void Bit64ToChar8(unsigned char bit_a[64],unsigned char ch[8]);
void DES_PC1_Transfrom(unsigned char key[64],unsigned char tempbts[56]);
void DES_PC2_Transfrom(unsigned char key[56],unsigned char tempbts[48]);
void DES_ROL(unsigned char *data_a,unsigned char time);
void DES_ROR(unsigned char *data_a,unsigned char time);
void DES_IP_Transform(unsigned char data_a[64]);
void DES_IP_1_Transform(unsigned char data_a[64]);
void DES_E_Transform(unsigned char data_a[48]);
void DES_P_Transform(unsigned char data_a[32]);
void DES_SBOX(unsigned char data_a[48]);
void DES_XOR(unsigned char R[48],unsigned char L[48],unsigned char count);
void DES_Swap(unsigned char left[32],unsigned char right[32]);
void encrypt_des(unsigned char *inoutdata,unsigned char *keyStr);
void decrypt_des(unsigned char *inoutdata, unsigned char *keyStr);
void CbcDes_encrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key);
void CbcDes_decrypt(unsigned char *iv,unsigned char *MsgIn, unsigned char *Key);
void encrypt_3des(uint8_t *inoutdata,uint8_t *keyStr);
void decrypt_3des(uint8_t *inoutdata ,uint8_t *keyStr);
/*CBC_DES加密函数*/
/* iv 初始向量8字节; inoutdata 输入时为明文数据,输出时为密文数据(注:第一字节为长度); keyStr 加密密钥 */
void Cbcencrypt_3des(unsigned char *iv,uint8_t *inoutdata,uint8_t *keyStr);
/*CBC_DES解密函数*/
void Cbcdecrypt_3des(unsigned char *iv,uint8_t *inoutdata ,uint8_t *keyStr);
/*iv 初始向量8字节; inoutdata 输入时为密文数据,输出时为明文数据(注:第一字节为长度); keyStr 加密密钥 */
#endif
#include "lk_seeta_verify.h"
#include <iostream>
#include <string>
int main()
{
LK_SEETA_VERIFY verify;
verify.contrast_authenticate();
while (1);
return 0;
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!