Compute SM2 signature and verify it by invoking EVP interface in OpenSSL 1.1.1

        The SM2 algorithm is defined in the Chinese national standard GB/T 32918. SM2 is supported in OpenSSL 1.1.1. A rough introduction on SM2 operation is given at the link: https://www.openssl.org/docs/manmaster/man7/SM2.html . But it is not sufficient for learners to grasp it. A more detailed demo program is provided here:

/**************************************************
* File name: EVP_sm2_sign_and_verify.c
* Author: HAN Wei
* Author's blog: https://blog.csdn.net/henter/
* Date: April 27th, 2020
* Description: demonstrate how to compute SM2 signature
	    and verify it by invoking EVP interface
	    in OpenSSL 1.1.1
**************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "openssl/ec.h"
#include "openssl/evp.h"

/**************************************************
* Main function return value:
	0:   main function executes successfully
	-1:  an error occurs
**************************************************/
int main(void)
{
	int ret = -1, i;
	EVP_PKEY_CTX* pctx = NULL, * sctx = NULL;
	EVP_PKEY* pkey = NULL;
	EVP_MD_CTX* md_ctx = NULL, * md_ctx_verify = NULL;
	unsigned char message[16] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
				    0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF };
	size_t message_len = sizeof(message);
	unsigned char* sig = NULL;
	size_t sig_len;
	unsigned char sm2_id[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
				    0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
	unsigned int sm2_id_len = sizeof(sm2_id);
	EC_KEY* key_pair = NULL;
	const BIGNUM* priv_key = NULL;
	char* priv_key_str = NULL;

	const EC_GROUP* group = NULL;
	const EC_POINT* pub_key = NULL;
	BN_CTX* ctx = NULL;
	BIGNUM* x_coordinate = NULL, * y_coordinate = NULL;
	char* x_coordinate_str = NULL, * y_coordinate_str = NULL;

	/* create SM2 Ellipse Curve parameters and key pair */
	if ( !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) )
	{
		goto clean_up;
	}

	if ( (EVP_PKEY_paramgen_init(pctx)) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, NID_sm2)) <= 0 )
	{
		goto clean_up;
	}

	if ( (EVP_PKEY_keygen_init(pctx)) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_PKEY_keygen(pctx, &pkey)) != 1 )
	{
		goto clean_up;
	}

	/* print SM2 key pair */
	if ( !(key_pair = EVP_PKEY_get0_EC_KEY(pkey)) )
	{
		goto clean_up;
	}

	if ( !(priv_key = EC_KEY_get0_private_key(key_pair)) )
	{
		goto clean_up;
	}

	if ( !(priv_key_str = BN_bn2hex(priv_key)) )
	{
		goto clean_up;
	}
	printf("SM2 private key (in hex form):\n");
	printf("%s\n\n", priv_key_str);

	if ( !(pub_key = EC_KEY_get0_public_key(key_pair)) )
	{
		goto clean_up;
	}

	if ( !(group = EC_KEY_get0_group(key_pair)) )
	{
		goto clean_up;
	}

	if ( !(ctx = BN_CTX_new()) )
	{
		goto clean_up;
	}
	BN_CTX_start(ctx);
	x_coordinate = BN_CTX_get(ctx);
	y_coordinate = BN_CTX_get(ctx);
	if ( !(y_coordinate) )
	{
		goto clean_up;
	}
	if ( !(EC_POINT_get_affine_coordinates(group, pub_key, x_coordinate, y_coordinate, ctx)) )
	{
		goto clean_up;
	}
	if ( !(x_coordinate_str = BN_bn2hex(x_coordinate)) )
	{
		goto clean_up;
	}
	printf("x coordinate in SM2 public key (in hex form):\n");
	printf("%s\n\n", x_coordinate_str);

	if ( !(y_coordinate_str = BN_bn2hex(y_coordinate)) )
	{
		goto clean_up;
	}
	printf("y coordinate in SM2 public key (in hex form):\n");
	printf("%s\n\n", y_coordinate_str);

	/* compute SM2 signature */
	if ( (EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2)) != 1 )
	{
		goto clean_up;
	}

	if ( !(md_ctx = EVP_MD_CTX_new()) )
	{
		goto clean_up;
	}

	if ( !(sctx = EVP_PKEY_CTX_new(pkey, NULL)) )
	{
		goto clean_up;
	}

	if ( EVP_PKEY_CTX_set1_id(sctx, sm2_id, sm2_id_len) <= 0 )
	{
		goto clean_up;
	}

	EVP_MD_CTX_set_pkey_ctx(md_ctx, sctx);

	if ( (EVP_DigestSignInit(md_ctx, NULL, EVP_sm3(), NULL, pkey)) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_DigestSignUpdate(md_ctx, message, message_len)) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_DigestSignFinal(md_ctx, NULL, &sig_len)) != 1 )
	{
		goto clean_up;
	}

	if ( !(sig = (unsigned char*)malloc(sig_len)) )
	{
		goto clean_up;
	}

	if ( (EVP_DigestSignFinal(md_ctx, sig, &sig_len)) != 1 )
	{
		goto clean_up;
	}

	printf("Message length: %d bytes.\n", message_len);
	printf("Message:\n");
	for (i = 0; i < (int)message_len; i++)
	{
		printf("0x%x  ", message[i]);
	}
	printf("\n\n");

	printf("SM2 signature length: %d bytes.\n", sig_len);
	printf("SM2 signature (ASN.1 encode):\n");
	for (i = 0; i < (int)sig_len; i++)
	{
		printf("0x%x  ", sig[i]);
	}
	printf("\n\n");

	/* verify SM2 signature */
	if ( !(md_ctx_verify = EVP_MD_CTX_new()) )
	{
		goto clean_up;
	}

	EVP_MD_CTX_set_pkey_ctx(md_ctx_verify, sctx);

	if ( (EVP_DigestVerifyInit(md_ctx_verify, NULL, EVP_sm3(), NULL, pkey)) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_DigestVerifyUpdate(md_ctx_verify, message, sizeof(message))) != 1 )
	{
		goto clean_up;
	}

	if ( (EVP_DigestVerifyFinal(md_ctx_verify, sig, sig_len)) != 1 )
	{
		printf("Verify SM2 signature failed!\n");
		goto clean_up;
	}
	else
	{
		printf("Verify SM2 signature succeeded!\n");
	}

	ret = 0;
clean_up:
	if (pctx)
	{
		EVP_PKEY_CTX_free(pctx);
	}

	if (pkey)
	{
		EVP_PKEY_free(pkey);
	}

	if (priv_key_str)
	{
		OPENSSL_free(priv_key_str);
	}

	if (ctx)
	{
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}

	if (x_coordinate_str)
	{
		OPENSSL_free(x_coordinate_str);
	}

	if (y_coordinate_str)
	{
		OPENSSL_free(y_coordinate_str);
	}

	if (md_ctx)
	{
		EVP_MD_CTX_free(md_ctx);
	}

	if (sctx)
	{
		EVP_PKEY_CTX_free(sctx);
	}

	if (sig)
	{
		free(sig);
	}

	if (md_ctx_verify)
	{
		EVP_MD_CTX_free(md_ctx_verify);
	}

#if defined(_WIN32) || defined(_WIN64)
	system("pause");
#endif
	return ret;
}

        The result is as below:

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 35
    评论
Sure, here's an example implementation of a linear SVM model for binary classification using PyTorch and autograd for optimization: ``` import torch import torch.nn as nn import torch.optim as optim class LinearSVM(nn.Module): def __init__(self, input_dim): super(LinearSVM, self).__init__() self.weights = nn.Parameter(torch.randn(input_dim)) self.bias = nn.Parameter(torch.randn(1)) def forward(self, x): linear_output = torch.dot(x, self.weights) + self.bias return linear_output # Define training data and labels train_data = torch.tensor([[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0]]) train_labels = torch.tensor([1, 1, -1, -1]) # Initialize model and optimizer svm = LinearSVM(input_dim=2) optimizer = optim.SGD(svm.parameters(), lr=0.01) # Define training loop num_epochs = 1000 for epoch in range(num_epochs): svm.train() optimizer.zero_grad() output = svm(train_data) loss = torch.mean(torch.clamp(1 - train_labels * output, min=0)) loss.backward() optimizer.step() # Evaluate model on test data test_data = torch.tensor([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]) svm.eval() test_predictions = torch.sign(svm(test_data)).detach().numpy() print(test_predictions) ``` In this example, we define a `LinearSVM` class that inherits from `nn.Module` and implements a linear SVM model with a single linear layer. We use `nn.Parameter` to define the model's weight and bias parameters, which are then optimized using the `optim.SGD` optimizer. In the training loop, we compute the SVM loss using the hinge loss function and backpropagate the gradients using autograd. We then update the model parameters using the optimizer's `step` method. Finally, we evaluate the trained model on some test data by passing it through the model and taking the sign of the output (since the SVM is a binary classifier). We use `detach().numpy()` to convert the output to a numpy array for easier interpretation. Note: This is just a simple example implementation of a linear SVM in PyTorch using autograd. In practice, you may want to use a more robust implementation or library for SVMs, such as LIBLINEAR or scikit-learn.
评论 35
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值