line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::XXTEA_PP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Pure Perl Implementation of Corrected Block Tiny Encryption Algorithm |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
487
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
23
|
|
7
|
1
|
|
|
1
|
|
11
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
8
|
1
|
|
|
1
|
|
407
|
use integer; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
4
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
21
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
53
|
|
11
|
1
|
|
|
1
|
|
4
|
use List::Util qw(all); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
66
|
|
12
|
1
|
|
|
1
|
|
350
|
use Scalar::Util::Numeric qw(isint); |
|
1
|
|
|
|
|
478
|
|
|
1
|
|
|
|
|
57
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.01'; # VERSION |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
4
|
use Config; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
17
|
|
|
|
|
|
|
BEGIN { |
18
|
1
|
50
|
|
1
|
|
417
|
if ( not defined $Config{use64bitint} ) { |
19
|
0
|
|
|
|
|
0
|
require bigint; |
20
|
0
|
|
|
|
|
0
|
bigint->import; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $DELTA = 0x9e3779b9; |
26
|
|
|
|
|
|
|
my $FACTOR = 4; |
27
|
|
|
|
|
|
|
my $KEY_SIZE = 16; |
28
|
|
|
|
|
|
|
my $ELEMENTS_IN_KEY = $KEY_SIZE / $FACTOR; |
29
|
|
|
|
|
|
|
my $MIN_BLOCK_SIZE = 8; |
30
|
|
|
|
|
|
|
my $MIN_ELEMENTS_IN_BLOCK = $MIN_BLOCK_SIZE / $FACTOR; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
1
|
|
1863
|
use constant keysize => $KEY_SIZE; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
45
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
3
|
use constant blocksize => $MIN_BLOCK_SIZE; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
738
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
40
|
2
|
|
|
2
|
1
|
577
|
my $class = shift; |
41
|
2
|
|
|
|
|
2
|
my $key = shift; |
42
|
2
|
|
|
|
|
2
|
my $xxtea_key; |
43
|
2
|
50
|
|
|
|
4
|
croak( 'key is required' ) if not defined $key; |
44
|
2
|
50
|
|
|
|
4
|
if ( my $ref_of_key = ref( $key ) ) { |
45
|
0
|
0
|
|
|
|
0
|
croak( sprintf( 'key must be a %d-byte-long STRING or a reference of ARRAY', $KEY_SIZE ) ) if not $ref_of_key eq 'ARRAY'; |
46
|
0
|
0
|
|
|
|
0
|
croak( sprintf( 'key must has %d elements if key is a reference of ARRAY', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key } ) != $ELEMENTS_IN_KEY; |
|
0
|
|
|
|
|
0
|
|
47
|
0
|
0
|
|
0
|
|
0
|
croak( 'each element of key must be a 32bit Integer if key is a reference of ARRAY' ) if not all { isint( $_ ) != 0 } @{ $key }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
48
|
0
|
|
|
|
|
0
|
$xxtea_key = $key; |
49
|
|
|
|
|
|
|
} else { |
50
|
2
|
50
|
|
|
|
4
|
croak( sprintf( 'key must be a %d-byte-long STRING or a reference of ARRAY', $KEY_SIZE ) ) if length $key != $KEY_SIZE; |
51
|
2
|
|
|
|
|
4
|
$xxtea_key = key_setup($key); |
52
|
|
|
|
|
|
|
} |
53
|
2
|
|
|
|
|
4
|
my $self = { |
54
|
|
|
|
|
|
|
key => $xxtea_key, |
55
|
|
|
|
|
|
|
}; |
56
|
2
|
|
33
|
|
|
8
|
bless $self, ref($class) || $class; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub encrypt { |
61
|
2
|
|
|
2
|
1
|
412
|
my $self = shift; |
62
|
2
|
|
|
|
|
2
|
my $plain_text = shift; |
63
|
|
|
|
|
|
|
|
64
|
2
|
50
|
|
|
|
5
|
croak( sprintf( 'plain_text size must be at least %d bytes', $MIN_BLOCK_SIZE) ) if length($plain_text) < $MIN_BLOCK_SIZE; |
65
|
2
|
50
|
|
|
|
9
|
croak( sprintf( 'plain_text size must be a multiple of %d bytes', $FACTOR) ) if length($plain_text) % $FACTOR != 0; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
5
|
my @block = unpack 'N*', $plain_text; |
68
|
2
|
|
|
|
|
3
|
my $cipher_text_ref = $self->encrypt_block( \@block ); |
69
|
2
|
|
|
|
|
2
|
return pack( 'N*', @{$cipher_text_ref} ); |
|
2
|
|
|
|
|
9
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub decrypt { |
74
|
2
|
|
|
2
|
1
|
564
|
my $self = shift; |
75
|
2
|
|
|
|
|
2
|
my $cipher_text = shift; |
76
|
|
|
|
|
|
|
|
77
|
2
|
50
|
|
|
|
5
|
croak( sprintf( 'cipher_text size must be at least %d bytes', $MIN_BLOCK_SIZE) ) if length($cipher_text) < $MIN_BLOCK_SIZE; |
78
|
2
|
50
|
|
|
|
4
|
croak( sprintf( 'cipher_text size must be a multiple of %d bytes', $FACTOR) ) if length($cipher_text) % $FACTOR != 0; |
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
|
|
4
|
my @block = unpack 'N*', $cipher_text; |
81
|
2
|
|
|
|
|
3
|
my $plain_text_ref = $self->decrypt_block( \@block ); |
82
|
2
|
|
|
|
|
2
|
return pack( 'N*', @{$plain_text_ref} ); |
|
2
|
|
|
|
|
5
|
|
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub encrypt_block { |
86
|
2
|
|
|
2
|
0
|
2
|
my $self = shift; |
87
|
2
|
|
|
|
|
2
|
my $block_ref = shift; |
88
|
2
|
|
|
|
|
3
|
my $key_ref = $self->{key}; |
89
|
|
|
|
|
|
|
|
90
|
2
|
50
|
|
|
|
2
|
croak( sprintf( 'block must has at least %d elements', $MIN_ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) < $MIN_ELEMENTS_IN_BLOCK; |
|
2
|
|
|
|
|
6
|
|
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
2
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
2
|
|
93
|
2
|
|
|
|
|
2
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
94
|
|
|
|
|
|
|
|
95
|
2
|
|
|
|
|
2
|
my $delta = $DELTA & 0xffff_ffff; |
96
|
2
|
|
|
|
|
15
|
my $rounds = 6 + 52 / ( scalar @block ); |
97
|
2
|
|
|
|
|
2
|
my $sum = 0 & 0xffff_ffff; |
98
|
2
|
|
|
|
|
1
|
my $z = $block[-1]; |
99
|
2
|
|
|
|
|
2
|
my ( $e, $p, $y ); |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
6
|
for ( 0 .. $rounds-1 ) { |
102
|
64
|
|
|
|
|
38
|
$sum = ( $sum + $delta ) & 0xffff_ffff; |
103
|
64
|
|
|
|
|
40
|
$e = ( $sum >> 2 ) & 3; |
104
|
64
|
|
|
|
|
53
|
for ( 0 .. $#block-1 ) { |
105
|
64
|
|
|
|
|
39
|
$p = $_; |
106
|
64
|
|
|
|
|
338
|
$y = $block[ $p + 1 ]; |
107
|
64
|
|
|
|
|
60
|
$z = $block[ $p ] = ( $block[ $p ] + _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff; |
108
|
|
|
|
|
|
|
} |
109
|
64
|
|
|
|
|
42
|
$p += 1; |
110
|
64
|
|
|
|
|
40
|
$y = $block[0]; |
111
|
64
|
|
|
|
|
55
|
$z = $block[-1] = ( $block[-1] + _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff; |
112
|
|
|
|
|
|
|
} |
113
|
2
|
|
|
|
|
3
|
return \@block; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub decrypt_block { |
117
|
2
|
|
|
2
|
0
|
2
|
my $self = shift; |
118
|
2
|
|
|
|
|
2
|
my $block_ref = shift; |
119
|
2
|
|
|
|
|
2
|
my $key_ref = $self->{key}; |
120
|
|
|
|
|
|
|
|
121
|
2
|
50
|
|
|
|
0
|
croak( sprintf( 'block must has at least %d elements', $MIN_ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) < $MIN_ELEMENTS_IN_BLOCK; |
|
2
|
|
|
|
|
4
|
|
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
1
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
3
|
|
124
|
2
|
|
|
|
|
2
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
125
|
|
|
|
|
|
|
|
126
|
2
|
|
|
|
|
2
|
my $delta = $DELTA & 0xffff_ffff; |
127
|
2
|
|
|
|
|
2
|
my $rounds = 6 + 52 / ( scalar @block ); |
128
|
2
|
|
|
|
|
2
|
my $sum = ( $rounds * $delta ) & 0xffff_ffff; |
129
|
2
|
|
|
|
|
1
|
my $y = $block[0]; |
130
|
2
|
|
|
|
|
2
|
my ( $e, $p, $z ); |
131
|
2
|
|
|
|
|
3
|
for ( 0 .. $rounds-1 ) { |
132
|
64
|
|
|
|
|
39
|
$e = ( $sum >> 2 ) & 3; |
133
|
64
|
|
|
|
|
50
|
for ( reverse 1 .. $#block ) { |
134
|
64
|
|
|
|
|
36
|
$p = $_; |
135
|
64
|
|
|
|
|
40
|
$z = $block[ $p - 1 ]; |
136
|
64
|
|
|
|
|
59
|
$y = $block[ $p ] = ( $block[ $p ] - _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff; |
137
|
|
|
|
|
|
|
} |
138
|
64
|
|
|
|
|
44
|
$p -= 1; |
139
|
64
|
|
|
|
|
41
|
$z = $block[-1]; |
140
|
64
|
|
|
|
|
54
|
$y = $block[0] = ( $block[0] - _MX( $y, $z, $sum, $p, $e, \@key ) ) & 0xffff_ffff; |
141
|
64
|
|
|
|
|
56
|
$sum = ( $sum - $delta ) & 0xffff_ffff; |
142
|
|
|
|
|
|
|
} |
143
|
2
|
|
|
|
|
3
|
return \@block; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub _MX { |
147
|
256
|
|
|
256
|
|
185
|
my ( $y, $z, $sum, $p, $e, $key ) = @_; |
148
|
256
|
|
|
|
|
487
|
return ( ( ( ( ( ( ( $z >> 5 ) & 0xffff_ffff ) ^ ( ( $y << 2 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + ( ( ( ( $y >> 3 ) & 0xffff_ffff ) ^ ( ( $z << 4 ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $sum ^ $y ) & 0xffff_ffff ) + ( ( $key->[ ( $p & 3 ) ^ $e ] ^ $z ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub key_setup { |
152
|
2
|
|
|
2
|
0
|
1
|
my $key_str = shift; |
153
|
2
|
50
|
|
|
|
4
|
croak( sprintf( 'key must be %s bytes long', $KEY_SIZE ) ) if length( $key_str ) != $KEY_SIZE; |
154
|
2
|
|
|
|
|
5
|
my @xxtea_key = unpack 'N*', $key_str; |
155
|
2
|
|
|
|
|
3
|
return \@xxtea_key; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
__END__ |