赞
踩
- // rockey_test.cpp : Defines the entry point for the console application.
- //
-
- #include "stdafx.h"
- #include "SeKey.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include "mbedtls/des.h"
- #include "mbedtls/sha1.h"
- #include "mbedtls/sha256.h"
- #include "mbedtls/sha512.h"
- #include "mbedtls/aes.h"
- #include "mbedtls/rsa.h"
- #include "mbedtls/config.h"
-
- //1
- int test_find_open_close()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFA;
- unsigned short count = 0;
- unsigned long ret_status = 0;
-
- printf("test_find_open_close()\n");
- //Dongle_Find1 当开发商ID输入错误时
- ret_status = Dongle_Find(did, &count);
- if(ret_status != DONGLE_NOT_FOUND)
- {
- printf("Dongle_Find未找到指定的设备\n");
- printf("错误处101\n");
- return 101;
- }
- did = 0XFFFFFFFF;
- //Dongle_Find2 当开发商ID输入正确时
- ret_status = Dongle_Find(did, &count);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("Dongle_Find操作成功\n");
- }
- // 从1开始的索引值。表示打开找到的第几把加密锁。打开方式:0为独占打开,1为共享打开
- //Dongle_Open1 当索引值输入错误,其他参数输入正确时
- ret_status = Dongle_Open(2, 1, &hDongle);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处102\n");
- return 102;
- }
- //Dongle_Open2 当索引值输入正确,打开方式输入错误时
- ret_status = Dongle_Open(1, 2, &hDongle);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处103\n");
- return 103;
- }
- //Dongle_Open3 当索引值输入正确,其他参数输入正确时
- ret_status = Dongle_Open(1, 0, &hDongle);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("Dongle_Open操作成功\n");
- }
-
- ret_status = Dongle_Close(hDongle);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("Dongle_Close操作成功\n");
- }
- else
- {
- printf("错误处104\n");
- return 104;
- }
- printf("\n\n");
-
- return 0;
- }
- //2
- int test_verifyPin()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
-
- //char* upin = "00000000";
- unsigned char upin[8] = {0};
- //char* dpin = "FFFFFFFFFFFFFFFFFFFFFFFF";
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- unsigned char upin1[8] = {0,1,2,3,4,5,6,7};
- unsigned char dpin0[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4};
-
- unsigned char pTryCount;
- //写
- char *pBuffer = "123456789abc";
- char rec_buffer[256] = {0};
- unsigned short wOffset = 0;
- unsigned short wLen = 0;
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- //1表示用户pin,0表示开发商pin 用户PIN码(8字节,出厂默认全0)和开发商PIN码(24字节,出厂默认全F)
- //Dongle_VerifyPIN1 表示用户参数输入错误时
- ret_status = Dongle_VerifyPIN(hDongle, 3, upin, (BYTE *)&pTryCount);
-
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处201\n");
- return 201;
- }
- //Dongle_VerifyPIN2 开发商pin输入错误时
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin0, (BYTE *)&pTryCount);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处202\n");
- return 202;
- }
-
- //Dongle_VerifyPIN3 用户pin输入错误时
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin1, (BYTE *)&pTryCount);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处203\n");
- return 203;
- }
-
- //Dongle_VerifyPIN4 开发商pin输入正确时
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处204\n");
- return 204;
- }
-
- //Dongle_VerifyPIN5 用户pin输入正确时
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处205\n");
- return 205;
- }
-
- ret_status = Dongle_ResetState(hDongle);
- Dongle_Close(hDongle);
- return 0;
- }
- //3
- int test_write()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
- //写
- char pBuffer[256] = "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789a";
- //读
- char rBuffer[256] = {0};
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- //验证用户权限写,参数传入错误
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_WriteData1 用户权限写,参数错误 偏移量和长度都为0 返回错误
- ret_status = Dongle_WriteData(hDongle, 0, 0, (BYTE *)pBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处301\n");
- return 301;
- }
-
- //验证开发商权限写,参数传入错误
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_WriteData2 开发商权限写,参数错误 偏移量和长度都为0 返回错误
- ret_status = Dongle_WriteData(hDongle, 0, 0, (BYTE *)pBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处302\n");
- return 302;
- }
-
- //验证用户权限写高128字节,返回错误
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_WriteData3 用户权限写高128字节,返回错误
- ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处303\n");
- return 303;
- }
-
- //恢复匿名权限写高128字节,返回错误
- ret_status = Dongle_ResetState(hDongle);
- //Dongle_WriteData4 匿名权限写高128字节,返回错误
- ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处304\n");
- return 304;
- }
-
- //验证开发商权限写高128字节,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_WriteData5 开发商权限写高128字节,返回成功
- ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处305\n");
- return 305;
- }
- ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("开发商权限写高128字节,写入数据错误\n");
- return 306;
- }
-
-
-
- //恢复匿名权限写低128字节,返回错误
- ret_status = Dongle_ResetState(hDongle);
- //Dongle_WriteData6 匿名权限写低128字节,返回错误
- ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处307\n");
- return 307;
- }
-
- //验证开发商权限写低128字节,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_WriteData7 开发商权限写低128字节,返回成功
- ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处308\n");
- return 308;
- }
- ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("开发商权限写低128字节,写入数据错误\n");
- return 309;
- }
-
- //验证用户权限写低128字节,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_WriteData8 用户权限写低128字节,返回成功
- ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处310\n");
- return 310;
- }
- ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("用户权限写低128字节,写入数据错误\n");
- return 311;
- }
-
- //验证开发商权限写全部256字节,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_WriteData9 开发商权限写全部256字节,返回成功
- ret_status = Dongle_WriteData(hDongle, 0, 256, (BYTE *)pBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处312\n");
- return 312;
- }
-
- Dongle_Close(hDongle);
-
- return 0;
- }
- //4
- int test_read()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
- //写
- char pBuffer[256] = "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789a";
- //读
- char rBuffer[256] = {0};
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
- //开发商权限写全部256字节,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_WriteData 开发商权限写全部256字节,返回成功
- ret_status = Dongle_WriteData(hDongle, 0, 256, (BYTE *)pBuffer);
-
-
- //1验证用户权限读,参数传入错误
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_ReadData1 用户权限读,参数错误 偏移量和长度都为0 返回错误
- ret_status = Dongle_ReadData(hDongle, 0, 0, (BYTE *)rBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处401\n");
- return 401;
- }
-
- //2验证开发商权限读,参数传入错误
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_ReadData2 开发商权限读,参数错误 偏移量和长度都为0 返回错误
- ret_status = Dongle_ReadData(hDongle, 0, 0, (BYTE *)rBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处402\n");
- return 402;
- }
-
- //3验证用户权限读高128字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_ReadData3 用户权限读高128字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处403\n");
- return 403;
- }
- if(0 == strcmp(rBuffer, "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
- {
- }
- else
- {
- printf("用户权限读高128字节,读取数据错误\n");
- return 404;
- }
- //4恢复匿名权限读高128字节,返回错误
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- //Dongle_ReadData4 匿名权限读高128字节,返回错误
- ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处405\n");
- return 405;
- }
-
- //5验证开发商权限读高128字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_ReadData5 开发商权限读高128字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处406\n");
- return 406;
- }
- if(0 == strcmp(rBuffer, "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
- {
- }
- else
- {
- printf("开发商权限读高128字节,读取数据错误\n");
- return 407;
- }
- //6恢复匿名权限读低128字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- //Dongle_ReadData6 匿名权限读低128字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处408\n");
- return 408;
- }
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("匿名权限读低128字节,读取数据错误\n");
- return 409;
- }
- //7验证开发商权限读低128字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_ReadData7 开发商权限读低128字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处410\n");
- return 410;
- }
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("开发商权限读低128字节,读取数据错误\n");
- return 411;
- }
- //8验证用户权限读低128字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_ReadData8 用户权限读低128字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处412\n");
- return 412;
- }
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
- {
- }
- else
- {
- printf("用户权限读低128字节,读取数据错误\n");
- return 413;
- }
- //9验证开发商权限读全部256字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //Dongle_ReadData9 开发商权限读全部256字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 0, 256, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处414\n");
- return 414;
- }
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"
- "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abc"
- "defghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
- {
- }
- else
- {
- printf("开发商权限读全部256字节,读取数据错误\n");
- return 415;
- }
- //10验证用户权限读全部256字节,返回成功
- memset(rBuffer, 0, 256);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //Dongle_ReadData10 用户权限读全部256字节,返回成功
- ret_status = Dongle_ReadData(hDongle, 0, 256, (BYTE *)rBuffer);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处416\n");
- return 416;
- }
- if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
- "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"
- "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abc"
- "defghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
- {
- }
- else
- {
- printf("用户权限读全部256字节,读取数据错误\n");
- return 417;
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
- //5
- int test_set3DESKey()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
- //密钥数值
- unsigned char key3[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,0,1,2,3,4,5,6,7,8,9};
- unsigned char key2[16] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
- unsigned char key1[8] = {1,2,3,4,5,6,7,8};
-
- unsigned char pLv[8] = {4,3,2,1,8,7,5,6};
- unsigned char OutsideKeyPlv[8] = {4,3,2,1,8,7,5,6};
-
- unsigned char buf[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
- unsigned char OutsideKeyBuf[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
- //明文
- unsigned char plain[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
- unsigned char plain7[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
- //密文
- unsigned char dec_plain[24] = {0};
- unsigned char mbedtlsPlain[24] = {0};
- //mbedTLS中关于3DES相关配置
- mbedtls_des3_context des3_ctx;
- mbedtls_des_context des_ctx;
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
-
- mbedtls_des3_init(&des3_ctx);
- mbedtls_des_init(&des_ctx);
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- //设置3DES密钥
- //1匿名状态设置3DES密钥 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处501\n");
- return 501;
- }
-
- //2用户权限设置3DES密钥 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处502\n");
- return 502;
- }
-
- //3开发商权限设置3DES密钥 密钥ID设置为1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 1, key3, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处503\n");
- return 503;
- }
-
- //4开发商权限设置3DES密钥 密钥ID设置为0,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处504\n");
- return 504;
- }
-
- //5开发商权限设置3DES密钥 密钥ID设置为1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 1, key3, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处505\n");
- return 505;
- }
- //3DES运算
- //6参数验证,3DES运算函数传参,用户权限下传入密钥ID 1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 1, 0, pLv, plain, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处506\n");
- return 506;
- }
-
- //7参数验证,3DES运算函数传参,用户权限下传入运算类型2,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 2, pLv, plain7, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处507\n");
- //return 507;
- }
-
- //8参数验证,3DES运算函数传参,用户权限下传入运算类型2,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 2, pLv, plain7, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处508\n");
- //return 508;
- }
- //9权限验证,3DES运算函数传参,匿名状态下进行加密运算,返回失败
- ret_status = Dongle_ResetState(hDongle);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0, pLv, plain, 24);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处509\n");
- return 509;
- }
-
- //一 3DES运算 ecb模式 8字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key1, 8);
- //10权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处510\n");
- return 510;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处511\n");
- return 511;
- }
-
- //10 ecb模式 8字节key 外库加密
- mbedtls_des_setkey_enc(&des_ctx, key1);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des_crypt_ecb( &des_ctx, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 8字节key enc出错 i = %d\n", i);
- return 512;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 8字节key enc出错 i = %d\n", i);
- return 513;
- }
- }
- //11权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处514\n");
- return 514;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处515\n");
- return 515;
- }
-
- //12 ecb模式 8字节key 外库解密
- mbedtls_des_setkey_dec(&des_ctx, key1);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des_crypt_ecb( &des_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 8字节key dec出错 i = %d\n", i);
- return 516;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 8字节key dec出错 i = %d\n", i);
- return 517;
- }
- }
- //二 3DES运算 ecb模式 16字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key2, 16);
- //13权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处518\n");
- return 518;
- }
-
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处519\n");
- return 519;
- }
-
- //14 ecb模式 16字节key 外库加密
- memset(dec_plain, 0, 24);
- mbedtls_des3_set2key_enc(&des3_ctx, key2);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_ecb( &des3_ctx, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 16字节key enc出错 i = %d\n", i);
- return 520;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 16字节key enc出错 i = %d\n", i);
- return 521;
- }
- }
-
- //15权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处522\n");
- return 522;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处523\n");
- return 523;
- }
-
- //16 ecb模式 16字节key 外库解密
- memset(mbedtlsPlain, 0, 24);
- mbedtls_des3_set2key_dec(&des3_ctx, key2);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_ecb( &des3_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 16字节key dec出错 i = %d\n", i);
- return 524;
- }
- }
-
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 16字节key dec出错 i = %d\n", i);
- return 525;
- }
- }
-
- //三 3DES运算 ecb模式 24字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
- //17权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处526\n");
- return 526;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处527\n");
- return 527;
- }
-
- //18 ecb模式 24字节key 外库加密
- memset(dec_plain, 0, 24);
- mbedtls_des3_set3key_enc(&des3_ctx, key3);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_ecb( &des3_ctx, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 24字节key enc出错 i = %d\n", i);
- return 528;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 24字节key enc出错 i = %d\n", i);
- return 528;
- }
- }
-
- //19 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处529\n");
- return 529;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处530\n");
- return 530;
- }
-
- //20 ecb模式 24字节key 外库解密
- memset(mbedtlsPlain, 0, 24);
- mbedtls_des3_set3key_dec(&des3_ctx, key3);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_ecb( &des3_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 24字节key dec出错 i = %d\n", i);
- return 531;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 24字节key dec出错 i = %d\n", i);
- return 532;
- }
- }
-
-
- //四 3DES运算 cbc模式 8字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key1, 8);
- //21 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处533\n");
- return 533;
- }
-
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处534\n");
- return 534;
- }
-
- //22 cbc模式 8字节key 外库加密
- memset(dec_plain, 0, 24);
- unsigned char mbedtls_enc_pLv1[8] = {4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv1[8] = {4,3,2,1,8,7,5,6};
- mbedtls_des_setkey_enc(&des_ctx, key1);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv1, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 8字节key enc出错 i = %d\n", i);
- return 535;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 8字节key enc出错 i = %d\n", i);
- return 536;
- }
- }
-
- //23 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处537\n");
- return 537;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处538\n");
- return 538;
- }
- //24 cbc模式 8字节key 外库解密
- memset(mbedtlsPlain, 0, 24);
- mbedtls_des_setkey_dec(&des_ctx, key1);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv1, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 8字节key dec出错 i = %d\n", i);
- return 539;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 8字节key dec出错 i = %d\n", i);
- return 540;
- }
- }
-
- //五 3DES运算 cbc模式 16字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key2, 16);
- //25 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处541\n");
- return 541;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处542\n");
- return 542;
- }
-
- //26 cbc模式 16字节key 外库加密
- memset(dec_plain, 0, 24);
- unsigned char mbedtls_enc_pLv2[8] = {4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv2[8] = {4,3,2,1,8,7,5,6};
- mbedtls_des3_set2key_enc(&des3_ctx, key2);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv2, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 16字节key enc出错 i = %d\n", i);
- return 543;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 16字节key enc出错 i = %d\n", i);
- return 544;
- }
- }
-
- //27 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处545\n");
- return 545;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处546\n");
- return 546;
- }
- //28 cbc模式 16字节key 外库解密
- memset(mbedtlsPlain, 0, 24);
- mbedtls_des3_set2key_dec(&des3_ctx, key2);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv2, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 16字节key dec出错 i = %d\n", i);
- return 547;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 16字节key dec出错 i = %d\n", i);
- return 548;
- }
- }
-
-
- //六 3DES运算 cbc模式 24字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
- //29 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处549\n");
- return 549;
- }
-
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处550\n");
- return 550;
- }
-
- //30 cbc模式 24字节key 外库加密验证
- memset(dec_plain, 0, 24);
- unsigned char mbedtls_enc_pLv3[8] = {4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv3[8] = {4,3,2,1,8,7,5,6};
- mbedtls_des3_set3key_enc(&des3_ctx, key3);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv3, plain + (8*i), dec_plain + (8*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 24字节key enc出错 i = %d\n", i);
- return 551;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 24字节key enc出错 i = %d\n", i);
- return 552;
- }
- }
- //31 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处553\n");
- return 553;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处554\n");
- return 554;
- }
- //32 cbc模式 24字节key 外库解密
- memset(mbedtlsPlain, 0, 24);
- mbedtls_des3_set3key_dec(&des3_ctx, key3);
- for(i = 0; i < 3; i++)
- {
- mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv3, dec_plain + (8*i), mbedtlsPlain + (8*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 24字节key dec出错 i = %d\n", i);
- return 555;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 24; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 24字节key dec出错 i = %d\n", i);
- return 556;
- }
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
- //6
- int test_userID()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
-
- unsigned char pUID[8] = {1,2,3,4,5,6,7,8};
- unsigned char get_pUID[8] = {0};
- unsigned char bySN[8] = {0};
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
- //设置用户ID号
- //1验证匿名状态设置用户ID号,返回失败
- ret_status = Dongle_ResetState(hDongle);
- //ret_status = Dongle_SetUserID(hDongle, pUID);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处601\n");
- return 601;
- }
-
- //2验证用户权限设置用户ID号,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //ret_status = Dongle_SetUserID(hDongle, pUID);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处602\n");
- return 602;
- }
-
- //3验证开发商权限设置用户ID号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //ret_status = Dongle_SetUserID(hDongle, pUID);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处603\n");
- return 603;
- }
- //获取用户ID号
- //4验证匿名状态获取用户ID号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- //ret_status = Dongle_GetUserID(hDongle, get_pUID);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处604\n");
- return 604;
- }
-
- //5验证匿名状态获取用户ID号的数据是否正确
- for(i = 0; i < 8; i++)
- {
- if(get_pUID[i] != pUID[i])
- {
- printf("匿名状态获取用户ID号出错。\n");
- return 605;
- }
- }
-
- //6验证用户权限获取用户ID号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //ret_status = Dongle_GetUserID(hDongle, get_pUID);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处606\n");
- return 606;
- }
-
- //7验证用户权限获取用户ID号的数据是否正确
- for(i = 0; i < 8; i++)
- {
- if(get_pUID[i] != pUID[i])
- {
- printf("用户权限获取用户ID号出错。\n");
- return 607;
- }
- }
-
-
- //8验证开发商权限获取用户ID号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- // ret_status = Dongle_GetUserID(hDongle, get_pUID);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处608\n");
- return 608;
- }
-
- //9验证开发商权限获取用户ID号的数据是否正确
- for(i = 0; i < 8; i++)
- {
- if(get_pUID[i] != pUID[i])
- {
- printf("开发商权限获取用户ID号出错。\n");
- return 609;
- }
- }
-
- //获取设备硬件序列号
- //10验证匿名状态获取设备硬件序列号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_GetSerialNumber(hDongle, bySN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处610\n");
- return 610;
- }
-
- //11验证用户权限获取设备硬件序列号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_GetSerialNumber(hDongle, bySN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处611\n");
- return 611;
- }
-
- //9验证开发商权限获取设备硬件序列号,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_GetSerialNumber(hDongle, bySN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处612\n");
- return 612;
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
- //7
- int test_moduleCount()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- unsigned long pwCount = 0;
- int pbLic = 0;
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
- //设置
- //1验证匿名状态设置指定计次模块的模块值,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 1, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处701\n");
- return 701;
- }
-
- //2验证用户权限设置指定计次模块的模块值,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 1, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处702\n");
- return 702;
- }
-
- //3验证开发商权限设置指定计次模块的模块值,模块号为0,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 65535, 0);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处703\n");
- return 703;
- }
-
- //4验证开发商权限设置指定计次模块的模块值,参数二传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 4, 0, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处704\n");
- return 704;
- }
- //问题1
- //5验证开发商权限设置指定计次模块的模块值,参数三传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 0, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处705\n");
- //return 705;
- }
- //问题2
- //6验证开发商权限设置指定计次模块的模块值,参数三传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 65536, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处706\n");
- //return 706;
- }
- //问题3
- //7验证开发商权限设置指定计次模块的模块值,参数四传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 0, 0, 3);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处707\n");
- //return 707;
- }
-
- //8验证开发商权限设置指定计次模块的模块值,模块号为1,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 1, 65536, 1);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处708\n");
- return 708;
- }
-
- //9验证开发商权限设置指定计次模块的模块值,模块号为2,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 2, 65536, 2);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处709\n");
- return 709;
- }
-
- //10验证开发商权限设置指定计次模块的模块值,模块号为3,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_SetModuleCount(hDongle, 3, 65536, 0);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处710\n");
- return 710;
- }
- //获取
- //12验证匿名状态获取指定计次模块的模块值,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_GetModuleCount(hDongle, 0, &pwCount, (BYTE *)&pbLic);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处712\n");
- return 712;
- }
-
- //13验证用户权限获取指定计次模块的模块值,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_GetModuleCount(hDongle, 1, &pwCount, (BYTE *)&pbLic);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处713\n");
- return 713;
- }
-
- //14验证开发商权限获取指定计次模块的模块值,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_GetModuleCount(hDongle, 2, &pwCount, (BYTE *)&pbLic);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处714\n");
- return 714;
- }
-
- //15验证用户权限获取指定计次模块的模块值,参数二传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- ret_status = Dongle_GetModuleCount(hDongle, 4, &pwCount, (BYTE *)&pbLic);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处715\n");
- return 715;
- }
- //递减
- //16验证匿名状态递减指定计次模块的模块值,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- //模块号
- ret_status = Dongle_DecModuleCount(hDongle, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处716\n");
- return 716;
- }
-
- //17验证用户权限递减指定计次模块的模块值,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号
- ret_status = Dongle_DecModuleCount(hDongle, 0);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处717\n");
- return 717;
- }
-
- //18验证开发商权限递减指定计次模块的模块值,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //模块号
- ret_status = Dongle_DecModuleCount(hDongle, 0);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处718\n");
- return 718;
- }
- //问题4
- //19验证用户权限递减指定计次模块的模块值,参数二传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号
- ret_status = Dongle_DecModuleCount(hDongle, 1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处719\n");
- //return 719;
- }
- //问题5
- //20验证用户权限递减指定计次模块的模块值,参数二传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //模块号
- ret_status = Dongle_DecModuleCount(hDongle, 2);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处720\n");
- //return 720;
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
-
- void dump_buf(const char *title, unsigned char *buf, size_t len)
- {
- size_t i;
-
- printf("%s", title);
- for (i = 0; i < len; i++)
- printf("%c%c", "0123456789ABCDEF"[buf[i] / 16],
- "0123456789ABCDEF"[buf[i] % 16]);
- printf("\n");
- }
-
- //8
- int test_seed_sha1_getRandom()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
-
- mbedtls_sha1_context sha1_1ctx;
- mbedtls_sha1_context sha1_2ctx;
-
- mbedtls_sha256_context sha256_1ctx;
- mbedtls_sha256_context sha256_2ctx;
-
- mbedtls_sha512_context sha512_1ctx;
- mbedtls_sha512_context sha512_2ctx;
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
- //sha1摘要算法
- unsigned char pSeed1[1024] = {0,1,2,3,4,5,6,7,8,9};
- unsigned char mbedtls_input1[1024] = {0,1,2,3,4,5,6,7,8,9};
- //unsigned char pSeed1[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
- //unsigned char mbedtls_input1[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
- unsigned char pSeed2[1] = {1};
- unsigned char mbedtls_input2[1] = {1};
-
- unsigned char pSeed3[1025] = {0,1,2,3};
- unsigned char pSeed4[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
- //sha1摘要算法 输出
- unsigned char pSeedResult1[20] = {0};
- unsigned char pSeedResult2[32] = {0};
- unsigned char pSeedResult3[64] = {0};
-
- unsigned char mbedtls_output1[20] = {0};
- unsigned char mbedtls_output2[32] = {0};
- unsigned char mbedtls_output3[64] = {0};
-
- //种子码运算
- unsigned char pSeedOrResult[255] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
-
- int bSeedLen = 255;
- unsigned char pSeedOrResult1[1025] = {0};
- int bSeedLen1 = 1025;
- //产生一个指定长度的随机数
- unsigned char pRandom[255] = {0};
- unsigned char pRandom1[256] = {0};
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
- //种子码运算
- //1验证匿名状态设置种子码运算,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处801\n");
- return 801;
- }
-
- //2验证用户权限设置种子码运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处802\n");
- return 802;
- }
-
- //3验证开发商权限设置种子码运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处803\n");
- return 803;
- }
-
- //4验证用户权限设置种子码运算,参数三传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Seed(hDongle, pSeedOrResult, 0);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处804\n");
- return 804;
- }
-
- //5验证用户权限设置种子码运算,参数三传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Seed(hDongle, pSeedOrResult1, bSeedLen1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处805\n");
- return 805;
- }
- //Sha1摘要算法
- mbedtls_sha1_init( &sha1_1ctx );
- mbedtls_sha1_starts( &sha1_1ctx );
-
- mbedtls_sha1_init( &sha1_2ctx );
- mbedtls_sha1_starts( &sha1_2ctx );
-
- mbedtls_sha256_init( &sha256_1ctx );
- mbedtls_sha256_starts( &sha256_1ctx, 0);
-
- mbedtls_sha256_init( &sha256_2ctx );
- mbedtls_sha256_starts( &sha256_2ctx, 0);
-
- mbedtls_sha512_init( &sha512_1ctx );
- mbedtls_sha512_starts( &sha512_1ctx, 0);
-
- mbedtls_sha512_init( &sha512_2ctx );
- mbedtls_sha512_starts( &sha512_2ctx, 0);
-
- //6验证匿名状态设置Sha1摘要算法,参数2传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
- ret_status = Dongle_Sha(hDongle, 0, pSeed4, 25, pSeedResult1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处806\n");
- //return 806;
- }
- //7验证匿名状态设置Sha1摘要算法,参数2传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
- ret_status = Dongle_Sha(hDongle, 4, pSeed4, 25, pSeedResult1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处807\n");
- //return 807;
- }
-
- //8验证匿名状态设置Sha1摘要算法,参数4传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed4, 0, pSeedResult1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处808\n");
- //return 808;
- }
-
- /*
- //9验证匿名状态设置Sha1摘要算法,参数4传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed3, 1025, pSeedResult1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处809\n");
- //0xf0006700
- //return 809;
- }
- */
- //10验证用户权限设置Sha1摘要算法,返回成功
- memset(pSeedResult1, 0, 20);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处810\n");
- return 810;
- }
-
- //11验证开发商权限设置Sha1摘要算法,返回成功
- memset(pSeedResult1, 0, 20);
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处811\n");
- return 811;
- }
-
- //一 SHA-1 摘要算法外库验证
- //1 摘要数据为1个字节
-
- ret_status = Dongle_ResetState(hDongle);
- memset(pSeedResult1, 0, 20);
- memset(mbedtls_output1, 0, 20);
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed2, 1, pSeedResult1);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处812\n");
- return 812;
- }
- dump_buf("result0:", pSeedResult1, 20);
-
- mbedtls_sha1_update( &sha1_1ctx, mbedtls_input2, 1);
- mbedtls_sha1_finish( &sha1_1ctx, mbedtls_output1);
-
-
- dump_buf("result1:", mbedtls_output1, 20);
- //摘要算法外库验证
- for(i = 0; i < 20; i++)
- {
- if(pSeedResult1[i] != mbedtls_output1[i])
- {
-
- printf("SHA-1 摘要数据为1个字节算法外库验证出错 i = %d\n", i);
- return 813;
- }
- }
- printf("\n\n");
- mbedtls_sha1_free( &sha1_1ctx );
-
- //2 摘要数据为1024个字节
- ret_status = Dongle_ResetState(hDongle);
- memset(pSeedResult1, 0, 20);
- memset(mbedtls_output1, 0, 20);
- ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处814\n");
- return 814;
- }
- dump_buf("result2:", pSeedResult1, 20);
-
- mbedtls_sha1_update( &sha1_2ctx, mbedtls_input1, 1024);
- mbedtls_sha1_finish( &sha1_2ctx, mbedtls_output1);
-
- dump_buf("result3:", mbedtls_output1, 20);
- //摘要算法外库验证
- for(i = 0; i < 20; i++)
- {
- if(pSeedResult1[i] != mbedtls_output1[i])
- {
- printf("SHA-1 摘要数据为1024个字节算法外库验证出错 i = %d\n", i);
- return 815;
- }
- }
-
- mbedtls_sha1_free( &sha1_2ctx );
-
- //二 SHA-256 摘要算法外库验证
- //1 摘要数据为1个字节
- ret_status = Dongle_Sha(hDongle, 0x02, pSeed2, 1, pSeedResult2);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处816\n");
- return 816;
- }
- mbedtls_sha256_update( &sha256_1ctx, mbedtls_input2, 1 );
- mbedtls_sha256_finish( &sha256_1ctx, mbedtls_output2);
- //摘要算法外库验证
- for(i = 0; i < 32; i++)
- {
- if(pSeedResult2[i] != mbedtls_output2[i])
- {
- printf("SHA-256 1个字节摘要算法外库验证出错 i = %d\n", i);
- return 817;
- }
- }
- mbedtls_sha256_free( &sha256_1ctx );
-
- //2 摘要数据为1024个字节
- ret_status = Dongle_Sha(hDongle, 0x02, pSeed1, 1024, pSeedResult2);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处818\n");
- return 818;
- }
- mbedtls_sha256_update( &sha256_2ctx, mbedtls_input1, 1024 );
- mbedtls_sha256_finish( &sha256_2ctx, mbedtls_output2);
- //摘要算法外库验证
- for(i = 0; i < 32; i++)
- {
- if(pSeedResult2[i] != mbedtls_output2[i])
- {
- printf("SHA-256 1024个字节摘要算法外库验证出错 i = %d\n", i);
- return 819;
- }
- }
-
- mbedtls_sha256_free( &sha256_2ctx );
- //三 SHA-512 摘要算法外库验证
- //1 摘要数据为1个字节
- ret_status = Dongle_Sha(hDongle, 0x03, pSeed2, 1, pSeedResult3);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处820\n");
- return 820;
- }
- mbedtls_sha512_update( &sha512_1ctx, mbedtls_input2, 1 );
- mbedtls_sha512_finish( &sha512_1ctx, mbedtls_output3);
- //摘要算法外库验证
- for(i = 0; i < 64; i++)
- {
- if(pSeedResult2[i] != mbedtls_output2[i])
- {
- printf("SHA-512 1个字节摘要算法外库验证出错 i = %d\n", i);
- return 821;
- }
- }
- mbedtls_sha512_free( &sha512_1ctx );
-
- //2 摘要数据为1024个字节
- ret_status = Dongle_Sha(hDongle, 0x02, pSeed1, 1024, pSeedResult3);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处822\n");
- return 822;
- }
- mbedtls_sha512_update( &sha512_2ctx, mbedtls_input1, 1024 );
- mbedtls_sha512_finish( &sha512_2ctx, mbedtls_output3);
- //摘要算法外库验证
- for(i = 0; i < 64; i++)
- {
- if(pSeedResult2[i] != mbedtls_output2[i])
- {
- printf("SHA-512 1024个字节摘要算法外库验证出错 i = %d\n", i);
- return 823;
- }
- }
-
- mbedtls_sha512_free( &sha512_2ctx );
-
- //随机数
- //12验证匿名状态产生一个指定长度的随机数,参数传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //随机数长度。取值范围为0--255字节。
- ret_status = Dongle_Get_Random(hDongle, 256, pRandom1);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处824\n");
- //return 824;
- }
-
- //13验证匿名状态产生一个指定长度的随机数,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处825\n");
- return 825;
- }
-
- //14验证用户权限产生一个指定长度的随机数,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处826\n");
- return 826;
- }
-
- //15验证开发商权限产生一个指定长度的随机数,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处827\n");
- return 827;
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
-
- static int myrand( void *rng_state, unsigned char *output, size_t len )
- {
- #if !defined(__OpenBSD__)
- size_t i;
-
- if( rng_state != NULL )
- rng_state = NULL;
-
- for( i = 0; i < len; ++i )
- output[i] = rand();
- #else
- if( rng_state != NULL )
- rng_state = NULL;
-
- arc4random_buf( output, len );
- #endif /* !OpenBSD */
-
- return( 0 );
- }
-
- //9
- int test_RSA()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
- RSA_KEY pKey;
-
- unsigned char buf1[128] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,2,2,2,2};
- unsigned char buf2[256] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,3,3,3,3};
-
- //1024位密钥
- unsigned char plain1[128] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,2,2,2,2};
- unsigned char dec_plain1[128] = {0};
- unsigned char mbedtlsPlain1[128] = {0};
- //2048位密钥
- unsigned char plain2[256] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,3,3,3,3};
- unsigned char dec_plain2[256] = {0};
- unsigned char mbedtlsPlain2[256] = {0};
-
- int ret_mbedtls = 0;
- mbedtls_rsa_context rsa_ctx;
- mbedtls_rsa_init( &rsa_ctx, 0, 0);
-
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
-
- //设置RSA加解密密钥
- ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
- memset(pKey.n, 0, 256);
- memset(pKey.e, 0, 4);
- memset(pKey.p, 0, 128);
- memset(pKey.q, 0, 128);
- memset(pKey.dp, 0, 128);
- memset(pKey.dq, 0, 128);
- memset(pKey.InvQ, 0, 128);
-
- pKey.len = 0x0400;
- mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
- mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 64);
- //1验证匿名状态 设置RSA加解密密钥,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处901\n");
- return 901;
- }
- //2验证用户权限 设置RSA加解密密钥,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处902\n");
- return 902;
- }
- //3验证开发商权限 设置RSA加解密密钥,参数传入正确, 返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处903\n");
- return 903;
- }
- //4验证开发商权限 设置RSA加解密密钥,参数二传入错误, 返回失败
- ret_status = Dongle_SetRSAKey(hDongle, 1, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处904\n");
- return 904;
- }
-
- //生成RSA密钥对
- //5验证匿名状态 生成RSA密钥对,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处905\n");
- //return 905;
- }
-
- //6验证用户权限 生成RSA密钥对,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处906\n");
- //return 906;
- }
- //7验证开发商权限 生成RSA密钥对,参数传入正确, 返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处907\n");
- return 907;
- }
- //8验证开发商权限 生成RSA密钥对,参数二传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 1, 0x02, 0x01, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处908\n");
- return 908;
- }
-
- //9验证开发商权限 生成RSA密钥对,参数三传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 1, 0x01, 0x01, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处909\n");
- return 909;
- }
-
- //10验证开发商权限 生成RSA密钥对,参数四传入错误, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 1, 0x01, 0x10, &pKey);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处910\n");
- return 910;
- }
-
- //RSA运算
-
- //1024位密钥 RSA运算
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
-
- //11验证匿名状态 进行RSA运算,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处911\n");
- return 911;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处912\n");
- return 912;
- }
- //12验证用户权限 进行RSA运算,参数传入正确, 返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处913\n");
- return 913;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处914\n");
- return 914;
- }
-
- //ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
- //一,1024位密钥 用自己的库产生的公私钥赋值给mbedtls中的公私钥
-
-
- unsigned char pKey_n[128]= {0xD7,0x09,0x21,0xC9,0x2E,0xF9,0xF1,0x48,0x68,0x10,0x59,0x76,0x73,0xCD,0x16,0x31,0x9C,0x00,0x97,0xAD,0x59,0xDD,0x62,0x58,0xEC,0x4F,0x13,0xDA,0xD5,0x96,0x67,0x41,0xBA,0x9D,0x08,0x2B,0x65,0xBE,0x51,0x29,0x87,0xFA,0x54,0x21,0x3E,0x46,0x6F,0xF1,0x7A,0x3C,0xF7,0xDE,0x38,0xD3,0x56,0x76,0x9A,0xD7,0x81,0x13,0xB1,0x9D,0xE6,0xC8,0x01,0x64,0x3E,0x16,0x62,0x13,0x15,0x81,0x06,0x97,0x15,0xBC,0xCE,0x4C,0xC7,0x1B,0xDC,0x70,0xF0,0x61,0xB1,0x00,0xA2,0xB0,0x15,0x4D,0xDA,0xCF,0x9A,0x49,0x58,0x68,0x71,0x5C,0x36,0xFF,0x2B,0xC3,0xF8,0xCF,0xCC,0x91,0x94,0xA1,0xDD,0xA9,0xB0,0xDA,0x3D,0x69,0x48,0xC0,0xED,0x11,0xAF,0x81,0x40,0xAA,0x92,0x1D,0x05,0x55,0x92,0xBF};
- unsigned char pKey_e[4] = {0x00,0x01,0x00,0x01};
- unsigned char pKey_d[128] = {0xC2,0x1D,0x7B,0x43,0x5A,0xC5,0xC4,0x5B,0x21,0x9D,0xC8,0xB4,0xD6,0x5A,0x87,0xB3,0x66,0xE9,0x5E,0x02,0xDE,0x01,0xAC,0x5B,0x2B,0x8F,0x43,0x14,0x24,0x05,0x6E,0xD6,0xF5,0xF7,0xF5,0xBC,0xFF,0x4E,0xAB,0x83,0xBB,0x3D,0x6E,0x1E,0xC8,0x51,0xDB,0x91,0xF8,0x4F,0xFB,0x4D,0xBE,0xAC,0x80,0xFB,0xF6,0x51,0x9E,0x32,0xC1,0xFF,0x11,0xA9,0x48,0x71,0xC5,0xCB,0x70,0x41,0xE8,0x2F,0xA1,0x5F,0x9D,0xFF,0x6A,0xEC,0xEF,0x27,0x53,0xF0,0x88,0x00,0xCB,0x2A,0x5D,0xD7,0x31,0x49,0x3B,0x1F,0x03,0x43,0x61,0x0F,0x1A,0x25,0x2E,0xF8,0xB5,0x3F,0x75,0x62,0x60,0x09,0xF2,0x03,0xC0,0x32,0x5E,0x3B,0xA1,0x85,0x48,0x1A,0xB9,0x4C,0x89,0x0D,0xB6,0x93,0x27,0x12,0x29,0xD2,0x27,0xB9};
- unsigned char pKey_p[64] = {0xDA,0x7A,0xF6,0xF3,0x0A,0xAF,0x6F,0x91,0xE7,0xD6,0x7D,0x83,0x7C,0xA8,0xC0,0x15,0x2E,0x0F,0x52,0x6C,0x81,0x2A,0x2C,0xFF,0x64,0x77,0x58,0x61,0x8F,0xFE,0x9E,0xDB,0x5B,0x3F,0x1A,0x66,0x4E,0x71,0xCE,0xB2,0x92,0x10,0x7D,0x8D,0xB5,0x83,0xF8,0xD8,0xCE,0xEB,0xE2,0xC0,0x3B,0x64,0x1E,0xC0,0x75,0xFE,0x07,0xD1,0xAD,0x69,0xE2,0x1B};
- unsigned char pKey_q[64] = {0xFB,0xF6,0xBA,0xE3,0x9A,0x12,0xE7,0xCF,0x03,0xAF,0x4D,0x37,0x6C,0x87,0x3D,0x08,0x4D,0xAF,0xD6,0xF0,0xE0,0x53,0x40,0x48,0x23,0x13,0xED,0x7A,0x90,0xD2,0x73,0x18,0x0E,0x9A,0xD6,0x57,0x72,0x02,0xB8,0x1B,0x58,0x29,0x4C,0xD9,0x7A,0xBB,0x20,0x28,0x07,0xF4,0x17,0x14,0x4F,0xCC,0xED,0x62,0x44,0x23,0x6C,0x16,0xD9,0xD7,0xBC,0x2D};
- unsigned char pKey_dp[64] = {0x83,0x8D,0xE3,0x40,0xF9,0x40,0x25,0xE8,0x2F,0x37,0x42,0x20,0x78,0x61,0x8C,0x37,0xB4,0xA2,0x59,0x39,0x28,0x95,0x62,0xBE,0xED,0x99,0xC7,0x48,0x07,0xD2,0x83,0x89,0x05,0xEA,0x7B,0x54,0xFB,0x0B,0x20,0x30,0xFA,0x2E,0x9F,0xCB,0x42,0x6F,0x12,0xDA,0x49,0xCE,0x53,0x9C,0xF8,0xA6,0x12,0xB6,0x44,0xD6,0x0F,0x93,0x3B,0xDE,0x33,0xDD};
- unsigned char pKey_dq[64] = {0x53,0x14,0xF6,0x67,0x1A,0x64,0xE5,0x7F,0x03,0x6E,0x08,0x7E,0x20,0x11,0x2F,0xF4,0xCE,0xF9,0x0E,0x5C,0xFA,0xFB,0x38,0x95,0x10,0xCB,0xFF,0x60,0x8C,0xE4,0x78,0x03,0x14,0xBD,0xC5,0xB3,0xAB,0xEA,0x6D,0x9E,0xC6,0xA1,0x47,0xAF,0xE6,0xA2,0x51,0x66,0x21,0xC9,0x40,0xDA,0xF7,0xBD,0x51,0x01,0x38,0x0A,0x9E,0x17,0xA2,0xD9,0x5A,0x39};
- unsigned char pKey_InvQ[64] = {0x45,0x6F,0xEA,0x63,0x6C,0x21,0x2E,0xAC,0x92,0xBA,0xED,0xDA,0xA9,0x3A,0x03,0xEC,0x55,0xCB,0x78,0x95,0xC2,0x8C,0xE3,0x16,0xD2,0xC5,0x12,0xB1,0x6A,0x51,0x75,0xD4,0x03,0x19,0x4B,0xC1,0xA2,0xD9,0xDD,0x9A,0xB7,0xBC,0xEE,0x7F,0xD0,0x40,0xE6,0x7E,0x76,0xEE,0x1C,0x85,0x0E,0x0B,0x63,0x01,0x1D,0x43,0x0C,0x4A,0xA1,0xF4,0x73,0x64};
-
-
- rsa_ctx.len = 128;
- mbedtls_mpi_read_binary(&rsa_ctx.N , pKey_n, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.E , pKey_e, 4);
- mbedtls_mpi_read_binary(&rsa_ctx.D , pKey_d, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.P , pKey_p, 64);
- mbedtls_mpi_read_binary(&rsa_ctx.Q , pKey_q, 64);
- mbedtls_mpi_read_binary(&rsa_ctx.DP , pKey_dp, 64);
- mbedtls_mpi_read_binary(&rsa_ctx.DQ ,pKey_dq, 64);
- mbedtls_mpi_read_binary(&rsa_ctx.QP , pKey_InvQ, 64);
-
-
- ret_mbedtls = mbedtls_rsa_check_pubkey( &rsa_ctx );
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处915\n");
- return 915;
- }
-
- ret_mbedtls = mbedtls_rsa_check_privkey( &rsa_ctx );
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处916\n");
- return 916;
- }
-
- ret_mbedtls = mbedtls_rsa_public(&rsa_ctx, plain1, dec_plain1);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处917\n");
- return 917;
- }
-
- ret_mbedtls = mbedtls_rsa_private(&rsa_ctx, NULL, NULL, dec_plain1, mbedtlsPlain1);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处918\n");
- return 918;
- }
-
- //2.1024位密钥 外面的库产生的公私钥验证自己库
- ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
- memset(pKey.n, 0, 256);
- memset(pKey.e, 0, 4);
- memset(pKey.p, 0, 128);
- memset(pKey.q, 0, 128);
- memset(pKey.dp, 0, 128);
- memset(pKey.dq, 0, 128);
- memset(pKey.InvQ, 0, 128);
-
- pKey.len = 0x0400;
- mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
- mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 64);
- mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 64);
-
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //设置密钥
- ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
-
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处919\n");
- return 919;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处920\n");
- return 920;
- }
-
- //2048位密钥 RSA运算
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
- ret_status = Dongle_GenRSAKey(hDongle, 0, 0x04, 0x01, &pKey);
-
- //21验证匿名状态 进行RSA运算,权限不够, 返回失败
- ret_status = Dongle_ResetState(hDongle);
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处921\n");
- return 921;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处922\n");
- return 922;
- }
- //22验证用户权限 进行RSA运算,参数传入正确, 返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处923\n");
- return 923;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处924\n");
- return 924;
- }
- //二,2048位密钥 用自己的库产生的公私钥赋值给mbedtls中的公私钥
- //2048位密钥
-
- unsigned char pKey_n2[256]= {0xB1,0x8E,0x62,0x2E,0x72,0xB9,0xA0,0xFC,0x99,0x6D,0x0A,0x8C,0x77,0xDE,0x40,0x3E,0xC7,0x71,0x3C,0xB6,0x7D,0x79,0x9A,0xFF,0xF8,0xB2,0x2C,0x83,0xD1,0x92,0xBD,0xE2,0x80,0xCC,0x4C,0x7A,0xBB,0xCB,0xA6,0xFE,0xCC,0x5F,0xBA,0x99,0xD1,0x18,0x5A,0x39,0x40,0xC0,0xDE,0x64,0x3A,0xD4,0x90,0xD0,0x82,0x04,0x0F,0xFE,0xAC,0x9D,0x78,0x74,0x1D,0xF6,0xFD,0xCC,0xC6,0xEA,0x6D,0x42,0xE1,0x72,0x4B,0x8E,0x6A,0x2D,0xEE,0xF6,0xEF,0x7D,0xA1,0xA4,0xA5,0x1E,0x34,0x4E,0xF1,0x32,0x98,0xE5,0xB9,0x3C,0x05,0xAC,0x20,0xD6,0xA7,0xEC,0x41,0xA3,0x1A,0xBA,0x6A,0x51,0x5E,0xFF,0xC2,0xD3,0x4A,0x39,0x4F,0x2C,0x95,0x34,0xAA,0xA5,0x4A,0xD6,0xEA,0x9E,0x0A,0xEB,0x5A,0xE5,0x29,0xAD,0x12,0x67,0xBC,0x17,0xDC,0x0F,0x3F,0x34,0x4E,0x8A,0x7F,0xD1,0x5A,0x8C,0xD0,0xD3,0xC0,0x6A,0xD5,0x37,0x36,0x5B,0x41,0xB1,0xC4,0x4A,0x54,0x80,0x1B,0x48,0xCB,0xDC,0x3F,0x4C,0xA5,0x7D,0x1F,0xF4,0x32,0x66,0xDB,0x8E,0xFF,0x27,0x3C,0xF6,0x2F,0x6E,0x81,0x54,0x5D,0xE2,0xD2,0x07,0xD0,0x17,0x97,0x0A,0x62,0xA4,0x58,0x1A,0xDF,0x0A,0x85,0x00,0x7E,0xCA,0x18,0x96,0xED,0xE8,0xDC,0xD4,0x48,0x92,0x5C,0x65,0xF2,0x84,0x15,0xC7,0xEF,0x56,0xF1,0xC1,0xC1,0x07,0x9A,0x46,0x97,0x73,0x57,0xD2,0xFF,0x7D,0x62,0x6E,0xAF,0x82,0x6A,0x27,0xB7,0x86,0x3F,0xB4,0x42,0xBD,0x86,0x87,0xB6,0xEE,0x29,0xE9,0x29,0xD8,0xB0,0x25,0x3F,0xFF,0xBC,0x00,0x7F,0x4A,0xD5,0x05,0x38,0x09};
- unsigned char pKey_e2[4] = {0x00,0x01,0x00,0x01};
- unsigned char pKey_d2[256] = {0x44,0xFA,0xF1,0x0F,0xE4,0x88,0x53,0x8F,0xEF,0x37,0x5A,0xB2,0xC9,0xDA,0xF1,0x90,0xB2,0x41,0x3E,0xFA,0x12,0xF3,0x4C,0x91,0x87,0x98,0x09,0x0D,0x49,0x3D,0x3E,0x42,0x22,0x11,0xBC,0xE0,0xAD,0xD6,0xE7,0xD0,0xF3,0xAB,0x79,0x3E,0x4A,0xEA,0xB0,0x1A,0xB0,0x1A,0x7E,0x0B,0xE8,0x83,0x2E,0xA3,0xA2,0xEF,0x5B,0x1B,0x71,0x0A,0x58,0x82,0x9C,0x25,0x99,0xE2,0x95,0xA0,0xFB,0xC3,0xD0,0xFC,0x6B,0x78,0x2F,0xAE,0x1A,0xB9,0x9A,0xBF,0x6F,0x2E,0x1C,0xEC,0x08,0x1C,0x67,0x3E,0x66,0x02,0x51,0x9D,0x8D,0x02,0x9B,0x5B,0x69,0xAF,0xB2,0xAC,0x60,0x34,0xEE,0x1B,0x20,0xD2,0x32,0x69,0x8D,0xDB,0x87,0xF3,0xE5,0x1F,0xA9,0xF0,0x3F,0x8C,0x11,0x68,0x47,0x0B,0xBA,0xAB,0x6F,0x2D,0xE4,0x89,0x4D,0x82,0x9D,0x67,0x34,0xE0,0x75,0x22,0x8D,0x55,0x62,0x8C,0x16,0x5B,0xC9,0x4F,0x97,0x96,0x62,0xEB,0x8E,0x3C,0xCD,0x6E,0xA0,0x26,0x60,0x64,0x42,0xE1,0x9B,0x6E,0x1D,0xB5,0x3A,0xAC,0xBC,0xE6,0x7E,0xE5,0x80,0x86,0x11,0xB5,0x64,0x7B,0xE9,0xFA,0xB5,0xF4,0x4E,0x70,0xBB,0x24,0x4B,0xBC,0x0F,0xC4,0x52,0xE9,0x4A,0xAA,0x9D,0xF1,0x56,0xE3,0xBD,0xB3,0x1A,0xEF,0x61,0xAC,0x3C,0xE5,0xEB,0x20,0x8E,0x9B,0x3D,0xD6,0x40,0x18,0x7F,0x3A,0x99,0xBF,0xAC,0x4D,0x26,0xC7,0xD9,0x40,0x4A,0x48,0x5D,0x81,0xE7,0x35,0x5E,0x93,0x42,0x76,0x74,0xB4,0x33,0x05,0x17,0x08,0x08,0xC7,0x95,0xB9,0x52,0xAB,0x7B,0x4A,0xFC,0x02,0x61,0x31,0xB7,0xC3,0x14,0xA7,0x68,0x15};
- unsigned char pKey_p2[128] = {0xC0,0xC0,0x17,0x7E,0x5A,0x99,0x22,0x81,0x5E,0xFE,0x02,0x31,0xD6,0xE3,0xE1,0xB1,0x73,0xF6,0x9C,0xB7,0xE9,0x9A,0xB7,0x28,0xF0,0x5A,0xBD,0x6C,0x88,0x5E,0xAD,0x33,0xC1,0x74,0xFC,0x29,0xBF,0x80,0xB0,0xA9,0xEB,0xDC,0x87,0x3C,0xB7,0x27,0x40,0x63,0x1E,0x63,0xFD,0x59,0x08,0x6D,0x37,0xD8,0x0E,0x35,0x55,0xF8,0x8D,0xFC,0x49,0x43,0xBA,0xB7,0xBD,0x99,0xB0,0xD7,0x1E,0xAF,0x81,0x38,0x30,0xA6,0x26,0xDD,0x4E,0xDD,0x67,0x6A,0xAB,0x7E,0x8D,0xFF,0x59,0x08,0xF1,0x3F,0x09,0x10,0xF2,0x2D,0xF0,0x60,0xAF,0xFF,0xAB,0x59,0x4C,0x91,0xB0,0x20,0xEE,0x83,0x5D,0x4B,0xF9,0x6D,0x5D,0x74,0x17,0xA3,0x6A,0xB1,0xA8,0xC9,0xD2,0x9E,0x42,0x76,0x44,0x88,0x07,0xD9,0x61,0xDB};
- unsigned char pKey_q2[128] = {0xEB,0xD1,0xE9,0x79,0x73,0xD9,0x68,0x90,0x72,0xC7,0xFF,0xC7,0xCB,0x50,0x71,0xD0,0x96,0x9A,0x51,0x8D,0xB3,0xF3,0xFA,0x96,0xDE,0x0E,0x7B,0x67,0xFC,0x9F,0x42,0xB4,0x88,0xA9,0xF8,0xD2,0xEA,0x59,0xAB,0xB6,0xAF,0x34,0xFE,0x30,0x80,0x15,0x27,0x32,0x40,0xBC,0xAD,0x53,0x32,0x79,0x11,0x1D,0x3A,0xBE,0xA1,0xFC,0xE9,0xC9,0x06,0x54,0x1C,0x85,0x6A,0x6A,0xC9,0x4F,0x7E,0x47,0x99,0x5E,0x6E,0xD5,0x41,0x14,0xE9,0xDF,0xD8,0xD9,0x9E,0xF3,0xD5,0xE0,0xC8,0x08,0x92,0xD7,0x3E,0x38,0x16,0x45,0xBC,0x72,0x5A,0xAC,0xA9,0xF0,0x20,0xD8,0xF3,0xAE,0x7B,0x95,0xAA,0x8A,0x74,0xB5,0x90,0x6D,0xD4,0x72,0x9B,0x38,0x24,0x5C,0x25,0xED,0xDD,0x71,0x00,0xC4,0xA0,0x90,0x6C,0xEB};
- unsigned char pKey_dp2[128] = {0xA9,0x4A,0xCE,0x17,0xAB,0x49,0x1E,0xB6,0x54,0x11,0x73,0x2A,0x05,0x1C,0x87,0x89,0x0D,0x5A,0x07,0xD1,0xBC,0x7A,0xA7,0xEE,0x6C,0x14,0xB6,0x94,0xD1,0xBD,0x9E,0x10,0x7C,0x14,0xDC,0xB9,0x6A,0x3A,0x84,0x6F,0xE9,0xF9,0xFA,0x49,0xC8,0x6F,0xD0,0x2C,0x7E,0x6F,0xC1,0xFF,0xE5,0xD7,0xCF,0x99,0x92,0xF9,0x23,0x76,0x38,0x76,0xE2,0x7E,0x7B,0x4B,0x90,0x5A,0xA1,0xA8,0x7F,0x8A,0x14,0x4B,0x9E,0xE6,0xFF,0xF1,0x96,0xA7,0xFD,0x82,0x01,0xF8,0x18,0x8D,0xB2,0xCC,0x98,0xDF,0x0D,0x7E,0x75,0xC7,0x05,0x7E,0x56,0x76,0x8F,0x2F,0xD2,0x81,0x8F,0x19,0x8D,0x83,0x91,0x78,0xFE,0x2D,0x21,0x4C,0xAA,0xCA,0x5C,0x16,0x88,0x32,0x23,0x2B,0xDF,0x21,0xF3,0x15,0x2B,0x17,0xB1,0xCB};
- unsigned char pKey_dq2[128] = {0xA9,0xED,0xAC,0x18,0xF9,0x93,0x0B,0x57,0x33,0x6E,0xCC,0xF0,0xB2,0xA0,0x38,0x33,0x7F,0xA2,0x99,0xEB,0xCB,0x65,0x9F,0x08,0x41,0xE1,0x1B,0xA9,0xF6,0x0E,0xE9,0x29,0x60,0x65,0x62,0xC8,0xD1,0x58,0x66,0x75,0x10,0xD1,0xD5,0xED,0x2B,0x0E,0x93,0x1D,0x12,0x2B,0x23,0x96,0x95,0x03,0x4A,0x82,0xCE,0x86,0xFA,0x01,0x5E,0x91,0x9D,0xFE,0xD4,0x42,0x8B,0x49,0x81,0xE0,0xC7,0x75,0x5E,0x9D,0xFE,0xAD,0xAA,0xE5,0x20,0x16,0x4E,0x93,0x9A,0x87,0xC8,0x71,0xDE,0x92,0xD7,0x05,0x45,0x6F,0x73,0x15,0x33,0x53,0x0F,0xE5,0xE5,0xDB,0x04,0x40,0x71,0x70,0x24,0xDB,0xD7,0x75,0x47,0xDE,0x61,0xC1,0xD2,0xD9,0xF6,0xE1,0xC0,0xC9,0xAA,0x07,0x3A,0x8F,0xA8,0x67,0x07,0xF0,0xAA,0x9B};
- unsigned char pKey_InvQ2[128] = {0x5D,0xCE,0x67,0xC9,0x34,0x96,0x0E,0x0E,0x28,0xA5,0x72,0xA9,0x62,0xD4,0xA5,0xA7,0x39,0x9E,0x3F,0x4A,0x1C,0x52,0xD1,0xC1,0x4A,0xB8,0xEA,0x59,0x64,0xCB,0x3F,0xE0,0x59,0x40,0x18,0x5C,0xBA,0xD9,0xD4,0x3C,0xE5,0x66,0x39,0xA4,0x76,0x08,0xDE,0x3C,0x41,0x35,0xF7,0x37,0x55,0x9B,0xB7,0x94,0xFB,0x49,0xD3,0xB0,0xE5,0x1D,0xDE,0x94,0x27,0xD5,0x54,0xD8,0x61,0x71,0x88,0x9A,0x5D,0x15,0x61,0x76,0xA5,0xFC,0x3C,0x36,0x0B,0x51,0x23,0x51,0x84,0x15,0xD5,0x78,0x23,0x25,0xE8,0x69,0xB8,0x27,0x36,0xFB,0x75,0x8A,0x0C,0x78,0x33,0x6A,0x55,0x9C,0x2E,0x3F,0x09,0x03,0x35,0xBD,0x97,0x4A,0x7B,0x38,0x74,0x7F,0xA5,0x6A,0x77,0xAC,0x5A,0x78,0xEC,0xC6,0xEF,0x50,0xB9,0x1E};
-
- rsa_ctx.len = 256;
- mbedtls_mpi_read_binary(&rsa_ctx.N , pKey_n2, 256);
- mbedtls_mpi_read_binary(&rsa_ctx.E , pKey_e2, 4);
- mbedtls_mpi_read_binary(&rsa_ctx.D , pKey_d2, 256);
- mbedtls_mpi_read_binary(&rsa_ctx.P , pKey_p2, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.Q , pKey_q2, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.DP , pKey_dp2, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.DQ , pKey_dq2, 128);
- mbedtls_mpi_read_binary(&rsa_ctx.QP , pKey_InvQ2, 128);
-
-
- ret_mbedtls = mbedtls_rsa_check_pubkey( &rsa_ctx );
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处925\n");
- return 925;
- }
-
- ret_mbedtls = mbedtls_rsa_check_privkey( &rsa_ctx );
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处926\n");
- return 926;
- }
-
- ret_mbedtls = mbedtls_rsa_public(&rsa_ctx, plain2, dec_plain2);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处927\n");
- return 927;
- }
-
- ret_mbedtls = mbedtls_rsa_private(&rsa_ctx, NULL, NULL, dec_plain2, mbedtlsPlain2);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处928\n");
- return 928;
- }
-
- //2.1024位密钥 外面的库产生的公私钥验证自己库
- ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 2048, 65537);
- memset(pKey.n, 0, 256);
- memset(pKey.e, 0, 4);
- memset(pKey.p, 0, 128);
- memset(pKey.q, 0, 128);
- memset(pKey.dp, 0, 128);
- memset(pKey.dq, 0, 128);
- memset(pKey.InvQ, 0, 128);
-
- pKey.len = 0x0800;
- mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 256);
- mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
- mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 128);
- mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 128);
-
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
-
- ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
-
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处929\n");
- return 929;
- }
- // 运算类型。0x00公钥运算, 0x01私钥运算
- ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
- if(ret_mbedtls != DONGLE_SUCCESS)
- {
- printf("错误处930\n");
- return 930;
- }
- mbedtls_rsa_free(&rsa_ctx);
- Dongle_Close(hDongle);
- return 0;
- }
- //10
- int test_PIN()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
-
- //种子码产生用户PIN码
- char szUserPIN[8] = {0};
- unsigned char bySeed[1023] = {1,2,3,4,5,6,7,8};
- unsigned char bySeed1[1024] = {1,2,3,4,5,6,7,8};
- //修改开发商PIN码
- char szNewPin[24] = {0};
-
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- //种子码产生用户PIN码
- //1验证匿名状态 种子码产生用户PIN码,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1001\n");
- return 1001;
- }
-
- //2验证用户权限 种子码产生用户PIN码,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1002\n");
- return 1002;
- }
-
- //3验证开发商权限 种子码产生用户PIN码,参数三传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_GenUserPIN(hDongle, bySeed1, 1024, 255, szUserPIN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1003\n");
- return 1003;
- }
-
- //4验证开发商权限 种子码产生用户PIN码,参数四传入错误,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 256, szUserPIN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1004\n");
- return 1004;
- }
-
- //5验证开发商权限 种子码产生用户PIN码,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1005\n");
- return 1005;
- }
-
- //修改开发商PIN码
- //6验证匿名状态 修改开发商PIN码,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1006\n");
- return 1006;
- }
-
- //7验证用户权限 修改开发商PIN码,权限不够,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1007\n");
- return 1007;
- }
-
- //8验证开发商权限 修改开发商PIN码,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- //ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1008\n");
- return 1008;
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
- //11
- int test_setAESKey()
- {
- DONGLE_HANDLE hDongle;
- unsigned long did = 0XFFFFFFFF;
- unsigned short count = 0;
- unsigned long ret_status = 0;
- unsigned char pTryCount;
- //密钥数值
- unsigned char key1[16] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
- unsigned char key2[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,0,1,2,3,4,5,6,7,8,9};
- unsigned char key3[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2};
-
-
- unsigned char buf[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
- unsigned char OutsideKeyBuf[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
- //明文
- unsigned char plain[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
- //密文
- unsigned char dec_plain[32] = {0};
- unsigned char mbedtlsPlain[32] = {0};
- //初始化向量
- unsigned char pLv[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- unsigned char OutsideKeyPlv[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
-
- //mbedTLS中关于3DES相关配置
- mbedtls_aes_context aes_ctx;
-
- unsigned char upin[8] = {0};
- unsigned char dpin[24] = {0};
- int i = 0;
- for(i = 0; i < 24; i++)
- {
- dpin[i] = 0xFF;
- }
-
- mbedtls_aes_init(&aes_ctx);
- ret_status = Dongle_Find(did, &count);
- ret_status = Dongle_Open(1, 0, &hDongle);
-
- //设置AES密钥
- //1匿名状态设置AES密钥 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1101\n");
- return 1101;
- }
-
- //2用户权限设置AES密钥 返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1102\n");
- return 1102;
- }
-
- //3开发商权限设置AES密钥 密钥ID设置为1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 1, key3, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1103\n");
- return 1103;
- }
-
- //4开发商权限设置AES密钥 密钥ID设置为0,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1104\n");
- return 1104;
- }
-
- //5开发商权限设置AES密钥 密钥ID设置为1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 1, key3, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1105\n");
- return 1105;
- }
-
- //AES运算
- //6参数验证,AES运算函数传参,用户权限下传入密钥ID 1,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 1, 0x00, pLv, plain, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1106\n");
- return 1106;
- }
-
- //7参数验证,AES运算函数传参,用户权限下传入运算类型2,返回失败
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 2, pLv, plain, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1107\n");
- //return 1107;
- }
-
- //8权限验证,AES运算函数传参,匿名状态下进行加密运算,返回失败
- ret_status = Dongle_ResetState(hDongle);
- // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, plain, 32);
- if(ret_status == DONGLE_SUCCESS)
- {
- printf("错误处1108\n");
- return 1108;
- }
- //一 AES运算 ecb模式 16字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key1, 16);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1109\n");
- return 1109;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1110\n");
- return 1110;
- }
- //10 ecb模式 16字节key 外库加密
- mbedtls_aes_setkey_enc( &aes_ctx, key1, 128);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 16字节key enc出错 i = %d\n", i);
- return 1111;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 16字节key enc出错 i = %d\n", i);
- return 1112;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1113\n");
- return 1113;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1114\n");
- return 1114;
- }
- //12 ecb模式 16字节key 外库解密验证
- mbedtls_aes_setkey_dec(&aes_ctx, key1, 128);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 16字节key dec出错 i = %d\n", i);
- return 1115;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 16字节key dec出错 i = %d\n", i);
- return 1116;
- }
- }
-
- //二 AES运算 ecb模式 24字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key2, 24);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1117\n");
- return 1117;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1118\n");
- return 1118;
- }
- //10 ecb模式 24字节key 外库加密must be 128, 192 or 256
- mbedtls_aes_setkey_enc( &aes_ctx, key2, 192);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 24字节key enc出错 i = %d\n", i);
- return 1119;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 24字节key enc出错 i = %d\n", i);
- return 1120;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1121\n");
- return 1121;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1122\n");
- return 1122;
- }
- //12 ecb模式 24字节key 外库解密验证must be 128, 192 or 256
- mbedtls_aes_setkey_dec(&aes_ctx, key2, 192);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 24字节key dec出错 i = %d\n", i);
- return 1123;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 24字节key dec出错 i = %d\n", i);
- return 1124;
- }
- }
-
- //三 AES运算 ecb模式 32字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1125\n");
- return 1125;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1126\n");
- return 1126;
- }
- //10 ecb模式 32字节key 外库加密must be 128, 192 or 256
- mbedtls_aes_setkey_enc( &aes_ctx, key3, 256);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("ecb模式 32字节key enc出错 i = %d\n", i);
- return 1127;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 32字节key enc出错 i = %d\n", i);
- return 1128;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1129\n");
- return 1129;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1130\n");
- return 1130;
- }
- //12 ecb模式 32字节key 外库解密验证must be 128, 192 or 256
- mbedtls_aes_setkey_dec(&aes_ctx, key3, 256);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("ecb模式 32字节key dec出错 i = %d\n", i);
- return 1131;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("ecb模式 32字节key dec出错 i = %d\n", i);
- return 1132;
- }
- }
-
-
- //四 AES运算 cbc模式 16字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key1, 16);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1133\n");
- return 1133;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1134\n");
- return 1134;
- }
-
- //10 cbc模式 16字节key 外库加密must be 128, 192 or 256
- memset(dec_plain, 0, 32);
- unsigned char mbedtls_enc_pLv1[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv1[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- mbedtls_aes_setkey_enc( &aes_ctx, key1, 128);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv1, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 16字节key enc出错 i = %d\n", i);
- return 1135;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 16字节key enc出错 i = %d\n", i);
- return 1136;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1137\n");
- return 1137;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1138\n");
- return 1138;
- }
- //12 cbc模式 16字节key 外库解密验证must be 128, 192 or 256
- mbedtls_aes_setkey_dec(&aes_ctx, key1, 128);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv1, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 16字节key dec出错 i = %d\n", i);
- return 1139;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 16字节key dec出错 i = %d\n", i);
- return 1140;
- }
- }
-
- //五 AES运算 cbc模式 24字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key2, 24);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1141\n");
- return 1141;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1142\n");
- return 1142;
- }
-
- //10 cbc模式 24字节key 外库加密must be 128, 192 or 256
- memset(dec_plain, 0, 32);
- unsigned char mbedtls_enc_pLv2[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv2[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- mbedtls_aes_setkey_enc( &aes_ctx, key2, 192);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv2, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 24字节key enc出错 i = %d\n", i);
- return 1143;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 24字节key enc出错 i = %d\n", i);
- return 1144;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1145\n");
- return 1145;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2,24, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1146\n");
- return 1146;
- }
- //12 cbc模式 24字节key 外库解密验证must be 128, 192 or 256
- mbedtls_aes_setkey_dec(&aes_ctx, key2, 192);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv2, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 24字节key dec出错 i = %d\n", i);
- return 1147;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 24字节key dec出错 i = %d\n", i);
- return 1148;
- }
- }
-
- //六 AES运算 cbc模式 32字节key
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
- ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
- //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
- ret_status = Dongle_ResetState(hDongle);
- ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1149\n");
- return 1149;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1150\n");
- return 1150;
- }
-
- //10 cbc模式 32字节key 外库加密must be 128, 192 or 256
- memset(dec_plain, 0, 32);
- unsigned char mbedtls_enc_pLv3[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- unsigned char mbedtls_dec_pLv3[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
- mbedtls_aes_setkey_enc( &aes_ctx, key3, 256);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv3, plain + (16*i), dec_plain + (16*i));
- }
- //外库与锁内 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != dec_plain[i])
- {
- printf("cbc模式 32字节key enc出错 i = %d\n", i);
- return 1151;
- }
- }
- //锁内与锁外 加密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 32字节key enc出错 i = %d\n", i);
- return 1152;
- }
- }
- //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
- // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1153\n");
- return 1153;
- }
- // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
- if(ret_status != DONGLE_SUCCESS)
- {
- printf("错误处1154\n");
- return 1154;
- }
- //12 cbc模式 32字节key 外库解密验证must be 128, 192 or 256
- mbedtls_aes_setkey_dec(&aes_ctx, key3, 256);
- for(i = 0; i < 2; i++)
- {
- mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv3, dec_plain + (16*i), mbedtlsPlain + (16*i));
- }
- //外库与锁内 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != mbedtlsPlain[i])
- {
- printf("cbc模式 32字节key dec出错 i = %d\n", i);
- return 1155;
- }
- }
- //锁内与锁外 解密验证
- for(i = 0; i < 32; i++)
- {
- if(buf[i] != OutsideKeyBuf[i])
- {
- printf("cbc模式 32字节key dec出错 i = %d\n", i);
- return 1156;
- }
- }
-
- Dongle_Close(hDongle);
- return 0;
- }
-
- int main(int argc, char * argv[])
- {
- //1 测试Dongle_Find、Dongle_Open、Dongle_Close函数功能
- //test_find_open_close();
-
- //2 测试Dongle_VerifyPIN函数功能
- //test_verifyPin();
-
- //3 测试Dongle_WriteData、Dongle_ResetState函数功能
- test_write();
-
- //4 测试Dongle_ReadData函数功能
- //test_read();
-
- //5 测试Dongle_Set3DESKey、Dongle_3DES_Crypt函数功能
- test_set3DESKey();
-
- //6 测试Dongle_SetUserID、Dongle_GetUserID、Dongle_GetSerialNumber函数功能
- //test_userID();
-
- //7 测试Dongle_SetModuleCount、Dongle_GetModuleCount、Dongle_DecModuleCount函数功能
- test_moduleCount();
-
- //8 测试Dongle_Seed、Dongle_Sha1、Dongle_Get_Random函数功能
- test_seed_sha1_getRandom();
-
- //9 测试Dongle_SetRSAKey、Dongle_RSA、Dongle_GenRSAKey函数功能
- test_RSA();
-
- //10 测试Dongle_GenUserPIN、Dongle_ChangeAdminPIN函数功能
- //test_PIN();
-
- //11 测试Dongle_SetAESKey、Dongle_AES_Crypt_ByInsideKey、Dongle_AES_Crypt_ByOutsideKey函数功能
- test_setAESKey();
-
- //12 测试Dongle_MakeUpdatePacket、Dongle_Update函数功能
- //test_update();
-
- //13 测试Dongle_ClearCos、Dongle_Init函数功能
- //test_clearCos();
- return 0;
- }
- #ifndef _SEKEY_H_
- #define _SEKEY_H_
- #include <windows.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- typedef void * DONGLE_HANDLE; //加密锁句柄
- typedef struct _RSA_KEY
- {
- WORD len;
- BYTE e[4];
- BYTE n[256];
- BYTE p[128];
- BYTE q[128];
- BYTE dp[128];
- BYTE dq[128];
- BYTE InvQ[128];
- }RSA_KEY,*PRSA_KEY;
-
- //错误码
- #define DONGLE_SUCCESS 0x00000000 // 操作成功
- #define DONGLE_NOT_FOUND 0xF0000001 // 未找到指定的设备
- #define DONGLE_INVALID_HANDLE 0xF0000002 // 无效的句柄
- #define DONGLE_INVALID_PARAMETER 0xF0000003 // 参数错误
- #define DONGLE_COMM_ERROR 0xF0000004 // 通讯错误
- #define DONGLE_GETC_ERROR 0xF0000005 // 获取通信密钥错误
-
- #define DONGLE_ERROR_UNKNOWN 0xFFFFFFFF // 未知的错误
-
- /**
- * @brief 查找指定产品ID的加密锁设备。
- *
- * @param dwDID [in] 开发商ID,即DID
- * @param pwCount [out] 存放加密锁设备个数
- *
- * @return DONGLE_SUCCESS 表示找到加密锁设备
- */
- DWORD WINAPI Dongle_Find(DWORD dwDID, WORD *pwCount);
-
-
- /**
- * @brief 关闭加密锁。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- *
- * @return DONGLE_SUCCESS 表示关闭加密锁成功
- */
- DWORD WINAPI Dongle_Close(DONGLE_HANDLE hDongle);
-
-
- /**
- * @brief 打开加密锁。
- *
- * @param nIndex [in] 从1开始的索引值。表示打开找到的第几把加密锁。
- * @param bMode [in] 打开方式:0为独占打开,1为共享打开
- * @param phDongle [out] 存放打开后加密锁的句柄
- *
- * @return DONGLE_SUCCESS 表示打开加密锁成功
- */
- DWORD WINAPI Dongle_Open(int nIndex, BYTE bMode, DONGLE_HANDLE * phDongle);
-
- /**
- * @brief 将加密锁状态恢复到匿名态。最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- *
- * @return DONGLE_SUCCESS 重置加密锁状态成功
- */
- DWORD WINAPI Dongle_ResetState(DONGLE_HANDLE hDongle);
-
-
- /**
- * @brief 校验PIN码。PIN分为用户PIN码(8字节,出厂默认全0)和开发商PIN码(24字节,出厂默认全F),
- * 校验用户PIN码成功后,加密锁获得用户权限,校验开发商PIN码后,加密锁获得开发商权限,
- * 不校验PIN码,加密锁为匿名权限。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param nFlag [in] PIN码类型, 1表示用户pin,0表示开发商pin
- * @param pbPin [in] PIN码的值
- * @param pTryCount [out] 剩余重试次数,返回0表示已锁死;1~253表示剩余次数;255表示不限制
- *
- * @return DONGLE_SUCCESS 表示校验PIN码成功
- */
- DWORD WINAPI Dongle_VerifyPIN(DONGLE_HANDLE hDongle, int nFlag, BYTE * pbPin, BYTE * pTryCount);
-
-
- /**
- * @brief 种子码产生用户PIN码。最小调用权限:开发商。输入种子码产生用户PIN码,用户PIN码只能用种子码产生,且不可修改,但是
- * 可以开发商权限下重复生成。
- *
- * @param hDongle [in] 打开加密锁的句柄 。
- * @param bySeed [in] 种子码数据缓冲区指针。
- * @param nSeedLen [in] 参数bySeed的缓冲区长度,大于0且小于等于1023。
- * @param bTryCount [in] 用户PIN重试次数。取值范围为0~255,255表示不限重试次数
- * @param szUserPIN [out] 用户PIN码。存放产生的用户PIN码数据。该缓冲区大小至少应该为8字节,
- * 返回字符串长度为8字节的以0结尾的ansi字符串。
- *
- * @return DONGLE_SUCCESS 产生用户PIN码成功。
- */
- DWORD WINAPI Dongle_GenUserPIN(DONGLE_HANDLE hDongle, BYTE * bySeed, WORD wSeedLen, BYTE bTryCount, char * szUserPIN);
-
-
- /**
- * @brief 读加密锁的数据区。数据区大小根据不同类型的锁分为256字节、1024字节、4096字节;数据区平分位高低数据空间
- * 低数据空间任何权限可读,用户以上权限可写;高数据空间用户以上权限可读,只有开发商可写。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param wOffset [in] 数据区的偏移值
- * @param wLen [in] 读取数据区的长度
- * @param pBuffer [out] 存放读取的数据
- *
- * @return DONGLE_SUCCESS 表示读数据区成功
- */
- DWORD WINAPI Dongle_ReadData(DONGLE_HANDLE hDongle, WORD wOffset, WORD wLen, BYTE * pBuffer);
-
-
- /**
- * @brief 写加密锁的数据区。数据区大小根据不同类型的锁分为256字节、1024字节、4096字节;数据区平分位高低数据空间
- * 低数据空间任何权限可读,用户以上权限可写;高数据空间用户以上权限可读,只有开发商可写。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param wOffset [in] 数据区的偏移值
- * @param wLen [in] 读取数据区的长度
- * @param pBuffer [in] 写入数据区的数据
- *
- * @return DONGLE_SUCCESS 表示写数据区成功
- */
- DWORD WINAPI Dongle_WriteData(DONGLE_HANDLE hDongle, WORD wOffset, WORD wLen, BYTE * pBuffer);
-
-
- /**
- * @brief 获取设备硬件序列号。硬件序列号长度固定为8字节大小。的最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁句柄
- * @param bySN [out] 存放获取的硬件序列号,至少分配8字节空间
- */
- DWORD WINAPI Dongle_GetSerialNumber(DONGLE_HANDLE hDongle, BYTE * bySN);
-
-
- /**
- * @brief 获取用户ID号。最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param pUID [out] 存放用户ID值,8字节
- *
- * @return DONGLE_SUCCESS 表示获取用户ID成功
- */
- DWORD WINAPI Dongle_GetUserID(DONGLE_HANDLE hDongle, BYTE * pUID);
-
-
- /**
- * @brief 设置用户ID号。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param nUID [in] 用户ID值,8字节
- *
- * @return DONGLE_SUCCESS 表示设置用户ID成功
- */
- DWORD WINAPI Dongle_SetUserID(DONGLE_HANDLE hDongle, BYTE * pUID);
-
-
- /**
- * @brief 修改开发商PIN码。最小调用权限:开发商权限。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param szOldPin [in] 旧的开发商PIN码值
- * @param szNewPin [in] 新的开发商PIN码值
- *
- * @return DONGLE_SUCCESS 表示修改PIN码成功
- */
- DWORD WINAPI Dongle_ChangeAdminPIN(DONGLE_HANDLE hDongle, char * szOldPin, char * szNewPin);
-
-
- /**
- * @brief 设置指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bModule [in] 模块号
- * @param wCount [in] 计次模块值,范围为 1~65535
- * @param bLic [in] 模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- *
- * @return DONGLE_SUCCESS 设置计次模块值成功
- */
- DWORD WINAPI Dongle_SetModuleCount(DONGLE_HANDLE hDongle, BYTE bModule, WORD wCount, BYTE bLic);
-
- /**
- * @brief 获取指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bModule [in] 模块号
- * @param pwCount [in] 存放计次模块值
- * @param pbLic [out] 模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
- *
- * @return DONGLE_SUCCESS 获取计次模块值成功
- */
- DWORD WINAPI Dongle_GetModuleCount(DONGLE_HANDLE hDongle, BYTE bModule, DWORD * pwCount, BYTE *pbLic);
-
- /**
- * @brief 递减指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bModule [in] 模块号
- *
- * @return DONGLE_SUCCESS 递减指定计次模块值成功
- */
- DWORD WINAPI Dongle_DecModuleCount(DONGLE_HANDLE hDongle, BYTE bModule);
-
- /**
- * @brief 设置指定计时模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bModule [in] 模块号
- * @param dwStartTime [in] 计时模块开始时间值
- * @param dwEndTime [in] 计时模块结束之间值
- * @param dwTimeLength [in] 计时模块有效时长值
- * @param bLic [in] 模块属性。1 (起止时间型), 2(有效时长型)
- *
- * @return DONGLE_SUCCESS 设置计时模块值成功
- */
- DWORD WINAPI Dongle_SetTimeModule(DONGLE_HANDLE hDongle, BYTE bModule, DWORD dwStartTime, DWORD dwEndTime, DWORD dwTimeLength, BYTE bLic);
-
- /**
- * @brief 获取指定计时模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param wModule [in] 模块号
- * @param pdwTimeLength [in] 剩余有效时长
- * @param pbLic [out] 模块属性。1 (起止时间型), 2(有效时长型)
- *
- * @return DONGLE_SUCCESS 获取计次模块值成功
- */
- DWORD WINAPI Dongle_GetTimeModule(DONGLE_HANDLE hDongle, BYTE bModule, DWORD * pdwTimeLength, BYTE *pbLic);
-
-
- /**
- * @brief 设置3DES加密解密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID,ID从0号开始
- * @param pKey [in] 密钥
- * @param bKeyLen [in] 密钥长度8字节,16字节,24字节
- *
- * @return DONGLE_SUCCESS 设置3DES密钥成功
- */
- DWORD WINAPI Dongle_Set3DESKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE * pKey, BYTE bKeyLen);
-
- /**
- * @brief 3DES加解密运算。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID
- * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- * @param pLv [in] CBC运算模式8字节初始化向量
- * @param pInOutData [in,out] 输入输出数据
- * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为8的倍数,最大1024字节
- *
- * @return DONGLE_SUCCESS 3DES运算成功
- */
- DWORD WINAPI Dongle_3DES_Crypt_ByInsideKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
-
- /**
- * @brief 3DES加解密运算。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param pKey [in] 密钥
- * @param bKeyLen [in] 密钥长度8字节,16字节,24字节
- * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- * @param pLv [in] CBC运算模式8字节初始化向量
- * @param pInOutData [in,out] 输入输出数据
- * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为8的倍数,最大1024字节
- *
- * @return DONGLE_SUCCESS 3DES运算成功
- */
- DWORD WINAPI Dongle_3DES_Crypt_ByOutsideKey(DONGLE_HANDLE hDongle, BYTE * pKey, BYTE bKeyLen, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
-
- /**
- * @brief 设置AES加密解密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID,ID从0号开始
- * @param pKey [in] 密钥
- * @param bKeyLen [in] 密钥长度16字节,24字节,32字节
- *
- * @return DONGLE_SUCCESS 设置AES密钥成功
- */
- DWORD WINAPI Dongle_SetAESKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE * pKey, BYTE bKeyLen);
-
- /**
- * @brief AES加解密运算。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID
- * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- * @param pLv [in] CBC运算模式16字节初始化向量
- * @param pInOutData [in,out] 输入输出数据
- * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为16的倍数,最大1024字节
- *
- * @return DONGLE_SUCCESS AES运算成功
- */
- DWORD WINAPI Dongle_AES_Crypt_ByInsideKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
-
- /**
- * @brief AES加解密运算。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param pKey [in] 密钥
- * @param bKeyLen [in] 密钥长度16字节,24字节,32字节
- * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
- * @param pLv [in] CBC运算模式16字节初始化向量
- * @param pInOutData [in,out] 输入输出数据
- * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为16的倍数,最大1024字节
- *
- * @return DONGLE_SUCCESS AES运算成功
- */
- DWORD WINAPI Dongle_AES_Crypt_ByOutsideKey(DONGLE_HANDLE hDongle, BYTE * pKey, BYTE bKeyLen, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
-
-
- /**
- * @brief 种子码运算。此算法与用户PIN和PID有关,这两个值有变化的话同样的种子运算出的结果不同。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄。
- * @param pSeedOrResult [inout] 种子码及运算结果数据。
- * @param bSeedLen [in] 种子码数据有效长度。取值范围为1~1024字节。
- *
- * @return DONGLE_SUCCESS 种子码运算成功
- */
- DWORD WINAPI Dongle_Seed(DONGLE_HANDLE hDongle, BYTE * pSeedOrResult, BYTE bSeedLen);
-
- /**
- * @brief Sha摘要算法,最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁的句柄。
- * @param bFlag [in] 运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512
- * @param pSrc [in] 摘要数据。
- * @param wSrcLen [in] 摘要数据有效长度。取值范围为1~1024字节。
- * @param pResult [out] 输出数据缓冲区。20字节、32字节、64字节。
- *
- * @return DONGLE_SUCCESS 运算成功
- */
- DWORD WINAPI Dongle_Sha(DONGLE_HANDLE hDongle, BYTE bFlag, BYTE * pSrc, WORD wSrcLen, BYTE * pResult);
-
- /**
- * @brief 产生一个指定长度的随机数。最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁的句柄。
- * @param bRandLen [in] 随机数长度。取值范围为0--255字节。
- * @param pRandom [out] 输出数据缓冲区。
- *
- * @return DONGLE_SUCCESS 产生随机数成功
- */
- DWORD WINAPI Dongle_Get_Random(DONGLE_HANDLE hDongle, BYTE bRandLen, BYTE * pRandom);
-
-
- /**
- * @brief 设置RSA加解密密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID
- * @param pKey [in] RSA密钥
- *
- * @return DONGLE_SUCCESS 设置RSA密钥成功
- */
- DWORD WINAPI Dongle_SetRSAKey(DONGLE_HANDLE hDongle, BYTE bKeyID, RSA_KEY *pKey);
-
- /**
- * @brief RSA运算。最小调用权限:用户。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID
- * @param bFlag [in] 运算类型。0x00公钥运算, 0x01私钥运算
- * @param pData [in] 输入数据缓冲区
- * @param wDataLen [in] 输入数据有效长度
- *
- * @return DONGLE_SUCCESS 运算成功
- */
- DWORD WINAPI Dongle_RSA(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE * pData, WORD wDataLen);
-
-
- /**
- * @brief 生成RSA密钥对。最小调用权限:开发商。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param bKeyID [in] 密钥ID
- * @param bBits [in] 密钥位数:0x01=512位;0x02=1024位;0x04=2048位
- * @param bOperation [in] 运算类型。0x00只导出公钥, 0x01导出公私鈅
- * @param pKey [in] 输缓冲区
- *
- * @return DONGLE_SUCCESS 运算成功
- */
- DWORD WINAPI Dongle_GenRSAKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bBits, BYTE bOperation, RSA_KEY *pKey);
-
-
- typedef struct _UPDATE_PACKET
- {
- WORD len;
- }UPDATE_PACKET,*PUPDATE_PACKET;
-
-
- /**
- * @brief 制作升级数据包。
- *
- * @param pHID [in] 需要绑定的硬件序列号SN,为NULL则表示不绑定。绑定了SN后,只有该SN的加密锁才能进行升级。
- * @param pUpdatePacket [in] 升级配置。
- * @param pOut [in] 输出数据缓冲区
- * @param nInLen [in] 输出数据缓冲区长度和返回升级包的有效长度
- *
- * @return DONGLE_SUCCESS 制作远程升级包成功
- */
- DWORD WINAPI Dongle_MakeUpdatePacket(char * pSN, UPDATE_PACKET *pUpdatePacket, BYTE *pOut, int * pOutLen);
-
- /**
- * @brief 执行远程升级。最小调用权限:匿名。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- * @param pBuffer [in] 升级数据
- * @param nBufferLen [in] 升级数据长度
- *
- * @return DONGLE_SUCCESS 执行升级成功
- */
- DWORD WINAPI Dongle_Update(DONGLE_HANDLE hDongle, BYTE * pBuffer, int nBufferLen);
-
-
- /**
- * @brief 清COS, 不需要任何权限。
- *
- * @param hDongle [in] 打开的加密锁的句柄
- */
- DWORD WINAPI Dongle_ClearCos(DONGLE_HANDLE hDongle);
-
-
- /**
- * @brief 加密锁出厂设置。
- *
- * @param hDongle [in] 打开的加密锁句柄。
- * @param byInitBuf [in] 初始化数据。
- *
- * @return DONGLE_SUCCESS 初始化锁成功。
- */
- DWORD WINAPI Dongle_Init(DONGLE_HANDLE hDongle, BYTE * byInitBuf);
-
-
- #ifdef __cplusplus
- }
- #endif
-
- #endif
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。