summaryrefslogtreecommitdiff
path: root/src/crypto-types-random_source-file.adb
diff options
context:
space:
mode:
Diffstat (limited to 'src/crypto-types-random_source-file.adb')
-rw-r--r--src/crypto-types-random_source-file.adb144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/crypto-types-random_source-file.adb b/src/crypto-types-random_source-file.adb
new file mode 100644
index 0000000..e665990
--- /dev/null
+++ b/src/crypto-types-random_source-file.adb
@@ -0,0 +1,144 @@
+package body Crypto.Types.Random_Source.File is
+ use Ada.Strings.Unbounded;
+ use Ada.Streams.Stream_IO;
+
+ ---------------------------------------------------------------------------
+ ------------------------ Initialization -----------------------------------
+ ---------------------------------------------------------------------------
+
+
+ procedure Initialize(This : in out Random_Source_File) is
+ Path : constant String := "/dev/random";
+ Mode : constant File_Mode := In_File;
+ begin
+ if This.Source_File = null then
+ This.Source_File := new Ada.Streams.Stream_IO.File_Type;
+ end if;
+ if not Is_Open(This.Source_File.all) then
+ Open(This.Source_File.all, Mode, Path, "shared=yes");
+ This.Source_Path := To_Unbounded_String(Path);
+ end if;
+ end Initialize;
+
+ ---------------------------------------------------------------------------
+
+ procedure Initialize(This : in out Random_Source_File;
+ File_Path : in String) is
+ Mode : constant File_Mode := In_File;
+ begin
+ if Is_Open(This.Source_File.all) then
+ Close(This.Source_File.all);
+ end if;
+ if not Is_Open(This.Source_File.all) then
+ Open(This.Source_File.all, Mode, File_Path, "shared=yes");
+ This.Source_Path := To_Unbounded_String(File_Path);
+ end if;
+ end Initialize;
+
+ ---------------------------------------------------------------------------
+ ------------------------------- Read Byte ---------------------------------
+ ---------------------------------------------------------------------------
+
+
+ procedure Read(This : in out Random_Source_File; B : out Byte) is
+
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ Byte'Read(Stream(This.Source_File.all), B);
+ end if;
+ end Read;
+
+ ---------------------------------------------------------------------------
+
+ procedure Read(This : in out Random_Source_File; Byte_Array : out Bytes) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ Bytes'Read(Stream(This.Source_File.all), Byte_Array);
+ end if;
+ end Read;
+
+ ---------------------------------------------------------------------------
+
+ procedure Read(This : in out Random_Source_File; B : out B_Block128) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ B_Block128'Read(Stream(This.Source_File.all), B);
+ end if;
+ end Read;
+
+ ---------------------------------------------------------------------------
+ ------------------------------- Read Word ---------------------------------
+ ---------------------------------------------------------------------------
+
+ procedure Read(This : in out Random_Source_File; W : out Word) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ Word'Read(Stream(This.Source_File.all), W);
+ end if;
+ end Read;
+
+ ---------------------------------------------------------------------------
+
+ procedure Read(This : in out Random_Source_File; Word_Array : out Words) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ Words'Read(Stream(This.Source_File.all), Word_Array);
+ end if;
+ end Read;
+
+ ---------------------------------------------------------------------------
+ ------------------------------- Read DWord --------------------------------
+ ---------------------------------------------------------------------------
+
+
+ procedure Read(This : in out Random_Source_File; D : out DWord) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ DWord'Read(Stream(This.Source_File.all), D);
+ end if;
+ end Read;
+
+ procedure Read(This : in out Random_Source_File; DWord_Array : out DWords) is
+ begin
+ if not Path_Starts_With(This, "/dev/") and then End_Of_File(This.Source_File.all) then
+ raise RANDOM_SOURCE_READ_ERROR with To_String(This.Source_Path);
+ else
+ DWords'Read(Stream(This.Source_File.all), DWord_Array);
+ end if;
+ end Read;
+
+
+ ---------------------------------------------------------------------------
+ ------------------------------- Finalize ----------------------------------
+ ---------------------------------------------------------------------------
+
+ procedure Finalize(This : in out Random_Source_File) is
+ begin
+ if Is_Open(This.Source_File.all) then
+ Close(This.Source_File.all);
+ end if;
+ end Finalize;
+
+ ---------------------------------------------------------------------------
+ --------------------------- Path_Starts_With ------------------------------
+ ---------------------------------------------------------------------------
+
+ function Path_Starts_With(This : Random_Source_File; S : String) return Boolean is
+ Path : constant String := To_String(This.Source_Path);
+ begin
+ return Path(Path'First..S'Last) = S;
+ end;
+
+end Crypto.Types.Random_Source.File;