mirror of
https://github.com/romanz/amodem.git
synced 2026-03-17 07:05:59 +08:00
Follow GPG implementation for subpacket prefix encoding.
Conflicts: trezor_agent/gpg/protocol.py
This commit is contained in:
@@ -47,17 +47,17 @@ def subpacket_byte(subpacket_type, value):
|
||||
return subpacket(subpacket_type, '>B', value)
|
||||
|
||||
|
||||
def subpacket_prefix_len(blob):
|
||||
"""Prefix GPG subpacket with the encoding of it's length."""
|
||||
if len(blob) < 2**8:
|
||||
length_type = 0
|
||||
elif len(blob) < 2**16:
|
||||
length_type = 1
|
||||
def subpacket_prefix_len(item):
|
||||
"""Prefix subpacket length according to RFC 4880 section-5.2.3.1."""
|
||||
n = len(item)
|
||||
if n >= 8384:
|
||||
prefix = b'\xFF' + struct.pack('>L', n)
|
||||
elif n >= 192:
|
||||
n = n - 192
|
||||
prefix = struct.pack('BB', (n // 256) + 192, n % 256)
|
||||
else:
|
||||
length_type = 2
|
||||
fmt = ['>B', '>H', '>L'][length_type]
|
||||
prefix = [b'', b'', struct.pack('>B', 255)][length_type]
|
||||
return prefix + struct.pack(fmt, len(blob)) + blob
|
||||
prefix = struct.pack('B', n)
|
||||
return prefix + item
|
||||
|
||||
|
||||
def subpackets(*items):
|
||||
|
||||
@@ -5,7 +5,7 @@ import os
|
||||
|
||||
import pytest
|
||||
|
||||
from .. import decode
|
||||
from .. import decode, protocol
|
||||
from ... import util
|
||||
|
||||
|
||||
@@ -14,6 +14,15 @@ def test_subpackets():
|
||||
assert decode.parse_subpackets(util.Reader(s)) == [b'\xAB\xCD', b'\xEF']
|
||||
|
||||
|
||||
def test_subpackets_prefix():
|
||||
for n in [0, 1, 2, 4, 5, 10, 191, 192, 193,
|
||||
255, 256, 257, 8383, 8384, 65530]:
|
||||
item = b'?' * n # create dummy subpacket
|
||||
prefixed = protocol.subpackets(item)
|
||||
result = decode.parse_subpackets(util.Reader(io.BytesIO(prefixed)))
|
||||
assert [item] == result
|
||||
|
||||
|
||||
def test_mpi():
|
||||
s = io.BytesIO(b'\x00\x09\x01\x23')
|
||||
assert decode.parse_mpi(util.Reader(s)) == 0x123
|
||||
|
||||
Reference in New Issue
Block a user