diff --git a/c2pa/cai_tool.py b/c2pa/cai_tool.py index ef82998..97704b0 100644 --- a/c2pa/cai_tool.py +++ b/c2pa/cai_tool.py @@ -19,7 +19,6 @@ import hashlib import json import os -import sys import multibase import multihash @@ -48,7 +47,7 @@ def parse_json(fname): return output -def convert_to_hex(label, indent = 0, sec_indent = -1): +def convert_to_hex(label, indent=0, sec_indent=-1): '''Convert label into hexadecimals ''' if sec_indent == -1: @@ -130,7 +129,7 @@ def get_content_lbox(fname): data = parse_json(fname) t_box_size = len(convert_to_hex('json')) - + payload_size = len(convert_to_hex(data)) total_size = 4 + t_box_size + payload_size @@ -144,12 +143,12 @@ def get_uuid_content_box(): '''Generate l_box for size for uuid content ''' t_box_size = len(convert_to_hex('uuid')) - + data_hex = ['63', '61', '73', '67', '00', '11', '00', '10', '80', '00', '00', 'aa', '00', '38', '9b', '71'] data_hex_size = len(data_hex) # signature 1: placeholder signature data as mockup - # payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] + # payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] # noqa E501 # signature 2: real example signature payload_data = ['00', '87', '65', '0C', '94', '6D', 'EE', '53', '05', 'B2', 'D8', '87', '19', 'AD', '30', 'A9', '9C', 'AB', 'CF', '3D', 'A2', '00', 'C2', '3D', '61', '71', '0B', 'EB', 'E7', '24', 'D0', 'CD', @@ -256,7 +255,7 @@ def create_uuid_box(l_box): ''' t_box = convert_to_hex('uuid') data_hex = ['63', '61', '73', '67', '00', '11', '00', '10', '80', '00', '00', 'aa', '00', '38', '9b', '71'] - # payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] + # payload_data = ['73', '69', '67', '6e', '61', '74', '75', '72', '65', '20', '70', '6c', '61', '63', '65', '68', '6f', '6c', '64', '65', '72', '3a', '63', '62', '2e', '73', '74', '61', '72', '6c', '69', '6e', '67', '5f', '31', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20', '20'] # noqa E501 payload_data = ['00', '87', '65', '0C', '94', '6D', 'EE', '53', '05', 'B2', 'D8', '87', '19', 'AD', '30', 'A9', '9C', 'AB', 'CF', '3D', 'A2', '00', 'C2', '3D', '61', '71', '0B', 'EB', 'E7', '24', 'D0', 'CD', 'B1', 'CC', 'E5', '0C', '3A', '74', '26', '71', '5A', '86', '04', 'DB', '36', '55', 'C5', '30', @@ -302,9 +301,20 @@ def create_injection_block(cai_block, store_block, assertion_store, assertion, c header = ['FF', 'EB'] c_box = convert_to_hex('JP') - box_remain = ['00', '01', '00', '00', '00','01'] - - block = header + l_box + c_box + box_remain + cai_block + store_block + assertion_store + assertion + claim + signature + box_remain = ['00', '01', '00', '00', '00', '01'] + + block = ( + header + + l_box + + c_box + + box_remain + + cai_block + + store_block + + assertion_store + + assertion + + claim + + signature + ) return block @@ -421,9 +431,20 @@ def create_complete(cai_l_box, cai_block, store_block, assertion_block, assertio header = ['FF', 'EB'] c_box = convert_to_hex('JP') - box_remain = ['00', '01', '00', '00', '00','01'] - - final_cai_block = header + l_box + c_box + box_remain + cai_block + store_block + assertion_block + assertions + claim_block + signature_block + box_remain = ['00', '01', '00', '00', '00', '01'] + + final_cai_block = ( + header + + l_box + + c_box + + box_remain + + cai_block + + store_block + + assertion_block + + assertions + + claim_block + + signature_block + ) return final_cai_block diff --git a/c2pa/core.py b/c2pa/core.py index 37eab3a..c8dc0d9 100644 --- a/c2pa/core.py +++ b/c2pa/core.py @@ -25,12 +25,10 @@ from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5 -from cai.jumbf import Box from cai.jumbf import ContentBox from cai.jumbf import DescriptionBox from cai.jumbf import SuperBox -from cai.jumbf import create_json_superbox from cai.jumbf import json_to_bytes from endesive import plain @@ -40,10 +38,10 @@ ''' Cai_content_types = { - 'claim_block' : '6361636200110010800000aa00389b71', - 'store' : '6361737400110010800000aa00389b71', + 'claim_block' : '6361636200110010800000aa00389b71', # noqa E203 + 'store' : '6361737400110010800000aa00389b71', # noqa E203 'assertion_store': '6361617300110010800000aa00389b71', - 'claim' : '6361636c00110010800000aa00389b71', + 'claim' : '6361636c00110010800000aa00389b71', # noqa E203 'claim_signature': '6361736700110010800000aa00389b71', } @@ -152,6 +150,7 @@ def create_claim(self, assertion_store, claim['parent_claim'] = parent_claim return claim + class CaiClaimEndesiveSignature(SuperBox): def __init__(self, claim, key): super(CaiClaimEndesiveSignature, self).__init__() @@ -169,6 +168,7 @@ def create_endesive_signature(self, claim, key): payload = bytes.fromhex(uuid) + signature return payload + class CaiClaimCMSSignature(SuperBox): def __init__(self, claim, key): super(CaiClaimCMSSignature, self).__init__() @@ -256,4 +256,4 @@ def __init__(self): super(CaiClaimBlock, self).__init__() self.description_box = DescriptionBox( content_type=Cai_content_types['claim_block'], - label='cai') \ No newline at end of file + label='cai') diff --git a/c2pa/jumbf.py b/c2pa/jumbf.py index 49123ba..6f7a701 100644 --- a/c2pa/jumbf.py +++ b/c2pa/jumbf.py @@ -26,9 +26,9 @@ # Spec B.1 Jumbf_content_types = { 'codestream': '6579d6fbdba2446bb2ac1b82feeb89d1', - 'xml' : '786d6c2000110010800000aa00389b71', - 'json' : '6a736f6e00110010800000aa00389b71', - 'uuid' : '7575696400110010800000aa00389b71', + 'xml' : '786d6c2000110010800000aa00389b71', # noqa E203 + 'json' : '6a736f6e00110010800000aa00389b71', # noqa E203 + 'uuid' : '7575696400110010800000aa00389b71', # noqa E203 } @@ -45,7 +45,7 @@ def get_size(self): # Calculate box size dynamically. # 8 is from l_box (4) + t_box (4) return 8 + len(self.payload) - + def set_payload(self): pass @@ -173,9 +173,9 @@ def convert_bytes(self): def create_single_content_superbox(content=b'', - t_box_type='', - content_type='', - label=''): + t_box_type='', + content_type='', + label=''): c_box = ContentBox(t_box_type=t_box_type) c_box.payload = content d_box = DescriptionBox(content_type=content_type, label=label) @@ -202,7 +202,7 @@ def create_codestream_superbox(content=b'', label=''): def json_to_bytes(json_object): - return json.dumps(json_object, separators=(',',':')).encode('utf-8') + return json.dumps(json_object, separators=(',', ':')).encode('utf-8') def get_app11_marker_segment_headers(data_bytes): @@ -211,24 +211,24 @@ def get_app11_marker_segment_headers(data_bytes): headers = {} for offset in offsets: try: - ci = data_bytes[offset + 4 : offset + 6].decode('utf-8') + ci = data_bytes[offset + 4: offset + 6].decode('utf-8') except Exception as e: - print('Find App11 marker, and fail to get CI') + print(f'Find App11 marker, and fail to get CI. Exception: {e}') ci = None try: - tbox = data_bytes[offset + 16 : offset + 20].decode('utf-8') + tbox = data_bytes[offset + 16: offset + 20].decode('utf-8') except Exception as e: - print('Find App11 marker, and fail to get TBox') + print(f'Find App11 marker, and fail to get TBox. Exception: {e}') tbox = None if ci == 'JP' and tbox == 'jumb': header = {} - header['le'] = int.from_bytes(data_bytes[offset + 2 : offset + 4], byteorder='big') - header['ci'] = data_bytes[offset + 4 : offset + 6].decode('utf-8') - header['en'] = int.from_bytes(data_bytes[offset + 6 : offset + 8], byteorder='big') - header['z'] = int.from_bytes(data_bytes[offset + 8 : offset + 12], byteorder='big') - header['lbox'] = int.from_bytes(data_bytes[offset + 12 : offset + 16], byteorder='big') - header['tbox'] = data_bytes[offset + 16 : offset + 20].decode('utf-8') + header['le'] = int.from_bytes(data_bytes[offset + 2: offset + 4], byteorder='big') + header['ci'] = data_bytes[offset + 4: offset + 6].decode('utf-8') + header['en'] = int.from_bytes(data_bytes[offset + 6: offset + 8], byteorder='big') + header['z'] = int.from_bytes(data_bytes[offset + 8: offset + 12], byteorder='big') + header['lbox'] = int.from_bytes(data_bytes[offset + 12: offset + 16], byteorder='big') + header['tbox'] = data_bytes[offset + 16: offset + 20].decode('utf-8') header['offset'] = offset # passive protection to skip illegal or empty segment @@ -236,4 +236,4 @@ def get_app11_marker_segment_headers(data_bytes): headers[header['z']] = header else: print('Unknown CI ({0}) or TBox ({1}) of offset {2}'.format(ci, tbox, hex(offset))) - return headers \ No newline at end of file + return headers diff --git a/c2pa/starling.py b/c2pa/starling.py index 9d6e7a5..d971f08 100644 --- a/c2pa/starling.py +++ b/c2pa/starling.py @@ -19,7 +19,6 @@ import os from c2pa.core import CaiClaimBlock -from c2pa.core import CaiClaimCMSSignature from c2pa.core import CaiStore from c2pa.jumbf import App11Box @@ -36,6 +35,7 @@ '''Starling CLI tool to generate CAI metadata. ''' + class Starling(object): def __init__(self, media_bytes, @@ -161,13 +161,13 @@ def multiple_claims_injection(self): # re-construct Claim Block payload claim_block_payload = bytearray() - ## App11 Length of Marker Segment (the Le parameter) value is 8 ~ 65535. - ## App11 Packet Sequence number (the Z parameter) value is 1 ~ 2^32-1. - ## The Claim Block maximum size will be ~= 2^48 B ~= 280 TB. + # App11 Length of Marker Segment (the Le parameter) value is 8 ~ 65535. + # App11 Packet Sequence number (the Z parameter) value is 1 ~ 2^32-1. + # The Claim Block maximum size will be ~= 2^48 B ~= 280 TB. for i in range(1, header_number + 1): payload_start = self.app11_headers[i]['offset'] + 20 payload_end = payload_start + (self.app11_headers[i]['le'] - 18) - payload = self.raw_bytes[payload_start : payload_end] + payload = self.raw_bytes[payload_start: payload_end] claim_block_payload += payload store_bytes = cai_store.convert_bytes() @@ -178,27 +178,31 @@ def multiple_claims_injection(self): updated_app11_segment = App11Box(en=last_en) updated_app11_segment.payload = updated_claim_block_bytes - ## Assuming that current CAI data consists of 3 App11 segments. - ## - ## +-- starting point - ## v - ## +-------+----+----+------+-----+------+------+-------------------------------+ - ## | APP11 | Le | CI | En=1 | Z=1 | LBox | TBox | Payload (Claim Block, part 1) | - ## +-------+----+----+------+-----+------+------+-------------------------------+ - ## | APP11 | Le | CI | En=1 | Z=2 | LBox | TBox | Payload (Claim Block, part 2) | - ## +-------+----+----+------+-----+------+------+-------------------------------+ - ## | APP11 | Le | CI | En=1 | Z=3 | LBox | TBox | Payload (Claim Block, part 3) | - ## +-------+----+----+------+-----+------+------+-------------------------------+ - ## ^ - ## ending point --+ - ## - ## starting point of current CAI data + # Assuming that current CAI data consists of 3 App11 segments. + # + # +-- starting point + # v + # +-------+----+----+------+-----+------+------+-------------------------------+ + # | APP11 | Le | CI | En=1 | Z=1 | LBox | TBox | Payload (Claim Block, part 1) | + # +-------+----+----+------+-----+------+------+-------------------------------+ + # | APP11 | Le | CI | En=1 | Z=2 | LBox | TBox | Payload (Claim Block, part 2) | + # +-------+----+----+------+-----+------+------+-------------------------------+ + # | APP11 | Le | CI | En=1 | Z=3 | LBox | TBox | Payload (Claim Block, part 3) | + # +-------+----+----+------+-----+------+------+-------------------------------+ + # ^ + # ending point --+ + # + # starting point of current CAI data update_range_s = self.app11_headers[1]['offset'] - ## ending point of current CAI data + # ending point of current CAI data update_range_e = self.app11_headers[header_number]['offset'] + self.app11_headers[header_number]['le'] + 2 # save CAI-injected media - data_bytes = self.raw_bytes[:update_range_s] + updated_app11_segment.convert_bytes() + self.raw_bytes[update_range_e:] + data_bytes = ( + self.raw_bytes[:update_range_s] + + updated_app11_segment.convert_bytes() + + self.raw_bytes[update_range_e:] + ) cai_data_bytes = insert_xmp_key(data_bytes, store_label=self.store_label) return cai_data_bytes @@ -283,10 +287,11 @@ def main(): # private key for signature if key_filepath != '': with open(key_filepath, 'rb') as f: - if type_sig=='cms': + if type_sig == 'cms': key = f.read() - elif type_sig=='endesive': - # load_key_and_certificates second parameter is password to decrypt the data. Can be set to None of PKCS12 is not encrypted + elif type_sig == 'endesive': + # load_key_and_certificates second parameter is password to decrypt the data. + # Can be set to None of PKCS12 is not encrypted # https://cryptography.io/en/latest/hazmat/primitives/asymmetric/serialization.html key = pkcs12.load_key_and_certificates(f.read(), b'', backends.default_backend()) else: @@ -314,4 +319,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/data/Keys/endesive_sig.py b/data/Keys/endesive_sig.py index 8594122..6f50716 100644 --- a/data/Keys/endesive_sig.py +++ b/data/Keys/endesive_sig.py @@ -7,7 +7,8 @@ def main(): with open('demo2_user1.p12', 'rb') as fp: p12 = pkcs12.load_key_and_certificates(fp.read(), b'1234', backends.default_backend()) unsigned = open('plain-unsigned.txt', 'rb').read() - signed = plain.sign(unsigned, + signed = plain.sign( + unsigned, p12[0], p12[1], p12[2], 'sha256', attrs=True @@ -15,4 +16,4 @@ def main(): open('plain-signed-attr.txt', 'wb').write(signed) -main() \ No newline at end of file +main() diff --git a/data/Keys/verify_sig.py b/data/Keys/verify_sig.py index ee78579..eaeecc5 100644 --- a/data/Keys/verify_sig.py +++ b/data/Keys/verify_sig.py @@ -1,5 +1,6 @@ from endesive import plain + def main(): trusted_cert_pems = (open('demo2_ca.crt.pem', 'rt').read(),) unsigned = open('plain-unsigned.txt', 'rb').read() @@ -12,5 +13,6 @@ def main(): print('signature ok?', signatureok) print('hash ok?', hashok) + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/setup.py b/setup.py index 6606525..5540741 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,3 @@ -import subprocess - from setuptools import setup from setuptools import find_packages diff --git a/tests/test_core.py b/tests/test_core.py index ed2cbf8..0541650 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1,5 +1,3 @@ -import io -import json import unittest from cai.core import CaiAssertionStore @@ -76,7 +74,7 @@ def test_cai_claim(self): bytes.fromhex(testing_claim)) def test_cai_claim_signature(self): - testing_claim_signature = ( + testing_claim_signature = ( # noqa F841 '000000446a756d62000000276a756d646361736700110010800000aa00389b71' '036361692e7369676e617475726500000000156a736f6e7b22666f6f223a2262' '6172227d' @@ -90,11 +88,11 @@ def test_cai_claim_signature(self): cai_claim_signature.description_box.print_box() cai_claim_signature.print_box() - #self.assertEqual(cai_claim_signature.convert_bytes(), + # self.assertEqual(cai_claim_signature.convert_bytes(), # bytes.fromhex(testing_claim_signature)) def test_cai_store(self): - testing_store = ( + testing_store = ( # noqa F841 '000001296a756d62000000276a756d646361737400110010800000aa00389b71' '0363622e737461726c696e675f3100000000766a756d62000000286a756d6463' '61617300110010800000aa00389b71036361692e617373657274696f6e730000' @@ -122,11 +120,11 @@ def test_cai_store(self): cai_store.description_box.print_box() cai_store.print_box() - #self.assertEqual(cai_store.convert_bytes(), + # self.assertEqual(cai_store.convert_bytes(), # bytes.fromhex(testing_store)) def test_cai_claim_block(self): - testing_claim_block = ( + testing_claim_block = ( # noqa F841 '0000014e6a756d620000001d6a756d646361636200110010800000aa00389b71' '0363616900000001296a756d62000000276a756d646361737400110010800000' 'aa00389b710363622e737461726c696e675f3100000000766a756d6200000028' @@ -157,11 +155,11 @@ def test_cai_claim_block(self): cai_claim_block.description_box.print_box() cai_claim_block.print_box() - #self.assertEqual(cai_claim_block.convert_bytes(), + # self.assertEqual(cai_claim_block.convert_bytes(), # bytes.fromhex(testing_claim_block)) def test_app11_segment(self): - testing_app11_segment = ( + testing_app11_segment = ( # noqa F841 'ffeb01584a500001000000010000014e6a756d620000001d6a756d6463616362' '00110010800000aa00389b710363616900000001296a756d62000000276a756d' '646361737400110010800000aa00389b710363622e737461726c696e675f3100' @@ -191,7 +189,7 @@ def test_app11_segment(self): app11_segment.payload = cai_claim_block.convert_bytes() print('\tbytes:', app11_segment.convert_bytes().hex()) - #self.assertEqual(app11_segment.convert_bytes(), + # self.assertEqual(app11_segment.convert_bytes(), # bytes.fromhex(testing_app11_segment)) diff --git a/tests/test_jumbf.py b/tests/test_jumbf.py index 2f92220..38ee895 100644 --- a/tests/test_jumbf.py +++ b/tests/test_jumbf.py @@ -102,7 +102,7 @@ def test_jumbf_hello_world(self): '223a22626172227d' ) - data = {"foo":"bar"} + data = {"foo": "bar"} f = io.BytesIO(json.dumps(data, separators=(',', ':')).encode('utf-8')) data_bytes = f.read() diff --git a/utils/c2pa_hello_world.py b/utils/c2pa_hello_world.py index 39eabf1..bf93f3b 100755 --- a/utils/c2pa_hello_world.py +++ b/utils/c2pa_hello_world.py @@ -44,8 +44,8 @@ photo_filename = 'meimei-fried-chicken.jpg' # multi-claim injection -#photo_url = 'https://ipfs.io/ipfs/QmPa8Dokcjcouv1KYrXn1cYA6XLACDBPVmnaMZ4un8K54L' -#photo_filename = 'meimei-nbj.jpg' +# photo_url = 'https://ipfs.io/ipfs/QmPa8Dokcjcouv1KYrXn1cYA6XLACDBPVmnaMZ4un8K54L' +# photo_filename = 'meimei-nbj.jpg' photo_bytes = requests.get(photo_url).content @@ -108,4 +108,4 @@ fname, fext = os.path.splitext(photo_filename) fpath = fname + '-cai' + fext with open(fpath, 'wb') as f: - f.write(starling_cai_bytes) \ No newline at end of file + f.write(starling_cai_bytes) diff --git a/utils/c2pa_multiple_injection.py b/utils/c2pa_multiple_injection.py index 3d8fb79..e0dca6a 100755 --- a/utils/c2pa_multiple_injection.py +++ b/utils/c2pa_multiple_injection.py @@ -34,8 +34,6 @@ import os import sys -import requests - from c2pa.jumbf import json_to_bytes from c2pa.starling import Starling diff --git a/utils/digital-signature/cms/digital_signature.py b/utils/digital-signature/cms/digital_signature.py index 13e8476..66c2ff2 100644 --- a/utils/digital-signature/cms/digital_signature.py +++ b/utils/digital-signature/cms/digital_signature.py @@ -5,9 +5,11 @@ def usage(): - print("Usage: \n" - "digi-sig -s \n" - "digi-sig -v \n") + print( + "Usage: \n" + "digi-sig -s \n" + "digi-sig -v \n" + ) if (len(sys.argv) < 5): @@ -26,7 +28,8 @@ def generate_signature(key, data, sig_f): rsa = RSA.importKey(key) signer = PKCS1_v1_5.new(rsa) signature = signer.sign(h) - with open(sig_f, 'wb') as f: f.write(signature) + with open(sig_f, 'wb') as f: + f.write(signature) def verify_signature(key, data, sig_f): @@ -34,7 +37,8 @@ def verify_signature(key, data, sig_f): h = SHA256.new(data) rsa = RSA.importKey(key) signer = PKCS1_v1_5.new(rsa) - with open(sig_f, 'rb') as f: signature = f.read() + with open(sig_f, 'rb') as f: + signature = f.read() if (signer.verify(h, signature)): rsp = 'Success' @@ -45,8 +49,10 @@ def verify_signature(key, data, sig_f): def main(): # Read all file contents - with open(key_f, 'rb') as f: key = f.read() - with open(data_f, 'rb') as f: data = f.read() + with open(key_f, 'rb') as f: + key = f.read() + with open(data_f, 'rb') as f: + data = f.read() if (op == "-s"): # Generate Signature @@ -55,9 +61,9 @@ def main(): # Verify Signature verify_signature(key, data, sig_f) else: - #Error + # Error usage() if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/utils/digital-signature/digital_signature.py b/utils/digital-signature/digital_signature.py index 13e8476..66c2ff2 100644 --- a/utils/digital-signature/digital_signature.py +++ b/utils/digital-signature/digital_signature.py @@ -5,9 +5,11 @@ def usage(): - print("Usage: \n" - "digi-sig -s \n" - "digi-sig -v \n") + print( + "Usage: \n" + "digi-sig -s \n" + "digi-sig -v \n" + ) if (len(sys.argv) < 5): @@ -26,7 +28,8 @@ def generate_signature(key, data, sig_f): rsa = RSA.importKey(key) signer = PKCS1_v1_5.new(rsa) signature = signer.sign(h) - with open(sig_f, 'wb') as f: f.write(signature) + with open(sig_f, 'wb') as f: + f.write(signature) def verify_signature(key, data, sig_f): @@ -34,7 +37,8 @@ def verify_signature(key, data, sig_f): h = SHA256.new(data) rsa = RSA.importKey(key) signer = PKCS1_v1_5.new(rsa) - with open(sig_f, 'rb') as f: signature = f.read() + with open(sig_f, 'rb') as f: + signature = f.read() if (signer.verify(h, signature)): rsp = 'Success' @@ -45,8 +49,10 @@ def verify_signature(key, data, sig_f): def main(): # Read all file contents - with open(key_f, 'rb') as f: key = f.read() - with open(data_f, 'rb') as f: data = f.read() + with open(key_f, 'rb') as f: + key = f.read() + with open(data_f, 'rb') as f: + data = f.read() if (op == "-s"): # Generate Signature @@ -55,9 +61,9 @@ def main(): # Verify Signature verify_signature(key, data, sig_f) else: - #Error + # Error usage() if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/utils/digital-signature/endesive/endesive-sign.py b/utils/digital-signature/endesive/endesive-sign.py index 3988460..f125dc7 100644 --- a/utils/digital-signature/endesive/endesive-sign.py +++ b/utils/digital-signature/endesive/endesive-sign.py @@ -3,32 +3,40 @@ from cryptography.hazmat.primitives.serialization import pkcs12 from endesive import plain + def usage(): - print("Usage: \n" - "endesive-sig -s \n" - "endesive-verify -v \n") + print( + "Usage: \n" + "endesive-sig -s \n" + "endesive-verify -v \n" + ) + if (len(sys.argv) < 5): usage() quit() + op = sys.argv[1] key_f = sys.argv[2] data_f = sys.argv[3] sign_f = sys.argv[4] + def generate_signature(key, data, signed): print("Generating Signature") with open(key, 'rb') as fp: p12 = pkcs12.load_key_and_certificates(fp.read(), b'', backends.default_backend()) datau = open(data, 'rb').read() - datas = plain.sign(datau, + datas = plain.sign( + datau, p12[0], p12[1], p12[2], 'sha256', attrs=True ) open(signed, 'wb').write(datas) + def verfy_signature(key, data, signed): print("Verifying Signature") trusted_cert_pems = (open(key, 'rt').read(),) @@ -39,8 +47,9 @@ def verfy_signature(key, data, signed): print('hash ok?', hashok) print('cert ok?', certok) + def main(): - + if (op == '-s'): # Generate Signature generate_signature(key_f, data_f, sign_f) @@ -53,4 +62,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/utils/digital-signature/hex2bin.py b/utils/digital-signature/hex2bin.py index 553e217..2f3c761 100644 --- a/utils/digital-signature/hex2bin.py +++ b/utils/digital-signature/hex2bin.py @@ -22,4 +22,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/utils/encode_hashlink.py b/utils/encode_hashlink.py index f5a6988..c8f6af2 100644 --- a/utils/encode_hashlink.py +++ b/utils/encode_hashlink.py @@ -45,4 +45,4 @@ def parse_args(): print('Input file: {0}\nhashlink param: {1}'.format( os.path.basename(filepath), encode_hashlink(data_bytes, to_hexstr=True) - )) \ No newline at end of file + )) diff --git a/utils/get_marker_index.py b/utils/get_marker_index.py index 74e5e31..dacf5e9 100644 --- a/utils/get_marker_index.py +++ b/utils/get_marker_index.py @@ -12,10 +12,10 @@ final_lbox = 0 for offset in offsets: - Le = int.from_bytes(f[offset + 2 : offset + 4], byteorder='big') - En = int.from_bytes(f[offset + 6 : offset + 8], byteorder='big') - Z = int.from_bytes(f[offset + 8 : offset + 12], byteorder='big') - LBox = int.from_bytes(f[offset + 12 : offset + 16], byteorder='big') + Le = int.from_bytes(f[offset + 2: offset + 4], byteorder='big') + En = int.from_bytes(f[offset + 6: offset + 8], byteorder='big') + Z = int.from_bytes(f[offset + 8: offset + 12], byteorder='big') + LBox = int.from_bytes(f[offset + 12: offset + 16], byteorder='big') print('# Offset: {0}, Le: {1}, En: {2}, Z (seq): {3}, LBox: {4}'.format( offset, Le, En, Z, LBox))