DCIT / perl-CryptX

https://metacpan.org/pod/CryptX
Other
35 stars 23 forks source link

Crypt::AuthEnc::GCM - Encryption of the empty string #37

Closed lixmal closed 7 years ago

lixmal commented 7 years ago

Hi,

it seems encryption of the empty string is not supported in GCM mode:

my $gcm = Crypt::AuthEnc::GCM->new("AES", Crypt::URandom::urandom(32));
$gcm->iv_add(Crypt::URandom::urandom(16));
$gcm->adata_add('test');
$gcm->encrypt_add('');
$gcm->encrypt_done;

leads to FATAL: gcm_done failed: Invalid argument provided.

Is that expected behaviour or some restriction of the GCM mode? Crypt::Mode::CBC and CTR work fine on the empty string. Although CTR produces empty ciphertext, I'd like to know if somebody tampered with it, at least.

Using CryptX version 0.048.

karel-m commented 7 years ago

It is a bug.

The following code:

use Crypt::AuthEnc::GCM;
use Crypt::PRNG 'random_bytes';

my $gcm = Crypt::AuthEnc::GCM->new("AES", "1234567890123456");
$gcm->iv_add("1234567890123456");
$gcm->adata_add('test');
my $ct = $gcm->encrypt_add('');
my $tag = $gcm->encrypt_done;

print "CT='", unpack("H*", $ct), "'\n";
print "TAG='", unpack("H*", $tag), "'\n";

should IMO print:

CT=''
TAG='222798d502b90b2a283d80acd40eed5b'

which means: 1/ empty cipher text + 2/ non-empty tag

Slightly off topic advise: if you are already using CryptX distribution you can use https://metacpan.org/pod/Crypt::PRNG#random_bytes to get random bytes

karel-m commented 7 years ago

Try CryptX-0.048_001

lixmal commented 7 years ago

Works as intended now, thanks for the info and the quick reply!