2828from Crypto .Random import get_random_bytes
2929
3030# HMAC signs, encrypts, or decrypts payload string
31- # This is a really messy function for now
32- def sign_and_encrypt ( payload_s :str ,
33- sign :bool = True , sign_key_s :str = '00112233445566778899AABBCCDDEEFF' ,
34- enc :bool = True , enc_key_s :str = '00112233445566778899AABBCCDDEEFF' ,
35- denc :bool = False , denc_key_s :str = '71776572747975696f70617364666768' , denc_tag_s :str = '5d37d992dbc6fc795ea597ed7e8a6078' ):
36-
37- #print(f'{payload_s} | {sign} | {sign_key_s} | {enc} | {enc_key_s} | {denc} | {denc_key_s} | {denc_tag_s}\n')
31+ # This is a really messy function for now
32+
33+
34+ def sign_and_encrypt (payload_s : str ,
35+ sign : bool = True , sign_key_s : str = '00112233445566778899AABBCCDDEEFF' ,
36+ enc : bool = True , enc_key_s : str = '00112233445566778899AABBCCDDEEFF' ,
37+ denc : bool = False , denc_key_s : str = '71776572747975696f70617364666768' , denc_tag_s : str = '5d37d992dbc6fc795ea597ed7e8a6078' ):
38+
39+ # print(f'{payload_s} | {sign} | {sign_key_s} | {enc} | {enc_key_s} | {denc} | {denc_key_s} | {denc_tag_s}\n')
3840
3941 # TODO only convert what's needed in if statements below
4042 payload = bytes .fromhex (payload_s )
@@ -64,7 +66,8 @@ def sign_and_encrypt( payload_s:str,
6466 cipher = AES .new (enc_key , AES .MODE_GCM , nonce = iv )
6567 cipher .update (aad )
6668 ciphertext , tag = cipher .encrypt_and_digest (payload )
67- results [1 ] = (ciphertext .hex (), tag .hex (), '69c411276fecddc4780df42c8a2af89296fabf34d7fae700' , iv .hex ())
69+ results [1 ] = (ciphertext .hex (), tag .hex (),
70+ '69c411276fecddc4780df42c8a2af89296fabf34d7fae700' , iv .hex ())
6871
6972 if denc :
7073 cipher = AES .new (denc_key , AES .MODE_GCM , nonce = iv )
@@ -81,10 +84,10 @@ def add_bib_to_bundle_over_x(bundle, x):
8184 if x == 0 :
8285 sign = sign_and_encrypt (cbor2 .dumps (bundle [0 ]).hex (), sign = True , enc = False , denc = False )[0 ]
8386 asb = [
84- [x ], 1 , 1 , [2 , [2 , 1 ]],
85- [[1 , 7 ], [3 , 0 ]],
86- [[[1 , bytes .fromhex (sign )]]]
87- ]
87+ [x ], 1 , 1 , [2 , [2 , 1 ]],
88+ [[1 , 7 ], [3 , 0 ]],
89+ [[[1 , bytes .fromhex (sign )]]]
90+ ]
8891 buf = io .BytesIO ()
8992 for b in asb :
9093 cbor2 .dump (b , buf )
@@ -101,7 +104,7 @@ def add_bib_to_bundle_over_x(bundle, x):
101104 # find the target block
102105 if blk [1 ] == x :
103106
104- # get the HMAC signature
107+ # get the HMAC signature
105108 sign = sign_and_encrypt (blk [4 ], sign = True , enc = False , denc = False )[0 ]
106109
107110 # create ASB and cbor dump as BIB btsd
@@ -119,6 +122,7 @@ def add_bib_to_bundle_over_x(bundle, x):
119122
120123 return bundle
121124
125+
122126def add_bcb_to_bundle_over_x (bundle , x ):
123127 # find max blk num, we will make new BCB max+1. This guarantees the block num is free
124128 mx_blk_num = max (bundle , key = lambda blk : blk [1 ])[1 ]
@@ -133,9 +137,10 @@ def add_bcb_to_bundle_over_x(bundle, x):
133137 # create ASB and cbor dump as BCB btsd
134138 asb = [
135139 [x ], 2 , 1 , [2 , [2 , 1 ]],
136- # IV # wrapped key
137- [[1 , bytes .fromhex (ciphertext [3 ])], [2 , 1 ], [3 , bytes .fromhex (ciphertext [2 ])], [4 , 0 ]],
138- # ciphertext
140+ # IV # wrapped key
141+ [[1 , bytes .fromhex (ciphertext [3 ])], [2 , 1 ], [
142+ 3 , bytes .fromhex (ciphertext [2 ])], [4 , 0 ]],
143+ # ciphertext
139144 [[[1 , bytes .fromhex (ciphertext [1 ])]]]
140145 ]
141146 buf = io .BytesIO ()
@@ -150,14 +155,15 @@ def add_bcb_to_bundle_over_x(bundle, x):
150155
151156 return bundle
152157
158+
153159b = [
154160 [7 , 0 , 0 , [2 , [1 , 2 ]], [2 , [2 , 1 ]], [2 , [2 , 1 ]], [0 , 40 ], 1000000 ],
155161 [1 , 1 , 0 , 0 , '526561647920746F2067656E657261746520612033322D62797465207061796C6F6164' ],
156162]
157163
158164
159- print (f"ORIGINAL BUNDLE: { b } " )
165+ print (f"ORIGINAL BUNDLE: { b } " )
160166b = add_bib_to_bundle_over_x (b , 1 )
161167print (f'BUNDLE AFTER BIB: { b } ' )
162- #b = add_bcb_to_bundle_over_x(b, 1)
168+ # b = add_bcb_to_bundle_over_x(b, 1)
163169print (f'FINAL BUNDLE: { b } ' )
0 commit comments