From 87ca33c1048a99ffc2b28959573e8e2c0552cac4 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 30 Apr 2016 15:34:18 +0300 Subject: [PATCH] gpg: fixup encoding for large packets --- trezor_agent/gpg/encode.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/trezor_agent/gpg/encode.py b/trezor_agent/gpg/encode.py index bfb95a4..8aa88b8 100644 --- a/trezor_agent/gpg/encode.py +++ b/trezor_agent/gpg/encode.py @@ -13,9 +13,18 @@ log = logging.getLogger(__name__) def packet(tag, blob): """Create small GPG packet.""" - length_type = 1 # : 2 bytes for length + assert len(blob) < 2**32 + + if len(blob) < 2**8: + length_type = 0 + elif len(blob) < 2**16: + length_type = 1 + else: + length_type = 2 + + fmt = ['>B', '>H', '>L'][length_type] leading_byte = 0x80 | (tag << 2) | (length_type) - return struct.pack('>B', leading_byte) + util.prefix_len('>H', blob) + return struct.pack('>B', leading_byte) + util.prefix_len(fmt, blob) def subpacket(subpacket_type, fmt, *values):