File: 01-cipher.t

package info (click to toggle)
libcrypt-gcrypt-perl 1.26-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 364 kB
  • sloc: perl: 237; makefile: 3
file content (110 lines) | stat: -rw-r--r-- 3,049 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl 01-use.t'

#########################

use Test;
BEGIN { plan tests => 20 }; # <--- number of tests

use ExtUtils::testlib;
use Crypt::GCrypt;
ok(1);

#########################

ok(Crypt::GCrypt::cipher_algo_available('aes'));
ok(Crypt::GCrypt::cipher_algo_available('arcfour'));
ok(Crypt::GCrypt::cipher_algo_available('twofish'));

my $c = Crypt::GCrypt->new(
                           type => 'cipher',
                           algorithm => 'aes',
                           mode => 'cbc',
                           padding => 'null'
);
ok(defined $c && $c->isa('Crypt::GCrypt'));
ok($c->keylen == 16);
ok($c->blklen == 16);

$c->start('encrypting');
$c->setkey(my $key = "the key, the key");

my $p = 'plain text';
my ($e0, $e, $d);
$e0 = pack('H*', 'c796843558cefa157bf108ab79823a5a');
$e = $c->encrypt($p);
$e .= $c->finish;
ok($e eq $e0) or print STDERR "[",unpack('H*',$e),"]\n";

$c->setiv();
$c->start('decrypting');
$d = $c->decrypt($e);
$d .= $c->finish;
ok(substr($d, 0, length $p) eq $p)
  or print STDERR "[",unpack('H*',$d),"]\n";;

$c = Crypt::GCrypt->new(
                        type => 'cipher',
                        algorithm => 'aes',
                        mode => 'ecb',
                        padding => 'null'
);
$c->start('encrypting');
$c->setkey($key);
$e = $c->encrypt($p);
$e .= $c->finish;
ok($e eq $e0) or print STDERR "[",unpack('H*',$e),"]\n";

$c = Crypt::GCrypt->new(
                        type => 'cipher',
                        algorithm => 'twofish',
                        padding => 'null'
);
ok($c->keylen == 32);
ok($c->blklen == 16);
$c->start('encrypting');
$c->setkey($key);
$c->setiv(my $iv = 'explicit iv');
$e = $c->encrypt($p);
$e .= $c->finish;
ok($e eq pack('H*', '9c93705d7b3348c73cd2047ce5ecc1a8'))
  or print STDERR "[",unpack('H*',$e),"]\n";
$c->start('decrypting');
$c->setiv($iv);
$d = $c->decrypt($e);
$d .= $c->finish;
ok(substr($d, 0, length $p) eq $p)
 or print STDERR "[$d|",unpack('H*',$d),"]\n";

$c = Crypt::GCrypt->new(
                        type => 'cipher',
                        algorithm => 'arcfour',
                        padding => 'null'
);
ok($c->keylen == 16);
ok($c->blklen == 1);
$c->start('encrypting');
$c->setkey($key);
$e = $c->encrypt($p);
ok($e eq pack('H*', '02a98d20a176729ea7cd'))
  or print STDERR "[",unpack('H*',$e),"]\n";
$c->setkey($key);
$c->start('decrypting');
$d = $c->decrypt($e);
$d .= $c->finish;
ok(substr($d, 0, length $p) eq $p)
 or print STDERR "[$d|",unpack('H*',$d),"]\n";

### 'none' padding
{
    $c = Crypt::GCrypt->new(
                            type => 'cipher',
                            algorithm => 'aes',
                            padding => 'none'
    );
    $c->setkey($key);
    $c->start('encrypting');
    ok(!eval {my $e2 = $c->encrypt('aaa'); 1});  # this should die
    ok(eval { my $e2 = $c->encrypt('aaaaaaaaaaaaaaaa') . $c->finish; 1 });  # this should not die
}