6

I'm encrypting a file using the python-gnupg module like this. Ultimately, I want to use the AES256 cipher for encryption:

status = gpg.encrypt_file(fin, recipients=['root@frarakee'], output='foo.bin.gpg')

When I check the file at the CLI, it tells me it's a PGP message:

# file foo.bin.gpg 
foo.bin.gpg: PGP message

If I encrypt the file using gpg at the CLI (Ubuntu 14.04), the file command tells me its data yet gpg shows it's using AES256:

# gpg --verbose -r root@frarakee --cipher-algo AES256 -e foo.bin
gpg: using PGP trust model
gpg: using subkey 1C32653F instead of primary key A7173E08
gpg: This key belongs to us
gpg: reading from `foo.bin'
File `foo.bin.gpg' exists. Overwrite? (y/N) y
gpg: writing to `foo.bin.gpg'
gpg: RSA/AES256 encrypted for: "132653F root@frarakee <root@frarakee>"

# file foo.bin
foo.bin: data

Notably, the python docs state:

The default cipher_algo, if unspecified, is 'AES256'

But the file command only seems to agree with this when I used symmetric encryption:

# gpg --cipher-algo AES256 -c /tmp/foo.bin
# file /tmp/foo.bin.gpg 
foo.bin.gpg: GPG symmetrically encrypted data (AES256 cipher)

I'm not sure if this is a bug in the file magic, the python-gpg module, or just my understanding. How can I tell what cipher is actually being used by gpg or the python-gpg module? Is there a better place to ask this?

2 Answers 2

2

file only looks for some magic bytes to determine the file type, but does not fully parse the OpenPGP packets. You can view all encryption details by running gpg --list-packets or pgpdump (output is easier to read, but needs to be installed separately). Reading through RFC 4880, OpenPGP might help at understanding the very technical output.

2

TL;DR

gpg -vvv --list-packets can probably answer most questions.

This post builds on Jens Erat's answer

gpg --list-packets can use the verbosity and debug switches. Note, that while the man page only talks about passing up to 2 verbose switches gpg does respond up to 3 verbose swiches.

The --debug-level 1, switch isn't that interesting, --debug-all is interesting, but for the love of god do it on an empty file. My encrypted empty file resulted in 414 lines of output, my 1GB file resulted in 2.7 million lines.

I've included examples of the output of pgpdump, and gpg with differing levels of verbosity. I had to cut the --debug-all out because it took too much space.

FYI I'm running gpg (GnuPG) 2.2.27

pgpdump ./big_buck_bunny_1080p_surround.avi.gpg  2>&1  | head -n 15

Old: Public-Key Encrypted Session Key Packet(tag 1)(94 bytes)
        New version(3)
        Key ID - 0xD2E1F0CBF16B1266
        Pub alg - Reserved for Elliptic Curve(pub 18)
                unknown(pub 18)
                -> m = sym alg(1 byte) + checksum(2 bytes) + PKCS-1 block type 02
New: Symmetrically Encrypted and MDC Packet(tag 18)(8192 bytes) partial start
        Ver 1
        Encrypted data [sym alg is specified in pub-key encrypted session key]
                (plain text + MDC SHA1(20 bytes))
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
New:    (8192 bytes) partial continue
$ gpg --list-packets ./big_buck_bunny_1080p_surround.avi.gpg

gpg: encrypted with 255-bit ECDH key, ID D2E1F0CBF16B1266, created 2021-03-23
      "your dog (doggy) <[email protected]>"
# off=0 ctb=84 tag=1 hlen=2 plen=94
:pubkey enc packet: version 3, algo 18, keyid D2E1F0CBF16B1266
        data: [263 bits]
        data: [392 bits]
# off=96 ctb=d2 tag=18 hlen=2 plen=0 partial new-ctb
:encrypted data packet:
        length: unknown
        mdc_method: 2
# off=117 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
:compressed packet: algo=2
# off=119 ctb=ae tag=11 hlen=5 plen=928670793
:literal data packet:
        mode b (62), created 1616485594, name="big_buck_bunny_1080p_surround.avi",
        raw data: 928670754 bytes
$ gpg -vvv --list-packets ./big_buck_bunny_1080p_surround.avi.gpg 

gpg: using character set 'utf-8'
# off=0 ctb=84 tag=1 hlen=2 plen=94
:pubkey enc packet: version 3, algo 18, keyid D2E1F0CBF16B1266
        data: [263 bits]
        data: [392 bits]
gpg: public key is D2E1F0CBF16B1266
gpg: using subkey D2E1F0CBF16B1266 instead of primary key CCD3793A53651BF6
gpg: public key encrypted data: good DEK
# off=96 ctb=d2 tag=18 hlen=2 plen=0 partial new-ctb
:encrypted data packet:
        length: unknown
        mdc_method: 2
gpg: using subkey D2E1F0CBF16B1266 instead of primary key CCD3793A53651BF6
gpg: encrypted with 255-bit ECDH key, ID D2E1F0CBF16B1266, created 2021-03-23
      "your dog (doggy) <[email protected]>"
gpg: AES256 encrypted data
# off=117 ctb=a3 tag=8 hlen=1 plen=0 indeterminate
:compressed packet: algo=2
# off=119 ctb=ae tag=11 hlen=5 plen=928670793
:literal data packet:
        mode b (62), created 1616485594, name="big_buck_bunny_1080p_surround.avi",
        raw data: 928670754 bytes
gpg: decryption okay

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .