-- raw types for the communication protocol -- these are used throughout at the lowest level of the protocol -- essentially they are the units of packets and of messages -- SMG.Comms has only 2 types of packets: RSA and Serpent -- a message is the decrypted content of a packet -- S.MG, 2018 with Interfaces; use Interfaces; -- Unsigned_n and Integer_n with Ada.Unchecked_Conversion; package Raw_Types is -- no side effects or internal state Pragma Pure(Raw_Types); -- raw, low-level type -- all messages and packets are simply arrays of octets at low level/raw type Octets is array( Natural range <> ) of Interfaces.Unsigned_8; -- length of a text field (i.e. 16 bits, strictly > 0) subtype Text_Len is Positive range 1..2**16-1; -- "text" type has a 2-byte header with total length -- Len here is length of actual content ONLY (i.e. it needs + 2 for total) type Text_Octets( Len: Text_Len := 1 ) is record -- actual octets making up the "text" Content: Octets( 1..Len ) := (others => 0); end record; -- constants from SMG.COMMS standard specification -- RSA key size in octets -- NB: this should MATCH the size of RSA key -- as defined in the rsa implementation in C! -- NOT imported here because: -- a. it's C code that should import this, not the other way around. -- b. it needs to be static here. RSA_KEY_OCTETS : constant Positive := 490; -- RSA public exponent (e) size in octets -- NB: this should normally match the E_LENGTH_OCTETS in smg_rsa.h -- NOT imported here for the same reason given at RSA_KEY_OCTETS above E_LENGTH_OCTETS : constant Positive := 8; -- OAEP constants: defined here as still part of standard spec. -- OAEP package will use those values when using Raw_Types -- (NOT the other way around since dependency is oaep on types.) OAEP_R_STR : constant String := "TMSR-RSA"; -- "TMSR-RSA" as unsigned_8 values: OAEP_RESERVED : constant Octets := (84,77,83,82,45,82,83,65); -- max length of useful payload in a OAEP-encrypted entity OAEP_MAX_LEN : constant Natural := RSA_KEY_OCTETS / 2 - OAEP_R_STR'Length - 3; -- size of a RSA-encrypted packet and message in octets and bits RSA_PKT_OCTETS : constant Positive := 1470; RSA_MSG_OCTETS : constant Positive := RSA_PKT_OCTETS / RSA_KEY_OCTETS * OAEP_MAX_LEN; RSA_MSG_BITS : constant Positive := RSA_MSG_OCTETS * 8; -- size of a serpent-encrypted packet and message, in octets -- note that this corresponds to 1472/16 = 92 Serpent blocks -- NB: lengths are the same! SERPENT_OCTETS : constant Positive := 1472; -- raw representations of basic types (with fixed, well-defined sizes) subtype Octets_1 is Octets( 1 .. 1 ); subtype Octets_2 is Octets( 1 .. 2 ); subtype Octets_4 is Octets( 1 .. 4 ); subtype Octets_8 is Octets( 1 .. 8 ); -- raw representations of RSA key components subtype RSA_len is Octets ( 1 .. RSA_KEY_OCTETS); subtype RSA_half is Octets( 1 .. RSA_KEY_OCTETS/2); subtype RSA_e is Octets( 1 .. E_LENGTH_OCTETS); -- RSA packets and contained raw messages subtype RSA_Pkt is Octets( 1 .. RSA_PKT_OCTETS ); subtype RSA_Msg is Octets( 1 .. RSA_MSG_OCTETS ); -- Serpent packets and contained raw messages -- NB: length is the same but the distinction makes the code clearer subtype Serpent_Pkt is Octets( 1 .. SERPENT_OCTETS ); subtype Serpent_Msg is Octets( 1 .. SERPENT_OCTETS ); -- blind, unchecked casts ( memcpy style ) function Cast is new Ada.Unchecked_Conversion( Integer_8 , Octets_1 ); function Cast is new Ada.Unchecked_Conversion( Octets_1 , Integer_8 ); function Cast is new Ada.Unchecked_Conversion( Unsigned_8 , Octets_1 ); function Cast is new Ada.Unchecked_Conversion( Octets_1 , Unsigned_8 ); function Cast is new Ada.Unchecked_Conversion( Integer_16 , Octets_2 ); function Cast is new Ada.Unchecked_Conversion( Octets_2 , Integer_16 ); function Cast is new Ada.Unchecked_Conversion( Unsigned_16, Octets_2 ); function Cast is new Ada.Unchecked_Conversion( Octets_2 , Unsigned_16 ); function Cast is new Ada.Unchecked_Conversion( Integer_32 , Octets_4 ); function Cast is new Ada.Unchecked_Conversion( Octets_4 , Integer_32 ); function Cast is new Ada.Unchecked_Conversion( Unsigned_32, Octets_4 ); function Cast is new Ada.Unchecked_Conversion( Octets_4 , Unsigned_32 ); -- Gnat's Float has 32 bits but this might be different with other compilers function Cast is new Ada.Unchecked_Conversion( Float, Octets_4 ); function Cast is new Ada.Unchecked_Conversion( Octets_4, Float ); function Cast is new Ada.Unchecked_Conversion( Integer_64, Octets_8 ); function Cast is new Ada.Unchecked_Conversion( Octets_8, Integer_64 ); function Cast is new Ada.Unchecked_Conversion( Unsigned_64, Octets_8 ); function Cast is new Ada.Unchecked_Conversion( Octets_8, Unsigned_64 ); end Raw_Types;