无法计算使用Cygwin和OpenSSL一个CMAC [英] Unable to compute a CMAC using Cygwin and OpenSSL
问题描述
我要计算CMAC使用OpenSSL。我发现这个问题这帮助了我。
但我遇到用下面的code的一个问题:
的#include<的OpenSSL / cmac.h>无效dispHex(const的无符号字符*缓冲区,unsigned int类型的大小){
INT I = 0; 对于(i = 0; I<大小-1;我++){
的printf(%02X,缓冲[I]);
}
的printf(%02X \\ n,缓冲[I]);
}诠释主(){
size_t型out_len;
unsigned char型水库[16]; unsigned char型的mac_key [16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; unsigned char型味精[16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; CMAC_CTX * CMAC = CMAC_CTX_new();
CMAC_Init(CMAC,的mac_key,16,EVP_aes_128_cbc(),NULL);
CMAC_Update(CMAC,味精,sizeof的(MSG));
CMAC_Final(CMAC,RES和放大器; out_len);
dispHex(RES,sizeof的(RES)); 返回0;
}
/ OpenSSL的 - Win32的-llibeay32 -IC:
我的gcc -o test_cmac test_cmac_openssl.c -LC编译/ OpenSSL的-的Win32 /包括
和它产生 test_cmac.exe
没有问题。
但是当我运行它( ./ test_cmac.exe
),没有任何反应。它只是打印一个空行并停止:
XXX @ DESKTOP / cygdrive / E /
$ ./test_cmac.exeXX @ DESKTOP / cygdrive / E /
即使我添加的printf(...);
它会以同样的方式计算CMAC前
什么是奇怪的是,下面的程序:
的#include<的OpenSSL / hmac.h>无效dispHex(const的无符号字符*缓冲区,unsigned int类型的大小){
INT I = 0; 对于(i = 0; I<大小-1;我++){
的printf(%02X,缓冲[I]);
}
的printf(%02X \\ n,缓冲[I]);
}诠释主(){
size_t型out_len;
unsigned char型水库[32]; unsigned char型的mac_key [16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; unsigned char型味精[16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; HMAC_CTX HMAC;
HMAC_CTX_init(安培; HMAC);
HMAC_Init_ex(安培; HMAC,的mac_key,16,EVP_sha256(),NULL);
HMAC_Update(安培; HMAC,味精,sizeof的(MSG));
HMAC_Final(安培; HMAC,RES和放大器; out_len);
dispHex(RES,sizeof的(RES)); 返回0;
}
运行正常的情况:在的gcc -o test_hmac test_hmac_openssl.c -LC:/ OpenSSL的 - Win32的-llibeay32 -IC:/的OpenSSL的Win32 /包括
和 ./ test_hmac.exe
我得到:
XXX @ DESKTOP / cygdrive / E /
$ ./test_hmac.exe
... 9A 21 F8 2D 60 84 09 6C 08 98 A5 1F 23 8C C8 8F C4 A9 0℃C4 49 45 10 DA 39 B9 93 C0 C3 10 60 BEXXX @ DESKTOP / cygdrive / E /
所以我有点糊涂了...... 为什么它的工作原理与HMAC原始而与CMAC吗?是否有人已经遇到这样的问题呢?
我使用OpenSSL 32位版本: OpenSSL的版本
收益 OpenSSL的1.0.2e 2015年12月3日
。
我还检查了 C:\\的OpenSSL的Win32 \\
在PATH环境变量声明
为什么它的工作原理与HMAC原始而与CMAC吗?是否有人已经遇到这样的问题呢?
块引用>我猜大部分的问题都是由于混合和匹配的编译器。闪亮亮的的Win32 OpenSSL的使用微软的编译器,而你正在使用GCC。有可能是运行时的一些混合和匹配了。
我有点惊讶HMAC code和预期一样的配置。我猜你真的很幸运。
我下面的作品,但也有一些区别:
的#include<的OpenSSL / evp.h>
#包括LT&;的OpenSSL / cmac.h>#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&ASSERT.H GT;无效dispHex(const的无符号字符*缓冲区,unsigned int类型的大小){
unsigned int类型I = 0; 对于(i = 0; I<大小;我++){
的printf(%02X,缓冲[I]);
}
的printf(\\ n);
}诠释主(){
INT RC = 0;
为size_t out_len = 0;
unsigned char型水库[EVP_MAX_MD_SIZE] unsigned char型的mac_key [16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; unsigned char型味精[16] = {0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07,
0×00,0×01,0×02,×03,0×04,0×05,0×06,0×07}; CMAC_CTX * CMAC = CMAC_CTX_new();
断言(CMAC!= NULL); RC = CMAC_Init(CMAC,mac_key的,的sizeof(的mac_key),EVP_aes_128_cbc(),NULL);
断言(RC == 1); RC = CMAC_Update(CMAC,味精,sizeof的(MSG));
断言(RC == 1); RC = CMAC_Final(CMAC,RES和放大器; out_len);
断言(RC == 1); dispHex(RES,out_len); CMAC_CTX_free(CMAC); 返回0;
}您的配置,我用一之间的区别:
- 的相同的编译器既用于OpenSSL和测试程序
RES
是EVP_MAX_MD_SIZE
尺寸CMAC_Init
使用的sizeof(的mac_key)
out_len
初始化displayHex
使用out_len
,而不是的sizeof(RES)
使用
的sizeof(RES)
将打印16个字节的MAC,然后打印16乱码,因为RES
声明为unsigned char型水库[32]
。我不认为你有那么远,所以记住它。该项目将产生以下结果。我不知道这是否是校正/预期的结果,但它产生的结果,并输出它
$ ./test.exe
43 91 63 0E 47 4E 75 A6 2D 95 7A 04 1A E8 CC CC<击> 的OpenSSL不支持Cygwin的-64 的,所以你需要使用的东西i686的版本。另请参见问题#4326:无法配置Cygwin的-x64 对OpenSSL的Bug跟踪。击>
下面是如何在Cygwin的-64打造的OpenSSL。 OpenSSL的构建脚本有几个弯,所以你不能使用的
配置
的。你必须使用的配置
的,调出三重。因为配置
应该得到的东西的bug跟踪系统的问题仍然是有效的权利。$卷曲https://www.openssl.org/source/openssl-1.0.2f.tar.gz -o OpenSSL的-1.0.2f.tar.gz
...
$焦油-xzf的OpenSSL 1.0.2f.tar.gz
...
$ CD的OpenSSL 1.0.2f然后:
$出口KERNEL_BITS = 64
$的./configure Cygwin的-x86_64的共享不SSL2无SSL3 --openssldir =$ HOME / SSL
...
$靠化妆
...
$化妆
...
$使install_sw
install_sw
安装头在$ OPENSSLDIR /包括
,而在$库OPENSSLDIR / lib目录
。它不会安装手册页。您再编译,链接:
$ gcc的-I$ HOME / SSL /包括test.c的-o test.exe的$ HOME / SSL /lib/libcrypto.a
针对链接
libcrypto.a
意味着你避免库路径的问题。事情会只是工作给你的。I want to compute a CMAC using OpenSSL. I found this question which helped me.
But I am encountering a problem with the following code:
#include <openssl/cmac.h> void dispHex(const unsigned char *buffer, unsigned int size) { int i=0; for (i=0; i<size-1; i++) { printf("%02X ", buffer[i]); } printf("%02x\n", buffer[i]); } int main() { size_t out_len; unsigned char res[16]; unsigned char mac_key[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; unsigned char msg[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; CMAC_CTX *cmac = CMAC_CTX_new(); CMAC_Init(cmac, mac_key, 16, EVP_aes_128_cbc(), NULL); CMAC_Update(cmac, msg, sizeof(msg)); CMAC_Final(cmac, res, &out_len); dispHex(res, sizeof(res)); return 0; }
I compile it with
gcc -o test_cmac test_cmac_openssl.c -L C:/OpenSSL-Win32 -llibeay32 -I C:/OpenSSL-Win32/include
and it producestest_cmac.exe
without problem.but when I run it (
./test_cmac.exe
), nothing happens. It just prints an empty line and stops:xxx@DESKTOP /cygdrive/e/ $ ./test_cmac.exe xx@DESKTOP /cygdrive/e/
Even if I add
printf("...");
before the CMAC computation it goes the same way.What is strange is that the following program:
#include <openssl/hmac.h> void dispHex(const unsigned char *buffer, unsigned int size) { int i=0; for (i=0; i<size-1; i++) { printf("%02X ", buffer[i]); } printf("%02X\n", buffer[i]); } int main() { size_t out_len; unsigned char res[32]; unsigned char mac_key[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; unsigned char msg[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; HMAC_CTX hmac; HMAC_CTX_init(&hmac); HMAC_Init_ex(&hmac, mac_key, 16, EVP_sha256(), NULL); HMAC_Update(&hmac, msg, sizeof(msg)); HMAC_Final(&hmac, res, &out_len); dispHex(res, sizeof(res)); return 0;
}
runs normally: after
gcc -o test_hmac test_hmac_openssl.c -L C:/OpenSSL-Win32 -llibeay32 -I C:/OpenSSL-Win32/include
and./test_hmac.exe
I get:xxx@DESKTOP /cygdrive/e/ $ ./test_hmac.exe ...9A 21 F8 2D 60 84 6C 09 08 98 A5 1F 23 8C C8 8F C4 A9 0C C4 49 45 DA 10 B9 39 C0 93 C3 10 60 BE xxx@DESKTOP /cygdrive/e/
So I'm a little confused... Why does it works with the HMAC primitive but not with the CMAC one? Does anybody already encountered this kind of problem?
I am using OpenSSL 32bit version:
openssl version
returnsOpenSSL 1.0.2e 3 Dec 2015
. I also checked thatC:\OpenSSL-Win32\
is declared in the PATH environment variable.解决方案Why does it works with the HMAC primitive but not with the CMAC one? Does anybody already encountered this kind of problem?
I'm guessing most of the problems are due to mixing and matching compilers. Shining Light's Win32 OpenSSL use Microsoft's compiler, while you are using GCC. There's probably some mixing and matching of runtimes, too.
I'm kind of surprised the HMAC code worked as expected in the configuration. I guess you got lucky.
The following works for me, but there are some differences:
#include <openssl/evp.h> #include <openssl/cmac.h> #include <stdio.h> #include <string.h> #include <assert.h> void dispHex(const unsigned char *buffer, unsigned int size) { unsigned int i=0; for (i=0; i<size; i++) { printf("%02X ", buffer[i]); } printf("\n"); } int main() { int rc = 0; size_t out_len = 0; unsigned char res[EVP_MAX_MD_SIZE]; unsigned char mac_key[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; unsigned char msg[16] = { 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07, 0x00, 0x01 ,0x02 ,0x03, 0x04, 0x05, 0x06, 0x07}; CMAC_CTX *cmac = CMAC_CTX_new(); assert(cmac != NULL); rc = CMAC_Init(cmac, mac_key, sizeof(mac_key), EVP_aes_128_cbc(), NULL); assert(rc == 1); rc = CMAC_Update(cmac, msg, sizeof(msg)); assert(rc == 1); rc = CMAC_Final(cmac, res, &out_len); assert(rc == 1); dispHex(res, out_len); CMAC_CTX_free(cmac); return 0; }
The differences between your configuration and the one I used:
- The same compiler is used for both OpenSSL and the test program
res
isEVP_MAX_MD_SIZE
in sizeCMAC_Init
usessizeof(mac_key)
out_len
is initializeddisplayHex
usesout_len
, and notsizeof(res)
Using
sizeof(res)
will print the 16 bytes of the MAC, and then print 16 garbage characters becauseres
is declared asunsigned char res[32]
. I don't think you got that far, so keep it in mind.
The program produces the following result. I don't know if this is the corrected/expected result, but it produces a result and prints it:
$ ./test.exe 43 91 63 0E 47 4E 75 A6 2D 95 7A 04 1A E8 CC CC
OpenSSL does not support Cygwin-x64, so you will need to use i686 version of things. Also see Issue #4326: Failed to configure for Cygwin-x64 on the OpenSSL Bug Tracker.Here's how to build OpenSSL under Cygwin-x64. OpenSSL's build script has a few bends, so you can't use
config
. You have to useConfigure
and call out the triplet. The bug tracker issue is still valid becauseconfig
is supposed to get things right.$ curl https://www.openssl.org/source/openssl-1.0.2f.tar.gz -o openssl-1.0.2f.tar.gz ... $ tar -xzf openssl-1.0.2f.tar.gz ... $ cd openssl-1.0.2f
Then:
$ export KERNEL_BITS=64 $ ./Configure Cygwin-x86_64 shared no-ssl2 no-ssl3 --openssldir="$HOME/ssl" ... $ make depend ... $ make ... $ make install_sw
install_sw
installs the headers in$OPENSSLDIR/include
, and the libraries in$OPENSSLDIR/lib
. It does not install the man pages.You then compile and link with:
$ gcc -I "$HOME/ssl/include" test.c -o test.exe "$HOME/ssl/lib/libcrypto.a"
Linking against
libcrypto.a
means you avoid library path problems. Things will "just work" for you.这篇关于无法计算使用Cygwin和OpenSSL一个CMAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!