line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::XTEA_PP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Pure Perl Implementation of the eXtended Tiny Encryption Algorithm |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
607
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
42
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
7
|
1
|
|
|
1
|
|
14
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
457
|
use integer; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
21
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
68
|
|
11
|
1
|
|
|
1
|
|
4
|
use List::Util qw(all); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
75
|
|
12
|
1
|
|
|
1
|
|
413
|
use Scalar::Util::Numeric qw(isint); |
|
1
|
|
|
|
|
581
|
|
|
1
|
|
|
|
|
56
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.0106'; # VERSION |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
5
|
use Config; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
57
|
|
17
|
|
|
|
|
|
|
BEGIN { |
18
|
1
|
50
|
|
1
|
|
452
|
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 $ROUNDS = 32; |
27
|
|
|
|
|
|
|
my $KEY_SIZE = 16; |
28
|
|
|
|
|
|
|
my $ELEMENTS_IN_KEY = $KEY_SIZE / 4; |
29
|
|
|
|
|
|
|
my $BLOCK_SIZE = 8; |
30
|
|
|
|
|
|
|
my $ELEMENTS_IN_BLOCK = $BLOCK_SIZE / 4; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
1
|
|
1965
|
use constant keysize => $KEY_SIZE; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
3
|
use constant blocksize => $BLOCK_SIZE; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
653
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
40
|
2
|
|
|
2
|
1
|
725
|
my $class = shift; |
41
|
2
|
|
|
|
|
3
|
my $key = shift; |
42
|
2
|
|
33
|
|
|
9
|
my $rounds = shift // $ROUNDS; |
43
|
2
|
|
|
|
|
2
|
my $xtea_key; |
44
|
2
|
50
|
|
|
|
3
|
croak( 'key is required' ) if not defined $key; |
45
|
2
|
50
|
|
|
|
5
|
if ( my $ref_of_key = ref( $key ) ) { |
46
|
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'; |
47
|
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
|
|
48
|
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
|
|
49
|
0
|
|
|
|
|
0
|
$xtea_key = $key; |
50
|
|
|
|
|
|
|
} else { |
51
|
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; |
52
|
2
|
|
|
|
|
3
|
$xtea_key = key_setup($key); |
53
|
|
|
|
|
|
|
} |
54
|
2
|
50
|
|
|
|
8
|
croak( 'rounds must be a positive NUMBER' ) if isint( $rounds ) != 1; |
55
|
2
|
|
|
|
|
5
|
my $self = { |
56
|
|
|
|
|
|
|
key => $xtea_key, |
57
|
|
|
|
|
|
|
rounds => $rounds, |
58
|
|
|
|
|
|
|
}; |
59
|
2
|
|
33
|
|
|
10
|
bless $self, ref($class) || $class; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub encrypt { |
64
|
2
|
|
|
2
|
1
|
420
|
my $self = shift; |
65
|
2
|
|
|
|
|
3
|
my $plain_text = shift; |
66
|
2
|
50
|
|
|
|
6
|
croak( sprintf( 'plain_text size must be %d bytes', $BLOCK_SIZE) ) if length($plain_text) != $BLOCK_SIZE; |
67
|
2
|
|
|
|
|
5
|
my @block = unpack 'N*', $plain_text; |
68
|
2
|
|
|
|
|
4
|
my $cipher_text_ref = $self->encrypt_block( \@block ); |
69
|
2
|
|
|
|
|
2
|
return pack( 'N*', @{$cipher_text_ref} ); |
|
2
|
|
|
|
|
10
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub decrypt { |
74
|
2
|
|
|
2
|
1
|
563
|
my $self = shift; |
75
|
2
|
|
|
|
|
2
|
my $cipher_text = shift; |
76
|
2
|
50
|
|
|
|
5
|
croak( sprintf( 'cipher_text size must be %d bytes', $BLOCK_SIZE) ) if length($cipher_text) != $BLOCK_SIZE; |
77
|
2
|
|
|
|
|
4
|
my @block = unpack 'N*', $cipher_text; |
78
|
2
|
|
|
|
|
3
|
my $plain_text_ref = $self->decrypt_block( \@block ); |
79
|
2
|
|
|
|
|
3
|
return pack( 'N*', @{$plain_text_ref} ); |
|
2
|
|
|
|
|
5
|
|
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub encrypt_block { |
83
|
2
|
|
|
2
|
0
|
2
|
my $self = shift; |
84
|
2
|
|
|
|
|
2
|
my $block_ref = shift; |
85
|
2
|
|
|
|
|
3
|
my $key_ref = $self->{key}; |
86
|
|
|
|
|
|
|
|
87
|
2
|
50
|
|
|
|
3
|
croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK; |
|
2
|
|
|
|
|
4
|
|
88
|
2
|
50
|
|
|
|
1
|
croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY; |
|
2
|
|
|
|
|
4
|
|
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
2
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
3
|
|
91
|
2
|
|
|
|
|
1
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
11
|
|
|
2
|
|
|
|
|
12
|
|
92
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
2
|
my $sumation = 0 & 0xffff_ffff; |
94
|
2
|
|
|
|
|
3
|
my $delta = $DELTA & 0xffff_ffff; |
95
|
|
|
|
|
|
|
|
96
|
2
|
|
|
|
|
4
|
for my $i ( 0 .. $self->{rounds}-1 ) { |
97
|
64
|
|
|
|
|
67
|
$block[0] = ( $block[0] + ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) ^ ( ( $block[1] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[1] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ $sumation & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
98
|
64
|
|
|
|
|
435
|
$sumation = ( $sumation + $delta ) & 0xffff_ffff; |
99
|
64
|
|
|
|
|
76
|
$block[1] = ( $block[1] + ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) ^ ( ( $block[0] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[0] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ ( ( $sumation >> 11 ) & 0xffff_ffff ) & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
100
|
|
|
|
|
|
|
} |
101
|
2
|
|
|
|
|
5
|
return \@block; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub decrypt_block { |
105
|
2
|
|
|
2
|
0
|
2
|
my $self = shift; |
106
|
2
|
|
|
|
|
1
|
my $block_ref = shift; |
107
|
2
|
|
|
|
|
2
|
my $key_ref = $self->{key}; |
108
|
|
|
|
|
|
|
|
109
|
2
|
|
|
|
|
2
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
3
|
|
110
|
2
|
|
|
|
|
1
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
111
|
|
|
|
|
|
|
|
112
|
2
|
50
|
|
|
|
2
|
croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK; |
|
2
|
|
|
|
|
3
|
|
113
|
2
|
50
|
|
|
|
1
|
croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY; |
|
2
|
|
|
|
|
3
|
|
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
|
|
2
|
my $delta = $DELTA & 0xffff_ffff; |
116
|
2
|
|
|
|
|
1
|
my $sumation = ( $delta * $self->{rounds} ) & 0xffff_ffff; |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
5
|
for my $i ( 0 .. $self->{rounds}-1 ) { |
119
|
64
|
|
|
|
|
91
|
$block[1] = ( $block[1] - ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) ^ ( ( $block[0] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[0] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ ( ( $sumation >> 11 ) & 0xffff_ffff ) & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
120
|
64
|
|
|
|
|
38
|
$sumation = ( $sumation - $delta ) & 0xffff_ffff; |
121
|
64
|
|
|
|
|
70
|
$block[0] = ( $block[0] - ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) ^ ( ( $block[1] >> 5 ) & 0xffff_ffff ) ) & 0xffff_ffff ) + $block[1] ) & 0xffff_ffff ) ^ ( ( $sumation + $key[ $sumation & 3 ] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
122
|
|
|
|
|
|
|
} |
123
|
2
|
|
|
|
|
4
|
return \@block; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub key_setup { |
127
|
2
|
|
|
2
|
0
|
2
|
my $key_str = shift; |
128
|
2
|
50
|
|
|
|
4
|
croak( sprintf( 'key must be %s bytes long', $KEY_SIZE ) ) if length( $key_str ) != $KEY_SIZE; |
129
|
2
|
|
|
|
|
6
|
my @xtea_key = unpack 'N*', $key_str; |
130
|
2
|
|
|
|
|
4
|
return \@xtea_key; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |