mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-27 02:36:29 +08:00
add gmapi
This commit is contained in:
@@ -64,6 +64,7 @@ static ERR_STRING_DATA ERR_str_libraries[] = {
|
||||
{ERR_PACK(ERR_LIB_PAILLIER, 0, 0), "PAILLIER routines"},
|
||||
{ERR_PACK(ERR_LIB_CPK, 0, 0), "CPK routines"},
|
||||
{ERR_PACK(ERR_LIB_OTP, 0, 0), "OTP routines"},
|
||||
{ERR_PACK(ERR_LIB_GMAPI, 0, 0), "GMAPI routines"},
|
||||
{0, NULL},
|
||||
};
|
||||
|
||||
@@ -113,6 +114,7 @@ static ERR_STRING_DATA ERR_str_reasons[] = {
|
||||
{ERR_R_PAILLIER_LIB, "PAILLIER lib"},
|
||||
{ERR_R_CPK_LIB, "CPK lib"},
|
||||
{ERR_R_OTP_LIB, "OTP lib"},
|
||||
{ERR_R_GMAPI_LIB, "GMAPI lib"},
|
||||
|
||||
{ERR_R_NESTED_ASN1_ERROR, "nested asn1 error"},
|
||||
{ERR_R_MISSING_ASN1_EOS, "missing asn1 eos"},
|
||||
|
||||
@@ -43,6 +43,8 @@
|
||||
#include <openssl/ffx.h>
|
||||
#include <openssl/paillier.h>
|
||||
#include <openssl/cpk.h>
|
||||
#include <openssl/otp.h>
|
||||
#include <openssl/gmapi.h>
|
||||
|
||||
int err_load_crypto_strings_int(void)
|
||||
{
|
||||
@@ -120,6 +122,9 @@ int err_load_crypto_strings_int(void)
|
||||
# ifndef OPENSSL_NO_OTP
|
||||
ERR_load_OTP_strings() == 0 ||
|
||||
# endif
|
||||
# ifndef OPENSSL_NO_GMAPI
|
||||
ERR_load_GMAPI_strings() == 0 ||
|
||||
# endif
|
||||
#endif
|
||||
ERR_load_KDF_strings() == 0)
|
||||
return 0;
|
||||
|
||||
@@ -40,6 +40,7 @@ L FFX include/openssl/ffx.h crypto/ffx/ffx_err.c
|
||||
L PAILLIER include/openssl/paillier.h crypto/paillier/pai_err.c
|
||||
L CPK include/openssl/cpk.h crypto/cpk/cpk_err.c
|
||||
L OTP include/openssl/otp.h crypto/otp/otp_err.c
|
||||
L GMAPI include/openssl/gmapi.h crypto/gmapi/gmapi_err.c
|
||||
|
||||
# additional header files to be scanned for function names
|
||||
L NONE crypto/x509/x509_vfy.h NONE
|
||||
|
||||
3
crypto/gmapi/build.info
Normal file
3
crypto/gmapi/build.info
Normal file
@@ -0,0 +1,3 @@
|
||||
LIBS=../../libcrypto
|
||||
SOURCE[../../libcrypto]=gmapi_err.c gmapi_sgd.c \
|
||||
gmapi_sdf_ec.c gmapi_sdf_rsa.c gmapi_skf_ec.c gmapi_skf_rsa.c
|
||||
252
crypto/gmapi/gmapi_err.c
Normal file
252
crypto/gmapi/gmapi_err.c
Normal file
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
* Generated by util/mkerr.pl DO NOT EDIT
|
||||
* Copyright 1995-2016 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
|
||||
* in the file LICENSE in the source distribution or at
|
||||
* https://www.openssl.org/source/license.html
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/gmapi.h>
|
||||
|
||||
/* BEGIN ERROR CODES */
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
# define ERR_FUNC(func) ERR_PACK(ERR_LIB_GMAPI,func,0)
|
||||
# define ERR_REASON(reason) ERR_PACK(ERR_LIB_GMAPI,0,reason)
|
||||
|
||||
static ERR_STRING_DATA GMAPI_str_functs[] = {
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATURE),
|
||||
"ECDSA_SIG_get_ECCSignature"},
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB),
|
||||
"ECDSA_SIG_get_ECCSIGNATUREBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATURE),
|
||||
"ECDSA_SIG_new_from_ECCSignature"},
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATUREBLOB),
|
||||
"ECDSA_SIG_new_from_ECCSIGNATUREBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE),
|
||||
"ECDSA_SIG_set_ECCSignature"},
|
||||
{ERR_FUNC(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATUREBLOB),
|
||||
"ECDSA_SIG_set_ECCSIGNATUREBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB),
|
||||
"EC_KEY_get_ECCPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB),
|
||||
"EC_KEY_get_ECCPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_GET_ECCREFPRIVATEKEY),
|
||||
"EC_KEY_get_ECCrefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY),
|
||||
"EC_KEY_get_ECCrefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_NEW_FROM_ECCPRIVATEKEYBLOB),
|
||||
"EC_KEY_new_from_ECCPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_NEW_FROM_ECCPUBLICKEYBLOB),
|
||||
"EC_KEY_new_from_ECCPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPRIVATEKEY),
|
||||
"EC_KEY_new_from_ECCrefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPUBLICKEY),
|
||||
"EC_KEY_new_from_ECCrefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB),
|
||||
"EC_KEY_set_ECCPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB),
|
||||
"EC_KEY_set_ECCPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_SET_ECCREFPRIVATEKEY),
|
||||
"EC_KEY_set_ECCrefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY),
|
||||
"EC_KEY_set_ECCrefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_GET_RSAPRIVATEKEYBLOB),
|
||||
"RSA_get_RSAPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_GET_RSAPUBLICKEYBLOB), "RSA_get_RSAPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_GET_RSAREFPRIVATEKEY), "RSA_get_RSArefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_GET_RSAREFPUBLICKEY), "RSA_get_RSArefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB),
|
||||
"RSA_new_from_RSAPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB),
|
||||
"RSA_new_from_RSAPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_NEW_FROM_RSAREFPRIVATEKEY),
|
||||
"RSA_new_from_RSArefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_NEW_FROM_RSAREFPUBLICKEY),
|
||||
"RSA_new_from_RSArefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_SET_RSAPRIVATEKEYBLOB),
|
||||
"RSA_set_RSAPRIVATEKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB), "RSA_set_RSAPUBLICKEYBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_SET_RSAREFPRIVATEKEY), "RSA_set_RSArefPrivateKey"},
|
||||
{ERR_FUNC(GMAPI_F_RSA_SET_RSAREFPUBLICKEY), "RSA_set_RSArefPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_CREATEBASE64OBJ),
|
||||
"SAF_Base64_CreateBase64Obj"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_DECODE), "SAF_Base64_Decode"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_DECODEFINAL), "SAF_Base64_DecodeFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_DECODEUPDATE), "SAF_Base64_DecodeUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_ENCODE), "SAF_Base64_Encode"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_ENCODEFINAL), "SAF_Base64_EncodeFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_BASE64_ENCODEUPDATE), "SAF_Base64_EncodeUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_CREATESYMMKEYOBJ), "SAF_CreateSymmKeyObj"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_ECCPUBLICKEYENC), "SAF_EccPublicKeyEnc"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_ECCPUBLICKEYENCBYCERT),
|
||||
"SAF_EccPublicKeyEncByCert"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_ECCSIGN), "SAF_EccSign"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_ECCVERIFYSIGN), "SAF_EccVerifySign"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_ECCVERIFYSIGNBYCERT), "SAF_EccVerifySignByCert"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_GENECCKEYPAIR), "SAF_GenEccKeyPair"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_GENERATEAGREEMENTDATAWITHECC),
|
||||
"SAF_GenerateAgreementDataWithECC"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_GENERATEKEYWITHECC), "SAF_GenerateKeyWithECC"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_GETECCPUBLICKEY), "SAF_GetEccPublicKey"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_MACFINAL), "SAF_MacFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_MACUPDATE), "SAF_MacUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_PKCS7_DECODEDIGESTEDDATA),
|
||||
"SAF_Pkcs7_DecodeDigestedData"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_PKCS7_ENCODEDIGESTEDDATA),
|
||||
"SAF_Pkcs7_EncodeDigestedData"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_SYMMDECRYPTUPDATE), "SAF_SymmDecryptUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SAF_SYMMENCRYPTUPDATE), "SAF_SymmEncryptUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_CLOSEHANDLE), "SKF_CloseHandle"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DECRYPT), "SKF_Decrypt"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DECRYPTFINAL), "SKF_DecryptFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DECRYPTINIT), "SKF_DecryptInit"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DECRYPTUPDATE), "SKF_DecryptUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DIGEST), "SKF_Digest"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DIGESTFINAL), "SKF_DigestFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DIGESTINIT), "SKF_DigestInit"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_DIGESTUPDATE), "SKF_DigestUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_ENCRYPT), "SKF_Encrypt"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_ENCRYPTFINAL), "SKF_EncryptFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_ENCRYPTINIT), "SKF_EncryptInit"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_ENCRYPTUPDATE), "SKF_EncryptUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTECCDECRYPT), "SKF_ExtECCDecrypt"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTECCENCRYPT), "SKF_ExtECCEncrypt"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTECCSIGN), "SKF_ExtECCSign"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTECCVERIFY), "SKF_ExtECCVerify"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTRSAPRIKEYOPERATION),
|
||||
"SKF_ExtRSAPriKeyOperation"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_EXTRSAPUBKEYOPERATION),
|
||||
"SKF_ExtRSAPubKeyOperation"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_GENEXTECCKEYPAIR), "SKF_GenExtECCKeyPair"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_GENEXTRSAKEY), "SKF_GenExtRSAKey"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_GENRANDOM), "SKF_GenRandom"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_GETDEVINFO), "SKF_GetDevInfo"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_GETDEVSTATE), "SKF_GetDevState"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_HANDLE_GET_CBCMAC_CTX),
|
||||
"SKF_HANDLE_get_cbcmac_ctx"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_HANDLE_GET_CIPHER), "SKF_HANDLE_get_cipher"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_HANDLE_GET_CIPHER_CTX),
|
||||
"SKF_HANDLE_get_cipher_ctx"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_HANDLE_GET_KEY), "SKF_HANDLE_get_key"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_HANDLE_GET_MD_CTX), "SKF_HANDLE_get_md_ctx"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_MAC), "SKF_Mac"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_MACFINAL), "SKF_MacFinal"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_MACINIT), "SKF_MacInit"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_MACUPDATE), "SKF_MacUpdate"},
|
||||
{ERR_FUNC(GMAPI_F_SKF_SETSYMMKEY), "SKF_SetSymmKey"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER),
|
||||
"SM2_CIPHERTEXT_VALUE_get_ECCCipher"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB),
|
||||
"SM2_CIPHERTEXT_VALUE_get_ECCCIPHERBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER),
|
||||
"SM2_CIPHERTEXT_VALUE_new_from_ECCCipher"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHERBLOB),
|
||||
"SM2_CIPHERTEXT_VALUE_new_from_ECCCIPHERBLOB"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER),
|
||||
"SM2_CIPHERTEXT_VALUE_set_ECCCipher"},
|
||||
{ERR_FUNC(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB),
|
||||
"SM2_CIPHERTEXT_VALUE_set_ECCCIPHERBLOB"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
static ERR_STRING_DATA GMAPI_str_reasons[] = {
|
||||
{ERR_REASON(GMAPI_R_BUFFER_TOO_SMALL), "buffer too small"},
|
||||
{ERR_REASON(GMAPI_R_CBCMAC_FAILURE), "cbcmac failure"},
|
||||
{ERR_REASON(GMAPI_R_CTX_NOT_CREATED), "ctx not created"},
|
||||
{ERR_REASON(GMAPI_R_DECODE_EC_PRIVATE_KEY_FAILED),
|
||||
"decode ec private key failed"},
|
||||
{ERR_REASON(GMAPI_R_DECODE_EC_PUBLIC_KEY_FAILED),
|
||||
"decode ec public key failed"},
|
||||
{ERR_REASON(GMAPI_R_DECRYPT_FAILED), "decrypt failed"},
|
||||
{ERR_REASON(GMAPI_R_ENCODE_CIPHERTEXT_FAILED),
|
||||
"encode ciphertext failed"},
|
||||
{ERR_REASON(GMAPI_R_ENCODE_FAILED), "encode failed"},
|
||||
{ERR_REASON(GMAPI_R_ENCODE_RSA_PUBLIC_KEY_FAILED),
|
||||
"encode rsa public key failed"},
|
||||
{ERR_REASON(GMAPI_R_ENCODE_SIGNATURE_FAILED), "encode signature failed"},
|
||||
{ERR_REASON(GMAPI_R_ENCRYPT_FAILED), "encrypt failed"},
|
||||
{ERR_REASON(GMAPI_R_FAIL), "fail"},
|
||||
{ERR_REASON(GMAPI_R_GEN_RANDOM), "gen random"},
|
||||
{ERR_REASON(GMAPI_R_GEN_RSA_FAILED), "gen rsa failed"},
|
||||
{ERR_REASON(GMAPI_R_GET_PRIVATE_KEY_FAILED), "get private key failed"},
|
||||
{ERR_REASON(GMAPI_R_GET_PUBLIC_KEY_FAILED), "get public key failed"},
|
||||
{ERR_REASON(GMAPI_R_INT_OVERFLOW), "int overflow"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ALGID), "invalid algid"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ALGOR), "invalid algor"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ARGUMENTS), "invalid arguments"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_BIGNUM_LENGTH), "invalid bignum length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_BLOB), "invalid blob"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHERTEXT), "invalid ciphertext"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHERTEXT_LENGTH),
|
||||
"invalid ciphertext length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHERTEXT_MAC), "invalid ciphertext mac"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHERTEXT_POINT),
|
||||
"invalid ciphertext point"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHER_CTX_HANDLE),
|
||||
"invalid cipher ctx handle"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CIPHETEXT_LENGTH),
|
||||
"invalid ciphetext length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_CONTEXT), "invalid context"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_DIGEST_ALGOR), "invalid digest algor"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_DIGEST_LENGTH), "invalid digest length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ECC_PRIVATE_KEY), "invalid ecc private key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ECC_PUBLIC_KEY), "invalid ecc public key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_EC_KEY), "invalid ec key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_EC_PRIVATE_KEY), "invalid ec private key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_EC_PUBLIC_KEY), "invalid ec public key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_FEED_BIT_LENGTH), "invalid feed bit length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_HANDLE), "invalid handle"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_HANDLE_ALGOR), "invalid handle algor"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_HANDLE_MAGIC), "invalid handle magic"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_HANDLE_TYPE), "invalid handle type"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_HASH_HANDLE), "invalid hash handle"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_ID_LENGTH), "invalid id length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_INPUT_LENGTH), "invalid input length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_IV_LENGTH), "invalid iv length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_KEY_HANDLE), "invalid key handle"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_KEY_LENGTH), "invalid key length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_KEY_USAGE), "invalid key usage"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_LENGTH), "invalid length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_MAC_HANDLE), "invalid mac handle"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_PLAINTEXT_LENGTH),
|
||||
"invalid plaintext length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_PRIVATE_KEY), "invalid private key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_PUBLIC_KEY), "invalid public key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_RANDOM_LENGTH), "invalid random length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_RSA_KEY_LENGTH), "invalid rsa key length"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_RSA_PRIVATE_KEY), "invalid rsa private key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_RSA_PUBLIC_KEY), "invalid rsa public key"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_SIGNATURE), "invalid signature"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_SM2_CIPHERTEXT), "invalid sm2 ciphertext"},
|
||||
{ERR_REASON(GMAPI_R_INVALID_SM2_SIGNATURE), "invalid sm2 signature"},
|
||||
{ERR_REASON(GMAPI_R_MAC_FAILURE), "mac failure"},
|
||||
{ERR_REASON(GMAPI_R_MALLOC_FAILED), "malloc failed"},
|
||||
{ERR_REASON(GMAPI_R_NOT_CONVERTABLE), "not convertable"},
|
||||
{ERR_REASON(GMAPI_R_NO_PUBLIC_KEY), "no public key"},
|
||||
{ERR_REASON(GMAPI_R_NULL_ARGUMENT), "null argument"},
|
||||
{ERR_REASON(GMAPI_R_OPERATION_NOT_INITIALIZED),
|
||||
"operation not initialized"},
|
||||
{ERR_REASON(GMAPI_R_SAF_ERROR), "saf error"},
|
||||
{ERR_REASON(GMAPI_R_SIGN_FAILED), "sign failed"},
|
||||
{ERR_REASON(GMAPI_R_VERIFY_NOT_PASS), "verify not pass"},
|
||||
{0, NULL}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
int ERR_load_GMAPI_strings(void)
|
||||
{
|
||||
#ifndef OPENSSL_NO_ERR
|
||||
|
||||
if (ERR_func_error_string(GMAPI_str_functs[0].error) == NULL) {
|
||||
ERR_load_strings(0, GMAPI_str_functs);
|
||||
ERR_load_strings(0, GMAPI_str_reasons);
|
||||
}
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
696
crypto/gmapi/gmapi_sdf_ec.c
Normal file
696
crypto/gmapi/gmapi_sdf_ec.c
Normal file
@@ -0,0 +1,696 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
/* put these functions under name `gmapi`, even if in the future the SDF
|
||||
* implementation is organized into a standalone sub-library, we still need
|
||||
* some convert functions between native types and the SDF types. So we can
|
||||
* mix this with SDF engine without relying on the `sdf` sub-library
|
||||
*
|
||||
* Attension: as the functions in this file can be seen as the native
|
||||
* functions of GmSSL/OpenSSL, so the return values of these functions will
|
||||
* follow the convention of OpenSSL, return 1 for success and 0 for error.
|
||||
* This is different from SAF/SDF/SKF where return 0 means success.
|
||||
*/
|
||||
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/sdf.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include <openssl/objects.h>
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCrefPublicKey(const ECCrefPublicKey *ref)
|
||||
{
|
||||
EC_KEY *ret;
|
||||
|
||||
if (!(ret = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPUBLICKEY, ERR_R_EC_LIB);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_ECCrefPublicKey(ret, ref)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPUBLICKEY,
|
||||
GMAPI_R_DECODE_EC_PUBLIC_KEY_FAILED);
|
||||
EC_KEY_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_set_ECCrefPublicKey(EC_KEY *ec_key, const ECCrefPublicKey *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
int nbytes;
|
||||
|
||||
/* check arguments */
|
||||
if (!ec_key || !ref) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (ref->bits != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ECCrefPublicKey ==> EC_KEY */
|
||||
nbytes = (ref->bits + 7)/8;
|
||||
|
||||
if (!(x = BN_bin2bn(ref->x, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(y = BN_bin2bn(ref->y, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_PUBLIC_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_get_ECCrefPublicKey(EC_KEY *ec_key, ECCrefPublicKey *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
|
||||
const EC_POINT *point = EC_KEY_get0_public_key(ec_key);
|
||||
BIGNUM *x;
|
||||
BIGNUM *y;
|
||||
|
||||
/* check arguments */
|
||||
if (!ec_key || !ref) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* prepare */
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
BN_CTX_start(bn_ctx);
|
||||
x = BN_CTX_get(bn_ctx);
|
||||
y = BN_CTX_get(bn_ctx);
|
||||
if (!x || !y) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EC_GROUP_get_degree(group) > ECCref_MAX_BITS) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
/* EC_KEY ==> ECCrefPublicKey */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
ref->bits = EC_GROUP_get_degree(group);
|
||||
if (!BN_bn2bin(x, ref->x + ECCref_MAX_LEN - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, ref->y + ECCref_MAX_LEN - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCrefPrivateKey(const ECCrefPrivateKey *ref)
|
||||
{
|
||||
EC_KEY *ret;
|
||||
|
||||
if (!(ret = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPRIVATEKEY, ERR_R_EC_LIB);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_ECCrefPrivateKey(ret, ref)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCREFPRIVATEKEY,
|
||||
GMAPI_R_DECODE_EC_PRIVATE_KEY_FAILED);
|
||||
EC_KEY_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_set_ECCrefPrivateKey(EC_KEY *ec_key, const ECCrefPrivateKey *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *d = NULL;
|
||||
|
||||
if (!ec_key || !ref) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPRIVATEKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ref->bits != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(d = BN_bin2bn(ref->K, ECCref_MAX_LEN, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPRIVATEKEY, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!EC_KEY_set_private_key(ec_key, d)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_PRIVATE_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
BN_clear_free(d);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_get_ECCrefPrivateKey(EC_KEY *ec_key, ECCrefPrivateKey *ref)
|
||||
{
|
||||
const EC_GROUP *group;
|
||||
const BIGNUM *sk;
|
||||
int nbytes;
|
||||
|
||||
/* check arguments */
|
||||
if (!ec_key || !ref) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPRIVATEKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
group = EC_KEY_get0_group(ec_key);
|
||||
sk = EC_KEY_get0_private_key(ec_key);
|
||||
|
||||
if (!group || !sk) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_EC_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (EC_GROUP_get_degree(group) > ECCref_MAX_BITS) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* EC_KEY ==> ECCrefPrivateKey */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
|
||||
ref->bits = EC_GROUP_get_degree(group);
|
||||
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
if (!BN_bn2bin(sk, ref->K + nbytes - BN_num_bytes(sk))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCREFPRIVATEKEY, ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
SM2_CIPHERTEXT_VALUE *SM2_CIPHERTEXT_VALUE_new_from_ECCCipher(
|
||||
const ECCCipher *ref)
|
||||
{
|
||||
SM2_CIPHERTEXT_VALUE *ret = NULL;
|
||||
SM2_CIPHERTEXT_VALUE *cv = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
|
||||
/* check arguments */
|
||||
if (!ref) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
if (ref->L > INT_MAX) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHETEXT_LENGTH);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ECCCipher => SM2_CIPHERTEXT_VALUE */
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(cv = SM2_CIPHERTEXT_VALUE_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!SM2_CIPHERTEXT_VALUE_set_ECCCipher(cv, ref)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHER,
|
||||
GMAPI_R_INVALID_SM2_CIPHERTEXT);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = cv;
|
||||
cv = NULL;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
SM2_CIPHERTEXT_VALUE_free(cv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* Different vendors might have different encoding of field elements when
|
||||
* the buffer is larger than requirment. We assume the encoding to be
|
||||
* big-endian, which means that there will be prefix zeros in the buffer
|
||||
* before the field element. Another popular encoding is to use the suffix
|
||||
* zeros. When the gmapi wrapper is working with vendor's SDF
|
||||
* implementations, developers have to check the encoding of the vendor's
|
||||
* library to make sure the encoding/decoding is correct
|
||||
*/
|
||||
int SM2_CIPHERTEXT_VALUE_set_ECCCipher(SM2_CIPHERTEXT_VALUE *cv,
|
||||
const ECCCipher *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x;
|
||||
BIGNUM *y;
|
||||
int nbytes;
|
||||
|
||||
/* check arguments */
|
||||
if (!cv || !ref) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* variables */
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
/* this will never happen with GmSSL's sdf.h */
|
||||
if (EC_GROUP_get_degree(group) > ECCref_MAX_BITS) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
|
||||
/* malloc */
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
BN_CTX_start(bn_ctx);
|
||||
x = BN_CTX_get(bn_ctx);
|
||||
y = BN_CTX_get(bn_ctx);
|
||||
if (!x || !y) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* ECCCipher ==> SM2_CIPHERTEXT_VALUE */
|
||||
if (!BN_bin2bn(ref->x, ECCref_MAX_LEN, x)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bin2bn(ref->y, ECCref_MAX_LEN, y)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!cv->ephem_point) {
|
||||
if (!(cv->ephem_point = EC_POINT_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_set_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
cv->mactag_size = 32;
|
||||
memcpy(cv->mactag, ref->M, 32);
|
||||
|
||||
if (ref->L <= 0 || ref->L > INT_MAX) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
cv->ciphertext_size = (size_t)ref->L;
|
||||
|
||||
if (!(cv->ciphertext = OPENSSL_realloc(cv->ciphertext, (size_t)ref->L))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHER,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
memcpy(cv->ciphertext, ref->C, (size_t)ref->L);
|
||||
|
||||
/* set return value */
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* The caller need to prepare buffer larger than `sizeof(ECCipher)` to hold
|
||||
* the result. Some vendors might change the define of `ECCCipher->C[1]` to
|
||||
* larger buffer, such as `ECCCipher->C[ECC_CIPHER_MAX_LEN]`, but our
|
||||
* implementation will not know this. So always init `ECCCipher->L` to the
|
||||
* buffer size for ciphertext.
|
||||
* Some vendors might even change the definition of `ECCCipher`, then there
|
||||
* will be compiling errors when compiled with the vendor's header file. So
|
||||
* when you will use a vendor's SDF library, do not use `openssl/sdf.h`, but
|
||||
* use the vendor's header file. Then the errors can be found by the
|
||||
* compiler.
|
||||
*/
|
||||
int SM2_CIPHERTEXT_VALUE_get_ECCCipher(const SM2_CIPHERTEXT_VALUE *cv,
|
||||
ECCCipher *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x;
|
||||
BIGNUM *y;
|
||||
|
||||
/* check arguments */
|
||||
if (!cv || !ref) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
/* as the `ECCCipher->C[1]` default size is too small, we have to
|
||||
* check `ECCCipher->L` to make sure caller has initialized this
|
||||
* structure and prepared enough buffer to hold variable length
|
||||
* ciphertext
|
||||
*/
|
||||
if (ref->L < cv->ciphertext_size) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* malloc */
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
BN_CTX_start(bn_ctx);
|
||||
x = BN_CTX_get(bn_ctx);
|
||||
y = BN_CTX_get(bn_ctx);
|
||||
if (!x || !y) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
ERR_R_MALLOC_FAILURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* SM2_CIPHERTEXT_VALUE ==> ECCCipher */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
|
||||
/* encode ephem point `ECCCipher->x`, `ECCCipher->y` */
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* check compatible of SM2_CIPHERTEXT_VALUE with EC_GROUP
|
||||
* In gmapi we only do simple checks, i.e. length of coordinates.
|
||||
* We assume that more checks, such as x, y in the range of [1, p]
|
||||
* and other semantic checks should be done by the `sm2` module.
|
||||
*/
|
||||
if (BN_num_bits(x) > EC_GROUP_get_degree(group) ||
|
||||
BN_num_bits(y) > EC_GROUP_get_degree(group)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_POINT);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(x, ref->x + ECCref_MAX_LEN - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, ref->y + ECCref_MAX_LEN - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* encode mac `ECCCipher->M[32]` */
|
||||
if (cv->mactag_size != 32) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_MAC);
|
||||
goto end;
|
||||
}
|
||||
memcpy(ref->M, cv->mactag, cv->mactag_size);
|
||||
|
||||
/* encode ciphertext `ECCCipher->L`, `ECCCipher->C[]` */
|
||||
if (cv->ciphertext_size <= 0 || cv->ciphertext_size > INT_MAX) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHER,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
ref->L = (unsigned int)cv->ciphertext_size;
|
||||
memcpy(ref->C, cv->ciphertext, cv->ciphertext_size);
|
||||
|
||||
/* set return value */
|
||||
ret = 1;
|
||||
end:
|
||||
if (bn_ctx) {
|
||||
BN_CTX_end(bn_ctx);
|
||||
}
|
||||
BN_CTX_free(bn_ctx);
|
||||
EC_GROUP_free(group);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_new_from_ECCSignature(const ECCSignature *ref)
|
||||
{
|
||||
ECDSA_SIG *ret = NULL;
|
||||
ECDSA_SIG *sig = NULL;
|
||||
|
||||
/* check arguments */
|
||||
if (!ref) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATURE,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* generate and convert */
|
||||
if (!(sig = ECDSA_SIG_new())) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATURE,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
if (!ECDSA_SIG_set_ECCSignature(sig, ref)) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATURE,
|
||||
GMAPI_R_INVALID_SIGNATURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* set return value */
|
||||
ret = sig;
|
||||
sig = NULL;
|
||||
|
||||
end:
|
||||
ECDSA_SIG_free(sig);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_set_ECCSignature(ECDSA_SIG *sig, const ECCSignature *ref)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *r = NULL;
|
||||
BIGNUM *s = NULL;
|
||||
|
||||
/* check arguments */
|
||||
if (!sig || !ref) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ECCSignature ==> ECDSA_SIG */
|
||||
if (!(r = BN_bin2bn(ref->r, ECCref_MAX_LEN, NULL))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(s = BN_bin2bn(ref->s, ECCref_MAX_LEN, NULL))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
/* when using `sm2p256v1`, we need to check (s, r) length correct */
|
||||
if (BN_num_bytes(r) != 256/8 || BN_num_bytes(s) != 256/8) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE,
|
||||
GMAPI_R_INVALID_SM2_SIGNATURE);
|
||||
goto end;
|
||||
}
|
||||
|
||||
/* set return value
|
||||
* `ECDSA_SIG_set0` should make sure that if failed, do not accept
|
||||
* the value of (r, s), or there will be double-free
|
||||
*/
|
||||
if (!ECDSA_SIG_set0(sig, r, s)) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATURE,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
r = NULL;
|
||||
s = NULL;
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
BN_free(r);
|
||||
BN_free(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_get_ECCSignature(const ECDSA_SIG *sig, ECCSignature *ref)
|
||||
{
|
||||
/* (r, s) are pointed to (sig->r, sig->s), so dont free (r, s) */
|
||||
const BIGNUM *r = NULL;
|
||||
const BIGNUM *s = NULL;
|
||||
|
||||
/* check arguments */
|
||||
if (!sig || !ref) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATURE,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* check ECDSA_SIG
|
||||
* `ECDSA_SIG_get0() return void
|
||||
*/
|
||||
ECDSA_SIG_get0(sig, &r, &s);
|
||||
|
||||
if (BN_num_bytes(r) > ECCref_MAX_LEN ||
|
||||
BN_num_bytes(s) > ECCref_MAX_LEN) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATURE,
|
||||
GMAPI_R_NOT_CONVERTABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ECDSA_SIG ==> ECCSignature */
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
|
||||
if (!BN_bn2bin(r, ref->r + ECCref_MAX_LEN - BN_num_bytes(r))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATURE, ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
if (!BN_bn2bin(s, ref->s + ECCref_MAX_LEN - BN_num_bytes(s))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATURE, ERR_R_BN_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
209
crypto/gmapi/gmapi_sdf_rsa.c
Normal file
209
crypto/gmapi/gmapi_sdf_rsa.c
Normal file
@@ -0,0 +1,209 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015-2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
/* convert RSA between native and SDF types, never include skf.h */
|
||||
#include <stdio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/sdf.h>
|
||||
#include "../rsa/rsa_locl.h" //FIXME
|
||||
|
||||
|
||||
RSA *RSA_new_from_RSArefPublicKey(const RSArefPublicKey *ref)
|
||||
{
|
||||
RSA *ret;
|
||||
if (!(ret = RSA_new())) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAREFPUBLICKEY, ERR_R_RSA_LIB);
|
||||
return NULL;
|
||||
}
|
||||
if (!RSA_set_RSArefPublicKey(ret, ref)) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int RSA_set_RSArefPublicKey(RSA *rsa, const RSArefPublicKey *ref)
|
||||
{
|
||||
if (!rsa || !ref) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPUBLICKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if ((ref->bits < 1024) || (ref->bits > RSAref_MAX_LEN*8) ||
|
||||
(ref->bits % 8 != 0)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if (!(rsa->n = BN_bin2bn(ref->m, ref->bits/8, rsa->n))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
if (!(rsa->e = BN_bin2bn(ref->e, RSAref_MAX_LEN, rsa->e))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
if (!RSA_check_key(rsa)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_get_RSArefPublicKey(RSA *rsa, RSArefPublicKey *ref)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !ref) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPUBLICKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (!rsa->n || !rsa->e) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
nbytes = BN_num_bytes(rsa->n);
|
||||
if (!BN_bn2bin(rsa->n, ref->m) || !BN_bn2bin(rsa->e,
|
||||
ref->e + MAX_RSA_EXPONENT_LEN - BN_num_bytes(rsa->e))) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPUBLICKEY,
|
||||
GMAPI_R_ENCODE_RSA_PUBLIC_KEY_FAILED);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
RSA *RSA_new_from_RSArefPrivateKey(const RSArefPrivateKey *ref)
|
||||
{
|
||||
RSA *ret;
|
||||
if (!(ret = RSA_new())) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAREFPRIVATEKEY, ERR_R_RSA_LIB);
|
||||
return NULL;
|
||||
}
|
||||
if (!RSA_set_RSArefPrivateKey(ret, ref)) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int RSA_set_RSArefPrivateKey(RSA *rsa, const RSArefPrivateKey *ref)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !ref) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPRIVATEKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if ((ref->bits < 1024) || (ref->bits > RSAref_MAX_LEN*8) ||
|
||||
(ref->bits % 8 != 0) || (ref->bits % 16 != 0)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
nbytes = ref->bits/8;
|
||||
if (!(rsa->n = BN_bin2bn(ref->m, nbytes, rsa->n)) ||
|
||||
!(rsa->e = BN_bin2bn(ref->e, MAX_RSA_EXPONENT_LEN, rsa->e)) ||
|
||||
!(rsa->d = BN_bin2bn(ref->d, nbytes, rsa->d)) ||
|
||||
!(rsa->p = BN_bin2bn(ref->prime[0], nbytes/2, rsa->p)) ||
|
||||
!(rsa->q = BN_bin2bn(ref->prime[1], nbytes/2, rsa->q)) ||
|
||||
!(rsa->dmp1 = BN_bin2bn(ref->pexp[0], nbytes/2, rsa->dmp1)) ||
|
||||
!(rsa->dmq1 = BN_bin2bn(ref->pexp[1], nbytes/2, rsa->dmq1)) ||
|
||||
!(rsa->iqmp = BN_bin2bn(ref->coef, nbytes/2, rsa->iqmp))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_get_RSArefPrivateKey(RSA *rsa, RSArefPrivateKey *ref)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !ref) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPRIVATEKEY,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (!rsa->n || !rsa->e || !rsa->d || !rsa->p || !rsa->q ||
|
||||
!rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(ref, 0, sizeof(*ref));
|
||||
ref->bits = BN_num_bits(rsa->n);
|
||||
|
||||
nbytes = BN_num_bytes(rsa->n);
|
||||
if (!BN_bn2bin(rsa->n, ref->m) ||
|
||||
!BN_bn2bin(rsa->e, ref->e + MAX_RSA_EXPONENT_LEN - BN_num_bytes(rsa->e)) ||
|
||||
!BN_bn2bin(rsa->d, ref->d + nbytes - BN_num_bytes(rsa->d)) ||
|
||||
!BN_bn2bin(rsa->p, &(ref->prime[0]) + nbytes/2 - BN_num_bytes(rsa->p)) ||
|
||||
!BN_bn2bin(rsa->q, &(ref->prime[1]) + nbytes/2 - BN_num_bytes(rsa->q)) ||
|
||||
!BN_bn2bin(rsa->dmp1, &(ref->pexp[0]) + nbytes/2 - BN_num_bytes(rsa->dmp1)) ||
|
||||
!BN_bn2bin(rsa->dmq1, &(ref->pexp[1]) + nbytes/2 - BN_num_bytes(rsa->dmq1)) ||
|
||||
!BN_bn2bin(rsa->iqmp, ref->coef + nbytes/2 - BN_num_bytes(rsa->iqmp))) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAREFPRIVATEKEY,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
118
crypto/gmapi/gmapi_sgd.c
Normal file
118
crypto/gmapi/gmapi_sgd.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/sgd.h>
|
||||
|
||||
const EVP_MD *EVP_get_digestbysgd(int sgd)
|
||||
{
|
||||
switch (sgd) {
|
||||
case SGD_SM3:
|
||||
return EVP_sm3();
|
||||
case SGD_SHA1:
|
||||
return EVP_sha1();
|
||||
case SGD_SHA256:
|
||||
return EVP_sha256();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const EVP_CIPHER *EVP_get_cipherbysgd(int sgd)
|
||||
{
|
||||
switch (sgd) {
|
||||
case SGD_SM4_ECB:
|
||||
return EVP_sms4_ecb();
|
||||
case SGD_SM4_CBC:
|
||||
return EVP_sms4_cbc();
|
||||
case SGD_SM4_CFB:
|
||||
return EVP_sms4_cfb();
|
||||
case SGD_SM4_OFB:
|
||||
return EVP_sms4_ofb();
|
||||
#define OPENSSL_NO_ZUC
|
||||
#ifndef OPENSSL_NO_ZUC
|
||||
case SGD_ZUC:
|
||||
return EVP_zuc();
|
||||
case SGD_ZUC_EEA3:
|
||||
return EVP_zuc_eea3();
|
||||
#endif
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
int load_engine(void)
|
||||
{
|
||||
ENGINE *e;
|
||||
|
||||
ENGINE_load_builtin_engines(0);
|
||||
|
||||
ENGINE_register_all_complete();
|
||||
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
const char *GMAPI_keyusage2str(int usage)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int GMAPI_sgd2ciphernid(int sgd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int GMAPI_sgd2mdnid(int sgd)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
492
crypto/gmapi/gmapi_skf_ec.c
Normal file
492
crypto/gmapi/gmapi_skf_ec.c
Normal file
@@ -0,0 +1,492 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include <openssl/skf.h>
|
||||
#include "../ec/ec_lcl.h"
|
||||
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCPUBLICKEYBLOB(const ECCPUBLICKEYBLOB *blob)
|
||||
{
|
||||
EC_KEY *ret;
|
||||
|
||||
if (!(ret = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCPUBLICKEYBLOB, ERR_R_EC_LIB);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_ECCPUBLICKEYBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCPUBLICKEYBLOB,
|
||||
GMAPI_R_DECODE_EC_PUBLIC_KEY_FAILED);
|
||||
EC_KEY_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_set_ECCPUBLICKEYBLOB(EC_KEY *ec_key, const ECCPUBLICKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
int nbytes;
|
||||
|
||||
if (blob->BitLen != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
nbytes = (blob->BitLen + 7)/8;
|
||||
|
||||
if (!(x = BN_bin2bn(blob->XCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(y = BN_bin2bn(blob->YCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!EC_KEY_set_public_key_affine_coordinates(ec_key, x, y)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPUBLICKEYBLOB, GMAPI_R_INVALID_PUBLIC_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_get_ECCPUBLICKEYBLOB(EC_KEY *ec_key, ECCPUBLICKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
int nbytes;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
|
||||
const EC_POINT *point = EC_KEY_get0_public_key(ec_key);
|
||||
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
if (nbytes > ECC_MAX_MODULUS_BITS_LEN/8) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
x = BN_new();
|
||||
y = BN_new();
|
||||
bn_ctx = BN_CTX_new();
|
||||
if (!x || !y || !bn_ctx) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
memset(blob, 0, sizeof(*blob));
|
||||
blob->BitLen = EC_GROUP_get_degree(group);
|
||||
if (!BN_bn2bin(x, blob->XCoordinate + nbytes - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, blob->YCoordinate + nbytes - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
EC_KEY *EC_KEY_new_from_ECCPRIVATEKEYBLOB(const ECCPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
EC_KEY *ret;
|
||||
|
||||
if (!(ret = EC_KEY_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCPRIVATEKEYBLOB, ERR_R_EC_LIB);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_ECCPRIVATEKEYBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_NEW_FROM_ECCPRIVATEKEYBLOB,
|
||||
GMAPI_R_DECODE_EC_PRIVATE_KEY_FAILED);
|
||||
EC_KEY_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_set_ECCPRIVATEKEYBLOB(EC_KEY *ec_key, const ECCPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *d = NULL;
|
||||
int nbytes;
|
||||
|
||||
//FIXME: is this right?
|
||||
if (blob->BitLen != EC_GROUP_get_degree(EC_KEY_get0_group(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
nbytes = (blob->BitLen + 7)/8;
|
||||
|
||||
if (!(d = BN_bin2bn(blob->PrivateKey, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!EC_KEY_set_private_key(ec_key, d)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_SET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_PRIVATE_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
BN_clear_free(d);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int EC_KEY_get_ECCPRIVATEKEYBLOB(EC_KEY *ec_key, ECCPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
BIGNUM *order = NULL;
|
||||
const BIGNUM *d;
|
||||
int nbytes;
|
||||
|
||||
if (!(order = BN_new())) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!EC_GROUP_get_order(EC_KEY_get0_group(ec_key), order, NULL)) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
nbytes = BN_num_bytes(order);
|
||||
if (nbytes > ECC_MAX_MODULUS_BITS_LEN/8) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(d = EC_KEY_get0_private_key(ec_key))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, GMAPI_R_INVALID_EC_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(d, blob->PrivateKey + nbytes - BN_num_bytes(d))) {
|
||||
GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPRIVATEKEYBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
end:
|
||||
BN_free(order);
|
||||
return ret;
|
||||
}
|
||||
|
||||
SM2_CIPHERTEXT_VALUE *SM2_CIPHERTEXT_VALUE_new_from_ECCCIPHERBLOB(
|
||||
const ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ok = 0;
|
||||
SM2_CIPHERTEXT_VALUE *ret = NULL;
|
||||
EC_GROUP *group = NULL;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(ret = SM2_CIPHERTEXT_VALUE_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!SM2_CIPHERTEXT_VALUE_set_ECCCIPHERBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_NEW_FROM_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_EC_PUBLIC_KEY);
|
||||
goto end;
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
end:
|
||||
if (!ok) {
|
||||
SM2_CIPHERTEXT_VALUE_free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
EC_GROUP_free(group);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SM2_CIPHERTEXT_VALUE_set_ECCCIPHERBLOB(SM2_CIPHERTEXT_VALUE *cv,
|
||||
const ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
int nbytes;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB,
|
||||
ERR_R_EC_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
nbytes = (EC_GROUP_get_degree(group) + 7)/8;
|
||||
if (nbytes > ECC_MAX_XCOORDINATE_BITS_LEN/8) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!(x = BN_bin2bn(blob->XCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(y = BN_bin2bn(blob->YCoordinate, nbytes, NULL))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!(bn_ctx = BN_CTX_new())) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (!cv->ephem_point) {
|
||||
if (!(cv->ephem_point = EC_POINT_new(group))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_set_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(cv->mactag, blob->HASH, 32);
|
||||
cv->mactag_size = 32;
|
||||
|
||||
if ((cv->ciphertext_size = blob->CipherLen) <= 0) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
if (!(cv->ciphertext = OPENSSL_realloc(cv->ciphertext, blob->CipherLen))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_SET_ECCCIPHERBLOB,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
goto end;
|
||||
}
|
||||
memcpy(cv->ciphertext, blob->Cipher, blob->CipherLen);
|
||||
|
||||
ret = 0;
|
||||
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SM2_CIPHERTEXT_VALUE_get_ECCCIPHERBLOB(const SM2_CIPHERTEXT_VALUE *cv,
|
||||
ECCCIPHERBLOB *blob)
|
||||
{
|
||||
int ret = 0;
|
||||
EC_GROUP *group = NULL;
|
||||
BIGNUM *x = NULL;
|
||||
BIGNUM *y = NULL;
|
||||
BN_CTX *bn_ctx = NULL;
|
||||
|
||||
if (!(group = EC_GROUP_new_by_curve_name(NID_sm2p256v1))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
x = BN_new();
|
||||
y = BN_new();
|
||||
bn_ctx = BN_CTX_new();
|
||||
if (!x || !y || !bn_ctx) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
|
||||
if (!EC_POINT_get_affine_coordinates_GFp(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
if (!EC_POINT_get_affine_coordinates_GF2m(group, cv->ephem_point, x, y, bn_ctx)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_EC_LIB);
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if ((BN_num_bytes(x) > 256/8) || (BN_num_bytes(y) > 256/8)) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_POINT);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(x, blob->XCoordinate + 256/8 - BN_num_bytes(x))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
if (!BN_bn2bin(y, blob->YCoordinate + 256/8 - BN_num_bytes(y))) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB, ERR_R_BN_LIB);
|
||||
goto end;
|
||||
}
|
||||
|
||||
if (cv->mactag_size != 32) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_MAC);
|
||||
goto end;
|
||||
}
|
||||
memcpy(blob->HASH, cv->mactag, cv->mactag_size);
|
||||
|
||||
if (cv->ciphertext_size <= 0) {
|
||||
GMAPIerr(GMAPI_F_SM2_CIPHERTEXT_VALUE_GET_ECCCIPHERBLOB,
|
||||
GMAPI_R_INVALID_CIPHERTEXT_LENGTH);
|
||||
goto end;
|
||||
}
|
||||
memcpy(blob->Cipher, cv->ciphertext, cv->ciphertext_size);
|
||||
|
||||
ret = 1;
|
||||
end:
|
||||
EC_GROUP_free(group);
|
||||
BN_free(x);
|
||||
BN_free(y);
|
||||
BN_CTX_free(bn_ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ECDSA_SIG *ECDSA_SIG_new_from_ECCSIGNATUREBLOB(const ECCSIGNATUREBLOB *blob)
|
||||
{
|
||||
ECDSA_SIG *ret = NULL;
|
||||
|
||||
if (!(ret = ECDSA_SIG_new())) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATUREBLOB,
|
||||
GMAPI_R_MALLOC_FAILED);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!ECDSA_SIG_set_ECCSIGNATUREBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_NEW_FROM_ECCSIGNATUREBLOB,
|
||||
GMAPI_R_INVALID_SIGNATURE);
|
||||
ECDSA_SIG_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_set_ECCSIGNATUREBLOB(ECDSA_SIG *sig, const ECCSIGNATUREBLOB *blob)
|
||||
{
|
||||
if (!(sig->r = BN_bin2bn(blob->r, 256/8, sig->r))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
}
|
||||
|
||||
if (!(sig->s = BN_bin2bn(blob->s, 256/8, sig->s))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_SET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
}
|
||||
|
||||
return SAR_OK;
|
||||
}
|
||||
|
||||
int ECDSA_SIG_get_ECCSIGNATUREBLOB(const ECDSA_SIG *sig, ECCSIGNATUREBLOB *blob)
|
||||
{
|
||||
if ((BN_num_bytes(sig->r) > 256/8) || (BN_num_bytes(sig->s) > 256/8)) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, GMAPI_R_INVALID_BIGNUM_LENGTH);
|
||||
return SAR_FAIL;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(sig->r, blob->r + 256/8 - BN_num_bytes(sig->r))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
}
|
||||
|
||||
if (!BN_bn2bin(sig->s, blob->s + 256/8 - BN_num_bytes(sig->s))) {
|
||||
GMAPIerr(GMAPI_F_ECDSA_SIG_GET_ECCSIGNATUREBLOB, ERR_R_BN_LIB);
|
||||
return SAR_FAIL;
|
||||
}
|
||||
|
||||
return SAR_OK;
|
||||
}
|
||||
214
crypto/gmapi/gmapi_skf_rsa.c
Normal file
214
crypto/gmapi/gmapi_skf_rsa.c
Normal file
@@ -0,0 +1,214 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2015-2016 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/skf.h>
|
||||
#include <openssl/gmapi.h>
|
||||
#include "../rsa/rsa_locl.h"
|
||||
|
||||
/* Wrapper functions */
|
||||
|
||||
RSA *RSA_new_from_RSAPUBLICKEYBLOB(const RSAPUBLICKEYBLOB *blob)
|
||||
{
|
||||
RSA *ret;
|
||||
if (!(ret = RSA_new())) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB, ERR_R_RSA_LIB);
|
||||
return NULL;
|
||||
}
|
||||
if (!RSA_set_RSAPUBLICKEYBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
RSA *RSA_new_from_RSAPRIVATEKEYBLOB(const RSAPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
RSA *ret;
|
||||
if (!(ret = RSA_new())) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB, ERR_R_RSA_LIB);
|
||||
return NULL;
|
||||
}
|
||||
if (!RSA_set_RSAPRIVATEKEYBLOB(ret, blob)) {
|
||||
GMAPIerr(GMAPI_F_RSA_NEW_FROM_RSAPRIVATEKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
RSA_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int RSA_set_RSAPUBLICKEYBLOB(RSA *rsa, const RSAPUBLICKEYBLOB *blob)
|
||||
{
|
||||
if (!rsa || !blob) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if ((blob->BitLen < 1024) || (blob->BitLen > MAX_RSA_MODULUS_LEN*8) ||
|
||||
(blob->BitLen / 8 != 0)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
if (!(rsa->n = BN_bin2bn(blob->Modulus, blob->BitLen/8, rsa->n))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
if (!(rsa->e = BN_bin2bn(blob->PublicExponent, MAX_RSA_EXPONENT_LEN,
|
||||
rsa->e))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
if (!RSA_check_key(rsa)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_get_RSAPUBLICKEYBLOB(RSA *rsa, RSAPUBLICKEYBLOB *blob)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !blob) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPUBLICKEYBLOB,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (!rsa->n || !rsa->e) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PUBLIC_KEY);
|
||||
return 0;
|
||||
}
|
||||
nbytes = BN_num_bytes(rsa->n);
|
||||
if (!BN_bn2bin(rsa->n, blob->Modulus) || !BN_bn2bin(rsa->e,
|
||||
blob->PublicExponent + MAX_RSA_EXPONENT_LEN - BN_num_bytes(rsa->e))) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPUBLICKEYBLOB,
|
||||
GMAPI_R_ENCODE_RSA_PUBLIC_KEY_FAILED);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_set_RSAPRIVATEKEYBLOB(RSA *rsa, const RSAPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !blob) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPRIVATEKEYBLOB,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (blob->AlgID != SGD_RSA) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPRIVATEKEYBLOB,
|
||||
GMAPI_R_INVALID_ALGOR);
|
||||
return 0;
|
||||
}
|
||||
if ((blob->BitLen < 1024) || (blob->BitLen > MAX_RSA_MODULUS_LEN*8) ||
|
||||
(blob->BitLen % 8 != 0) || (blob->BitLen % 16 != 0)) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPRIVATEKEYBLOB,
|
||||
GMAPI_R_INVALID_KEY_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
nbytes = blob->BitLen/8;
|
||||
if (!(rsa->n = BN_bin2bn(blob->Modulus, nbytes, rsa->n)) ||
|
||||
!(rsa->e = BN_bin2bn(blob->PublicExponent, MAX_RSA_EXPONENT_LEN, rsa->e)) ||
|
||||
!(rsa->d = BN_bin2bn(blob->PrivateExponent, nbytes, rsa->d)) ||
|
||||
!(rsa->p = BN_bin2bn(blob->Prime1, nbytes/2, rsa->p)) ||
|
||||
!(rsa->q = BN_bin2bn(blob->Prime2, nbytes/2, rsa->q)) ||
|
||||
!(rsa->dmp1 = BN_bin2bn(blob->Prime1Exponent, nbytes/2, rsa->dmp1)) ||
|
||||
!(rsa->dmq1 = BN_bin2bn(blob->Prime2Exponent, nbytes/2, rsa->dmq1)) ||
|
||||
!(rsa->iqmp = BN_bin2bn(blob->Coefficient, nbytes/2, rsa->iqmp))) {
|
||||
GMAPIerr(GMAPI_F_RSA_SET_RSAPRIVATEKEYBLOB, GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RSA_get_RSAPRIVATEKEYBLOB(RSA *rsa, RSAPRIVATEKEYBLOB *blob)
|
||||
{
|
||||
int nbytes;
|
||||
if (!rsa || !blob) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPRIVATEKEYBLOB,
|
||||
ERR_R_PASSED_NULL_PARAMETER);
|
||||
return 0;
|
||||
}
|
||||
if (!rsa->n || !rsa->e || !rsa->d || !rsa->p || !rsa->q ||
|
||||
!rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPRIVATEKEYBLOB,
|
||||
GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(blob, 0, sizeof(*blob));
|
||||
blob->AlgID = SGD_RSA;
|
||||
blob->BitLen = BN_num_bits(rsa->n);
|
||||
|
||||
nbytes = BN_num_bytes(rsa->n);
|
||||
if (!BN_bn2bin(rsa->n, blob->Modulus) ||
|
||||
!BN_bn2bin(rsa->e, blob->PublicExponent + MAX_RSA_EXPONENT_LEN - BN_num_bytes(rsa->e)) ||
|
||||
!BN_bn2bin(rsa->d, blob->PrivateExponent + nbytes - BN_num_bytes(rsa->d)) ||
|
||||
!BN_bn2bin(rsa->p, blob->Prime1 + nbytes/2 - BN_num_bytes(rsa->p)) ||
|
||||
!BN_bn2bin(rsa->q, blob->Prime2 + nbytes/2 - BN_num_bytes(rsa->q)) ||
|
||||
!BN_bn2bin(rsa->dmp1, blob->Prime1Exponent + nbytes/2 - BN_num_bytes(rsa->dmp1)) ||
|
||||
!BN_bn2bin(rsa->dmq1, blob->Prime2Exponent + nbytes/2 - BN_num_bytes(rsa->dmq1)) ||
|
||||
!BN_bn2bin(rsa->iqmp, blob->Coefficient + nbytes/2 - BN_num_bytes(rsa->iqmp))) {
|
||||
GMAPIerr(GMAPI_F_RSA_GET_RSAPRIVATEKEYBLOB, GMAPI_R_INVALID_RSA_PRIVATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user