1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
------------------------------------------------------------------------------
-- Secure Sockets Layer --
-- --
-- Copyright (C) 2005-2012, AdaCore --
-- --
-- This library is free software; you can redistribute it and/or modify --
-- it under terms of the GNU General Public License as published by the --
-- Free Software Foundation; either version 3, or (at your option) any --
-- later version. This library is distributed in the hope that it will be --
-- useful, but WITHOUT ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are --
-- granted additional permissions described in the GCC Runtime Library --
-- Exception, version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- As a special exception, if other files instantiate generics from this --
-- unit, or you link this unit with other files to produce an executable, --
-- this unit does not by itself cause the resulting executable to be --
-- covered by the GNU General Public License. This exception does not --
-- however invalidate any other reasons why the executable file might be --
-- covered by the GNU Public License. --
------------------------------------------------------------------------------
with Ada.Numerics.Discrete_Random;
with Ada.Unchecked_Conversion;
with Interfaces.C.Strings;
with System;
with SSL.Thin;
package body SSL.Ada_Random is
use type System.Address;
package C renames Interfaces.C;
package CS renames C.Strings;
type Byte_Type is mod 256;
pragma Convention (C, Byte_Type);
type Byte_Array is array (Positive range <>) of Byte_Type;
pragma Pack (Byte_Array);
pragma Convention (C, Byte_Array);
subtype C_Byte_Array is Byte_Array (Positive'Range);
type Unsigned is mod 2**System.Word_Size;
subtype Bytes_4 is Byte_Array (1 .. Unsigned'Size / System.Storage_Unit);
package Unsigned_Random is new Ada.Numerics.Discrete_Random (Unsigned);
protected Generator is
procedure Reset (Initiator : in Integer);
procedure Random (Result : out Unsigned);
-- Use protected procedure to get random number, because
-- function Ada.Numerics.Discrete_Random.Random is modifying state of the
-- random numbers generator.
function Is_Initialized return Boolean;
private
Gen : Unsigned_Random.Generator;
Initialized : Boolean := False;
end Generator;
function To_Integer is new Ada.Unchecked_Conversion (Unsigned, Integer);
function To_Unsigned is new Ada.Unchecked_Conversion (Integer, Unsigned);
function To_Bytes_4 is new Ada.Unchecked_Conversion (Unsigned, Bytes_4);
function To_Unsigned is new Ada.Unchecked_Conversion (Bytes_4, Unsigned);
function Random return Unsigned;
pragma Inline (Random);
procedure Seed (Buf : in C_Byte_Array; Num : Integer);
pragma Convention (C, Seed);
function Bytes (Buf : access C_Byte_Array; Num : Integer) return Integer;
pragma Convention (C, Bytes);
procedure Cleanup;
pragma Convention (C, Cleanup);
procedure Add (Buf : in C_Byte_Array; Num : Integer; Entropy : Integer);
pragma Convention (C, Add);
function Status return Integer;
pragma Convention (C, Status);
Method : aliased Thin.Rand_Meth_St
:= (Seed => Seed'Address,
Bytes => Bytes'Address,
Cleanup => Cleanup'Address,
Add => Add'Address,
Pseudorand => Bytes'Address,
Status => Status'Address);
function To_Initiator (Buf : in Byte_Array) return Integer;
---------
-- Add --
---------
procedure Add (Buf : in C_Byte_Array; Num : Integer; Entropy : Integer) is
begin
Generator.Reset (To_Initiator (To_Bytes_4 (Random) & Buf (1 .. Num)));
end Add;
-----------
-- Bytes --
-----------
function Bytes (Buf : access C_Byte_Array; Num : Integer) return Integer is
B4 : Bytes_4;
Index : Positive := B4'Last + 1;
function Get_Next return Byte_Type;
--------------
-- Get_Next --
--------------
function Get_Next return Byte_Type is
begin
Index := Index + 1;
if Index > B4'Last then
Index := B4'First;
B4 := To_Bytes_4 (Random);
end if;
return B4 (Index);
end Get_Next;
begin
for J in 1 .. Num loop
Buf (J) := Get_Next;
end loop;
return 1;
end Bytes;
-------------
-- Cleanup --
-------------
procedure Cleanup is
begin
Generator.Reset (0);
end Cleanup;
---------------
-- Generator --
---------------
protected body Generator is
--------------------
-- Is_Initialized --
--------------------
function Is_Initialized return Boolean is
begin
return Initialized;
end Is_Initialized;
------------
-- Random --
------------
procedure Random (Result : out Unsigned) is
begin
Result := Unsigned_Random.Random (Gen);
end Random;
-----------
-- Reset --
-----------
procedure Reset (Initiator : in Integer) is
begin
Unsigned_Random.Reset (Gen, Initiator);
Initialized := True;
end Reset;
end Generator;
------------
-- Random --
------------
function Random return Unsigned is
Result : Unsigned;
begin
Generator.Random (Result);
return Result;
end Random;
----------------
-- Initialize --
----------------
procedure Initialize is
begin
Thin.RAND_set_rand_method (Method'Access);
end Initialize;
----------
-- Seed --
----------
procedure Seed (Buf : in C_Byte_Array; Num : Integer) is
begin
Generator.Reset (To_Initiator (Buf (1 .. Num)));
end Seed;
------------
-- Status --
------------
function Status return Integer is
begin
return Boolean'Pos (Generator.Is_Initialized);
end Status;
function To_Initiator (Buf : in Byte_Array) return Integer is
Init : Unsigned := 0;
begin
for J in Buf'Range loop
declare
function Next_Unsigned return Unsigned;
pragma Inline (Next_Unsigned);
-------------------
-- Next_Unsigned --
-------------------
function Next_Unsigned return Unsigned is
begin
case Buf'Last - J is
when 0 =>
return To_Unsigned ((Buf (J), Buf (Buf'First), 0, 0));
when 1 =>
return To_Unsigned
((Buf (J), Buf (J + 1), Buf (Buf'First), 0));
when 2 =>
return To_Unsigned
((Buf (J),
Buf (J + 1),
Buf (J + 2),
Buf (Buf'First)));
when others =>
return To_Unsigned (Buf (J .. J + 3));
end case;
end Next_Unsigned;
begin
Init := Init xor Next_Unsigned;
end;
end loop;
return To_Integer (Init);
end To_Initiator;
end SSL.Ada_Random;
|