Converting SM2 Signature Formats (C/C++)
HarmonyOS supports the conversion between the DER and R|S formats.
You can specify SM2 ciphertext parameters and convert them to DER ciphertext. You can also extract SM2 ciphertext parameters from DER ciphertext.
Converting Ciphertext Parameters to DER Ciphertext
-
Call OH_CryptoEccSignatureSpec_Create to create an OH_CryptoEccSignatureSpec object for setting SM2 ciphertext parameters.
-
Call OH_CryptoEccSignatureSpec_SetRAndS to set the R and S parameters in the OH_CryptoEccSignatureSpec object.
-
Call OH_CryptoEccSignatureSpec_Encode to obtain the converted DER ciphertext.
-
Call OH_CryptoEccSignatureSpec_Destroy to release the object.
#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_asym_key.h"
#include "CryptoArchitectureKit/crypto_signature.h"
OH_Crypto_ErrCode DoTestSm2RStoDER()
{
static unsigned char rCoordinate[] = {
107, 93, 198, 247, 119, 18, 40, 110, 90, 156, 193,
158, 205, 113, 170, 128, 146, 109, 75, 17, 181, 109,
110, 91, 149, 5, 110, 233, 209, 78, 229, 96};
static unsigned char sCoordinate[] = {
45, 153, 88, 82, 104, 221, 226, 43, 174, 21, 122,
248, 5, 232, 105, 41, 92, 95, 102, 224, 216, 149,
85, 236, 110, 6, 64, 188, 149, 70, 70, 183};
// Generate DER signature data using R and S.
OH_CryptoEccSignatureSpec *spec = NULL;
Crypto_DataBlob r = {0};
Crypto_DataBlob s = {0};
r.data = rCoordinate;
r.len = sizeof(rCoordinate);
s.data = sCoordinate;
s.len = sizeof(sCoordinate);
OH_Crypto_ErrCode ret = OH_CryptoEccSignatureSpec_Create(NULL, &spec);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEccSignatureSpec_Destroy(spec);
return ret;
}
ret = OH_CryptoEccSignatureSpec_SetRAndS(spec, &r, &s);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEccSignatureSpec_Destroy(spec);
return ret;
}
Crypto_DataBlob sig = {0};
ret = OH_CryptoEccSignatureSpec_Encode(spec, &sig);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEccSignatureSpec_Destroy(spec);
return ret;
}
OH_Crypto_FreeDataBlob(&sig);
OH_CryptoEccSignatureSpec_Destroy(spec);
spec = NULL;
return CRYPTO_SUCCESS;
}
Converting DER Ciphertext to the R and S Parameters
-
Call OH_CryptoEccSignatureSpec_Create to pass the signature data and create a OH_CryptoEccSignatureSpec object to obtain the converted data.
-
Call OH_CryptoEccSignatureSpec_GetRAndS to obtain the converted R and S parameters.
-
Call OH_CryptoEccSignatureSpec_Destroy to free the memory.
#include "CryptoArchitectureKit/crypto_common.h"
#include "CryptoArchitectureKit/crypto_asym_key.h"
#include "CryptoArchitectureKit/crypto_signature.h"
OH_Crypto_ErrCode DoTestSm2DerConvertRS()
{
uint8_t signText[] = {
0x30, 0x45, 0x02, 0x21, 0x00, 0xab, 0xf8, 0xe2, 0x96, 0x7d, 0x5b, 0x28, 0xfb, 0x9a, 0xbd, 0x05, 0xa6,
0x81, 0xd6, 0xb1, 0x55, 0x69, 0x22, 0x25, 0xd2, 0xa3, 0x5d, 0xa8, 0xc0, 0x96, 0xe0, 0x1d, 0x38, 0x74,
0xa0, 0xc9, 0x4f, 0x02, 0x20, 0x20, 0x27, 0x04, 0x7a, 0x31, 0x94, 0xe7, 0x32, 0x61, 0xc3, 0x55, 0xa6,
0x5e, 0x1e, 0xdd, 0x3d, 0x04, 0x1c, 0x1e, 0x2d, 0x8d, 0x8d, 0x45, 0xca, 0xd9, 0x40, 0xe8, 0x97, 0xcd,
0x01, 0x18, 0xc5,
};
Crypto_DataBlob signBlob = {
.data = reinterpret_cast<uint8_t *>(signText),
.len = sizeof(signText)};
OH_CryptoEccSignatureSpec *eccSignSpec = nullptr;
OH_Crypto_ErrCode ret = OH_CryptoEccSignatureSpec_Create(&signBlob, &eccSignSpec);
if (ret != CRYPTO_SUCCESS) {
return ret;
}
Crypto_DataBlob r = {.data = nullptr, .len = 0};
Crypto_DataBlob s = {.data = nullptr, .len = 0};
ret = OH_CryptoEccSignatureSpec_GetRAndS(eccSignSpec, &r, &s);
if (ret != CRYPTO_SUCCESS) {
OH_CryptoEccSignatureSpec_Destroy(eccSignSpec);
return ret;
}
OH_Crypto_FreeDataBlob(&r);
OH_Crypto_FreeDataBlob(&s);
OH_CryptoEccSignatureSpec_Destroy(eccSignSpec);
return CRYPTO_SUCCESS;
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 crypto-rsa-asym-encrypt-decrypt-pkcs1-ndk
openharmony 鸿蒙 crypto-key-agreement-using-ecdh
openharmony 鸿蒙 crypto-aes-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm4-sym-encrypt-decrypt-gcm-ndk
openharmony 鸿蒙 crypto-sm2-sign-sig-verify-pkcs1-ndk
openharmony 鸿蒙 crypto-key-derivation-using-scrypt-ndk
openharmony 鸿蒙 crypto-sym-encrypt-decrypt-spec
openharmony 鸿蒙 crypto-rsa-sign-sig-verify-pkcs1-by-segment