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
|
3
|
|
|
3
|
|
15137
|
use vars qw(@ISA); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
172
|
|
14
|
3
|
|
|
3
|
|
5528
|
use Math::Pari qw(PARI pari2pv floor pari2num); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Crypt::Random qw(makerandom); |
16
|
|
|
|
|
|
|
use Digest::SHA1 qw(sha1); |
17
|
|
|
|
|
|
|
use Carp; |
18
|
|
|
|
|
|
|
require Exporter; |
19
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
@EXPORT_OK = qw(i2osp os2ip h2osp octet_xor octet_len bitsize |
22
|
|
|
|
|
|
|
generate_random_octet mgf1 steak); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub i2osp { |
26
|
|
|
|
|
|
|
my $num = PARI(shift); |
27
|
|
|
|
|
|
|
my $d = $num; |
28
|
|
|
|
|
|
|
my $l = shift || 0; |
29
|
|
|
|
|
|
|
my $base = PARI(256); my $result = ''; |
30
|
|
|
|
|
|
|
if ($l) { return if $num > $base ** $l } |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
do { |
33
|
|
|
|
|
|
|
my $r = $d % $base; |
34
|
|
|
|
|
|
|
$d = ($d-$r) / $base; |
35
|
|
|
|
|
|
|
$result = chr($r) . $result; |
36
|
|
|
|
|
|
|
} until ($d < $base); |
37
|
|
|
|
|
|
|
$result = chr($d) . $result if $d != 0; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
if (length($result) < $l) { |
40
|
|
|
|
|
|
|
$result = chr(0)x($l-length($result)) . $result; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
return $result; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub os2ip { |
47
|
|
|
|
|
|
|
my $string = shift; |
48
|
|
|
|
|
|
|
my $base = PARI(256); |
49
|
|
|
|
|
|
|
my $result = 0; |
50
|
|
|
|
|
|
|
my $l = length($string); |
51
|
|
|
|
|
|
|
for (0 .. $l-1) { |
52
|
|
|
|
|
|
|
my ($c) = unpack "x$_ a", $string; |
53
|
|
|
|
|
|
|
my $a = int(ord($c)); |
54
|
|
|
|
|
|
|
my $val = int($l-$_-1); |
55
|
|
|
|
|
|
|
$result += $a * ($base**$val); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
return $result; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub h2osp { |
62
|
|
|
|
|
|
|
my $hex = shift; |
63
|
|
|
|
|
|
|
$hex =~ s/[ \n]//ig; |
64
|
|
|
|
|
|
|
my $num = Math::Pari::_hex_cvt($hex); |
65
|
|
|
|
|
|
|
return i2osp ($num); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub generate_random_octet { |
70
|
|
|
|
|
|
|
my ( $l, $str ) = @_; |
71
|
|
|
|
|
|
|
my $r = makerandom ( Size => int($l*8), Strength => $str ); |
72
|
|
|
|
|
|
|
return i2osp ($r, $l); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub bitsize ($) { |
77
|
|
|
|
|
|
|
return pari2num(floor(Math::Pari::log(shift)/Math::Pari::log(2)) + 1); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub octet_len { |
82
|
|
|
|
|
|
|
return pari2num(floor(PARI((bitsize(shift)+7)/8))); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub octet_xor { |
87
|
|
|
|
|
|
|
my ($a, $b) = @_; my @xor; |
88
|
|
|
|
|
|
|
my @ba = split //, unpack "B*", $a; |
89
|
|
|
|
|
|
|
my @bb = split //, unpack "B*", $b; |
90
|
|
|
|
|
|
|
if (@ba != @bb) { |
91
|
|
|
|
|
|
|
if (@ba < @bb) { |
92
|
|
|
|
|
|
|
for (1..@bb-@ba) { unshift @ba, '0' } |
93
|
|
|
|
|
|
|
} else { |
94
|
|
|
|
|
|
|
for (1..@ba-@bb) { unshift @bb, '0' } |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
for (0..$#ba) { |
98
|
|
|
|
|
|
|
$xor[$_] = ($ba[$_] xor $bb[$_]) || 0; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
return pack "B*", join '',@xor; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub mgf1 { |
105
|
|
|
|
|
|
|
my ($seed, $l) = @_; |
106
|
|
|
|
|
|
|
my $hlen = 20; my ($T, $i) = ("",0); |
107
|
|
|
|
|
|
|
while ($i <= $l) { |
108
|
|
|
|
|
|
|
my $C = i2osp (int($i), 4); |
109
|
|
|
|
|
|
|
$T .= sha1("$seed$C"); |
110
|
|
|
|
|
|
|
$i += $hlen; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
my ($output) = unpack "a$l", $T; |
113
|
|
|
|
|
|
|
return $output; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub steak { |
118
|
|
|
|
|
|
|
my ($text, $blocksize) = @_; |
119
|
|
|
|
|
|
|
my $textsize = length($text); |
120
|
|
|
|
|
|
|
my $chunkcount = $textsize % $blocksize |
121
|
|
|
|
|
|
|
? int($textsize/$blocksize) + 1 : $textsize/$blocksize; |
122
|
|
|
|
|
|
|
my @segments = unpack "a$blocksize"x$chunkcount, $text; |
123
|
|
|
|
|
|
|
return @segments; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 NAME |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Crypt::RSA::DataFormat - Data creation, conversion and reporting primitives. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 DESCRIPTION |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
This module implements several data creation, conversion and reporting |
135
|
|
|
|
|
|
|
primitives used throughout the Crypt::RSA implementation. Primitives are |
136
|
|
|
|
|
|
|
available as exportable functions. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 FUNCTIONS |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=over 4 |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item B Integer, Length |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Integer To Octet String Primitive. Converts an integer into its |
145
|
|
|
|
|
|
|
equivalent octet string representation of length B. If |
146
|
|
|
|
|
|
|
necessary, the resulting string is prefixed with nulls. If |
147
|
|
|
|
|
|
|
B is not provided, returns an octet string of shortest |
148
|
|
|
|
|
|
|
possible length. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item B Hex String |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Hex To Octet String Primitive. Converts a I into its |
153
|
|
|
|
|
|
|
equivalent octet string representation and returns an octet |
154
|
|
|
|
|
|
|
string of shortest possible length. The hex string is not |
155
|
|
|
|
|
|
|
prefixed with C<0x>, etc. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item B String |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Octet String to Integer Primitive. Converts an octet string into its |
160
|
|
|
|
|
|
|
equivalent integer representation. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item B Length, Strength |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Generates a random octet string of length B. B specifies |
165
|
|
|
|
|
|
|
the degree of randomness. See Crypt::Random(3) for an explanation of the |
166
|
|
|
|
|
|
|
B parameter. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item B Integer |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Returns the length of the B in bits. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item B Integer |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Returns the octet length of the integer. If the length is not a whole |
175
|
|
|
|
|
|
|
number, the fractional part is dropped to make it whole. |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item B String1, String2 |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
Returns the result of B XOR B. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item B String, Length |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Returns an array of segments of length B from B. The final |
184
|
|
|
|
|
|
|
segment can be smaller than B. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 AUTHOR |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Vipul Ved Prakash, Email@vipul.netE |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
|