| 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 | 13 | ▶ | use vars qw(@ISA); | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use Math::Pari qw(PARI pari2pv floor pari2num); | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use Crypt::Random qw(makerandom); | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use Digest::SHA1 qw(sha1); | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||
| 17 | 13 | ▶ | use Carp; | |||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||
| 18 | 13 | require Exporter; | ||||||||||||||||||||||||||||
| 19 | 13 | @ISA = qw(Exporter); | ||||||||||||||||||||||||||||
| 20 | ||||||||||||||||||||||||||||||
| 21 | 13 | @EXPORT_OK = qw(i2osp os2ip h2osp octet_xor octet_len bitsize | ||||||||||||||||||||||||||||
| 22 | generate_random_octet mgf1 steak); | |||||||||||||||||||||||||||||
| 23 | ||||||||||||||||||||||||||||||
| 24 | ||||||||||||||||||||||||||||||
| 25 | 5766 | ▶ | sub i2osp { | |||||||||||||||||||||||||||
|
Subroutine
i2osp: called CC=7 SCAR=19.5
Pod
documented
| ||||||||||||||||||||||||||||||
| 26 | 5766 | my $num = PARI(shift); | ||||||||||||||||||||||||||||
| 27 | 5766 | my $d = $num; | ||||||||||||||||||||||||||||
| 28 | 5766 | ▶ | my $l = shift || 0; | |||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| ||||||||||||||||||||||||||||||
| 29 | 5766 | my $base = PARI(256); my $result = ''; | ||||||||||||||||||||||||||||
| 30 | 5766 | ▶ | if ($l) { return if $num > $base ** $l } | |||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||
| 31 | ||||||||||||||||||||||||||||||
| 32 | 5566 | ▶ | do { | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 33 | 84929 | my $r = $d % $base; | ||||||||||||||||||||||||||||
| 34 | 84929 | $d = ($d-$r) / $base; | ||||||||||||||||||||||||||||
| 35 | 84929 | $result = chr($r) . $result; | ||||||||||||||||||||||||||||
| 36 | } until ($d < $base); | |||||||||||||||||||||||||||||
| 37 | 5566 | ▶ | $result = chr($d) . $result if $d != 0; | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 38 | ||||||||||||||||||||||||||||||
| 39 | 5566 | ▶ | if (length($result) < $l) { | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 40 | 3957 | $result = chr(0)x($l-length($result)) . $result; | ||||||||||||||||||||||||||||
| 41 | } | |||||||||||||||||||||||||||||
| 42 | 5566 | return $result; | ||||||||||||||||||||||||||||
| 43 | } | |||||||||||||||||||||||||||||
| 44 | ||||||||||||||||||||||||||||||
| 45 | ||||||||||||||||||||||||||||||
| 46 | 1504 | ▶ | sub os2ip { | |||||||||||||||||||||||||||
|
Subroutine
os2ip: called CC=2 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||
| 47 | 1504 | my $string = shift; | ||||||||||||||||||||||||||||
| 48 | 1504 | my $base = PARI(256); | ||||||||||||||||||||||||||||
| 49 | 1504 | my $result = 0; | ||||||||||||||||||||||||||||
| 50 | 1504 | my $l = length($string); | ||||||||||||||||||||||||||||
| 51 | 1504 | for (0 .. $l-1) { | ||||||||||||||||||||||||||||
| 52 | 90469 | my ($c) = unpack "x$_ a", $string; | ||||||||||||||||||||||||||||
| 53 | 90469 | my $a = int(ord($c)); | ||||||||||||||||||||||||||||
| 54 | 90469 | my $val = int($l-$_-1); | ||||||||||||||||||||||||||||
| 55 | 90469 | $result += $a * ($base**$val); | ||||||||||||||||||||||||||||
| 56 | } | |||||||||||||||||||||||||||||
| 57 | 1504 | return $result; | ||||||||||||||||||||||||||||
| 58 | } | |||||||||||||||||||||||||||||
| 59 | ||||||||||||||||||||||||||||||
| 60 | ||||||||||||||||||||||||||||||
| 61 | 307 | ▶ | sub h2osp { | |||||||||||||||||||||||||||
|
Subroutine
h2osp: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||
| 62 | 307 | my $hex = shift; | ||||||||||||||||||||||||||||
| 63 | 307 | $hex =~ s/[ \n]//ig; | ||||||||||||||||||||||||||||
| 64 | 307 | my $num = Math::Pari::_hex_cvt($hex); | ||||||||||||||||||||||||||||
| 65 | 307 | return i2osp ($num); | ||||||||||||||||||||||||||||
| 66 | } | |||||||||||||||||||||||||||||
| 67 | ||||||||||||||||||||||||||||||
| 68 | ||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub generate_random_octet { | |||||||||||||||||||||||||||
|
Subroutine
generate_random_octet: not called CC=1 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||
| 70 | 0 | my ( $l, $str ) = @_; | ||||||||||||||||||||||||||||
| 71 | 0 | my $r = makerandom ( Size => int($l*8), Strength => $str ); | ||||||||||||||||||||||||||||
| 72 | 0 | return i2osp ($r, $l); | ||||||||||||||||||||||||||||
| 73 | } | |||||||||||||||||||||||||||||
| 74 | ||||||||||||||||||||||||||||||
| 75 | ||||||||||||||||||||||||||||||
| 76 | 2243 | ▶ | sub bitsize ($) { | |||||||||||||||||||||||||||
|
Subroutine
bitsize: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||
| 77 | 2243 | return pari2num(floor(Math::Pari::log(shift)/Math::Pari::log(2)) + 1); | ||||||||||||||||||||||||||||
| 78 | } | |||||||||||||||||||||||||||||
| 79 | ||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||
| 81 | 1516 | ▶ | sub octet_len { | |||||||||||||||||||||||||||
|
Subroutine
octet_len: called CC=1 SCAR=0.0
Pod
documented
| ||||||||||||||||||||||||||||||
| 82 | 1516 | return pari2num(floor(PARI((bitsize(shift)+7)/8))); | ||||||||||||||||||||||||||||
| 83 | } | |||||||||||||||||||||||||||||
| 84 | ||||||||||||||||||||||||||||||
| 85 | ||||||||||||||||||||||||||||||
| 86 | 1666 | ▶ | sub octet_xor { | |||||||||||||||||||||||||||
|
Subroutine
octet_xor: called CC=8 SCAR=21.0
Pod
documented
| ||||||||||||||||||||||||||||||
| 87 | 1666 | my ($a, $b) = @_; my @xor; | ||||||||||||||||||||||||||||
| 88 | 1666 | my @ba = split //, unpack "B*", $a; | ||||||||||||||||||||||||||||
| 89 | 1666 | my @bb = split //, unpack "B*", $b; | ||||||||||||||||||||||||||||
| 90 | 1666 | ▶ | if (@ba != @bb) { | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 91 | 2 | ▶ | if (@ba < @bb) { | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 92 | 0 | for (1..@bb-@ba) { unshift @ba, '0' } | ||||||||||||||||||||||||||||
| 93 | } else { | |||||||||||||||||||||||||||||
| 94 | 2 | for (1..@ba-@bb) { unshift @bb, '0' } | ||||||||||||||||||||||||||||
| 95 | } | |||||||||||||||||||||||||||||
| 96 | } | |||||||||||||||||||||||||||||
| 97 | 1666 | for (0..$#ba) { | ||||||||||||||||||||||||||||
| 98 | 408744 | ▶ | $xor[$_] = ($ba[$_] xor $bb[$_]) || 0; | |||||||||||||||||||||||||||
|
Condition100%6/6
$ba[$_] ^^ $bb[$_] || 0
$ba[$_] xor $bb[$_]
MC/DC100%2/2
$ba[$_] ^^ $bb[$_] || 0
$ba[$_]
$bb[$_]
Truth table100%4/4
A xor B
A= $ba[$_]B= $bb[$_]
| ||||||||||||||||||||||||||||||
| 99 | } | |||||||||||||||||||||||||||||
| 100 | 1666 | return pack "B*", join '',@xor; | ||||||||||||||||||||||||||||
| 101 | } | |||||||||||||||||||||||||||||
| 102 | ||||||||||||||||||||||||||||||
| 103 | ||||||||||||||||||||||||||||||
| 104 | 1663 | ▶ | sub mgf1 { | |||||||||||||||||||||||||||
|
Subroutine
mgf1: called CC=2 SCAR=6.9
Pod
undocumented
| ||||||||||||||||||||||||||||||
| 105 | 1663 | my ($seed, $l) = @_; | ||||||||||||||||||||||||||||
| 106 | 1663 | my $hlen = 20; my ($T, $i) = ("",0); | ||||||||||||||||||||||||||||
| 107 | 1663 | ▶ | while ($i <= $l) { | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 108 | 3953 | my $C = i2osp (int($i), 4); | ||||||||||||||||||||||||||||
| 109 | 3953 | $T .= sha1("$seed$C"); | ||||||||||||||||||||||||||||
| 110 | 3953 | $i += $hlen; | ||||||||||||||||||||||||||||
| 111 | } | |||||||||||||||||||||||||||||
| 112 | 1663 | my ($output) = unpack "a$l", $T; | ||||||||||||||||||||||||||||
| 113 | 1663 | return $output; | ||||||||||||||||||||||||||||
| 114 | } | |||||||||||||||||||||||||||||
| 115 | ||||||||||||||||||||||||||||||
| 116 | ||||||||||||||||||||||||||||||
| 117 | 6 | ▶ | sub steak { | |||||||||||||||||||||||||||
|
Subroutine
steak: called CC=2 SCAR=6.9
Pod
documented
| ||||||||||||||||||||||||||||||
| 118 | 6 | my ($text, $blocksize) = @_; | ||||||||||||||||||||||||||||
| 119 | 6 | my $textsize = length($text); | ||||||||||||||||||||||||||||
| 120 | 6 | ▶ | my $chunkcount = $textsize % $blocksize | |||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||
| 121 | ? int($textsize/$blocksize) + 1 : $textsize/$blocksize; | |||||||||||||||||||||||||||||
| 122 | 6 | my @segments = unpack "a$blocksize"x$chunkcount, $text; | ||||||||||||||||||||||||||||
| 123 | 6 | return @segments; | ||||||||||||||||||||||||||||
| 124 | } | |||||||||||||||||||||||||||||
| 125 | ||||||||||||||||||||||||||||||
| 126 | 13 | 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<i2osp> Integer, Length | |||||||||||||||||||||||||||||
| 143 | ||||||||||||||||||||||||||||||
| 144 | Integer To Octet String Primitive. Converts an integer into its | |||||||||||||||||||||||||||||
| 145 | equivalent octet string representation of length B<Length>. If | |||||||||||||||||||||||||||||
| 146 | necessary, the resulting string is prefixed with nulls. If | |||||||||||||||||||||||||||||
| 147 | B<Length> is not provided, returns an octet string of shortest | |||||||||||||||||||||||||||||
| 148 | possible length. | |||||||||||||||||||||||||||||
| 149 | ||||||||||||||||||||||||||||||
| 150 | =item B<h2osp> Hex String | |||||||||||||||||||||||||||||
| 151 | ||||||||||||||||||||||||||||||
| 152 | Hex To Octet String Primitive. Converts a I<hex string> 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<os2ip> String | |||||||||||||||||||||||||||||
| 158 | ||||||||||||||||||||||||||||||
| 159 | Octet String to Integer Primitive. Converts an octet string into its | |||||||||||||||||||||||||||||
| 160 | equivalent integer representation. | |||||||||||||||||||||||||||||
| 161 | ||||||||||||||||||||||||||||||
| 162 | =item B<generate_random_octet> Length, Strength | |||||||||||||||||||||||||||||
| 163 | ||||||||||||||||||||||||||||||
| 164 | Generates a random octet string of length B<Length>. B<Strength> specifies | |||||||||||||||||||||||||||||
| 165 | the degree of randomness. See Crypt::Random(3) for an explanation of the | |||||||||||||||||||||||||||||
| 166 | B<Strength> parameter. | |||||||||||||||||||||||||||||
| 167 | ||||||||||||||||||||||||||||||
| 168 | =item B<bitsize> Integer | |||||||||||||||||||||||||||||
| 169 | ||||||||||||||||||||||||||||||
| 170 | Returns the length of the B<Integer> in bits. | |||||||||||||||||||||||||||||
| 171 | ||||||||||||||||||||||||||||||
| 172 | =item B<octet_len> 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<octet_xor> String1, String2 | |||||||||||||||||||||||||||||
| 178 | ||||||||||||||||||||||||||||||
| 179 | Returns the result of B<String1> XOR B<String2>. | |||||||||||||||||||||||||||||
| 180 | ||||||||||||||||||||||||||||||
| 181 | =item B<steak> String, Length | |||||||||||||||||||||||||||||
| 182 | ||||||||||||||||||||||||||||||
| 183 | Returns an array of segments of length B<Length> from B<String>. The final | |||||||||||||||||||||||||||||
| 184 | segment can be smaller than B<Length>. | |||||||||||||||||||||||||||||
| 185 | ||||||||||||||||||||||||||||||
| 186 | =back | |||||||||||||||||||||||||||||
| 187 | ||||||||||||||||||||||||||||||
| 188 | =head1 AUTHOR | |||||||||||||||||||||||||||||
| 189 | ||||||||||||||||||||||||||||||
| 190 | Vipul Ved Prakash, E<lt>mail@vipul.netE<gt> | |||||||||||||||||||||||||||||
| 191 | ||||||||||||||||||||||||||||||
| 192 | =cut | |||||||||||||||||||||||||||||
| 193 | ||||||||||||||||||||||||||||||
| 194 | ||||||||||||||||||||||||||||||