使用CyaSSL Keygen时出现分段错误

时间:2015-03-23 19:09:33

标签: c ssl wolfssl

我尝试使用第7.7节中的示例来使CyaSSL的Keygen函数工作:http://www.yassl.com/yaSSL/Docs-cyassl-manual-7-keys-and-certificates.html

我使用带有--enable-keygen选项的CyaSSL 3.2.0,但也无法使用3.1.0。

这是代码:

#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;
}

我在使用InitRsaKey的行中遇到了分段错误,可能是因为写入无效或其他原因。

任何人都知道我的问题可能在哪里?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

早上好,请不要忘记包含options.h标题。这将确保您在项目中获得正确的配置设置,例如,如果使用--enable-keygen配置CyaSSL,然后查看cyassl / options.h,您将看到行#undef CYASSL_KEY_GEN后跟#define CYASSL_KEY_GEN。同样在你的makefile中不要忘记包含cyassl库。这可以使用构建行中的-lcyassl来完成。请参阅以下代码以供参考:

#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;
}

生成文件:

 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