-- Message reader & writers for SMG Communication Protocol -- This part effectively serializes/deserializes game data structures -- for transmission between client and server. -- Note that messages themeselves are simply arrays of octets. -- (see also raw_types.ads/adb and packing.ads/adb for related parts) -- NB: message ids and padding as per protocol spec are handled HERE ONLY. -- Relies on: -- RNG (for random padding) -- CRC32 (for CRC calculations) -- Raw_Types -- Data_Structs -- S.MG, 2018 with Raw_Types; with RNG; with CRC32; with Data_Structs; use Data_Structs; with Interfaces; package Messages is -- exception raised when given message to read fails sanity checks Invalid_Msg: exception; ---------------------- -- Serpent Messages -- ---------------------- -------------------------Keys---------------------------------------- -- Writes a Serpent Keyset to the given Serpent Message -- -- Keyset - the set of keys to write to message -- Counter - the message count procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset; Counter : in Interfaces.Unsigned_16; Msg : out Raw_Types.Serpent_Msg); -- Reads a Serpent Keyset from the given Serpent Message -- The opposite of Write_SKeys_SMsg above -- Raises Invalid_Message exception if given message fails sanity checks procedure Read_SKeys_SMsg( Msg : in Raw_Types.Serpent_Msg; Counter : out Interfaces.Unsigned_16; Keyset : out Serpent_Keyset); ------------------------Keys Management------------------------------ -- Writes a Key Management structure to the given Serpent Message -- -- KMgm - the keys management structure to write to message -- Counter - the message count procedure Write_KMgm_SMsg( KMgm : in Keys_Mgm; Counter : in Interfaces.Unsigned_16; Msg : out Raw_Types.Serpent_Msg); -- Reads a Key management structure from the given Serpent Message -- The opposite of Write_KMgm_SMsg above -- Raises Invalid_Message exception if given message fails sanity checks procedure Read_KMgm_SMsg( Msg : in Raw_Types.Serpent_Msg; Counter : out Interfaces.Unsigned_16; KMgm : out Keys_Mgm); ------------------ -- RSA Messages -- ------------------ -------------------------Keys---------------------------------------- -- Writes a Serpent Keyset to the given RSA Message -- -- Keyset - the set of keys to write to message -- Counter - the message count procedure Write_SKeys_RMsg( Keyset : in Serpent_Keyset; Counter : in Interfaces.Unsigned_16; Msg : out Raw_Types.RSA_Msg); -- Reads a Serpent Keyset from the given RSA Message -- The opposite of Write_SKeys_RMsg above -- Raises Invalid_Message exception if given message fails sanity checks procedure Read_SKeys_RMsg( Msg : in Raw_Types.RSA_Msg; Counter : out Interfaces.Unsigned_16; Keyset : out Serpent_Keyset); ------------------------Keys Management------------------------------ -- Writes a Key Management structure to the given RSA Message -- -- KMgm - the keys management structure to write to message -- Counter - the message count procedure Write_KMgm_RMsg( KMgm : in Keys_Mgm; Counter : in Interfaces.Unsigned_16; Msg : out Raw_Types.RSA_Msg); -- Reads a Key management structure from the given RSA Message -- The opposite of Write_KMgm_SMsg above -- Raises Invalid_Message exception if given message fails sanity checks procedure Read_KMgm_RMsg( Msg : in Raw_Types.RSA_Msg; Counter : out Interfaces.Unsigned_16; KMgm : out Keys_Mgm); private -- if native is little endian, does nothing; -- if native is big endian, it flips the input's octets. -- (strictly for arrays of octets) procedure Cast_LE( LE: in out Raw_Types.Octets ); -- protocol message types IDs, fixed as per protocol specification -- Serpent messages end in "S_Type" -- RSA messages end in "R_Type" -- Character action types end in "A_Type" -- Serpent message types SKeys_S_Type : constant Interfaces.Unsigned_8 := 1; Key_Mgm_S_Type : constant Interfaces.Unsigned_8 := 2; File_Transfer_S_Type : constant Interfaces.Unsigned_8 := 3; File_Req_S_Type : constant Interfaces.Unsigned_8 := 4; Client_Action_S_Type : constant Interfaces.Unsigned_8 := 5; World_Bulletin_S_Type: constant Interfaces.Unsigned_8 := 6; Obj_Request_S_Type : constant Interfaces.Unsigned_8 := 7; Obj_Info_S_Type : constant Interfaces.Unsigned_8 := 8; -- RSA message types RKeys_R_Type : constant Interfaces.Unsigned_8 := 251; SKeys_R_Type : constant Interfaces.Unsigned_8 := 157; Key_Mgm_R_Type : constant Interfaces.Unsigned_8 := 233; -- Character action types Lock_A_Type : constant Interfaces.Unsigned_8 := 0; Make_A_Type : constant Interfaces.Unsigned_8 := 1; Explore_A_Type : constant Interfaces.Unsigned_8 := 2; Exchange_A_Type : constant Interfaces.Unsigned_8 := 3; Attack_A_Type : constant Interfaces.Unsigned_8 := 4; Repair_A_Type : constant Interfaces.Unsigned_8 := 5; Move_A_Type : constant Interfaces.Unsigned_8 := 6; Train_A_Type : constant Interfaces.Unsigned_8 := 7; -- internal read/write of Serpent Keys -- those are called by both interface methods for RSA and Serpent messages -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID -- Msg will be padded with random octets until full length. procedure Write_SKeys( Keyset : in Serpent_Keyset; Counter : in Interfaces.Unsigned_16; Type_ID : in Interfaces.Unsigned_8; Msg : out Raw_Types.Octets); -- NB: caller has to ensure that Msg is a valid RSA or Serpent message procedure Read_SKeys( Msg : in Raw_Types.Octets; Counter : out Interfaces.Unsigned_16; Keyset : out Serpent_Keyset); -- internal read/write of Key Management structures -- those are called by both interface methods for RSA and Serpent messages -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID -- Msg will be padded with random octets until full length. procedure Write_KMgm( KMgm : in Keys_Mgm; Counter : in Interfaces.Unsigned_16; Type_ID : in Interfaces.Unsigned_8; Msg : out Raw_Types.Octets); -- NB: caller has to ensure that Msg is a valid RSA or Serpent message procedure Read_KMgm( Msg : in Raw_Types.Octets; Counter : out Interfaces.Unsigned_16; KMgm : out Keys_Mgm); end Messages;