1

我想使用libtomcrypt運行示例rsa/dsa代碼。運行時錯誤:使用libtommath和libtomcrypt的分段錯誤

我已經安裝了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。任何幫助/鏈接將不勝感激。提前致謝。

+0

嘗試在您的'gcc'命令中添加'-DUSE_TFM'。 – LPs

+0

當我使用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安裝重新編譯libtomcrypt,但仍然得到與gcc的-DUSE_TFM選項相同的編譯時錯誤。但是,如果沒有-DUSE_TFM,gcc dsa_make_key_example.c -ltomcrypt -o dsa,則沒有編譯時錯誤。 – bholanath

+0

gcc -DLTM_LTM沒有編譯時錯誤dsa_make_key_example.c -ltomcrypt -ltfm -o dsa,但運行時分段錯誤。 – bholanath

回答

0

它已經過了一年多以來這是問過,但我有一個答案的一些組件和解決方法。

原因mp_init失敗的是,「math_descriptor」未初始化。mp_init是定義爲

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

其中ltc_mp是一個全球性的結構(ltc_math_descriptor型的),它保存指針的數學例程。

有幾種可用的數學例程的實現,用戶可以選擇他們想要的。無論出於何種原因,似乎都沒有爲libtomcrypt的某些版本選擇默認的數學實現。因此,的成員爲空,我們得到SIGSEGV。

這裏是一個手動解決方法:

您可以通過#define使提供所需ltc_math_descriptor結構到你的日常main()荷蘭國際集團的

  • LTM_DESC一個 - 內置數學LIB
  • 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; 

要使用它,請手動將其複製到全球的數學描述: 例如,在我的情況下,當地數學imlpementation,

ltc_mp = ltm_desc; 

現在libtomcrypt的作品。