From 76ce25fab19dd97ceb88cba9b0d3e1c08befa432 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 23 Apr 2016 22:08:18 +0300 Subject: [PATCH] gpg: fixup imports --- trezor_agent/__main__.py | 2 +- trezor_agent/gpg/check.py | 4 ++-- trezor_agent/gpg/git_gpg_wrapper.py | 8 +++----- trezor_agent/gpg/signer.py | 28 ++++++++++++++-------------- trezor_agent/gpg/util.py | 18 ------------------ trezor_agent/util.py | 17 +++++++++++++++++ 6 files changed, 37 insertions(+), 40 deletions(-) delete mode 100644 trezor_agent/gpg/util.py diff --git a/trezor_agent/__main__.py b/trezor_agent/__main__.py index bebb043..5248a9f 100644 --- a/trezor_agent/__main__.py +++ b/trezor_agent/__main__.py @@ -2,8 +2,8 @@ import argparse import functools import logging -import re import os +import re import subprocess import sys import time diff --git a/trezor_agent/gpg/check.py b/trezor_agent/gpg/check.py index d228620..b1b3557 100755 --- a/trezor_agent/gpg/check.py +++ b/trezor_agent/gpg/check.py @@ -4,8 +4,8 @@ import base64 import io import logging -import decode -import util +from . import decode +from .. import util log = logging.getLogger(__name__) diff --git a/trezor_agent/gpg/git_gpg_wrapper.py b/trezor_agent/gpg/git_gpg_wrapper.py index 9467ffe..f3296bd 100755 --- a/trezor_agent/gpg/git_gpg_wrapper.py +++ b/trezor_agent/gpg/git_gpg_wrapper.py @@ -1,11 +1,9 @@ #!/usr/bin/env python -import sys -import subprocess as sp -import time import logging -import os +import subprocess as sp +import sys -import signer +from . import signer log = logging.getLogger(__name__) diff --git a/trezor_agent/gpg/signer.py b/trezor_agent/gpg/signer.py index 06a3c65..3b644d1 100755 --- a/trezor_agent/gpg/signer.py +++ b/trezor_agent/gpg/signer.py @@ -9,11 +9,9 @@ import struct import subprocess import time -import decode -import trezor_agent.client -import trezor_agent.formats -import trezor_agent.util -import util +from . import decode +from .. import client, factory, formats +from .. import util log = logging.getLogger(__name__) @@ -79,23 +77,24 @@ def _dump_nist256(vk): def _dump_ed25519(vk): return mpi((0x40 << 256) | - trezor_agent.util.bytes2num(vk.to_bytes())) + util.bytes2num(vk.to_bytes())) SUPPORTED_CURVES = { - trezor_agent.formats.CURVE_NIST256: { + formats.CURVE_NIST256: { # https://tools.ietf.org/html/rfc6637#section-11 'oid': b'\x2A\x86\x48\xCE\x3D\x03\x01\x07', 'algo_id': 19, 'dump': _dump_nist256 }, - trezor_agent.formats.CURVE_ED25519: { + formats.CURVE_ED25519: { 'oid': b'\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01', 'algo_id': 22, 'dump': _dump_ed25519 } } + def find_curve_by_algo_id(algo_id): curve_name, = [name for name, info in SUPPORTED_CURVES.items() if info['algo_id'] == algo_id] @@ -106,19 +105,19 @@ class Signer(object): def __init__(self, user_id, created, curve_name): self.user_id = user_id - assert curve_name in trezor_agent.formats.SUPPORTED_CURVES + assert curve_name in formats.SUPPORTED_CURVES self.curve_name = curve_name - self.client_wrapper = trezor_agent.factory.load() + self.client_wrapper = factory.load() self.identity = self.client_wrapper.identity_type() self.identity.proto = 'gpg' self.identity.host = user_id - addr = trezor_agent.client.get_address(self.identity) + addr = client.get_address(self.identity) public_node = self.client_wrapper.connection.get_public_node( n=addr, ecdsa_curve_name=self.curve_name) - self.verifying_key = trezor_agent.formats.decompress_pubkey( + self.verifying_key = formats.decompress_pubkey( pubkey=public_node.node.public_key, curve_name=self.curve_name) @@ -185,6 +184,7 @@ class Signer(object): data_to_sign=msg, hashed_subpackets=hashed_subpackets) return packet(tag=2, blob=blob) + def _make_signature(self, visual, data_to_sign, hashed_subpackets, sig_type=0): curve_info = SUPPORTED_CURVES[self.curve_name] @@ -210,8 +210,8 @@ class Signer(object): ecdsa_curve_name=self.curve_name) assert result.signature[:1] == b'\x00' sig = result.signature[1:] - sig = [trezor_agent.util.bytes2num(sig[:32]), - trezor_agent.util.bytes2num(sig[32:])] + sig = [util.bytes2num(sig[:32]), + util.bytes2num(sig[32:])] hash_prefix = digest[:2] # used for decoder's sanity check signature = mpi(sig[0]) + mpi(sig[1]) # actual ECDSA signature diff --git a/trezor_agent/gpg/util.py b/trezor_agent/gpg/util.py deleted file mode 100644 index a2b1f57..0000000 --- a/trezor_agent/gpg/util.py +++ /dev/null @@ -1,18 +0,0 @@ -import struct - - -def crc24(blob): - CRC24_INIT = 0xB704CEL - CRC24_POLY = 0x1864CFBL - - crc = CRC24_INIT - for octet in bytearray(blob): - crc ^= (octet << 16) - for _ in range(8): - crc <<= 1 - if crc & 0x1000000: - crc ^= CRC24_POLY - assert 0 <= crc < 0x1000000 - crc_bytes = struct.pack('>L', crc) - assert crc_bytes[0] == b'\x00' - return crc_bytes[1:] diff --git a/trezor_agent/util.py b/trezor_agent/util.py index 8119a82..822dbe7 100644 --- a/trezor_agent/util.py +++ b/trezor_agent/util.py @@ -75,3 +75,20 @@ def frame(*msgs): res.write(msg) msg = res.getvalue() return pack('L', len(msg)) + msg + + +def crc24(blob): + CRC24_INIT = 0xB704CEL + CRC24_POLY = 0x1864CFBL + + crc = CRC24_INIT + for octet in bytearray(blob): + crc ^= (octet << 16) + for _ in range(8): + crc <<= 1 + if crc & 0x1000000: + crc ^= CRC24_POLY + assert 0 <= crc < 0x1000000 + crc_bytes = struct.pack('>L', crc) + assert crc_bytes[0] == b'\x00' + return crc_bytes[1:]