RSA_generate_key()使用prngd而不是/ dev/random或/ dev/urandom

Nag*_*aga 5 c openssl cryptography rsa hp-ux

我想RSA_generate_key()在HP-UX 11.11上使用.但是hp-ux 11.11不提供/ dev/random或/ dev/urandom,所以我需要使用openssl prngd.

请告诉我如何在C代码中默认使用它.我安装了openssl,可以使用prngd.

$ ls /opt/openssl/prngd/prngd  
/opt/openssl/prngd/prngd
Run Code Online (Sandbox Code Playgroud)

如果您需要更多信息,请与我们联系.

Sea*_*ner 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;
}
Run Code Online (Sandbox Code Playgroud)

这将返回一个文件描述符,您可以传递给 read() 以获得随机数据(注意:此代码未经测试)。基于 TCP/IP 的连接稍微复杂一些,需要将套接字绑定到本地地址并连接到远程地址,但 Internet 上有很多此类代码的示例。