1 (edited by telina 2017-05-04 02:39:12)

Topic: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hello,

I am trying to use the DH key agreement but I keep running into a Segmentation Fault. After some debugging and analysing memory usage I came to the conclusion, that the bug must lie within the library. Even the code in your provided test wouldn't work.
I read through the code and figured that  wc_InitDhKey(DhKey* key) (dh.c) calls mp_init_multi(&key->p, &key->g, NULL, NULL, NULL, NULL) of the used integer library (in my case tfm.c). I previously configured wolfssl using fastmath and realised that this might be the issue.
Reconfiguring without using fastmath "solved" the issue for me.

Is this 'just' a bug, or did I misconfigure fast math?


Edit: Since the version I used was fetched from GitHub a while ago,  I tried both the latest version and the latest stable version without success.

Best Regards,
telina

Share

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hi telina,

The most common culprit for seg faults during math operations is that the wolfSSL library and the application have not been compiled with the same preprocessor defines.

If you are using Autoconf (./configure) to configure and make the library, this will generate the <wolfssl/options.h> header with the defines used during compilation of wolfSSL.  Your application can then simply include <wolfssl/options.h> as the first wolfSSL header include, ie:

#include <wolfssl/options.h>
#include <wolfssl/wolfcrypt/dh.h>

Can you verify that you are using the same preprocessor defines when compiling both wolfSSL and your test app?

If that matches, can you please send me more details about the platform you are compiling on, how you are compiling wolfSSL, and a simple test app that demonstrates the seg fault?

Thanks!
Chris

3 (edited by telina 2017-05-05 03:02:23)

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hello,

I was indeed not including the options.h and just set the flags I need myself. I deleted all of them and included the wolfssl/options.h instead but it did not make a difference.
I am working on Debian Linux:

$ uname -a
Linux dev 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux

and I configured with the following flags:

./configure --enable-aesgcm --enable-aesccm --enable-mcapi --enable-fastmath --enable-sha512 --enable-ecc --with-libz

with generated the options.h attached to this post. It should be noted that I also wrote code for running AES with different modes of operation using fastmath without problems. In the future, this code is supposed to run on a board and I did cross compile the AES-tests with fastmath for it without problems, too.
After configuration I run make clean, make, sudo make install..

When I encountered this error in my code I tried to run a more simple example code taken but adjusted from the test.c.

#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/dh.h>
#include <wolfssl/options.h>

void
simple_test()
{
    size_t testVal = 23;
    WC_RNG rng;
    int    ret;
    ret = wc_InitRng(&rng);
    DhKey  smallKey;

    byte   p[2] = { 0, 5 };
    byte   g[2] = { 0, 2 };
    byte   priv[2];
    word32 privSz = sizeof(priv);
    byte   pub[2];
    word32 pubSz = sizeof(pub);
    
    printf("testval is %zi\n", testVal);
    wc_InitDhKey(&smallKey);
    printf("testval is %zi\n", testVal);

    ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
    if (ret != 0)
        printf("Error");

    ret = wc_DhGenerateKeyPair(&smallKey, &rng, priv, &privSz, pub, &pubSz);
    wc_FreeDhKey(&smallKey);
    if (ret != 0)
        printf("Error");
    
    wc_FreeRng(&rng);
    wc_FreeDhKey(&smallKey);
}

int main(int argc, char** argv) 
{
    simple_test();
   
    return 0;
}

I am using netbeans with its default generated Makefile. The compilation process looks like this

g++ -lwolfssl   -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
g++ -lwolfssl    -o dist/Debug/GNU-Linux/wolfssl_test build/Debug/GNU-Linux/main.o

And the output like this:

testval is 23
testval is 0

RUN FINISHED; Segmentation fault; real time: 10ms; user: 0ms; system: 0ms

I might have found something: Netbeans showed that there are 'unresolved includes' in wolfssl/wolfcrypt/dh.h leading to integer.h and to mpi_class.h, but I don't know what to do about it.

Hope that helps,
telina

Post's attachments

options.h 1.89 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.

Share

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hi telina,

As Chris initially pointed out it sounds like you have a configuration mismatch. In the configuration you sent you have the configure option

--enable-fastmath

Then in the Netbeans project it has "Unresolved includes" in "wolfssl/wolfcrypt/dh.h" leading to "integer.h". HOWEVER "integer.h" is the normal math libraries and SHOULD NOT be being built when using FASTMATH, instead that should be "tfm.h" (short for tom's fast math).

It sounds like the Netbeans project still does not have the same settings as the library was built with. Could you double check that the Netbeans project is including "wolfssl/options.h" header file?


Warmest Regards,

Kaleb

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hi Kaleb,

I managed to fix it by changing the include order and putting the options.h first >.< Nevertheless, dh.h includes integer.h and not tmf.h


Thanks for your help,
telina

Share

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hi telina,

Ahh yes, I regret we did not make specific note of that, it is customary not only to include options.h but to include it before any other wolfSSL header file so that the options are defined when the next header is included. I tend to start all external app development by doing the following:

#include <stdio.h>
#include <wolfssl/options.h>

... other c include headers here ....
... other wolfssl include headers here ...

I apologize we failed to specifically mention that it must be include before all other wolfSSL headers. I wil keep that in mind for any other customers that contact us with similar issues.

You said "I managed to fix it... Nevertheless ...", it is fine if a file includes integer.h as that file is IFDEF protected with this logic:

                                                                              
 35 #include <wolfssl/wolfcrypt/types.h>       /* will set MP_xxBIT if not default */
 36 #ifdef USE_FAST_MATH                                                             
 37     #include <wolfssl/wolfcrypt/tfm.h>                                           
 38 #else

      ... integer.h code ...

374 #endif /* USE_FAST_MATH */ 

The only issue is if you are getting errors from integer.h as you were previously that indicated that the define USE_FAST_MATH was not properly being detected. Is there still an outstanding issue or is everything now working properly?

Warmest Regards,

Kaleb

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hey Kaleb,

don't worry about, it was really my fault. Such a stupid mistake, but I was too determined on the idea that the problem would be caused by something bigger.
Everything works now and makes sense big_smile. Thank you very much! I am trying to get the code to run on the board now.
I had some troubles configuring the library to cross-compile for sparc-rtems but I got something working now.

Out of interest:
I was using --enable-mcapi, but it requires me to define --with-libz and I cannot do that, because I can't cross compile libz for sparc-rtems. If I don't use --enable-mcapi, I get undefined references for both wc_AesCtrEncrypt and wc_AesSetKeyDirect. After searching through the configure file, I realised the necessary defines are set with --enable-fortress as well, so I am using that instead. Is there a reason theres nothing like --enable-ctr ? At least to me it was not obvious that I had to use --enable-fortress, maybe this could be made more obvious/added to the documentation ?


Best Regards
telina

Share

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hi telina,

Just to double check is it possible you are working with a version older than wolfssl-3.11.0? We added the define for --enable-aesctr back in December of 2016 and it has been in our development master on github for some time, but it only made it into the newest release 3.11.0

If that is the case I would recommend you download our latest release from the website here: https://wolfssl.com/wolfSSL/download/downloadForm.php as that version also contained several bug fixes and addressed some other issues including adding a configure option for aesctr!

 --enable-aesctr


Warm Regards,

Kaleb

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Hey Kaleb,

I am indeed only using 3.10.3. I'm going to update to the newest stable version, thank you! Still the --enable-ctr flag does not seem to be documented in the manual  (https://www.wolfssl.com/wolfSSL/Docs-wo … lfssl.html)


Best Regards
telina

Share

Re: [Bug] Segmentation Fault in wc_InitDhKey using Fastmath

Thanks telina for this note!

I have added that to the list of things to update in the next round of documentation!

ACTION ITEM: add --enable-aesctr to configure options section


Warm Regards,

Kaleb