Entropy/RawSource/CryptCounter.pm

filter: Statementstmt 83.4% 131 / 157 Branchbran 58.1% 43 / 74 Conditioncond 38.4% 10 / 26 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 96.4% 27 / 28 Podpod 100% 14 / 14 totaltotal 72.5% 227 / 313 CC 47 SCAR 25.2
CC
47
Cov
72%
sysseek
33.8
setpos
19.4
read
10.0
CRAP 12.4
« Entropy/Algorithms.pm ↑ Summary Entropy/Source.pm »
1 =head1 NAME
2  
3 Data::Entropy::RawSource::CryptCounter - counter mode of block cipher
4 as I/O handle
5  
6 =head1 SYNOPSIS
7  
8 use Data::Entropy::RawSource::CryptCounter;
9  
10 my $rawsrc = Data::Entropy::RawSource::CryptCounter
11 ->new(Crypt::Rijndael->new($key));
12  
13 $c = $rawsrc->getc;
14 # and the rest of the I/O handle interface
15  
16 =head1 DESCRIPTION
17  
18 This class provides an I/O handle connected to a virtual file which
19 contains the output of a block cipher in counter mode. This makes a
20 good source of pseudorandom bits. The handle implements a substantial
21 subset of the interfaces described in L<IO::Handle> and L<IO::Seekable>.
22  
23 For use as a general entropy source, it is recommended to wrap an object
24 of this class using C<Data::Entropy::Source>, which provides methods to
25 extract entropy in more convenient forms than mere octets.
26  
27 The amount of entropy the virtual file actually contains is only the
28 amount that is in the key, which is at most the length of the key.
29 It superficially appears to be much more than this, if (and to the
30 extent that) the block cipher is secure. This technique is not
31 suitable for all problems, and requires a careful choice of block
32 cipher and keying method. Applications requiring true entropy
33 should generate it (see L<Data::Entropy::RawSource::Local>).
34  
35 =cut
36  
37 package Data::Entropy::RawSource::CryptCounter;
38  
39 4 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 4 ▶use Params::Classify 0.000 qw(is_number is_ref is_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 our $VERSION = "0.010";
46  
47 =head1 CONSTRUCTOR
48  
49 =over
50  
51 =item Data::Entropy::RawSource::CryptCounter->new(KEYED_CIPHER)
52  
53 KEYED_CIPHER must be a cipher object supporting the standard C<blocksize>
54 and C<encrypt> methods. For example, an instance of C<Crypt::Rijndael>
55 (with the default C<MODE_ECB>) would be appropriate. A handle object
56 is created and returned which refers to a virtual file containing the
57 output of the cipher's counter mode.
58  
59 =cut
60  
61 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
62 3 my($class, $cipher) = @_;
63 3 return bless({
64 cipher => $cipher,
65 blksize => $cipher->blocksize,
66 counter => "\0" x $cipher->blocksize,
67 subpos => 0,
68 }, $class);
69 }
70  
71 =back
72  
73 =head1 METHODS
74  
75 A subset of the interfaces described in L<IO::Handle> and L<IO::Seekable>
76 are provided:
77  
78 =over
79  
80 =item $rawsrc->read(BUFFER, LENGTH[, OFFSET])
81  
82 =item $rawsrc->getc
83  
84 =item $rawsrc->ungetc(ORD)
85  
86 =item $rawsrc->eof
87  
88 Buffered reading from the source, as in L<IO::Handle>.
89  
90 =item $rawsrc->sysread(BUFFER, LENGTH[, OFFSET])
91  
92 Unbuffered reading from the source, as in L<IO::Handle>.
93  
94 =item $rawsrc->close
95  
96 Does nothing.
97  
98 =item $rawsrc->opened
99  
100 Retruns true to indicate that the source is available for I/O.
101  
102 =item $rawsrc->clearerr
103  
104 =item $rawsrc->error
105  
106 Error handling, as in L<IO::Handle>.
107  
108 =item $rawsrc->getpos
109  
110 =item $rawsrc->setpos(POS)
111  
112 =item $rawsrc->tell
113  
114 =item $rawsrc->seek(POS, WHENCE)
115  
116 Move around within the buffered source, as in L<IO::Seekable>.
117  
118 =item $rawsrc->sysseek(POS, WHENCE)
119  
120 Move around within the unbuffered source, as in L<IO::Seekable>.
121  
122 =back
123  
124 The buffered (C<read> et al) and unbuffered (C<sysread> et al) sets
125 of methods are interchangeable, because no such distinction is made by
126 this class.
127  
128 C<tell>, C<seek>, and C<sysseek> only work within the first 4 GiB of the
129 virtual file. The file is actually much larger than that: for Rijndael
130 (AES), or any other cipher with a 128-bit block, the file is 2^52 YiB
131 (2^132 B). C<getpos> and C<setpos> work throughout the file.
132  
133 Methods to write to the file are unimplemented because the virtual file
134 is fundamentally read-only.
135  
136 =cut
137  
138 60047 ▶sub _ensure_buffer {
Subroutine _ensure_buffer: called CC=2 SCAR=6.9
139 60047 my($self) = @_;
140 $self->{buffer} = $self->{cipher}->encrypt($self->{counter})
141 60047 ▶ unless exists $self->{buffer};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"buffer"} 3763 56284 60047
142 }
143  
144 3764 ▶sub _clear_buffer {
Subroutine _clear_buffer: called CC=1 SCAR=0.0
145 3764 my($self) = @_;
146 3764 delete $self->{buffer};
147 }
148  
149 3755 ▶sub _increment_counter {
Subroutine _increment_counter: called CC=3 SCAR=11.1
150 3755 my($self) = @_;
151 3755 ▶ for(my $i = 0; $i != $self->{blksize}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $self->{"blksize"}) 3769 0 3769
152 3769 my $c = ord(substr($self->{counter}, $i, 1));
153 3769 ▶ unless($c == 255) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c == 255) 3755 14 3769
154 3755 substr $self->{counter}, $i, 1, chr($c + 1);
155 3755 return;
156 }
157 14 substr $self->{counter}, $i, 1, "\0";
158 }
159 0 $self->{counter} = undef;
160 }
161  
162 0 ▶sub _decrement_counter {
Subroutine _decrement_counter: not called CC=2 SCAR=17.9
163 0 my($self) = @_;
164 0 for(my $i = 0; ; $i++) {
165 0 my $c = ord(substr($self->{counter}, $i, 1));
166 0 ▶ unless($c == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c == 0) 0 0 0
167 0 substr $self->{counter}, $i, 1, chr($c - 1);
168 0 return;
169 }
170 0 substr $self->{counter}, $i, 1, "\xff";
171 }
172 }
173  
174 1 ▶sub close { 1 }
Subroutine close: called CC=1 SCAR=0.0
Pod documented
175  
176 1 ▶sub opened { 1 }
Subroutine opened: called CC=1 SCAR=0.0
Pod documented
177  
178 1 ▶sub error { 0 }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
179  
180 1 ▶sub clearerr { 0 }
Subroutine clearerr: called CC=1 SCAR=0.0
Pod documented
181  
182 60039 ▶sub getc {
Subroutine getc: called CC=3 SCAR=11.0
Pod documented
183 60039 my($self) = @_;
184 60039 ▶ return undef unless defined $self->{counter};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"counter"} 0 60039 60039
185 60039 $self->_ensure_buffer;
186 60039 my $ret = substr($self->{buffer}, $self->{subpos}, 1);
187 60039 ▶ if(++$self->{subpos} == $self->{blksize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->{"subpos"} == $self->{"blksize"}) 3752 56287 60039
188 3752 $self->_increment_counter;
189 3752 $self->{subpos} = 0;
190 3752 $self->_clear_buffer;
191 }
192 60039 return $ret;
193 }
194  
195 1 ▶sub ungetc {
Subroutine ungetc: called CC=3 SCAR=15.7
Pod documented
196 1 my($self, undef) = @_;
197 1 ▶ unless($self->{subpos} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"subpos"} == 0) 1 0 1
198 1 $self->{subpos}--;
199 1 return;
200 }
201 0 ▶ return if $self->{counter} =~ /\A\0*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"counter"} =~ /\A\0*\z/ 0 0 0
202 0 $self->_decrement_counter;
203 0 $self->{subpos} = $self->{blksize} - 1;
204 0 $self->_clear_buffer;
205 }
206  
207 7 ▶sub read {
Subroutine read: called CC=10 SCAR=23.1
Pod documented
208 7 my($self, undef, $length, $offset) = @_;
209 7 ▶ return undef if $length < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $length < 0 0 7 7
210 7 ▶ $_[1] = "" unless defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 1 6 7
211 7 ▶ if(!defined($offset)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $offset) { } 4 3 7
elsif ($offset < 0) { } 1 2 3
elsif ($offset > length $_[1]) { } 1 1 2
212 4 $offset = 0;
213 4 $_[1] = "";
214 } elsif($offset < 0) {
215 1 ▶ return undef if $offset < -length($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $offset < -length($_[1]) 0 1 1
216 1 substr $_[1], $offset, -$offset, "";
217 1 $offset = length($_[1]);
218 } elsif($offset > length($_[1])) {
219 1 $_[1] .= "\0" x ($offset - length($_[1]));
220 } else {
221 1 substr $_[1], $offset, length($_[1]) - $offset, "";
222 }
223 7 my $original_offset = $offset;
224 7 ▶ while($length != 0 && defined($self->{counter})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length != 0 and defined $self->{"counter"}) 8 2 10
Condition66%2/3
$length != 0 and defined $self->{"counter"}
!ll&&!rl&&r
208
MC/DC50%1/2
$length != 0 and defined $self->{"counter"}
$length != 0 defined $self->{"counter"}
Truth table66%2/3
A and B
A= $length != 0B= defined $self->{"counter"}
ABresult
0X0
100
111
225 8 $self->_ensure_buffer;
226 8 my $avail = $self->{blksize} - $self->{subpos};
227 8 ▶ if($length < $avail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length < $avail) 5 3 8
228 $_[1] .= substr($self->{buffer}, $self->{subpos},
229 5 $length);
230 5 $offset += $length;
231 5 $self->{subpos} += $length;
232 5 last;
233 }
234 3 $_[1] .= substr($self->{buffer}, $self->{subpos}, $avail);
235 3 $offset += $avail;
236 3 $length -= $avail;
237 3 $self->_increment_counter;
238 3 $self->{subpos} = 0;
239 3 $self->_clear_buffer;
240 }
241 7 return $offset - $original_offset;
242 }
243  
244 4 *sysread = \&read;
245  
246 20 ▶sub tell {
Subroutine tell: called CC=6 SCAR=19.7
Pod documented
247 20 my($self) = @_;
248 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 20 my $ctr = $self->{counter};
250 20 my $nblocks;
251 20 ▶ if(defined $ctr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctr) { } 20 0 20
252 20 ▶ return -1 if $ctr =~ /\A.{4,}[^\0]/s;
Branch: 1 branch
BranchTrueFalseTotal
if $ctr =~ /\A.{4,}[^\0]/s 0 20 20
253 20 ▶ $ctr .= "\0\0\0\0" if $self->{blksize} < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"blksize"} < 4 0 20 20
254 20 $nblocks = unpack("V", $ctr);
255 } else {
256 0 ▶ return -1 if $self->{blksize} >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"blksize"} >= 4 0 0 0
257 0 $nblocks = 1 << ($self->{blksize} << 3);
258 }
259 20 my $pos = $nblocks * $self->{blksize} + $self->{subpos};
260 20 ▶ return -1 unless ($pos-$self->{subpos}) / $self->{blksize} == $nblocks;
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos - $self->{"subpos"}) / $self->{"blksize"} == $nblocks 0 20 20
261 20 return $pos;
262 }
263  
264 4 ▶use constant SEEK_SET => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
265 4 ▶use constant SEEK_CUR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
266 4 ▶use constant SEEK_END => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
267  
268 19 ▶sub sysseek {
Subroutine sysseek: called CC=15 SCAR=35.2
Pod documented
269 19 my($self, $offset, $whence) = @_;
270 19 ▶ if($whence == SEEK_SET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($whence == 0) { } 11 8 19
elsif ($whence == 1) { } 7 1 8
elsif ($whence == 2) { } 1 0 1
271 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 11 ▶ return undef if $offset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset < 0 3 8 11
273 8 my $ctr = $offset / $self->{blksize};
274 8 my $subpos = $offset % $self->{blksize};
275 8 $ctr = pack("V", $ctr);
276 8 ▶ if($self->{blksize} < 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'blksize'} < 4) { } 0 8 8
277 return undef unless
278 my $chopped = substr($ctr, $self->{blksize},
279 0 ▶ 4-$self->{blksize}, "");
Branch: 1 branch
BranchTrueFalseTotal
unless my $chopped = substr($ctr, $self->{"blksize"}, 4 - $self->{"blksize"}, "") 0 0 0
280 0 ▶ if($chopped =~ /\A\x{01}\0*\z/ && $subpos == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chopped =~ /\A\x{01}\0*\z/ and $subpos == 0) { } 0 0 0
elsif (not $chopped =~ /\A\0+\z/) { } 0 0 0
Condition0%0/3
$chopped =~ /\A\x{01}\0*\z/ and $subpos == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$chopped =~ /\A\x{01}\0*\z/ and $subpos == 0
$chopped =~ /\A\x{01}\0*\z/ $subpos == 0
Truth table0%0/3
A and B
A= $chopped =~ /\A\x{01}\0*\z/B= $subpos == 0
ABresult
0X0
100
111
281 0 $self->{counter} = undef;
282 0 $self->{subpos} = 0;
283 0 $self->_clear_buffer;
284 0 return $offset;
285 } elsif($chopped !~ /\A\0+\z/) {
286 0 return undef;
287 }
288 } else {
289 8 $ctr .= "\0" x ($self->{blksize} - 4);
290 }
291 8 $self->{counter} = $ctr;
292 8 $self->{subpos} = $subpos;
293 8 $self->_clear_buffer;
294 8 ▶ return $offset || "0 but true";
Condition100%2/2
$offset || '0 but true'
l!l
62
MC/DC100%1/1
$offset || '0 but true'
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
295 } elsif($whence == SEEK_CUR) {
296 7 my $pos = $self->tell;
297 7 ▶ return undef if $pos == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pos == -1 0 7 7
298 7 return $self->sysseek($pos + $offset, SEEK_SET);
299 } elsif($whence == SEEK_END) {
300 4 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
301 1 ▶ return undef if $offset > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $offset > 0 0 1 1
302 1 ▶ return undef if $self->{blksize} >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"blksize"} >= 4 1 0 1
303 0 my $nblocks = 1 << ($self->{blksize} << 3);
304 0 my $pos = $nblocks * $self->{blksize};
305 0 ▶ return undef unless $pos/$self->{blksize} == $nblocks;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos / $self->{"blksize"} == $nblocks 0 0 0
306 0 return $self->sysseek($pos + $offset, SEEK_SET);
307 } else {
308 0 return undef;
309 }
310 }
311  
312 7 ▶sub seek { shift->sysseek(@_) ? 1 : 0 }
Subroutine seek: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
(shift())->sysseek(@_) ? : 4 3 7
313  
314 1 ▶sub getpos {
Subroutine getpos: called CC=1 SCAR=0.0
Pod documented
315 1 my($self) = @_;
316 1 return [ $self->{counter}, $self->{subpos} ];
317 }
318  
319 1 ▶sub setpos {
Subroutine setpos: called CC=10 SCAR=29.6
Pod documented
320 1 my($self, $pos) = @_;
321 1 ▶ return undef unless is_ref($pos, "ARRAY") && @$pos == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless rand $pos and @$pos == 2 0 1 1
Condition33%1/3
rand $pos and @$pos == 2
!ll&&!rl&&r
001
MC/DC0%0/2
rand $pos and @$pos == 2
rand $pos @$pos == 2
Truth table33%1/3
A and B
A= rand $posB= @$pos == 2
ABresult
0X0
100
111
322 1 my($ctr, $subpos) = @$pos;
323 1 ▶ unless(!defined($ctr) && $subpos == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $ctr and $subpos == 0) 1 0 1
Condition33%1/3
not defined $ctr and $subpos == 0
!ll&&!rl&&r
100
MC/DC0%0/2
not defined $ctr and $subpos == 0
not defined $ctr $subpos == 0
Truth table33%1/3
not(A) and B
A= not defined $ctrB= $subpos == 0
ABresult
0X0
100
111
324 return undef unless is_string($ctr) &&
325 length($ctr) == $self->{blksize} &&
326 is_number($subpos) &&
327 1 ▶ $subpos >= 0 && $subpos < $self->{blksize};
Branch: 1 branch
BranchTrueFalseTotal
unless rand $ctr and length $ctr == $self->{"blksize"} and &is_number($subpos) and $subpos >= 0 and $subpos < $self->{"blksize"} 0 1 1
Condition33%4/12
rand $ctr and length $ctr == $self->{"blksize"} and &is_number($subpos) and $subpos >= 0 and $subpos < $self->{"blksize"}
!ll&&!rl&&r
001
rand $ctr and length $ctr == $self->{"blksize"} and &is_number($subpos) and $subpos >= 0
!ll&&!rl&&r
001
rand $ctr and length $ctr == $self->{"blksize"} and &is_number($subpos)
!ll&&!rl&&r
001
rand $ctr and length $ctr == $self->{"blksize"}
!ll&&!rl&&r
001
MC/DC0%0/5
rand $ctr and length $ctr == $self->{"blksize"} and &is_number($subpos) and $subpos >= 0 and $subpos < $self->{"blksize"}
rand $ctr length $ctr == $self->{"blksize"} &is_number($subpos) $subpos >= 0 $subpos < $self->{"blksize"}
Truth table16%1/6
A and B and C and D and E
A= rand $ctrB= length $ctr == $self->{"blksize"}C= &is_number($subpos)D= $subpos >= 0E= $subpos < $self->{"blksize"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
328 }
329 1 $self->{counter} = $ctr;
330 1 $self->{subpos} = $subpos;
331 1 $self->_clear_buffer;
332 1 return "0 but true";
333 }
334  
335 1 ▶sub eof {
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
336 1 my($self) = @_;
337 1 return !defined($self->{counter});
338 }
339  
340 =head1 SEE ALSO
341  
342 L<Crypt::Rijndael>,
343 L<Data::Entropy::RawSource::Local>,
344 L<Data::Entropy::Source>
345  
346 =head1 AUTHOR
347  
348 Andrew Main (Zefram) <zefram@fysh.org>
349  
350 Maintained by Robert Rothenberg <perl@rhizomnic.com>
351  
352 =head1 COPYRIGHT
353  
354 Copyright (C) 2006, 2007, 2009, 2011, 2025-2026
355 Andrew Main (Zefram) <zefram@fysh.org>
356  
357 =head1 LICENSE
358  
359 This module is free software; you can redistribute it and/or modify it
360 under the same terms as Perl itself.
361  
362 =cut
363  
364 4 1;
« Entropy/Algorithms.pm ↑ Summary Entropy/Source.pm »