Example #1
0
static int test_sm2_sign(void)
{
	int rv;
	EC_KEY *ec_key = NULL;
	unsigned char dgst[32];
	ECDSA_SIG *sig = NULL;
	unsigned char sigbuf[128];
	unsigned int siglen;

	ec_key = EC_KEY_new_by_curve_name(NID_sm2p256v1);
	OPENSSL_assert(ec_key);
	rv = EC_KEY_generate_key(ec_key);
	OPENSSL_assert(rv == 1);

	RAND_bytes(dgst, sizeof(dgst));
	
	sig = SM2_do_sign(dgst, (int)sizeof(dgst), ec_key);
	OPENSSL_assert(sig);
	rv = SM2_do_verify(dgst, (int)sizeof(dgst), sig, ec_key);
	OPENSSL_assert(rv == 1);

	rv = SM2_sign(0, dgst, sizeof(dgst), sigbuf, &siglen, ec_key);
	OPENSSL_assert(rv == 1);
	rv = SM2_verify(0, dgst, sizeof(dgst), sigbuf, siglen, ec_key);
	OPENSSL_assert(rv == 1);

	EC_KEY_free(ec_key);
	ECDSA_SIG_free(sig);

	printf("%s() success\n", __FUNCTION__);
	return 0;
}
Example #2
0
static int pkey_ec_signctx(EVP_PKEY_CTX *ctx,
	unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx)
{
	int ret;
	unsigned int len;
	EC_PKEY_CTX *dctx = ctx->data;
	EC_KEY *ec_key = ctx->pkey->pkey.ec;
	unsigned char dgst[EVP_MAX_MD_SIZE];
	unsigned int dgstlen;
	int type = NID_undef;

	if (!sig) {
		*siglen = SM2_signature_size(ec_key);
		return 1;
	} else if (*siglen < (size_t)SM2_signature_size(ec_key)) {
		ECerr(EC_F_PKEY_SM2_SIGNCTX, EC_R_BUFFER_TOO_SMALL);
		return 0;
	}

	if (!EVP_DigestFinal_ex(mctx, dgst, &dgstlen)) {
		ECerr(EC_F_PKEY_SM2_SIGNCTX, ERR_R_EVP_LIB);
		return 0;
	}

	if (dctx->sign_type == NID_sm2sign)
		ret = SM2_sign(type, dgst, dgstlen, sig, &len, ec_key);
	else
		ret = ECDSA_sign(type, dgst, dgstlen, sig, &len, ec_key);

	*siglen = (size_t)len;
	return ret;
}
Example #3
0
static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
	const unsigned char *dgst, size_t dgstlen)
{
	int ret;
	EC_PKEY_CTX *ec_ctx = ctx->data;
	EC_KEY *ec_key = ctx->pkey->pkey.ec;
	int type;
	unsigned int len;

	if (!sig) {
		*siglen = SM2_signature_size(ec_key);
		return 1;
	}
	if (*siglen < (size_t)SM2_signature_size(ec_key)) {
		ECerr(EC_F_PKEY_SM2_SIGN, EC_R_BUFFER_TOO_SMALL);
		return 0;
	}

	type = ec_ctx->md ? EVP_MD_type(ec_ctx->md) : NID_sm3;
	if ((ret = SM2_sign(type, dgst, dgstlen, sig, &len, ec_key)) <= 0) {
		return ret;
	}

	*siglen = (size_t)len;
	return 1;
}
Example #4
0
static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
                        const unsigned char *tbs, size_t tbslen)
{
    int ret, type;
    unsigned int sltmp;
    EC_PKEY_CTX *dctx = ctx->data;
    EC_KEY *ec = ctx->pkey->pkey.ec;

    if (!sig) {
        *siglen = ECDSA_size(ec);
        return 1;
    } else if (*siglen < (size_t)ECDSA_size(ec)) {
        ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
        return 0;
    }

    if (dctx->md)
        type = EVP_MD_type(dctx->md);
    else
        type = NID_sha1;

#ifndef OPENSSL_NO_SM2
    if (dctx->ec_scheme == NID_sm_scheme)
        ret = SM2_sign(NID_undef, tbs, tbslen, sig, &sltmp, ec);
    else
#endif

    ret = ECDSA_sign(type, tbs, tbslen, sig, &sltmp, ec);

    if (ret <= 0)
        return ret;
    *siglen = (size_t)sltmp;
    return 1;
}
Example #5
0
static int pkey_ec_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
                        const unsigned char *dgst, size_t dgstlen)
{
	int ret;
	EC_PKEY_CTX *dctx = ctx->data;
	EC_KEY *ec_key = ctx->pkey->pkey.ec;
	int type;
	unsigned int len;

	if (!sig) {
		*siglen = ECDSA_size(ec_key);
		return 1;
	} else if (*siglen < (size_t)ECDSA_size(ec_key)) {
		ECerr(EC_F_PKEY_EC_SIGN, EC_R_BUFFER_TOO_SMALL);
		return 0;
	}

	if (dctx->sign_type != NID_secg_scheme &&
		dctx->sign_type != NID_sm_scheme) {
		return 0;
	}
		
	if (dctx->md)
		type = EVP_MD_type(dctx->md);
	else if (dctx->sign_type == NID_secg_scheme)
		type = NID_sha1;
	else if (dctx->sign_type == NID_sm_scheme)
		type = NID_sm3;

	if (dctx->sign_type == NID_secg_scheme) {
		ret = ECDSA_sign(type, dgst, dgstlen, sig, &len, ec_key);
	} else if (dctx->sign_type == NID_sm_scheme) {
		ret = SM2_sign(type, dgst, dgstlen, sig, &len, ec_key);
	}

	if (ret <= 0)
		return ret;

	*siglen = len;
	return 1;
}