RSA_generate_key_ex函数调用中RSA密钥生成错误。

问题描述 投票:0回答:1

我试图使用openssl生成RSA密钥。我在 RSA_generate_key_ex 却不知道为什么会出现这个错误。

我结合 ERR_get_error()ERR_error_string() 我有下一条消息: error:2506906C:lib(37):func(105):reason(108). 我还发现108错误代码意味着 RSA_R_DATA_GREATER_THAN_MOD_LEN。

我试图用下面的C代码来生成RSA密钥。为了简洁起见,我减少了自由调用和错误输出。

RSA* generateRSA()
{
  BIGNUM *bne = BN_new();
  if (bne == NULL)
  {
    return NULL;
  }
  if (BN_set_word(bne, RSA_F4) != 1)
  {
    return NULL;
  }

  RSA *r = RSA_new();
  if (r == NULL)
  {
    return NULL;
  }
  // THERE I'VE GOT ERROR
  if (RSA_generate_key_ex(r, 2048, bne, NULL)!= 1)
  {
    // ERR_get_error() returns 2506906C
    // ERR_error_string() gives me RSA_R_DATA_GREATER_THAN_MOD_LEN
    return NULL;
  }

  return r;
}

问题是这个错误是什么意思,怎么解决?

编辑: 我使用OpenSSL 1.1.0e 2017年2月16日。我把它作为 EDK II项目

openssl rsa bios uefi edk2
1个回答
0
投票

我发现随机生成器需要添加种子(openssl 1.0.2和1.1.0版本的随机生成器必须是显式种子)。

我检查了 RAND_status(). 返回0.所以解决的办法是,只需添加 RAND_seed() 在密钥生成之前。

const void* getSeedBuffer(int num);

RSA* generateRSA()
{
  RAND_seed(getSeedBuffer(1000), 1000); // don't forget to free memory
  BIGNUM *bne = BN_new();
  if (bne == NULL)
  {
    return NULL;
  }
  if (BN_set_word(bne, RSA_F4) != 1)
  {
    return NULL;
  }

  RSA *r = RSA_new();
  if (r == NULL)
  {
    return NULL;
  }
  if (RSA_generate_key_ex(r, 2048, bne, NULL)!= 1)
  {
    return NULL;
  }

  return r;
}
© www.soinside.com 2019 - 2024. All rights reserved.