【问题标题】:RSA_generate_key() using prngd instead of /dev/random or /dev/urandomRSA_generate_key() 使用 prngd 而不是 /dev/random 或 /dev/urandom
【发布时间】:2013-02-02 14:00:20
【问题描述】:
我想在 HP-UX 11.11 上使用 RSA_generate_key()。但是hp-ux 11.11没有提供/dev/random或者/dev/urandom,所以需要使用openssl prngd。
请让我知道如何在 C 代码中默认使用它。我已经安装了 openssl 并且 prngd 可用。
$ ls /opt/openssl/prngd/prngd
/opt/openssl/prngd/prngd
如果您需要更多信息,请告诉我。
【问题讨论】:
标签:
c
cryptography
openssl
rsa
hp-ux
【解决方案1】:
注意到 prngd 使用与 EGD 相同的界面,请查看找到的说明 here。感兴趣的报价是:
在没有从内核提供熵的 /dev/*random 设备的系统上
或者,可以使用与 EGD 接口兼容的守护进程 PRNGD。
OpenSSL 在通过 RAND_bytes() 请求熵或通过 RAND_status() 第一次检查状态时自动查询 EGD,如果套接字位于 /var/run/egd-pool、/dev/egd-pool或 /etc/egd-pool。
因此,当您运行 prngd 时,请以 prngd /dev/egd-pool 或其他替代方式之一运行它
【解决方案2】:
prngd 通过网络连接模拟“/dev/random”和“/dev/urandom”。它支持基于 Unix 流的域套接字(“/var/run/egd-pool”)或(如果配置为)或使用 TCP 端口 708 或 4840 的 IP(默认值---可以更改)。
因此,在使用 Unix 域套接字时,它看起来像:
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int devrandom(void)
{
union
{
struct sockaddr sa;
struct sockaddr_un path;
} location;
int sock;
memset(&location,0,sizeof(location));
location.path.sun_family = AF_UNIX;
strcpy(location.path.sun_path,"/var/run/egd-pool");
sock = socket(AF_UNIX,SOCK_STREAM,0);
if (sock < 0)
return -1;
if (connect(sock,&location.sa,sizeof(struct sockaddr_un)) < 0)
return -1;
return sock;
}
这将返回一个文件描述符,您可以将其传递给 read() 以获取随机数据(注意:此代码未经测试)。基于 TCP/IP 的连接涉及更多,需要将套接字绑定到本地地址并连接到远程地址,但 Internet 上有大量此类代码的示例。