mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-10 16:03:48 +08:00
411
src/sdf/sdf.c
411
src/sdf/sdf.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,208 +7,207 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sdf.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/error.h>
|
||||
#include "sdf.h"
|
||||
#include "sdf_ext.h"
|
||||
|
||||
|
||||
|
||||
static const uint8_t zeros[ECCref_MAX_LEN - 32] = {0};
|
||||
|
||||
static int SDF_ECCrefPublicKey_to_SM2_KEY(const ECCrefPublicKey *ref, SM2_KEY *sm2_key)
|
||||
{
|
||||
SM2_POINT point;
|
||||
|
||||
if (ref->bits != 256) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(ref->x, zeros, sizeof(zeros)) != 0
|
||||
|| memcmp(ref->y, zeros, sizeof(zeros)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_point_from_xy(&point, ref->x + ECCref_MAX_LEN - 32, ref->y + ECCref_MAX_LEN - 32) != 1
|
||||
|| sm2_key_set_public_key(sm2_key, &point) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
static int SDF_ECCSignature_to_SM2_SIGNATURE(const ECCSignature *ref, SM2_SIGNATURE *sig)
|
||||
{
|
||||
if (memcmp(ref->r, zeros, sizeof(zeros)) != 0
|
||||
|| memcmp(ref->s, zeros, sizeof(zeros)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(sig, 0, sizeof(SM2_SIGNATURE));
|
||||
memcpy(sig->r, ref->r + ECCref_MAX_LEN - 32, 32);
|
||||
memcpy(sig->s, ref->s + ECCref_MAX_LEN - 32, 32);
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int sdf_load_library(const char *so_path, const char *vendor)
|
||||
{
|
||||
if (SDF_LoadLibrary((char *)so_path, (char *)vendor) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void sdf_unload_library(void)
|
||||
{
|
||||
SDF_UnloadLibrary();
|
||||
}
|
||||
|
||||
int sdf_open_device(SDF_DEVICE *dev)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hDevice = NULL;
|
||||
void *hSession = NULL;
|
||||
DEVICEINFO devInfo;
|
||||
|
||||
if (SDF_OpenDevice(&hDevice) != SDR_OK
|
||||
|| SDF_OpenSession(hDevice, &hSession) != SDR_OK
|
||||
|| SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(dev, 0, sizeof(SDF_DEVICE));
|
||||
dev->handle = hDevice;
|
||||
hDevice = NULL;
|
||||
memcpy(dev->issuer, devInfo.IssuerName, 40);
|
||||
memcpy(dev->name, devInfo.DeviceName, 16);
|
||||
memcpy(dev->serial, devInfo.DeviceSerial, 16);
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
if (hDevice) SDF_CloseDevice(hDevice);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
DEVICEINFO devInfo;
|
||||
|
||||
if (SDF_OpenSession(dev->handle, hSession) != SDR_OK
|
||||
|| SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
SDF_PrintDeviceInfo(fp, &devInfo);
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_rand_bytes(SDF_DEVICE *dev, uint8_t *buf, size_t len)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
|
||||
if (!dev || !buf || !len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
|
||||
|| SDF_GenerateRandom(hSession, len, buf) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_load_sign_key(SDF_DEVICE *dev, SDF_KEY *key, int index, const char *pass)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
ECCrefPublicKey eccPublicKey;
|
||||
SM2_KEY public_key;
|
||||
|
||||
if (!dev || !key || !pass) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
|
||||
|| SDF_ExportSignPublicKey_ECC(hSession, index, &eccPublicKey) != SDR_OK
|
||||
|| SDF_ECCrefPublicKey_to_SM2_KEY(&eccPublicKey, &public_key) != SDR_OK
|
||||
|| SDF_GetPrivateKeyAccessRight(hSession, index, (unsigned char *)pass, strlen(pass)) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(key, 0, sizeof(SDF_KEY));
|
||||
key->public_key = public_key;
|
||||
key->session = hSession;
|
||||
key->index = index;
|
||||
hSession = NULL;
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_sign(SDF_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen)
|
||||
{
|
||||
ECCSignature ecc_sig;
|
||||
SM2_SIGNATURE sm2_sig;
|
||||
|
||||
if (!key || !dgst || !sig || !siglen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_InternalSign_ECC(key->session, key->index, (unsigned char *)dgst, 32, &ecc_sig) != SDR_OK
|
||||
|| SDF_ECCSignature_to_SM2_SIGNATURE(&ecc_sig, &sm2_sig) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
*siglen = 0;
|
||||
if (sm2_signature_to_der(&sm2_sig, &sig, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sdf_release_key(SDF_KEY *key)
|
||||
{
|
||||
if (SDF_ReleasePrivateKeyAccessRight(key->session, key->index) != SDR_OK
|
||||
|| SDF_CloseSession(key->session) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(key, 0, sizeof(SDF_KEY));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sdf_close_device(SDF_DEVICE *dev)
|
||||
{
|
||||
if (SDF_CloseDevice(dev->handle) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(dev, 0, sizeof(SDF_DEVICE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sdf.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/error.h>
|
||||
#include "sdf.h"
|
||||
#include "sdf_ext.h"
|
||||
|
||||
|
||||
|
||||
static const uint8_t zeros[ECCref_MAX_LEN - 32] = {0};
|
||||
|
||||
static int SDF_ECCrefPublicKey_to_SM2_KEY(const ECCrefPublicKey *ref, SM2_KEY *sm2_key)
|
||||
{
|
||||
SM2_POINT point;
|
||||
|
||||
if (ref->bits != 256) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(ref->x, zeros, sizeof(zeros)) != 0
|
||||
|| memcmp(ref->y, zeros, sizeof(zeros)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_point_from_xy(&point, ref->x + ECCref_MAX_LEN - 32, ref->y + ECCref_MAX_LEN - 32) != 1
|
||||
|| sm2_key_set_public_key(sm2_key, &point) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
static int SDF_ECCSignature_to_SM2_SIGNATURE(const ECCSignature *ref, SM2_SIGNATURE *sig)
|
||||
{
|
||||
if (memcmp(ref->r, zeros, sizeof(zeros)) != 0
|
||||
|| memcmp(ref->s, zeros, sizeof(zeros)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(sig, 0, sizeof(SM2_SIGNATURE));
|
||||
memcpy(sig->r, ref->r + ECCref_MAX_LEN - 32, 32);
|
||||
memcpy(sig->s, ref->s + ECCref_MAX_LEN - 32, 32);
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int sdf_load_library(const char *so_path, const char *vendor)
|
||||
{
|
||||
if (SDF_LoadLibrary((char *)so_path, (char *)vendor) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void sdf_unload_library(void)
|
||||
{
|
||||
SDF_UnloadLibrary();
|
||||
}
|
||||
|
||||
int sdf_open_device(SDF_DEVICE *dev)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hDevice = NULL;
|
||||
void *hSession = NULL;
|
||||
DEVICEINFO devInfo;
|
||||
|
||||
if (SDF_OpenDevice(&hDevice) != SDR_OK
|
||||
|| SDF_OpenSession(hDevice, &hSession) != SDR_OK
|
||||
|| SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(dev, 0, sizeof(SDF_DEVICE));
|
||||
dev->handle = hDevice;
|
||||
hDevice = NULL;
|
||||
memcpy(dev->issuer, devInfo.IssuerName, 40);
|
||||
memcpy(dev->name, devInfo.DeviceName, 16);
|
||||
memcpy(dev->serial, devInfo.DeviceSerial, 16);
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
if (hDevice) SDF_CloseDevice(hDevice);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
DEVICEINFO devInfo;
|
||||
|
||||
if (SDF_OpenSession(dev->handle, hSession) != SDR_OK
|
||||
|| SDF_GetDeviceInfo(hSession, &devInfo) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
SDF_PrintDeviceInfo(fp, &devInfo);
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_rand_bytes(SDF_DEVICE *dev, uint8_t *buf, size_t len)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
|
||||
if (!dev || !buf || !len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
|
||||
|| SDF_GenerateRandom(hSession, len, buf) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_load_sign_key(SDF_DEVICE *dev, SDF_KEY *key, int index, const char *pass)
|
||||
{
|
||||
int ret = -1;
|
||||
void *hSession = NULL;
|
||||
ECCrefPublicKey eccPublicKey;
|
||||
SM2_KEY public_key;
|
||||
|
||||
if (!dev || !key || !pass) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_OpenSession(dev->handle, &hSession) != SDR_OK
|
||||
|| SDF_ExportSignPublicKey_ECC(hSession, index, &eccPublicKey) != SDR_OK
|
||||
|| SDF_ECCrefPublicKey_to_SM2_KEY(&eccPublicKey, &public_key) != SDR_OK
|
||||
|| SDF_GetPrivateKeyAccessRight(hSession, index, (unsigned char *)pass, strlen(pass)) != SDR_OK) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(key, 0, sizeof(SDF_KEY));
|
||||
key->public_key = public_key;
|
||||
key->session = hSession;
|
||||
key->index = index;
|
||||
hSession = NULL;
|
||||
ret = 1;
|
||||
end:
|
||||
if (hSession) SDF_CloseSession(hSession);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int sdf_sign(SDF_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen)
|
||||
{
|
||||
ECCSignature ecc_sig;
|
||||
SM2_SIGNATURE sm2_sig;
|
||||
|
||||
if (!key || !dgst || !sig || !siglen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (SDF_InternalSign_ECC(key->session, key->index, (unsigned char *)dgst, 32, &ecc_sig) != SDR_OK
|
||||
|| SDF_ECCSignature_to_SM2_SIGNATURE(&ecc_sig, &sm2_sig) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
*siglen = 0;
|
||||
if (sm2_signature_to_der(&sm2_sig, &sig, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sdf_release_key(SDF_KEY *key)
|
||||
{
|
||||
if (SDF_ReleasePrivateKeyAccessRight(key->session, key->index) != SDR_OK
|
||||
|| SDF_CloseSession(key->session) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(key, 0, sizeof(SDF_KEY));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sdf_close_device(SDF_DEVICE *dev)
|
||||
{
|
||||
if (SDF_CloseDevice(dev->handle) != SDR_OK) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
memset(dev, 0, sizeof(SDF_DEVICE));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,7 +7,6 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* SDF API is a cryptographic API for PCI-E cards defined in standard
|
||||
* GM/T 0018-2012: Interface Specifications of Cryptography Device Application
|
||||
|
||||
1443
src/sdf/sdf_dummy.c
1443
src/sdf/sdf_dummy.c
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,257 +7,256 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <gmssl/error.h>
|
||||
#include "sdf_int.h"
|
||||
#include "sdf_sansec.h"
|
||||
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
|
||||
typedef struct {
|
||||
ULONG id;
|
||||
char *name;
|
||||
} table_item_t;
|
||||
|
||||
static table_item_t sdf_cipher_caps[] = {
|
||||
{ SGD_SM1_ECB, "sm1-ecb" },
|
||||
{ SGD_SM1_CBC, "sm1-cbc" },
|
||||
{ SGD_SM1_CFB, "sm1-cfb" },
|
||||
{ SGD_SM1_OFB, "sm1-ofb128" },
|
||||
{ SGD_SM1_MAC, "cbcmac-sm1" },
|
||||
{ SGD_SSF33_ECB, "ssf33-ecb" },
|
||||
{ SGD_SSF33_CBC, "ssf33-cbc" },
|
||||
{ SGD_SSF33_CFB, "ssf33-cfb" },
|
||||
{ SGD_SSF33_OFB, "ssf33-ofb128" },
|
||||
{ SGD_SSF33_MAC, "cbcmac-ssf33" },
|
||||
{ SGD_SM4_ECB, "sms4-ecb" },
|
||||
{ SGD_SM4_CBC, "sms4-cbc" },
|
||||
{ SGD_SM4_CFB, "sms4-cfb" },
|
||||
{ SGD_SM4_OFB, "sms4-ofb128" },
|
||||
{ SGD_SM4_MAC, "cbcmac-sms4" },
|
||||
{ SGD_ZUC_EEA3, "zuc_128eea3" },
|
||||
{ SGD_ZUC_EIA3, "zuc_128eia3" }
|
||||
};
|
||||
|
||||
static table_item_t sdf_digest_caps[] = {
|
||||
{ SGD_SM3, "sm3" },
|
||||
{ SGD_SHA1, "sha1" },
|
||||
{ SGD_SHA256, "sha256" },
|
||||
};
|
||||
|
||||
static table_item_t sdf_pkey_caps[] = {
|
||||
{ SGD_RSA_SIGN, "rsa" },
|
||||
{ SGD_RSA_ENC, "rsaEncryption" },
|
||||
{ SGD_SM2_1, "sm2sign" },
|
||||
{ SGD_SM2_2, "sm2exchange" },
|
||||
{ SGD_SM2_3, "sm2encrypt" }
|
||||
};
|
||||
|
||||
int SDF_PrintDeviceInfo(FILE *fp, const DEVICEINFO *pstDeviceInfo)
|
||||
{
|
||||
size_t i, n;
|
||||
DEVICEINFO buf;
|
||||
DEVICEINFO *devInfo = &buf;
|
||||
int fmt = 0, ind = 4;
|
||||
|
||||
memcpy(devInfo, pstDeviceInfo, sizeof(DEVICEINFO));
|
||||
devInfo->IssuerName[39] = 0;
|
||||
devInfo->DeviceName[15] = 0;
|
||||
devInfo->DeviceSerial[15] = 0;
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Device Name", devInfo->DeviceName);
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Serial Number", devInfo->DeviceSerial);
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Issuer", devInfo->IssuerName);
|
||||
format_print(fp, fmt, ind, "%-18s: %u\n", "Hardware Version", devInfo->DeviceVersion);
|
||||
format_print(fp, fmt, ind, "%-18s: %u\n", "Standard Version", devInfo->StandardVersion);
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Public Key Algors");
|
||||
for (i = n = 0; i < sizeof(sdf_pkey_caps)/sizeof(sdf_pkey_caps[0]); i++) {
|
||||
if ((devInfo->AsymAlgAbility[0] & sdf_pkey_caps[i].id) ==
|
||||
sdf_pkey_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_pkey_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Ciphers");
|
||||
for (i = n = 0; i < sizeof(sdf_cipher_caps)/sizeof(sdf_cipher_caps[0]); i++) {
|
||||
if ((devInfo->SymAlgAbility & sdf_cipher_caps[i].id) ==
|
||||
sdf_cipher_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_cipher_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Digests");
|
||||
for (i = n = 0; i < sizeof(sdf_digest_caps)/sizeof(sdf_digest_caps[0]); i++) {
|
||||
if ((devInfo->HashAlgAbility & sdf_digest_caps[i].id) ==
|
||||
sdf_digest_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_digest_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintRSAPublicKey(FILE *fp, const RSArefPublicKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d\n", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
|
||||
(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintRSAPrivateKey(FILE *fp, const RSArefPrivateKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
|
||||
(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
|
||||
(void)format_bytes(fp, fmt, ind, "d", blob->d, sizeof(blob->d));
|
||||
(void)format_bytes(fp, fmt, ind, "prime[0]", blob->prime[0], sizeof(blob->prime[0]));
|
||||
(void)format_bytes(fp, fmt, ind, "prime[1]", blob->prime[1], sizeof(blob->prime[1]));
|
||||
(void)format_bytes(fp, fmt, ind, "pexp[0]", blob->pexp[0], sizeof(blob->pexp[0]));
|
||||
(void)format_bytes(fp, fmt, ind, "pexp[1]", blob->pexp[1], sizeof(blob->pexp[1]));
|
||||
(void)format_bytes(fp, fmt, ind, "coef", blob->coef, sizeof(blob->coef));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCPublicKey(FILE *fp, const ECCrefPublicKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
|
||||
(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCPrivateKey(FILE *fp, const ECCrefPrivateKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "K", blob->K, sizeof(blob->K));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCCipher(FILE *fp, const ECCCipher *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
|
||||
(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
|
||||
(void)format_bytes(fp, fmt, ind, "M", blob->M, sizeof(blob->M));
|
||||
(void)format_print(fp, fmt, ind, "L: %d", blob->L);
|
||||
(void)format_bytes(fp, fmt, ind, "C", blob->C, sizeof(blob->C));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCSignature(FILE *fp, const ECCSignature *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_bytes(fp, fmt, ind, "r", blob->r, sizeof(blob->r));
|
||||
(void)format_bytes(fp, fmt, ind, "s", blob->s, sizeof(blob->s));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_ImportKey(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle)
|
||||
{
|
||||
(void)hSessionHandle;
|
||||
(void)pucKey;
|
||||
(void)uiKeyLength;
|
||||
(void)phKeyHandle;
|
||||
SDFerr(SDF_F_SDF_IMPORTKEY, SDF_R_NOT_IMPLEMENTED);
|
||||
return SDR_NOTSUPPORT;
|
||||
}
|
||||
|
||||
int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen)
|
||||
{
|
||||
ECCCipher *ecc_cipher = NULL;
|
||||
size_t len;
|
||||
|
||||
if (!cipher) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return SDR_INARGERR;
|
||||
}
|
||||
|
||||
if (!ulDataLen || ulDataLen > INT_MAX) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER,
|
||||
SDF_R_INVALID_SM2_CIPHERTEXT_LENGTH);
|
||||
return SDR_INARGERR;
|
||||
}
|
||||
|
||||
len = sizeof(ECCCipher) - 1 + ulDataLen;
|
||||
if (len < sizeof(SANSEC_ECCCipher)) {
|
||||
len = sizeof(SANSEC_ECCCipher);
|
||||
}
|
||||
|
||||
if (!(ecc_cipher = malloc(len))) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_MALLOC_FAILURE);
|
||||
return SDR_NOBUFFER;
|
||||
}
|
||||
memset(ecc_cipher, 0, sizeof(*ecc_cipher));
|
||||
|
||||
ecc_cipher->L = (unsigned int)ulDataLen;
|
||||
|
||||
*cipher = ecc_cipher;
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_FreeECCCipher(ECCCipher *cipher)
|
||||
{
|
||||
free(cipher);
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
const char *SDF_GetErrorReason(int err)
|
||||
{
|
||||
switch (err) {
|
||||
case SDR_OK: return "SDR_OK";
|
||||
case SDR_BASE: return "SDR_BASE";
|
||||
case SDR_UNKNOWERR: return "SDR_UNKNOWERR";
|
||||
case SDR_NOTSUPPORT: return "SDR_NOTSUPPORT";
|
||||
case SDR_COMMFAIL: return "SDR_COMMFAIL";
|
||||
case SDR_HARDFAIL: return "SDR_HARDFAIL";
|
||||
case SDR_OPENDEVICE: return "SDR_OPENDEVICE";
|
||||
case SDR_OPENSESSION: return "SDR_OPENSESSION";
|
||||
case SDR_PARDENY: return "SDR_PARDENY";
|
||||
case SDR_KEYNOTEXIST: return "SDR_KEYNOTEXIST";
|
||||
case SDR_ALGNOTSUPPORT: return "SDR_ALGNOTSUPPORT";
|
||||
case SDR_ALGMODNOTSUPPORT: return "SDR_ALGMODNOTSUPPORT";
|
||||
case SDR_PKOPERR: return "SDR_PKOPERR";
|
||||
case SDR_SKOPERR: return "SDR_SKOPERR";
|
||||
case SDR_SIGNERR: return "SDR_SIGNERR";
|
||||
case SDR_VERIFYERR: return "SDR_VERIFYERR";
|
||||
case SDR_SYMOPERR: return "SDR_SYMOPERR";
|
||||
case SDR_STEPERR: return "SDR_STEPERR";
|
||||
case SDR_FILESIZEERR: return "SDR_FILESIZEERR";
|
||||
case SDR_FILENOEXIST: return "SDR_FILENOEXIST";
|
||||
case SDR_FILEOFSERR: return "SDR_FILEOFSERR";
|
||||
case SDR_KEYTYPEERR: return "SDR_KEYTYPEERR";
|
||||
case SDR_KEYERR: return "SDR_KEYERR";
|
||||
case SDR_ENCDATAERR: return "SDR_ENCDATAERR";
|
||||
case SDR_RANDERR: return "SDR_RANDERR";
|
||||
case SDR_PRKRERR: return "SDR_PRKRERR";
|
||||
case SDR_MACERR: return "SDR_MACERR";
|
||||
case SDR_FILEEXSITS: return "SDR_FILEEXSITS";
|
||||
case SDR_FILEWERR: return "SDR_FILEWERR";
|
||||
case SDR_NOBUFFER: return "SDR_NOBUFFER";
|
||||
case SDR_INARGERR: return "SDR_INARGERR";
|
||||
case SDR_OUTARGERR: return "SDR_OUTARGERR";
|
||||
}
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <limits.h>
|
||||
#include <gmssl/error.h>
|
||||
#include "sdf_int.h"
|
||||
#include "sdf_sansec.h"
|
||||
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
|
||||
typedef struct {
|
||||
ULONG id;
|
||||
char *name;
|
||||
} table_item_t;
|
||||
|
||||
static table_item_t sdf_cipher_caps[] = {
|
||||
{ SGD_SM1_ECB, "sm1-ecb" },
|
||||
{ SGD_SM1_CBC, "sm1-cbc" },
|
||||
{ SGD_SM1_CFB, "sm1-cfb" },
|
||||
{ SGD_SM1_OFB, "sm1-ofb128" },
|
||||
{ SGD_SM1_MAC, "cbcmac-sm1" },
|
||||
{ SGD_SSF33_ECB, "ssf33-ecb" },
|
||||
{ SGD_SSF33_CBC, "ssf33-cbc" },
|
||||
{ SGD_SSF33_CFB, "ssf33-cfb" },
|
||||
{ SGD_SSF33_OFB, "ssf33-ofb128" },
|
||||
{ SGD_SSF33_MAC, "cbcmac-ssf33" },
|
||||
{ SGD_SM4_ECB, "sms4-ecb" },
|
||||
{ SGD_SM4_CBC, "sms4-cbc" },
|
||||
{ SGD_SM4_CFB, "sms4-cfb" },
|
||||
{ SGD_SM4_OFB, "sms4-ofb128" },
|
||||
{ SGD_SM4_MAC, "cbcmac-sms4" },
|
||||
{ SGD_ZUC_EEA3, "zuc_128eea3" },
|
||||
{ SGD_ZUC_EIA3, "zuc_128eia3" }
|
||||
};
|
||||
|
||||
static table_item_t sdf_digest_caps[] = {
|
||||
{ SGD_SM3, "sm3" },
|
||||
{ SGD_SHA1, "sha1" },
|
||||
{ SGD_SHA256, "sha256" },
|
||||
};
|
||||
|
||||
static table_item_t sdf_pkey_caps[] = {
|
||||
{ SGD_RSA_SIGN, "rsa" },
|
||||
{ SGD_RSA_ENC, "rsaEncryption" },
|
||||
{ SGD_SM2_1, "sm2sign" },
|
||||
{ SGD_SM2_2, "sm2exchange" },
|
||||
{ SGD_SM2_3, "sm2encrypt" }
|
||||
};
|
||||
|
||||
int SDF_PrintDeviceInfo(FILE *fp, const DEVICEINFO *pstDeviceInfo)
|
||||
{
|
||||
size_t i, n;
|
||||
DEVICEINFO buf;
|
||||
DEVICEINFO *devInfo = &buf;
|
||||
int fmt = 0, ind = 4;
|
||||
|
||||
memcpy(devInfo, pstDeviceInfo, sizeof(DEVICEINFO));
|
||||
devInfo->IssuerName[39] = 0;
|
||||
devInfo->DeviceName[15] = 0;
|
||||
devInfo->DeviceSerial[15] = 0;
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Device Name", devInfo->DeviceName);
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Serial Number", devInfo->DeviceSerial);
|
||||
format_print(fp, fmt, ind, "%-18s: %s\n", "Issuer", devInfo->IssuerName);
|
||||
format_print(fp, fmt, ind, "%-18s: %u\n", "Hardware Version", devInfo->DeviceVersion);
|
||||
format_print(fp, fmt, ind, "%-18s: %u\n", "Standard Version", devInfo->StandardVersion);
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Public Key Algors");
|
||||
for (i = n = 0; i < sizeof(sdf_pkey_caps)/sizeof(sdf_pkey_caps[0]); i++) {
|
||||
if ((devInfo->AsymAlgAbility[0] & sdf_pkey_caps[i].id) ==
|
||||
sdf_pkey_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_pkey_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Ciphers");
|
||||
for (i = n = 0; i < sizeof(sdf_cipher_caps)/sizeof(sdf_cipher_caps[0]); i++) {
|
||||
if ((devInfo->SymAlgAbility & sdf_cipher_caps[i].id) ==
|
||||
sdf_cipher_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_cipher_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
|
||||
format_print(fp, fmt, ind, "%-18s: ", "Digests");
|
||||
for (i = n = 0; i < sizeof(sdf_digest_caps)/sizeof(sdf_digest_caps[0]); i++) {
|
||||
if ((devInfo->HashAlgAbility & sdf_digest_caps[i].id) ==
|
||||
sdf_digest_caps[i].id) {
|
||||
format_print(fp, fmt, 0, "%s%s", n ? "," : "", sdf_digest_caps[i].name);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
format_print(fp, fmt, 0, "\n");
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintRSAPublicKey(FILE *fp, const RSArefPublicKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d\n", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
|
||||
(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintRSAPrivateKey(FILE *fp, const RSArefPrivateKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "m", blob->m, sizeof(blob->m));
|
||||
(void)format_bytes(fp, fmt, ind, "e", blob->e, sizeof(blob->e));
|
||||
(void)format_bytes(fp, fmt, ind, "d", blob->d, sizeof(blob->d));
|
||||
(void)format_bytes(fp, fmt, ind, "prime[0]", blob->prime[0], sizeof(blob->prime[0]));
|
||||
(void)format_bytes(fp, fmt, ind, "prime[1]", blob->prime[1], sizeof(blob->prime[1]));
|
||||
(void)format_bytes(fp, fmt, ind, "pexp[0]", blob->pexp[0], sizeof(blob->pexp[0]));
|
||||
(void)format_bytes(fp, fmt, ind, "pexp[1]", blob->pexp[1], sizeof(blob->pexp[1]));
|
||||
(void)format_bytes(fp, fmt, ind, "coef", blob->coef, sizeof(blob->coef));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCPublicKey(FILE *fp, const ECCrefPublicKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
|
||||
(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCPrivateKey(FILE *fp, const ECCrefPrivateKey *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_print(fp, fmt, ind, "bits: %d", blob->bits);
|
||||
(void)format_bytes(fp, fmt, ind, "K", blob->K, sizeof(blob->K));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCCipher(FILE *fp, const ECCCipher *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_bytes(fp, fmt, ind, "x", blob->x, sizeof(blob->x));
|
||||
(void)format_bytes(fp, fmt, ind, "y", blob->y, sizeof(blob->y));
|
||||
(void)format_bytes(fp, fmt, ind, "M", blob->M, sizeof(blob->M));
|
||||
(void)format_print(fp, fmt, ind, "L: %d", blob->L);
|
||||
(void)format_bytes(fp, fmt, ind, "C", blob->C, sizeof(blob->C));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_PrintECCSignature(FILE *fp, const ECCSignature *blob)
|
||||
{
|
||||
int fmt = 0, ind = 4;
|
||||
(void)format_bytes(fp, fmt, ind, "r", blob->r, sizeof(blob->r));
|
||||
(void)format_bytes(fp, fmt, ind, "s", blob->s, sizeof(blob->s));
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_ImportKey(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle)
|
||||
{
|
||||
(void)hSessionHandle;
|
||||
(void)pucKey;
|
||||
(void)uiKeyLength;
|
||||
(void)phKeyHandle;
|
||||
SDFerr(SDF_F_SDF_IMPORTKEY, SDF_R_NOT_IMPLEMENTED);
|
||||
return SDR_NOTSUPPORT;
|
||||
}
|
||||
|
||||
int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen)
|
||||
{
|
||||
ECCCipher *ecc_cipher = NULL;
|
||||
size_t len;
|
||||
|
||||
if (!cipher) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_PASSED_NULL_PARAMETER);
|
||||
return SDR_INARGERR;
|
||||
}
|
||||
|
||||
if (!ulDataLen || ulDataLen > INT_MAX) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER,
|
||||
SDF_R_INVALID_SM2_CIPHERTEXT_LENGTH);
|
||||
return SDR_INARGERR;
|
||||
}
|
||||
|
||||
len = sizeof(ECCCipher) - 1 + ulDataLen;
|
||||
if (len < sizeof(SANSEC_ECCCipher)) {
|
||||
len = sizeof(SANSEC_ECCCipher);
|
||||
}
|
||||
|
||||
if (!(ecc_cipher = malloc(len))) {
|
||||
SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_MALLOC_FAILURE);
|
||||
return SDR_NOBUFFER;
|
||||
}
|
||||
memset(ecc_cipher, 0, sizeof(*ecc_cipher));
|
||||
|
||||
ecc_cipher->L = (unsigned int)ulDataLen;
|
||||
|
||||
*cipher = ecc_cipher;
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
int SDF_FreeECCCipher(ECCCipher *cipher)
|
||||
{
|
||||
free(cipher);
|
||||
return SDR_OK;
|
||||
}
|
||||
|
||||
const char *SDF_GetErrorReason(int err)
|
||||
{
|
||||
switch (err) {
|
||||
case SDR_OK: return "SDR_OK";
|
||||
case SDR_BASE: return "SDR_BASE";
|
||||
case SDR_UNKNOWERR: return "SDR_UNKNOWERR";
|
||||
case SDR_NOTSUPPORT: return "SDR_NOTSUPPORT";
|
||||
case SDR_COMMFAIL: return "SDR_COMMFAIL";
|
||||
case SDR_HARDFAIL: return "SDR_HARDFAIL";
|
||||
case SDR_OPENDEVICE: return "SDR_OPENDEVICE";
|
||||
case SDR_OPENSESSION: return "SDR_OPENSESSION";
|
||||
case SDR_PARDENY: return "SDR_PARDENY";
|
||||
case SDR_KEYNOTEXIST: return "SDR_KEYNOTEXIST";
|
||||
case SDR_ALGNOTSUPPORT: return "SDR_ALGNOTSUPPORT";
|
||||
case SDR_ALGMODNOTSUPPORT: return "SDR_ALGMODNOTSUPPORT";
|
||||
case SDR_PKOPERR: return "SDR_PKOPERR";
|
||||
case SDR_SKOPERR: return "SDR_SKOPERR";
|
||||
case SDR_SIGNERR: return "SDR_SIGNERR";
|
||||
case SDR_VERIFYERR: return "SDR_VERIFYERR";
|
||||
case SDR_SYMOPERR: return "SDR_SYMOPERR";
|
||||
case SDR_STEPERR: return "SDR_STEPERR";
|
||||
case SDR_FILESIZEERR: return "SDR_FILESIZEERR";
|
||||
case SDR_FILENOEXIST: return "SDR_FILENOEXIST";
|
||||
case SDR_FILEOFSERR: return "SDR_FILEOFSERR";
|
||||
case SDR_KEYTYPEERR: return "SDR_KEYTYPEERR";
|
||||
case SDR_KEYERR: return "SDR_KEYERR";
|
||||
case SDR_ENCDATAERR: return "SDR_ENCDATAERR";
|
||||
case SDR_RANDERR: return "SDR_RANDERR";
|
||||
case SDR_PRKRERR: return "SDR_PRKRERR";
|
||||
case SDR_MACERR: return "SDR_MACERR";
|
||||
case SDR_FILEEXSITS: return "SDR_FILEEXSITS";
|
||||
case SDR_FILEWERR: return "SDR_FILEWERR";
|
||||
case SDR_NOBUFFER: return "SDR_NOBUFFER";
|
||||
case SDR_INARGERR: return "SDR_INARGERR";
|
||||
case SDR_OUTARGERR: return "SDR_OUTARGERR";
|
||||
}
|
||||
return "(unknown)";
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,49 +7,48 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef SDFUTIL_SDF_EXT_H
|
||||
#define SDFUTIL_SDF_EXT_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "../sgd.h"
|
||||
#include "sdf.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define SDF_MIN_KEY_INDEX 1 /* defined by GM/T 0018 */
|
||||
#define SDF_MAX_KEY_INDEX 32 /* defined by GmSSL */
|
||||
#define SDF_MIN_PASSWORD_LENGTH 8 /* defined by GM/T 0018 */
|
||||
#define SDF_MAX_PASSWORD_LENGTH 255 /* defined by GmSSL */
|
||||
#define SDF_MAX_FILE_SIZE (256 * 1024)
|
||||
|
||||
|
||||
|
||||
int SDF_LoadLibrary(char *so_path, char *vendor);
|
||||
int SDF_UnloadLibrary(void);
|
||||
int SDF_ImportKey(void *hSessionHandle, unsigned char *pucKey,
|
||||
unsigned int uiKeyLength, void **phKeyHandle);
|
||||
|
||||
int SDF_PrintDeviceInfo(FILE *fp, const DEVICEINFO *devInfo);
|
||||
int SDF_PrintRSAPublicKey(FILE *fp, const RSArefPublicKey *ref);
|
||||
int SDF_PrintRSAPrivateKey(FILE *fp, const RSArefPrivateKey *ref);
|
||||
int SDF_PrintECCPublicKey(FILE *fp, const ECCrefPublicKey *ref);
|
||||
int SDF_PrintECCPrivateKey(FILE *fp, const ECCrefPrivateKey *ref);
|
||||
int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen); // FIMXE: 和GmSSL的内存使用方式不同
|
||||
int SDF_FreeECCCipher(ECCCipher *cipher);
|
||||
int SDF_PrintECCCipher(FILE *out, ECCCipher *cipher);
|
||||
int SDF_PrintECCSignature(FILE *out, ECCSignature *sig);
|
||||
const char *SDF_GetErrorReason(int err);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifndef SDFUTIL_SDF_EXT_H
|
||||
#define SDFUTIL_SDF_EXT_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include "../sgd.h"
|
||||
#include "sdf.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define SDF_MIN_KEY_INDEX 1 /* defined by GM/T 0018 */
|
||||
#define SDF_MAX_KEY_INDEX 32 /* defined by GmSSL */
|
||||
#define SDF_MIN_PASSWORD_LENGTH 8 /* defined by GM/T 0018 */
|
||||
#define SDF_MAX_PASSWORD_LENGTH 255 /* defined by GmSSL */
|
||||
#define SDF_MAX_FILE_SIZE (256 * 1024)
|
||||
|
||||
|
||||
|
||||
int SDF_LoadLibrary(char *so_path, char *vendor);
|
||||
int SDF_UnloadLibrary(void);
|
||||
int SDF_ImportKey(void *hSessionHandle, unsigned char *pucKey,
|
||||
unsigned int uiKeyLength, void **phKeyHandle);
|
||||
|
||||
int SDF_PrintDeviceInfo(FILE *fp, const DEVICEINFO *devInfo);
|
||||
int SDF_PrintRSAPublicKey(FILE *fp, const RSArefPublicKey *ref);
|
||||
int SDF_PrintRSAPrivateKey(FILE *fp, const RSArefPrivateKey *ref);
|
||||
int SDF_PrintECCPublicKey(FILE *fp, const ECCrefPublicKey *ref);
|
||||
int SDF_PrintECCPrivateKey(FILE *fp, const ECCrefPrivateKey *ref);
|
||||
int SDF_NewECCCipher(ECCCipher **cipher, size_t ulDataLen); // FIMXE: 和GmSSL的内存使用方式不同
|
||||
int SDF_FreeECCCipher(ECCCipher *cipher);
|
||||
int SDF_PrintECCCipher(FILE *out, ECCCipher *cipher);
|
||||
int SDF_PrintECCSignature(FILE *out, ECCSignature *sig);
|
||||
const char *SDF_GetErrorReason(int err);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,416 +7,415 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef SDFUTIL_SDF_METH_H
|
||||
#define SDFUTIL_SDF_METH_H
|
||||
|
||||
#include "sdf.h"
|
||||
|
||||
typedef int (*SDF_OpenDevice_FuncPtr)(
|
||||
void **phDeviceHandle);
|
||||
|
||||
typedef int (*SDF_CloseDevice_FuncPtr)(
|
||||
void *hDeviceHandle);
|
||||
|
||||
typedef int (*SDF_OpenSession_FuncPtr)(
|
||||
void *hDeviceHandle,
|
||||
void **phSessionHandle);
|
||||
|
||||
typedef int (*SDF_CloseSession_FuncPtr)(
|
||||
void *hSessionHandle);
|
||||
|
||||
typedef int (*SDF_GetDeviceInfo_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
DEVICEINFO *pstDeviceInfo);
|
||||
|
||||
typedef int (*SDF_GenerateRandom_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiLength,
|
||||
unsigned char *pucRandom);
|
||||
|
||||
typedef int (*SDF_GetPrivateKeyAccessRight_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucPassword,
|
||||
unsigned int uiPwdLength);
|
||||
|
||||
typedef int (*SDF_ReleasePrivateKeyAccessRight_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex);
|
||||
|
||||
typedef int (*SDF_ExportSignPublicKey_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_ExportEncPublicKey_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyPair_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
RSArefPrivateKey *pucPrivateKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithIPK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithEPK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithISK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDEInput,
|
||||
unsigned int uiDELength,
|
||||
unsigned char *pucDEOutput,
|
||||
unsigned int *puiDELength);
|
||||
|
||||
typedef int (*SDF_ExportSignPublicKey_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
ECCrefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_ExportEncPublicKey_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
ECCrefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyPair_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKeyBits,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCrefPrivateKey *pucPrivateKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithIPK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithEPK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithISK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateAgreementDataWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucSponsorID,
|
||||
unsigned int uiSponsorIDLength,
|
||||
ECCrefPublicKey *pucSponsorPublicKey,
|
||||
ECCrefPublicKey *pucSponsorTmpPublicKey,
|
||||
void **phAgreementHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucResponseID,
|
||||
unsigned int uiResponseIDLength,
|
||||
ECCrefPublicKey *pucResponsePublicKey,
|
||||
ECCrefPublicKey *pucResponseTmpPublicKey,
|
||||
void *hAgreementHandle,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucResponseID,
|
||||
unsigned int uiResponseIDLength,
|
||||
unsigned char *pucSponsorID,
|
||||
unsigned int uiSponsorIDLength,
|
||||
ECCrefPublicKey *pucSponsorPublicKey,
|
||||
ECCrefPublicKey *pucSponsorTmpPublicKey,
|
||||
ECCrefPublicKey *pucResponsePublicKey,
|
||||
ECCrefPublicKey *pucResponseTmpPublicKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCCipher *pucEncDataIn,
|
||||
ECCCipher *pucEncDataOut);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithKEK_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKEKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithKEK_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKEKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_DestroyKey_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExternalPublicKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_InternalPublicKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_InternalPrivateKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_ExternalVerify_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_InternalSign_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_InternalVerify_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_ExternalEncrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCCipher *pucEncData);
|
||||
|
||||
typedef int (*SDF_ExternalDecrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPrivateKey *pucPrivateKey,
|
||||
ECCCipher *pucEncData,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_InternalEncrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCCipher *pucEncData);
|
||||
|
||||
typedef int (*SDF_InternalDecrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiAlgID,
|
||||
ECCCipher *pucEncData,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_Encrypt_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
unsigned char *pucEncData,
|
||||
unsigned int *puiEncDataLength);
|
||||
|
||||
typedef int (*SDF_Decrypt_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucEncData,
|
||||
unsigned int uiEncDataLength,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_CalculateMAC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
unsigned char *pucMAC,
|
||||
unsigned int *puiMACLength);
|
||||
|
||||
typedef int (*SDF_HashInit_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucID,
|
||||
unsigned int uiIDLength);
|
||||
|
||||
typedef int (*SDF_HashUpdate_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength);
|
||||
|
||||
typedef int (*SDF_HashFinal_FuncPtr)(void *hSessionHandle,
|
||||
unsigned char *pucHash,
|
||||
unsigned int *puiHashLength);
|
||||
|
||||
typedef int (*SDF_CreateObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiFileSize);
|
||||
|
||||
typedef int (*SDF_ReadObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiOffset,
|
||||
unsigned int *puiReadLength,
|
||||
unsigned char *pucBuffer);
|
||||
|
||||
typedef int (*SDF_WriteObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiOffset,
|
||||
unsigned int uiWriteLength,
|
||||
unsigned char *pucBuffer);
|
||||
|
||||
typedef int (*SDF_DeleteObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen);
|
||||
|
||||
typedef struct sdf_method_st {
|
||||
char *name;
|
||||
void *dso;
|
||||
SDF_OpenDevice_FuncPtr OpenDevice;
|
||||
SDF_CloseDevice_FuncPtr CloseDevice;
|
||||
SDF_OpenSession_FuncPtr OpenSession;
|
||||
SDF_CloseSession_FuncPtr CloseSession;
|
||||
SDF_GetDeviceInfo_FuncPtr GetDeviceInfo;
|
||||
SDF_GenerateRandom_FuncPtr GenerateRandom;
|
||||
SDF_GetPrivateKeyAccessRight_FuncPtr GetPrivateKeyAccessRight;
|
||||
SDF_ReleasePrivateKeyAccessRight_FuncPtr ReleasePrivateKeyAccessRight;
|
||||
SDF_ExportSignPublicKey_RSA_FuncPtr ExportSignPublicKey_RSA;
|
||||
SDF_ExportEncPublicKey_RSA_FuncPtr ExportEncPublicKey_RSA;
|
||||
SDF_GenerateKeyPair_RSA_FuncPtr GenerateKeyPair_RSA;
|
||||
SDF_GenerateKeyWithIPK_RSA_FuncPtr GenerateKeyWithIPK_RSA;
|
||||
SDF_GenerateKeyWithEPK_RSA_FuncPtr GenerateKeyWithEPK_RSA;
|
||||
SDF_ImportKeyWithISK_RSA_FuncPtr ImportKeyWithISK_RSA;
|
||||
SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr ExchangeDigitEnvelopeBaseOnRSA;
|
||||
SDF_ExportSignPublicKey_ECC_FuncPtr ExportSignPublicKey_ECC;
|
||||
SDF_ExportEncPublicKey_ECC_FuncPtr ExportEncPublicKey_ECC;
|
||||
SDF_GenerateKeyPair_ECC_FuncPtr GenerateKeyPair_ECC;
|
||||
SDF_GenerateKeyWithIPK_ECC_FuncPtr GenerateKeyWithIPK_ECC;
|
||||
SDF_GenerateKeyWithEPK_ECC_FuncPtr GenerateKeyWithEPK_ECC;
|
||||
SDF_ImportKeyWithISK_ECC_FuncPtr ImportKeyWithISK_ECC;
|
||||
SDF_GenerateAgreementDataWithECC_FuncPtr GenerateAgreementDataWithECC;
|
||||
SDF_GenerateKeyWithECC_FuncPtr GenerateKeyWithECC;
|
||||
SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr GenerateAgreementDataAndKeyWithECC;
|
||||
SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr ExchangeDigitEnvelopeBaseOnECC;
|
||||
SDF_GenerateKeyWithKEK_FuncPtr GenerateKeyWithKEK;
|
||||
SDF_ImportKeyWithKEK_FuncPtr ImportKeyWithKEK;
|
||||
SDF_DestroyKey_FuncPtr DestroyKey;
|
||||
SDF_ExternalPublicKeyOperation_RSA_FuncPtr ExternalPublicKeyOperation_RSA;
|
||||
SDF_InternalPublicKeyOperation_RSA_FuncPtr InternalPublicKeyOperation_RSA;
|
||||
SDF_InternalPrivateKeyOperation_RSA_FuncPtr InternalPrivateKeyOperation_RSA;
|
||||
SDF_ExternalVerify_ECC_FuncPtr ExternalVerify_ECC;
|
||||
SDF_InternalSign_ECC_FuncPtr InternalSign_ECC;
|
||||
SDF_InternalVerify_ECC_FuncPtr InternalVerify_ECC;
|
||||
SDF_ExternalEncrypt_ECC_FuncPtr ExternalEncrypt_ECC;
|
||||
SDF_ExternalDecrypt_ECC_FuncPtr ExternalDecrypt_ECC;
|
||||
SDF_InternalEncrypt_ECC_FuncPtr InternalEncrypt_ECC;
|
||||
SDF_InternalDecrypt_ECC_FuncPtr InternalDecrypt_ECC;
|
||||
SDF_Encrypt_FuncPtr Encrypt;
|
||||
SDF_Decrypt_FuncPtr Decrypt;
|
||||
SDF_CalculateMAC_FuncPtr CalculateMAC;
|
||||
SDF_HashInit_FuncPtr HashInit;
|
||||
SDF_HashUpdate_FuncPtr HashUpdate;
|
||||
SDF_HashFinal_FuncPtr HashFinal;
|
||||
SDF_CreateObject_FuncPtr CreateObject;
|
||||
SDF_ReadObject_FuncPtr ReadObject;
|
||||
SDF_WriteObject_FuncPtr WriteObject;
|
||||
SDF_DeleteObject_FuncPtr DeleteObject;
|
||||
} SDF_METHOD;
|
||||
|
||||
SDF_METHOD *SDF_METHOD_load_library(const char *so_path);
|
||||
void SDF_METHOD_free(SDF_METHOD *meth);
|
||||
|
||||
|
||||
typedef struct sdf_vendor_st {
|
||||
char *name;
|
||||
unsigned int (*cipher_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*cipher_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*cipher_cap)(unsigned int vendor_cap);
|
||||
unsigned int (*digest_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*digest_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*digest_cap)(unsigned int vendor_cap);
|
||||
unsigned int (*pkey_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*pkey_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*pkey_cap)(unsigned int vendor_cap);
|
||||
int (*encode_ecccipher)(const ECCCipher *a, void *buf);
|
||||
int (*decode_ecccipher)(ECCCipher *a, const void *buf);
|
||||
unsigned long (*get_error_reason)(int err);
|
||||
} SDF_VENDOR;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef SDFUTIL_SDF_METH_H
|
||||
#define SDFUTIL_SDF_METH_H
|
||||
|
||||
#include "sdf.h"
|
||||
|
||||
typedef int (*SDF_OpenDevice_FuncPtr)(
|
||||
void **phDeviceHandle);
|
||||
|
||||
typedef int (*SDF_CloseDevice_FuncPtr)(
|
||||
void *hDeviceHandle);
|
||||
|
||||
typedef int (*SDF_OpenSession_FuncPtr)(
|
||||
void *hDeviceHandle,
|
||||
void **phSessionHandle);
|
||||
|
||||
typedef int (*SDF_CloseSession_FuncPtr)(
|
||||
void *hSessionHandle);
|
||||
|
||||
typedef int (*SDF_GetDeviceInfo_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
DEVICEINFO *pstDeviceInfo);
|
||||
|
||||
typedef int (*SDF_GenerateRandom_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiLength,
|
||||
unsigned char *pucRandom);
|
||||
|
||||
typedef int (*SDF_GetPrivateKeyAccessRight_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucPassword,
|
||||
unsigned int uiPwdLength);
|
||||
|
||||
typedef int (*SDF_ReleasePrivateKeyAccessRight_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex);
|
||||
|
||||
typedef int (*SDF_ExportSignPublicKey_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_ExportEncPublicKey_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyPair_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
RSArefPrivateKey *pucPrivateKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithIPK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithEPK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithISK_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDEInput,
|
||||
unsigned int uiDELength,
|
||||
unsigned char *pucDEOutput,
|
||||
unsigned int *puiDELength);
|
||||
|
||||
typedef int (*SDF_ExportSignPublicKey_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
ECCrefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_ExportEncPublicKey_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
ECCrefPublicKey *pucPublicKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyPair_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKeyBits,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCrefPrivateKey *pucPrivateKey);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithIPK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithEPK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithISK_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
ECCCipher *pucKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateAgreementDataWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucSponsorID,
|
||||
unsigned int uiSponsorIDLength,
|
||||
ECCrefPublicKey *pucSponsorPublicKey,
|
||||
ECCrefPublicKey *pucSponsorTmpPublicKey,
|
||||
void **phAgreementHandle);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucResponseID,
|
||||
unsigned int uiResponseIDLength,
|
||||
ECCrefPublicKey *pucResponsePublicKey,
|
||||
ECCrefPublicKey *pucResponseTmpPublicKey,
|
||||
void *hAgreementHandle,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned char *pucResponseID,
|
||||
unsigned int uiResponseIDLength,
|
||||
unsigned char *pucSponsorID,
|
||||
unsigned int uiSponsorIDLength,
|
||||
ECCrefPublicKey *pucSponsorPublicKey,
|
||||
ECCrefPublicKey *pucSponsorTmpPublicKey,
|
||||
ECCrefPublicKey *pucResponsePublicKey,
|
||||
ECCrefPublicKey *pucResponseTmpPublicKey,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
ECCCipher *pucEncDataIn,
|
||||
ECCCipher *pucEncDataOut);
|
||||
|
||||
typedef int (*SDF_GenerateKeyWithKEK_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyBits,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKEKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int *puiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_ImportKeyWithKEK_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned int uiKEKIndex,
|
||||
unsigned char *pucKey,
|
||||
unsigned int uiKeyLength,
|
||||
void **phKeyHandle);
|
||||
|
||||
typedef int (*SDF_DestroyKey_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle);
|
||||
|
||||
typedef int (*SDF_ExternalPublicKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
RSArefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_InternalPublicKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_InternalPrivateKeyOperation_RSA_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiKeyIndex,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
unsigned char *pucDataOutput,
|
||||
unsigned int *puiOutputLength);
|
||||
|
||||
typedef int (*SDF_ExternalVerify_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucDataInput,
|
||||
unsigned int uiInputLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_InternalSign_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_InternalVerify_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCSignature *pucSignature);
|
||||
|
||||
typedef int (*SDF_ExternalEncrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCCipher *pucEncData);
|
||||
|
||||
typedef int (*SDF_ExternalDecrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPrivateKey *pucPrivateKey,
|
||||
ECCCipher *pucEncData,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_InternalEncrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiIPKIndex,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
ECCCipher *pucEncData);
|
||||
|
||||
typedef int (*SDF_InternalDecrypt_ECC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiISKIndex,
|
||||
unsigned int uiAlgID,
|
||||
ECCCipher *pucEncData,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_Encrypt_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
unsigned char *pucEncData,
|
||||
unsigned int *puiEncDataLength);
|
||||
|
||||
typedef int (*SDF_Decrypt_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucEncData,
|
||||
unsigned int uiEncDataLength,
|
||||
unsigned char *pucData,
|
||||
unsigned int *puiDataLength);
|
||||
|
||||
typedef int (*SDF_CalculateMAC_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
void *hKeyHandle,
|
||||
unsigned int uiAlgID,
|
||||
unsigned char *pucIV,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength,
|
||||
unsigned char *pucMAC,
|
||||
unsigned int *puiMACLength);
|
||||
|
||||
typedef int (*SDF_HashInit_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned int uiAlgID,
|
||||
ECCrefPublicKey *pucPublicKey,
|
||||
unsigned char *pucID,
|
||||
unsigned int uiIDLength);
|
||||
|
||||
typedef int (*SDF_HashUpdate_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucData,
|
||||
unsigned int uiDataLength);
|
||||
|
||||
typedef int (*SDF_HashFinal_FuncPtr)(void *hSessionHandle,
|
||||
unsigned char *pucHash,
|
||||
unsigned int *puiHashLength);
|
||||
|
||||
typedef int (*SDF_CreateObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiFileSize);
|
||||
|
||||
typedef int (*SDF_ReadObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiOffset,
|
||||
unsigned int *puiReadLength,
|
||||
unsigned char *pucBuffer);
|
||||
|
||||
typedef int (*SDF_WriteObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen,
|
||||
unsigned int uiOffset,
|
||||
unsigned int uiWriteLength,
|
||||
unsigned char *pucBuffer);
|
||||
|
||||
typedef int (*SDF_DeleteObject_FuncPtr)(
|
||||
void *hSessionHandle,
|
||||
unsigned char *pucFileName,
|
||||
unsigned int uiNameLen);
|
||||
|
||||
typedef struct sdf_method_st {
|
||||
char *name;
|
||||
void *dso;
|
||||
SDF_OpenDevice_FuncPtr OpenDevice;
|
||||
SDF_CloseDevice_FuncPtr CloseDevice;
|
||||
SDF_OpenSession_FuncPtr OpenSession;
|
||||
SDF_CloseSession_FuncPtr CloseSession;
|
||||
SDF_GetDeviceInfo_FuncPtr GetDeviceInfo;
|
||||
SDF_GenerateRandom_FuncPtr GenerateRandom;
|
||||
SDF_GetPrivateKeyAccessRight_FuncPtr GetPrivateKeyAccessRight;
|
||||
SDF_ReleasePrivateKeyAccessRight_FuncPtr ReleasePrivateKeyAccessRight;
|
||||
SDF_ExportSignPublicKey_RSA_FuncPtr ExportSignPublicKey_RSA;
|
||||
SDF_ExportEncPublicKey_RSA_FuncPtr ExportEncPublicKey_RSA;
|
||||
SDF_GenerateKeyPair_RSA_FuncPtr GenerateKeyPair_RSA;
|
||||
SDF_GenerateKeyWithIPK_RSA_FuncPtr GenerateKeyWithIPK_RSA;
|
||||
SDF_GenerateKeyWithEPK_RSA_FuncPtr GenerateKeyWithEPK_RSA;
|
||||
SDF_ImportKeyWithISK_RSA_FuncPtr ImportKeyWithISK_RSA;
|
||||
SDF_ExchangeDigitEnvelopeBaseOnRSA_FuncPtr ExchangeDigitEnvelopeBaseOnRSA;
|
||||
SDF_ExportSignPublicKey_ECC_FuncPtr ExportSignPublicKey_ECC;
|
||||
SDF_ExportEncPublicKey_ECC_FuncPtr ExportEncPublicKey_ECC;
|
||||
SDF_GenerateKeyPair_ECC_FuncPtr GenerateKeyPair_ECC;
|
||||
SDF_GenerateKeyWithIPK_ECC_FuncPtr GenerateKeyWithIPK_ECC;
|
||||
SDF_GenerateKeyWithEPK_ECC_FuncPtr GenerateKeyWithEPK_ECC;
|
||||
SDF_ImportKeyWithISK_ECC_FuncPtr ImportKeyWithISK_ECC;
|
||||
SDF_GenerateAgreementDataWithECC_FuncPtr GenerateAgreementDataWithECC;
|
||||
SDF_GenerateKeyWithECC_FuncPtr GenerateKeyWithECC;
|
||||
SDF_GenerateAgreementDataAndKeyWithECC_FuncPtr GenerateAgreementDataAndKeyWithECC;
|
||||
SDF_ExchangeDigitEnvelopeBaseOnECC_FuncPtr ExchangeDigitEnvelopeBaseOnECC;
|
||||
SDF_GenerateKeyWithKEK_FuncPtr GenerateKeyWithKEK;
|
||||
SDF_ImportKeyWithKEK_FuncPtr ImportKeyWithKEK;
|
||||
SDF_DestroyKey_FuncPtr DestroyKey;
|
||||
SDF_ExternalPublicKeyOperation_RSA_FuncPtr ExternalPublicKeyOperation_RSA;
|
||||
SDF_InternalPublicKeyOperation_RSA_FuncPtr InternalPublicKeyOperation_RSA;
|
||||
SDF_InternalPrivateKeyOperation_RSA_FuncPtr InternalPrivateKeyOperation_RSA;
|
||||
SDF_ExternalVerify_ECC_FuncPtr ExternalVerify_ECC;
|
||||
SDF_InternalSign_ECC_FuncPtr InternalSign_ECC;
|
||||
SDF_InternalVerify_ECC_FuncPtr InternalVerify_ECC;
|
||||
SDF_ExternalEncrypt_ECC_FuncPtr ExternalEncrypt_ECC;
|
||||
SDF_ExternalDecrypt_ECC_FuncPtr ExternalDecrypt_ECC;
|
||||
SDF_InternalEncrypt_ECC_FuncPtr InternalEncrypt_ECC;
|
||||
SDF_InternalDecrypt_ECC_FuncPtr InternalDecrypt_ECC;
|
||||
SDF_Encrypt_FuncPtr Encrypt;
|
||||
SDF_Decrypt_FuncPtr Decrypt;
|
||||
SDF_CalculateMAC_FuncPtr CalculateMAC;
|
||||
SDF_HashInit_FuncPtr HashInit;
|
||||
SDF_HashUpdate_FuncPtr HashUpdate;
|
||||
SDF_HashFinal_FuncPtr HashFinal;
|
||||
SDF_CreateObject_FuncPtr CreateObject;
|
||||
SDF_ReadObject_FuncPtr ReadObject;
|
||||
SDF_WriteObject_FuncPtr WriteObject;
|
||||
SDF_DeleteObject_FuncPtr DeleteObject;
|
||||
} SDF_METHOD;
|
||||
|
||||
SDF_METHOD *SDF_METHOD_load_library(const char *so_path);
|
||||
void SDF_METHOD_free(SDF_METHOD *meth);
|
||||
|
||||
|
||||
typedef struct sdf_vendor_st {
|
||||
char *name;
|
||||
unsigned int (*cipher_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*cipher_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*cipher_cap)(unsigned int vendor_cap);
|
||||
unsigned int (*digest_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*digest_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*digest_cap)(unsigned int vendor_cap);
|
||||
unsigned int (*pkey_vendor2std)(unsigned int vendor_id);
|
||||
unsigned int (*pkey_std2vendor)(unsigned int std_id);
|
||||
unsigned int (*pkey_cap)(unsigned int vendor_cap);
|
||||
int (*encode_ecccipher)(const ECCCipher *a, void *buf);
|
||||
int (*decode_ecccipher)(ECCCipher *a, const void *buf);
|
||||
unsigned long (*get_error_reason)(int err);
|
||||
} SDF_VENDOR;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
2921
src/sdf/sdf_lib.c
2921
src/sdf/sdf_lib.c
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,100 +7,99 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
#include "sdf_int.h"
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
#define SDF_METHOD_BIND_FUNCTION_EX(func,name) \
|
||||
sdf->func = (SDF_##func##_FuncPtr)dlsym(sdf->dso, "SDF_"#name)
|
||||
|
||||
#define SDF_METHOD_BIND_FUNCTION(func) \
|
||||
SDF_METHOD_BIND_FUNCTION_EX(func,func)
|
||||
|
||||
SDF_METHOD *SDF_METHOD_load_library(const char *so_path)
|
||||
{
|
||||
SDF_METHOD *ret = NULL;
|
||||
SDF_METHOD *sdf = NULL;
|
||||
|
||||
if (!(sdf = malloc(sizeof(*sdf)))) {
|
||||
SDFerr(SDF_F_SDF_METHOD_LOAD_LIBRARY, ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
memset(sdf, 0, sizeof(*sdf));
|
||||
|
||||
if (!(sdf->dso = dlopen(so_path, RTLD_LAZY))) {
|
||||
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, dlerror());
|
||||
SDFerr(SDF_F_SDF_METHOD_LOAD_LIBRARY, SDF_R_DSO_LOAD_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
SDF_METHOD_BIND_FUNCTION(OpenDevice);
|
||||
SDF_METHOD_BIND_FUNCTION(CloseDevice);
|
||||
SDF_METHOD_BIND_FUNCTION(OpenSession);
|
||||
SDF_METHOD_BIND_FUNCTION(CloseSession);
|
||||
SDF_METHOD_BIND_FUNCTION(GetDeviceInfo);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateRandom);
|
||||
SDF_METHOD_BIND_FUNCTION(GetPrivateKeyAccessRight);
|
||||
SDF_METHOD_BIND_FUNCTION(ReleasePrivateKeyAccessRight);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportSignPublicKey_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportEncPublicKey_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyPair_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithIPK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithEPK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithISK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExchangeDigitEnvelopeBaseOnRSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportSignPublicKey_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportEncPublicKey_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyPair_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithIPK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithEPK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithISK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateAgreementDataWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateAgreementDataAndKeyWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExchangeDigitEnvelopeBaseOnECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithKEK);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithKEK);
|
||||
SDF_METHOD_BIND_FUNCTION(DestroyKey);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalPublicKeyOperation_RSA);
|
||||
//SDF_METHOD_BIND_FUNCTION(InternalPublicKeyOperation_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalPrivateKeyOperation_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalVerify_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalSign_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalVerify_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalEncrypt_ECC);
|
||||
//SDF_METHOD_BIND_FUNCTION(ExternalDecrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalEncrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalDecrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(Encrypt);
|
||||
SDF_METHOD_BIND_FUNCTION(Decrypt);
|
||||
SDF_METHOD_BIND_FUNCTION(CalculateMAC);
|
||||
SDF_METHOD_BIND_FUNCTION(HashInit);
|
||||
SDF_METHOD_BIND_FUNCTION(HashUpdate);
|
||||
SDF_METHOD_BIND_FUNCTION(HashFinal);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(CreateObject,CreateFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(ReadObject,ReadFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(WriteObject,WriteFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(DeleteObject,DeleteFile);
|
||||
|
||||
ret = sdf;
|
||||
sdf = NULL;
|
||||
|
||||
end:
|
||||
SDF_METHOD_free(sdf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SDF_METHOD_free(SDF_METHOD *meth)
|
||||
{
|
||||
if (meth) free(meth->dso);
|
||||
free(meth);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <dlfcn.h>
|
||||
#include "sdf_int.h"
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
#define SDF_METHOD_BIND_FUNCTION_EX(func,name) \
|
||||
sdf->func = (SDF_##func##_FuncPtr)dlsym(sdf->dso, "SDF_"#name)
|
||||
|
||||
#define SDF_METHOD_BIND_FUNCTION(func) \
|
||||
SDF_METHOD_BIND_FUNCTION_EX(func,func)
|
||||
|
||||
SDF_METHOD *SDF_METHOD_load_library(const char *so_path)
|
||||
{
|
||||
SDF_METHOD *ret = NULL;
|
||||
SDF_METHOD *sdf = NULL;
|
||||
|
||||
if (!(sdf = malloc(sizeof(*sdf)))) {
|
||||
SDFerr(SDF_F_SDF_METHOD_LOAD_LIBRARY, ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
memset(sdf, 0, sizeof(*sdf));
|
||||
|
||||
if (!(sdf->dso = dlopen(so_path, RTLD_LAZY))) {
|
||||
fprintf(stderr, "%s %d: %s\n", __FILE__, __LINE__, dlerror());
|
||||
SDFerr(SDF_F_SDF_METHOD_LOAD_LIBRARY, SDF_R_DSO_LOAD_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
SDF_METHOD_BIND_FUNCTION(OpenDevice);
|
||||
SDF_METHOD_BIND_FUNCTION(CloseDevice);
|
||||
SDF_METHOD_BIND_FUNCTION(OpenSession);
|
||||
SDF_METHOD_BIND_FUNCTION(CloseSession);
|
||||
SDF_METHOD_BIND_FUNCTION(GetDeviceInfo);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateRandom);
|
||||
SDF_METHOD_BIND_FUNCTION(GetPrivateKeyAccessRight);
|
||||
SDF_METHOD_BIND_FUNCTION(ReleasePrivateKeyAccessRight);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportSignPublicKey_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportEncPublicKey_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyPair_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithIPK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithEPK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithISK_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExchangeDigitEnvelopeBaseOnRSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportSignPublicKey_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExportEncPublicKey_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyPair_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithIPK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithEPK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithISK_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateAgreementDataWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateAgreementDataAndKeyWithECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExchangeDigitEnvelopeBaseOnECC);
|
||||
SDF_METHOD_BIND_FUNCTION(GenerateKeyWithKEK);
|
||||
SDF_METHOD_BIND_FUNCTION(ImportKeyWithKEK);
|
||||
SDF_METHOD_BIND_FUNCTION(DestroyKey);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalPublicKeyOperation_RSA);
|
||||
//SDF_METHOD_BIND_FUNCTION(InternalPublicKeyOperation_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalPrivateKeyOperation_RSA);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalVerify_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalSign_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalVerify_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(ExternalEncrypt_ECC);
|
||||
//SDF_METHOD_BIND_FUNCTION(ExternalDecrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalEncrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(InternalDecrypt_ECC);
|
||||
SDF_METHOD_BIND_FUNCTION(Encrypt);
|
||||
SDF_METHOD_BIND_FUNCTION(Decrypt);
|
||||
SDF_METHOD_BIND_FUNCTION(CalculateMAC);
|
||||
SDF_METHOD_BIND_FUNCTION(HashInit);
|
||||
SDF_METHOD_BIND_FUNCTION(HashUpdate);
|
||||
SDF_METHOD_BIND_FUNCTION(HashFinal);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(CreateObject,CreateFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(ReadObject,ReadFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(WriteObject,WriteFile);
|
||||
SDF_METHOD_BIND_FUNCTION_EX(DeleteObject,DeleteFile);
|
||||
|
||||
ret = sdf;
|
||||
sdf = NULL;
|
||||
|
||||
end:
|
||||
SDF_METHOD_free(sdf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SDF_METHOD_free(SDF_METHOD *meth)
|
||||
{
|
||||
if (meth) free(meth->dso);
|
||||
free(meth);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,261 +7,260 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include <string.h>
|
||||
#include "sdf.h"
|
||||
#include "sdf_int.h"
|
||||
#include "sdf_sansec.h"
|
||||
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
typedef struct {
|
||||
unsigned int std_id;
|
||||
unsigned int vendor_id;
|
||||
} SDF_ALGOR_PAIR;
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_ciphers[] = {
|
||||
{ SGD_SM1, SANSEC_SM1 },
|
||||
{ SGD_SM1_ECB, SANSEC_SM1_ECB },
|
||||
{ SGD_SM1_CBC, SANSEC_SM1_CBC },
|
||||
{ SGD_SM1_CFB, SANSEC_SM1_CFB },
|
||||
{ SGD_SM1_OFB, SANSEC_SM1_OFB },
|
||||
{ SGD_SM1_MAC, SANSEC_SM1_MAC },
|
||||
{ SGD_SM4, SANSEC_SM4 },
|
||||
{ SGD_SM4_ECB, SANSEC_SM4_ECB },
|
||||
{ SGD_SM4_CBC, SANSEC_SM4_CBC },
|
||||
{ SGD_SM4_CFB, SANSEC_SM4_CFB },
|
||||
{ SGD_SM4_OFB, SANSEC_SM4_OFB },
|
||||
{ SGD_SM4_MAC, SANSEC_SM4_MAC },
|
||||
{ SGD_SSF33, SANSEC_SSF33 },
|
||||
{ SGD_SSF33_ECB, SANSEC_SSF33_ECB },
|
||||
{ SGD_SSF33_CBC, SANSEC_SSF33_CBC },
|
||||
{ SGD_SSF33_CFB, SANSEC_SSF33_CFB },
|
||||
{ SGD_SSF33_OFB, SANSEC_SSF33_OFB },
|
||||
{ SGD_SSF33_MAC, SANSEC_SSF33_MAC },
|
||||
{ 0, SANSEC_AES },
|
||||
{ 0, SANSEC_AES_ECB },
|
||||
{ 0, SANSEC_AES_CBC },
|
||||
{ 0, SANSEC_AES_CFB },
|
||||
{ 0, SANSEC_AES_OFB },
|
||||
{ 0, SANSEC_AES_MAC },
|
||||
{ 0, SANSEC_DES },
|
||||
{ 0, SANSEC_DES_ECB },
|
||||
{ 0, SANSEC_DES_CBC },
|
||||
{ 0, SANSEC_DES_CFB },
|
||||
{ 0, SANSEC_DES_OFB },
|
||||
{ 0, SANSEC_DES_MAC },
|
||||
{ 0, SANSEC_3DES },
|
||||
{ 0, SANSEC_3DES_ECB },
|
||||
{ 0, SANSEC_3DES_CBC },
|
||||
{ 0, SANSEC_3DES_CFB },
|
||||
{ 0, SANSEC_3DES_OFB },
|
||||
{ 0, SANSEC_3DES_MAC },
|
||||
};
|
||||
|
||||
static unsigned int sansec_cipher_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (vendor_id == sansec_ciphers[i].vendor_id) {
|
||||
return sansec_ciphers[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_cipher_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (std_id == sansec_ciphers[i].std_id) {
|
||||
return sansec_ciphers[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_cipher_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (vendor_cap & sansec_ciphers[i].vendor_id) {
|
||||
std_cap |= sansec_ciphers[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_digests[] = {
|
||||
{ SGD_SM3, SANSEC_SM3 },
|
||||
{ SGD_SHA1, SANSEC_SHA1 },
|
||||
{ SGD_SHA256, SANSEC_SHA256 },
|
||||
{ 0, SANSEC_SHA512 },
|
||||
{ 0, SANSEC_SHA384 },
|
||||
{ 0, SANSEC_SHA224 },
|
||||
{ 0, SANSEC_MD5 },
|
||||
};
|
||||
|
||||
static unsigned int sansec_digest_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (vendor_id == sansec_digests[i].vendor_id) {
|
||||
return sansec_digests[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_digest_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (std_id == sansec_digests[i].std_id) {
|
||||
return sansec_digests[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_digest_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (vendor_cap & sansec_digests[i].vendor_id) {
|
||||
std_cap |= sansec_digests[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_pkeys[] = {
|
||||
{ SGD_RSA,SANSEC_RSA },
|
||||
{ SGD_RSA_SIGN,SANSEC_RSA_SIGN },
|
||||
{ SGD_RSA_ENC,SANSEC_RSA_ENC },
|
||||
{ SGD_SM2,SANSEC_SM2 },
|
||||
{ SGD_SM2_1,SANSEC_SM2_1 },
|
||||
{ SGD_SM2_2,SANSEC_SM2_2 },
|
||||
{ SGD_SM2_3,SANSEC_SM2_3 },
|
||||
};
|
||||
|
||||
static unsigned int sansec_pkey_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (vendor_id == sansec_pkeys[i].vendor_id) {
|
||||
return sansec_pkeys[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_pkey_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (std_id == sansec_pkeys[i].std_id) {
|
||||
return sansec_pkeys[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_pkey_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (vendor_cap & sansec_pkeys[i].vendor_id) {
|
||||
std_cap |= sansec_pkeys[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static int sansec_encode_ecccipher(const ECCCipher *ec, void *vendor)
|
||||
{
|
||||
int ret;
|
||||
SANSEC_ECCCipher *sansec = vendor;
|
||||
ret = sizeof(SANSEC_ECCCipher);
|
||||
|
||||
if (ec->L > sizeof(sansec->C)) {
|
||||
SDFerr(SDF_F_SANSEC_ENCODE_ECCCIPHER,
|
||||
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (vendor) {
|
||||
sansec->clength = ec->L;
|
||||
memcpy(sansec->x, ec->x, sizeof(ec->x));
|
||||
memcpy(sansec->y, ec->y, sizeof(ec->y));
|
||||
memcpy(sansec->M, ec->M, sizeof(ec->M));
|
||||
memset(sansec->M + sizeof(ec->M), 0, sizeof(sansec->M) - sizeof(ec->M));
|
||||
memcpy(sansec->C, ec->C, ec->L);
|
||||
memset(sansec->C + ec->L, 0, sizeof(sansec->C) - ec->L);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int sansec_decode_ecccipher(ECCCipher *ec, const void *vendor)
|
||||
{
|
||||
int ret;
|
||||
const SANSEC_ECCCipher *sansec = vendor;
|
||||
ret = sizeof(ECCCipher) -1 + sansec->clength;
|
||||
|
||||
if (sansec->clength > sizeof(sansec->C)) {
|
||||
SDFerr(SDF_F_SANSEC_DECODE_ECCCIPHER,
|
||||
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ec) {
|
||||
memcpy(ec->x, sansec->x, sizeof(ec->x));
|
||||
memcpy(ec->y, sansec->y, sizeof(ec->y));
|
||||
memcpy(ec->M, sansec->M, sizeof(ec->M));
|
||||
ec->L = sansec->clength;
|
||||
memcpy(ec->C, sansec->C, sansec->clength);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static unsigned long sansec_get_error_reason(int err)
|
||||
{
|
||||
/*
|
||||
size_t i = 0;
|
||||
for (i = 0; i < OSSL_NELEM(sansec_errors); i++) {
|
||||
if (err == sansec_errors[i].err) {
|
||||
return sansec_errors[i].reason;
|
||||
}
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDF_VENDOR sdf_sansec = {
|
||||
"sansec",
|
||||
sansec_cipher_vendor2std,
|
||||
sansec_cipher_std2vendor,
|
||||
sansec_cipher_cap,
|
||||
sansec_digest_vendor2std,
|
||||
sansec_digest_std2vendor,
|
||||
sansec_digest_cap,
|
||||
sansec_pkey_vendor2std,
|
||||
sansec_pkey_std2vendor,
|
||||
sansec_pkey_cap,
|
||||
sansec_encode_ecccipher,
|
||||
sansec_decode_ecccipher,
|
||||
sansec_get_error_reason,
|
||||
};
|
||||
|
||||
#include <string.h>
|
||||
#include "sdf.h"
|
||||
#include "sdf_int.h"
|
||||
#include "sdf_sansec.h"
|
||||
|
||||
|
||||
#define SDFerr(a,b)
|
||||
|
||||
typedef struct {
|
||||
unsigned int std_id;
|
||||
unsigned int vendor_id;
|
||||
} SDF_ALGOR_PAIR;
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_ciphers[] = {
|
||||
{ SGD_SM1, SANSEC_SM1 },
|
||||
{ SGD_SM1_ECB, SANSEC_SM1_ECB },
|
||||
{ SGD_SM1_CBC, SANSEC_SM1_CBC },
|
||||
{ SGD_SM1_CFB, SANSEC_SM1_CFB },
|
||||
{ SGD_SM1_OFB, SANSEC_SM1_OFB },
|
||||
{ SGD_SM1_MAC, SANSEC_SM1_MAC },
|
||||
{ SGD_SM4, SANSEC_SM4 },
|
||||
{ SGD_SM4_ECB, SANSEC_SM4_ECB },
|
||||
{ SGD_SM4_CBC, SANSEC_SM4_CBC },
|
||||
{ SGD_SM4_CFB, SANSEC_SM4_CFB },
|
||||
{ SGD_SM4_OFB, SANSEC_SM4_OFB },
|
||||
{ SGD_SM4_MAC, SANSEC_SM4_MAC },
|
||||
{ SGD_SSF33, SANSEC_SSF33 },
|
||||
{ SGD_SSF33_ECB, SANSEC_SSF33_ECB },
|
||||
{ SGD_SSF33_CBC, SANSEC_SSF33_CBC },
|
||||
{ SGD_SSF33_CFB, SANSEC_SSF33_CFB },
|
||||
{ SGD_SSF33_OFB, SANSEC_SSF33_OFB },
|
||||
{ SGD_SSF33_MAC, SANSEC_SSF33_MAC },
|
||||
{ 0, SANSEC_AES },
|
||||
{ 0, SANSEC_AES_ECB },
|
||||
{ 0, SANSEC_AES_CBC },
|
||||
{ 0, SANSEC_AES_CFB },
|
||||
{ 0, SANSEC_AES_OFB },
|
||||
{ 0, SANSEC_AES_MAC },
|
||||
{ 0, SANSEC_DES },
|
||||
{ 0, SANSEC_DES_ECB },
|
||||
{ 0, SANSEC_DES_CBC },
|
||||
{ 0, SANSEC_DES_CFB },
|
||||
{ 0, SANSEC_DES_OFB },
|
||||
{ 0, SANSEC_DES_MAC },
|
||||
{ 0, SANSEC_3DES },
|
||||
{ 0, SANSEC_3DES_ECB },
|
||||
{ 0, SANSEC_3DES_CBC },
|
||||
{ 0, SANSEC_3DES_CFB },
|
||||
{ 0, SANSEC_3DES_OFB },
|
||||
{ 0, SANSEC_3DES_MAC },
|
||||
};
|
||||
|
||||
static unsigned int sansec_cipher_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (vendor_id == sansec_ciphers[i].vendor_id) {
|
||||
return sansec_ciphers[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_cipher_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (std_id == sansec_ciphers[i].std_id) {
|
||||
return sansec_ciphers[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_cipher_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
|
||||
if (vendor_cap & sansec_ciphers[i].vendor_id) {
|
||||
std_cap |= sansec_ciphers[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_digests[] = {
|
||||
{ SGD_SM3, SANSEC_SM3 },
|
||||
{ SGD_SHA1, SANSEC_SHA1 },
|
||||
{ SGD_SHA256, SANSEC_SHA256 },
|
||||
{ 0, SANSEC_SHA512 },
|
||||
{ 0, SANSEC_SHA384 },
|
||||
{ 0, SANSEC_SHA224 },
|
||||
{ 0, SANSEC_MD5 },
|
||||
};
|
||||
|
||||
static unsigned int sansec_digest_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (vendor_id == sansec_digests[i].vendor_id) {
|
||||
return sansec_digests[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_digest_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (std_id == sansec_digests[i].std_id) {
|
||||
return sansec_digests[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_digest_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
|
||||
if (vendor_cap & sansec_digests[i].vendor_id) {
|
||||
std_cap |= sansec_digests[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static SDF_ALGOR_PAIR sansec_pkeys[] = {
|
||||
{ SGD_RSA,SANSEC_RSA },
|
||||
{ SGD_RSA_SIGN,SANSEC_RSA_SIGN },
|
||||
{ SGD_RSA_ENC,SANSEC_RSA_ENC },
|
||||
{ SGD_SM2,SANSEC_SM2 },
|
||||
{ SGD_SM2_1,SANSEC_SM2_1 },
|
||||
{ SGD_SM2_2,SANSEC_SM2_2 },
|
||||
{ SGD_SM2_3,SANSEC_SM2_3 },
|
||||
};
|
||||
|
||||
static unsigned int sansec_pkey_vendor2std(unsigned int vendor_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (vendor_id == sansec_pkeys[i].vendor_id) {
|
||||
return sansec_pkeys[i].std_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_pkey_std2vendor(unsigned int std_id)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (std_id == sansec_pkeys[i].std_id) {
|
||||
return sansec_pkeys[i].vendor_id;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static unsigned int sansec_pkey_cap(unsigned int vendor_cap)
|
||||
{
|
||||
unsigned int std_cap = 0;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
|
||||
if (vendor_cap & sansec_pkeys[i].vendor_id) {
|
||||
std_cap |= sansec_pkeys[i].std_id;
|
||||
}
|
||||
}
|
||||
|
||||
return std_cap;
|
||||
}
|
||||
|
||||
static int sansec_encode_ecccipher(const ECCCipher *ec, void *vendor)
|
||||
{
|
||||
int ret;
|
||||
SANSEC_ECCCipher *sansec = vendor;
|
||||
ret = sizeof(SANSEC_ECCCipher);
|
||||
|
||||
if (ec->L > sizeof(sansec->C)) {
|
||||
SDFerr(SDF_F_SANSEC_ENCODE_ECCCIPHER,
|
||||
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (vendor) {
|
||||
sansec->clength = ec->L;
|
||||
memcpy(sansec->x, ec->x, sizeof(ec->x));
|
||||
memcpy(sansec->y, ec->y, sizeof(ec->y));
|
||||
memcpy(sansec->M, ec->M, sizeof(ec->M));
|
||||
memset(sansec->M + sizeof(ec->M), 0, sizeof(sansec->M) - sizeof(ec->M));
|
||||
memcpy(sansec->C, ec->C, ec->L);
|
||||
memset(sansec->C + ec->L, 0, sizeof(sansec->C) - ec->L);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int sansec_decode_ecccipher(ECCCipher *ec, const void *vendor)
|
||||
{
|
||||
int ret;
|
||||
const SANSEC_ECCCipher *sansec = vendor;
|
||||
ret = sizeof(ECCCipher) -1 + sansec->clength;
|
||||
|
||||
if (sansec->clength > sizeof(sansec->C)) {
|
||||
SDFerr(SDF_F_SANSEC_DECODE_ECCCIPHER,
|
||||
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ec) {
|
||||
memcpy(ec->x, sansec->x, sizeof(ec->x));
|
||||
memcpy(ec->y, sansec->y, sizeof(ec->y));
|
||||
memcpy(ec->M, sansec->M, sizeof(ec->M));
|
||||
ec->L = sansec->clength;
|
||||
memcpy(ec->C, sansec->C, sansec->clength);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static unsigned long sansec_get_error_reason(int err)
|
||||
{
|
||||
/*
|
||||
size_t i = 0;
|
||||
for (i = 0; i < OSSL_NELEM(sansec_errors); i++) {
|
||||
if (err == sansec_errors[i].err) {
|
||||
return sansec_errors[i].reason;
|
||||
}
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDF_VENDOR sdf_sansec = {
|
||||
"sansec",
|
||||
sansec_cipher_vendor2std,
|
||||
sansec_cipher_std2vendor,
|
||||
sansec_cipher_cap,
|
||||
sansec_digest_vendor2std,
|
||||
sansec_digest_std2vendor,
|
||||
sansec_digest_cap,
|
||||
sansec_pkey_vendor2std,
|
||||
sansec_pkey_std2vendor,
|
||||
sansec_pkey_cap,
|
||||
sansec_encode_ecccipher,
|
||||
sansec_decode_ecccipher,
|
||||
sansec_get_error_reason,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
@@ -7,148 +7,147 @@
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef SDFUTIL_SDF_SANSEC_H
|
||||
#define SDFUTIL_SDF_SANSEC_H
|
||||
|
||||
#include "../sgd.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SANSEC_SM1 (SGD_SM1)
|
||||
#define SANSEC_SM1_ECB (SANSEC_SM1|SGD_ECB)
|
||||
#define SANSEC_SM1_CBC (SANSEC_SM1|SGD_CBC)
|
||||
#define SANSEC_SM1_CFB (SANSEC_SM1|SGD_CFB)
|
||||
#define SANSEC_SM1_OFB (SANSEC_SM1|SGD_OFB)
|
||||
#define SANSEC_SM1_MAC (SANSEC_SM1|SGD_MAC)
|
||||
|
||||
#define SANSEC_SM4 0x00002000
|
||||
#define SANSEC_SM4_ECB (SANSEC_SM4|SGD_ECB)
|
||||
#define SANSEC_SM4_CBC (SANSEC_SM4|SGD_CBC)
|
||||
#define SANSEC_SM4_CFB (SANSEC_SM4|SGD_CFB)
|
||||
#define SANSEC_SM4_OFB (SANSEC_SM4|SGD_OFB)
|
||||
#define SANSEC_SM4_MAC (SANSEC_SM4|SGD_MAC)
|
||||
|
||||
#define SANSEC_SSF33 (SGD_SSF33)
|
||||
#define SANSEC_SSF33_ECB (SANSEC_SSF33|SGD_ECB)
|
||||
#define SANSEC_SSF33_CBC (SANSEC_SSF33|SGD_CBC)
|
||||
#define SANSEC_SSF33_CFB (SANSEC_SSF33|SGD_CFB)
|
||||
#define SANSEC_SSF33_OFB (SANSEC_SSF33|SGD_OFB)
|
||||
#define SANSEC_SSF33_MAC (SANSEC_SSF33|SGD_MAC)
|
||||
|
||||
#define SANSEC_AES 0x00000400
|
||||
#define SANSEC_AES_ECB (SANSEC_AES|SGD_ECB)
|
||||
#define SANSEC_AES_CBC (SANSEC_AES|SGD_CBC)
|
||||
#define SANSEC_AES_CFB (SANSEC_AES|SGD_CFB)
|
||||
#define SANSEC_AES_OFB (SANSEC_AES|SGD_OFB)
|
||||
#define SANSEC_AES_MAC (SANSEC_AES|SGD_MAC)
|
||||
|
||||
#define SANSEC_DES 0x00004000
|
||||
#define SANSEC_DES_ECB (SANSEC_DES|SGD_ECB)
|
||||
#define SANSEC_DES_CBC (SANSEC_DES|SGD_CBC)
|
||||
#define SANSEC_DES_CFB (SANSEC_DES|SGD_CFB)
|
||||
#define SANSEC_DES_OFB (SANSEC_DES|SGD_OFB)
|
||||
#define SANSEC_DES_MAC (SANSEC_DES|SGD_MAC)
|
||||
|
||||
#define SANSEC_3DES 0x00000800
|
||||
#define SANSEC_3DES_ECB (SANSEC_3DES|SGD_ECB)
|
||||
#define SANSEC_3DES_CBC (SANSEC_3DES|SGD_CBC)
|
||||
#define SANSEC_3DES_CFB (SANSEC_3DES|SGD_CFB)
|
||||
#define SANSEC_3DES_OFB (SANSEC_3DES|SGD_OFB)
|
||||
#define SANSEC_3DES_MAC (SANSEC_3DES|SGD_MAC)
|
||||
|
||||
#define SANSEC_SM3 (SGD_SM3)
|
||||
#define SANSEC_SHA1 (SGD_SHA1)
|
||||
#define SANSEC_SHA256 (SGD_SHA256)
|
||||
#define SANSEC_SHA512 0x00000008
|
||||
#define SANSEC_SHA384 0x00000010
|
||||
#define SANSEC_SHA224 0x00000020
|
||||
#define SANSEC_MD5 0x00000080
|
||||
|
||||
#define SANSEC_RSA (SGD_RSA)
|
||||
#define SANSEC_RSA_SIGN (SGD_RSA_SIGN)
|
||||
#define SANSEC_RSA_ENC 0x00010200
|
||||
#define SANSEC_SM2 (SGD_SM2)
|
||||
#define SANSEC_SM2_1 (SGD_SM2_1)
|
||||
#define SANSEC_SM2_2 (SGD_SM2_2)
|
||||
#define SANSEC_SM2_3 (SGD_SM2_3)
|
||||
|
||||
#define SANSEC_BASE (SDR_BASE + 0x00010000)
|
||||
#define SANSEC_INVALID_USER (SANSEC_BASE + 0x00000001)
|
||||
#define SANSEC_INVALID_AUTHENCODE (SANSEC_BASE + 0x00000002)
|
||||
#define SANSEC_PROTOCOL_VERSION_ERROR (SANSEC_BASE + 0x00000003)
|
||||
#define SANSEC_INVALID_COMMAND (SANSEC_BASE + 0x00000004)
|
||||
#define SANSEC_INVALID_PARAMETERS (SANSEC_BASE + 0x00000005)
|
||||
#define SANSEC_FILE_ALREADY_EXIST (SANSEC_BASE + 0x00000006)
|
||||
#define SANSEC_SYNC_ERROR (SANSEC_BASE + 0x00000007)
|
||||
#define SANSEC_SYNC_LOGIN_ERROR (SANSEC_BASE + 0x00000008)
|
||||
#define SANSEC_SOCKET_TIMEOUT (SANSEC_BASE + 0x00000100)
|
||||
#define SANSEC_CONNECT_ERROR (SANSEC_BASE + 0x00000101)
|
||||
#define SANSEC_SET_SOCKET_OPTION_ERROR (SANSEC_BASE + 0x00000102)
|
||||
#define SANSEC_SOCKET_SEND_ERROR (SANSEC_BASE + 0x00000104)
|
||||
#define SANSEC_SOCKET_RECV_ERROR (SANSEC_BASE + 0x00000105)
|
||||
#define SANSEC_SOCKET_RECV_0 (SANSEC_BASE + 0x00000106)
|
||||
#define SANSEC_SEM_TIMEOUT (SANSEC_BASE + 0x00000200)
|
||||
#define SANSEC_NO_AVAILABLE_HSM (SANSEC_BASE + 0x00000201)
|
||||
#define SANSEC_NO_AVAILABLE_CSM (SANSEC_BASE + 0x00000202)
|
||||
#define SANSEC_CONFIG_ERROR (SANSEC_BASE + 0x00000301)
|
||||
#define SANSEC_CARD_BASE (SDR_BASE + 0x00020000)
|
||||
#define SANSEC_CARD_UNKNOW_ERROR (SANSEC_CARD_BASE + 0x00000001)
|
||||
#define SANSEC_CARD_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000002)
|
||||
#define SANSEC_CARD_COMMMUCATION_FAILED (SANSEC_CARD_BASE + 0x00000003)
|
||||
#define SANSEC_CARD_HARDWARE_FAILURE (SANSEC_CARD_BASE + 0x00000004)
|
||||
#define SANSEC_CARD_OPEN_DEVICE_FAILED (SANSEC_CARD_BASE + 0x00000005)
|
||||
#define SANSEC_CARD_OPEN_SESSION_FAILED (SANSEC_CARD_BASE + 0x00000006)
|
||||
#define SANSEC_CARD_PRIVATE_KEY_ACCESS_DENYED (SANSEC_CARD_BASE + 0x00000007)
|
||||
#define SANSEC_CARD_KEY_NOT_EXIST (SANSEC_CARD_BASE + 0x00000008)
|
||||
#define SANSEC_CARD_ALGOR_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000009)
|
||||
#define SANSEC_CARD_ALG_MODE_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000010)
|
||||
#define SANSEC_CARD_PUBLIC_KEY_OPERATION_ERROR (SANSEC_CARD_BASE + 0x00000011)
|
||||
#define SANSEC_CARD_PRIVATE_KEY_OPERATION_ERROR (SANSEC_CARD_BASE + 0x00000012)
|
||||
#define SANSEC_CARD_SIGN_ERROR (SANSEC_CARD_BASE + 0x00000013)
|
||||
#define SANSEC_CARD_VERIFY_ERROR (SANSEC_CARD_BASE + 0x00000014)
|
||||
#define SANSEC_CARD_SYMMETRIC_ALGOR_ERROR (SANSEC_CARD_BASE + 0x00000015)
|
||||
#define SANSEC_CARD_STEP_ERROR (SANSEC_CARD_BASE + 0x00000016)
|
||||
#define SANSEC_CARD_FILE_SIZE_ERROR (SANSEC_CARD_BASE + 0x00000017)
|
||||
#define SANSEC_CARD_FILE_NOT_EXIST (SANSEC_CARD_BASE + 0x00000018)
|
||||
#define SANSEC_CARD_FILE_OFFSET_ERROR (SANSEC_CARD_BASE + 0x00000019)
|
||||
#define SANSEC_CARD_KEY_TYPE_ERROR (SANSEC_CARD_BASE + 0x00000020)
|
||||
#define SANSEC_CARD_KEY_ERROR (SANSEC_CARD_BASE + 0x00000021)
|
||||
#define SANSEC_CARD_BUFFER_TOO_SMALL (SANSEC_CARD_BASE + 0x00000101)
|
||||
#define SANSEC_CARD_DATA_PADDING_ERROR (SANSEC_CARD_BASE + 0x00000102)
|
||||
#define SANSEC_CARD_DATA_SIZE (SANSEC_CARD_BASE + 0x00000103)
|
||||
#define SANSEC_CARD_CRYPTO_NOT_INITED (SANSEC_CARD_BASE + 0x00000104)
|
||||
#define SANSEC_CARD_MANAGEMENT_DENYED (SANSEC_CARD_BASE + 0x00001001)
|
||||
#define SANSEC_CARD_OPERATION_DENYED (SANSEC_CARD_BASE + 0x00001002)
|
||||
#define SANSEC_CARD_DEVICE_STATUS_ERROR (SANSEC_CARD_BASE + 0x00001003)
|
||||
#define SANSEC_CARD_LOGIN_ERROR (SANSEC_CARD_BASE + 0x00001011)
|
||||
#define SANSEC_CARD_USERID_ERROR (SANSEC_CARD_BASE + 0x00001012)
|
||||
#define SANSEC_CARD_PARAMENT_ERROR (SANSEC_CARD_BASE + 0x00001013)
|
||||
#define SANSEC_CARD_MANAGEMENT_DENYED_05 (SANSEC_CARD_BASE + 0x00000801)
|
||||
#define SANSEC_CARD_OPERATION_DENYED_05 (SANSEC_CARD_BASE + 0x00000802)
|
||||
#define SANSEC_CARD_DEVICE_STATUS_ERROR_05 (SANSEC_CARD_BASE + 0x00000803)
|
||||
#define SANSEC_CARD_LOGIN_ERROR_05 (SANSEC_CARD_BASE + 0x00000811)
|
||||
#define SANSEC_CARD_USERID_ERROR_05 (SANSEC_CARD_BASE + 0x00000812)
|
||||
#define SANSEC_CARD_PARAMENT_ERROR_05 (SANSEC_CARD_BASE + 0x00000813)
|
||||
#define SANSEC_CARD_READER_BASE (SDR_BASE + 0x00030000)
|
||||
#define SANSEC_CARD_READER_PIN_ERROR (SANSEC_CARD_READER_BASE + 0x000063CE)
|
||||
#define SANSEC_CARD_READER_NO_CARD (SANSEC_CARD_READER_BASE + 0x0000FF01)
|
||||
#define SANSEC_CARD_READER_CARD_INSERT (SANSEC_CARD_READER_BASE + 0x0000FF02)
|
||||
#define SANSEC_CARD_READER_CARD_INSERT_TYPE (SANSEC_CARD_READER_BASE + 0x0000FF03)
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct {
|
||||
unsigned int clength;
|
||||
unsigned char x[ECCref_MAX_LEN];
|
||||
unsigned char y[ECCref_MAX_LEN];
|
||||
unsigned char C[136];
|
||||
unsigned char M[ECCref_MAX_LEN];
|
||||
} SANSEC_ECCCipher;
|
||||
#pragma pack()
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
#ifndef SDFUTIL_SDF_SANSEC_H
|
||||
#define SDFUTIL_SDF_SANSEC_H
|
||||
|
||||
#include "../sgd.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SANSEC_SM1 (SGD_SM1)
|
||||
#define SANSEC_SM1_ECB (SANSEC_SM1|SGD_ECB)
|
||||
#define SANSEC_SM1_CBC (SANSEC_SM1|SGD_CBC)
|
||||
#define SANSEC_SM1_CFB (SANSEC_SM1|SGD_CFB)
|
||||
#define SANSEC_SM1_OFB (SANSEC_SM1|SGD_OFB)
|
||||
#define SANSEC_SM1_MAC (SANSEC_SM1|SGD_MAC)
|
||||
|
||||
#define SANSEC_SM4 0x00002000
|
||||
#define SANSEC_SM4_ECB (SANSEC_SM4|SGD_ECB)
|
||||
#define SANSEC_SM4_CBC (SANSEC_SM4|SGD_CBC)
|
||||
#define SANSEC_SM4_CFB (SANSEC_SM4|SGD_CFB)
|
||||
#define SANSEC_SM4_OFB (SANSEC_SM4|SGD_OFB)
|
||||
#define SANSEC_SM4_MAC (SANSEC_SM4|SGD_MAC)
|
||||
|
||||
#define SANSEC_SSF33 (SGD_SSF33)
|
||||
#define SANSEC_SSF33_ECB (SANSEC_SSF33|SGD_ECB)
|
||||
#define SANSEC_SSF33_CBC (SANSEC_SSF33|SGD_CBC)
|
||||
#define SANSEC_SSF33_CFB (SANSEC_SSF33|SGD_CFB)
|
||||
#define SANSEC_SSF33_OFB (SANSEC_SSF33|SGD_OFB)
|
||||
#define SANSEC_SSF33_MAC (SANSEC_SSF33|SGD_MAC)
|
||||
|
||||
#define SANSEC_AES 0x00000400
|
||||
#define SANSEC_AES_ECB (SANSEC_AES|SGD_ECB)
|
||||
#define SANSEC_AES_CBC (SANSEC_AES|SGD_CBC)
|
||||
#define SANSEC_AES_CFB (SANSEC_AES|SGD_CFB)
|
||||
#define SANSEC_AES_OFB (SANSEC_AES|SGD_OFB)
|
||||
#define SANSEC_AES_MAC (SANSEC_AES|SGD_MAC)
|
||||
|
||||
#define SANSEC_DES 0x00004000
|
||||
#define SANSEC_DES_ECB (SANSEC_DES|SGD_ECB)
|
||||
#define SANSEC_DES_CBC (SANSEC_DES|SGD_CBC)
|
||||
#define SANSEC_DES_CFB (SANSEC_DES|SGD_CFB)
|
||||
#define SANSEC_DES_OFB (SANSEC_DES|SGD_OFB)
|
||||
#define SANSEC_DES_MAC (SANSEC_DES|SGD_MAC)
|
||||
|
||||
#define SANSEC_3DES 0x00000800
|
||||
#define SANSEC_3DES_ECB (SANSEC_3DES|SGD_ECB)
|
||||
#define SANSEC_3DES_CBC (SANSEC_3DES|SGD_CBC)
|
||||
#define SANSEC_3DES_CFB (SANSEC_3DES|SGD_CFB)
|
||||
#define SANSEC_3DES_OFB (SANSEC_3DES|SGD_OFB)
|
||||
#define SANSEC_3DES_MAC (SANSEC_3DES|SGD_MAC)
|
||||
|
||||
#define SANSEC_SM3 (SGD_SM3)
|
||||
#define SANSEC_SHA1 (SGD_SHA1)
|
||||
#define SANSEC_SHA256 (SGD_SHA256)
|
||||
#define SANSEC_SHA512 0x00000008
|
||||
#define SANSEC_SHA384 0x00000010
|
||||
#define SANSEC_SHA224 0x00000020
|
||||
#define SANSEC_MD5 0x00000080
|
||||
|
||||
#define SANSEC_RSA (SGD_RSA)
|
||||
#define SANSEC_RSA_SIGN (SGD_RSA_SIGN)
|
||||
#define SANSEC_RSA_ENC 0x00010200
|
||||
#define SANSEC_SM2 (SGD_SM2)
|
||||
#define SANSEC_SM2_1 (SGD_SM2_1)
|
||||
#define SANSEC_SM2_2 (SGD_SM2_2)
|
||||
#define SANSEC_SM2_3 (SGD_SM2_3)
|
||||
|
||||
#define SANSEC_BASE (SDR_BASE + 0x00010000)
|
||||
#define SANSEC_INVALID_USER (SANSEC_BASE + 0x00000001)
|
||||
#define SANSEC_INVALID_AUTHENCODE (SANSEC_BASE + 0x00000002)
|
||||
#define SANSEC_PROTOCOL_VERSION_ERROR (SANSEC_BASE + 0x00000003)
|
||||
#define SANSEC_INVALID_COMMAND (SANSEC_BASE + 0x00000004)
|
||||
#define SANSEC_INVALID_PARAMETERS (SANSEC_BASE + 0x00000005)
|
||||
#define SANSEC_FILE_ALREADY_EXIST (SANSEC_BASE + 0x00000006)
|
||||
#define SANSEC_SYNC_ERROR (SANSEC_BASE + 0x00000007)
|
||||
#define SANSEC_SYNC_LOGIN_ERROR (SANSEC_BASE + 0x00000008)
|
||||
#define SANSEC_SOCKET_TIMEOUT (SANSEC_BASE + 0x00000100)
|
||||
#define SANSEC_CONNECT_ERROR (SANSEC_BASE + 0x00000101)
|
||||
#define SANSEC_SET_SOCKET_OPTION_ERROR (SANSEC_BASE + 0x00000102)
|
||||
#define SANSEC_SOCKET_SEND_ERROR (SANSEC_BASE + 0x00000104)
|
||||
#define SANSEC_SOCKET_RECV_ERROR (SANSEC_BASE + 0x00000105)
|
||||
#define SANSEC_SOCKET_RECV_0 (SANSEC_BASE + 0x00000106)
|
||||
#define SANSEC_SEM_TIMEOUT (SANSEC_BASE + 0x00000200)
|
||||
#define SANSEC_NO_AVAILABLE_HSM (SANSEC_BASE + 0x00000201)
|
||||
#define SANSEC_NO_AVAILABLE_CSM (SANSEC_BASE + 0x00000202)
|
||||
#define SANSEC_CONFIG_ERROR (SANSEC_BASE + 0x00000301)
|
||||
#define SANSEC_CARD_BASE (SDR_BASE + 0x00020000)
|
||||
#define SANSEC_CARD_UNKNOW_ERROR (SANSEC_CARD_BASE + 0x00000001)
|
||||
#define SANSEC_CARD_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000002)
|
||||
#define SANSEC_CARD_COMMMUCATION_FAILED (SANSEC_CARD_BASE + 0x00000003)
|
||||
#define SANSEC_CARD_HARDWARE_FAILURE (SANSEC_CARD_BASE + 0x00000004)
|
||||
#define SANSEC_CARD_OPEN_DEVICE_FAILED (SANSEC_CARD_BASE + 0x00000005)
|
||||
#define SANSEC_CARD_OPEN_SESSION_FAILED (SANSEC_CARD_BASE + 0x00000006)
|
||||
#define SANSEC_CARD_PRIVATE_KEY_ACCESS_DENYED (SANSEC_CARD_BASE + 0x00000007)
|
||||
#define SANSEC_CARD_KEY_NOT_EXIST (SANSEC_CARD_BASE + 0x00000008)
|
||||
#define SANSEC_CARD_ALGOR_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000009)
|
||||
#define SANSEC_CARD_ALG_MODE_NOT_SUPPORTED (SANSEC_CARD_BASE + 0x00000010)
|
||||
#define SANSEC_CARD_PUBLIC_KEY_OPERATION_ERROR (SANSEC_CARD_BASE + 0x00000011)
|
||||
#define SANSEC_CARD_PRIVATE_KEY_OPERATION_ERROR (SANSEC_CARD_BASE + 0x00000012)
|
||||
#define SANSEC_CARD_SIGN_ERROR (SANSEC_CARD_BASE + 0x00000013)
|
||||
#define SANSEC_CARD_VERIFY_ERROR (SANSEC_CARD_BASE + 0x00000014)
|
||||
#define SANSEC_CARD_SYMMETRIC_ALGOR_ERROR (SANSEC_CARD_BASE + 0x00000015)
|
||||
#define SANSEC_CARD_STEP_ERROR (SANSEC_CARD_BASE + 0x00000016)
|
||||
#define SANSEC_CARD_FILE_SIZE_ERROR (SANSEC_CARD_BASE + 0x00000017)
|
||||
#define SANSEC_CARD_FILE_NOT_EXIST (SANSEC_CARD_BASE + 0x00000018)
|
||||
#define SANSEC_CARD_FILE_OFFSET_ERROR (SANSEC_CARD_BASE + 0x00000019)
|
||||
#define SANSEC_CARD_KEY_TYPE_ERROR (SANSEC_CARD_BASE + 0x00000020)
|
||||
#define SANSEC_CARD_KEY_ERROR (SANSEC_CARD_BASE + 0x00000021)
|
||||
#define SANSEC_CARD_BUFFER_TOO_SMALL (SANSEC_CARD_BASE + 0x00000101)
|
||||
#define SANSEC_CARD_DATA_PADDING_ERROR (SANSEC_CARD_BASE + 0x00000102)
|
||||
#define SANSEC_CARD_DATA_SIZE (SANSEC_CARD_BASE + 0x00000103)
|
||||
#define SANSEC_CARD_CRYPTO_NOT_INITED (SANSEC_CARD_BASE + 0x00000104)
|
||||
#define SANSEC_CARD_MANAGEMENT_DENYED (SANSEC_CARD_BASE + 0x00001001)
|
||||
#define SANSEC_CARD_OPERATION_DENYED (SANSEC_CARD_BASE + 0x00001002)
|
||||
#define SANSEC_CARD_DEVICE_STATUS_ERROR (SANSEC_CARD_BASE + 0x00001003)
|
||||
#define SANSEC_CARD_LOGIN_ERROR (SANSEC_CARD_BASE + 0x00001011)
|
||||
#define SANSEC_CARD_USERID_ERROR (SANSEC_CARD_BASE + 0x00001012)
|
||||
#define SANSEC_CARD_PARAMENT_ERROR (SANSEC_CARD_BASE + 0x00001013)
|
||||
#define SANSEC_CARD_MANAGEMENT_DENYED_05 (SANSEC_CARD_BASE + 0x00000801)
|
||||
#define SANSEC_CARD_OPERATION_DENYED_05 (SANSEC_CARD_BASE + 0x00000802)
|
||||
#define SANSEC_CARD_DEVICE_STATUS_ERROR_05 (SANSEC_CARD_BASE + 0x00000803)
|
||||
#define SANSEC_CARD_LOGIN_ERROR_05 (SANSEC_CARD_BASE + 0x00000811)
|
||||
#define SANSEC_CARD_USERID_ERROR_05 (SANSEC_CARD_BASE + 0x00000812)
|
||||
#define SANSEC_CARD_PARAMENT_ERROR_05 (SANSEC_CARD_BASE + 0x00000813)
|
||||
#define SANSEC_CARD_READER_BASE (SDR_BASE + 0x00030000)
|
||||
#define SANSEC_CARD_READER_PIN_ERROR (SANSEC_CARD_READER_BASE + 0x000063CE)
|
||||
#define SANSEC_CARD_READER_NO_CARD (SANSEC_CARD_READER_BASE + 0x0000FF01)
|
||||
#define SANSEC_CARD_READER_CARD_INSERT (SANSEC_CARD_READER_BASE + 0x0000FF02)
|
||||
#define SANSEC_CARD_READER_CARD_INSERT_TYPE (SANSEC_CARD_READER_BASE + 0x0000FF03)
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct {
|
||||
unsigned int clength;
|
||||
unsigned char x[ECCref_MAX_LEN];
|
||||
unsigned char y[ECCref_MAX_LEN];
|
||||
unsigned char C[136];
|
||||
unsigned char M[ECCref_MAX_LEN];
|
||||
} SANSEC_ECCCipher;
|
||||
#pragma pack()
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user