Add SDF pkey encrypt/decrypt support

This commit is contained in:
Zhi Guan
2018-03-30 19:51:49 +08:00
parent 92971350c4
commit a94e307888
11 changed files with 5522 additions and 5360 deletions

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -75,6 +75,7 @@ static ERR_STRING_DATA SDF_str_functs[] = {
{ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHISK_RSA), "SDF_ImportKeyWithISK_RSA"},
{ERR_FUNC(SDF_F_SDF_IMPORTKEYWITHKEK), "SDF_ImportKeyWithKEK"},
{ERR_FUNC(SDF_F_SDF_INTERNALDECRYPT_ECC), "SDF_InternalDecrypt_ECC"},
{ERR_FUNC(SDF_F_SDF_INTERNALENCRYPT_ECC), "SDF_InternalEncrypt_ECC"},
{ERR_FUNC(SDF_F_SDF_INTERNALPRIVATEKEYOPERATION_RSA),
"SDF_InternalPrivateKeyOperation_RSA"},
{ERR_FUNC(SDF_F_SDF_INTERNALPUBLICKEYOPERATION_RSA),
@@ -83,6 +84,7 @@ static ERR_STRING_DATA SDF_str_functs[] = {
{ERR_FUNC(SDF_F_SDF_INTERNALVERIFY_ECC), "SDF_InternalVerify_ECC"},
{ERR_FUNC(SDF_F_SDF_LOADLIBRARY), "SDF_LoadLibrary"},
{ERR_FUNC(SDF_F_SDF_METHOD_LOAD_LIBRARY), "SDF_METHOD_load_library"},
{ERR_FUNC(SDF_F_SDF_NEWECCCIPHER), "SDF_NewECCCipher"},
{ERR_FUNC(SDF_F_SDF_OPENDEVICE), "SDF_OpenDevice"},
{ERR_FUNC(SDF_F_SDF_OPENSESSION), "SDF_OpenSession"},
{ERR_FUNC(SDF_F_SDF_READFILE), "SDF_ReadFile"},
@@ -117,6 +119,8 @@ static ERR_STRING_DATA SDF_str_reasons[] = {
"invalid sansec ecccipher length"},
{ERR_REASON(SDF_R_INVALID_SDF_LIBRARY), "invalid sdf library"},
{ERR_REASON(SDF_R_INVALID_SESSION_HANDLE), "invalid session handle"},
{ERR_REASON(SDF_R_INVALID_SM2_CIPHERTEXT_LENGTH),
"invalid sm2 ciphertext length"},
{ERR_REASON(SDF_R_KEY_NOT_EXIST), "key not exist"},
{ERR_REASON(SDF_R_LOAD_LIBRARY_FAILURE), "load library failure"},
{ERR_REASON(SDF_R_MAC_ERROR), "mac error"},

View File

@@ -52,7 +52,7 @@
#include <openssl/gmsdf.h>
#include "internal/sdf_int.h"
#include "../../e_os.h"
#include "sdf_sansec.h"
static void print_str(const char *name, const void *value)
@@ -302,3 +302,41 @@ int SDF_ImportKey(
{
return 0;
}
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 = OPENSSL_zalloc(len))) {
SDFerr(SDF_F_SDF_NEWECCCIPHER, ERR_R_MALLOC_FAILURE);
return SDR_NOBUFFER;
}
ecc_cipher->L = (unsigned int)ulDataLen;
*cipher = ecc_cipher;
return SDR_OK;
}
int SDF_FreeECCCipher(ECCCipher *cipher)
{
OPENSSL_free(cipher);
return SDR_OK;
}

View File

@@ -1162,6 +1162,83 @@ int SDF_ExternalEncrypt_ECC(
return SDR_OK;
}
int SDF_InternalEncrypt_ECC(
void *hSessionHandle,
unsigned int uiIPKIndex,
unsigned int uiAlgID,
unsigned char *pucData,
unsigned int uiDataLength,
ECCCipher *pucEncData)
{
int ret = SDR_UNKNOWERR;
ECCCipher *buf = pucEncData;
if (!sdf_method || !sdf_method->InternalEncrypt_ECC) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC, SDF_R_NOT_INITIALIZED);
return SDR_NOTSUPPORT;
}
if (pucEncData->L < uiDataLength) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC, SDF_R_BUFFER_TOO_SMALL);
return SDR_NOBUFFER;
}
if (sdf_vendor && sdf_vendor->decode_ecccipher) {
if (SDF_NewECCCipher(&buf, uiDataLength) != SDR_OK) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC, ERR_R_SDF_LIB);
return SDR_UNKNOWERR;
}
}
if (sdf_vendor && sdf_vendor->pkey_std2vendor) {
if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC,
SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
ret = SDR_ALGNOTSUPPORT;
goto end;
}
}
if ((ret = sdf_method->InternalEncrypt_ECC(
hSessionHandle,
uiIPKIndex,
uiAlgID,
pucData,
uiDataLength,
buf)) != SDR_OK) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC,
sdf_get_error_reason(ret));
goto end;
}
if (sdf_vendor && sdf_vendor->decode_ecccipher) {
if (!sdf_vendor->decode_ecccipher(pucEncData, buf)) {
SDFerr(SDF_F_SDF_INTERNALENCRYPT_ECC, ERR_R_SDF_LIB);
ret = SDR_UNKNOWERR;
goto end;
}
}
/*
{
int i;
unsigned char *p = (unsigned char *)pucEncData;
for (i = 0; i < sizeof(ECCCipher) -1 + uiDataLength; i++) {
printf("%02x", p[i]);
}
printf("\n");
}
*/
ret = SDR_OK;
end:
if (sdf_vendor && sdf_vendor->decode_ecccipher && buf) {
SDF_FreeECCCipher(buf);
}
return ret;
}
int SDF_InternalDecrypt_ECC(
void *hSessionHandle,
unsigned int uiISKIndex,
@@ -1171,25 +1248,51 @@ int SDF_InternalDecrypt_ECC(
unsigned int *uiDataLength)
{
int ret = SDR_UNKNOWERR;
ECCCipher *buf = pucEncData;
if (!sdf_method || !sdf_method->InternalDecrypt_ECC) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC, SDF_R_NOT_INITIALIZED);
return SDR_NOTSUPPORT;
}
if (sdf_vendor && sdf_vendor->pkey_std2vendor) {
if (!(uiAlgID = sdf_vendor->pkey_std2vendor(uiAlgID))) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC,
SDF_R_NOT_SUPPORTED_PKEY_ALGOR);
return SDR_ALGNOTSUPPORT;
}
}
if (sdf_vendor && sdf_vendor->encode_ecccipher) {
if (SDF_NewECCCipher(&buf, pucEncData->L) != SDR_OK) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC, ERR_R_SDF_LIB);
return SDR_UNKNOWERR;
}
if (!sdf_vendor->encode_ecccipher(pucEncData, buf)) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC, ERR_R_SDF_LIB);
ret = SDR_UNKNOWERR;
goto end;
}
}
if ((ret = sdf_method->InternalDecrypt_ECC(
hSessionHandle,
uiISKIndex,
uiAlgID,
pucEncData,
buf,
pucData,
uiDataLength)) != SDR_OK) {
SDFerr(SDF_F_SDF_INTERNALDECRYPT_ECC,
sdf_get_error_reason(ret));
return ret;
goto end;
}
return SDR_OK;
end:
if (sdf_vendor && sdf_vendor->encode_ecccipher && buf) {
SDF_FreeECCCipher(buf);
}
return ret;
}
int SDF_Encrypt(

View File

@@ -108,8 +108,8 @@ SDF_METHOD *SDF_METHOD_load_library(const char *so_path)
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(ExternalDecrypt_ECC);
SDF_METHOD_BIND_FUNCTION(InternalEncrypt_ECC);
SDF_METHOD_BIND_FUNCTION(InternalDecrypt_ECC);
SDF_METHOD_BIND_FUNCTION(Encrypt);
SDF_METHOD_BIND_FUNCTION(Decrypt);