gpg: fixup imports

This commit is contained in:
Roman Zeyde 2016-04-23 22:08:18 +03:00
parent 5506310239
commit 76ce25fab1
6 changed files with 37 additions and 40 deletions

View File

@ -2,8 +2,8 @@
import argparse import argparse
import functools import functools
import logging import logging
import re
import os import os
import re
import subprocess import subprocess
import sys import sys
import time import time

View File

@ -4,8 +4,8 @@ import base64
import io import io
import logging import logging
import decode from . import decode
import util from .. import util
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@ -1,11 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import subprocess as sp
import time
import logging import logging
import os import subprocess as sp
import sys
import signer from . import signer
log = logging.getLogger(__name__) log = logging.getLogger(__name__)

View File

@ -9,11 +9,9 @@ import struct
import subprocess import subprocess
import time import time
import decode from . import decode
import trezor_agent.client from .. import client, factory, formats
import trezor_agent.formats from .. import util
import trezor_agent.util
import util
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -79,23 +77,24 @@ def _dump_nist256(vk):
def _dump_ed25519(vk): def _dump_ed25519(vk):
return mpi((0x40 << 256) | return mpi((0x40 << 256) |
trezor_agent.util.bytes2num(vk.to_bytes())) util.bytes2num(vk.to_bytes()))
SUPPORTED_CURVES = { SUPPORTED_CURVES = {
trezor_agent.formats.CURVE_NIST256: { formats.CURVE_NIST256: {
# https://tools.ietf.org/html/rfc6637#section-11 # https://tools.ietf.org/html/rfc6637#section-11
'oid': b'\x2A\x86\x48\xCE\x3D\x03\x01\x07', 'oid': b'\x2A\x86\x48\xCE\x3D\x03\x01\x07',
'algo_id': 19, 'algo_id': 19,
'dump': _dump_nist256 'dump': _dump_nist256
}, },
trezor_agent.formats.CURVE_ED25519: { formats.CURVE_ED25519: {
'oid': b'\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01', 'oid': b'\x2B\x06\x01\x04\x01\xDA\x47\x0F\x01',
'algo_id': 22, 'algo_id': 22,
'dump': _dump_ed25519 'dump': _dump_ed25519
} }
} }
def find_curve_by_algo_id(algo_id): def find_curve_by_algo_id(algo_id):
curve_name, = [name for name, info in SUPPORTED_CURVES.items() curve_name, = [name for name, info in SUPPORTED_CURVES.items()
if info['algo_id'] == algo_id] if info['algo_id'] == algo_id]
@ -106,19 +105,19 @@ class Signer(object):
def __init__(self, user_id, created, curve_name): def __init__(self, user_id, created, curve_name):
self.user_id = user_id 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.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 = self.client_wrapper.identity_type()
self.identity.proto = 'gpg' self.identity.proto = 'gpg'
self.identity.host = user_id 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( public_node = self.client_wrapper.connection.get_public_node(
n=addr, ecdsa_curve_name=self.curve_name) 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, pubkey=public_node.node.public_key,
curve_name=self.curve_name) curve_name=self.curve_name)
@ -185,6 +184,7 @@ class Signer(object):
data_to_sign=msg, hashed_subpackets=hashed_subpackets) data_to_sign=msg, hashed_subpackets=hashed_subpackets)
return packet(tag=2, blob=blob) return packet(tag=2, blob=blob)
def _make_signature(self, visual, data_to_sign, def _make_signature(self, visual, data_to_sign,
hashed_subpackets, sig_type=0): hashed_subpackets, sig_type=0):
curve_info = SUPPORTED_CURVES[self.curve_name] curve_info = SUPPORTED_CURVES[self.curve_name]
@ -210,8 +210,8 @@ class Signer(object):
ecdsa_curve_name=self.curve_name) ecdsa_curve_name=self.curve_name)
assert result.signature[:1] == b'\x00' assert result.signature[:1] == b'\x00'
sig = result.signature[1:] sig = result.signature[1:]
sig = [trezor_agent.util.bytes2num(sig[:32]), sig = [util.bytes2num(sig[:32]),
trezor_agent.util.bytes2num(sig[32:])] util.bytes2num(sig[32:])]
hash_prefix = digest[:2] # used for decoder's sanity check hash_prefix = digest[:2] # used for decoder's sanity check
signature = mpi(sig[0]) + mpi(sig[1]) # actual ECDSA signature signature = mpi(sig[0]) + mpi(sig[1]) # actual ECDSA signature

View File

@ -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:]

View File

@ -75,3 +75,20 @@ def frame(*msgs):
res.write(msg) res.write(msg)
msg = res.getvalue() msg = res.getvalue()
return pack('L', len(msg)) + msg 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:]