C 通过设置RAND\u set\u RAND\u方法()不调用RAND\u字节?
即使我们使用本地函数设置currentMethod.bytes以生成随机数,C 通过设置RAND\u set\u RAND\u方法()不调用RAND\u字节?,c,openssl,cryptography,fips,C,Openssl,Cryptography,Fips,即使我们使用本地函数设置currentMethod.bytes以生成随机数,RAND_bytes也不会调用。在我们设置RAND\u set\u RAND\u方法(&cuurentMethod)之后 这里我附加了链接[我已经尝试过了] int main() { unsigned char rand[16]; int ret; RAND_METHOD *oldMethod,currentMethod,*temp; oldMethod = RAND_get_rand_m
RAND_bytes
也不会调用。在我们设置RAND\u set\u RAND\u方法(&cuurentMethod)
之后
这里我附加了链接[我已经尝试过了]
int main()
{
unsigned char rand[16];
int ret;
RAND_METHOD *oldMethod,currentMethod,*temp;
oldMethod = RAND_get_rand_method();/*getting default method*/
currentMethod = *oldMethod;
currentMethod.bytes = local_function_rand;
if((ret = RAND_set_rand_method(¤tMethod))!= 1)
return 0;
/* Now we are printing both address of local_function_method_rand() and
temp->bytes , those address are same after getting. */
temp = RAND_get_rand_method();
/* after we are comparing with RAND_SSLeay() function , to find default or not*/
if((ret = RAND_bytes(rand,16)) != 1)
return 0;
return 1;
}
预期结果是我们的本地函数应该调用。另外,要调用
RAND_bytes()
是否需要在Linux系统中设置fips模式?在清理和最小化测试程序并填充缺少的部分后:
#include <openssl/rand.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int local_function_rand(unsigned char *buf, int num) {
printf("in local_function_rand(); requested %d bytes\n", num);
memset(buf, 4, num); // RFC 1149.5 standard random number
return 1;
}
int main(void) {
unsigned char rand[16];
RAND_METHOD currentMethod = {.bytes = local_function_rand};
RAND_set_rand_method(¤tMethod);
if (RAND_bytes(rand, sizeof rand) != 1) {
return EXIT_FAILURE;
}
return 0;
}
它按预期工作;用户提供的函数正由
RAND\u bytes()
调用。如果您从代码中得到不同的结果,则问题中未包含的位可能有问题。在问题正文中包含a?也可称为RAND\u set\u RAND\u method()
不会返回任何内容…至少OpenSSL 1.1.1不会。它是否更改了原型以在该版本之前或之后的某个时间点返回值?此外,您似乎从main()
向后返回值。0
/退出成功
成功时,退出失败
(通常定义为1)关于错误…感谢您的回复,在OpenSSL v2.1.0中,RAND_set_RAND_method()是返回值(),您能告诉我是否有任何日志打印与控制台上的本地_函数_RAND()有关。没有这样的OpenSSL版本…谢谢Shawn,当我使用简单的OpenSSL示例执行时,它工作正常(1.0.2g)。
$ gcc -Wall -Wextra rand.c -lcrypto
$ ./a.out
in local_function_rand(); requested 16 bytes