首页 > 其他分享 >测试代码

测试代码

时间:2023-10-20 11:59:18浏览次数:25  
标签:const len EVP 测试代码 data tc out

测试版本:

export LD_LIBRATY_PATH='/home/yuanyi/openssl-1.1.1w:$LD_LIBRARY_PATH'
./apps/openssl version

测试代码:

echo -n "abc"|./apps/openssl dgst -SM3

./apps/openssl ecparam -list_curves | grep SM2


/** 文件名: 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());
    }
}

标签:const,len,EVP,测试代码,data,tc,out
From: https://www.cnblogs.com/yuanyi23/p/17776731.html

相关文章

  • Jest 匹配器:简化测试代码,提高测试效率
    Jest匹配器在软件开发中,测试是一个非常重要的环节。而在JavaScript开发中,Jest是一个非常流行的测试框架。Jest提供了丰富的功能和工具,其中的匹配器(Matchers)是其中之一。匹配器可以帮助我们编写更简洁、更高效的测试代码。本文将介绍Jest匹配器的使用方法和一些常用的匹配器......
  • 9.17测试代码
    packageceshi;publicclassWarehouseInformation{privateStringitemnode;//编号privateStringitemname;//名字privateStringsuppliername;//供应商privateStringwarehousingtime;//privateStringshipmenttime;privateStringwarehousenumb......
  • 开学测试代码
    importjdk.dynalink.beans.StaticClass;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Scanner;publicclassWarehouseManagement{staticWarehouseInformation[]wareho......
  • testing-code-测试代码unittest如何编写?
    1---name_function.py函数name_function.py用来获取人名,入参时选择姓、名、中间字三项(middle为可选入参项)1#!usr/bin/env/python23defget_formatted_name(first,last,middle=""):4ifmiddle:5full_name=f"{first}{middle}{last}"6returnf......
  • 测试代码
    usingSystem.IO.Compression;#pragmawarningdisable414,3021namespaceMyApplication{[Obsolete("...")]classProgram:IInterface{publicstaticListJustDoIt(intcount){Spannumbers=stackallo......
  • 用 GPT-4 给开源项目 GoPool 重构测试代码 - 每天5分钟玩转 GPT 编程系列(8)
    目录1.好险,差点被喷2.重构测试代码2.1引入Ginkgo测试框架2.2尝试改造旧的测试用例2.3重构功能测试代码3.总结1.好险,差点被喷早几天发了一篇文章:《仅三天,我用GPT-4生成了性能全网第一的GolangWorkerPool,轻松打败GitHub万星项目》,这标题是挺容易被怼,哇咔咔;不过最......
  • 自动化测试代码 java
    自动化测试代码Java实现流程自动化测试在软件开发过程中扮演着重要的角色,它可以帮助我们提高测试效率,并保证软件质量。在Java开发中,我们可以使用一些成熟的框架和工具来实现自动化测试。下面我将介绍一下实现自动化测试代码的流程,并提供相应的代码示例。流程概览下面的表格展......
  • 使用 Benchmark.NET 测试代码性能
    今天,我们将研究如何使用Benchmark.Net来测试代码性能。借助基准测试,我们可以创建基准来验证所做的更改是否按预期工作并且不会导致性能下降。并非每个项目都需要进行基准测试,但是如果您正在开发的是NuGet程序包或通用dll,则很有意义。 今天,我们将研究如何......
  • OpenGL环境配置 和 测试代码(GLEW/GLFW VS2019)
    1.配置 2.测试代码:2.1 在黑板色的窗口上画一个三角形1#include<iostream>23//GLEW4#defineGLEW_STATIC5#include<GL/glew.h>67//GLFW8#include<GLFW/glfw3.h>91011//Functionprototypes12voidkey_callback(GLFWw......
  • N层研习中的测试代码01
    这是前文《N层研习记录01:试图通过Boolean参数控制并发冲突的检查方式(LINQtoSQL)》用到的测试代码。只是包含了其中最重要的部分,如果要想获取完整的代码,可以通过以下地址进行下载:下载地址2:http://u.115.com/file/f26716bcc2以上地址如果均不能下载,请留言通知我!测试代码的服务端使......