chiark / gitweb /
7a8074fc9cb8ac11f26dc2e8cc4261a59c2e5dbb
[catacomb-perl] / t / key.t
1 # -*-mode: perl; comment-column: 68-*-
2 use Test;
3 BEGIN { plan tests => 19; }
4 use Catacomb qw(:const mp);
5
6 sub dumphash {
7   my ($n, $h) = @_;
8   print "# $n\n";
9   foreach my $k (keys %$h) {
10     print "#   $k -> $h->{$k}\n";
11   }
12 }
13
14 sub 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
39 ok $d->flags() == KENC_STRUCT;                                      #t    4
40 $h = $d->structopen();
41 ok exists $h->{"p"};                                                #t    5
42 ok !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());
47 ok +($p * $r)->atinfp();                                            #t    7
48
49 $h = $k->attrs;
50 ok 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");
58 ok $k->type, "rsa";                                                 #t    9
59 ok $d->flags == KENC_ENCRYPT;                                       #t   10
60 ok $n, sprintf("%08x:rsa.private", $k->id);                         #t   11
61
62 $h = $f->bytag("rsa")->data()->structfind("private")
63   ->unlock("pass")->structopen();
64 ok 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");
70 ok $rest, ",zqzqv";                                                 #t   13
71 ok defined $kd;                                                     #t   14
72 $h = $kd->structopen(); ok defined $h;                              #t   15
73 ok $h->{"p"}->getmp() == 23;                                        #t   16
74 ok $h->{"q"}->getmp() == 11;                                        #t   17
75 $pkd = $kd->lock("passphrase");
76 ok !defined $pkd->unlock("wrong");                                  #t   18
77 $ukd = $pkd->unlock("passphrase"); ok defined $ukd;                 #t   19