首页 > 编程语言 >openssl 全面支持国密SM2/SM3/SM4加密算法

openssl 全面支持国密SM2/SM3/SM4加密算法

时间:2022-10-26 14:47:40浏览次数:46  
标签:SM4 SM3 len char EVP data tc 加密算法 out

sm4展示

代码

/** 文件名: https://github.com/liuqun/openssl-sm4-demo/blob/cmake/src/main.c */
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/err.h"
#include "openssl/evp.h"
 
/* Before OpenSSL 1.1.1-pre1, we did not have EVP_sm4_ecb() */
#if defined(OPENSSL_VERSION_NUMBER) \
    && OPENSSL_VERSION_NUMBER < 0x10101001L
static const EVP_CIPHER *(*EVP_sm4_ecb)()=EVP_aes_128_ecb;
#endif
 
typedef struct {
    const unsigned char *in_data;
    size_t in_data_len;
    int in_data_is_already_padded;
    const unsigned char *in_ivec;
    const unsigned char *in_key;
    size_t in_key_len;
} test_case_t;
 
 
void test_encrypt_with_cipher(const test_case_t *in, const EVP_CIPHER *cipher)
{
    unsigned char *out_buf = NULL;
    int out_len;
    int out_padding_len;
    EVP_CIPHER_CTX *ctx;
 
    ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit_ex(ctx, cipher, NULL, in->in_key, in->in_ivec);
 
    if (in->in_data_is_already_padded)
    {
        /* Check whether the input data is already padded.
        And its length must be an integral multiple of the cipher's block size. */
        const size_t bs = EVP_CIPHER_block_size(cipher);
        if (in->in_data_len % bs != 0)
        {
            printf("ERROR-1: data length=%d which is not added yet; block size=%d\n", (int) in->in_data_len, (int) bs);
            /* Warning: Remember to do some clean-ups */
            EVP_CIPHER_CTX_free(ctx);
            return;
        }
        /* Disable the implicit PKCS#7 padding defined in EVP_CIPHER */
        EVP_CIPHER_CTX_set_padding(ctx, 0);
    }
 
    out_buf = (unsigned char *) malloc(((in->in_data_len>>4)+1) << 4);
    out_len = 0;
    EVP_EncryptUpdate(ctx, out_buf, &out_len, in->in_data, in->in_data_len);
    if (1)
    {
        printf("Debug: out_len=%d\n", out_len);
    }
 
    out_padding_len = 0;
    EVP_EncryptFinal_ex(ctx, out_buf+out_len, &out_padding_len);
    if (1)
    {
        printf("Debug: out_padding_len=%d\n", out_padding_len);
    }
 
    EVP_CIPHER_CTX_free(ctx);
    if (1)
    {
        int i;
        int len;
        len = out_len + out_padding_len;
        for (i=0; i<len; i++)
        {
            printf("%02x ", out_buf[i]);
        }
        printf("\n");
    }
 
    if (out_buf)
    {
        free(out_buf);
        out_buf = NULL;
    }
}
 
void main()
{
    int have_sm4 = (OPENSSL_VERSION_NUMBER >= 0x10101001L);
    int have_aes = 1;
    const unsigned char data[]=
    {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };
    unsigned char ivec[EVP_MAX_IV_LENGTH]; ///< IV 向量
    const unsigned char key1[16] = ///< key_data, 密钥内容, 至少16字节
    {
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
        0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
    };
    test_case_t tc;
 
    tc.in_data = data;
    tc.in_data_len = sizeof(data);
    tc.in_data_is_already_padded = (tc.in_data_len % 16)==0; // Hard coded 16 as the cipher's block size
    tc.in_key = key1;
    tc.in_key_len = sizeof(key1);
    memset(ivec, 0x00, EVP_MAX_IV_LENGTH);
    tc.in_ivec = ivec;
 
#if defined(OPENSSL_NO_SM4)
    have_sm4 = 0;
#endif
    if (have_sm4)
    {
        printf("[1]\n");
        printf("Debug: EVP_sm4_ecb() test\n");
        test_encrypt_with_cipher(&tc, EVP_sm4_ecb());
    }
#if defined(OPENSSL_NO_AES)
    have_aes = 0;
#endif
    if (have_aes)
    {
        printf("[2]\n");
        printf("Debug: EVP_aes_128_ecb() test\n");
        test_encrypt_with_cipher(&tc, EVP_aes_128_ecb());
    }
}

相关性质



sm3展示

sm3代码

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
void tDigest()
{
    unsigned char sm3_value[EVP_MAX_MD_SIZE];   //保存输出的摘要值的数组
    int sm3_len, i;
    EVP_MD_CTX *sm3ctx;                         //EVP消息摘要结构体
    sm3ctx = EVP_MD_CTX_new();//调用函数初始化
    char msg1[] = "20201310";              //待计算摘要的消息1
    //char msg2[] = "hzx";              //待计算摘要的消息2
     
    EVP_MD_CTX_init(sm3ctx);                    //初始化摘要结构体
    EVP_DigestInit_ex(sm3ctx, EVP_sm3(), NULL); //设置摘要算法和密码算法引擎,这里密码算法使用sm3,算法引擎使用OpenSSL默认引擎即软算法
    EVP_DigestUpdate(sm3ctx, msg1, strlen(msg1));//调用摘要UpDate计算msg1的摘要
    //EVP_DigestUpdate(sm3ctx, msg2, strlen(msg2));//调用摘要UpDate计算msg2的摘要 
    EVP_DigestFinal_ex(sm3ctx, sm3_value, &sm3_len);//摘要结束,输出摘要值   
    EVP_MD_CTX_reset(sm3ctx);                       //释放内存
     
    printf("原始数据%s的摘要值为:\n",msg1);
    for(i = 0; i < sm3_len; i++)
    {
        printf("0x%x ", sm3_value[i]);
    }
    printf("\n");
}
int main()
{
    OpenSSL_add_all_algorithms();
    tDigest();
    return 0;
}

标签:SM4,SM3,len,char,EVP,data,tc,加密算法,out
From: https://www.cnblogs.com/hzxjkl/p/16828260.html

相关文章

  • 接口测试加密算法理解【多测师】
    问题:1.接口中有数据要进行加密,如何处理?答:(1)写个函数或者方法,把要加密的参数使用这个函数过滤一遍,等于就是说把数据丢进去,加密了之后,再通过这个加密好的数据传输过去就可以......
  • MD5 哈希加密算法 - C++ 实现
    MD5加密算法-C++实现写在前头:还在学习中!整个文档写的很匆忙,肯定还有很多不周到的地方.欢迎在评论中提出你的宝贵意见!!算法背景BackgroundMD5消息摘要算法......
  • VL53L1CBV0FY(测距传感器)ISM330DHCXTR(运动传感器) 概述 应用
    概述1、VL53L1X飞行时间(ToF)测距传感器是最先进的激光测距传感器,进一步丰富了STFlightSense系列产品。该款远距离测距微型ToF传感器测距精度达4米,频率高达50Hz。VL53L1......
  • SM30: Add custom button on maintenance view
    SM30:Addcustombuttononmaintenanceview:Stepbystep:CreateDBtable:ZLM_TEST_MTCreatemaintenanceviewAddevent:FormroutinenamemustbeSAPL+function......
  • 对称加密算法和非对称加密算法说明
    加密算法和协议对称加密:非对称加密单向加密:哈希(hash)加密认证协议1.对称加密:加密和解密:加密和解密使用的是同一个密钥加密方式:是通过将原始数据分割......
  • HTTPS涉及的加密算法讲解
    前言从2015年左右开始,Google、Baidu、Facebook等互联网巨头,不谋而合地开始大力推行HTTPS,国内外的大型互联网公司很多也都已经启用了全站HTTPS为鼓励全球网站的HTTPS......
  • SM30相关操作
    目录SM30相关操作...1前提条件:创建的表必须支持表维护...1一、建立表维护,维护数据...1二、使用FM调用表维护,维护数据...3三、创建Tcode维护表维护...4四、......
  • 加密算法总结
    1.加密算法的分类根本不考虑解密问题;私用密钥加密技术:对称式加密(SymmetricKeyEncryption):对称式加密方式对加密和解密使用相同的密钥。通常,这种加密方式在应用中难以实施,......
  • Java加解密-SM4国密算法
    SM4国密算法简介SM4依赖包SM4类SM4_Context类SecuritySM4类=================================== SM4国密算法简介与DES和AES算法相似,国密SM4算法是一种分组加密......
  • AES加密算法原理及python实现
    AES对称加密算法  AES加密算法即密码学中的高级加密标准(AdvancedEncryptionStandard,AES),又称Rijndael加密法(2000年10月2日,比利时密码专家JoanDaemen和VincentRijmen提......