pragmarc_20240810.0.0_fc017aa4/src/pragmarc-encryption-threefish.adb

 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
-- PragmAda Reusable Component (PragmARC)
-- Copyright (C) 2022 by PragmAda Software Engineering.  All rights reserved.
-- Released under the terms of the BSD 3-Clause license; see https://opensource.org/licenses
-- **************************************************************************
--
-- 2022 Feb 01     J. Carter     V1.2--Reorganization for 512- and 1024-bit versions
-- 2021 May 01     J. Carter     V1.1--Adhere to coding standard
-- 2021 Feb 01     J. Carter     V1.0--Initial PragmARC version
--
with Ada.Unchecked_Conversion;
with System;

package body PragmARC.Encryption.Threefish is
   use type System.Bit_Order;

   procedure Reverse_Bytes (List : in out Word_As_Bytes);
   -- Reverses the bytes of List

   function Word_From_Bytes (List : in Word_As_Bytes) return Word is
      function To_Word is new Ada.Unchecked_Conversion (Source => Word_As_Bytes, Target => Word);

      Local : Word_As_Bytes := List;
   begin -- Word_From_Bytes
      if System.Default_Bit_Order = System.High_Order_First then
         Reverse_Bytes (List => Local);
      end if;

      return To_Word (Local);
   end Word_From_Bytes;

   function Bytes_From_Word (Value : in Word) return Word_As_Bytes is
      function To_Bytes is new Ada.Unchecked_Conversion (Source => Word, Target => Word_As_Bytes);

      Result : Word_As_Bytes := To_Bytes (Value);
   begin -- Bytes_From_Word
      if System.Default_Bit_Order = System.High_Order_First then
         Reverse_Bytes (List => Result);
      end if;

      return Result;
   end Bytes_From_Word;

   procedure Reverse_Bytes (List : in out Word_As_Bytes) is
      procedure Swap (Left : in out Byte; Right : in out Byte);
      -- Swaps Left and Right

      procedure Swap (Left : in out Byte; Right : in out Byte) is
         Temp : constant Byte := Left;
      begin -- Swap
         Left := Right;
         Right := Temp;
      end Swap;

      Last : Natural := List'Last;
   begin -- Reverse_Bytes
      Swap_All : for I in List'First .. Last / 2 loop
         Swap (Left => List (I), Right => List (Last) );
         Last := Last - 1;
      end loop Swap_All;
   end Reverse_Bytes;
end PragmARC.Encryption.Threefish;