@@ -671,25 +671,25 @@ def test_encode_wireline_command_device_id_limit(self):
671
671
secplus .encode_wireline_command (rolling , device_id , command , payload )
672
672
self .assertIn (str (cm .exception ), ["Device ID must be less than 2^40" , "Invalid input" ])
673
673
674
- def test_encode_wireline_command_limit (self ):
674
+ def test_encode_wireline_command_command_limit (self ):
675
675
rolling = 2 ** 28 - 1
676
676
device_id = 2 ** 40 - 1
677
677
command = 2 ** 12
678
678
payload = 2 ** 20 - 1
679
679
680
680
with self .assertRaises (ValueError ) as cm :
681
681
secplus .encode_wireline_command (rolling , device_id , command , payload )
682
- self .assertEqual (str (cm .exception ), "Command must be less than 2^12" )
682
+ self .assertIn (str (cm .exception ), [ "Command must be less than 2^12" , "Invalid input" ] )
683
683
684
- def test_encode_wireline_payload_limit (self ):
684
+ def test_encode_wireline_command_payload_limit (self ):
685
685
rolling = 2 ** 28 - 1
686
686
device_id = 2 ** 40 - 1
687
687
command = 2 ** 12 - 1
688
688
payload = 2 ** 20
689
689
690
690
with self .assertRaises (ValueError ) as cm :
691
691
secplus .encode_wireline_command (rolling , device_id , command , payload )
692
- self .assertEqual (str (cm .exception ), "Payload value must be less than 2^20" )
692
+ self .assertIn (str (cm .exception ), [ "Payload value must be less than 2^20" , "Invalid input" ] )
693
693
694
694
def test_decode_wireline_command_bits_8_9 (self ):
695
695
for code in self .wireline_codes :
@@ -741,6 +741,8 @@ def substitute_c():
741
741
libsecplus .decode_v2 .restype = c_int8
742
742
libsecplus .encode_wireline .restype = c_int8
743
743
libsecplus .decode_wireline .restype = c_int8
744
+ libsecplus .encode_wireline_command .restype = c_int8
745
+ libsecplus .decode_wireline_command .restype = c_int8
744
746
745
747
def encode (rolling , fixed ):
746
748
if rolling >= 2 ** 32 :
@@ -845,6 +847,33 @@ def decode_wireline(code):
845
847
846
848
secplus .decode_wireline = decode_wireline
847
849
850
+ def encode_wireline_command (rolling , device_id , command , payload ):
851
+ packet = create_string_buffer (os .urandom (19 ), 19 )
852
+ err = libsecplus .encode_wireline_command (c_uint32 (rolling ), c_uint64 (device_id ), c_uint16 (command ), c_uint32 (payload ), packet )
853
+ if err < 0 :
854
+ raise ValueError ("Invalid input" )
855
+ return packet .raw
856
+
857
+ secplus .encode_wireline_command = encode_wireline_command
858
+
859
+ def decode_wireline_command (code ):
860
+ if not isinstance (code , bytes ):
861
+ raise ValueError ("Input must be bytes" )
862
+ if len (code ) != 19 :
863
+ raise ValueError ("Input must be 19 bytes long" )
864
+
865
+ rolling = c_uint32 ()
866
+ device_id = c_uint64 ()
867
+ command = c_uint16 ()
868
+ payload = c_uint32 ()
869
+
870
+ err = libsecplus .decode_wireline_command (code , byref (rolling ), byref (device_id ), byref (command ), byref (payload ))
871
+ if err < 0 :
872
+ raise ValueError ("Invalid input" )
873
+ return rolling .value , device_id .value , command .value , payload .value
874
+
875
+ secplus .decode_wireline_command = decode_wireline_command
876
+
848
877
849
878
def substitute_avr ():
850
879
import subprocess
@@ -869,7 +898,7 @@ def encode(rolling, fixed):
869
898
secplus .encode = encode
870
899
871
900
def decode (code ):
872
- sim .stdin .write (bytes ([5 ]))
901
+ sim .stdin .write (bytes ([6 ]))
873
902
sim .stdin .write (bytes (code ))
874
903
sim .stdin .flush ()
875
904
err , rolling , fixed = struct .unpack ("<BLL" , sim .stdout .read (9 ))
@@ -909,7 +938,7 @@ def encode_v2(rolling, fixed, data=None):
909
938
secplus .encode_v2 = encode_v2
910
939
911
940
def decode_v2 (code ):
912
- command = 6 if len (code ) == 80 else 7
941
+ command = 7 if len (code ) == 80 else 8
913
942
914
943
code_bytes = []
915
944
for offset in range (0 , len (code ), 8 ):
@@ -951,7 +980,7 @@ def decode_wireline(code):
951
980
if len (code ) != 19 :
952
981
raise ValueError ("Input must be 19 bytes long" )
953
982
954
- sim .stdin .write (bytes ([8 ]))
983
+ sim .stdin .write (bytes ([9 ]))
955
984
sim .stdin .write (code )
956
985
sim .stdin .flush ()
957
986
err , rolling , fixed , data = struct .unpack ("<BLQL" , sim .stdout .read (17 ))
@@ -962,6 +991,35 @@ def decode_wireline(code):
962
991
963
992
secplus .decode_wireline = decode_wireline
964
993
994
+ def encode_wireline_command (rolling , device_id , command , payload ):
995
+ sim .stdin .write (struct .pack ("<BLQHL" , 5 , rolling , device_id , command , payload ))
996
+ sim .stdin .flush ()
997
+ err = sim .stdout .read (1 )[0 ]
998
+ packet = sim .stdout .read (19 )
999
+
1000
+ if err != 0 :
1001
+ raise ValueError ("Invalid input" )
1002
+ return packet
1003
+
1004
+ secplus .encode_wireline_command = encode_wireline_command
1005
+
1006
+ def decode_wireline_command (code ):
1007
+ if not isinstance (code , bytes ):
1008
+ raise ValueError ("Input must be bytes" )
1009
+ if len (code ) != 19 :
1010
+ raise ValueError ("Input must be 19 bytes long" )
1011
+
1012
+ sim .stdin .write (bytes ([10 ]))
1013
+ sim .stdin .write (code )
1014
+ sim .stdin .flush ()
1015
+ err , rolling , device_id , command , payload = struct .unpack ("<BLQHL" , sim .stdout .read (19 ))
1016
+
1017
+ if err != 0 :
1018
+ raise ValueError ("Invalid input" )
1019
+ return rolling , device_id , command , payload
1020
+
1021
+ secplus .decode_wireline_command = decode_wireline_command
1022
+
965
1023
return sim
966
1024
967
1025
0 commit comments