Search code examples
csslwolfssl

Segmentation fault when using CyaSSL Keygen


I'm trying to get the Keygen function of CyaSSL to work using the example in section 7.7 from here: http://www.yassl.com/yaSSL/Docs-cyassl-manual-7-keys-and-certificates.html

I'm using CyaSSL 3.2.0 with the --enable-keygen option, but couldn't get it working with 3.1.0 either.

This is the code:

#include <stdio.h>
#include <cyassl/ctaocrypt/asn.h>
#include <cyassl/ctaocrypt/rsa.h>

int main() {
        RsaKey genKey;
        RNG rng;
        int ret;

        printf("%d\n",InitRng(&rng));
        printf("%d\n",InitRsaKey(&genKey, 0));
        ret = MakeRsaKey(&genKey, 1024, 65537, &rng);

        printf("ret: %d\n",ret);

        return 0;
}

I get a segmentation fault in the line with InitRsaKey, presumably because of an invalid write or something.

Anyone got an idea where my issue may be? Any help is appreciated


Solution

  • Good morning, please do not forget to include options.h header. This will ensure that you get the proper configuration settings in your project for example if you configure CyaSSL with --enable-keygen then view cyassl/options.h you will see the line #undef CYASSL_KEY_GEN followed by #define CYASSL_KEY_GEN. Also in your makefile do not forget to include the cyassl library. This can be accomplished using -lcyassl in your build line. See code below for reference:

    #include <stdio.h>
    #include <cyassl/options.h> //pull in the define for CYASSL_KEY_GEN
    #include <cyassl/ctaocrypt/asn.h>
    #include <cyassl/ctaocrypt/rsa.h>
    
    int main() {
            RsaKey genKey;
            RNG rng;
            int ret;
    
            printf("%d\n",InitRng(&rng));
            printf("%d\n",InitRsaKey(&genKey, 0));
            ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
    
            printf("ret: %d\n",ret);
    
            return 0;
    }
    

    Makefile:

     CC=gcc       #you can use clang or other instead of gcc                                                                   
     CFLAGS=-Wall                                                                    
     LIBS=-lpthread -lcyassl #must have -lcyassl in makefile                                                         
    
     all: run                                                                        
    
     #NOTE: arrows denote a hard tab, replace them with hard tab in makefile                                                                             
     run: test.o                                                                     
     →→→→$(CC) -o $@ $(LIBS) $^ $(CFLAGS) #put $(LIBS) into build command                                            
    
     .PHONY: clean all                                                               
    
     clean:                                                                          
     →→→→rm -f *.o test.o run