当前位置 博文首页 > KEY_好习惯成就伟大:【GmSSL】ECCrefPrivateKey 转EC

    KEY_好习惯成就伟大:【GmSSL】ECCrefPrivateKey 转EC

    作者:[db:作者] 时间:2021-07-09 18:58

    	ECCrefPublicKey *pkref = (ECCrefPublicKey *)pkbuf;
    	ECCrefPrivateKey *skref = (ECCrefPrivateKey *)skbuf;
    	ECCCipher *cvref = (ECCCipher *)cvbuf;
    	ECCSignature *sigref = (ECCSignature *)sigbuf;
    	EC_KEY *pk = NULL;
    	EC_KEY *sk = NULL;
    	SM2_CIPHERTEXT_VALUE *cv = NULL;
    	ECDSA_SIG *sig = NULL;
    
    	if (!(pk = EC_KEY_new_from_ECCrefPublicKey(pkref))
    		|| !EC_KEY_set_ECCrefPublicKey(pk, pkref)
    		|| !EC_KEY_get_ECCrefPublicKey(pk, pkref)
    		|| !(sk = EC_KEY_new_from_ECCrefPrivateKey(skref))
    		|| !EC_KEY_set_ECCrefPrivateKey(sk, skref)
    		|| !EC_KEY_get_ECCrefPrivateKey(sk, skref)
    		|| !(cv = SM2_CIPHERTEXT_VALUE_new_from_ECCCipher(cvref))
    		|| !SM2_CIPHERTEXT_VALUE_set_ECCCipher(cv, cvref)
    		|| !SM2_CIPHERTEXT_VALUE_get_ECCCipher(cv, cvref)
    		|| !(sig = ECDSA_SIG_new_from_ECCSignature(sigref))
    		|| !ECDSA_SIG_set_ECCSignature(sig, sigref)
    		|| !ECDSA_SIG_get_ECCSignature(sig, sigref)) {
    		goto end;
    	}

    /*?SDF?*/

    #ifndef?OPENSSL_NO_SDF

    #?ifndef?OPENSSL_NO_RSA

    RSA?*RSA_new_from_RSArefPublicKey(const?RSArefPublicKey?*ref);

    int?RSA_set_RSArefPublicKey(RSA?*rsa,?const?RSArefPublicKey?*ref);

    int?RSA_get_RSArefPublicKey(RSA?*rsa,?RSArefPublicKey?*ref);

    RSA?*RSA_new_from_RSArefPrivateKey(const?RSArefPrivateKey?*ref);

    int?RSA_set_RSArefPrivateKey(RSA?*rsa,?const?RSArefPrivateKey?*ref);

    int?RSA_get_RSArefPrivateKey(RSA?*rsa,?RSArefPrivateKey?*ref);

    #?endif

    #?ifndef?OPENSSL_NO_EC

    EC_KEY?*EC_KEY_new_from_ECCrefPublicKey(const?ECCrefPublicKey?*ref);

    int?EC_KEY_set_ECCrefPublicKey(EC_KEY?*ec_key,?const?ECCrefPublicKey?*ref);

    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 ((int)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;
    	}
    
    
        //貌似这边定死了,ref->bits 必须是256,要不然会报错!!!!!!!!!!
    
    
    	/* ECCrefPublicKey ==> EC_KEY */
    	nbytes = (ref->bits + 7)/8;
    
    	if (!(x = BN_bin2bn(ref->x + ECCref_MAX_LEN - nbytes, nbytes, NULL))) {
    		GMAPIerr(GMAPI_F_EC_KEY_SET_ECCREFPUBLICKEY, ERR_R_BN_LIB);
    		goto end;
    	}
    	if (!(y = BN_bin2bn(ref->y + ECCref_MAX_LEN - nbytes, 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);

    EC_KEY?*EC_KEY_new_from_ECCrefPrivateKey(const?ECCrefPrivateKey?*ref);

    int?EC_KEY_set_ECCrefPrivateKey(EC_KEY?*ec_key,?const?ECCrefPrivateKey?*ref);

    int?EC_KEY_get_ECCrefPrivateKey(EC_KEY?*ec_key,?ECCrefPrivateKey?*ref);

    #??ifndef?OPENSSL_NO_SM2

    SM2CiphertextValue?*SM2CiphertextValue_new_from_ECCCipher(const?ECCCipher?*ref);

    ECCCipher?转??SM2CiphertextValue

    int?SM2CiphertextValue_set_ECCCipher(SM2CiphertextValue?*cv,?const?ECCCipher?*ref);

    ? ? ? ??SM2CiphertextValue?转?ECCCipher

    int?SM2CiphertextValue_get_ECCCipher(const?SM2CiphertextValue?*cv,?ECCCipher?*ref);

    ? ? ? ? 注意项:?ECCCipher.L需要进行初始化分配空间,要不然会报错;

    ? ? ? ??

    	/* 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 < (unsigned int)ASN1_STRING_length(cv->ciphertext)) {
    		GMAPIerr(GMAPI_F_SM2CIPHERTEXTVALUE_GET_ECCCIPHER,
    			GMAPI_R_BUFFER_TOO_SMALL);
    		printf("LLLLL \n");
    		return 0;
    	}

    ? ? ? ??

    #??endif

    #??ifndef?OPENSSL_NO_ECIES

    ECIES_CIPHERTEXT_VALUE?*ECIES_CIPHERTEXT_VALUE_new_from_ECCCipher(const?ECCCipher?*ref);

    int?ECIES_CIPHERTEXT_VALUE_set_ECCCipher(ECIES_CIPHERTEXT_VALUE?*cv,?const?ECCCipher?*ref);

    int?ECIES_CIPHERTEXT_VALUE_get_ECCCipher(const?ECIES_CIPHERTEXT_VALUE?*cv,?ECCCipher?*ref);

    #??endif

    ECDSA_SIG?*ECDSA_SIG_new_from_ECCSignature(const?ECCSignature?*ref);

    int?ECDSA_SIG_set_ECCSignature(ECDSA_SIG?*sig,?const?ECCSignature?*ref);

    int?ECDSA_SIG_get_ECCSignature(const?ECDSA_SIG?*sig,?ECCSignature?*ref);

    ECCCipher?*d2i_ECCCipher(ECCCipher?**a,?const?unsigned?char?**pp,?long?length);

    int?i2d_ECCCipher(ECCCipher?*a,?unsigned?char?**pp);

    ECCSignature?*d2i_ECCSignature(ECCSignature?**a,?const?unsigned?char?**pp,?long?length);

    int?i2d_ECCSignature(ECCSignature?*a,?unsigned?char?**pp);

    #?endif

    #endif

    SM2CiphertextValue?*SM2_do_encrypt(const?EVP_MD?*md, const?unsigned?char?*in,?size_t?inlen,?EC_KEY?*ec_key);

    int?SM2_do_decrypt(const?EVP_MD?*md,?const?SM2CiphertextValue?*in,

    ????unsigned?char?*out,?size_t?*outlen,?EC_KEY?*ec_key);

    int?SM2_encrypt(int?type,?const?unsigned?char?*in,?size_t?inlen,

    ????unsigned?char?*out,?size_t?*outlen,?EC_KEY?*ec_key);

    int?SM2_decrypt(int?type,?const?unsigned?char?*in,?size_t?inlen,

    ????unsigned?char?*out,?size_t?*outlen,?EC_KEY?*ec_key);

    #define?SM2_encrypt_with_recommended(in,inlen,out,outlen,ec_key)?\

    ????SM2_encrypt(NID_sm3,in,inlen,out,outlen,ec_key)

    #define?SM2_decrypt_with_recommended(in,inlen,out,outlen,ec_key)?\

    ????SM2_decrypt(NID_sm3,in,inlen,out,outlen,ec_key)

    cs