RNG.pm 5.97 KB
Newer Older
1 2 3
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
#
5 6
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
7 8

package Bugzilla::RNG;
9 10

use 5.10.1;
11
use strict;
12
use warnings;
13

14
use parent qw(Exporter);
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
use Bugzilla::Constants qw(ON_WINDOWS);

use Math::Random::ISAAC;
use if ON_WINDOWS, 'Win32::API';

our $RNG;
our @EXPORT_OK = qw(rand srand irand);

# ISAAC, a 32-bit generator, should only be capable of generating numbers
# between 0 and 2^32 - 1. We want _to_float to generate numbers possibly
# including 0, but always less than 1.0. Dividing the integer produced
# by irand() by this number should do that exactly.
use constant DIVIDE_BY => 2**32;

# How many bytes of seed to read.
30
use constant SEED_SIZE => 16;    # 128 bits.
31 32 33 34 35 36 37 38 39 40 41 42 43 44

#################
# Windows Stuff #
#################

# The type of cryptographic service provider we want to use.
# This doesn't really matter for our purposes, so we just pick
# PROV_RSA_FULL, which seems reasonable. For more info, see
# http://msdn.microsoft.com/en-us/library/aa380244(v=VS.85).aspx
use constant PROV_RSA_FULL => 1;

# Flags for CryptGenRandom:
# Don't ever display a UI to the user, just fail if one would be needed.
use constant CRYPT_SILENT => 64;
45

46 47 48
# Don't require existing public/private keypairs.
use constant CRYPT_VERIFYCONTEXT => 0xF0000000;

49
# For some reason, BOOLEAN doesn't work properly as a return type with
50 51 52 53 54 55 56 57 58 59 60 61 62
# Win32::API.
use constant RTLGENRANDOM_PROTO => <<END;
INT SystemFunction036(
  PVOID RandomBuffer,
  ULONG RandomBufferLength
)
END

#################
# RNG Functions #
#################

sub rand (;$) {
63 64 65
  my ($limit) = @_;
  my $int = irand();
  return _to_float($int, $limit);
66 67 68
}

sub irand (;$) {
69 70 71 72 73 74 75 76 77 78 79 80
  my ($limit) = @_;
  Bugzilla::RNG::srand() if !defined $RNG;
  my $int = $RNG->irand();
  if (defined $limit) {

    # We can't just use the mod operator because it will bias
    # our output. Search for "modulo bias" on the Internet for
    # details. This is slower than mod(), but does not have a bias,
    # as demonstrated by Math::Random::Secure's uniform.t test.
    return int(_to_float($int, $limit));
  }
  return $int;
81 82 83
}

sub srand (;$) {
84 85 86 87 88 89 90 91 92
  my ($value) = @_;

  # Remove any RNG that might already have been made.
  $RNG = undef;
  my %args;
  if (defined $value) {
    $args{seed} = $value;
  }
  $RNG = _create_rng(\%args);
93 94 95
}

sub _to_float {
96 97 98
  my ($integer, $limit) = @_;
  $limit ||= 1;
  return ($integer / DIVIDE_BY) * $limit;
99 100 101 102 103 104 105
}

##########################
# Seed and PRNG Creation #
##########################

sub _create_rng {
106
  my ($params) = @_;
107

108 109 110
  if (!defined $params->{seed}) {
    $params->{seed} = _get_seed();
  }
111

112
  _check_seed($params->{seed});
113

114
  my @seed_ints = unpack('L*', $params->{seed});
115

116
  my $rng = Math::Random::ISAAC->new(@seed_ints);
117

118 119 120 121 122
  # It's faster to skip the frontend interface of Math::Random::ISAAC
  # and just use the backend directly. However, in case the internal
  # code of Math::Random::ISAAC changes at some point, we do make sure
  # that the {backend} element actually exists first.
  return $rng->{backend} ? $rng->{backend} : $rng;
123 124 125
}

sub _check_seed {
126 127 128 129 130 131 132 133 134 135
  my ($seed) = @_;
  if (length($seed) < 8) {
    warn "Your seed is less than 8 bytes (64 bits). It could be" . " easy to crack";
  }

  # If it looks like we were seeded with a 32-bit integer, warn the
  # user that they are making a dangerous, easily-crackable mistake.
  elsif (length($seed) <= 10 and $seed =~ /^\d+$/) {
    warn "RNG seeded with a 32-bit integer, this is easy to crack";
  }
136 137 138
}

sub _get_seed {
139
  return _windows_seed() if ON_WINDOWS;
140

141 142 143
  if (-r '/dev/urandom') {
    return _read_seed_from('/dev/urandom');
  }
144

145
  return _read_seed_from('/dev/random');
146 147 148
}

sub _read_seed_from {
149 150 151 152 153 154 155 156 157 158
  my ($from) = @_;

  open(my $fh, '<', $from) or die "$from: $!";
  my $buffer;
  read($fh, $buffer, SEED_SIZE);
  if (length($buffer) < SEED_SIZE) {
    die "Could not read enough seed bytes from $from, got only " . length($buffer);
  }
  close $fh;
  return $buffer;
159 160 161
}

sub _windows_seed {
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  my ($major, $minor) = (Win32::GetOSVersion())[1, 2];
  if ($major < 5) {
    die "Bugzilla does not support versions of Windows before" . " Windows 2000";
  }

  # This means Windows 2000.
  if ($major == 5 and $minor == 0) {
    return _win2k_seed();
  }

  my $rtlgenrand = Win32::API->new('advapi32', RTLGENRANDOM_PROTO);
  if (!defined $rtlgenrand) {
    die "Could not import RtlGenRand: $^E";
  }
  my $buffer = chr(0) x SEED_SIZE;
  my $result = $rtlgenrand->Call($buffer, SEED_SIZE);
  if (!$result) {
    die "RtlGenRand failed: $^E";
  }
  return $buffer;
182 183 184
}

sub _win2k_seed {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  my $crypt_acquire
    = Win32::API->new("advapi32", 'CryptAcquireContext', 'PPPNN', 'I');
  if (!defined $crypt_acquire) {
    die "Could not import CryptAcquireContext: $^E";
  }

  my $crypt_release
    = Win32::API->new("advapi32", 'CryptReleaseContext', 'NN', 'I');
  if (!defined $crypt_release) {
    die "Could not import CryptReleaseContext: $^E";
  }

  my $crypt_gen_random
    = Win32::API->new("advapi32", 'CryptGenRandom', 'NNP', 'I');
  if (!defined $crypt_gen_random) {
    die "Could not import CryptGenRandom: $^E";
  }

  my $context        = chr(0) x Win32::API::Type->sizeof('PULONG');
  my $acquire_result = $crypt_acquire->Call($context, 0, 0, PROV_RSA_FULL,
    CRYPT_SILENT | CRYPT_VERIFYCONTEXT);
  if (!defined $acquire_result) {
    die "CryptAcquireContext failed: $^E";
  }

  my $pack_type = Win32::API::Type::packing('PULONG');
  $context = unpack($pack_type, $context);

  my $buffer      = chr(0) x SEED_SIZE;
  my $rand_result = $crypt_gen_random->Call($context, SEED_SIZE, $buffer);
  my $rand_error  = $^E;

  # We don't check this if it fails, we don't care.
  $crypt_release->Call($context, 0);
  if (!defined $rand_result) {
    die "CryptGenRandom failed: $rand_error";
  }
  return $buffer;
223 224 225
}

1;
226 227 228 229 230 231 232 233 234 235 236 237

=head1 B<Methods in need of POD>

=over

=item srand

=item rand

=item irand

=back