Much wider support for Catacomb in all its glory.
[catacomb-perl] / t / key.t
CommitLineData
fcd15e0b 1# -*-mode: perl; comment-column: 68-*-
2use Test;
3BEGIN { plan tests => 19; }
4use Catacomb qw(:const mp);
5
6sub dumphash {
7 my ($n, $h) = @_;
8 print "# $n\n";
9 foreach my $k (keys %$h) {
10 print "# $k -> $h->{$k}\n";
11 }
12}
13
14sub checkhash {
15 my ($t, $r) = @_;
16 my @t = sort keys(%$t);
17 my @r = sort keys(%$r);
18 unless (@t == @r) {
19 print "# key count: ", scalar(@t), " != ", scalar(@r), "\n";
20 dumphash "t", $t;
21 dumphash "r", $r;
22 return undef;
23 }
24 for (my $i = 0; $i < @t; $i++) {
25 unless ($t[$i] eq $r[$i] && $t->{$t[$i]} eq $r->{$r[$i]}) {
26 print "# hash: $t[$i] -> $t->{$t[$i]} != $r[$i] -> $r->{$r[$i]}\n";
27 dumphash "t", $t;
28 dumphash "r", $r;
29 return undef;
30 }
31 }
32 return 1;
33}
34
35# Simple stuff
36$f = Catacomb::Key::File->new("keyring"); ok defined $f; #t 1
37$k = $f->bytag("tux"); ok defined $k; #t 2
38$d = $k->data(); ok defined $d; #t 3
39ok $d->flags() == KENC_STRUCT; #t 4
40$h = $d->structopen();
41ok exists $h->{"p"}; #t 5
42ok !exists $h->{"bogus"}; #t 6
43
44($C, undef, $r) =
45 Catacomb::EC::Curve->getinfo($h->{"curve"}->getstring());
46$p = $C->pt($h->{"p"}->getec());
47ok +($p * $r)->atinfp(); #t 7
48
49$h = $k->attrs;
50ok checkhash $h, { #t 8
51 "hash" => "sha256",
52 "mac" => "sha256-hmac/128",
53 "cipher" => "blowfish-cbc"
54};
55
56
57($k, $d, $n) = $f->qtag("rsa.private");
58ok $k->type, "rsa"; #t 9
59ok $d->flags == KENC_ENCRYPT; #t 10
60ok $n, sprintf("%08x:rsa.private", $k->id); #t 11
61
62$h = $f->bytag("rsa")->data()->structfind("private")
63 ->unlock("pass")->structopen();
64ok defined $h; #t 12
65
66
67# Key data
68($kd, $rest) = Catacomb::Key::Data->read
69 ("struct:[p=integer,public:23,q=integer,public:11],zqzqv");
70ok $rest, ",zqzqv"; #t 13
71ok defined $kd; #t 14
72$h = $kd->structopen(); ok defined $h; #t 15
73ok $h->{"p"}->getmp() == 23; #t 16
74ok $h->{"q"}->getmp() == 11; #t 17
75$pkd = $kd->lock("passphrase");
76ok !defined $pkd->unlock("wrong"); #t 18
77$ukd = $pkd->unlock("passphrase"); ok defined $ukd; #t 19