raw
smg_comms_raw_types     1  -- raw types for the communication protocol
smg_comms_raw_types 2 -- these are used throughout at the lowest level of the protocol
smg_comms_raw_types 3 -- essentially they are the units of packets and of messages
smg_comms_raw_types 4 -- SMG.Comms has only 2 types of packets: RSA and Serpent
smg_comms_raw_types 5 -- a message is the decrypted content of a packet
smg_comms_raw_types 6 -- S.MG, 2018
smg_comms_raw_types 7
smg_comms_raw_types 8 with Interfaces; use Interfaces; -- Unsigned_n and Integer_n
smg_comms_raw_types 9 with Ada.Unchecked_Conversion;
smg_comms_raw_types 10
smg_comms_raw_types 11 package Raw_Types is
smg_comms_packing... 12 -- no side effects or internal state
smg_comms_packing... 13 Pragma Pure(Raw_Types);
smg_comms_raw_types 14
smg_comms_packing... 15 -- raw, low-level type
smg_comms_packing... 16 -- all messages and packets are simply arrays of octets at low level/raw
smg_comms_packing... 17 type Octets is array( Natural range <> ) of Interfaces.Unsigned_8;
smg_comms_packing... 18
smg_comms_actions... 19 -- length of a text field (i.e. 16 bits, strictly > 0)
smg_comms_actions... 20 subtype Text_Len is Positive range 1..2**16-1;
smg_comms_actions... 21
smg_comms_actions... 22 -- "text" type has a 2-byte header with total length
smg_comms_actions... 23 -- Len here is length of actual content ONLY (i.e. it needs + 2 for total)
smg_comms_actions... 24 type Text_Octets( Len: Text_Len := 1 ) is
smg_comms_actions... 25 record
smg_comms_actions... 26 -- actual octets making up the "text"
smg_comms_actions... 27 Content: Octets( 1..Len ) := (others => 0);
smg_comms_actions... 28 end record;
smg_comms_actions... 29
smg_comms_raw_types 30 -- constants from SMG.COMMS standard specification
smg_comms_rsa_oaep 31
smg_comms_rsa_oaep 32 -- RSA key size in octets
smg_comms_rsa_oaep 33 -- NB: this should MATCH the size of RSA key
smg_comms_rsa_oaep 34 -- as defined in the rsa implementation in C!
smg_comms_rsa_oaep 35 -- NOT imported here because:
smg_comms_rsa_oaep 36 -- a. it's C code that should import this, not the other way around.
smg_comms_rsa_oaep 37 -- b. it needs to be static here.
smg_comms_rsa_oaep 38 RSA_KEY_OCTETS : constant Positive := 490;
smg_comms_shorter_e 39
smg_comms_shorter_e 40 -- RSA public exponent (e) size in octets
smg_comms_shorter_e 41 -- NB: this should normally match the E_LENGTH_OCTETS in smg_rsa.h
smg_comms_shorter_e 42 -- NOT imported here for the same reason given at RSA_KEY_OCTETS above
smg_comms_shorter_e 43 E_LENGTH_OCTETS : constant Positive := 8;
smg_comms_packing... 44
smg_comms_packing... 45 -- OAEP constants: defined here as still part of standard spec.
smg_comms_packing... 46 -- OAEP package will use those values when using Raw_Types
smg_comms_packing... 47 -- (NOT the other way around since dependency is oaep on types.)
smg_comms_packing... 48 OAEP_R_STR : constant String := "TMSR-RSA";
smg_comms_packing... 49 -- "TMSR-RSA" as unsigned_8 values:
smg_comms_packing... 50 OAEP_RESERVED : constant Octets := (84,77,83,82,45,82,83,65);
smg_comms_packing... 51 -- max length of useful payload in a OAEP-encrypted entity
smg_comms_packing... 52 OAEP_MAX_LEN : constant Natural := RSA_KEY_OCTETS / 2 -
smg_comms_packing... 53 OAEP_R_STR'Length - 3;
smg_comms_rsa_oaep 54
smg_comms_packing... 55 -- size of a RSA-encrypted packet and message in octets and bits
smg_comms_packing... 56 RSA_PKT_OCTETS : constant Positive := 1470;
smg_comms_packing... 57 RSA_MSG_OCTETS : constant Positive := RSA_PKT_OCTETS /
smg_comms_packing... 58 RSA_KEY_OCTETS *
smg_comms_packing... 59 OAEP_MAX_LEN;
smg_comms_packing... 60 RSA_MSG_BITS : constant Positive := RSA_MSG_OCTETS * 8;
smg_comms_rsa_oaep 61
smg_comms_raw_types 62 -- size of a serpent-encrypted packet and message, in octets
smg_comms_raw_types 63 -- note that this corresponds to 1472/16 = 92 Serpent blocks
smg_comms_packing... 64 -- NB: lengths are the same!
smg_comms_packing... 65 SERPENT_OCTETS : constant Positive := 1472;
smg_comms_raw_types 66
smg_comms_raw_types 67 -- raw representations of basic types (with fixed, well-defined sizes)
smg_comms_raw_types 68 subtype Octets_1 is Octets( 1 .. 1 );
smg_comms_raw_types 69 subtype Octets_2 is Octets( 1 .. 2 );
smg_comms_raw_types 70 subtype Octets_4 is Octets( 1 .. 4 );
smg_comms_raw_types 71 subtype Octets_8 is Octets( 1 .. 8 );
smg_comms_raw_types 72
smg_comms_rsa_oaep 73 -- raw representations of RSA key components
smg_comms_rsa_oaep 74 subtype RSA_len is Octets ( 1 .. RSA_KEY_OCTETS);
smg_comms_rsa_oaep 75 subtype RSA_half is Octets( 1 .. RSA_KEY_OCTETS/2);
smg_comms_shorter_e 76 subtype RSA_e is Octets( 1 .. E_LENGTH_OCTETS);
smg_comms_rsa_oaep 77
smg_comms_raw_types 78 -- RSA packets and contained raw messages
smg_comms_raw_types 79 subtype RSA_Pkt is Octets( 1 .. RSA_PKT_OCTETS );
smg_comms_raw_types 80 subtype RSA_Msg is Octets( 1 .. RSA_MSG_OCTETS );
smg_comms_raw_types 81
smg_comms_raw_types 82 -- Serpent packets and contained raw messages
smg_comms_raw_types 83 -- NB: length is the same but the distinction makes the code clearer
smg_comms_packing... 84 subtype Serpent_Pkt is Octets( 1 .. SERPENT_OCTETS );
smg_comms_packing... 85 subtype Serpent_Msg is Octets( 1 .. SERPENT_OCTETS );
smg_comms_raw_types 86
smg_comms_raw_types 87 -- blind, unchecked casts ( memcpy style )
smg_comms_raw_types 88 function Cast is new Ada.Unchecked_Conversion( Integer_8 , Octets_1 );
smg_comms_raw_types 89 function Cast is new Ada.Unchecked_Conversion( Octets_1 , Integer_8 );
smg_comms_raw_types 90 function Cast is new Ada.Unchecked_Conversion( Unsigned_8 , Octets_1 );
smg_comms_raw_types 91 function Cast is new Ada.Unchecked_Conversion( Octets_1 , Unsigned_8 );
smg_comms_raw_types 92
smg_comms_raw_types 93 function Cast is new Ada.Unchecked_Conversion( Integer_16 , Octets_2 );
smg_comms_raw_types 94 function Cast is new Ada.Unchecked_Conversion( Octets_2 , Integer_16 );
smg_comms_raw_types 95 function Cast is new Ada.Unchecked_Conversion( Unsigned_16, Octets_2 );
smg_comms_raw_types 96 function Cast is new Ada.Unchecked_Conversion( Octets_2 , Unsigned_16 );
smg_comms_raw_types 97
smg_comms_raw_types 98 function Cast is new Ada.Unchecked_Conversion( Integer_32 , Octets_4 );
smg_comms_raw_types 99 function Cast is new Ada.Unchecked_Conversion( Octets_4 , Integer_32 );
smg_comms_raw_types 100 function Cast is new Ada.Unchecked_Conversion( Unsigned_32, Octets_4 );
smg_comms_raw_types 101 function Cast is new Ada.Unchecked_Conversion( Octets_4 , Unsigned_32 );
smg_comms_raw_types 102
smg_comms_raw_types 103 -- Gnat's Float has 32 bits but this might be different with other compilers
smg_comms_raw_types 104 function Cast is new Ada.Unchecked_Conversion( Float, Octets_4 );
smg_comms_raw_types 105 function Cast is new Ada.Unchecked_Conversion( Octets_4, Float );
smg_comms_raw_types 106
smg_comms_raw_types 107 function Cast is new Ada.Unchecked_Conversion( Integer_64, Octets_8 );
smg_comms_raw_types 108 function Cast is new Ada.Unchecked_Conversion( Octets_8, Integer_64 );
smg_comms_raw_types 109 function Cast is new Ada.Unchecked_Conversion( Unsigned_64, Octets_8 );
smg_comms_raw_types 110 function Cast is new Ada.Unchecked_Conversion( Octets_8, Unsigned_64 );
smg_comms_raw_types 111
smg_comms_raw_types 112 end Raw_Types;