• Showing Page History #121399
  • Showing Page History #121400

Show page source of arc4random #121403

= arc4random 呼び出しの流れ

OpenSSL を切ってよければ arc4random.c/h と chacha.c/h を削除してもいいはずなので、どう呼び出している追いかけてみるテスト

== LibreSSL
arc4random.c/h あり
{{{
RAND_bytes() -> libressl/crypto/rand/rand_lib.c RAND_bytes()
  arc4random_buf() -> ttxssh/arc4random.c arc4random_buf()
    _rs_random_buf() -> ttxssh/arc4random.c _rs_random_buf()
      _rs_stir_if_needed() -> ttxssh/arc4random.c _rs_stir_if_needed()
        _rs_stir() -> ttxssh/arc4random.c _rs_stir()
          getentropy() -> ttxssh/arc4random.c getentropy() ※ここが RAND_bytes() だと無限ループになる
          _rs_init() -> ttxssh/arc4random.c _rs_init()
            chacha_keysetup() -> ttxssh/chacha.c chacha_keysetup()
            chacha_ivsetup() -> ttxssh/chacha.c chacha_ivsetup()
}}}
{{{
randombytes() -> ttxssh/ed25519_crypto_api.h arc4random_buf()
  arc4random_buf() -> ttxssh/arc4random.c arc4random_buf()
    以下同じ
}}}
{{{
arc4random() -> ttxssh/arc4random.c arc4random()
  _rs_random_u32() -> ttxssh/arc4random.c _rs_random_u32()
    _rs_stir_if_needed() -> ttxssh/arc4random.c _rs_stir_if_needed()
      以下同じ
}}}

== LibreSSL
 * arc4random.c/h をプロジェクトから削除、ファイルはそのまま、#include はそのまま(include は自前で本体はライブラリにまかせる・ちぐはぐ)のとき
 * arc4random.c/h をプロジェクトから削除、#include を変更(ファイル削除と同等)(すべてライブラリまかせ)
 * arc4random.c/h をプロジェクトに入れたまま #ifdef でコンパイルから除外、#include を変更(すべてライブラリまかせ)
{{{
RAND_bytes() -> libressl/crypto/rand/rand_lib.c RAND_bytes()
  arc4random_buf() -> libressl/crypto/compat/arc4random.c arc4random_buf()
    _rs_random_buf() -> libressl/crypto/compat/arc4random.c _rs_random_buf()
      _rs_stir_if_needed() -> libressl/crypto/compat/arc4random.c _rs_stir_if_needed()
        _rs_stir() -> libressl/crypto/compat/arc4random.c _rs_stir()
          getentropy() -> libressl/crypto/compat/getentropy_win.c getentropy()
          _rs_init() -> libressl/crypto/compat/arc4random.c _rs_init()
            chacha_keysetup() -> libressl/crypto/compat/chacha_private.h chacha_keysetup()
            chacha_ivsetup() -> libressl/crypto/compat/chacha_private.h chacha_ivsetup()
}}}
{{{
randombytes() -> ttxssh/ed25519_crypto_api.h arc4random_buf()
  arc4random_buf() -> libressl/crypto/compat/arc4random.c _rs_random_buf()
    以下同じ
}}}
{{{
arc4random() -> libressl/crypto/compat/arc4random.c arc4random()
  _rs_random_u32() -> libressl/crypto/compat/arc4random.c _rs_random_u32()
    _rs_stir_if_needed() -> libressl/crypto/compat/arc4random.c _rs_stir_if_needed()
      以下同じ
}}}

== OpenSSL
arc4random.c/h あり
{{{
RAND_bytes -> openssl/crypto/rand/rand_lib.c RAND_bytes()
  RAND_bytes_ex() -> openssl/crypto/rand/rand_lib.c RAND_bytes_ex()
    EVP_RAND_generate() -> openssl/crypto/evp/evp_rand.c EVP_RAND_generate()
}}}
{{{
arc4random_buf() -> ttxssh/arc4random.c arc4random_buf()
  _rs_random_buf() -> ttxssh/arc4random.c _rs_random_buf()
    _rs_stir_if_needed() -> ttxssh/arc4random.c _rs_stir_if_needed()
      _rs_stir() -> ttxssh/arc4random.c _rs_stir()
        RAND_bytes() -> openssl/crypto/rand/rand_lib.c RAND_bytes()
          以下同じ
}}}
{{{
arc4random() -> ttxssh/arc4random.c arc4random()
  _rs_random_u32()  -> ttxssh/arc4random.c _rs_random_u32()
    _rs_stir_if_needed() -> ttxssh/arc4random.c _rs_stir_if_needed()
      以下同じ
}}}

= libressl
{{{
libressl\include\openssl\rand.h(93): int  RAND_bytes(unsigned char *buf, int num);
libressl\crypto\rand\rand_lib.c(87): RAND_bytes(unsigned char *buf, int num)
libressl\include\compat\unistd.h(57): int getentropy(void *buf, size_t buflen);
libressl\crypto\compat\getentropy_win.c(29): int	getentropy(void *buf, size_t len);
libressl\include\compat\stdlib.h(23): uint32_t arc4random(void);
libressl\include\compat\stdlib.h(24): void arc4random_buf(void *_buf, size_t n);
libressl\crypto\compat\arc4random.c(86): _rs_stir(void)
libressl\crypto\compat\arc4random.c(180): arc4random(void)
libressl\crypto\compat\arc4random.c(191): arc4random_buf(void *buf, size_t n)
libressl\include\compat\chacha_private.h(55): chacha_keysetup(chacha_ctx *x,const u8 *k,u32 kbits,u32 ivbits)
libressl\include\compat\chacha_private.h(80): chacha_ivsetup(chacha_ctx *x,const u8 *iv)
libressl\include\compat\chacha_private.h(89): chacha_encrypt_bytes(chacha_ctx *x,const u8 *m,u8 *c,u32 bytes)
}}}