gcc - 运行时错误 : Segmentation fault with libtommath and libtomcrypt

标签 gcc shared-libraries static-libraries static-linking libtool

我正在尝试使用 libtomcrypt 运行示例 rsa/dsa 代码。

我首先通过 ma​​ke install 安装了 LibTomMath,结果创建了以下文件。

/usr/lib/libtommath.a /usr/include/tommath.h

之后我安装了 libtomcrypt 和 LibTomMath 作为外部库

CFLAGS="-DLTM_DESC -DUSE_LTM -I/usr/include" EXTRALIBS="/usr/lib/libtommath.a " make install

生成如下文件

/usr/lib/libtomcrypt.a

我在执行以下命令时没有收到任何错误

CFLAGS="-DLTM_DESC -DUSE_LTM -I/usr/include" EXTRALIBS="/usr/lib/libtommath.a " make test

我已经阅读了这份文件libtomcrypt_installationlibtomcrypt_resolved成功编译使用

gcc -DLTM_DESC rsa_make_key_example.c -o rsa -ltomcrypt 
or
gcc rsa_make_key_example.c -o rsa -ltomcrypt 

没有编译错误然而,当我尝试运行时,出现以下错误。

 ./rsa

 LTC_ARGCHK 'ltc_mp.name != NULL' failure on line 34 of file src/pk/rsa/rsa_make_key.c
 Aborted

这是我的示例 rsa 代码

#include <tomcrypt.h>
#include <stdio.h>

int main(void) {

# ifdef USE_LTM
ltc_mp = ltm_desc;
# elif defined (USE_TFM)
ltc_mp = tfm_desc;
# endif 


    rsa_key key;

    int      err;
    register_prng(&sprng_desc);

    if ((err = rsa_make_key(NULL, find_prng("sprng"), 1024/8, 65537,&key)) != CRYPT_OK) {
        printf("make_key error: %s\n", error_to_string(err));
        return -1;
    }
    /* use the key ... */
    return 0;

} 

这是我的示例 dsa 代码

#include <tomcrypt.h>
#include <stdio.h>

int main(void) {

# ifdef USE_LTM
ltc_mp = ltm_desc;
# elif defined (USE_TFM)
ltc_mp = tfm_desc;
# endif 


    int      err;
    register_prng(&sprng_desc);

    dsa_key key;


    if ((err = dsa_make_key(NULL, find_prng("sprng"), 20, 128,&key)) != CRYPT_OK) {
        printf("make_key error: %s\n", error_to_string(err));
        return -1;
    }
    /* use the key ... */
    return 0;

} 

下面是我编译成功的方法,

gcc dsa_make_key_example.c -o dsa -ltomcrypt 

当我尝试运行代码时,出现以下错误。

./dsa
segmentation fault

编辑 1: 我进一步调查,找到了段错误的原因

#ifdef LTC_MPI
#include <stdarg.h>

int ltc_init_multi(void **a, ...)
{
...
...    
if (mp_init(cur) != CRYPT_OK) ---> This line causes segmentation fault

我哪里出错了?如何解决这个问题才能成功运行这些程序?

我正在使用 linux 和 gcc。任何帮助/链接将不胜感激。提前致谢。

最佳答案

这个问题已经过去一年左右了,但我有一些答案和解决方法。

原因mp_init失败是“math_descriptor”未初始化。 mp_init定义为

#define mp_init(a) ltc_mp.init(a)

哪里ltc_mp是一个全局结构(类型为 ltc_math_descriptor ),它包含指向数学例程的指针。

有多种可用的数学例程实现,用户可以选择他们想要的。无论出于何种原因,似乎都没有为某些 libtomcrypt 构建选择默认的数学实现。因此,init ltc_mp的成员为空,我们得到 SIGSEGV。

这是一个手动解决方法:

您可以制作您想要的ltc_math_descriptor可用于您的结构 main()常规 #define正在其中之一

  • LTM_DESC -- 内置数学库
  • TFM_DESC -- 外部快速数学包
  • GMP_DESC——大概是 GNU MultiPrecision 实现?

之前#include <tomcrypt.h> (或通过在命令行上使用 -D)。 无论您选择哪个,都会声明一个相应的对象:

extern const ltc_math_descriptor ltm_desc;
extern const ltc_math_descriptor tfm_desc;
extern const ltc_math_descriptor gmp_desc;

要使用它,手动将它复制到全局数学描述符: 例如,在我的例子中,对于本地数学实现,

ltc_mp = ltm_desc;

现在 libtomcrypt 可以工作了。

关于gcc - 运行时错误 : Segmentation fault with libtommath and libtomcrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37955209/

相关文章:

c++ - 如何使用 scons 和 Visual C++ 链接到使用静态库本身的静态库?

c++ - 如何禁用缩小转换警告?

gcc:为什么没有优化文字类型?

c - 通过环境变量告诉 ld 在哪里查找目录

python - 如何将 C 库包装在 SWIG 中,它通常在 C 编译期间链接?

c++ - 静态链接可以导致执行一些初始化例程吗?

类中的 C++17 变体<any>

c - 下面是一个求一个数的阶乘的程序。为什么我得到 0 作为答案?我应该改用哪种数据类型?

linux - 如何在 makefile 中将您的项目链接到 64 位库

c - 使用静态库编译的共享库中 undefined symbol