当时要是知道有下面这个网站,之前也不用那么辛苦了
网上流传的c语言国密核心算法好像至少两种,两种一起使用的话还会出现问题。所以如果某个功能已经有sm算法了,可以直接使用(可以让其提供调用方法名称,或hopper自己看)。
之前写的是一种方法。
今天再看看这个。
在编译与安装中含有 编译iPhone OS目标文件 的代码
export CC=clang
export CROSS_TOP=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer
export CROSS_SDK=iPhoneOS.sdk
export PATH="/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin:$PATH"
./Configure ios64-cross no-shared --prefix=/usr/local/openssl-ios64
make
步骤1 先下载源码GitHub - guanzhi/GmSSL: 支持国密SM2/SM3/SM4/SM9/ZUC/SSL的OpenSSL分支
步骤2.命令行cd到代码解压后的目录,输入上面的编译代码
然后出现了问题
Undefined symbols for architecture arm64:
"_EC_GFp_sm2z256_method", referenced from:
_curve_list in libcrypto.a(ec_curve.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [link_app.] Error 1
make[1]: *** [apps/gmssl] Error 2
make: *** [all] Error 2
yyy@YYY GmSSL-master %
这里有解决办法x86_64环境下生成arm64库失败的问题 · Issue #710 · guanzhi/GmSSL · GitHub
摘抄
方法1:
./Configure 时 加上 no-asm
方法2:
./Configure后, 打开Makefile,找到CFLAGS,加上-DGMSSL_NO_TURBO
其实,这个问题,打开源代码找一下_EC_GFp_sm2z256_method在哪调用,很快就明白了。
方法1就是将代码改成这个 ./Configure ios64-cross no-asm no-shared --prefix=/usr/local/openssl-ios64
yyy@YYY GmSSL-master % ./Configure ios64-cross no-shared no-asm --prefix=/usr/local/openssl-ios64
Configuring GmSSL version 2.5.4 (0x1010004fL)
target already defined - ios64-cross (offending arg: no-asm)
报错。
试试方法2
步骤3 成功
可是编译成的文件在哪
参考 文章正在审核中... - 简书 如何合并静态库
步骤4
sudo make install
然后这两个就是
头文件好像是 include文件夹
如何使用呢
新建工程 ,导入,
添加测试代码
测试代码在下载的源码目录下 /GmSSL-master/test/sm2test.c 和 sm2_lcl.h
自己建一个sm2test.h
编译报错
参考 《ios 如何导入openssl库 解决 'openssl/bn.h' file not found》ios 如何导入openssl库 解决 'openssl/bn.h' file not found - 简书
再解决一些小的引用问题后运行成功
以sm2p256test为例,把加密方法拆出来
//加密
static int yyy_sm2_en(const EC_GROUP *group, const EVP_MD *md,const char *xP, const char *yP, const char *M, const char *k, char **CCCC){
int ret = 0;
EC_KEY *pub_key = NULL;
EC_KEY *pri_key = NULL;
SM2CiphertextValue *cv = NULL;
unsigned char *tbuf = NULL;
long tlen;
unsigned char mbuf[128] = {0};
unsigned char cbuf[sizeof(mbuf) + 256] = {0};
size_t mlen, clen;
unsigned char *p;
if (!(pub_key = new_ec_key(group, NULL, xP, yP))) {
goto end;
}
change_rand(k);
if (!(cv = SM2_do_encrypt(md, (unsigned char *)M, strlen(M), pub_key))) {
goto end;
}
p = cbuf;
if ((clen = i2o_SM2CiphertextValue(group, cv, &p)) <= 0) {
goto end;
}
NSLog(@"密文是%@ ",[NSData dataWithBytes:cbuf length:clen ] );
end:
ERR_print_errors_fp(stderr);
restore_rand();
EC_KEY_free(pub_key);
EC_KEY_free(pri_key);
SM2CiphertextValue_free(cv);
OPENSSL_free(tbuf);
return ret;
}
调用方法
yyy_sm2_en(sm2p256test, EVP_sm3(), "435B39CCA8F3B508C1488AFC67BE491A0F7BA07E581A0E4849A5CF70628A7E0A",
"75DDBA78F15FEECB4C7895E2C1CDF5FE01DEBB2CDBADF45399CCF77BBA076A42",
"encryption standard",
"4C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F", &c_c);
打印结果
2020-08-06 15:48:17.949882+0800 sm[3006:514579] 密文是{length = 116, bytes = 0x04245c26 fb68b1dd ddb12c4b 6bf9f2b6 ... 285e0748 0653426d }
示例代码