So molto poco di Ruby, quindi per favore perdonami se la risposta è ovvia. Ho notato a http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html che Ruby usa il pid e l'ora corrente per seminare OpenSSL :: Random quando viene effettuata una chiamata a random_bytes. A meno che non succeda qualcos'altro sotto le copertine, non è questo il seme che Netscape ha usato nella loro implementazione SSL iniziale a metà degli anni '90? http://en.wikipedia.org/wiki/Random_number_generator_attack#Prominent_examples_of_random_number_generator_security_issuesIl seme di Ruby per OpenSSL :: Random è sufficiente?
Sicuramente Ruby non ha rianimato un bug di 18 anni. Cosa mi manca qui?
Modifica: ecco la fonte per random_bytes. Notare il primo controllo per vedere se Ruby è stato compilato con OpenSSL, nel qual caso lo semina con il pid e l'ora corrente.
def self.random_bytes(n=nil)
n = n ? n.to_int : 16
if defined? OpenSSL::Random
@pid = 0 if !defined?(@pid)
pid = $$
if @pid != pid
now = Time.now
ary = [now.to_i, now.nsec, @pid, pid]
OpenSSL::Random.seed(ary.to_s)
@pid = pid
end
return OpenSSL::Random.random_bytes(n)
end
if !defined?(@has_urandom) || @has_urandom
flags = File::RDONLY
flags |= File::NONBLOCK if defined? File::NONBLOCK
flags |= File::NOCTTY if defined? File::NOCTTY
begin
File.open("/dev/urandom", flags) {|f|
unless f.stat.chardev?
raise Errno::ENOENT
end
@has_urandom = true
ret = f.readpartial(n)
if ret.length != n
raise NotImplementedError, "Unexpected partial read from random device: only #{ret.length} for #{n} bytes"
end
return ret
}
rescue Errno::ENOENT
@has_urandom = false
end
end
if !defined?(@has_win32)
begin
require 'Win32API'
crypt_acquire_context = Win32API.new("advapi32", "CryptAcquireContext", 'PPPII', 'L')
@crypt_gen_random = Win32API.new("advapi32", "CryptGenRandom", 'LIP', 'L')
hProvStr = " " * 4
prov_rsa_full = 1
crypt_verifycontext = 0xF0000000
if crypt_acquire_context.call(hProvStr, nil, nil, prov_rsa_full, crypt_verifycontext) == 0
raise SystemCallError, "CryptAcquireContext failed: #{lastWin32ErrorMessage}"
end
@hProv, = hProvStr.unpack('L')
@has_win32 = true
rescue LoadError
@has_win32 = false
end
end
if @has_win32
bytes = " ".force_encoding("ASCII-8BIT") * n
if @crypt_gen_random.call(@hProv, bytes.size, bytes) == 0
raise SystemCallError, "CryptGenRandom failed: #{lastWin32ErrorMessage}"
end
return bytes
end
raise NotImplementedError, "No random device"
end
Non è molto ben documentato, vero? Sarebbe educativo guardare più da vicino all'origine per vedere cosa fa OpenSSL con i valori forniti. Si suppone che utilizzi qualsiasi funzione casuale a livello di OS disponibile, come '/ dev/urandom' o'/dev/random 'invece di qualcosa del genere. – tadman
NOte che ho trovato alcune discussioni su come il biforcarsi possa causare vulnerabilità, il che non è di buon auspicio per il modo in cui Ruby chiama OpenSSL. –
Potrebbe indicare dove afferma che Ruby usa il pid e l'ora corrente come (solo) semi? –