raw
smg_comms_skeys_s...    1  -- Message reader & writers for SMG Communication Protocol
smg_comms_skeys_s... 2 -- This part effectively serializes/deserializes game data structures
smg_comms_skeys_s... 3 -- for transmission between client and server.
smg_comms_skeys_s... 4 -- Note that messages themeselves are simply arrays of octets.
smg_comms_skeys_s... 5 -- (see also raw_types.ads/adb and packing.ads/adb for related parts)
smg_comms_skeys_s... 6 -- NB: message ids and padding as per protocol spec are handled HERE ONLY.
smg_comms_skeys_s... 7 -- Relies on:
smg_comms_skeys_s... 8 -- RNG (for random padding)
smg_comms_skeys_s... 9 -- CRC32 (for CRC calculations)
smg_comms_skeys_s... 10 -- Raw_Types
smg_comms_skeys_s... 11 -- Data_Structs
smg_comms_skeys_s... 12 -- S.MG, 2018
smg_comms_skeys_s... 13
smg_comms_skeys_s... 14 with Raw_Types;
smg_comms_skeys_s... 15 with RNG;
smg_comms_skeys_s... 16 with CRC32;
smg_comms_skeys_s... 17 with Data_Structs; use Data_Structs;
smg_comms_skeys_s... 18 with Interfaces;
smg_comms_skeys_s... 19
smg_comms_skeys_s... 20 package Messages is
smg_comms_skeys_s... 21 -- exception raised when given message to read fails sanity checks
smg_comms_skeys_s... 22 Invalid_Msg: exception;
smg_comms_skeys_s... 23
smg_comms_keymgm 24 ----------------------
smg_comms_keymgm 25 -- Serpent Messages --
smg_comms_keymgm 26 ----------------------
smg_comms_keymgm 27
smg_comms_keymgm 28 -------------------------Keys----------------------------------------
smg_comms_skeys_s... 29 -- Writes a Serpent Keyset to the given Serpent Message
smg_comms_skeys_s... 30 --
smg_comms_skeys_s... 31 -- Keyset - the set of keys to write to message
smg_comms_skeys_s... 32 -- Counter - the message count
smg_comms_skeys_s... 33 procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset;
smg_comms_skeys_s... 34 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 35 Pad : in Raw_Types.Octets_8;
smg_comms_skeys_s... 36 Msg : out Raw_Types.Serpent_Msg);
smg_comms_skeys_s... 37
smg_comms_skeys_s... 38 -- Reads a Serpent Keyset from the given Serpent Message
smg_comms_skeys_s... 39 -- The opposite of Write_SKeys_SMsg above
smg_comms_skeys_s... 40 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_skeys_s... 41 procedure Read_SKeys_SMsg( Msg : in Raw_Types.Serpent_Msg;
smg_comms_skeys_s... 42 Counter : out Interfaces.Unsigned_16;
smg_comms_skeys_s... 43 Keyset : out Serpent_Keyset);
smg_comms_skeys_s... 44
smg_comms_keymgm 45 ------------------------Keys Management------------------------------
smg_comms_keymgm 46 -- Writes a Key Management structure to the given Serpent Message
smg_comms_keymgm 47 --
smg_comms_keymgm 48 -- KMgm - the keys management structure to write to message
smg_comms_keymgm 49 -- Counter - the message count
smg_comms_keymgm 50 procedure Write_KMgm_SMsg( KMgm : in Keys_Mgm;
smg_comms_keymgm 51 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 52 Pad : in Raw_Types.Octets_8;
smg_comms_keymgm 53 Msg : out Raw_Types.Serpent_Msg);
smg_comms_keymgm 54
smg_comms_keymgm 55 -- Reads a Key management structure from the given Serpent Message
smg_comms_keymgm 56 -- The opposite of Write_KMgm_SMsg above
smg_comms_keymgm 57 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_keymgm 58 procedure Read_KMgm_SMsg( Msg : in Raw_Types.Serpent_Msg;
smg_comms_keymgm 59 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 60 KMgm : out Keys_Mgm);
smg_comms_keymgm 61
smg_comms_files 62 ----------------- File Transfer ----------------------
smg_comms_files 63
smg_comms_files 64 -- Writes the given File Chunk to a File Transfer type of message
smg_comms_files 65 -- Chunk - chunk of file to write; contains counter, filename, content
smg_comms_actions... 66 -- Pad - whether random padding (RNG_PAD value) or given value
smg_comms_files 67 procedure Write_File_Transfer( Chunk : in File_Chunk;
smg_comms_actions... 68 Pad : in Raw_Types.Octets_8;
smg_comms_files 69 Msg : out Raw_Types.Serpent_Msg);
smg_comms_files 70
smg_comms_files 71 -- The opposite of Write_File_Transfer method above.
smg_comms_files 72 -- Chunk will contain the counter, filename and content
smg_comms_files 73 procedure Read_File_Transfer( Msg : in Raw_Types.Serpent_Msg;
smg_comms_files 74 Chunk : out File_Chunk);
smg_comms_files 75
smg_comms_files 76 ----------------- File Request ----------------------
smg_comms_files 77 -- Writes a message to request the files specified through their names
smg_comms_files 78 -- Written parameter will hold the number of filenames actually written
smg_comms_files 79 -- NB: this can be less than the number of filenames provided!
smg_comms_files 80 -- When Written < FR.F_No, the FIRST Written filenames were written; the rest
smg_comms_files 81 -- did not fit into the message and it's up to caller to decide what to do.
smg_comms_files 82 procedure Write_File_Request( FR : in Filenames;
smg_comms_files 83 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 84 Pad : in Raw_Types.Octets_8;
smg_comms_files 85 Msg : out Raw_Types.Serpent_Msg;
smg_comms_files 86 Written : out Natural);
smg_comms_keymgm 87
smg_comms_files 88 -- Reads a request for files; the opposite of Write_File_Request above
smg_comms_files 89 -- Raises Invalid_Msg exception if the provided message fails checks.
smg_comms_files 90 procedure Read_File_Request( Msg : in Raw_Types.Serpent_Msg;
smg_comms_files 91 Counter : out Interfaces.Unsigned_16;
smg_comms_files 92 FR : out Filenames);
smg_comms_keymgm 93
smg_comms_actions... 94 -------------------------Client Actions------------------------------
smg_comms_actions... 95 -- writes the action (octets+length) into the specified Serpent message
smg_comms_actions... 96 procedure Write_Action( A : in Raw_Types.Text_Octets;
smg_comms_actions... 97 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 98 Pad : in Raw_Types.Octets_8;
smg_comms_actions... 99 Msg : out Raw_Types.Serpent_Msg);
smg_comms_actions... 100
smg_comms_actions... 101 -- reads a client action as octets+length from the given Serpent message
smg_comms_actions... 102 procedure Read_Action( Msg : in Raw_Types.Serpent_Msg;
smg_comms_actions... 103 Counter : out Interfaces.Unsigned_16;
smg_comms_actions... 104 A : out Raw_Types.Text_Octets);
smg_comms_actions... 105
smg_comms_keymgm 106 ------------------
smg_comms_keymgm 107 -- RSA Messages --
smg_comms_keymgm 108 ------------------
smg_comms_keymgm 109
smg_comms_keymgm 110 -------------------------Keys----------------------------------------
smg_comms_actions... 111
smg_comms_actions... 112 -- Writes a RSA Keyset (with 8 octets e) to the given RSA Message.
smg_comms_actions... 113 -- K - the Player_RSA structures with all needed information to write
smg_comms_actions... 114 -- Counter - the message count
smg_comms_actions... 115 procedure Write_RKeys_RMsg( K : in Player_RSA;
smg_comms_actions... 116 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 117 Pad : in Raw_Types.Octets_8;
smg_comms_actions... 118 Msg : out Raw_Types.RSA_Msg);
smg_comms_actions... 119
smg_comms_actions... 120 -- Reads a RSA Keyset (Player_RSA structures) from the given RSA Message.
smg_comms_actions... 121 -- Opposite of Write_RKeys_RMsg above
smg_comms_actions... 122 procedure Read_RKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
smg_comms_actions... 123 Counter : out Interfaces.Unsigned_16;
smg_comms_actions... 124 K : out Player_RSA);
smg_comms_actions... 125
smg_comms_keymgm 126 -- Writes a Serpent Keyset to the given RSA Message
smg_comms_keymgm 127 --
smg_comms_keymgm 128 -- Keyset - the set of keys to write to message
smg_comms_keymgm 129 -- Counter - the message count
smg_comms_keymgm 130 procedure Write_SKeys_RMsg( Keyset : in Serpent_Keyset;
smg_comms_keymgm 131 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 132 Pad : in Raw_Types.Octets_8;
smg_comms_keymgm 133 Msg : out Raw_Types.RSA_Msg);
smg_comms_keymgm 134
smg_comms_keymgm 135 -- Reads a Serpent Keyset from the given RSA Message
smg_comms_keymgm 136 -- The opposite of Write_SKeys_RMsg above
smg_comms_keymgm 137 -- Raises Invalid_Message exception if given message fails sanity checks
smg_comms_keymgm 138 procedure Read_SKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
smg_comms_keymgm 139 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 140 Keyset : out Serpent_Keyset);
smg_comms_keymgm 141
smg_comms_keymgm 142 ------------------------Keys Management------------------------------
smg_comms_keymgm 143 -- Writes a Key Management structure to the given RSA Message
smg_comms_keymgm 144 --
smg_comms_keymgm 145 -- KMgm - the keys management structure to write to message
smg_comms_keymgm 146 -- Counter - the message count
smg_comms_actions... 147 -- Pad - whether random padding (RNG_PAD) or not.
smg_comms_keymgm 148 procedure Write_KMgm_RMsg( KMgm : in Keys_Mgm;
smg_comms_keymgm 149 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 150 Pad : in Raw_Types.Octets_8;
smg_comms_keymgm 151 Msg : out Raw_Types.RSA_Msg);
smg_comms_keymgm 152
smg_comms_keymgm 153 -- Reads a Key management structure from the given RSA Message
smg_comms_keymgm 154 -- The opposite of Write_KMgm_SMsg above
smg_comms_files 155 -- Raises Invalid_Msg exception if given message fails sanity checks
smg_comms_keymgm 156 procedure Read_KMgm_RMsg( Msg : in Raw_Types.RSA_Msg;
smg_comms_keymgm 157 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 158 KMgm : out Keys_Mgm);
smg_comms_keymgm 159
smg_comms_keymgm 160
smg_comms_files 161 -- String to Octets conversion
smg_comms_files 162 -- NB: Str'Length has to be EQUAL to Octets'Length!
smg_comms_files 163 procedure String_To_Octets(Str: in String; O: out Raw_Types.Octets);
smg_comms_files 164
smg_comms_files 165 -- Octets to string conversion
smg_comms_files 166 -- NB: Str'Length has to be EQUAL to Octets'Length!
smg_comms_files 167 procedure Octets_To_String(O: in Raw_Types.Octets; Str: out String);
smg_comms_files 168
smg_comms_actions... 169 -- magic value used to request random padding
smg_comms_actions... 170 RNG_PAD: constant Raw_Types.Octets_8 := ( 16#00#, 16#00#, 16#00#, 16#00#,
smg_comms_actions... 171 16#13#, 16#37#, 16#00#, 16#00#);
smg_comms_actions... 172
smg_comms_skeys_s... 173 private
smg_comms_skeys_s... 174
smg_comms_skeys_s... 175 -- if native is little endian, does nothing;
smg_comms_skeys_s... 176 -- if native is big endian, it flips the input's octets.
smg_comms_skeys_s... 177 -- (strictly for arrays of octets)
smg_comms_skeys_s... 178 procedure Cast_LE( LE: in out Raw_Types.Octets );
smg_comms_skeys_s... 179
smg_comms_files 180 -- separator used for list of filenames
smg_comms_files 181 F_Sep: constant String := ";"; --as string
smg_comms_files 182 Sep: constant Interfaces.Unsigned_8 := 16#3B#; -- ascii code
smg_comms_files 183
smg_comms_skeys_s... 184 -- protocol message types IDs, fixed as per protocol specification
smg_comms_skeys_s... 185 -- Serpent messages end in "S_Type"
smg_comms_skeys_s... 186 -- RSA messages end in "R_Type"
smg_comms_skeys_s... 187 -- Character action types end in "A_Type"
smg_comms_skeys_s... 188
smg_comms_skeys_s... 189 -- Serpent message types
smg_comms_skeys_s... 190 SKeys_S_Type : constant Interfaces.Unsigned_8 := 1;
smg_comms_skeys_s... 191 Key_Mgm_S_Type : constant Interfaces.Unsigned_8 := 2;
smg_comms_skeys_s... 192 File_Transfer_S_Type : constant Interfaces.Unsigned_8 := 3;
smg_comms_skeys_s... 193 File_Req_S_Type : constant Interfaces.Unsigned_8 := 4;
smg_comms_skeys_s... 194 Client_Action_S_Type : constant Interfaces.Unsigned_8 := 5;
smg_comms_skeys_s... 195 World_Bulletin_S_Type: constant Interfaces.Unsigned_8 := 6;
smg_comms_skeys_s... 196 Obj_Request_S_Type : constant Interfaces.Unsigned_8 := 7;
smg_comms_skeys_s... 197 Obj_Info_S_Type : constant Interfaces.Unsigned_8 := 8;
smg_comms_skeys_s... 198
smg_comms_skeys_s... 199 -- RSA message types
smg_comms_skeys_s... 200 RKeys_R_Type : constant Interfaces.Unsigned_8 := 251;
smg_comms_skeys_s... 201 SKeys_R_Type : constant Interfaces.Unsigned_8 := 157;
smg_comms_skeys_s... 202 Key_Mgm_R_Type : constant Interfaces.Unsigned_8 := 233;
smg_comms_skeys_s... 203
smg_comms_skeys_s... 204 -- Character action types
smg_comms_skeys_s... 205 Lock_A_Type : constant Interfaces.Unsigned_8 := 0;
smg_comms_skeys_s... 206 Make_A_Type : constant Interfaces.Unsigned_8 := 1;
smg_comms_skeys_s... 207 Explore_A_Type : constant Interfaces.Unsigned_8 := 2;
smg_comms_skeys_s... 208 Exchange_A_Type : constant Interfaces.Unsigned_8 := 3;
smg_comms_skeys_s... 209 Attack_A_Type : constant Interfaces.Unsigned_8 := 4;
smg_comms_skeys_s... 210 Repair_A_Type : constant Interfaces.Unsigned_8 := 5;
smg_comms_skeys_s... 211 Move_A_Type : constant Interfaces.Unsigned_8 := 6;
smg_comms_skeys_s... 212 Train_A_Type : constant Interfaces.Unsigned_8 := 7;
smg_comms_skeys_s... 213
smg_comms_keymgm 214 -- internal read/write of Serpent Keys
smg_comms_keymgm 215 -- those are called by both interface methods for RSA and Serpent messages
smg_comms_keymgm 216
smg_comms_keymgm 217 -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID
smg_comms_keymgm 218 -- Msg will be padded with random octets until full length.
smg_comms_keymgm 219 procedure Write_SKeys( Keyset : in Serpent_Keyset;
smg_comms_keymgm 220 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 221 Type_ID : in Interfaces.Unsigned_8;
smg_comms_actions... 222 Pad : in Raw_Types.Octets_8;
smg_comms_keymgm 223 Msg : out Raw_Types.Octets);
smg_comms_keymgm 224
smg_comms_keymgm 225 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
smg_comms_keymgm 226 procedure Read_SKeys( Msg : in Raw_Types.Octets;
smg_comms_keymgm 227 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 228 Keyset : out Serpent_Keyset);
smg_comms_keymgm 229
smg_comms_keymgm 230
smg_comms_keymgm 231 -- internal read/write of Key Management structures
smg_comms_keymgm 232 -- those are called by both interface methods for RSA and Serpent messages
smg_comms_keymgm 233
smg_comms_keymgm 234 -- NB: caller HAS TO provide ENOUGH space in Msg AND valid Type_ID
smg_comms_keymgm 235 -- Msg will be padded with random octets until full length.
smg_comms_keymgm 236 procedure Write_KMgm( KMgm : in Keys_Mgm;
smg_comms_keymgm 237 Counter : in Interfaces.Unsigned_16;
smg_comms_keymgm 238 Type_ID : in Interfaces.Unsigned_8;
smg_comms_actions... 239 Pad : in Raw_Types.Octets_8;
smg_comms_keymgm 240 Msg : out Raw_Types.Octets);
smg_comms_keymgm 241
smg_comms_keymgm 242 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
smg_comms_keymgm 243 procedure Read_KMgm( Msg : in Raw_Types.Octets;
smg_comms_keymgm 244 Counter : out Interfaces.Unsigned_16;
smg_comms_keymgm 245 KMgm : out Keys_Mgm);
smg_comms_keymgm 246
smg_comms_files 247 -- helper methods to read/write a value on 16 bits (using Cast_LE as needed)
smg_comms_files 248
smg_comms_files 249 -- Writing a 16-bits value to the given Octets at specified position.
smg_comms_files 250 -- NB: Caller has to make sure Pos <= Msg'Last -1 ; NO check here!
smg_comms_files 251 -- Msg - the array of octets into which the value is to be written
smg_comms_files 252 -- Pos - the position in Msg at which to write the value;
smg_comms_files 253 -- NB: Pos will be increased by 2 after the write!
smg_comms_files 254 -- U16 - the value to write
smg_comms_files 255 procedure Write_U16( Msg: in out Raw_Types.Octets;
smg_comms_files 256 Pos: in out Natural;
smg_comms_files 257 U16: in Interfaces.Unsigned_16);
smg_comms_files 258
smg_comms_files 259 -- Reading a 16-bits value from the given Octets at specified position.
smg_comms_files 260 -- NB: Caller has to make sure Pos <= Msg'Last -1 ; NO check here!
smg_comms_files 261 -- Pos - the position in Msg from which to read the value;
smg_comms_files 262 -- NB: Pos will be increased by 2 after the read!
smg_comms_files 263 -- The opposite of Write_U16 above.
smg_comms_files 264 procedure Read_U16( Msg: in Raw_Types.Octets;
smg_comms_files 265 Pos: in out Natural;
smg_comms_files 266 U16: out Interfaces.Unsigned_16);
smg_comms_actions... 267
smg_comms_actions... 268 -- Writes the Counter + padding at the end of messages.
smg_comms_actions... 269 -- The padding is either random or repeated value, depending on "Padding".
smg_comms_actions... 270 -- NB: This raises Invalid_Msg exception if Pos > Msg'Last - 1
smg_comms_actions... 271 -- Msg - the array of octets into which the counter+padding are to be written
smg_comms_actions... 272 -- Pos - the position in Msg from which to start writing counter+padding
smg_comms_actions... 273 -- Counter - the message counter to write at Pos in Msg.
smg_comms_actions... 274 -- Padding - magic value 0x13370000 means random padding;
smg_comms_actions... 275 -- - any other value for Padding gets used as it is (repeated)
smg_comms_actions... 276 procedure Write_End( Msg : in out Raw_Types.Octets;
smg_comms_actions... 277 Pos : in out Natural;
smg_comms_actions... 278 Counter : in Interfaces.Unsigned_16;
smg_comms_actions... 279 Padding : in Raw_Types.Octets_8);
smg_comms_skeys_s... 280 end Messages;