相关文章推荐
道上混的领结  ·  openssl库HMAC使用 ...·  4 周前    · 
任性的凉面  ·  OpenOffice/LibreOffice ...·  2 年前    · 
严肃的枕头  ·  2021-07-31 ...·  2 年前    · 

由于使用的openssl库版本的问题 HMAC_CTX *HMAC_CTX_new(void) 为OpenSSL 1.1.0后开始引入的函数,老的库要使用 void HMAC_CTX_init(HMAC_CTX *ctx)

老的 HMAC库函数(版本1.1.0前)

#include <openssl/hmac.h>
 unsigned char *HMAC(const EVP_MD *evp_md, const void *key,
               int key_len, const unsigned char *d, int n,
               unsigned char *md, unsigned int *md_len);
 void HMAC_CTX_init(HMAC_CTX *ctx);
 int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len,
               const EVP_MD *md);
 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len,
                   const EVP_MD *md, ENGINE *impl);
 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, int len);
 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
 void HMAC_CTX_cleanup(HMAC_CTX *ctx);
 void HMAC_cleanup(HMAC_CTX *ctx);

HMAC的实现在crypto/hmac/hmac.c中,如下:

unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
                  const unsigned char *d, size_t n, unsigned char *md,
                  unsigned int *md_len)
	HMAC_CTX c;
	static unsigned char m[EVP_MAX_MD_SIZE];
	if (md == NULL) md=m;
	HMAC_CTX_init(&c);
	HMAC_Init(&c,key,key_len,evp_md);
	HMAC_Update(&c,d,n);
	HMAC_Final(&c,md,md_len);
	HMAC_CTX_cleanup(&c);
	return(md);
  • evp_md指明HMAC使用的摘要算法;
  • key为秘密密钥指针地址;
  • key_len为秘密密钥的长度;
  • d为需要做HMAC运算的数据指针地址;
  • n为d的长度;
  • md用于存放HMAC值;
  • md_len为HMAC值的长度。
int hmac_sha256_test(void)
    // The secret key for hashing
    const char key[] = "012345678";
    // The data that we're going to hash
    char data[] = "hello world";
    // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
    // Change the length accordingly with your choosen hash engine.
    unsigned char *result;
    unsigned int len = 32;
    result = (unsigned char *)malloc(sizeof(char) * len);
    HMAC(EVP_sha256(), key, strlen(key), (uint8_t *)data, strlen(data), result, &len);
    printf("HMAC digest: ");
    for (int i = 0; i != len; i++)
        printf("%02x", (unsigned int)result[i]);
    printf("\n");
    free(result);
    return 0;

在线验证计算hmac网站

OpenSSL 1.1.0 HMAC库函数

 #include <openssl/hmac.h>
 unsigned char *HMAC(const EVP_MD *evp_md, const void *key,
                     int key_len, const unsigned char *d, size_t n,
                     unsigned char *md, unsigned int *md_len);
 HMAC_CTX *HMAC_CTX_new(void);
 int HMAC_CTX_reset(HMAC_CTX *ctx);
 int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len,
                  const EVP_MD *md, ENGINE *impl);
 int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len);
 int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
 void HMAC_CTX_free(HMAC_CTX *ctx);
 int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx);
 void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags);
 const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx);
 size_t HMAC_size(const HMAC_CTX *e);

HMAC介绍

HMAC用于保护消息的完整性,它采用摘要算法对消息、填充以及秘密密钥进行混合运算。在消息传输时,用户不仅传送消息本身,还传送HMAC值。接收方接收数据后也进行HMAC运算,再比对MAC值是否一致。由于秘密密钥只有发送方和接收方才有,其他人不可能伪造假的HMAC值,从而能够知道消息是否被篡改。

ssl协议中用HMAC来保护发送消息,并且ssl客户端和服务端的HMAC密钥是不同的,即对于双方都有一个读MAC保护密钥和写MAC保护密钥。

openSSL官网
OpenSSL 中文手册
C++ HMAC_CTX_init函数代码示例

1https://github.com/sqlcipher/sqlcipher 下载源代码 2 # ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" 3 # make make: tclsh: Command not found make:
为了降低第三方应用集成华为云会议难度,并且提升开放接口的安全性,华为云会议开放能力中支持基于App ID的鉴权方式。App ID是一个应用的标识,同一个App ID可以同时在第三方的桌面终端、移动终端、Web应用上使用。 App ID鉴权原理 图1 第三方客户端App ID鉴权流程 鉴权前提: 开发者在华为云会议控制台上为自己的应用申请AppID,并获取App ID和App Key。 开发者在自己的服务端集成Signature生成算法,请参考“第三方服务集成Signature生成算法”。 SDK初始化时传
HMAC_CTX hctx; HMAC_CTX_init(&hctx); HMAC_Init_ex(&hctx, mac_key, sizeof(mac_key), EVP_sha1(), NULL); HMAC_Update(&hctx, pTemp + offset, DEFAULT_PAGESIZE - reserve - offset + IV_SIZE); HMAC_Update(&hctx, (const unsigned char)&
编译sqlcipher为sqlcipher.so动态,利用该动态编译apptest时,出现以下报错,说明报错的原因和修改方式: mips-linux-g++ -DSQLITE_HAS_CODEC -I/home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/include -c apptest.cpp -o apptest.o mips-linux-g++ -L/home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib -lsqlcipher -o apptest apptest.o /home/rtl8198/toolchain/msdk-4.8.5-mips-EB-4.4-u0.9.33-m32ut-180206/bin/../lib/gcc/mips-linux-uclibc/4.8.5/../../../../mips-linux-uclibc/bin/ld: warning: libz.so.1, needed by /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so, not found (try using -rpath or -rpath-link) /home/rtl8198/toolchain/msdk-4.8.5-mips-EB-4.4-u0.9.33-m32ut-180206/bin/../lib/gcc/mips-linux-uclibc/4.8.5/../../../../mips-linux-uclibc/bin/ld: warning: libcrypto.so.1.1.1, needed by /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so, not found (try using -rpath or -rpath-link) /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CipherUpdate@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `RAND_add@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_CTX_new@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_MD_size@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `HMAC_Init_ex@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CipherFinal_ex@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_CTX_free@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_sha1@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `RAND_bytes@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_sha256@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `HMAC_CTX_free@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_get_cipherbyname@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_iv_length@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_CTX_set_padding@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `HMAC_CTX_new@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_block_size@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_nid@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_sha512@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `HMAC_Final@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CIPHER_key_length@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `HMAC_Update@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `PKCS5_PBKDF2_HMAC@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `EVP_CipherInit_ex@OPENSSL_1_1_0' /home/Iplatform/openwrt/staging_dir/target-mips-openwrt-linux-uclibc-wr1552xv1/usr/lib/libsqlcipher.so: undefined reference to `OBJ_nid2sn@OPENSSL_1_1_0' collect2: error: ld returned 1 exit status
/home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_add' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_num_bits' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `HMAC_CTX_init' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EVP_sha256' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `PKCS5_PBKDF2_HMAC' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_is_on_curve' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `HMAC_Update' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `OPENSSL_cleanse' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_invert' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_rand_range' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_bn2bin' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_CTX_free' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `HMAC_Final' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `SHA256_Init' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `CRYPTO_memcmp' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_GROUP_clear_free' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `HMAC_Init_ex' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `RAND_bytes' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_bin2bn' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `HMAC_CTX_cleanup' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_new' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_CTX_new' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_clear_free' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_GROUP_new_by_curve_name' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `SHA256_Update' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `SHA256_Final' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_new' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_oct2point' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_GROUP_get0_generator' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_point2oct' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_mul' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_POINT_clear_free' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `EC_GROUP_get_order' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_div' /home/bba/work/BBA1_5/MBB_Platform/build/../apps/private/bins/mtk_4.6.3/libs//libtpap.so: undefined reference to `BN_mod_mul' collect2: ld returned 1 exit status Makefile:140: recipe for target 'httpd' failed make[2]: *** [httpd] Error 1 make[2]: Leaving directory '/home/bba/work/BBA1_5/MBB_Platform/apps/private/user/httpd' Makefile:236: recipe for target 'httpd' failed make[1]: *** [httpd] Error 2 make[1]: Leaving directory '/home/bba/work/BBA1_5/MBB_Platform/apps/private/user' make/apps/privapps/cmm.mk:8: recipe for target 'cmm' failed make: *** [cmm] Error 2
[ 66%] Building CXX object proxy_ws/CMakeFiles/proxy_lws.dir/proxy_lws.cpp.o In file included from /vagrant/include/libwebsockets.h:600, from /vagrant/proxy_ws/proxy_lws_utils.hpp:12, from /vagrant/proxy_ws/proxy_lws.cpp:1
HMAC: Hash-based Message Authentication Code,即基于Hash的消息鉴别码 /// @file algo_hmac.h #ifndef _ALGO_HMAC_H_ #define _ALGO_HMAC_H_ int HmacEncode(const char * algo, const char * key, unsigned int ...
有个项目又要用到openssl,在Linux下编译的时候没注意,报了以下错误 libcrypto.a(threads_pthread.o): In function `fork_once_func': threads_pthread.c:(.text+0x76): undefined reference to `pthread_atfork' libssl.a(ssl3_record.o): In...