WolfSSL中ecc_mul2add()函数未定义引用问题求助
关于WolfSSL中
ecc_mul2add()未定义引用问题的排查 问题背景
- 已确认WolfSSL源码需编译FP_ECC模块,编译时已添加相关参数,编译命令如下:
./configure --enable-eccshamir --enable-fpecc --enable-opensslextra --enable-ecc --enable-ecccustcurves CFLAGS="-DWOLFSSL_TEST_CERT -DWOLFSSL_DER_TO_PEM -DHAVE_ECC_KOBLITZ -DWOLFSSL_PUBLIC_MP -DUSE_ECC_B_PARAM" && make && sudo make install - 编译自定义代码时出现错误:
undefined reference to 'ecc_mul2add()' - 未在WolfSSL测试文件中找到该函数的使用示例
自定义代码
#include <iostream> #include <string> #include <unistd.h> #include <wolfssl/options.h> #include <wolfssl/wolfcrypt/settings.h> #include <wolfssl/ssl.h> #include <wolfssl/wolfcrypt/types.h> #include <wolfssl/wolfcrypt/random.h> #include <wolfssl/wolfcrypt/ecc.h> #include <wolfssl/wolfcrypt/sp_int.h> #include <wolfssl/wolfcrypt/integer.h> #include <wolfssl/wolfcrypt/wolfmath.h> #include <wolfssl/wolfcrypt/sha.h> #include <wolfssl/wolfcrypt/error-crypt.h> #include <wolfssl/wolfcrypt/hash.h> #include <wolfssl/wolfcrypt/asn.h> #include <wolfssl/wolfcrypt/aes.h> //#include <wolfssl/wolfcrypt/sp.h> using namespace std; int main(){ ecc_key key; int ret; WC_RNG rng; wc_ecc_init(&key); wc_InitRng(&rng); int curveId = ECC_SECP256R1; int keySize = wc_ecc_get_curve_size_from_id(curveId); ret = wc_ecc_make_key_ex(&rng, keySize, &key, curveId); if (ret != MP_OKAY) { // error handling } //ecc_projective_add_point(); ecc_point *point_a = wc_ecc_new_point(); ecc_point *point_b = wc_ecc_new_point(); mp_int num1,num2,num3; mp_init(&num1); mp_init(&num2); mp_init(&num3); mp_set_int(&num1,23); mp_set_int(&num2,45); mp_add(&num1,&num2,&num3); mp_int a,b,prime,order,num; mp_init_multi(&a,&b,&prime,&order,&num,NULL); ret = mp_read_radix(&a,key.dp->Af,16); cout<<"get mp_int af: "<<ret<<endl; ret = mp_read_radix(&b,key.dp->Bf,16); cout<<"get mp_int bf: "<<ret<<endl; ret = mp_read_radix(&prime,key.dp->prime,16); cout<<"get mp_int prime: "<<ret<<endl; ret = mp_read_radix(&order,key.dp->order,16); cout<<"get mp_int order: "<<ret<<endl; ret = wc_ecc_gen_k(&rng,32,&num,&order); cout<<"get mp_int num: "<<ret<<endl; ecc_point* point = wc_ecc_new_point(); //ecc_point* point_res = wc_ecc_new_point(); ret = wc_ecc_get_generator(point,wc_ecc_get_curve_idx(ECC_SECP256R1)); cout<<"get ecc_point point: "<<ret<<endl; ret = wc_ecc_mulmod(&num1,point,point_a,&a,&prime,1); cout<<"num1 * point: "<<ret<<endl; ret = wc_ecc_mulmod(&num2,point,point_b,&a,&prime,1); cout<<"num2 * point: "<<ret<<endl; mp_int ka,kb; mp_init(&ka); mp_init(&kb); mp_set_int(&ka,1); mp_set_int(&kb,1); ecc_point* point_c = wc_ecc_new_point(); ret = ecc_mul2add(point_a,&ka,point_b,&kb,point_c,&a,&prime,NULL); cout<<"ecc_mul2add: "<<ret<<endl; return 0; }
排查思路与解决方法
1. 确认函数是否被编译进库文件
- 检查WolfSSL编译后的库文件中是否包含
ecc_mul2add符号:# 动态库检查 nm -D /usr/local/lib/libwolfssl.so | grep ecc_mul2add # 静态库检查 nm /usr/local/lib/libwolfssl.a | grep ecc_mul2add - 若未找到该符号,说明编译时未启用相关模块:
- 确认
--enable-fpecc参数是否被正确识别,查看configure输出日志中是否有FP_ECC: yes的记录 - 核实WolfSSL版本是否支持
--enable-fpecc参数,部分旧版本可能参数命名不同
- 确认
2. 检查函数的对外可见性
ecc_mul2add可能是内部函数,未用WOLFSSL_API声明对外暴露:- 查看WolfSSL源码中
ecc_mul2add的定义,确认是否有WOLFSSL_API修饰 - 若为内部函数,建议替换为WolfSSL对外提供的封装接口(如
wc_ecc_mul_add,若存在);若必须使用该函数,可修改源码添加WOLFSSL_API声明(不推荐,可能破坏兼容性)
- 查看WolfSSL源码中
3. 确保编译自定义代码时的链接参数正确
- 编译时需正确链接WolfSSL库,且链接顺序正确,示例编译命令:
g++ your_code.cpp -o your_code -lwolfssl -L/usr/local/lib -I/usr/local/include - 使用静态库时,需添加必要的依赖库(如
-lm数学库)
4. 保持编译宏定义一致
- 编译自定义代码时,需添加与WolfSSL编译时相同的宏定义:
g++ your_code.cpp -o your_code -lwolfssl -L/usr/local/lib -I/usr/local/include -DWOLFSSL_TEST_CERT -DWOLFSSL_DER_TO_PEM -DHAVE_ECC_KOBLITZ -DWOLFSSL_PUBLIC_MP -DUSE_ECC_B_PARAM - 宏定义不一致会导致函数声明被条件编译屏蔽,引发未定义引用错误
内容的提问来源于stack exchange,提问作者zihao wang
相关产品推荐
相关产品推荐

