File Coverage

lib/Crypt/RSA/DataFormat.pm
Criterion Covered Total %
statement 74 79 93.7
branch 13 14 92.9
condition 8 8 100.0
subroutine 14 15 93.3
pod 8 9 88.9
total 117 125 93.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -s
2             ##
3             ## Crypt::RSA::DataFormat -- Functions for converting, shaping and
4             ## creating and reporting data formats.
5             ##
6             ## Copyright (c) 2001, Vipul Ved Prakash. All rights reserved.
7             ## This code is free software; you can redistribute it and/or modify
8             ## it under the same terms as Perl itself.
9             ##
10             ## $Id: DataFormat.pm,v 1.13 2001/05/20 23:37:45 vipul Exp $
11              
12             package Crypt::RSA::DataFormat;
13 15     15   354 use lib qw(lib);
  15         316  
  15         274  
14 15     15   236 use vars qw(@ISA);
  15         236  
  15         235  
15 15     15   421 use Math::Pari qw(PARI pari2pv floor pari2num);
  15         239  
  15         278  
16 15     15   496 use Crypt::Random qw(makerandom);
  15         151  
  15         242  
17 15     15   3045 use Digest::SHA1 qw(sha1);
  15         163  
  15         223  
18 15     15   1190 use Carp;
  15         154  
  15         378  
19             require Exporter;
20             @ISA = qw(Exporter);
21              
22             @EXPORT_OK = qw(i2osp os2ip h2osp octet_xor octet_len bitsize
23             generate_random_octet mgf1 steak);
24              
25              
26             sub i2osp {
27 5766     5766 1 601686     my $num = PARI(shift);
28 5766         275916     my $d = $num;
29 5766   100     287511     my $l = shift || 0;
30 5766         286566     my $base = PARI(256); my $result = '';
  5766         509304  
31 5766 100       75299     if ($l) { return if $num > $base ** $l }
  5455 100       116400  
32              
33 5566         93317     do {
34 84929         2748978         my $r = $d % $base;
35 84929         4556607         $d = ($d-$r) / $base;
36 84929         5905519         $result = chr($r) . $result;
37                 } until ($d < $base);
38 5566 100       138061     $result = chr($d) . $result if $d != 0;
39              
40 5566 100       485350     if (length($result) < $l) {
41 3957         48798         $result = chr(0)x($l-length($result)) . $result;
42                 }
43 5566         105786     return $result;
44             }
45              
46              
47             sub os2ip {
48 1504     1504 1 16212     my $string = shift;
49 1504         23691     my $base = PARI(256);
50 1504         224466     my $result = 0;
51 1504         14708     my $l = length($string);
52 1504         17986     for (0 .. $l-1) {
53 90469         3139050         my ($c) = unpack "x$_ a", $string;
54 90469         2895542         my $a = int(ord($c));
55 90469         2650881         my $val = int($l-$_-1);
56 90469         7144769         $result += $a * ($base**$val);
57                 }
58 1504         25099     return $result;
59             }
60              
61              
62             sub h2osp {
63 307     307 1 3015     my $hex = shift;
64 307         17059     $hex =~ s/[ \n]//ig;
65 307         4378     my $num = Math::Pari::_hex_cvt($hex);
66 307         279161     return i2osp ($num);
67             }
68              
69              
70             sub generate_random_octet {
71 0     0 1 0     my ( $l, $str ) = @_;
72 0         0     my $r = makerandom ( Size => int($l*8), Strength => $str );
73 0         0     return i2osp ($r, $l);
74             }
75              
76              
77             sub bitsize ($) {
78 2240     2240 1 1065243     return pari2num(floor(Math::Pari::log(shift)/Math::Pari::log(2)) + 1);
79             }
80              
81              
82             sub octet_len {
83 1516     1516 1 19515     return pari2num(floor(PARI((bitsize(shift)+7)/8)));
84             }
85              
86              
87             sub octet_xor {
88 1666     1666 1 31920     my ($a, $b) = @_; my @xor;
  1666         14673  
89 1666         2270453     my @ba = split //, unpack "B*", $a;
90 1666         2709135     my @bb = split //, unpack "B*", $b;
91 1666 100       89996     if (@ba != @bb) {
92 2 50       22         if (@ba < @bb) {
93 0         0             for (1..@bb-@ba) { unshift @ba, '0' }
  0         0  
94                     } else {
95 2         23             for (1..@ba-@bb) { unshift @bb, '0' }
  256         2432  
96                     }
97                 }
98 1666         242873     for (0..$#ba) {
99 408744   100     23662697         $xor[$_] = ($ba[$_] xor $bb[$_]) || 0;
      100        
100                 }
101 1666         704199     return pack "B*", join '',@xor;
102             }
103              
104              
105             sub mgf1 {
106 1663     1663 0 26286     my ($seed, $l) = @_;
107 1663         21724     my $hlen = 20; my ($T, $i) = ("",0);
  1663         19340  
108 1663         21822     while ($i <= $l) {
109 3953         265732         my $C = i2osp (int($i), 4);
110 3953         71401         $T .= sha1("$seed$C");
111 3953         255629         $i += $hlen;
112                 }
113 1663         23838     my ($output) = unpack "a$l", $T;
114 1663         30069     return $output;
115             }
116              
117              
118             sub steak {
119 6     6 1 3270     my ($text, $blocksize) = @_;
120 6         90     my $textsize = length($text);
121 6 100       667     my $chunkcount = $textsize % $blocksize
122                     ? int($textsize/$blocksize) + 1 : $textsize/$blocksize;
123 6         1104     my @segments = unpack "a$blocksize"x$chunkcount, $text;
124 6         722     return @segments;
125             }
126              
127             1;
128              
129             =head1 NAME
130            
131             Crypt::RSA::DataFormat - Data creation, conversion and reporting primitives.
132            
133             =head1 DESCRIPTION
134            
135             This module implements several data creation, conversion and reporting
136             primitives used throughout the Crypt::RSA implementation. Primitives are
137             available as exportable functions.
138            
139             =head1 FUNCTIONS
140            
141             =over 4
142            
143             =item B<i2osp> Integer, Length
144            
145             Integer To Octet String Primitive. Converts an integer into its
146             equivalent octet string representation of length B<Length>. If
147             necessary, the resulting string is prefixed with nulls. If
148             B<Length> is not provided, returns an octet string of shortest
149             possible length.
150            
151             =item B<h2osp> Hex String
152            
153             Hex To Octet String Primitive. Converts a I<hex string> into its
154             equivalent octet string representation and returns an octet
155             string of shortest possible length. The hex string is not
156             prefixed with C<0x>, etc.
157            
158             =item B<os2ip> String
159            
160             Octet String to Integer Primitive. Converts an octet string into its
161             equivalent integer representation.
162            
163             =item B<generate_random_octet> Length, Strength
164            
165             Generates a random octet string of length B<Length>. B<Strength> specifies
166             the degree of randomness. See Crypt::Random(3) for an explanation of the
167             B<Strength> parameter.
168            
169             =item B<bitsize> Integer
170            
171             Returns the length of the B<Integer> in bits.
172            
173             =item B<octet_len> Integer
174            
175             Returns the octet length of the integer. If the length is not a whole
176             number, the fractional part is dropped to make it whole.
177            
178             =item B<octet_xor> String1, String2
179            
180             Returns the result of B<String1> XOR B<String2>.
181            
182             =item B<steak> String, Length
183            
184             Returns an array of segments of length B<Length> from B<String>. The final
185             segment can be smaller than B<Length>.
186            
187             =back
188            
189             =head1 AUTHOR
190            
191             Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>
192            
193             =cut
194              
195              
196