20221320冯泰瑞-实验三密码模块实现-4-6课时实践过程记录
完成gmt0018 中,对称加密解密,非对称加密解密,签名验签,hash运算,MAC运算接口中的至少一类(至少5选1)
我选择做签名验签
连接USB设备
Ubuntu
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ lsusb
Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 002: ID 0e0f:0003 VMware, Inc. Virtual Mouse
Bus 001 Device 003: ID 0e0f:0002 VMware, Inc. Virtual USB Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 096e:0321 Feitian Technologies, Inc. USB TOKEN 3000GM
接口放入sdf.h,实现内容实现 sdf.c,testsdf.c
sdf.h源代码
#ifndef __SDF_H
#define __SDF_H
typedef struct DeviceInfo_st
{
unsigned char IssuerName[40 ];
unsigned char DeviceName[16 ];
unsigned char DeviceSerial[16 ];
unsigned int DeviceVersion;
unsigned int StandardVersion;
unsigned int AsymAlgAbility[2 ];
unsigned int SymAlgAbility;
unsigned int HashAlgAbility;
unsigned int BufferSize;
} DEVICEINFO;
#define RSAref_MAX_BITS 2048
#define RSAref_MAX_LEN ((RSAref_MAX_BITS + 7) / 8)
#define RSAref_MAX_PBITS ((RSAref_MAX_BITS + 1) / 2)
#define RSAref_MAX_PLEN ((RSAref_MAX_PBITS + 7) / 8)
typedef struct RSArefPublicKey_st
{
unsigned int bits;
unsigned char m[RSAref_MAX_LEN];
unsigned char e[RSAref_MAX_LEN];
} RSArefPublicKey;
typedef struct RSArefPrivateKey_st
{
unsigned int bits;
unsigned char m[RSAref_MAX_LEN];
unsigned char e[RSAref_MAX_LEN];
unsigned char d[RSAref_MAX_LEN];
unsigned char prime[2 ][RSAref_MAX_PLEN];
unsigned char pexp[2 ][RSAref_MAX_PLEN];
unsigned char coef[RSAref_MAX_PLEN];
} RSArefPrivateKey;
#define ECCref_MAX_BITS 512
#define ECCref_MAX_LEN ((ECCref_MAX_BITS+ 7 ) / 8)
typedef struct ECCrefPublicKey_st
{
unsigned int bits;
unsigned char x[ECCref_MAX_LEN];
unsigned char y[ECCref_MAX_LEN];
} ECCrefPublicKey;
typedef struct ECCrefPrivateKey_st
{
unsigned int bits;
unsigned char K[ECCref_MAX_LEN];
} ECCrefPrivateKey;
typedef struct ECCCipher_st
{
unsigned char x[ECCref_MAX_LEN];
unsigned char y[ECCref_MAX_LEN];
unsigned char M[32 ];
unsigned int L;
unsigned char C[1 ];
} ECCCipher;
typedef struct ECCSignature_st
{
unsigned char r[ECCref_MAX_LEN];
unsigned char s[ECCref_MAX_LEN];
} ECCSignature;
typedef UINT8;
typedef UINT32;
typedef UINT32 ULONG;
typedef UINT8 BYTE;
#define ECC_MAX_XCOORDINATE_BITS_LEN 512
#define ECC_MAX_YCOORDINATE_BITS_LEN 512
typedef struct Struct_ECCPUBLICKEYBLOB {
ULONG BitLen;
BYTE XCoordinate[ECC_MAX_XCOORDINATE_BITS_LEN/8 ];
BYTE YCoordinate[ECC_MAX_YCOORDINATE_BITS_LEN/8 ];
}ECCPUBLICKEYBLOB, * PECCPUBLICKEYBLOB;
typedef struct Struct_ECCCIPHERBLOB {
BYTE XCoordinate[ECC_MAX_XCOORDINATE_BITS_LEN/8 ];
BYTE YCoordinate[ECC_MAX_XCOORDINATE_BITS_LEN/8 ];
BYTE HASH[32 ];
ULONG CipherLen;
BYTE Cipher[1 ];
}ECCCIPHERBLOB, * PECCCIPHERBLOB;
typedef struct SDF_ENVELOPEDKEYBLOB
{
unsigned long ulAsymmAlgID;
unsigned long ulSymmAlgID;
ECCCIPHERBLOB ECCCipherBlob;
ECCPUBLICKEYBLOB PubKey;
unsigned char cbEncryptedPriKey[64 ];
} ENVELOPEDKEYBLOB, * PENVELOPEDKEYBLOB;
#define SDR_OK 0x0
#define SDR_BASE 0x01000000
#define SDR_UNKNOWERR SDR_BASE + 0x00000001
#define SDR_NOTSUPPORT SDR_BASE + 0x00000002
#define SDR_COMMFAIL SDR_BASE + 0x00000003
#define SDR_HARDFAIL SDR_BASE + 0x00000004
#define SDR_OPENDEVICE SDR_BASE + 0x00000005
#define SDR_OPENSESSION SDR_BASE + 0x00000006
#define SDR_PARDENY SDR_BASE + 0x00000007
#define SDR_KEYNOTEXIST SDR_BASE + 0x00000008
#define SDR_ALGNOTSUPPORT SDR_BASE + 0x00000009
#define SDR_ALGMODNOTSUPPORT SDR_BASE + 0x0000000A
#define SDR_PKOPERR SDR_BASE + 0x0000000B
#define SDR_SKOPERR SDR_BASE + 0x0000000C
#define SDR_SIGNERR SDR_BASE + 0x0000000D
#define SDR_VERIFYERR SDR_BASE + 0x0000000E
#define SDR_SYMOPERR SDR_BASE + 0x0000000F
#define SDR_STEPERR SDR_BASE + 0x00000010
#define SDR_FILESIZEERR SDR_BASE + 0x00000011
#define SDR_FILENOEXIST SDR_BASE + 0x00000012
#define SDR_FILEOFSERR SDR_BASE + 0x00000013
#define SDR_KEYTYPEERR SDR_BASE + 0x00000014
#define SDR_KEYERR SDR BASE + 0x00000015
#define SDR_ENCDATAERR SDR_BASE + 0x00000016
#define SDR_RANDERR SDR_BASE + 0x00000017
#define SDR_PRKRERR SDR_BASE + 0x00000018
#define SDR_MACERR SDR_BASE + 0x00000019
#define SDR_FILEEXISTS SDR_BASE + 0x0000001A
#define SDR_FILEWERR SDR_BASE + 0x0000001B
#define SDR_NOBUFFER SDR_BASE + 0x0000001C
#define SDR_INARGERR SDR_BASE + 0x0000001D
#define SDR_OUTARGERR SDR_BASE + 0x0000001E
int SDF_OpenDevice (void ** phDeviceHandle) ;
int SDF_CloseDevice (void * hDeviceHandle) ;
int SDF_GetDeviceInfo (void * hSessionHandle,DEVICEINFO * pstDeviceInfo) ;
int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength,unsigned char * pucRandom) ;
int SDF_ExportSignPublicKey_ECC (void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey) ;
int SDF_ExportEncPublicKey_ECC (void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey) ;
int SDF_GenerateKeyPair_ECC ( void * hSessionHandle,unsigned int uiAlgID,unsigned int uiKeyBits, ECCrefPublicKey * pucPublicKey,ECCrefPrivateKey * pucPrivateKey) ;
int SDF_ExternalVerify_ECC (void * hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey, unsigned char * pucDataInput, unsigned int uiInputLength, ECCSignature * pucSignature) ;
int SDFInternalSign_ECC (void * hSessionHandle, unsigned int uiISKIndex, unsigned char * pucData,unsigned int uiDataLength, ECCSignature * pucSignature) ;
int SDF_InternalVerify_ECC (void * hSessionHandle,unsigned int uiISKIndex,unsigned char * pucData,unsigned int uiDataLength, ECCSignature * pucSignature) ;
int SDF_ExternalEncrypt_ECC (void * hSessionHandle, unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey, unsigned char * pucData, unsigned int uiDataLength, ECCCipher * pucEncData) ;
int SDF_Encrypt (void * hSessionHandle,void * hKeyHandle,unsigned int uiAlgID, unsigned char * pucIV, unsigned char * pucData, unsigned int uiDataLength , unsigned char * pucEncData,unsigned int * puiEncDataLength) ;
int SDF_Decrypt (void * hSessionHandle, void * hKeyHandle, unsigned int uiAlgID, unsigned char * pucIV,unsigned char * pucEncData, unsigned int uiEncDataLength, unsigned char * pucData, unsigned int * puiDataLength) ;
int SDF_CalculateMAC (void * hSessionHandle,void * hKeyHandle, unsigned int uiAlgID, unsigned char * pucIV, unsigned char * pucData, unsigned int uiDataLength ,unsigned char * pucMAC, unsigned int * puiMACLength) ;
int SDF_HashInit (void * hSessionHandle, unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,unsigned char * pucID, unsigned int uiIDLength) ;
int SDF_HashUpdate (void * hSessionHandle,unsigned char * pucData,unsigned int uiDataLength) ;
int SDF_HashFinal (void * hSessionHandle,unsigned char * pucHash,unsigned int * puiHashLength) ;
#endif
sdf.c源代码
#include "sdf.h"
int SDF_OpenDevice (void ** phDeviceHandle)
{
return SDR_OK;
}
int SDF_CloseDevice (void * hDeviceHandle)
{
return SDR_OK;
}
int SDF_GetDeviceInfo (void * hSessionHandle,DEVICEINFO * pstDeviceInfo)
{
return SDR_OK;
}
int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength,unsigned char * pucRandom)
{
return SDR_OK;
}
int SDF_ExportSignPublicKey_ECC (void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey)
{
return SDR_OK;
}
int SDF_ExportEncPublicKey_ECC (void * hSessionHandle,unsigned int uiKeyIndex,ECCrefPublicKey * pucPublicKey)
{
return SDR_OK;
}
int SDF_GenerateKeyPair_ECC ( void * hSessionHandle,unsigned int uiAlgID,unsigned int uiKeyBits, ECCrefPublicKey * pucPublicKey,ECCrefPrivateKey * pucPrivateKey)
{
return SDR_OK;
}
int SDF_ExternalVerify_ECC (void * hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey, unsigned char * pucDataInput, unsigned int uiInputLength, ECCSignature * pucSignature)
{
return SDR_OK;
}
int SDFInternalSign_ECC (void * hSessionHandle, unsigned int uiISKIndex, unsigned char * pucData,unsigned int uiDataLength, ECCSignature * pucSignature)
{
return SDR_OK;
}
int SDF_InternalVerify_ECC (void * hSessionHandle,unsigned int uiISKIndex,unsigned char * pucData,unsigned int uiDataLength, ECCSignature * pucSignature)
{
return SDR_OK;
}
int SDF_ExternalEncrypt_ECC (void * hSessionHandle, unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey, unsigned char * pucData, unsigned int uiDataLength, ECCCipher * pucEncData)
{
return SDR_OK;
}
int SDF_Encrypt (void * hSessionHandle,void * hKeyHandle,unsigned int uiAlgID, unsigned char * pucIV, unsigned char * pucData, unsigned int uiDataLength, unsigned char * pucEncData,unsigned int * puiEncDataLength)
{
return SDR_OK;
}
int SDF_Decrypt (void * hSessionHandle, void * hKeyHandle, unsigned int uiAlgID, unsigned char * pucIV,unsigned char * pucEncData, unsigned int uiEncDataLength, unsigned char * pucData, unsigned int * puiDataLength)
{
return SDR_OK;
}
int SDF_CalculateMAC (void * hSessionHandle,void * hKeyHandle, unsigned int uiAlgID, unsigned char * pucIV, unsigned char * pucData, unsigned int uiDataLength,unsigned char * pucMAC, unsigned int * puiMACLength)
{
return SDR_OK;
}
int SDF_HashInit (void * hSessionHandle, unsigned int uiAlgID,ECCrefPublicKey * pucPublicKey,unsigned char * pucID, unsigned int uiIDLength)
{
return SDR_OK;
}
int SDF_HashUpdate (void * hSessionHandle,unsigned char * pucData,unsigned int uiDataLength)
{
return SDR_OK;
}
int SDF_HashFinal (void * hSessionHandle,unsigned char * pucHash,unsigned int * puiHashLength)
{
return SDR_OK;
}
sdftest.c源代码
#include "sdf.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
void **pdh;
pdh = (void **)malloc (20 );
int ret;
ret = SDF_OpenDevice(pdh);
if (ret != SDR_OK)
{
printf ("OpenDevice Failed!\n" );
}
else
{
printf ("OpenDevice Successed!\n" );
}
DEVICEINFO a;
ret = SDF_GetDeviceInfo(*pdh, &a);
if (ret != SDR_OK)
{
printf ("GetDeviceInfo Failed!\n" );
}
else
{
printf ("GetDeviceInfo Successed!\n" );
printf ("DeviceName:%s\n" , a.DeviceName);
printf ("DeviceVersion:%d\n" , a.DeviceVersion);
}
unsigned char data[] = "Hello, World!" ;
unsigned int dataLength = strlen ((char *)data);
ECCSignature signature;
unsigned char publicKey[ECCref_MAX_LEN * 2 ];
unsigned char privateKey[ECCref_MAX_LEN];
ret = SDFInternalSign_ECC(*pdh, 0 , data, dataLength, &signature);
if (ret != SDR_OK)
{
printf ("Sign Failed!\n" );
}
else
{
printf ("Sign Successed!\n" );
}
ret = SDF_InternalVerify_ECC(*pdh, 0 , data, dataLength, &signature);
if (ret != SDR_OK)
{
printf ("Verify Failed!\n" );
}
else
{
printf ("Verify Successed!\n" );
}
ret = SDF_CloseDevice(*pdh);
if (ret != SDR_OK)
{
printf ("CloseDevice Failed!\n" );
}
else
{
printf ("CloseDevice Successed!\n" );
}
free (pdh);
return 0 ;
}
运行结果
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ gcc -o testsdf sdf.c testsdf.c -lgmssl
In file included from sdf.c:3:
sdf.h:71:9: warning: type defaults to ‘int’ in declaration of ‘UINT8’ [-Wimplicit-int]
71 | typedef UINT8;
| ^~~~~
sdf.h:72:9: warning: type defaults to ‘int’ in declaration of ‘UINT32’ [-Wimplicit-int]
72 | typedef UINT32;
| ^~~~~~
In file included from testsdf.c:1:
sdf.h:71:9: warning: type defaults to ‘int’ in declaration of ‘UINT8’ [-Wimplicit-int]
71 | typedef UINT8;
| ^~~~~
sdf.h:72:9: warning: type defaults to ‘int’ in declaration of ‘UINT32’ [-Wimplicit-int]
72 | typedef UINT32;
| ^~~~~~
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ ./testsdf
OpenDevice Successed!
GetDeviceInfo Successed!
DeviceName:
DeviceVersion:0
Sign Successed!
Verify Successed!
CloseDevice Successed!
git commit
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ git add sdf.c sdf.h testsdf testsdf.c
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ git commit -m "gmt0018SM2qianmingyanqian"
[master 3d417b4] gmt0018SM2qianmingyanqian
4 files changed, 490 insertions(+), 38 deletions(-)
rewrite ch06/sdftest/src/sdf.c (83%)
create mode 100644 ch06/sdftest/src/sdf.h
create mode 100755 ch06/sdftest/src/testsdf
rewrite ch06/sdftest/src/testsdf.c (89%)
fengtairui@fengtairui-virtual-machine:~/bestidiocs2024/ch06/sdftest/src$ git log
commit 3d417b4e6f14fdd83d61192b6bddd37f36e4a0b3 (HEAD -> master)
Author: fengtairui <1978274655@qq.com>
Date: Sun Nov 24 21:53:12 2024 +0800
gmt0018SM2qianmingyanqian
Git库链接
冯泰瑞/Information Security Design
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步