【问题标题】:Runtime error : Segmentation fault with libtommath and libtomcrypt运行时错误:libtommath 和 libtomcrypt 的分段错误
【发布时间】:2016-06-21 21:52:36
【问题描述】:

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

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

/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。任何帮助/链接将不胜感激。提前致谢。

【问题讨论】:

  • 尝试将-DUSE_TFM 添加到您的gcc 命令中。
  • 当我使用 gcc -DUSE_TFM dsa_make_key_example.c -ltomcrypt -ltfm -o dsa 时,它会给出编译时错误。 tfm_desc 未声明。然后我再次使用 CFLAGS="-DTFM_DESC -DUSE_TFM" EXTRALIBS=-ltfm make -f makefile.shared install 重建 libtomcrypt ,但使用 gcc 的 -DUSE_TFM 选项仍然出现相同的编译时错误。但是,没有 -DUSE_TFM ,gcc dsa_make_key_example.c -ltomcrypt -o dsa,没有编译时错误。
  • gcc -DLTM_LTM dsa_make_key_example.c -ltomcrypt -ltfm -o dsa 没有编译时错误,但是运行时分段错误。
  • 与 gcc -DUSE_LTM dsa_make_key_example.c -ltomcrypt -ltommath -o dsa ,它给出了编译时错误。 ltm_desc 未声明。

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


【解决方案1】:

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

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

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

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

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

这是一个手动解决方法:

您可以通过#defineing 之一使您想要的ltc_math_descriptor 结构可用于您的main() 例程

  • LTM_DESC -- 内置数学库
  • TFM_DESC -- 一个外部快速数学包
  • GMP_DESC -- 可能是 GNU MultiPrecision 实现?

#include &lt;tomcrypt.h&gt; 之前(或在命令行上使用-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 可以工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多