summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl/lib/Crypt/RSA/DataFormat.pm
blob: 07edffd5340a50717f9c5ebf859c267a09d1a6cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/perl -s
##
## Crypt::RSA::DataFormat -- Functions for converting, shaping and 
##                           creating and reporting data formats.
##
## Copyright (c) 2001, Vipul Ved Prakash.  All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: DataFormat.pm,v 1.13 2001/05/20 23:37:45 vipul Exp $

package Crypt::RSA::DataFormat; 
use vars qw(@ISA);
use Math::Pari qw(PARI pari2pv floor pari2num);
use Crypt::Random qw(makerandom);
use Digest::SHA1 qw(sha1);
use Carp;
require Exporter;
@ISA = qw(Exporter);

@EXPORT_OK = qw(i2osp os2ip h2osp octet_xor octet_len bitsize 
                generate_random_octet mgf1 steak);


sub i2osp {
    my $num = PARI(shift); 
    my $d = $num;
    my $l = shift || 0;
    my $base = PARI(256); my $result = '';
    if ($l) { return if $num > $base ** $l }

    do { 
        my $r = $d % $base;
        $d = ($d-$r) / $base;
        $result = chr($r) . $result;
    } until ($d < $base);
    $result = chr($d) . $result if $d != 0;

    if (length($result) < $l) { 
        $result = chr(0)x($l-length($result)) . $result;
    }
    return $result;
}


sub os2ip {
    my $string = shift;
    my $base = PARI(256);
    my $result = 0;
    my $l = length($string); 
    for (0 .. $l-1) {
        my ($c) = unpack "x$_ a", $string;
        my $a = int(ord($c));
        my $val = int($l-$_-1); 
        $result += $a * ($base**$val);
    }
    return $result;
}


sub h2osp { 
    my $hex = shift;
    $hex =~ s/[ \n]//ig;
    my $num = Math::Pari::_hex_cvt($hex);
    return i2osp ($num);
}


sub generate_random_octet {
    my ( $l, $str ) = @_;
    my $r = makerandom ( Size => int($l*8), Strength => $str );
    return i2osp ($r, $l);
}


sub bitsize ($) {
    return pari2num(floor(Math::Pari::log(shift)/Math::Pari::log(2)) + 1);
}


sub octet_len { 
    return pari2num(floor(PARI((bitsize(shift)+7)/8)));
}


sub octet_xor { 
    my ($a, $b) = @_; my @xor;
    my @ba = split //, unpack "B*", $a; 
    my @bb = split //, unpack "B*", $b; 
    if (@ba != @bb) {
        if (@ba < @bb) { 
            for (1..@bb-@ba) { unshift @ba, '0' }
        } else { 
            for (1..@ba-@bb) { unshift @bb, '0' }
        }
    } 
    for (0..$#ba) { 
        $xor[$_] = ($ba[$_] xor $bb[$_]) || 0; 
    }
    return pack "B*", join '',@xor; 
}


sub mgf1 {
    my ($seed, $l) = @_;
    my $hlen = 20;  my ($T, $i) = ("",0);
    while ($i <= $l) { 
        my $C = i2osp (int($i), 4);
        $T .= sha1("$seed$C");
        $i += $hlen;
    }
    my ($output) = unpack "a$l", $T;
    return $output;
}


sub steak { 
    my ($text, $blocksize) = @_; 
    my $textsize = length($text);
    my $chunkcount = $textsize % $blocksize 
        ? int($textsize/$blocksize) + 1 : $textsize/$blocksize;
    my @segments = unpack "a$blocksize"x$chunkcount, $text;
    return @segments;
}

1;

=head1 NAME

Crypt::RSA::DataFormat - Data creation, conversion and reporting primitives.

=head1 DESCRIPTION

This module implements several data creation, conversion and reporting
primitives used throughout the Crypt::RSA implementation. Primitives are
available as exportable functions.

=head1 FUNCTIONS

=over 4

=item B<i2osp> Integer, Length

Integer To Octet String Primitive. Converts an integer into its
equivalent octet string representation of length B<Length>. If
necessary, the resulting string is prefixed with nulls. If
B<Length> is not provided, returns an octet string of shortest
possible length.

=item B<h2osp> Hex String

Hex To Octet String Primitive. Converts a I<hex string> into its
equivalent octet string representation and returns an octet
string of shortest possible length. The hex string is not
prefixed with C<0x>, etc.

=item B<os2ip> String

Octet String to Integer Primitive. Converts an octet string into its
equivalent integer representation.

=item B<generate_random_octet> Length, Strength

Generates a random octet string of length B<Length>. B<Strength> specifies
the degree of randomness. See Crypt::Random(3) for an explanation of the
B<Strength> parameter.

=item B<bitsize> Integer

Returns the length of the B<Integer> in bits.

=item B<octet_len> Integer

Returns the octet length of the integer. If the length is not a whole
number, the fractional part is dropped to make it whole.

=item B<octet_xor> String1, String2

Returns the result of B<String1> XOR B<String2>.

=item B<steak> String, Length 

Returns an array of segments of length B<Length> from B<String>. The final
segment can be smaller than B<Length>.

=back

=head1 AUTHOR

Vipul Ved Prakash, E<lt>mail@vipul.netE<gt>

=cut