ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-FCP/FCP/Key/CHK.pm
Revision: 1.5
Committed: Thu May 13 21:43:16 2004 UTC (22 years, 4 months ago) by root
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Net::FCP::Key::CHK - manage CHK keys.
4
5 =head1 SYNOPSIS
6
7 use Net::FCP::Key::CHK;
8
9 my $key = new Net::FCP::Key::CHK;
10 my $key = new_from_uri Net::FCP::Key::CHK $uri;
11 my $key = new_from_data Net::FCP::Key::CHK $data, $metadata;
12 ... more to come
13
14
15 =head1 DESCRIPTION
16
17 =back
18
19 =head2 THE Net::FCP::Key::CHK CLASS
20
21 =over 4
22
23 =cut
24
25 package Net::FCP::Key::CHK;
26
27 use Carp;
28 use Digest::SHA1;
29 use MIME::Base64;
30
31 use Crypt::Rijndael;
32 use Crypt::Twofish;
33
34 use Net::FCP::Util;
35
36 no warnings;
37
38 =item my $key = new Net::FCP::Key::CHK;
39
40 Heavily under development, don't use :)
41
42 =cut
43
44 sub new {
45 my $class = shift;
46
47 bless { }, $class;
48 }
49
50 =item my $key = new_from_data Net::FCP::Key::CHK $metadata, $data, $cipher;
51
52 Generate a CHK from the given data and metadata strings.
53
54 =cut
55
56 sub new_from_data {
57 my ($class, $metadata, $data, $cipher) = @_;
58
59 $class->new->set_data ($metadata, $data, $cipher);
60 }
61
62 sub rolling_hashpad($$$) {
63 my $sha1 = $_[1];
64 my $pad = "";
65 my $dig;
66
67 while ($_[2] > length $_[0]) {
68 $sha1->add ($dig = $sha1->digest_noreset);
69 $pad .= $dig;
70 $_[0] .= $pad;
71 }
72
73 substr $_[0], $_[2], length $_[0], "";
74 }
75
76 sub encode_number($) {
77 my $num = pack "N", $_[0];
78 $num =~ s/^\x00+//;
79 pack "n a*", length $num, $num;
80 }
81
82 sub set_data {
83 my ($self, $metadata, $data, $cipher) = @_;
84
85 $cipher ||= "Twofish";
86
87 my $cipher_class = "Crypt::$cipher";
88
89 $cipher_class->blocksize == 16 or die "only ciphers with a blocksize of 128 bits are supported";
90
91 my $total_len = (length $metadata) + (length $data);
92 my $padded_log = Net::FCP::Util::log2 $total_len, 10;
93 my $padded_len = 1 << $padded_log;
94
95 my $plaintext = "$metadata$data";
96
97 # crypto key (hash) generation. this is an iterative
98 # algorithm, but it is "unrolled" here for the
99 # common keysize of 16 bytes.
100 my $data_sha1 = Digest::SHA1->new->add ($plaintext);
101
102 # only works for 128 bit keys
103 my $k = new Digest::SHA1;
104 $k->add ("\x00" x 1);
105 $k->add ($data_sha1->clone->digest);
106
107 my $hash = substr $k->digest, 0, 16; # extract leading 128 bit
108
109 my $buf = "";
110
111 $buf .= pack "n a20", 20, Digest::SHA1::sha1 $hash;
112
113 $buf .= encode_number $total_len;
114 $buf .= encode_number length $metadata;
115
116 $buf .= "\x00\x00";
117
118 rolling_hashpad $buf, Digest::SHA1->new->add ($buf), 1 << Net::FCP::Util::log2 length $buf;
119
120 my $pcfb_cipher = $cipher_class->new ($hash);
121 my $pcfb_reg = "\x00" x 16;
122
123 my $pcfb_enc = sub {
124 my $length = length $_[0];
125 my $enc = "";
126 for (my $i = 0; $i < $length; $i += 16) {
127 $enc .= $pcfb_reg = $pcfb_cipher->encrypt ($pcfb_reg) ^ substr $_[0], $i, 16;
128 }
129 $enc;
130 };
131
132 # buf length must be multiple of 16
133 $buf = $pcfb_enc->($buf);
134
135 my $senc = unpack "H*", $buf;
136
137 rolling_hashpad $plaintext, $data_sha1, $padded_len;
138
139 # plaintext length must be a multiple of 16, too
140 $plaintext = $pcfb_enc->($plaintext);
141
142 my $partsize = $padded_len < 16384 ? $padded_len
143 : $padded_len < 16384 << 7 ? 16384
144 : $padded_len >> 7; # 2MB
145
146 my $dig = "";
147 for (my $ofs = ($padded_len-1) - ($padded_len-1) % $partsize; $ofs >= 0; $ofs -= $partsize) {
148 $dig = Digest::SHA1::sha1 substr ($plaintext, $ofs, $partsize) . $dig;
149 }
150
151 my $sini = unpack "H*", $dig;
152
153 my $route = sprintf
154 "Document-header\xfe%s\xff"
155 . "Initial-digest\xfe%s\xff"
156 . "Part-size\xfe%x\xff"
157 . "Symmetric-cipher\xfe%s\xff",
158 $senc, $sini, $partsize, $cipher;
159
160 $route = Net::FCP::Util::encode_base64 +(Digest::SHA1::sha1 $route) . (pack "C", $padded_log) . "\x03\x02";
161 $hash = Net::FCP::Util::encode_base64 $hash;
162
163 return "freenet:CHK\@$route,$hash";
164 $self;
165 }
166
167 =item $size = $key->size
168
169 Returns the size of the data (in bytes).
170
171 =cut
172
173 =item $digest = $key->digest
174
175 Return the store digest/hash.
176
177 =cut
178
179 =item $keynum = $key->keynumber
180
181 Returns the keynumber (version?)
182
183 =cut
184
185 =item $chk = $key->chk
186
187 Return the full CHK.
188
189 =cut
190
191 =back
192
193 =head1 SEE ALSO
194
195 L<Net::FCP>.
196
197 =head1 BUGS
198
199 Not heavily tested.
200
201 =head1 AUTHOR
202
203 Marc Lehmann <pcg@goof.com>
204 http://www.goof.com/pcg/marc/
205
206 =cut
207
208 1;
209