mirror of https://github.com/aria2/aria2
Merge pull request #2091 from aria2/crypto-lib-rand
Prefer random number generator from crypto librariespull/2092/head
commit
8ae5ce5df3
|
@ -45,6 +45,14 @@
|
||||||
# include <Security/SecRandom.h>
|
# include <Security/SecRandom.h>
|
||||||
#endif // __APPLE__
|
#endif // __APPLE__
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBGNUTLS
|
||||||
|
# include <gnutls/crypto.h>
|
||||||
|
#endif // HAVE_LIBGNUTLS
|
||||||
|
|
||||||
|
#ifdef HAVE_OPENSSL
|
||||||
|
# include <openssl/rand.h>
|
||||||
|
#endif // HAVE_OPENSSL
|
||||||
|
|
||||||
#include "a2time.h"
|
#include "a2time.h"
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
#include "LogFactory.h"
|
#include "LogFactory.h"
|
||||||
|
@ -101,7 +109,19 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
auto rv = SecRandomCopyBytes(kSecRandomDefault, len, buf);
|
auto rv = SecRandomCopyBytes(kSecRandomDefault, len, buf);
|
||||||
assert(errSecSuccess == rv);
|
assert(errSecSuccess == rv);
|
||||||
#else // !__MINGW32__ && !__APPLE__
|
#elif defined(HAVE_LIBGNUTLS)
|
||||||
|
auto rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, len);
|
||||||
|
if (rv != 0) {
|
||||||
|
assert(0 == rv);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
#elif defined(HAVE_OPENSSL)
|
||||||
|
auto rv = RAND_bytes(buf, len);
|
||||||
|
if (rv != 1) {
|
||||||
|
assert(1 == rv);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
#else
|
||||||
constexpr static size_t blocklen = 256;
|
constexpr static size_t blocklen = 256;
|
||||||
auto iter = len / blocklen;
|
auto iter = len / blocklen;
|
||||||
auto p = buf;
|
auto p = buf;
|
||||||
|
@ -128,7 +148,7 @@ void SimpleRandomizer::getRandomBytes(unsigned char* buf, size_t len)
|
||||||
assert(0);
|
assert(0);
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
#endif // ! __MINGW32__
|
#endif // !__MINGW32__ && !__APPLE__ && !HAVE_OPENSSL && !HAVE_LIBGNUTLS
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
Loading…
Reference in New Issue