首页 > 编程语言 >open-api 算法实现

open-api 算法实现

时间:2022-10-26 14:37:45浏览次数:51  
标签:open unsigned len char 算法 api EVP data out

作者:20201303张奕博

要求:

  • 推荐在openEuler中实现 ,参考https://www.cnblogs.com/rocedu/p/6012545.html第三节
  • 提交相关代码码云(或github)链接(我使用博客园)
  • 提交不少于6张编译测试过程截图
  • 至少包含SM4,SM3的测试

1.检查版本

2.检查sm2 sm3

$ ./apps/openssl ecparam -list_curves | grep SM2
$ echo -n "abc" | ./apps/openssl dgst -SM3

3.检查函数

./apps/openssl enc -ciphers

4.测试SM4

代码:

#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());
    }
}

5.测试SM3

#include <stdio.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/err.h>
void tDigest(){
	unsigned char md_value[EVP_MAX_MD_SIZE];
	unsigned int md_len;
	EVP_MD_CTX *mdctx;
	mdctx = EVP_MD_CTX_new();
	char msg1[] = "20201303";
	char msg2[] = "ZHANGyibo";
	EVP_MD_CTX_init(mdctx);
	EVP_DigestInit_ex(mdctx, EVP_sm3(), NULL);
	EVP_DigestUpdate(mdctx, msg1, strlen(msg1));
	EVP_DigestUpdate(mdctx, msg2, strlen(msg2));
	EVP_DigestFinal_ex(mdctx, md_value, &md_len);
	EVP_MD_CTX_destroy(mdctx);

	printf("Debug:Message1%s and Message2%s digest to:\n",msg1, msg2);
	for(int i = 0; i<md_len; i++){

		printf("0x%02x ", md_value[i]);
	}
	printf("\n");
}

int main(){

	OpenSSL_add_all_algorithms();
	tDigest();
	return 0;
}

标签:open,unsigned,len,char,算法,api,EVP,data,out
From: https://www.cnblogs.com/sanfeng-ooo/p/16828239.html

相关文章

  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、......
  • vue源码分析-diff算法核心原理
    这一节,依然是深入剖析Vue源码系列,上几节内容介绍了VirtualDOM是Vue在渲染机制上做的优化,而渲染的核心在于数据变化时,如何高效的更新节点,这就是diff算法。由于源码中关于d......
  • OpenSSL API使用
    OpenSSLAPI使用配置LD_LIBRARY_PATH并检查openssl可执行程序版本号检查SM3哈希校验和检查椭圆曲线是否包含SM2检查对称算法SM4-自测试数据/**文件名:https:......
  • 实验二 OpenSSL API使用
    截图代码/**文件名:https://github.com/liuqun/openssl-sm4-demo/blob/cmake/src/main.c*/#include<stddef.h>#include<stdio.h>#include<stdlib.h>#inc......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践基础要求1.编写Python程序,调用OpenDaylight的北向接口实现以下功能利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;下......
  • prim算法(普里姆算法)详解
    了解了什么是最小生成树后,本节为您讲解如何用普里姆(prim)算法查找连通网(带权的连通图)中的最小生成树。普里姆算法查找最小生成树的过程,采用了贪心算法的思想。对于包含N个......
  • 实验7:基于REST API的SDN北向应用实践
    目录一、基本要求1、编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight(2)下发指令删除s1上的流表数据·创......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一.基础实验1.编写python程序,调用odl北向接口实现以下功能①利用mininet平台搭建下图网络拓扑并连接odl编写Python程序,调用OpenDa......
  • 实验二 OpenSSL API使用
    一、任务详情参考 https://blog.csdn.net/bruce135lee/article/details/81811403 调用OpenSSLAPI0推荐在openEuler中实现,参考https://www.cnblogs.com/rocedu/p/60......
  • [CS] C2-1. 算法
    书名:Algorithmdesign作者:JonKleinberg一、算法基础...二、排序...三、贪心...四、分治...五、动态规划...六、进阶问题6.1最大流问题......