RAND_bytes 通过设置 RAND_set_rand_method() 不调用?
RAND_bytes not invoking though setting a RAND_set_rand_method()?
即使我们用本地函数设置currentMethod.bytes来生成随机数,RAND_bytes
也没有调用。在我们设置 RAND_set_rand_method(&cuurentMethod)
.
之后
我在这里附上了 link [https://github.com/openssl/openssl/blob/master/test/sm2_internal_test.c] 我已经试过了。
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;
}
和运行它(使用OpenSSL 1.1.1):
$ gcc -Wall -Wextra rand.c -lcrypto
$ ./a.out
in local_function_rand(); requested 16 bytes
它按预期工作; user-supplied 函数正在被 RAND_bytes()
调用。如果您从代码中得到不同的结果,则可能是您未包含在问题中的部分有问题。
即使我们用本地函数设置currentMethod.bytes来生成随机数,RAND_bytes
也没有调用。在我们设置 RAND_set_rand_method(&cuurentMethod)
.
我在这里附上了 link [https://github.com/openssl/openssl/blob/master/test/sm2_internal_test.c] 我已经试过了。
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;
}
和运行它(使用OpenSSL 1.1.1):
$ gcc -Wall -Wextra rand.c -lcrypto
$ ./a.out
in local_function_rand(); requested 16 bytes
它按预期工作; user-supplied 函数正在被 RAND_bytes()
调用。如果您从代码中得到不同的结果,则可能是您未包含在问题中的部分有问题。