首页 > 其他分享 >sm4代码

sm4代码

时间:2023-10-20 12:24:29浏览次数:26  
标签:const 代码 sm4 len EVP data tc out

   

点击查看代码
#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());
    }
}

标签:const,代码,sm4,len,EVP,data,tc,out
From: https://www.cnblogs.com/ljy12392/p/17776781.html

相关文章

  • 代码
    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"/*Befo......
  • 代码sm3
      点击查看代码#include<stdio.h>#include<stdlib.h>#include<string.h>#defineSHL(x,n)(x<<n)#defineSHR(x,n)(x>>n)#defineROTL(x,n)((x<<n)|(x>>(32-n)))#defineROTR(x,n)((x>>n)|(x<<(32-n)))#de......
  • 测试代码
    测试版本:exportLD_LIBRATY_PATH='/home/yuanyi/openssl-1.1.1w:$LD_LIBRARY_PATH'./apps/opensslversion测试代码:echo-n"abc"|./apps/openssldgst-SM3./apps/opensslecparam-list_curves|grepSM2/**文件名:https://github.com/liuqun/opens......
  • 源代码
    SM3:echo-n"abc"|./apps/openssldgst-SM3SM2:./apps/opensslecparam-list_curves|grepSM2SM4:/**文件名:https://github.com/liuqun/openssl-sm4-demo/blob/cmake/src/main.c*/include<stddef.h>include<stdio.h>include<stdl......
  • 实验二代码SM4
    .h文件#include"sm4.h"//4字节无符号数组转无符号long型voidfour_uCh2uLong(u8*in,u32*out){ inti=0; *out=0; for(i=0;i<4;i++) *out=((u32)in[i]<<(24-i*8))^*out;}//无符号long型转4字节无符号数组voiduLong2four_uCh(u32in,u8*o......
  • 实验二代码
    SM3编译#include<stdio.h>#include<stdlib.h>#include<string.h>#defineSHL(x,n)(x<<n)#defineSHR(x,n)(x>>n)#defineROTL(x,n)((x<<n)|(x>>(32-n)))#defineROTR(x,n)((x>>n)|(x<<(32-n)))#defineP......
  • C#增删改查代码
    //增加DataBasedataBase=newDataBase();SqlConnectionsqlConnection=dataBase.getConn();//打开连接sqlConnection.Open();//定义sql语句stringsql="insertintotest1007.g......
  • JS加密:JavaScript代码加密混淆
    JS加密,即JavaScript代码加密混淆,是指对js代码进行数据加密、逻辑混淆。使js代码不能被分析、复制、盗用,以达到保护js代码、保护js产品、保护js知识产权的目的。JS加密定义JS加密,即:JavaScript代码加密混淆、JavaScript代码混淆加密。JavaScript,简称JS,加密指对JS代码进行密文化处理......
  • 使用Pytorch Geometric 进行链接预测代码示例
    PyTorchGeometric(PyG)是构建图神经网络模型和实验各种图卷积的主要工具。在本文中我们将通过链接预测来对其进行介绍。链接预测答了一个问题:哪两个节点应该相互链接?我们将通过执行“转换分割”,为建模准备数据。为批处理准备专用的图数据加载器。在TorchGeometric中构建一个......
  • VS Code 统计代码行数
    安装插件VSCode可以通过安装VSCodeCounter插件来很方便的统计代码行数。使用Shift+Ctrl+X快捷键打开扩展界面,搜索VSCodeCounter并安装,如下使用插件统计代码点击顶部'查看>命令面板'菜单,如下:工作区选择VscodeCounter:Countlinesindirectory点击回车......