diff -uNr a/smg_comms/manifest b/smg_comms/manifest --- a/smg_comms/manifest 7e1d9c77e461686ba11e96f1f50adfb9b9b9749714940766072489c1e3b9acde5a892c7422ce2eaf6712af95317dfa09984ce8ec57d1e69a9674f85650be7287 +++ b/smg_comms/manifest 32a691949af3fb21eaf17c73269d01a30cb9f940ee5662772eb2c5e67a8352aedc07b8a7cba6a6d7f7be1e42559e003b82bbeb97fb059ffb579bcc33a9ab2693 @@ -5,3 +5,4 @@ 547983 smg_comms_rsa_oaep diana_coman RSA with OAEP from ADA using the c_wrappers for RSA only. It includes reading from FG in Ada and repeat of OAEP until first octet is < first octet of key's modulus (i.e. without going through MPI for comparison). 548433 smg_comms_packing_rsa diana_coman Packing/Unpacking RSA messages <-> RSA packets of Eulora's communication protocol. 548894 smg_comms_80cols diana_coman Changes tests for RSA to read the key from a file in order to avoid lines > 80 columns in the code itself. +549511 smg_comms_skeys_smsgs diana_coman Defines data structures and message types as well as methods for reading/writing Serpent keysets to/from Serpent messages. diff -uNr a/smg_comms/src/crc32.adb b/smg_comms/src/crc32.adb --- a/smg_comms/src/crc32.adb false +++ b/smg_comms/src/crc32.adb 936d9785bc02f35a58ac415074d9a7daa857d24af3093baea9bbf082d71e83edb881efcd2fce95f774f600c6a99e8fe8f3c2c2ef7553c1e72556c31f6c7b7c3a @@ -0,0 +1,52 @@ +------------------------------------------------------------------------------ +------------------------------------------------------------------------------ +-- This file is part of 'CRC32' -- +-- -- +-- You do not have, nor can you ever acquire the right to use, copy or -- +-- distribute this software ; Should you use this software for any purpose, -- +-- or copy and distribute it to anyone or in any manner, you are breaking -- +-- the laws of whatever soi-disant jurisdiction, and you promise to -- +-- continue doing so for the indefinite future. In any case, please -- +-- always : read and understand any software ; verify any PGP signatures -- +-- that you use - for any purpose. -- +-- -- +-- See also http://trilema.com/2015/a-new-software-licensing-paradigm . -- +------------------------------------------------------------------------------ +------------------------------------------------------------------------------ + + -- CRC32 implementation + -- S.MG, 2018 + +package body CRC32 is + function CRC( S: in String ) return CRC32 is + Result : CRC32 := Init_Value; + Value : CRC32; + begin + -- process input character by character + for C of S loop + Value := CRC32( Character'Pos( C ) ); + Result := Shift_Right(Result, 8) xor + Lookup( Value xor (Result and LSB_MASK)); + end loop; + -- reflect result + Result := Result xor Xor_Out; + + return Result; + end CRC; + + function CRC( Data: in Octet_Array ) return CRC32 is + Result : CRC32 := Init_Value; + begin + -- process input octet by octet + for C of Data loop + Result := Shift_Right(Result, 8) xor + Lookup( CRC32(C) xor (Result and LSB_MASK)); + end loop; + -- reflect result + Result := Result xor Xor_Out; + + return Result; + end CRC; + +end CRC32; + diff -uNr a/smg_comms/src/crc32.ads b/smg_comms/src/crc32.ads --- a/smg_comms/src/crc32.ads false +++ b/smg_comms/src/crc32.ads 6b3f7a4b32ff15ecba5b7b580107cfd8b32083ee75bcd75191a394f9bd0e3e4369b0d4960d6693497047ee26fcd4442cd340a53d9ce10e9247b44da98737146c @@ -0,0 +1,131 @@ +------------------------------------------------------------------------------ +------------------------------------------------------------------------------ +-- This file is part of 'CRC32' -- +-- -- +-- You do not have, nor can you ever acquire the right to use, copy or -- +-- distribute this software ; Should you use this software for any purpose, -- +-- or copy and distribute it to anyone or in any manner, you are breaking -- +-- the laws of whatever soi-disant jurisdiction, and you promise to -- +-- continue doing so for the indefinite future. In any case, please -- +-- always : read and understand any software ; verify any PGP signatures -- +-- that you use - for any purpose. -- +-- -- +-- See also http://trilema.com/2015/a-new-software-licensing-paradigm . -- +------------------------------------------------------------------------------ +------------------------------------------------------------------------------ + + -- CRC32, lookup-based implementation + -- S.MG, 2018 + -- + -- The CRC32 is a checksum calculated as the remainder of dividing + -- the input data by the 0x04C11DB7 polynomial. Specifications: + -- Name : "CRC-32" + -- Width : 32 (number of bits) + -- Poly : 04C11DB7 (generator polynomial) + -- Init : FFFFFFFF + -- RefIn : True (input is reflected) + -- RefOut : True (output is reflected) + -- XorOut : FFFFFFFF + -- Check : CBF43926 (expected value for input "123456789") + -- + -- This implementation is based on the CRC32 specification in: + -- Sarwate, D.V. "Computation of Cyclic Redundancy Checks via Table Look-Up" + -- in Communications of the ACM, Vol. 31 No. 8, pp.1008-1013, Aug. 1988 + +with Interfaces; use Interfaces; +with Raw_Types; + +package CRC32 is + -- local, shorthand version of Interfaces. types + subtype CRC32 is Interfaces.Unsigned_32; + subtype Octet is Interfaces.Unsigned_8; + + subtype Octet_Array is Raw_Types.Octets; + + -- interface for external callers + -- calculate CRC32 for the given string + function CRC( S: in String ) return CRC32; + + -- calculate CRC32 for the given array of octets + function CRC( Data: in Octet_Array ) return CRC32; + + + -- internal constants and helper methods +private + function Shift_Right( Value : CRC32; + Amount : Natural) + return CRC32; + pragma Import(Intrinsic, Shift_Right); + + Init_Value : constant CRC32 := 16#FFFF_FFFF#; -- Initial value + Xor_Out : constant CRC32 := 16#FFFF_FFFF#; -- For extracting result + LSB_Mask : constant CRC32 := 16#0000_00FF#; -- lsb mask for a CRC32 value + + -- lookup table with precomputed values for CRC32 + Lookup : constant array (CRC32 range 0 .. 255) of CRC32 := + (16#0000_0000#, 16#7707_3096#, 16#EE0E_612C#, 16#9909_51BA#, + 16#076D_C419#, 16#706A_F48F#, 16#E963_A535#, 16#9E64_95A3#, + 16#0EDB_8832#, 16#79DC_B8A4#, 16#E0D5_E91E#, 16#97D2_D988#, + 16#09B6_4C2B#, 16#7EB1_7CBD#, 16#E7B8_2D07#, 16#90BF_1D91#, + 16#1DB7_1064#, 16#6AB0_20F2#, 16#F3B9_7148#, 16#84BE_41DE#, + 16#1ADA_D47D#, 16#6DDD_E4EB#, 16#F4D4_B551#, 16#83D3_85C7#, + 16#136C_9856#, 16#646B_A8C0#, 16#FD62_F97A#, 16#8A65_C9EC#, + 16#1401_5C4F#, 16#6306_6CD9#, 16#FA0F_3D63#, 16#8D08_0DF5#, + 16#3B6E_20C8#, 16#4C69_105E#, 16#D560_41E4#, 16#A267_7172#, + 16#3C03_E4D1#, 16#4B04_D447#, 16#D20D_85FD#, 16#A50A_B56B#, + 16#35B5_A8FA#, 16#42B2_986C#, 16#DBBB_C9D6#, 16#ACBC_F940#, + 16#32D8_6CE3#, 16#45DF_5C75#, 16#DCD6_0DCF#, 16#ABD1_3D59#, + 16#26D9_30AC#, 16#51DE_003A#, 16#C8D7_5180#, 16#BFD0_6116#, + 16#21B4_F4B5#, 16#56B3_C423#, 16#CFBA_9599#, 16#B8BD_A50F#, + 16#2802_B89E#, 16#5F05_8808#, 16#C60C_D9B2#, 16#B10B_E924#, + 16#2F6F_7C87#, 16#5868_4C11#, 16#C161_1DAB#, 16#B666_2D3D#, + 16#76DC_4190#, 16#01DB_7106#, 16#98D2_20BC#, 16#EFD5_102A#, + 16#71B1_8589#, 16#06B6_B51F#, 16#9FBF_E4A5#, 16#E8B8_D433#, + 16#7807_C9A2#, 16#0F00_F934#, 16#9609_A88E#, 16#E10E_9818#, + 16#7F6A_0DBB#, 16#086D_3D2D#, 16#9164_6C97#, 16#E663_5C01#, + 16#6B6B_51F4#, 16#1C6C_6162#, 16#8565_30D8#, 16#F262_004E#, + 16#6C06_95ED#, 16#1B01_A57B#, 16#8208_F4C1#, 16#F50F_C457#, + 16#65B0_D9C6#, 16#12B7_E950#, 16#8BBE_B8EA#, 16#FCB9_887C#, + 16#62DD_1DDF#, 16#15DA_2D49#, 16#8CD3_7CF3#, 16#FBD4_4C65#, + 16#4DB2_6158#, 16#3AB5_51CE#, 16#A3BC_0074#, 16#D4BB_30E2#, + 16#4ADF_A541#, 16#3DD8_95D7#, 16#A4D1_C46D#, 16#D3D6_F4FB#, + 16#4369_E96A#, 16#346E_D9FC#, 16#AD67_8846#, 16#DA60_B8D0#, + 16#4404_2D73#, 16#3303_1DE5#, 16#AA0A_4C5F#, 16#DD0D_7CC9#, + 16#5005_713C#, 16#2702_41AA#, 16#BE0B_1010#, 16#C90C_2086#, + 16#5768_B525#, 16#206F_85B3#, 16#B966_D409#, 16#CE61_E49F#, + 16#5EDE_F90E#, 16#29D9_C998#, 16#B0D0_9822#, 16#C7D7_A8B4#, + 16#59B3_3D17#, 16#2EB4_0D81#, 16#B7BD_5C3B#, 16#C0BA_6CAD#, + 16#EDB8_8320#, 16#9ABF_B3B6#, 16#03B6_E20C#, 16#74B1_D29A#, + 16#EAD5_4739#, 16#9DD2_77AF#, 16#04DB_2615#, 16#73DC_1683#, + 16#E363_0B12#, 16#9464_3B84#, 16#0D6D_6A3E#, 16#7A6A_5AA8#, + 16#E40E_CF0B#, 16#9309_FF9D#, 16#0A00_AE27#, 16#7D07_9EB1#, + 16#F00F_9344#, 16#8708_A3D2#, 16#1E01_F268#, 16#6906_C2FE#, + 16#F762_575D#, 16#8065_67CB#, 16#196C_3671#, 16#6E6B_06E7#, + 16#FED4_1B76#, 16#89D3_2BE0#, 16#10DA_7A5A#, 16#67DD_4ACC#, + 16#F9B9_DF6F#, 16#8EBE_EFF9#, 16#17B7_BE43#, 16#60B0_8ED5#, + 16#D6D6_A3E8#, 16#A1D1_937E#, 16#38D8_C2C4#, 16#4FDF_F252#, + 16#D1BB_67F1#, 16#A6BC_5767#, 16#3FB5_06DD#, 16#48B2_364B#, + 16#D80D_2BDA#, 16#AF0A_1B4C#, 16#3603_4AF6#, 16#4104_7A60#, + 16#DF60_EFC3#, 16#A867_DF55#, 16#316E_8EEF#, 16#4669_BE79#, + 16#CB61_B38C#, 16#BC66_831A#, 16#256F_D2A0#, 16#5268_E236#, + 16#CC0C_7795#, 16#BB0B_4703#, 16#2202_16B9#, 16#5505_262F#, + 16#C5BA_3BBE#, 16#B2BD_0B28#, 16#2BB4_5A92#, 16#5CB3_6A04#, + 16#C2D7_FFA7#, 16#B5D0_CF31#, 16#2CD9_9E8B#, 16#5BDE_AE1D#, + 16#9B64_C2B0#, 16#EC63_F226#, 16#756A_A39C#, 16#026D_930A#, + 16#9C09_06A9#, 16#EB0E_363F#, 16#7207_6785#, 16#0500_5713#, + 16#95BF_4A82#, 16#E2B8_7A14#, 16#7BB1_2BAE#, 16#0CB6_1B38#, + 16#92D2_8E9B#, 16#E5D5_BE0D#, 16#7CDC_EFB7#, 16#0BDB_DF21#, + 16#86D3_D2D4#, 16#F1D4_E242#, 16#68DD_B3F8#, 16#1FDA_836E#, + 16#81BE_16CD#, 16#F6B9_265B#, 16#6FB0_77E1#, 16#18B7_4777#, + 16#8808_5AE6#, 16#FF0F_6A70#, 16#6606_3BCA#, 16#1101_0B5C#, + 16#8F65_9EFF#, 16#F862_AE69#, 16#616B_FFD3#, 16#166C_CF45#, + 16#A00A_E278#, 16#D70D_D2EE#, 16#4E04_8354#, 16#3903_B3C2#, + 16#A767_2661#, 16#D060_16F7#, 16#4969_474D#, 16#3E6E_77DB#, + 16#AED1_6A4A#, 16#D9D6_5ADC#, 16#40DF_0B66#, 16#37D8_3BF0#, + 16#A9BC_AE53#, 16#DEBB_9EC5#, 16#47B2_CF7F#, 16#30B5_FFE9#, + 16#BDBD_F21C#, 16#CABA_C28A#, 16#53B3_9330#, 16#24B4_A3A6#, + 16#BAD0_3605#, 16#CDD7_0693#, 16#54DE_5729#, 16#23D9_67BF#, + 16#B366_7A2E#, 16#C461_4AB8#, 16#5D68_1B02#, 16#2A6F_2B94#, + 16#B40B_BE37#, 16#C30C_8EA1#, 16#5A05_DF1B#, 16#2D02_EF8D#); + +end CRC32; diff -uNr a/smg_comms/src/data_structs.ads b/smg_comms/src/data_structs.ads --- a/smg_comms/src/data_structs.ads false +++ b/smg_comms/src/data_structs.ads 50379b31ed9f6b3e63c7037db679ade8309a44c6b0510520288afc535a8ffd55e006cc20a479374e992991116253625c588278b21b0e12c254ad035a64e57258 @@ -0,0 +1,80 @@ + -- Data structures for SMG Communication Protocol + -- S.MG, 2018 + +with Interfaces; -- for fixed-size types +with Raw_Types; -- for protocol raw types +with System; -- for Bit_Order +with Serpent; -- for Serpent.Key type + +package Data_Structs is + Pragma Pure(Data_Structs); + + -- an object in the world, with record layout fully specified + -- a storage element is 8-bit aka 1 octet + -- "at" gives number of storage element + -- "range" gives bits inside storage element + + type SMG_Object is + record + -- ID of the object + ID : Interfaces.Unsigned_32; + + -- Position of the object in the world. + -- For a world with map coordinates (MC) between -500 and +500, + -- the relationship with given figure (GF) is: + -- MC = GF / 65.535 - 500 + -- where 65.535 comes from the mapping 65535 / (500 - - 500) + X, Y, Z : Interfaces.Integer_16; + + -- Rotation of the object (RO) linked to GF by: + -- RO = GF / 128*pi + RX, RY, RZ : Interfaces.Unsigned_8; + end record; + for SMG_Object'Size use 104; -- in bits! + for SMG_Object'Bit_Order use System.Low_Order_First; + for SMG_Object use + record + ID at 0 range 0 .. 31; + X at 4 range 0 .. 15; + Y at 6 range 0 .. 15; + Z at 8 range 0 .. 15; + RX at 10 range 0 .. 7; + RY at 11 range 0 .. 7; + RZ at 12 range 0 .. 7; + end record; + + ------------------------- + -- A set of Serpent Keys + -- parametrized record for a Serpent Keyset + -- parameters: + -- N is number of Serpent Keys contained + -- this can be the content of messages 4.1 or 5.2 in protocol spec. + + -- an array of Serpent Keys + -- MAXIMUM 40 keys allowed in a message, hence subtype for key count + subtype Keys_Count is Interfaces.Unsigned_8 range 1..40; + type SKeys_Array is array( Keys_Count range <>) of Serpent.Key; + + type Serpent_Keyset( N : Keys_Count := Keys_Count'Last) is + record + -- actual Serpent Keys + Keys : SKeys_Array( 1..N ); + -- whether for talking to client (LSB set) or talking to server (MSB set) + Flag : Interfaces.Unsigned_8; + end record; + + ------------------------------ + -- Serpent Keys Management + type Keys_Mgm (N_Burnt: Interfaces.Unsigned_8) is + record + -- count of server keys requested + N_Server: Interfaces.Unsigned_8; + -- count of client keys requested + N_Client: Interfaces.Unsigned_8; + -- ID of Serpent key preferred for further inbound Serpent msgs. + Key_ID : Interfaces.Unsigned_8; + -- IDs of Serpent keys burnt by this message + Burnt : SKeys_Array( 1..N_Burnt ); + end record; + +end Data_Structs; diff -uNr a/smg_comms/src/messages.adb b/smg_comms/src/messages.adb --- a/smg_comms/src/messages.adb false +++ b/smg_comms/src/messages.adb 899126526fa67b199ae8b8c17f0dcbbb68f59e0c6e88bfb21dacac13d02fc11f3c6a2af97920afd44660c7ac689b51d474e3671cddee4443eeeaa5891ca374e6 @@ -0,0 +1,139 @@ + -- Message reader & writers for SMG Communication Protocol + -- S.MG, 2018 + +with Interfaces; use Interfaces; +with Serpent; +with System; use System; + +package body Messages is + + procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset; + Counter : in Interfaces.Unsigned_16; + Msg : out Raw_Types.Serpent_Msg) is + Pos : Integer := Msg'First; + Check : CRC32.CRC32; + PadLen: Integer; + K : Serpent.Key; + begin + -- write Type ID + Msg(Pos) := SKeys_S_Type; + Pos := Pos + 1; + + -- write count of keys (NB: this IS 8 bits by definition) + Msg(Pos) := Keyset.Keys'Length; + Pos := Pos + 1; + + -- write keys + for I in Keyset.Keys'Range loop + -- retrieve Key to write + K := Keyset.Keys( I ); + + -- write key itself + Msg(Pos..Pos+K'Length-1) := K; + -- ensure little endian order in message + Cast_LE(Msg(Pos..Pos+K'Length-1)); + Pos := Pos + K'Length; + + -- write CRC of key + Check := CRC32.CRC( K ); + Msg(Pos..Pos+3) := Raw_Types.Cast(Check); + Cast_LE(Msg(Pos..Pos+3)); + Pos := Pos + 4; + end loop; + + -- write flag + Msg(Pos) := Keyset.Flag; + Pos := Pos + 1; + + -- write message counter + Msg(Pos..Pos+1) := Raw_Types.Cast(Counter); + Cast_LE(Msg(Pos..Pos+1)); + Pos := Pos + 2; + + -- write padding as needed; endianness is irrelevant here + PadLen := Msg'Last - Pos + 1; + if PadLen > 0 then + declare + Pad : Raw_Types.Octets(1..PadLen); + begin + RNG.Get_Octets( Pad ); + Msg(Pos..Pos+PadLen-1) := Pad; + end; + end if; + end Write_SKeys_SMsg; + + + -- Reads a Serpent keyset from given Serpent Message + procedure Read_SKeys_SMsg( Msg : in Raw_Types.Serpent_Msg; + Counter : out Interfaces.Unsigned_16; + Keyset : out Serpent_Keyset) is + Pos: Integer := Msg'First; + begin + -- read type and check + if Msg(Pos) = SKeys_S_Type then + Pos := Pos + 1; + else + raise Invalid_Msg; + end if; + + -- read count of keys and check + if Msg(Pos) in Keys_Count'Range then + declare + N : Keys_Count := Keys_Count(Msg(Pos)); + KS : Serpent_Keyset(N); + K : Serpent.Key; + Check : CRC32.CRC32; + O4 : Raw_Types.Octets_4; + O2 : Raw_Types.Octets_2; + begin + Pos := Pos + 1; + --read keys and check crc for each + for I in 1 .. N loop + -- read key and advance pos + K := Msg(Pos..Pos+K'Length-1); + Cast_LE(K); + Pos := Pos + K'Length; + -- read crc and compare to crc32(key) + O4 := Msg(Pos..Pos+3); + Cast_LE(O4); + Check := Raw_Types.Cast(O4); + Pos := Pos + 4; + if Check /= CRC32.CRC(K) then + raise Invalid_Msg; + end if; + -- if it got here, key is fine so add to set + KS.Keys(KS.Keys'First + I -1) := K; + end loop; + -- read and set flag + KS.Flag := Msg(Pos); + Pos := Pos + 1; + -- read and set message counter + O2 := Msg(Pos..Pos+1); + Cast_LE(O2); + Counter := Raw_Types.Cast(O2); + -- rest of message is padding so it's ignored + -- copy keyset to output variable + Keyset := KS; + end; + else + raise Invalid_Msg; + end if; + end Read_SKeys_SMsg; + + -- private part + procedure Cast_LE( LE: in out Raw_Types.Octets ) is + begin + -- flip octets ONLY if native is big endian. + if System.Default_Bit_Order = System.High_Order_First then + declare + BE: constant Raw_Types.Octets := LE; + begin + for I in 1..LE'Length loop + LE(LE'First+I-1) := BE(BE'Last-I+1); + end loop; + end; + end if; + -- NOTHING to do for native little endian + end Cast_LE; + +end Messages; diff -uNr a/smg_comms/src/messages.ads b/smg_comms/src/messages.ads --- a/smg_comms/src/messages.ads false +++ b/smg_comms/src/messages.ads 1190fb877b7956b9b38f1d817858fe872f6e93b7e4fac674bf3c82d664acb11204fc88de2563b33298a2a9ad547343cdf03f2cf068b3b1208db0b1ee0b3c246c @@ -0,0 +1,77 @@ + -- 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; + + ------------------------------------------------ + -- 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); + +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; + +end Messages; diff -uNr a/smg_comms/tests/test_serializing.adb b/smg_comms/tests/test_serializing.adb --- a/smg_comms/tests/test_serializing.adb false +++ b/smg_comms/tests/test_serializing.adb 0b7d9c9ba8094d32022cafc676276ddf492fc26965ddae35cb67dddfc906b95b5d9025896bae70ed46a1c6ffd8999d87c707a1edb5577e5751fc48272108c8dd @@ -0,0 +1,59 @@ + -- Tests for the serialization of data structures in SMG Protocol + -- S.MG, 2018 + +with RNG; +with Data_Structs; use Data_Structs; +with Messages; use Messages; +with Interfaces; use Interfaces; +with System; +with System.Storage_Elements; use System.Storage_Elements; +with Ada.Text_IO; use Ada.Text_IO; + +package body Test_Serializing is + + procedure Serialize_Keyset_SS is + Msg : Serpent_Msg; + KSet : Serpent_Keyset(5); + LSB : Interfaces.Unsigned_8 := 16#01#; + MSB : Interfaces.Unsigned_8 := 16#80#; + LMSB: Interfaces.Unsigned_8 := 16#81#; + Counter : Interfaces.Unsigned_16 := 101; + NewSet: Serpent_Keyset; + NewCounter: Interfaces.Unsigned_16:=0; + begin + Put_Line("Generating the Serpent Keys..."); + -- fill a set of Serpent Keys + for I in 1..KSet.N loop + RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I))); + end loop; + KSet.Flag := LSB; + + Put_Line("Writing the keys to message..."); + -- write keyset to serpent message + Write_SKeys_SMsg( KSet, Counter, Msg ); + + Put_Line("Reading keys back from message..."); + -- read keyset from message + Read_SKeys_SMsg( Msg, Counter, NewSet ); + + Put_Line("Comparing the keysets..."); + -- compare the two keysets + if NewSet /= KSet then + Put_Line("FAIL: keysets are different!"); + else + Put_Line("PASS: keysets are the same!"); + end if; + + Put_Line("Attempting to read from mangled message"); + begin + Msg(Msg'First) := Msg(Msg'First)+25; + Read_SKeys_SMsg( Msg, Counter, NewSet); + Put_Line("FAIL: read failed to raise invalid message exception!"); + exception + when Invalid_Msg => + Put_Line("PASS: exception correctly raised for invalid message"); + end; + end Serialize_Keyset_SS; + +end Test_Serializing; + diff -uNr a/smg_comms/tests/test_serializing.ads b/smg_comms/tests/test_serializing.ads --- a/smg_comms/tests/test_serializing.ads false +++ b/smg_comms/tests/test_serializing.ads 0571dd3ea06e4684efb6ca56739fe9ff7f56befef87c107aa38f356772338418afb8562f989c615ff9d9b10cfa95cdb57bf88a395524adb6194e62bd66812637 @@ -0,0 +1,12 @@ + -- Tests for the serialization of data structures in SMG Protocol + -- S.MG, 2018 + +with Raw_Types; use Raw_Types; + +package Test_Serializing is + + procedure Serialize_Keyset_SS; + +private + +end Test_Serializing; diff -uNr a/smg_comms/tests/testall.adb b/smg_comms/tests/testall.adb --- a/smg_comms/tests/testall.adb 01ded6598577bf4fb8027beaa9678239ef98f27abb62e3c5739b9347e4a747a1107052a6f908c7b4d3603c8db762b59aaac9efa6ebf64c8cffe1287985d87bb6 +++ b/smg_comms/tests/testall.adb c0a18fd4b745715568d7ef048d6d39f9e161b859d781cdaf1ba656cd327407f11ca302b1e1f58bc6a5d17de14b871f577c151bfd17c6ba560b1a6f111f4021fc @@ -3,6 +3,7 @@ with Test_Serpent; with Test_Packing; with Test_RSA_OAEP; +with Test_Serializing; procedure testall is begin @@ -13,4 +14,5 @@ Test_RSA_OAEP.test_rsa_oaep; Test_Packing.Test_Pack_Unpack_Serpent; Test_Packing.Test_Pack_Unpack_RSA; + Test_Serializing.Serialize_Keyset_SS; end testall;