| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Crypt::OFB; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
######################################## |
|
4
|
|
|
|
|
|
|
# general module startup things |
|
5
|
|
|
|
|
|
|
######################################## |
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
187826
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
40
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
82
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
977
|
use Crypt::ECB; |
|
|
1
|
|
|
|
|
2395
|
|
|
|
1
|
|
|
|
|
843
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
@ISA=qw(Crypt::ECB); |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
@EXPORT_OK = qw(encrypt decrypt encrypt_hex decrypt_hex); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$VERSION = 0.01; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
######################################## |
|
21
|
|
|
|
|
|
|
# basic methods |
|
22
|
|
|
|
|
|
|
######################################## |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# |
|
25
|
|
|
|
|
|
|
# sets iv if given |
|
26
|
|
|
|
|
|
|
# returns iv |
|
27
|
|
|
|
|
|
|
# |
|
28
|
|
|
|
|
|
|
sub iv (\$;$) |
|
29
|
|
|
|
|
|
|
{ |
|
30
|
0
|
|
|
0
|
0
|
|
my $crypt = shift; |
|
31
|
0
|
0
|
|
|
|
|
$crypt->{Iv} = shift if @_; |
|
32
|
0
|
|
|
|
|
|
return $crypt->{Iv}; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# |
|
36
|
|
|
|
|
|
|
# calls the crypting module |
|
37
|
|
|
|
|
|
|
# returns the en-/decrypted data |
|
38
|
|
|
|
|
|
|
# |
|
39
|
|
|
|
|
|
|
sub crypt (\$;$) |
|
40
|
|
|
|
|
|
|
{ |
|
41
|
0
|
|
|
0
|
1
|
|
my $crypt = shift; |
|
42
|
0
|
|
0
|
|
|
|
my $data = shift || $_ || ''; |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $errmsg = $crypt->{Errstring}; |
|
45
|
0
|
|
|
|
|
|
my $bs = $crypt->{Blocksize}; |
|
46
|
0
|
|
|
|
|
|
my $mode = $crypt->{Mode}; |
|
47
|
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
die $errmsg if $errmsg; |
|
49
|
|
|
|
|
|
|
|
|
50
|
0
|
0
|
|
|
|
|
unless ($mode) |
|
51
|
|
|
|
|
|
|
{ |
|
52
|
0
|
|
|
|
|
|
die "You tried to use crypt() without calling start()" |
|
53
|
|
|
|
|
|
|
. " before. Use '\$your_obj->start(\$mode)' first," |
|
54
|
|
|
|
|
|
|
. " \$mode being one of 'decrypt' or 'encrypt'.\n"; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$data = $crypt->{buffer}.$data; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# data is split into blocks of proper size which is reported |
|
60
|
|
|
|
|
|
|
# by the cipher module |
|
61
|
0
|
|
|
|
|
|
my @blocks = $data=~/(.{1,$bs})/gs; |
|
62
|
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$crypt->{buffer} = pop @blocks; |
|
64
|
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $cipher = $crypt->_getcipher; |
|
66
|
0
|
|
|
|
|
|
my $text = ''; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# OFB Implementation here. |
|
69
|
0
|
|
|
|
|
|
my $skey = $crypt->iv; |
|
70
|
0
|
|
|
|
|
|
foreach my $block (@blocks) { |
|
71
|
0
|
|
|
|
|
|
$skey = $cipher->encrypt($skey); |
|
72
|
0
|
|
|
|
|
|
$text .= $block ^ $skey; |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
# |
|
75
|
0
|
|
|
|
|
|
return $text; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
######################################## |
|
79
|
|
|
|
|
|
|
# convenience functions/methods |
|
80
|
|
|
|
|
|
|
######################################## |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# |
|
83
|
|
|
|
|
|
|
# magic convenience encrypt function/method |
|
84
|
|
|
|
|
|
|
# |
|
85
|
|
|
|
|
|
|
sub encrypt ($$;$$) |
|
86
|
|
|
|
|
|
|
{ |
|
87
|
0
|
0
|
|
0
|
1
|
|
if (ref($_[0]) =~ /^Crypt/) |
|
88
|
|
|
|
|
|
|
{ |
|
89
|
0
|
|
|
|
|
|
my $crypt = shift; |
|
90
|
|
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
$crypt->start('encrypt') || die $crypt->errstring; |
|
92
|
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $text = $crypt->crypt(shift) |
|
94
|
|
|
|
|
|
|
. $crypt->finish; |
|
95
|
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $text; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
else |
|
99
|
|
|
|
|
|
|
{ |
|
100
|
0
|
|
|
|
|
|
my ($key, $cipher, $data, $padstyle) = @_; |
|
101
|
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $crypt = Crypt::OFB->new($key); |
|
103
|
|
|
|
|
|
|
|
|
104
|
0
|
|
0
|
|
|
|
$crypt->padding($padstyle || 0); |
|
105
|
0
|
0
|
|
|
|
|
$crypt->cipher($cipher) || die $crypt->errstring; |
|
106
|
0
|
0
|
|
|
|
|
$crypt->start('encrypt') || die $crypt->errstring; |
|
107
|
|
|
|
|
|
|
|
|
108
|
0
|
|
0
|
|
|
|
my $text = $crypt->crypt($data || $_) |
|
109
|
|
|
|
|
|
|
. $crypt->finish; |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
return $text; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# |
|
116
|
|
|
|
|
|
|
# magic convenience decrypt function/method |
|
117
|
|
|
|
|
|
|
# |
|
118
|
|
|
|
|
|
|
sub decrypt ($$;$$) |
|
119
|
|
|
|
|
|
|
{ |
|
120
|
0
|
0
|
|
0
|
1
|
|
if (ref($_[0]) =~ /^Crypt/) |
|
121
|
|
|
|
|
|
|
{ |
|
122
|
0
|
|
|
|
|
|
my $crypt = shift; |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
$crypt->start('decrypt') || die $crypt->errstring; |
|
125
|
|
|
|
|
|
|
|
|
126
|
0
|
|
|
|
|
|
my $text = $crypt->crypt(shift) |
|
127
|
|
|
|
|
|
|
. $crypt->finish; |
|
128
|
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
return $text; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
else |
|
132
|
|
|
|
|
|
|
{ |
|
133
|
0
|
|
|
|
|
|
my ($key, $cipher, $data, $padstyle) = @_; |
|
134
|
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
my $crypt = Crypt::OFB->new($key); |
|
136
|
|
|
|
|
|
|
|
|
137
|
0
|
|
0
|
|
|
|
$crypt->padding($padstyle || 0); |
|
138
|
0
|
0
|
|
|
|
|
$crypt->cipher($cipher) || die $crypt->errstring; |
|
139
|
0
|
0
|
|
|
|
|
$crypt->start('decrypt') || die $crypt->errstring; |
|
140
|
|
|
|
|
|
|
|
|
141
|
0
|
|
0
|
|
|
|
my $text = $crypt->crypt($data || $_) |
|
142
|
|
|
|
|
|
|
. $crypt->finish; |
|
143
|
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return $text; |
|
145
|
|
|
|
|
|
|
} |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# |
|
149
|
|
|
|
|
|
|
# calls encrypt, returns hex packed data |
|
150
|
|
|
|
|
|
|
# |
|
151
|
|
|
|
|
|
|
sub encrypt_hex ($$;$$) |
|
152
|
|
|
|
|
|
|
{ |
|
153
|
0
|
0
|
|
0
|
1
|
|
if (ref($_[0]) =~ /^Crypt/) |
|
154
|
|
|
|
|
|
|
{ |
|
155
|
0
|
|
|
|
|
|
my $crypt = shift; |
|
156
|
0
|
|
|
|
|
|
join('',unpack('H*',$crypt->encrypt(shift))); |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
else |
|
159
|
|
|
|
|
|
|
{ |
|
160
|
0
|
|
|
|
|
|
join('',unpack('H*',encrypt($_[0], $_[1], $_[2], $_[3]))); |
|
161
|
|
|
|
|
|
|
} |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
# |
|
165
|
|
|
|
|
|
|
# calls decrypt, expected input is hex packed |
|
166
|
|
|
|
|
|
|
# |
|
167
|
|
|
|
|
|
|
sub decrypt_hex ($$;$$) |
|
168
|
|
|
|
|
|
|
{ |
|
169
|
0
|
0
|
|
0
|
1
|
|
if (ref($_[0]) =~ /^Crypt/) |
|
170
|
|
|
|
|
|
|
{ |
|
171
|
0
|
|
|
|
|
|
my $crypt = shift; |
|
172
|
0
|
|
|
|
|
|
$crypt->decrypt(pack('H*',shift)); |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
else |
|
175
|
|
|
|
|
|
|
{ |
|
176
|
0
|
|
|
|
|
|
decrypt($_[0], $_[1], pack('H*',$_[2]), $_[3]); |
|
177
|
|
|
|
|
|
|
} |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
######################################## |
|
182
|
|
|
|
|
|
|
# finally, to satisfy require |
|
183
|
|
|
|
|
|
|
######################################## |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
|
186
|
|
|
|
|
|
|
__END__ |