【问题标题】:RAND_bytes not invoking though setting a RAND_set_rand_method()?通过设置 RAND_set_rand_method() 没有调用 RAND_bytes?
【发布时间】:2019-07-05 10:23:59
【问题描述】:

即使我们使用本地函数设置 currentMethod.bytes 来生成随机数,RAND_bytes 也不会被调用。在我们设置RAND_set_rand_method(&cuurentMethod)之后。

这里我附上了我已经尝试过的链接 [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(&currentMethod))!= 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;
}

预期结果是我们的本地函数应该调用。另外,在Linux系统中调用RAND_bytes()是否需要设置fips模式?

【问题讨论】:

  • 在您的问题正文中包含minimal reproducible example
  • 另外,RAND_set_rand_method() 不会返回任何东西……至少在 OpenSSL 1.1.1 中不会。它是否在该版本之前或之后的某个时间点更改了原型以返回一个值?
  • 另外,您似乎从main() 向后返回值。0/EXIT_SUCCESS 成功,EXIT_FAILURE(通常定义为 1)错误...
  • 感谢您的回复,在 OpenSSL v2.1.0 中,RAND_set_rand_method() 是返回值(wiki.openssl.org/index.php/Random_Numbers),如果有任何与控制台上的 local_function_rand() 相关的日志打印,请告诉我。
  • OpenSSL 没有这样的版本...

标签: c openssl cryptography fips


【解决方案1】:

在清理并最小化您的测试程序并填写缺失的部分之后:

#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(&currentMethod);

  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

它按预期工作; RAND_bytes() 正在调用用户提供的函数。如果您从代码中得到不同的结果,那么您的问题中未包含的位可能存在问题。

【讨论】:

  • 谢谢肖恩,当我使用 OpenSSL(1.0.2g) 的简单示例执行时,它工作正常。
猜你喜欢
  • 2014-02-10
  • 2020-07-15
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
相关资源
最近更新 更多