tree checksum vpatch file split hunks

all signers: diana_coman

antecedents: smg_comms_packing_rsa smg_comms_files

press order:

smg_comms_genesisdiana_coman
smg_comms_raw_typesdiana_coman
smg_comms_packing_serpentdiana_coman
smg_comms_c_wrappersdiana_coman
smg_comms_rsa_oaepdiana_coman
smg_comms_packing_rsadiana_coman
smg_comms_80colsdiana_coman
smg_comms_skeys_smsgsdiana_coman
smg_comms_io_rsa_tests_onlydiana_coman
smg_comms_keymgmdiana_coman
smg_comms_filesdiana_coman
smg_comms_actions_rsadiana_coman

patch:

- 268276F725EBBBF0EB0E11ADCD2516571ACB8958068B17040E1FB841A2EDD44878D73C932982D70C4192B70C43F2CC9EDF699380AA98FE15A968D81BFC911B14
+ A11EC7A815179C71A1C1B0239EE392930B811CFFC2D83328BC4D6D9D7182EB9D5D55F3C010ED9FB8A1A98B14E578407331FB6229B479386ABE16F6DFF21B98F1
smg_comms/manifest
(9 . 3)(9 . 4)
5 549785 smg_comms_io_rsa_tests_only diana_coman Small refactoring of tests extracting the reading of RSA key into package of its own so it can be used throughout tests and thus get rid of the too long lines in test_packing.adb.
6 550310 smg_comms_keymgm diana_coman Adds read/write for Keys Management messages (both Serpent and RSA). Refactors the read/write of Serpent Keys messages so that the same core is called by RSA/Serpent specific-methods, adding also read/write of keys from/to RSA messages.
7 551086 smg_comms_files diana_coman Adds read/write for File Transfer (4.3) and File Request (4.4). Refactors the rest to have read/write of 16 bits values in one single place (i.e. separate method called from everywhere else) because of how common it is + sensitive to endianness.
8 551832 smg_comms_actions_rsa diana_coman Adds read/write for RSA keys (5.1) and Client Action (4.5). Refactors to allow choice of padding and enable direct testing of private procedure in Messages.
- D472416A6FDBB7FD240664149E4A120F449AC15839D611E8A6987C145D3FE13C0A2227FE8C6BA3BC8F4ED2350973E7DFE6A074AB87CB02FBAF786396169FE5CF
+ 973FCD28AFF8D88504DE9A8507277D84C0514EDDE25437433F00E3662DA0070BEE6FC80D428FCCEBD7B0F996C5B0EDA8E2BBFCE9FCDD99A542C5AD647B4B5A09
smg_comms/src/data_structs.ads
(44 . 14)(44 . 13)
13 RZ at 12 range 0 .. 7;
14 end record;
15
16 -- length of a text field (i.e. 16 bits, strictly > 0)
17 subtype Text_Len is Positive range 1..2**16-1;
18
19 -- A set of file names glued into a single string
20 -- NB: there IS at least ONE filename and one character
21 -- Upper limit for Text_Len is due to protocol's spec of text basic type
22 type U16_Array is array (Text_Len range <> ) of Interfaces.Unsigned_16;
23 type Filenames( F_No: Text_Len := 1; Sz: Text_Len := 1 ) is
24 type U16_Array is array (Raw_Types.Text_Len range <> )
25 of Interfaces.Unsigned_16;
26 type Filenames( F_No: Raw_Types.Text_Len := 1;
27 Sz: Raw_Types.Text_Len := 1 ) is
28 record
29 -- filenames glued together into 1 single string
30 S : String( 1 .. Sz ) := (others => '0');
(60 . 9)(59 . 9)
32 end record;
33
34 -- A chunk of a file (for file transfer)
35 type File_Chunk( Len : Text_Len := 1;
36 type File_Chunk( Len : Raw_Types.Text_Len := 1;
37 Count : Interfaces.Unsigned_16 := 1;
38 Name_Len: Text_len := 1) is
39 Name_Len: Raw_Types.Text_len := 1) is
40 record
41 Filename: String(1..Name_Len);
42 Content : Raw_Types.Octets(1..Len);
(112 . 4)(111 . 27)
44 end case;
45 end record;
46
47 type Player_RSA is
48 record
49 -- communication protocol Version number
50 Proto_V : Interfaces.Unsigned_8;
51
52 -- communication protocol Subversion number
53 Proto_Subv : Interfaces.Unsigned_16;
54
55 -- Keccak hash of client binary
56 Client_Hash: Raw_Types.Octets_8;
57
58 -- public exponent (e) of RSA key (64 bits precisely)
59 -- nb: this is protocol-specific e, aka shorter than TMSR e...
60 e : Raw_Types.Octets_8;
61
62 -- public modulus (n) of RSA key (490 bits precisely)
63 n : Raw_Types.RSA_len;
64
65 -- preferred padding; magic value 0x13370000 means random padding
66 Padding : Raw_Types.Octets_8;
67
68 end record;
69
70 end Data_Structs;
- FD5121B49FCBCB70293B002298FC82272EF291E959FE63D17B5B22BA02B1ABB2B1D12A6D0D9541CAEBE43A300FE08CC329FBCACE81B8341A00296F66EB182A84
+ 04B433F10C5557D2F696FEBE95CA6E9497354129DDFCC95C1E366AF2744B214DB5F7A9E63B401754F1E345ADB0EF658A95A60FB54BC761BA628962AA2E40CCAC
smg_comms/src/messages.adb
(1 . 6)(1 . 7)
75 -- Message reader & writers for SMG Communication Protocol
76 -- S.MG, 2018
77
78 with Raw_Types; use Raw_Types;
79 with Interfaces; use Interfaces;
80 with Serpent;
81 with System; use System;
(14 . 10)(15 . 11)
83
84 procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset;
85 Counter : in Interfaces.Unsigned_16;
86 Pad : in Raw_Types.Octets_8;
87 Msg : out Raw_Types.Serpent_Msg) is
88 begin
89 -- call internal write on Octets with correct type id
90 Write_SKeys( Keyset, Counter, SKeys_S_Type, Msg );
91 Write_SKeys( Keyset, Counter, SKeys_S_Type, Pad, Msg );
92 end Write_SKeys_SMsg;
93
94
(37 . 10)(39 . 11)
96 -- writes given key mgm structure into a Serpent message
97 procedure Write_KMgm_SMsg( KMgm : in Keys_Mgm;
98 Counter : in Interfaces.Unsigned_16;
99 Pad : in Raw_Types.Octets_8;
100 Msg : out Raw_Types.Serpent_Msg) is
101 begin
102 -- call internal write of key mgm with correct type ID
103 Write_KMgm( KMgm, Counter, Key_Mgm_S_Type, Msg );
104 Write_KMgm( KMgm, Counter, Key_Mgm_S_Type, Pad, Msg );
105 end Write_KMgm_SMsg;
106
107 -- reads a key mgm structure from the given Serpent message
(58 . 6)(61 . 7)
109
110 ------ File Transfer ------
111 procedure Write_File_Transfer( Chunk : in File_Chunk;
112 Pad : in Raw_Types.Octets_8;
113 Msg : out Raw_Types.Serpent_Msg) is
114 Pos: Integer := Msg'First;
115 U16: Interfaces.Unsigned_16;
(68 . 7)(72 . 7)
117
118 -- write filename as text field (size+2, text)
119 -- check against overflows
120 if Chunk.Name_Len > Text_Len'Last - 2 or
121 if Chunk.Name_Len > Raw_Types.Text_Len'Last - 2 or
122 Pos + Integer(Chunk.Name_Len) + 2 > Msg'Last then
123 raise Invalid_Msg;
124 end if;
(84 . 7)(88 . 7)
126
127 --write content
128 -- check against overflow, including the 2 octets for counter at the end
129 if Chunk.Len > Text_Len'Last - 2 or
130 if Chunk.Len > Raw_Types.Text_Len'Last - 2 or
131 Pos + Integer(Chunk.Len) + 4 > Msg'Last then
132 raise Invalid_Msg;
133 end if;
(97 . 13)(101 . 8)
135 Msg(Pos..Pos+Chunk.Content'Length-1) := Chunk.Content;
136 Pos := Pos + Chunk.Content'Length;
137
138 -- write counter
139 Write_U16( Msg, Pos, Chunk.Count );
140
141 -- write padding if needed
142 if Pos <= Msg'Last then
143 RNG.Get_Octets( Msg(Pos..Msg'Last) );
144 end if;
145 -- write counter + padding
146 Write_End( Msg, Pos, Chunk.Count, Pad );
147
148 end Write_File_Transfer;
149
(115 . 9)(114 . 9)
151 Pos: Integer := Msg'First;
152 U16: Interfaces.Unsigned_16;
153 S_Name, E_Name: Integer; --start/end for filename in Msg
154 S_Len: Text_Len; -- length of filename (needed as Text_Len anyway)
155 S_Len: Raw_Types.Text_Len; -- length of filename
156 S_Content, E_Content: Integer; --start/end for content in Msg
157 Content_Len: text_Len; -- length of content (needed as Text_Len anyway)
158 Content_Len: Raw_Types.Text_Len; -- length of content
159 begin
160 -- read and check type ID
161 if Msg(Pos) /= File_Transfer_S_Type then
(134 . 7)(133 . 7)
163 raise Invalid_Msg;
164 end if;
165 U16 := U16 - 2;
166 S_Len := Text_Len(U16);
167 S_Len := Raw_Types.Text_Len(U16);
168
169 -- set start + end for reading filename later, when ready
170 S_Name := Pos;
(149 . 7)(148 . 7)
172 raise Invalid_msg;
173 end if;
174 U16 := U16 - 2;
175 Content_Len := Text_Len(U16);
176 Content_Len := Raw_Types.Text_Len(U16);
177 -- set start and end for reading content later, when ready
178 S_Content := Pos;
179 E_Content := Pos + Integer(U16) - 1;
(180 . 6)(179 . 7)
181 ---- File Requests ----
182 procedure Write_File_Request( FR : in Filenames;
183 Counter : in Interfaces.Unsigned_16;
184 Pad : in Raw_Types.Octets_8;
185 Msg : out Raw_Types.Serpent_Msg;
186 Written : out Natural) is
187 Pos : Integer := Msg'First;
(262 . 13)(262 . 9)
189 end;
190 end loop;
191
192 -- write the message counter in little endian at all times
193 Write_U16( Msg, Pos, Counter );
194 -- write counter + padding
195 Write_End( Msg, Pos, Counter, Pad );
196
197 -- write padding if needed
198 if Pos <= Msg'Last then
199 Rng.Get_Octets( Msg(Pos..Msg'Last) );
200 end if;
201 end Write_File_Request;
202
203 -- Reads a request for files; the opposite of Write_File_Request above
(314 . 7)(310 . 8)
205 -- if it's not that => Invalid_Msg
206 -- F_No and Text_Sz are not overflow (earlier check + calc)
207 declare
208 F : Filenames(Text_Len(F_No), Text_Len(Text_Sz-F_No+1));
209 F : Filenames(Raw_Types.Text_Len(F_No),
210 Raw_Types.Text_Len(Text_Sz-F_No+1));
211 S_Pos : Positive;
212 Index : Positive;
213 begin
(350 . 16)(347 . 158)
215
216 end Read_File_Request;
217
218 -- writes the action (octets+length) into the specified Serpent message
219 procedure Write_Action( A : in Raw_Types.Text_Octets;
220 Counter : in Interfaces.Unsigned_16;
221 Pad : in Raw_Types.Octets_8;
222 Msg : out Raw_Types.Serpent_Msg) is
223 Pos : Natural := Msg'First + 1;
224 MaxPos : Natural := Msg'Last - 1; --2 octets reserved for counter at end
225 U16 : Interfaces.Unsigned_16;
226 begin
227 -- check whether given action FITS into a Serpent message
228 if Pos + 2 + A.Len > MaxPos then
229 raise Invalid_Msg;
230 end if;
231
232 -- write correct type ID
233 Msg( Msg'First ) := Client_Action_S_Type;
234
235 -- write action's TOTAL length
236 U16 := Interfaces.Unsigned_16(A.Len + 2);
237 Write_U16( Msg, Pos, U16 );
238
239 -- write the action itself
240 Msg( Pos..Pos+A.Len-1 ) := A.Content;
241 Pos := Pos + A.Len;
242
243 -- write counter + padding
244 Write_End( Msg, Pos, Counter, Pad );
245
246 end Write_Action;
247
248 -- reads a client action as octets+length from the given Serpent message
249 procedure Read_Action( Msg : in Raw_Types.Serpent_Msg;
250 Counter : out Interfaces.Unsigned_16;
251 A : out Raw_Types.Text_Octets) is
252 Pos : Natural := Msg'First + 1;
253 U16 : Interfaces.Unsigned_16;
254 begin
255 -- read and check message type ID
256 if Msg( Msg'First ) /= Client_Action_S_Type then
257 raise Invalid_Msg;
258 end if;
259
260 -- read size of action (content+ 2 octets the size itself)
261 Read_U16( Msg, Pos, U16 );
262
263 -- check size
264 if U16 < 3 or Pos + Natural(U16) - 2 > Msg'Last - 1 then
265 raise Invalid_Msg;
266 else
267 U16 := U16 - 2; --size of content only
268 end if;
269
270 -- create action, read it from message + assign to output variable
271 declare
272 Act : Raw_Types.Text_Octets( Raw_Types.Text_Len( U16 ) );
273 begin
274 Act.Content := Msg( Pos..Pos+Act.Len-1 );
275 Pos := Pos + Act.Len;
276 A := Act;
277 end;
278
279 -- read counter
280 Read_U16( Msg, Pos, Counter );
281
282 end Read_Action;
283
284 ------------------
285 -- RSA Messages --
286 ------------------
287
288 procedure Write_RKeys_RMsg( K : in Player_RSA;
289 Counter : in Interfaces.Unsigned_16;
290 Pad : in Raw_Types.Octets_8;
291 Msg : out Raw_Types.RSA_Msg) is
292 Pos : Natural := Msg'First + 1;
293 begin
294 -- write correct message type
295 Msg( Msg'First ) := RKeys_R_Type;
296
297 -- write protocol version and subversion
298 Msg( Pos ) := K.Proto_V;
299 Pos := Pos + 1;
300 Write_U16( Msg, Pos, K.Proto_Subv );
301
302 -- write keccak hash of client binary
303 Msg( Pos..Pos + K.Client_Hash'Length-1 ) := K.Client_Hash;
304 Pos := Pos + K.Client_Hash'Length;
305
306 -- write e of RSA key
307 Msg( Pos..Pos + K.e'Length - 1 ) := K.e;
308 Pos := Pos + K.e'Length;
309
310 -- write n of RSA key
311 Msg( Pos..Pos + K.n'Length - 1 ) := K.n;
312 Pos := Pos + K.n'Length;
313
314 -- write preferred padding
315 Msg( Pos..Pos + K.Padding'Length - 1 ) := K.Padding;
316 Pos := Pos + K.Padding'Length;
317
318 -- write counter + padding
319 Write_End( Msg, Pos, Counter, Pad );
320
321 end Write_RKeys_RMsg;
322
323 -- Reads a RSA Keyset (Player_RSA structures) from the given RSA Message.
324 -- Opposite of Write_RKeys_RMsg above
325 procedure Read_RKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
326 Counter : out Interfaces.Unsigned_16;
327 K : out Player_RSA) is
328 Pos : Natural := Msg'First + 1;
329 begin
330 -- check type id and raise exception if incorrect
331 if Msg(Msg'First) /= RKeys_R_Type then
332 raise Invalid_Msg;
333 end if;
334
335 -- read protocol version and subversion
336 K.Proto_V := Msg( Pos );
337 Pos := Pos + 1;
338 Read_U16( Msg, Pos, K.Proto_Subv );
339
340 -- read Keccak hash of client binary
341 K.Client_Hash := Msg( Pos..Pos+K.Client_Hash'Length - 1 );
342 Pos := Pos + K.Client_Hash'Length;
343
344 -- read e
345 K.e := Msg( Pos .. Pos + K.e'Length - 1 );
346 Pos := Pos + K.e'Length;
347
348 -- read n
349 K.n := Msg( Pos .. Pos + K.n'Length - 1 );
350 Pos := Pos + K.n'Length;
351
352 -- read choice of padding
353 K.Padding := Msg( Pos .. Pos+K.Padding'Length - 1 );
354 Pos := Pos + K.Padding'Length;
355
356 -- read message counter
357 Read_U16( Msg, Pos, Counter );
358
359 -- the rest is message padding, so ignore it
360
361 end Read_RKeys_RMsg;
362
363 procedure Write_SKeys_RMsg( Keyset : in Serpent_Keyset;
364 Counter : in Interfaces.Unsigned_16;
365 Pad : in Raw_Types.Octets_8;
366 Msg : out Raw_Types.RSA_Msg) is
367 begin
368 -- call internal write of Serpent keys with correct type ID
369 Write_SKeys( Keyset, Counter, SKeys_R_Type, Msg );
370 Write_SKeys( Keyset, Counter, SKeys_R_Type, Pad, Msg );
371 end Write_SKeys_RMsg;
372
373 procedure Read_SKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
(376 . 10)(515 . 11)
375
376 procedure Write_KMgm_RMsg( KMgm : in Keys_Mgm;
377 Counter : in Interfaces.Unsigned_16;
378 Pad : in Raw_Types.Octets_8;
379 Msg : out Raw_Types.RSA_Msg) is
380 begin
381 -- call internal write of key mgm with correct type ID
382 Write_KMgm( KMgm, Counter, Key_Mgm_R_Type, Msg );
383 Write_KMgm( KMgm, Counter, Key_Mgm_R_Type, Pad, Msg );
384 end Write_KMgm_RMsg;
385
386 procedure Read_KMgm_RMsg( Msg : in Raw_Types.RSA_Msg;
(436 . 6)(576 . 7)
388 procedure Write_SKeys( Keyset : in Serpent_Keyset;
389 Counter : in Interfaces.Unsigned_16;
390 Type_ID : in Interfaces.Unsigned_8;
391 Pad : in Raw_Types.Octets_8;
392 Msg : out Raw_Types.Octets) is
393 Pos : Integer := Msg'First;
394 Check : CRC32.CRC32;
(471 . 13)(612 . 8)
396 Msg(Pos) := Keyset.Flag;
397 Pos := Pos + 1;
398
399 -- write message counter
400 Write_U16( Msg, Pos, Counter );
401
402 -- write padding as needed; endianness is irrelevant here
403 if Pos <= Msg'Last then
404 RNG.Get_Octets( Msg(Pos..Msg'Last) );
405 end if;
406 -- write counter + padding
407 Write_End( Msg, Pos, Counter, Pad );
408
409 end Write_SKeys;
410
(539 . 6)(675 . 7)
412 procedure Write_KMgm( KMgm : in Keys_Mgm;
413 Counter : in Interfaces.Unsigned_16;
414 Type_ID : in Interfaces.Unsigned_8;
415 Pad : in Raw_Types.Octets_8;
416 Msg : out Raw_Types.Octets) is
417 Pos : Integer := Msg'First;
418 begin
(568 . 13)(705 . 8)
420 Pos := Pos + KMgm.Burnt'Length;
421 end if;
422
423 -- write the message count
424 Write_U16( Msg, Pos, Counter );
425
426 -- pad with random octets until the end of Msg
427 if Pos <= Msg'Last then
428 RNG.Get_Octets( Msg(Pos..Msg'Last) );
429 end if;
430 -- write counter + padding
431 Write_End( Msg, Pos, Counter, Pad );
432
433 end Write_KMgm;
434
(647 . 4)(779 . 33)
436 Pos := Pos + 2;
437 end Read_U16;
438
439 -- Writes Counter and padding (rng or otherwise) into Msg starting from Pos.
440 procedure Write_End( Msg : in out Raw_Types.Octets;
441 Pos : in out Natural;
442 Counter : in Interfaces.Unsigned_16;
443 Padding : in Raw_Types.Octets_8) is
444 begin
445 -- check that there is space for Counter at the very least
446 if Pos > Msg'Last - 1 then
447 raise Invalid_Msg;
448 end if;
449
450 -- write counter
451 Write_U16( Msg, Pos, Counter );
452
453 -- pad to the end of the message
454 if Pos <= Msg'Last then
455 if Padding = RNG_PAD then
456 RNG.Get_Octets( Msg( Pos..Msg'Last ) );
457 else
458 -- repeat the Padding value itself
459 for I in Pos..Msg'Last loop
460 Msg(I) := Padding( Padding'First + (I - Pos) mod Padding'Length );
461 end loop;
462 end if;
463 -- either rng or fixed, update Pos though
464 Pos := Msg'Last + 1;
465 end if;
466 end Write_End;
467
468 end Messages;
- D6C3980DA332629A8C09CFBFB32E1962AD689C7ECBC4A8EE83B02F85C1AA1A3374C5371EEEE7F0CC49FE4C4D8E758F86FA5BA204C5900BD6F4F95CABB3A06EF6
+ 00B24D7C457C5E0A82DFFF5E1E184597D7699ADE009353120910BC18A5577B63291758F88461AAFB79EFCB80065CC8B85F45870F55C29050DD6BFB2F86BCA53C
smg_comms/src/messages.ads
(32 . 6)(32 . 7)
473 -- Counter - the message count
474 procedure Write_SKeys_SMsg( Keyset : in Serpent_Keyset;
475 Counter : in Interfaces.Unsigned_16;
476 Pad : in Raw_Types.Octets_8;
477 Msg : out Raw_Types.Serpent_Msg);
478
479 -- Reads a Serpent Keyset from the given Serpent Message
(48 . 6)(49 . 7)
481 -- Counter - the message count
482 procedure Write_KMgm_SMsg( KMgm : in Keys_Mgm;
483 Counter : in Interfaces.Unsigned_16;
484 Pad : in Raw_Types.Octets_8;
485 Msg : out Raw_Types.Serpent_Msg);
486
487 -- Reads a Key management structure from the given Serpent Message
(61 . 7)(63 . 9)
489
490 -- Writes the given File Chunk to a File Transfer type of message
491 -- Chunk - chunk of file to write; contains counter, filename, content
492 -- Pad - whether random padding (RNG_PAD value) or given value
493 procedure Write_File_Transfer( Chunk : in File_Chunk;
494 Pad : in Raw_Types.Octets_8;
495 Msg : out Raw_Types.Serpent_Msg);
496
497 -- The opposite of Write_File_Transfer method above.
(77 . 6)(81 . 7)
499 -- did not fit into the message and it's up to caller to decide what to do.
500 procedure Write_File_Request( FR : in Filenames;
501 Counter : in Interfaces.Unsigned_16;
502 Pad : in Raw_Types.Octets_8;
503 Msg : out Raw_Types.Serpent_Msg;
504 Written : out Natural);
505
(86 . 17)(91 . 45)
507 Counter : out Interfaces.Unsigned_16;
508 FR : out Filenames);
509
510 -------------------------Client Actions------------------------------
511 -- writes the action (octets+length) into the specified Serpent message
512 procedure Write_Action( A : in Raw_Types.Text_Octets;
513 Counter : in Interfaces.Unsigned_16;
514 Pad : in Raw_Types.Octets_8;
515 Msg : out Raw_Types.Serpent_Msg);
516
517 -- reads a client action as octets+length from the given Serpent message
518 procedure Read_Action( Msg : in Raw_Types.Serpent_Msg;
519 Counter : out Interfaces.Unsigned_16;
520 A : out Raw_Types.Text_Octets);
521
522 ------------------
523 -- RSA Messages --
524 ------------------
525
526 -------------------------Keys----------------------------------------
527
528 -- Writes a RSA Keyset (with 8 octets e) to the given RSA Message.
529 -- K - the Player_RSA structures with all needed information to write
530 -- Counter - the message count
531 procedure Write_RKeys_RMsg( K : in Player_RSA;
532 Counter : in Interfaces.Unsigned_16;
533 Pad : in Raw_Types.Octets_8;
534 Msg : out Raw_Types.RSA_Msg);
535
536 -- Reads a RSA Keyset (Player_RSA structures) from the given RSA Message.
537 -- Opposite of Write_RKeys_RMsg above
538 procedure Read_RKeys_RMsg( Msg : in Raw_Types.RSA_Msg;
539 Counter : out Interfaces.Unsigned_16;
540 K : out Player_RSA);
541
542 -- Writes a Serpent Keyset to the given RSA Message
543 --
544 -- Keyset - the set of keys to write to message
545 -- Counter - the message count
546 procedure Write_SKeys_RMsg( Keyset : in Serpent_Keyset;
547 Counter : in Interfaces.Unsigned_16;
548 Pad : in Raw_Types.Octets_8;
549 Msg : out Raw_Types.RSA_Msg);
550
551 -- Reads a Serpent Keyset from the given RSA Message
(111 . 8)(144 . 10)
553 --
554 -- KMgm - the keys management structure to write to message
555 -- Counter - the message count
556 -- Pad - whether random padding (RNG_PAD) or not.
557 procedure Write_KMgm_RMsg( KMgm : in Keys_Mgm;
558 Counter : in Interfaces.Unsigned_16;
559 Pad : in Raw_Types.Octets_8;
560 Msg : out Raw_Types.RSA_Msg);
561
562 -- Reads a Key management structure from the given RSA Message
(131 . 6)(166 . 10)
564 -- NB: Str'Length has to be EQUAL to Octets'Length!
565 procedure Octets_To_String(O: in Raw_Types.Octets; Str: out String);
566
567 -- magic value used to request random padding
568 RNG_PAD: constant Raw_Types.Octets_8 := ( 16#00#, 16#00#, 16#00#, 16#00#,
569 16#13#, 16#37#, 16#00#, 16#00#);
570
571 private
572
573 -- if native is little endian, does nothing;
(180 . 6)(219 . 7)
575 procedure Write_SKeys( Keyset : in Serpent_Keyset;
576 Counter : in Interfaces.Unsigned_16;
577 Type_ID : in Interfaces.Unsigned_8;
578 Pad : in Raw_Types.Octets_8;
579 Msg : out Raw_Types.Octets);
580
581 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
(196 . 6)(236 . 7)
583 procedure Write_KMgm( KMgm : in Keys_Mgm;
584 Counter : in Interfaces.Unsigned_16;
585 Type_ID : in Interfaces.Unsigned_8;
586 Pad : in Raw_Types.Octets_8;
587 Msg : out Raw_Types.Octets);
588
589 -- NB: caller has to ensure that Msg is a valid RSA or Serpent message
(223 . 4)(264 . 17)
591 procedure Read_U16( Msg: in Raw_Types.Octets;
592 Pos: in out Natural;
593 U16: out Interfaces.Unsigned_16);
594
595 -- Writes the Counter + padding at the end of messages.
596 -- The padding is either random or repeated value, depending on "Padding".
597 -- NB: This raises Invalid_Msg exception if Pos > Msg'Last - 1
598 -- Msg - the array of octets into which the counter+padding are to be written
599 -- Pos - the position in Msg from which to start writing counter+padding
600 -- Counter - the message counter to write at Pos in Msg.
601 -- Padding - magic value 0x13370000 means random padding;
602 -- - any other value for Padding gets used as it is (repeated)
603 procedure Write_End( Msg : in out Raw_Types.Octets;
604 Pos : in out Natural;
605 Counter : in Interfaces.Unsigned_16;
606 Padding : in Raw_Types.Octets_8);
607 end Messages;
- AF8D8AEF6AF31A1E9B664ED49AA62785576DE0526849596269EC46EDE622C474756FB58AC151A26C93484913881AFDEDD3A069D4327CBC741D6F0C62FED9238D
+ 6E8E85BFAFE5928021EE613A17D9F7D1BEFE761771E1508141128BE52B52424D380199D14EC42A5B1256373030BC474F3568038DBB2B3DA034715A9D7B61C5D6
smg_comms/src/raw_types.ads
(16 . 6)(16 . 17)
612 -- all messages and packets are simply arrays of octets at low level/raw
613 type Octets is array( Natural range <> ) of Interfaces.Unsigned_8;
614
615 -- length of a text field (i.e. 16 bits, strictly > 0)
616 subtype Text_Len is Positive range 1..2**16-1;
617
618 -- "text" type has a 2-byte header with total length
619 -- Len here is length of actual content ONLY (i.e. it needs + 2 for total)
620 type Text_Octets( Len: Text_Len := 1 ) is
621 record
622 -- actual octets making up the "text"
623 Content: Octets( 1..Len ) := (others => 0);
624 end record;
625
626 -- constants from SMG.COMMS standard specification
627
628 -- RSA key size in octets
-
+ 9F725D85955F8C39AA166D4F5E1B63D54F9F153084A351667D78C9BB709A41F86927936730FEB6615127F6111821624E7F67B5D7AE6A95299716F78B5C9A3B87
smg_comms/tests/messages-test_serializing.adb
(0 . 0)(1 . 478)
633 -- Tests for the serialization of data structures in SMG Protocol
634 -- S.MG, 2018
635
636 with RNG;
637 with Data_Structs;
638 with Messages; use Messages;
639 with Interfaces; use Interfaces;
640 with System;
641 with System.Storage_Elements; use System.Storage_Elements;
642 with Ada.Text_IO; use Ada.Text_IO;
643
644 package body Messages.Test_Serializing is
645
646 procedure Serialize_Keyset_RSA is
647 Msg : RSA_Msg;
648 ReadK : Player_RSA;
649 ReadC : Interfaces.Unsigned_16;
650 WMsg : RSA_Msg;
651 TypeID : constant Interfaces.Unsigned_8 := 251;
652 begin
653 Put_Line("Generating data for 5.1 RSA key set in RSA message.");
654 -- fill message with random data
655 RNG.Get_Octets(Msg);
656
657 -- attempt read - it should fail
658 begin
659 Read_RKeys_RMsg( Msg, ReadC, ReadK );
660 exception
661 when Invalid_Msg =>
662 if Msg(Msg'First) /= TypeID then
663 Put_Line("PASS: exception correctly raised for invalid message");
664 else
665 Put_Line("FAIL: exception raised on VALID message!");
666 end if;
667 end;
668
669 -- set correct type id
670 Msg(Msg'First) := TypeID;
671
672 -- attempt read + write and compare
673 Read_RKeys_RMsg( Msg, ReadC, ReadK );
674 Write_RKeys_RMsg( ReadK, ReadC, RNG_PAD, WMsg );
675
676 -- check result (without trailing padding since that CAN be different!)
677 if WMsg(1..520) /= Msg(1..520) then
678 Put_Line("FAIL: read+write of RSA keys to RSA message.");
679 for I in 1..520 loop
680 if WMsg(I) /= Msg(I) then
681 Put_Line("At " & Integer'Image(I) & " WMsg is " &
682 Unsigned_8'Image(WMsg(I)) & " while Msg is " &
683 Unsigned_8'Image(Msg(I)));
684 end if;
685 end loop;
686 else
687 Put_Line("PASS: read+write of RSA keys to RSA message.");
688 end if;
689 end Serialize_Keyset_RSA;
690
691 procedure Serialize_Keyset_SS is
692 Msg : Serpent_Msg;
693 RMsg : RSA_Msg;
694 KSet : Serpent_Keyset(5);
695 LSB : Interfaces.Unsigned_8 := 16#01#;
696 MSB : Interfaces.Unsigned_8 := 16#80#;
697 LMSB : Interfaces.Unsigned_8 := 16#81#;
698 Counter : Interfaces.Unsigned_16 := 101;
699 NewSetS : Serpent_Keyset;
700 NewSetR : Serpent_Keyset;
701 NewCounterR: Interfaces.Unsigned_16:=0;
702 NewCounterS: Interfaces.Unsigned_16:=0;
703 begin
704 Put_Line("Generating the Serpent Keys...");
705 -- fill a set of Serpent Keys
706 for I in 1..KSet.N loop
707 RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I)));
708 end loop;
709 KSet.Flag := LSB;
710
711 Put_Line("Writing the keys to messages...");
712 -- write keyset to serpent & rsa messages
713 Write_SKeys_SMsg( KSet, Counter, RNG_PAD, Msg );
714 Write_SKeys_RMsg( KSet, Counter, RNG_PAD, RMsg );
715
716 Put_Line("Reading keys back from messages...");
717 -- read keyset from serpent and rsa messages
718 Read_SKeys_SMsg( Msg, NewCounterS, NewSetS );
719 Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR );
720
721 Put_Line("Comparing the keysets...");
722 -- compare the two keysets
723 if NewCounterS /= Counter or NewCounterS /= Counter or
724 NewSetS /= KSet or NewSetR /= KSet then
725 Put_Line("FAIL: keysets are different!");
726 else
727 Put_Line("PASS: keysets are the same!");
728 end if;
729
730 Put_Line("Attempting to read from mangled message");
731 begin
732 Msg(Msg'First) := Msg(Msg'First)+25;
733 Read_SKeys_SMsg( Msg, Counter, NewSetS);
734 Put_Line("FAIL: read failed to raise invalid message exception!");
735 exception
736 when Invalid_Msg =>
737 Put_Line("PASS: exception correctly raised for invalid message");
738 end;
739 end Serialize_Keyset_SS;
740
741 procedure Serialize_Keys_Mgm is
742 N_Burnt : Counter_8bits;
743 Counter : Interfaces.Unsigned_16 := 16#EDA9#;
744 Mgm_S : Keys_Mgm;
745 Mgm_R : Keys_Mgm;
746 Cnt_S : Interfaces.Unsigned_16:=0;
747 Cnt_R : Interfaces.Unsigned_16:=0;
748 O1 : Octets_1;
749 SMsg : Serpent_Msg;
750 RMsg : RSA_Msg;
751 begin
752 -- fill the struct with random stuff
753 RNG.Get_Octets( O1 );
754 N_Burnt := Cast(O1);
755 declare
756 Mgm: Keys_Mgm(N_Burnt);
757 begin
758 RNG.Get_Octets( O1 );
759 Mgm.N_Server := O1(O1'First);
760 RNG.Get_Octets( O1 );
761 Mgm.N_Client := O1(O1'First);
762 RNG.Get_Octets( O1 );
763 Mgm.Key_ID := O1(O1'First);
764 if N_Burnt > 0 then
765 RNG.Get_Octets( Mgm.Burnt );
766 end if;
767 -- write it to Serpent and RSA messages
768 Write_KMgm_SMsg(Mgm, Counter, RNG_PAD, SMsg);
769 Write_KMgm_RMsg(Mgm, Counter, RNG_PAD, RMsg);
770
771 -- read it back from Serpent and RSA messages
772 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S );
773 Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R );
774
775 -- check results
776 if Cnt_S /= Counter or
777 Mgm_S.N_Burnt /= Mgm.N_Burnt or
778 Mgm_S /= Mgm then
779 Put_Line("FAIL: read/write key management struct to S msg.");
780 else
781 Put_Line("PASS: read/write key management struct to S msg.");
782 end if;
783
784 if Cnt_R /= Counter or
785 Mgm_R.N_Burnt /= Mgm.N_Burnt or
786 Mgm_R /= Mgm then
787 Put_Line("FAIL: read/write key management struct to R msg.");
788 Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R));
789 Put_Line("Counter is " & Unsigned_16'Image(Counter));
790 Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt));
791 Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt));
792 else
793 Put_Line("PASS: read/write key management struct to R msg.");
794 end if;
795
796 -- attempt mangled call - should raise exception
797 begin
798 SMsg(SMsg'First) := SMsg(SMsg'First) + 1;
799 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S);
800 Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!");
801 exception
802 when Invalid_Msg =>
803 Put_Line("PASS: Read_KMgm_SMsg correctly raised exception.");
804 end;
805
806 end;
807
808 end Serialize_Keys_Mgm;
809
810 procedure Serialize_File_Request( Reps: in Positive) is
811 MaxSz: Positive := 340;
812 O2 : Raw_Types.Octets_2;
813 U16 : Interfaces.Unsigned_16;
814 Counter : Interfaces.Unsigned_16;
815 ReadCounter: Interfaces.Unsigned_16;
816 Written: Natural;
817 Msg : Raw_Types.Serpent_Msg;
818 F_No, Sz: Text_Len;
819 begin
820 for I in 1 .. Reps loop
821 -- generate a random size
822 RNG.Get_Octets( O2 );
823 U16 := Raw_Types.Cast( O2 );
824 Sz := Text_Len( Positive(U16) mod MaxSz + 1);
825
826 -- generate a random number of files
827 RNG.Get_Octets( O2 );
828 U16 := Raw_Types.Cast( O2 );
829 -- make sure it's within Positive range i.e. not 0
830 if U16 = 0 then
831 U16 := 1;
832 end if;
833 F_No := Text_Len( Positive(U16) mod Sz + 1 );
834
835 declare
836 FR: Filenames(F_No, Sz);
837 ReadFR: Filenames;
838 O : Octets(1..Sz);
839 Len : Positive := Sz / F_No;
840 begin
841 Put_Line("Generating test for File Request with " &
842 Integer'Image(FR.F_No) & " filenames.");
843 -- generate a random counter
844 RNG.Get_Octets(O2);
845 Counter := Raw_Types.Cast(O2);
846
847 -- fill FR
848 RNG.Get_Octets( O );
849 -- replace separators if any
850 for I in O'Range loop
851 if O(I) = 59 then
852 O(I) := 69;
853 end if;
854 end loop;
855
856 Octets_To_String( O, FR.S );
857 for I in FR.Starts'Range loop
858 FR.Starts(I) := Interfaces.Unsigned_16( FR.Starts'First +
859 Len*(I-FR.Starts'First));
860 end loop;
861
862 -- write FR to message
863 Write_File_Request(FR, Counter, RNG_PAD, Msg, Written);
864
865 -- check how many filenames were written
866 if Written /= FR.F_No then
867 Put_Line("FAIL: only " & Natural'Image(Written) &
868 " filenames written out of " & Natural'Image(FR.F_No));
869 else
870 Put_Line("PASS: wrote " & Natural'Image(Written) & " filenames.");
871 end if;
872
873 -- read it from message and check result
874 Read_File_Request(Msg, ReadCounter, ReadFR);
875 if ReadCounter /= Counter then
876 Put_Line("FAIL: ReadCounter is " & Unsigned_16'Image(ReadCounter) &
877 " instead of expected " & Unsigned_16'Image(Counter) );
878 else
879 Put_Line("PASS: ReadCounter was read correctly as " &
880 Unsigned_16'Image(ReadCounter));
881 end if;
882 if ReadFr.Sz /= FR.Sz then
883 Put_Line("FAIL: Read FR.Sz = " & Text_Len'Image(ReadFr.Sz) &
884 " while expected sz is " & Text_Len'Image(FR.Sz));
885 else
886 Put_Line("PASS: Read FR.Sz as expected = " &
887 Text_Len'Image(ReadFr.Sz));
888 end if;
889 if ReadFr /= FR then
890 Put_Line("FAIL: ReadFr different from FR.");
891 else
892 Put_Line("PASS: ReadFr as expected, same as FR.");
893 end if;
894 end;
895 end loop;
896
897 -- test with more files than can fit
898 Put_Line("Test case for File Request with more files than fit.");
899 declare
900 F: Filenames(4, 16384);
901 ReadF: Filenames;
902 begin
903 F.Starts(1) := Interfaces.Unsigned_16(F.S'First);
904 F.Starts(2) := Interfaces.Unsigned_16(F.S'First + 342);
905 F.Starts(3) := 16370;
906 F.Starts(4) := 16380;
907 Write_File_Request(F, Counter, RNG_PAD, Msg, Written);
908 if Written /= 1 then
909 Put_Line("FAIL: Written is " & Natural'Image(Written) &
910 " instead of expected 1.");
911 else
912 Put_Line("PASS: Written is 1 out of 4, as expected.");
913 end if;
914 Read_File_Request(Msg, ReadCounter, ReadF);
915 if ReadF.F_No /= 1 or ReadF.Starts(1) /= 1 or ReadF.Sz /= 342 then
916 Put_Line("FAIL: F_No is " & Text_Len'Image(ReadF.F_No) &
917 " Sz is " & Text_Len'Image(ReadF.Sz));
918 else
919 Put_line("PASS: written 1 out of 4 correctly.");
920 end if;
921 end;
922 end Serialize_File_Request;
923
924 procedure Serialize_File_Chunk is
925 Filename : String := "afile.png";
926 FC : File_Chunk( Len => 945,
927 Count => 0,
928 Name_Len => Filename'Length);
929 ReadFC : File_Chunk;
930 Msg : Raw_Types.Serpent_Msg;
931 Pad : Raw_Types.Octets_8 := RNG_PAD;
932 begin
933 -- fill FC with random content
934 FC.Filename := Filename;
935 RNG.Get_Octets(FC.Content);
936
937 -- write FC to message with random padding
938 Write_File_Transfer( FC, Pad, Msg );
939
940 -- read FC and check
941 Read_File_Transfer( Msg, ReadFC );
942
943 if FC /= ReadFC then
944 Put_Line("FAIL: read/write file chunk.");
945 else
946 Put_Line("PASS: read/write file chunk.");
947 end if;
948 end Serialize_File_Chunk;
949
950 procedure Serialize_Action is
951 O2 : Raw_Types.Octets_2;
952 U16: Interfaces.Unsigned_16;
953 Len: Raw_Types.Text_Len;
954 Counter: Interfaces.Unsigned_16;
955 begin
956 Put_Line("Generating a random action for testing.");
957 -- generate random counter
958 RNG.Get_Octets( O2 );
959 Counter := Raw_Types.Cast( O2 );
960
961 -- generate action length
962 RNG.Get_Octets( O2 );
963 U16 := Raw_Types.Cast( O2 );
964 if U16 < 1 then
965 U16 := 1;
966 else
967 if U16 + 5 > Raw_Types.Serpent_Msg'Length then
968 U16 := Raw_Types.Serpent_Msg'Length - 5;
969 end if;
970 end if;
971 Len := Raw_Types.Text_Len( U16 );
972
973 declare
974 A: Raw_Types.Text_Octets( Len );
975 B: Raw_Types.Text_Octets;
976 Msg: Raw_Types.Serpent_Msg;
977 ReadC : Interfaces.Unsigned_16;
978 begin
979 RNG.Get_Octets( A.Content );
980 begin
981 Write_Action( A, Counter, RNG_PAD, Msg );
982 Read_Action( Msg, ReadC, B );
983 if B /= A then
984 Put_Line("FAIL: read/write of Action.");
985 else
986 Put_Line("PASS: read/write of Action.");
987 end if;
988 exception
989 when Invalid_Msg =>
990 if Len + 5 > Raw_Types.Serpent_Msg'Length then
991 Put_Line("PASS: exception correctly raised for Action too long");
992 else
993 Put_Line("FAIL: exception INCORRECTLY raised at action r/w!");
994 end if;
995 end;
996 end;
997 end Serialize_Action;
998
999 procedure Converter_String_Octets is
1000 Len: constant Natural := 234;
1001 -- original values
1002 S: String(1..Len);
1003 O: Octets(1..Len);
1004 -- converted values
1005 CS: String(1..Len);
1006 CO: Octets(1..Len);
1007 begin
1008 -- original octets
1009 RNG.Get_Octets(O);
1010 Octets_To_String(O, CS);
1011 String_To_Octets(CS, CO);
1012 if CO /= O then
1013 Put_Line("FAIL: octets different after string/octets conversion.");
1014 else
1015 Put_Line("PASS: octets same after string/octets conversion.");
1016 end if;
1017
1018 -- original string
1019 for I in S'Range loop
1020 S(I) := Character'Val(I mod 12);
1021 end loop;
1022 String_To_Octets(S, CO);
1023 Octets_To_String(CO, CS);
1024 if CS /= S then
1025 Put_Line("FAIL: string different after string/octets conversion.");
1026 else
1027 Put_Line("PASS: string same after string/octets conversion.");
1028 end if;
1029 end Converter_String_Octets;
1030
1031 procedure Test_Padding is
1032 Msg : Raw_Types.Serpent_Msg := (others => 12);
1033 Old : Raw_Types.Serpent_Msg := Msg;
1034 Pos : Natural := 16;
1035 NewPos : Natural := Pos;
1036 Counter : Interfaces.Unsigned_16;
1037 U16 : Interfaces.Unsigned_16;
1038 O2 : Raw_Types.Octets_2;
1039 Pad : Raw_Types.Octets_8;
1040 Pass : Boolean;
1041 begin
1042 -- get random counter
1043 RNG.Get_Octets( O2 );
1044 Counter := Raw_Types.Cast( O2 );
1045
1046 -- test with random padding
1047 Pad := RNG_PAD;
1048 Write_End( Msg, NewPos, Counter, Pad );
1049 -- check NewPos and counter
1050 Pass := True;
1051 if NewPos /= Msg'Last + 1 then
1052 Put_Line("FAIL: incorrect Pos value after Write_End with rng.");
1053 Pass := False;
1054 end if;
1055 Read_U16(Msg, Pos, U16);
1056 if U16 /= Counter then
1057 Put_Line("FAIL: incorrect Counter by Write_End with rng.");
1058 Pass := False;
1059 end if;
1060 -- check that the padding is at least different...
1061 if Msg(Pos..Msg'Last) = Old(Pos..Old'Last) or
1062 Msg(Pos..Pos+Pad'Length-1) = Pad then
1063 Put_Line("FAIL: no padding written by Write_End with rng.");
1064 Pass := False;
1065 end if;
1066 if Pass then
1067 Put_Line("PASS: Write_End with rng.");
1068 end if;
1069
1070 -- prepare for the next test
1071 Pass := True;
1072 Pos := Pos - 2;
1073 NewPos := Pos;
1074 Msg := Old;
1075
1076 -- get random padding
1077 RNG.Get_Octets( Pad );
1078
1079 -- write with fixed padding and check
1080 Write_End( Msg, NewPos, Counter, Pad );
1081 Pass := True;
1082
1083 if NewPos = Msg'Last + 1 then
1084 -- check counter + padding
1085 Read_U16( Msg, Pos, U16 );
1086 if U16 /= Counter then
1087 Put_Line("FAIL: Counter was not written by Write_End.");
1088 Pass := False;
1089 end if;
1090 for I in Pos..Msg'Last loop
1091 if Msg( I ) /= Pad( Pad'First + (I - Pos) mod Pad'Length ) then
1092 Put_Line("FAIL: Msg(" & Natural'Image(I) & ")=" &
1093 Unsigned_8'Image(Msg(I)) & " /= Pad(" &
1094 Natural'Image(Pad'First+(I-Pos) mod Pad'Length) &
1095 ") which is " &
1096 Unsigned_8'Image(Pad(Pad'First+(I-Pos) mod Pad'Length)));
1097 Pass := False;
1098 end if;
1099 end loop;
1100 else
1101 Put_Line("FAIL: Pos is wrong after call to Write_End.");
1102 Pass := False;
1103 end if;
1104 if Pass then
1105 Put_Line("PASS: test for Write_End with fixed padding.");
1106 end if;
1107 end Test_Padding;
1108
1109 end Messages.Test_Serializing;
1110
-
+ 2F33B454A28B303E34077B17360BDECEAA095DD336F286C26046DA82AB4395C2F783A3AB3DEE64C87FFD088728B4721196E20154F15B71BA704DA0F7D6F9CAB7
smg_comms/tests/messages-test_serializing.ads
(0 . 0)(1 . 20)
1115 -- Tests for the serialization of data structures in SMG Protocol
1116 -- S.MG, 2018
1117
1118 with Raw_Types; use Raw_Types;
1119
1120 package Messages.Test_Serializing is
1121
1122 procedure Serialize_Keyset_RSA;
1123 procedure Serialize_Keyset_SS;
1124 procedure Serialize_Keys_Mgm;
1125 procedure Serialize_File_Request( Reps: in Positive );
1126 procedure Serialize_File_Chunk;
1127 procedure Serialize_Action;
1128
1129 procedure Converter_String_Octets;
1130 procedure Test_Padding;
1131
1132 private
1133
1134 end Messages.Test_Serializing;
- 60BA5DFFA63414F5B2D28611DA978EF0CDAACCAEEBA3383699E8EBFF223F608BED0F6752411067CF8A40BBB209040FCC621557BCF771F2459268ABE0EDCDC6A9
+
smg_comms/tests/test_serializing.adb
(1 . 304)(0 . 0)
1139 -- Tests for the serialization of data structures in SMG Protocol
1140 -- S.MG, 2018
1141
1142 with RNG;
1143 with Data_Structs; use Data_Structs;
1144 with Messages; use Messages;
1145 with Interfaces; use Interfaces;
1146 with System;
1147 with System.Storage_Elements; use System.Storage_Elements;
1148 with Ada.Text_IO; use Ada.Text_IO;
1149
1150 package body Test_Serializing is
1151
1152 procedure Serialize_Keyset_SS is
1153 Msg : Serpent_Msg;
1154 RMsg : RSA_Msg;
1155 KSet : Serpent_Keyset(5);
1156 LSB : Interfaces.Unsigned_8 := 16#01#;
1157 MSB : Interfaces.Unsigned_8 := 16#80#;
1158 LMSB : Interfaces.Unsigned_8 := 16#81#;
1159 Counter : Interfaces.Unsigned_16 := 101;
1160 NewSetS : Serpent_Keyset;
1161 NewSetR : Serpent_Keyset;
1162 NewCounterR: Interfaces.Unsigned_16:=0;
1163 NewCounterS: Interfaces.Unsigned_16:=0;
1164 begin
1165 Put_Line("Generating the Serpent Keys...");
1166 -- fill a set of Serpent Keys
1167 for I in 1..KSet.N loop
1168 RNG.Get_Octets(KSet.Keys(Interfaces.Unsigned_8(I)));
1169 end loop;
1170 KSet.Flag := LSB;
1171
1172 Put_Line("Writing the keys to messages...");
1173 -- write keyset to serpent & rsa messages
1174 Write_SKeys_SMsg( KSet, Counter, Msg );
1175 Write_SKeys_RMsg( KSet, Counter, RMsg );
1176
1177 Put_Line("Reading keys back from messages...");
1178 -- read keyset from serpent and rsa messages
1179 Read_SKeys_SMsg( Msg, NewCounterS, NewSetS );
1180 Read_SKeys_RMsg( RMsg, NewCounterR, NewSetR );
1181
1182 Put_Line("Comparing the keysets...");
1183 -- compare the two keysets
1184 if NewCounterS /= Counter or NewCounterS /= Counter or
1185 NewSetS /= KSet or NewSetR /= KSet then
1186 Put_Line("FAIL: keysets are different!");
1187 else
1188 Put_Line("PASS: keysets are the same!");
1189 end if;
1190
1191 Put_Line("Attempting to read from mangled message");
1192 begin
1193 Msg(Msg'First) := Msg(Msg'First)+25;
1194 Read_SKeys_SMsg( Msg, Counter, NewSetS);
1195 Put_Line("FAIL: read failed to raise invalid message exception!");
1196 exception
1197 when Invalid_Msg =>
1198 Put_Line("PASS: exception correctly raised for invalid message");
1199 end;
1200 end Serialize_Keyset_SS;
1201
1202 procedure Serialize_Keys_Mgm is
1203 N_Burnt : Counter_8bits;
1204 Counter : Interfaces.Unsigned_16 := 16#EDA9#;
1205 Mgm_S : Keys_Mgm;
1206 Mgm_R : Keys_Mgm;
1207 Cnt_S : Interfaces.Unsigned_16:=0;
1208 Cnt_R : Interfaces.Unsigned_16:=0;
1209 O1 : Octets_1;
1210 SMsg : Serpent_Msg;
1211 RMsg : RSA_Msg;
1212 begin
1213 -- fill the struct with random stuff
1214 RNG.Get_Octets( O1 );
1215 N_Burnt := Cast(O1);
1216 declare
1217 Mgm: Keys_Mgm(N_Burnt);
1218 begin
1219 RNG.Get_Octets( O1 );
1220 Mgm.N_Server := O1(O1'First);
1221 RNG.Get_Octets( O1 );
1222 Mgm.N_Client := O1(O1'First);
1223 RNG.Get_Octets( O1 );
1224 Mgm.Key_ID := O1(O1'First);
1225 if N_Burnt > 0 then
1226 RNG.Get_Octets( Mgm.Burnt );
1227 end if;
1228 -- write it to Serpent and RSA messages
1229 Write_KMgm_SMsg(Mgm, Counter, SMsg);
1230 Write_KMgm_RMsg(Mgm, Counter, RMsg);
1231
1232 -- read it back from Serpent and RSA messages
1233 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S );
1234 Read_KMgm_RMsg( RMsg, Cnt_R, Mgm_R );
1235
1236 -- check results
1237 if Cnt_S /= Counter or
1238 Mgm_S.N_Burnt /= Mgm.N_Burnt or
1239 Mgm_S /= Mgm then
1240 Put_Line("FAIL: read/write key management struct to S msg.");
1241 else
1242 Put_Line("PASS: read/write key management struct to S msg.");
1243 end if;
1244
1245 if Cnt_R /= Counter or
1246 Mgm_R.N_Burnt /= Mgm.N_Burnt or
1247 Mgm_R /= Mgm then
1248 Put_Line("FAIL: read/write key management struct to R msg.");
1249 Put_Line("Cnt_R is " & Unsigned_16'Image(Cnt_R));
1250 Put_Line("Counter is " & Unsigned_16'Image(Counter));
1251 Put_Line("Mgm_R.N_Burnt is " & Counter_8bits'Image(Mgm_R.N_Burnt));
1252 Put_Line("Mgm.N_Burnt is " & Counter_8bits'Image(Mgm.N_Burnt));
1253 else
1254 Put_Line("PASS: read/write key management struct to R msg.");
1255 end if;
1256
1257 -- attempt mangled call - should raise exception
1258 begin
1259 SMsg(SMsg'First) := SMsg(SMsg'First) + 1;
1260 Read_KMgm_SMsg( SMsg, Cnt_S, Mgm_S);
1261 Put_Line("FAIL: Read_KMgm_SMsg failed to raise exception!");
1262 exception
1263 when Invalid_Msg =>
1264 Put_Line("PASS: Read_KMgm_SMsg correctly raised exception.");
1265 end;
1266
1267 end;
1268
1269 end Serialize_Keys_Mgm;
1270
1271 procedure Serialize_File_Request( Reps: in Positive) is
1272 MaxSz: Positive := 340;
1273 O2 : Raw_Types.Octets_2;
1274 U16 : Interfaces.Unsigned_16;
1275 Counter : Interfaces.Unsigned_16;
1276 ReadCounter: Interfaces.Unsigned_16;
1277 Written: Natural;
1278 Msg : Raw_Types.Serpent_Msg;
1279 F_No, Sz: Text_Len;
1280 begin
1281 for I in 1 .. Reps loop
1282 -- generate a random size
1283 RNG.Get_Octets( O2 );
1284 U16 := Raw_Types.Cast( O2 );
1285 Sz := Text_Len( Positive(U16) mod MaxSz + 1);
1286
1287 -- generate a random number of files
1288 RNG.Get_Octets( O2 );
1289 U16 := Raw_Types.Cast( O2 );
1290 F_No := Text_Len( Positive(U16) mod Sz );
1291 if F_No = 0 then
1292 F_No := 1;
1293 end if;
1294
1295 declare
1296 FR: Filenames(F_No, Sz);
1297 ReadFR: Filenames;
1298 O : Octets(1..Sz);
1299 Len : Positive := Sz / F_No;
1300 begin
1301 Put_Line("Generating test for File Request with " &
1302 Integer'Image(FR.F_No) & " filenames.");
1303 -- generate a random counter
1304 RNG.Get_Octets(O2);
1305 Counter := Raw_Types.Cast(O2);
1306
1307 -- fill FR
1308 RNG.Get_Octets( O );
1309 -- replace separators if any
1310 for I in O'Range loop
1311 if O(I) = 59 then
1312 O(I) := 69;
1313 end if;
1314 end loop;
1315
1316 Octets_To_String( O, FR.S );
1317 for I in FR.Starts'Range loop
1318 FR.Starts(I) := Interfaces.Unsigned_16( FR.Starts'First +
1319 Len*(I-FR.Starts'First));
1320 end loop;
1321
1322 -- write FR to message
1323 Write_File_Request(FR, Counter, Msg, Written);
1324
1325 -- check how many filenames were written
1326 if Written /= FR.F_No then
1327 Put_Line("FAIL: only " & Natural'Image(Written) &
1328 " filenames written out of " & Natural'Image(FR.F_No));
1329 else
1330 Put_Line("PASS: wrote " & Natural'Image(Written) & " filenames.");
1331 end if;
1332
1333 -- read it from message and check result
1334 Read_File_Request(Msg, ReadCounter, ReadFR);
1335 if ReadCounter /= Counter then
1336 Put_Line("FAIL: ReadCounter is " & Unsigned_16'Image(ReadCounter) &
1337 " instead of expected " & Unsigned_16'Image(Counter) );
1338 else
1339 Put_Line("PASS: ReadCounter was read correctly as " &
1340 Unsigned_16'Image(ReadCounter));
1341 end if;
1342 if ReadFr.Sz /= FR.Sz then
1343 Put_Line("FAIL: Read FR.Sz = " & Text_Len'Image(ReadFr.Sz) &
1344 " while expected sz is " & Text_Len'Image(FR.Sz));
1345 else
1346 Put_Line("PASS: Read FR.Sz as expected = " &
1347 Text_Len'Image(ReadFr.Sz));
1348 end if;
1349 if ReadFr /= FR then
1350 Put_Line("FAIL: ReadFr different from FR.");
1351 else
1352 Put_Line("PASS: ReadFr as expected, same as FR.");
1353 end if;
1354 end;
1355 end loop;
1356
1357 -- test with more files than can fit
1358 Put_Line("Test case for File Request with more files than fit.");
1359 declare
1360 F: Filenames(4, 16384);
1361 ReadF: Filenames;
1362 begin
1363 F.Starts(1) := Interfaces.Unsigned_16(F.S'First);
1364 F.Starts(2) := Interfaces.Unsigned_16(F.S'First + 342);
1365 F.Starts(3) := 16370;
1366 F.Starts(4) := 16380;
1367 Write_File_Request(F, Counter, Msg, Written);
1368 if Written /= 1 then
1369 Put_Line("FAIL: Written is " & Natural'Image(Written) &
1370 " instead of expected 1.");
1371 else
1372 Put_Line("PASS: Written is 1 out of 4, as expected.");
1373 end if;
1374 Read_File_Request(Msg, ReadCounter, ReadF);
1375 if ReadF.F_No /= 1 or ReadF.Starts(1) /= 1 or ReadF.Sz /= 342 then
1376 Put_Line("FAIL: F_No is " & Text_Len'Image(ReadF.F_No) &
1377 " Sz is " & Text_Len'Image(ReadF.Sz));
1378 else
1379 Put_line("PASS: written 1 out of 4 correctly.");
1380 end if;
1381 end;
1382 end Serialize_File_Request;
1383
1384 procedure Serialize_File_Chunk is
1385 Filename : String := "afile.png";
1386 FC : File_Chunk( Len => 945,
1387 Count => 0,
1388 Name_Len => Filename'Length);
1389 ReadFC : File_Chunk;
1390 Msg : Raw_Types.Serpent_Msg;
1391 begin
1392 -- fill FC with random content
1393 FC.Filename := Filename;
1394 RNG.Get_Octets(FC.Content);
1395
1396 -- write FC to message
1397 Write_File_Transfer( FC, Msg );
1398
1399 -- read FC and check
1400 Read_File_Transfer( Msg, ReadFC );
1401
1402 if FC /= ReadFC then
1403 Put_Line("FAIL: read/write file chunk.");
1404 else
1405 Put_Line("PASS: read/write file chunk.");
1406 end if;
1407 end Serialize_File_Chunk;
1408
1409 procedure Converter_String_Octets is
1410 Len: constant Natural := 234;
1411 -- original values
1412 S: String(1..Len);
1413 O: Octets(1..Len);
1414 -- converted values
1415 CS: String(1..Len);
1416 CO: Octets(1..Len);
1417 begin
1418 -- original octets
1419 RNG.Get_Octets(O);
1420 Octets_To_String(O, CS);
1421 String_To_Octets(CS, CO);
1422 if CO /= O then
1423 Put_Line("FAIL: octets different after string/octets conversion.");
1424 else
1425 Put_Line("PASS: octets same after string/octets conversion.");
1426 end if;
1427
1428 -- original string
1429 for I in S'Range loop
1430 S(I) := Character'Val(I mod 12);
1431 end loop;
1432 String_To_Octets(S, CO);
1433 Octets_To_String(CO, CS);
1434 if CS /= S then
1435 Put_Line("FAIL: string different after string/octets conversion.");
1436 else
1437 Put_Line("PASS: string same after string/octets conversion.");
1438 end if;
1439 end Converter_String_Octets;
1440
1441 end Test_Serializing;
1442
- 77DFBF402E60B9CD4D8838421571BC7D018078B66C4B7D54C40BCAE20CD5554072704D6476659F19F8DD4BA82A3A006F1E0B5B2C2228AAA0ABAAA87C338C025A
+
smg_comms/tests/test_serializing.ads
(1 . 17)(0 . 0)
1447 -- Tests for the serialization of data structures in SMG Protocol
1448 -- S.MG, 2018
1449
1450 with Raw_Types; use Raw_Types;
1451
1452 package Test_Serializing is
1453
1454 procedure Serialize_Keyset_SS;
1455 procedure Serialize_Keys_Mgm;
1456 procedure Serialize_File_Request( Reps: in Positive );
1457 procedure Serialize_File_Chunk;
1458
1459 procedure Converter_String_Octets;
1460
1461 private
1462
1463 end Test_Serializing;
- 8CCD7A252824F3A0BA71E0B69FB36B7347146EA1B64AA0B297799717384C3FF1E954792EC2C4E1C762252A0C96AE46E438FE4CD541FA5A477C29B11C991B5775
+ 4FCBE6D5F83C385C895668661F49EC5994D54B274866F52ED032815938CE3B3CDF94D8DFC18523AAA45AD17F9D691263AA6EF4080FB8CC55F2184217AF041F4A
smg_comms/tests/testall.adb
(3 . 7)(3 . 7)
1468 with Test_Serpent;
1469 with Test_Packing;
1470 with Test_RSA_OAEP;
1471 with Test_Serializing;
1472 with Messages.Test_Serializing;
1473
1474 procedure testall is
1475 begin
(14 . 9)(14 . 12)
1477 Test_RSA_OAEP.test_rsa_oaep;
1478 Test_Packing.Test_Pack_Unpack_Serpent;
1479 Test_Packing.Test_Pack_Unpack_RSA;
1480 Test_Serializing.Serialize_Keyset_SS;
1481 Test_Serializing.Serialize_Keys_Mgm;
1482 Test_Serializing.Serialize_File_Chunk;
1483 Test_Serializing.Serialize_File_Request(5);
1484 Test_Serializing.Converter_String_Octets;
1485 Messages.Test_Serializing.Serialize_Keyset_SS;
1486 Messages.Test_Serializing.Serialize_Keyset_RSA;
1487 Messages.Test_Serializing.Serialize_Keys_Mgm;
1488 Messages.Test_Serializing.Serialize_File_Chunk;
1489 Messages.Test_Serializing.Serialize_File_Request(5);
1490 Messages.Test_Serializing.Serialize_Action;
1491 Messages.Test_Serializing.Converter_String_Octets;
1492 Messages.Test_Serializing.Test_Padding;
1493 end testall;