Skip to content

Commit

Permalink
Fix sdftest command bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Jun 8, 2024
1 parent c4e5cf0 commit 3614daf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
24 changes: 0 additions & 24 deletions include/gmssl/sdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,6 @@ typedef struct {
SDF_SIGN_KEY key;
} SDF_SIGN_CTX;

/*
typedef struct {
void *hSession;
} SDF_SM3_CTX;
typedef struct {
void *hSession;
void *hKey;
} SDF_SM4_KEY;
typedef struct {
uint32_t index;
uint8_t passlen;
unsigned char pass[26 + 1];
} SDF_ENC_PRIVATE_KEY;
typedef struct {
uint32_t index;
uint8_t passlen;
unsigned char pass[26 + 1];
} SDF_PRIVATE_KEY;
*/


int sdf_load_library(const char *so_path, const char *vendor);
int sdf_open_device(SDF_DEVICE *dev);
int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev);
Expand Down
13 changes: 13 additions & 0 deletions src/sdf/sdf_sm2_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@

extern void *globalDeviceHandle;

typedef struct {
uint32_t index;
uint8_t passlen;
unsigned char pass[26 + 1];
} SDF_ENC_PRIVATE_KEY;

typedef struct {
uint32_t index;
uint8_t passlen;
unsigned char pass[26 + 1];
} SDF_PRIVATE_KEY;


static const uint8_t zeros[ECCref_MAX_LEN - 32] = {0};

// hSession is from HashInit/Update/Final, also means the uiISKIndex key has been opened with password
Expand Down
9 changes: 4 additions & 5 deletions src/sdf/sdf_sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
#include "sdf.h"
#include "sdf_ext.h"

/*
* TODO:
* 1. SM3-HMAC

void *globalDeviceHandle = NULL;

*/

void *globalDeviceHandle = NULL;
typedef struct {
void *hSession;
} SDF_SM3_CTX;


int sm3_digest_init(SM3_DIGEST_CTX *ctx, const uint8_t *key, size_t keylen)
Expand Down
5 changes: 5 additions & 0 deletions src/sdf/sdf_sm4_cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
extern void *globalDeviceHandle;


typedef struct {
void *hSession;
void *hKey;
} SDF_SM4_KEY;


static int sdf_sm4_cbc_encrypt_blocks(SDF_SM4_KEY *key,
const uint8_t iv[16], const uint8_t *in, size_t inlen, uint8_t *out)
Expand Down
19 changes: 13 additions & 6 deletions tools/sdftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <time.h>
#include <gmssl/hex.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include "../src/sdf/sdf.h"
Expand Down Expand Up @@ -870,6 +871,7 @@ static int test_SDF_GenerateKeyWithIPK_ECC(int key, char *pass)
void *hDeviceHandle = NULL;
void *hSessionHandle = NULL;
void *hKeyHandle = NULL;
uint8_t iv[16] = {1};
unsigned int uiIPKIndex =(unsigned int)key;
unsigned char *pucPassword = (unsigned char *)pass;
unsigned int uiPwdLength = (unsigned int)strlen(pass);
Expand Down Expand Up @@ -906,6 +908,7 @@ static int test_SDF_GenerateKeyWithIPK_ECC(int key, char *pass)
return -1;
}

memcpy(ucIV, iv, 16);
ret = SDF_Encrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucData, uiDataLength, ucEncData, &uiEncDataLength);
if (ret != SDR_OK) {
fprintf(stderr, "Error: SDF_Encrypt return 0x%X\n", ret);
Expand Down Expand Up @@ -941,6 +944,7 @@ static int test_SDF_GenerateKeyWithIPK_ECC(int key, char *pass)
return -1;
}

memcpy(ucIV, iv, 16);
ret = SDF_Decrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, ucIV, ucEncData, uiEncDataLength, ucDecData, &uiDecDataLength);
if (ret != SDR_OK) {
printf("Error: SDF_Encrypt returned 0x%X\n", ret);
Expand Down Expand Up @@ -1063,6 +1067,7 @@ static int test_SDF_Encrypt(int kek)
unsigned int uiKEKIndex = (unsigned int)kek;
unsigned char pucKey[64];
unsigned int uiKeyLength = (unsigned int)sizeof(pucKey);
unsigned char ucIV[16] = {1};
unsigned char pucIV[16];
unsigned char pucData[32];
unsigned int uiDataLength = sizeof(pucData);
Expand Down Expand Up @@ -1091,6 +1096,7 @@ static int test_SDF_Encrypt(int kek)
}

// encrypt and decrypt
memcpy(pucIV, ucIV, 16);
ret = SDF_Encrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, pucIV, pucData, uiDataLength, pucEncData, &uiEncDataLength);
if (ret != SDR_OK) {
error_print_msg("SDF library: 0x%08X\n", ret);
Expand All @@ -1109,6 +1115,7 @@ static int test_SDF_Encrypt(int kek)
}
*/

memcpy(pucIV, ucIV, 16);
ret = SDF_Decrypt(hSessionHandle, hKeyHandle, SGD_SM4_CBC, pucIV, pucEncData, uiEncDataLength, pucDecData, &uiDecDataLength);
if (ret != SDR_OK) {
error_print_msg("SDF library: 0x%08X\n", ret);
Expand Down Expand Up @@ -1542,14 +1549,15 @@ static int speed_SDF_GenerateKeyPair_ECC(void)
return 1;
}

// XXX: speed of `SDF_InternalSign_ECC` should be compared with `sm2_do_sign`
static int speed_SDF_InternalSign_ECC(int key, char *pass)
{
void *hDeviceHandle = NULL;
void *hSessionHandle = NULL;
unsigned int uiIPKIndex = (unsigned int)key;
unsigned char *ucPassword = (unsigned char *)pass;
unsigned int uiPwdLength = (unsigned int)strlen(pass);
unsigned char ucData[64 - 32 - 8] = {1}; // same length as sm2_signtest.c
unsigned char ucData[SM3_DIGEST_SIZE] = {1}; // XXX: `SDF_InternalSign_ECC` can only handle 32 bytes digest
unsigned int uiDataLength = (unsigned int)sizeof(ucData);
ECCSignature eccSignature;
clock_t begin, end;
Expand Down Expand Up @@ -1602,14 +1610,15 @@ static int speed_SDF_InternalSign_ECC(int key, char *pass)
return 1;
}

// XXX: speed of `SDF_InternalVerify_ECC` should be compared with `sm2_do_verify`
static int speed_SDF_InternalVerify_ECC(int key, char *pass)
{
void *hDeviceHandle = NULL;
void *hSessionHandle = NULL;
unsigned int uiIPKIndex = (unsigned int)key;
unsigned char *ucPassword = (unsigned char *)pass;
unsigned int uiPwdLength = (unsigned int)strlen(pass);
unsigned char ucData[64 - 32 - 8] = {1}; // same length as sm2_signtest.c
unsigned char ucData[32] = {1}; // XXX: `SDF_InternalVerify_ECC` can only handle 32 bytes digest
unsigned int uiDataLength = (unsigned int)sizeof(ucData);
ECCSignature eccSignature;
clock_t begin, end;
Expand All @@ -1636,7 +1645,6 @@ static int speed_SDF_InternalVerify_ECC(int key, char *pass)
error_print();
return -1;
}

ret = SDF_InternalSign_ECC(hSessionHandle, uiIPKIndex, ucData, uiDataLength, &eccSignature);
if (ret != SDR_OK) {
(void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex);
Expand All @@ -1645,7 +1653,7 @@ static int speed_SDF_InternalVerify_ECC(int key, char *pass)
error_print();
return -1;
}
//(void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex); // FIXME: error on calling this
(void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex);

begin = clock();
for (i = 0; i < 16; i++) {
Expand All @@ -1663,7 +1671,6 @@ static int speed_SDF_InternalVerify_ECC(int key, char *pass)
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;

(void)SDF_ReleasePrivateKeyAccessRight(hSessionHandle, uiIPKIndex);
(void)SDF_CloseSession(hSessionHandle);
(void)SDF_CloseDevice(hDeviceHandle);

Expand Down Expand Up @@ -1879,7 +1886,7 @@ int sdftest_main(int argc, char **argv)
#if ENABLE_TEST_SPEED
if (speed_SDF_Hash() != 1) goto err;
if (speed_SDF_Encrypt_SM4_CBC(kek) != 1) goto err;
//if (speed_SDF_Decrypt_SM4_CBC(kek) != 1) goto err; // FIXME: should implement CBC without padding in SoftSDF
if (speed_SDF_Decrypt_SM4_CBC(kek) != 1) goto err; // FIXME: should implement CBC without padding in SoftSDF
if (speed_SDF_GenerateKeyPair_ECC() != 1) goto err;
if (speed_SDF_InternalSign_ECC(key, pass) != 1) goto err;
if (speed_SDF_InternalVerify_ECC(key, pass) != 1) goto err;
Expand Down

0 comments on commit 3614daf

Please sign in to comment.