使用CyaSSL Keygen时出现段错误

3
我正在尝试使用此处第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个回答

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

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 

谢谢!#include <cyassl/options.h> 似乎可以解决问题。 - KakaduChat
没问题。如果您有任何其他问题,请不要犹豫问! - Kaleb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接