BIO_set_md

BIO_set_md

opensslengine机制实现chiper,digest的替换遇到问题了,到底EVP是怎么样调用init,updata,final以及init,updata,final应该如何实现呢?
---------------------------------

消息摘要算法

简单接口

简单接口使用一个函数调用就可以完成消息摘要计算,这些接口包括MD2,MD4,MD5,MDC2,RIPEMD,SHA1,函数声明都一样。

MD5为例,函数声明为:

unsigned char *MD5(const unsigned char *d, unsigned long n, unsigned char *md);

其中 d 指向要计算消息摘要的数据,n 为数据长度,md 指向保存消息摘要的缓冲区。如果 md 不为 NULL,那么它的长度必须能够容纳计算出来的消息摘要。对MD5,这个长度至少是 MD5_DIGEST_LENGTH。如果 md 为 NULL,那么计算出来的消息摘要保存在一个静态数组里,函数返回指向这个数组的指针。

    下面是一个使用MD()计算消息摘要的小程序:

//ex1.cpp

#include <stdio.h>

#include <string.h>

#include <openssl/md5.h>

static char *hexstr(unsigned char *buf,int len)

{

     const char *set = "0123456789abcdef";

     static char str[65],*tmp;

     unsigned char *end;

     if (len > 32)

         len = 32;

     end = buf + len;

     tmp = &str[0];

     while (buf < end)

     {

         *tmp++ = set[ (*buf) >> 4 ];

         *tmp++ = set[ (*buf) & 0xF ];

         buf ++;

     }

     *tmp = '';

     return str;

}

int main(int argc, char* argv[])

{

     char *buf = "Hello,OpenSSL\n";

     unsigned char *md;

     md = MD5((const unsigned char*)buf,strlen(buf),NULL);

     printf("%s\n",hexstr(md,MD5_DIGEST_LENGTH));

     return 0;

}

 

这个程序计算出字符串”Hello,OpenSSL\n”的消息摘要为97aa490ee85f397134404f7bb524b587。可以用Unix下的md5sum程序检验是否正确:

[root@cat /root]# echo "Hello,OpenSSL" | md5sum

97aa490ee85f397134404f7bb524b587  -

可以看到结果一样。

其它算法的计算类似,只用替换源代码中的md5为相应的算法名即可。

 

标准接口

简单接口容易使用,但是它要求被摘要数据在时间和空间上都是连续的。要

计算不连续数据的摘要,就必须使用标准接口。事实上,简单接口也是通过调用标准接口工作的。

    以MD5为例,标准接口包括如下函数:

    void MD5_Init(MD5_CTX *c);

    void MD5_Update(MD5_CTX *c,const void *data,unsigned long len);

    void MD5_Final(unsigned char *md, MD5_CTX *c);

MD5_Init初始化MD5_CTX结构,MD5_Update计算摘要,MD5_Final输出摘要值。

    下面是使用标准接口的 ex1.cpp 程序(只有main函数不同):

//ex2.cpp

//包含的头文件,hexstr函数都和 ex1.cpp一样

int main(int argc, char* argv[])

{

     char *buf = "Hello";

     char *buf2 = ",";

     char *buf3 = "OpenSSL\n";

     unsigned char md[MD5_DIGEST_LENGTH];

     MD5_CTX ctx;

     MD5_Init(&ctx);

     MD5_Update(&ctx,buf,strlen(buf));

     MD5_Update(&ctx,buf2,strlen(buf2));

     MD5_Update(&ctx,buf3,strlen(buf3));

     MD5_Final(md,&ctx);

    

     printf("%s\n",hexstr(md,MD5_DIGEST_LENGTH));

     return 0;

}

检验:

    E:\ssl\md5\ex2\Debug>ex2

97aa490ee85f397134404f7bb524b587

可以看出,和 ex1 和 md5sum 的结果一致。   

EVP 接口

#include <stdio.h>

#include <string.h>

#include <openssl/evp.h>

static char *hexstr(unsigned char *buf,int len)

{

     const char *set = "0123456789abcdef";

     static char str[65],*tmp;

     unsigned char *end;

     if (len > 32)

         len = 32;

     end = buf + len;

     tmp = &str[0];

     while (buf < end)

     {

         *tmp++ = set[ (*buf) >> 4 ];

         *tmp++ = set[ (*buf) & 0xF ];

         buf ++;

     }

     *tmp = '';

     return str;

}

int main(int argc, char* argv[])

{

     char *buf = "Hello";

     char *buf2 = ",";

     char *buf3 = "OpenSSL\n";

     unsigned int mdlen;

     unsigned char md[EVP_MAX_MD_SIZE];

     EVP_MD_CTX ctx;

     const EVP_MD *type = EVP_md5();

     OpenSSL_add_all_digests();

     if (argc > 1)

     {

         type = EVP_get_digestbyname(argv[1]);

         if (type == NULL)

         {

              fprintf(stderr,"Use default : MD5\n");

              type = EVP_md5();

         }

     }

     EVP_DigestInit(&ctx,type);

     EVP_DigestUpdate(&ctx,buf,strlen(buf));

     EVP_DigestUpdate(&ctx,buf2,strlen(buf2));

     EVP_DigestUpdate(&ctx,buf3,strlen(buf3));

     EVP_DigestFinal(&ctx,md,&mdlen);    

     printf("%s\n",hexstr(md,mdlen));

     return 0;

}

BIO 接口

BIO_f_md()返回消息摘要的BIO方法。任何经过一个消息摘要BIO的数据都

被自动摘要。BIO_set_md设置一个消息摘要BIO所使用的摘要算法。

    下面是使用BIO的MD5例子:

#include <stdio.h>

#include <string.h>

#include <openssl/evp.h>

#include <openssl/bio.h>

static char *hexstr(unsigned char *buf,int len)

{

     const char *set = "0123456789abcdef";

     static char str[65],*tmp;

     unsigned char *end;

     if (len > 32)

         len = 32;

     end = buf + len;

     tmp = &str[0];

     while (buf < end)

     {

         *tmp++ = set[ (*buf) >> 4 ];

         *tmp++ = set[ (*buf) & 0xF ];

         buf ++;

     }

     *tmp = '';

     return str;

}

int main(int argc, char* argv[])

{

     int len;

     const char *str = "Hello,OpenSSL\n";

     BIO *bio_null,*bio_md;

     unsigned char md[EVP_MAX_MD_SIZE];

     bio_null = BIO_new(BIO_s_null());

     bio_md = BIO_new(BIO_f_md());

     BIO_set_md(bio_md,EVP_md5());

     bio_md = BIO_push(bio_md,bio_null);

     BIO_write(bio_md,str,strlen(str));

     BIO_flush(bio_md);

     len = BIO_gets(bio_md,(char*)md,EVP_MAX_MD_SIZE);

     printf("%s\n",hexstr(md,len));

     BIO_free_all(bio_md);

     return 0;

}    

 

检验结果为:97aa490ee85f397134404f7bb524b587,与md5sum的结果相同,注意消息摘要BIO比较特殊:数据经过这种BIO不被修改,只是摘要值保留在BIO中,需要用BIO_gets而不是BIO_read读取

 

 

posted @ 2013-08-14 18:06 心灵捕手 阅读( ...) 评论( ...)   编辑 收藏
OpenWrt-libtool: compile: aarch64-openwrt-linux-musl-gcc -DHAVE_CONFIG_H -I. -I./compat -I./include -I./include -I/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/staging_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/usr/include -I/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/staging_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/include -I/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/staging_dir/usr-be68_be77/include -I/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/../sdk/mtk798x/openwrt-21.02/staging_dir/toolchain-aarch64_cortex-a53_gcc-8.4.0_musl/include -fpic -MT bufferevent_openssl.lo -MD -MP -MF .deps/bufferevent_openssl.Tpo -c bufferevent_openssl.c -fPIC -DPIC -o .libs/bufferevent_openssl.o bufferevent_openssl.c: In function 'bio_bufferevent_new': bufferevent_openssl.c:106:3: error: dereferencing pointer to incomplete type 'BIO' {aka 'struct bio_st'} b->init = 0; ^~ bufferevent_openssl.c: At top level: bufferevent_openssl.c:228:1: error: variable 'methods_bufferevent' has initializer but incomplete type static BIO_METHOD methods_bufferevent = { ^~~~~~ bufferevent_openssl.c:79:27: warning: excess elements in struct initializer #define BIO_TYPE_LIBEVENT 57 ^~ bufferevent_openssl.c:229:2: note: in expansion of macro 'BIO_TYPE_LIBEVENT' BIO_TYPE_LIBEVENT, "bufferevent", ^~~~~~~~~~~~~~~~~ bufferevent_openssl.c:79:27: note: (near initialization for 'methods_bufferevent') #define BIO_TYPE_LIBEVENT 57 ^~ bufferevent_openssl.c:229:2: note: in expansion of macro 'BIO_TYPE_LIBEVENT' BIO_TYPE_LIBEVENT, "bufferevent", ^~~~~~~~~~~~~~~~~ bufferevent_openssl.c:229:21: warning: excess elements in struct initializer BIO_TYPE_LIBEVENT, "bufferevent", ^~~~~~~~~~~~~ bufferevent_openssl.c:229:21: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:230:2: warning: excess elements in struct initializer bio_bufferevent_write, ^~~~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:230:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:231:2: warning: excess elements in struct initializer bio_bufferevent_read, ^~~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:231:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:232:2: warning: excess elements in struct initializer bio_bufferevent_puts, ^~~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:232:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:233:2: warning: excess elements in struct initializer NULL /* bio_bufferevent_gets */, ^~~~ bufferevent_openssl.c:233:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:234:2: warning: excess elements in struct initializer bio_bufferevent_ctrl, ^~~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:234:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:235:2: warning: excess elements in struct initializer bio_bufferevent_new, ^~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:235:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:236:2: warning: excess elements in struct initializer bio_bufferevent_free, ^~~~~~~~~~~~~~~~~~~~ bufferevent_openssl.c:236:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:237:2: warning: excess elements in struct initializer NULL /* callback_ctrl */, ^~~~ bufferevent_openssl.c:237:2: note: (near initialization for 'methods_bufferevent') bufferevent_openssl.c:228:19: error: storage size of 'methods_bufferevent' isn't known static BIO_METHOD methods_bufferevent = { ^~~~~~~~~~~~~~~~~~~ Makefile:766: recipe for target 'bufferevent_openssl.lo' failed make[6]: *** [bufferevent_openssl.lo] Error 1 make[6]: Leaving directory '/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/build_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/libevent-2.0.21-stable' Makefile:824: recipe for target 'all-recursive' failed make[5]: *** [all-recursive] Error 1 make[5]: Leaving directory '/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/build_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/libevent-2.0.21-stable' Makefile:569: recipe for target 'all' failed make[4]: *** [all] Error 2 make[4]: Leaving directory '/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/build_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/libevent-2.0.21-stable' Makefile:153: recipe for target '/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/build_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/libevent-2.0.21-stable/.built' failed make[3]: *** [/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/build_dir/target-aarch64-openwrt-linux-uclibc-be68_be77/libevent-2.0.21-stable/.built] Error 2 make[3]: Leaving directory '/home/soho/code/be67/iplatform/board/model_mtk_mt798x/openwrt/package/libevent2' package/Makefile:13 openssl是1.1.1w,libevent2是2.0.19.怎么解决,给出详细处理方法
最新发布
11-06
/usr/bin/ld: /tmp/cccycn5n.o: in function `decrypt_aes_gcm': openssl.c:(.text+0x8e): undefined reference to `EVP_CIPHER_CTX_new' /usr/bin/ld: openssl.c:(.text+0x97): undefined reference to `EVP_aes_256_gcm' /usr/bin/ld: openssl.c:(.text+0xb6): undefined reference to `EVP_DecryptInit_ex' /usr/bin/ld: openssl.c:(.text+0xcf): undefined reference to `EVP_CIPHER_CTX_ctrl' /usr/bin/ld: openssl.c:(.text+0xf3): undefined reference to `EVP_DecryptInit_ex' /usr/bin/ld: openssl.c:(.text+0x111): undefined reference to `EVP_DecryptUpdate' /usr/bin/ld: openssl.c:(.text+0x134): undefined reference to `EVP_CIPHER_CTX_ctrl' /usr/bin/ld: openssl.c:(.text+0x155): undefined reference to `EVP_DecryptFinal_ex' /usr/bin/ld: openssl.c:(.text+0x164): undefined reference to `EVP_CIPHER_CTX_free' /usr/bin/ld: /tmp/cccycn5n.o: in function `verify_signature': openssl.c:(.text+0x1f5): undefined reference to `PEM_read_PUBKEY' /usr/bin/ld: openssl.c:(.text+0x21b): undefined reference to `EVP_MD_CTX_new' /usr/bin/ld: openssl.c:(.text+0x224): undefined reference to `EVP_sha256' /usr/bin/ld: openssl.c:(.text+0x244): undefined reference to `EVP_DigestVerifyInit' /usr/bin/ld: openssl.c:(.text+0x254): undefined reference to `EVP_MD_CTX_free' /usr/bin/ld: openssl.c:(.text+0x260): undefined reference to `EVP_PKEY_free' /usr/bin/ld: openssl.c:(.text+0x280): undefined reference to `EVP_DigestVerifyUpdate' /usr/bin/ld: openssl.c:(.text+0x290): undefined reference to `EVP_MD_CTX_free' /usr/bin/ld: openssl.c:(.text+0x29c): undefined reference to `EVP_PKEY_free' /usr/bin/ld: openssl.c:(.text+0x2bc): undefined reference to `EVP_DigestVerifyFinal' /usr/bin/ld: openssl.c:(.text+0x2cb): undefined reference to `EVP_MD_CTX_free' /usr/bin/ld: openssl.c:(.text+0x2d7): undefined reference to `EVP_PKEY_free' /usr/bin/ld: /tmp/cccycn5n.o: in function `base64_decode': openssl.c:(.text+0x317): undefined reference to `BIO_new_mem_buf' /usr/bin/ld: openssl.c:(.text+0x320): undefined reference to `BIO_f_base64' /usr/bin/ld: openssl.c:(.text+0x328): undefined reference to `BIO_new' /usr/bin/ld: openssl.c:(.text+0x33d): undefined reference to `BIO_set_flags' /usr/bin/ld: openssl.c:(.text+0x350): undefined reference to `BIO_push' /usr/bin/ld: openssl.c:(.text+0x36a): undefined reference to `BIO_read' /usr/bin/ld: openssl.c:(.text+0x379): undefined reference to `BIO_free_all'
10-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值