line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::TEA_PP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Pure Perl Implementation of the Tiny Encryption Algorithm |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
426
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
7
|
1
|
|
|
1
|
|
11
|
use utf8; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
411
|
use integer; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
3
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
23
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
50
|
|
11
|
1
|
|
|
1
|
|
4
|
use List::Util qw(all); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
76
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.0306'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
5
|
use Config; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
53
|
|
16
|
|
|
|
|
|
|
BEGIN { |
17
|
1
|
50
|
|
1
|
|
486
|
if ( not defined $Config{use64bitint} ) { |
18
|
0
|
|
|
|
|
0
|
require bigint; |
19
|
0
|
|
|
|
|
0
|
bigint->import; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $DELTA = 0x9e3779b9; |
25
|
|
|
|
|
|
|
my $SUMATION = 0xc6ef3720; |
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
|
|
2500
|
use constant keysize => $KEY_SIZE; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
55
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
1
|
|
4
|
use constant blocksize => $BLOCK_SIZE; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
678
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub new { |
40
|
2
|
|
|
2
|
1
|
654
|
my $class = shift; |
41
|
2
|
|
|
|
|
2
|
my $key = shift; |
42
|
2
|
|
33
|
|
|
10
|
my $rounds = shift // $ROUNDS; |
43
|
2
|
|
|
|
|
2
|
my $tea_key; |
44
|
2
|
50
|
|
|
|
4
|
croak( 'key is required' ) if not defined $key; |
45
|
2
|
50
|
|
|
|
4
|
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 NUMBER if key is a reference of ARRAY' ) if not all { /^-?\d+$/ } @{ $key }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
49
|
0
|
|
|
|
|
0
|
$tea_key = $key; |
50
|
|
|
|
|
|
|
} else { |
51
|
2
|
50
|
|
|
|
5
|
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
|
$tea_key = key_setup($key); |
53
|
|
|
|
|
|
|
} |
54
|
2
|
50
|
|
|
|
9
|
croak( 'rounds must be a positive NUMBER' ) if $rounds !~ /^\d+$/; |
55
|
2
|
|
|
|
|
3
|
my $self = { |
56
|
|
|
|
|
|
|
key => $tea_key, |
57
|
|
|
|
|
|
|
rounds => $rounds, |
58
|
|
|
|
|
|
|
}; |
59
|
2
|
|
33
|
|
|
9
|
bless $self, ref($class) || $class; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub encrypt { |
64
|
2
|
|
|
2
|
1
|
422
|
my $self = shift; |
65
|
2
|
|
|
|
|
3
|
my $plain_text = shift; |
66
|
2
|
50
|
|
|
|
7
|
croak( sprintf( 'block size must be %d', $BLOCK_SIZE) ) if length($plain_text) != $BLOCK_SIZE; |
67
|
2
|
|
|
|
|
6
|
my @block = unpack 'N*', $plain_text; |
68
|
2
|
|
|
|
|
4
|
my $cipher_text_ref = $self->encrypt_block( \@block ); |
69
|
2
|
|
|
|
|
1
|
return pack( 'N*', @{$cipher_text_ref} ); |
|
2
|
|
|
|
|
8
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub decrypt { |
74
|
2
|
|
|
2
|
1
|
518
|
my $self = shift; |
75
|
2
|
|
|
|
|
2
|
my $cipher_text = shift; |
76
|
2
|
50
|
|
|
|
5
|
croak( sprintf( 'block size must be %d', $BLOCK_SIZE) ) if length($cipher_text) != $BLOCK_SIZE; |
77
|
2
|
|
|
|
|
4
|
my @block = unpack 'N*', $cipher_text; |
78
|
2
|
|
|
|
|
5
|
my $plain_text_ref = $self->decrypt_block( \@block ); |
79
|
2
|
|
|
|
|
2
|
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
|
|
|
|
|
5
|
my $key_ref = $self->{key}; |
86
|
|
|
|
|
|
|
|
87
|
2
|
50
|
|
|
|
2
|
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
|
|
|
|
|
9
|
|
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
3
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
4
|
|
91
|
2
|
|
|
|
|
2
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
3
|
|
92
|
2
|
|
|
|
|
20
|
my $sumation = 0 & 0xffff_ffff; |
93
|
2
|
|
|
|
|
3
|
my $delta = $DELTA & 0xffff_ffff; |
94
|
2
|
|
|
|
|
5
|
for my $i ( 0 .. $self->{rounds}-1 ) { |
95
|
64
|
|
|
|
|
39
|
$sumation = ( $sumation + $delta ) & 0xffff_ffff; |
96
|
64
|
|
|
|
|
69
|
$block[0] = ( $block[0] + ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) + $key[0] ) & 0xffff_ffff ) ^ ( ( $block[1] + $sumation ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $block[1] >> 5 ) & 0xffff_ffff ) + $key[1] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
97
|
64
|
|
|
|
|
79
|
$block[1] = ( $block[1] + ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) + $key[2] ) & 0xffff_ffff ) ^ ( ( $block[0] + $sumation ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $block[0] >> 5 ) & 0xffff_ffff ) + $key[3] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
98
|
|
|
|
|
|
|
#printf("\t--> Encrypting block round %d of %d\n", $i + 1, $ROUNDS); |
99
|
|
|
|
|
|
|
#printf("\t--> block[0] = %d, block[1] = %d\n", $block[0], $block[1]); |
100
|
|
|
|
|
|
|
} |
101
|
2
|
|
|
|
|
5
|
return \@block; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub decrypt_block { |
105
|
2
|
|
|
2
|
0
|
1
|
my $self = shift; |
106
|
2
|
|
|
|
|
2
|
my $block_ref = shift; |
107
|
2
|
|
|
|
|
3
|
my $key_ref = $self->{key}; |
108
|
|
|
|
|
|
|
|
109
|
2
|
50
|
|
|
|
2
|
croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK; |
|
2
|
|
|
|
|
3
|
|
110
|
2
|
50
|
|
|
|
2
|
croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY; |
|
2
|
|
|
|
|
427
|
|
111
|
|
|
|
|
|
|
|
112
|
2
|
|
|
|
|
2
|
my @block = map { $_ & 0xffff_ffff } @{ $block_ref }; |
|
4
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
2
|
|
113
|
2
|
|
|
|
|
1
|
my @key = map { $_ & 0xffff_ffff } @{ $key_ref }; |
|
8
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
3
|
|
114
|
2
|
|
|
|
|
2
|
my $sumation = $SUMATION & 0xffff_ffff; |
115
|
2
|
|
|
|
|
2
|
my $delta = $DELTA & 0xffff_ffff; |
116
|
2
|
|
|
|
|
4
|
for my $i ( 0 .. $self->{rounds}-1 ) { |
117
|
64
|
|
|
|
|
71
|
$block[1] = ( $block[1] - ( ( ( ( ( ( ( ( $block[0] << 4 ) & 0xffff_ffff ) + $key[2] ) & 0xffff_ffff ) ^ ( ( $block[0] + $sumation ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $block[0] >> 5 ) & 0xffff_ffff ) + $key[3] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
118
|
64
|
|
|
|
|
67
|
$block[0] = ( $block[0] - ( ( ( ( ( ( ( ( $block[1] << 4 ) & 0xffff_ffff ) + $key[0] ) & 0xffff_ffff ) ^ ( ( $block[1] + $sumation ) & 0xffff_ffff ) ) & 0xffff_ffff ) ^ ( ( ( ( $block[1] >> 5 ) & 0xffff_ffff ) + $key[1] ) & 0xffff_ffff ) ) & 0xffff_ffff ) ) & 0xffff_ffff; |
119
|
64
|
|
|
|
|
46
|
$sumation = ( $sumation - $delta ) & 0xffff_ffff; |
120
|
|
|
|
|
|
|
#printf("\t--> Decrypting block round %d of %d\n", $i + 1, $ROUNDS); |
121
|
|
|
|
|
|
|
#printf("\t--> block[0] = %d, block[1] = %d\n", $block[0], $block[1]); |
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 @tea_key = unpack 'N*', $key_str; |
130
|
2
|
|
|
|
|
4
|
return \@tea_key; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
1; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__END__ |