line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Camellia_PP; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
72457
|
use strict; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
212
|
|
4
|
4
|
|
|
4
|
|
76
|
use warnings; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
121
|
|
5
|
4
|
|
|
4
|
|
21
|
use Carp; |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
13886
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my $SIGMA1 = [ 0xA0, 0x9E, 0x66, 0x7F, 0x3B, 0xCC, 0x90, 0x8B ]; |
9
|
|
|
|
|
|
|
my $SIGMA2 = [ 0xB6, 0x7A, 0xE8, 0x58, 0x4C, 0xAA, 0x73, 0xB2 ]; |
10
|
|
|
|
|
|
|
my $SIGMA3 = [ 0xC6, 0xEF, 0x37, 0x2F, 0xE9, 0x4F, 0x82, 0xBE ]; |
11
|
|
|
|
|
|
|
my $SIGMA4 = [ 0x54, 0xFF, 0x53, 0xA5, 0xF1, 0xD3, 0x6F, 0x1C ]; |
12
|
|
|
|
|
|
|
my $SIGMA5 = [ 0x10, 0xE5, 0x27, 0xFA, 0xDE, 0x68, 0x2D, 0x1D ]; |
13
|
|
|
|
|
|
|
my $SIGMA6 = [ 0xB0, 0x56, 0x88, 0xC2, 0xB3, 0xE6, 0xC1, 0xFD ]; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my @S1 = ( |
16
|
|
|
|
|
|
|
112,130, 44,236,179, 39,192,229,228,133, 87, 53,234, 12,174, 65, |
17
|
|
|
|
|
|
|
35,239,107,147, 69, 25,165, 33,237, 14, 79, 78, 29,101,146,189, |
18
|
|
|
|
|
|
|
134,184,175,143,124,235, 31,206, 62, 48,220, 95, 94,197, 11, 26, |
19
|
|
|
|
|
|
|
166,225, 57,202,213, 71, 93, 61,217, 1, 90,214, 81, 86,108, 77, |
20
|
|
|
|
|
|
|
139, 13,154,102,251,204,176, 45,116, 18, 43, 32,240,177,132,153, |
21
|
|
|
|
|
|
|
223, 76,203,194, 52,126,118, 5,109,183,169, 49,209, 23, 4,215, |
22
|
|
|
|
|
|
|
20, 88, 58, 97,222, 27, 17, 28, 50, 15,156, 22, 83, 24,242, 34, |
23
|
|
|
|
|
|
|
254, 68,207,178,195,181,122,145, 36, 8,232,168, 96,252,105, 80, |
24
|
|
|
|
|
|
|
170,208,160,125,161,137, 98,151, 84, 91, 30,149,224,255,100,210, |
25
|
|
|
|
|
|
|
16,196, 0, 72,163,247,117,219,138, 3,230,218, 9, 63,221,148, |
26
|
|
|
|
|
|
|
135, 92,131, 2,205, 74,144, 51,115,103,246,243,157,127,191,226, |
27
|
|
|
|
|
|
|
82,155,216, 38,200, 55,198, 59,129,150,111, 75, 19,190, 99, 46, |
28
|
|
|
|
|
|
|
233,121,167,140,159,110,188,142, 41,245,249,182, 47,253,180, 89, |
29
|
|
|
|
|
|
|
120,152, 6,106,231, 70,113,186,212, 37,171, 66,136,162,141,250, |
30
|
|
|
|
|
|
|
114, 7,185, 85,248,238,172, 10, 54, 73, 42,104, 60, 56,241,164, |
31
|
|
|
|
|
|
|
64, 40,211,123,187,201, 67,193, 21,227,173,244,119,199,128,158 |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
my @S2 = ( |
34
|
|
|
|
|
|
|
224,5,88,217,103,78,129,203,201,11,174,106,213,24,93,130, |
35
|
|
|
|
|
|
|
70,223,214,39,138,50,75,66,219,28,158,156,58,202,37,123, |
36
|
|
|
|
|
|
|
13,113,95,31,248,215,62,157,124,96,185,190,188,139,22,52, |
37
|
|
|
|
|
|
|
77,195,114,149,171,142,186,122,179,2,180,173,162,172,216,154, |
38
|
|
|
|
|
|
|
23,26,53,204,247,153,97,90,232,36,86,64,225,99,9,51, |
39
|
|
|
|
|
|
|
191,152,151,133,104,252,236,10,218,111,83,98,163,46,8,175, |
40
|
|
|
|
|
|
|
40,176,116,194,189,54,34,56,100,30,57,44,166,48,229,68, |
41
|
|
|
|
|
|
|
253,136,159,101,135,107,244,35,72,16,209,81,192,249,210,160, |
42
|
|
|
|
|
|
|
85,161,65,250,67,19,196,47,168,182,60,43,193,255,200,165, |
43
|
|
|
|
|
|
|
32,137,0,144,71,239,234,183,21,6,205,181,18,126,187,41, |
44
|
|
|
|
|
|
|
15,184,7,4,155,148,33,102,230,206,237,231,59,254,127,197, |
45
|
|
|
|
|
|
|
164,55,177,76,145,110,141,118,3,45,222,150,38,125,198,92, |
46
|
|
|
|
|
|
|
211,242,79,25,63,220,121,29,82,235,243,109,94,251,105,178, |
47
|
|
|
|
|
|
|
240,49,12,212,207,140,226,117,169,74,87,132,17,69,27,245, |
48
|
|
|
|
|
|
|
228,14,115,170,241,221,89,20,108,146,84,208,120,112,227,73, |
49
|
|
|
|
|
|
|
128,80,167,246,119,147,134,131,42,199,91,233,238,143,1,61 |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
my @S3 = ( |
52
|
|
|
|
|
|
|
56,65,22,118,217,147,96,242,114,194,171,154,117,6,87,160, |
53
|
|
|
|
|
|
|
145,247,181,201,162,140,210,144,246,7,167,39,142,178,73,222, |
54
|
|
|
|
|
|
|
67,92,215,199,62,245,143,103,31,24,110,175,47,226,133,13, |
55
|
|
|
|
|
|
|
83,240,156,101,234,163,174,158,236,128,45,107,168,43,54,166, |
56
|
|
|
|
|
|
|
197,134,77,51,253,102,88,150,58,9,149,16,120,216,66,204, |
57
|
|
|
|
|
|
|
239,38,229,97,26,63,59,130,182,219,212,152,232,139,2,235, |
58
|
|
|
|
|
|
|
10,44,29,176,111,141,136,14,25,135,78,11,169,12,121,17, |
59
|
|
|
|
|
|
|
127,34,231,89,225,218,61,200,18,4,116,84,48,126,180,40, |
60
|
|
|
|
|
|
|
85,104,80,190,208,196,49,203,42,173,15,202,112,255,50,105, |
61
|
|
|
|
|
|
|
8,98,0,36,209,251,186,237,69,129,115,109,132,159,238,74, |
62
|
|
|
|
|
|
|
195,46,193,1,230,37,72,153,185,179,123,249,206,191,223,113, |
63
|
|
|
|
|
|
|
41,205,108,19,100,155,99,157,192,75,183,165,137,95,177,23, |
64
|
|
|
|
|
|
|
244,188,211,70,207,55,94,71,148,250,252,91,151,254,90,172, |
65
|
|
|
|
|
|
|
60,76,3,53,243,35,184,93,106,146,213,33,68,81,198,125, |
66
|
|
|
|
|
|
|
57,131,220,170,124,119,86,5,27,164,21,52,30,28,248,82, |
67
|
|
|
|
|
|
|
32,20,233,189,221,228,161,224,138,241,214,122,187,227,64,79 |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
my @S4 = ( |
70
|
|
|
|
|
|
|
112,44,179,192,228,87,234,174,35,107,69,165,237,79,29,146, |
71
|
|
|
|
|
|
|
134,175,124,31,62,220,94,11,166,57,213,93,217,90,81,108, |
72
|
|
|
|
|
|
|
139,154,251,176,116,43,240,132,223,203,52,118,109,169,209,4, |
73
|
|
|
|
|
|
|
20,58,222,17,50,156,83,242,254,207,195,122,36,232,96,105, |
74
|
|
|
|
|
|
|
170,160,161,98,84,30,224,100,16,0,163,117,138,230,9,221, |
75
|
|
|
|
|
|
|
135,131,205,144,115,246,157,191,82,216,200,198,129,111,19,99, |
76
|
|
|
|
|
|
|
233,167,159,188,41,249,47,180,120,6,231,113,212,171,136,141, |
77
|
|
|
|
|
|
|
114,185,248,172,54,42,60,241,64,211,187,67,21,173,119,128, |
78
|
|
|
|
|
|
|
130,236,39,229,133,53,12,65,239,147,25,33,14,78,101,189, |
79
|
|
|
|
|
|
|
184,143,235,206,48,95,197,26,225,202,71,61,1,214,86,77, |
80
|
|
|
|
|
|
|
13,102,204,45,18,32,177,153,76,194,126,5,183,49,23,215, |
81
|
|
|
|
|
|
|
88,97,27,28,15,22,24,34,68,178,181,145,8,168,252,80, |
82
|
|
|
|
|
|
|
208,125,137,151,91,149,255,210,196,72,247,219,3,218,63,148, |
83
|
|
|
|
|
|
|
92,2,74,51,103,243,127,226,155,38,55,59,150,75,190,46, |
84
|
|
|
|
|
|
|
121,140,110,142,245,182,253,89,152,106,70,186,37,66,162,250, |
85
|
|
|
|
|
|
|
7,85,238,10,73,104,56,164,40,123,201,193,227,244,199,158 |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
1
|
|
|
1
|
0
|
6
|
sub blocksize { 16 } |
90
|
1
|
|
|
1
|
0
|
24
|
sub keysize { 32 } |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub new { |
95
|
98
|
|
|
98
|
1
|
55181
|
my $class = shift; |
96
|
98
|
|
|
|
|
178
|
my $key = shift; |
97
|
98
|
50
|
|
|
|
240
|
if (!defined $key) { |
98
|
0
|
|
|
|
|
0
|
croak q{Usage: Crypt::Camellia_PP->new($key);}; |
99
|
|
|
|
|
|
|
} |
100
|
98
|
|
|
|
|
161
|
my $keysize = length $key; |
101
|
98
|
50
|
100
|
|
|
730
|
if ($keysize != 16 && $keysize != 24 && $keysize != 32) { |
|
|
|
66
|
|
|
|
|
102
|
0
|
|
|
|
|
0
|
croak q{wrong key length: key must be 128, 192 or 256 bit.}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
98
|
|
|
|
|
592
|
my @key = unpack 'C*', $key; |
106
|
98
|
|
|
|
|
3942
|
my $self = bless { |
107
|
|
|
|
|
|
|
keysize => $keysize, |
108
|
|
|
|
|
|
|
kw => [ |
109
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
110
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
111
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
112
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0] |
113
|
|
|
|
|
|
|
], |
114
|
|
|
|
|
|
|
k => [ |
115
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
116
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
117
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
118
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
119
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
120
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
121
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
122
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
123
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
124
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
125
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
126
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
127
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
128
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
129
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
130
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
131
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
132
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
133
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
134
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
135
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
136
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
137
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
138
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0] |
139
|
|
|
|
|
|
|
], |
140
|
|
|
|
|
|
|
kl => [ |
141
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
142
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
143
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
144
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
145
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0], |
146
|
|
|
|
|
|
|
[0,0,0,0,0,0,0,0] |
147
|
|
|
|
|
|
|
] |
148
|
|
|
|
|
|
|
}, $class; |
149
|
98
|
|
|
|
|
440
|
$self->_prepare_sub_key(\@key); |
150
|
98
|
|
|
|
|
916
|
return $self; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub encrypt { |
155
|
3928
|
|
|
3928
|
1
|
2408995
|
my $self = shift; |
156
|
3928
|
|
|
|
|
6441
|
my $in = shift; |
157
|
3928
|
|
|
|
|
23799
|
my $l = [ unpack 'C8', $in ]; |
158
|
3928
|
|
|
|
|
15354
|
my $r = [ unpack 'C8', substr $in, 8, 8]; |
159
|
3928
|
|
|
|
|
13497
|
_xor_block($l, $l, $self->{kw}->[0], 8); |
160
|
3928
|
|
|
|
|
10717
|
_xor_block($r, $r, $self->{kw}->[1], 8); |
161
|
3928
|
100
|
|
|
|
11542
|
if ($self->{keysize} == 16) { |
162
|
1284
|
|
|
|
|
5600
|
for (my $i = 0; $i < 18; $i += 2) { |
163
|
11556
|
|
|
|
|
25479
|
_feistel($r, 0, $l, $self->{k}->[$i]); |
164
|
11556
|
|
|
|
|
28393
|
_feistel($l, 0, $r, $self->{k}->[$i+1]); |
165
|
11556
|
100
|
|
|
|
41569
|
if ($i == 4) { # round 6 |
|
|
100
|
|
|
|
|
|
166
|
1284
|
|
|
|
|
3242
|
_flayer($l, $l, $self->{kl}->[0], 0); |
167
|
1284
|
|
|
|
|
3347
|
_flayer_1($r, $r, $self->{kl}->[1], 0); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
elsif ($i == 10) { # round 12 |
170
|
1284
|
|
|
|
|
3365
|
_flayer($l, $l, $self->{kl}->[2], 0); |
171
|
1284
|
|
|
|
|
2785
|
_flayer_1($r, $r, $self->{kl}->[3], 0); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
else { |
176
|
2644
|
|
|
|
|
6906
|
for (my $i = 0; $i < 24; $i += 2) { |
177
|
31728
|
|
|
|
|
76337
|
_feistel($r, 0, $l, $self->{k}->[$i]); |
178
|
31728
|
|
|
|
|
79257
|
_feistel($l, 0, $r, $self->{k}->[$i+1]); |
179
|
31728
|
100
|
|
|
|
122610
|
if ($i == 4) { # round 6 |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
180
|
2644
|
|
|
|
|
6960
|
_flayer($l, $l, $self->{kl}->[0], 0); |
181
|
2644
|
|
|
|
|
7196
|
_flayer_1($r, $r, $self->{kl}->[1], 0); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
elsif ($i == 10) { # round 12 |
184
|
2644
|
|
|
|
|
6395
|
_flayer($l, $l, $self->{kl}->[2], 0); |
185
|
2644
|
|
|
|
|
12507
|
_flayer_1($r, $r, $self->{kl}->[3], 0); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
elsif ($i == 16) { # round 18 |
188
|
2644
|
|
|
|
|
6450
|
_flayer($l, $l, $self->{kl}->[4], 0); |
189
|
2644
|
|
|
|
|
5934
|
_flayer_1($r, $r, $self->{kl}->[5], 0); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
3928
|
|
|
|
|
10022
|
_xor_block($r, $r, $self->{kw}->[2], 8); |
194
|
3928
|
|
|
|
|
9062
|
_xor_block($l, $l, $self->{kw}->[3], 8); |
195
|
|
|
|
|
|
|
|
196
|
3928
|
|
|
|
|
25473
|
return pack 'C16', @$r, @$l; |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub decrypt { |
201
|
3926
|
|
|
3926
|
1
|
22147
|
my $self = shift; |
202
|
3926
|
|
|
|
|
7688
|
my $in = shift; |
203
|
|
|
|
|
|
|
|
204
|
3926
|
|
|
|
|
19763
|
my $r = [ unpack 'C8', $in ]; |
205
|
3926
|
|
|
|
|
15217
|
my $l = [ unpack 'C8', substr $in, 8, 8]; |
206
|
3926
|
|
|
|
|
13137
|
_xor_block($r, $r, $self->{kw}->[2], 8); |
207
|
3926
|
|
|
|
|
15045
|
_xor_block($l, $l, $self->{kw}->[3], 8); |
208
|
3926
|
100
|
|
|
|
9615
|
if ($self->{keysize} == 16) { |
209
|
1284
|
|
|
|
|
3228
|
for (my $i = 16; $i >= 0; $i -= 2) { |
210
|
11556
|
|
|
|
|
28075
|
_feistel($l, 0, $r, $self->{k}->[$i+1]); |
211
|
11556
|
|
|
|
|
31081
|
_feistel($r, 0, $l, $self->{k}->[$i]); |
212
|
11556
|
100
|
|
|
|
41106
|
if ($i == 12) { |
|
|
100
|
|
|
|
|
|
213
|
1284
|
|
|
|
|
3338
|
_flayer($r, $r, $self->{kl}->[3]); |
214
|
1284
|
|
|
|
|
2989
|
_flayer_1($l, $l, $self->{kl}->[2]); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
elsif ($i == 6) { |
217
|
1284
|
|
|
|
|
3145
|
_flayer($r, $r, $self->{kl}->[1]); |
218
|
1284
|
|
|
|
|
2957
|
_flayer_1($l, $l, $self->{kl}->[0]); |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
else { |
223
|
2642
|
|
|
|
|
6577
|
for (my $i = 22; $i >= 0; $i -= 2) { |
224
|
31704
|
|
|
|
|
85279
|
_feistel($l, 0, $r, $self->{k}->[$i+1]); |
225
|
31704
|
|
|
|
|
89914
|
_feistel($r, 0, $l, $self->{k}->[$i]); |
226
|
31704
|
100
|
|
|
|
121660
|
if ($i == 18) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
227
|
2642
|
|
|
|
|
6645
|
_flayer($r, $r, $self->{kl}->[5]); |
228
|
2642
|
|
|
|
|
6142
|
_flayer_1($l, $l, $self->{kl}->[4]); |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
elsif ($i == 12) { |
231
|
2642
|
|
|
|
|
6763
|
_flayer($r, $r, $self->{kl}->[3]); |
232
|
2642
|
|
|
|
|
5912
|
_flayer_1($l, $l, $self->{kl}->[2]); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
elsif ($i == 6) { |
235
|
2642
|
|
|
|
|
6047
|
_flayer($r, $r, $self->{kl}->[1]); |
236
|
2642
|
|
|
|
|
6213
|
_flayer_1($l, $l, $self->{kl}->[0]); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
} |
241
|
3926
|
|
|
|
|
10409
|
_xor_block($l, $l, $self->{kw}->[0], 8); |
242
|
3926
|
|
|
|
|
9050
|
_xor_block($r, $r, $self->{kw}->[1], 8); |
243
|
|
|
|
|
|
|
|
244
|
3926
|
|
|
|
|
25980
|
return pack 'C16', @$l, @$r; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
sub _prepare_sub_key { |
250
|
98
|
|
|
98
|
|
158
|
my $self = shift; |
251
|
98
|
|
|
|
|
115
|
my $key = shift; |
252
|
98
|
|
|
|
|
270
|
my $kl = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; |
253
|
98
|
|
|
|
|
253
|
my $kr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; |
254
|
98
|
|
|
|
|
270
|
my $ka = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; |
255
|
|
|
|
|
|
|
|
256
|
98
|
100
|
|
|
|
433
|
if ($self->{keysize} == 16) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
257
|
18
|
|
|
|
|
41
|
_move($kl, 0, $key, 0, 16); |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
elsif ($self->{keysize} == 24) { |
260
|
11
|
|
|
|
|
33
|
_move($kl, 0, $key, 0, 16); |
261
|
11
|
|
|
|
|
22
|
_move($kr, 0, $key, 16, 8); |
262
|
11
|
|
|
|
|
40
|
for (my $i = 0; $i < 8; $i++) { |
263
|
88
|
|
|
|
|
211
|
$kr->[$i+8] = $key->[$i+16] ^ 0xff; |
264
|
|
|
|
|
|
|
} |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
elsif ($self->{keysize} == 32) { |
267
|
69
|
|
|
|
|
168
|
_move($kl, 0, $key, 0, 16); |
268
|
69
|
|
|
|
|
99
|
_move($kr, 0, $key, 16,16); |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
|
271
|
98
|
|
|
|
|
191
|
_xor_block($ka, $kl, $kr, 16); |
272
|
98
|
|
|
|
|
196
|
_feistel($ka, 8, $ka, $SIGMA1); |
273
|
98
|
|
|
|
|
353
|
_feistel($ka, 0, [@$ka[8..15]], $SIGMA2); |
274
|
98
|
|
|
|
|
283
|
_xor_block($ka, $kl, $ka, 16); |
275
|
|
|
|
|
|
|
|
276
|
98
|
|
|
|
|
198
|
_feistel($ka, 8, $ka, $SIGMA3); |
277
|
98
|
|
|
|
|
311
|
_feistel($ka, 0, [@$ka[8..15]], $SIGMA4); |
278
|
|
|
|
|
|
|
|
279
|
98
|
100
|
|
|
|
262
|
if ($self->{keysize} == 16) { |
280
|
18
|
|
|
|
|
64
|
_rot_shift($self->{kw}->[0], $self->{kw}->[1], $kl, 0); |
281
|
|
|
|
|
|
|
|
282
|
18
|
|
|
|
|
71
|
_rot_shift($self->{k}->[0], $self->{k}->[1], $ka, 0); |
283
|
18
|
|
|
|
|
50
|
_rot_shift($self->{k}->[2], $self->{k}->[3], $kl, 15); |
284
|
18
|
|
|
|
|
52
|
_rot_shift($self->{k}->[4], $self->{k}->[5], $ka, 15); |
285
|
|
|
|
|
|
|
|
286
|
18
|
|
|
|
|
47
|
_rot_shift($self->{kl}->[0], $self->{kl}->[1], $ka, 30); |
287
|
|
|
|
|
|
|
|
288
|
18
|
|
|
|
|
57
|
_rot_shift($self->{k}->[6], $self->{k}->[7], $kl, 45); |
289
|
18
|
|
|
|
|
73
|
_rot_shift($self->{k}->[8], [0,0,0,0,0,0,0,0], $ka, 45); |
290
|
18
|
|
|
|
|
78
|
_rot_shift([0,0,0,0,0,0,0,0], $self->{k}->[9], $kl, 60); |
291
|
18
|
|
|
|
|
61
|
_rot_shift($self->{k}->[10], $self->{k}->[11], $ka, 60); |
292
|
|
|
|
|
|
|
|
293
|
18
|
|
|
|
|
72
|
_rot_shift($self->{kl}->[2], $self->{kl}->[3], $kl, 77); |
294
|
|
|
|
|
|
|
|
295
|
18
|
|
|
|
|
55
|
_rot_shift($self->{k}->[12], $self->{k}->[13], $kl, 94); |
296
|
18
|
|
|
|
|
58
|
_rot_shift($self->{k}->[14], $self->{k}->[15], $ka, 94); |
297
|
18
|
|
|
|
|
81
|
_rot_shift($self->{k}->[16], $self->{k}->[17], $kl, 111); |
298
|
|
|
|
|
|
|
|
299
|
18
|
|
|
|
|
52
|
_rot_shift($self->{kw}->[2], $self->{kw}->[3], $ka, 111); |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
else { |
302
|
80
|
|
|
|
|
225
|
my $kb = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; |
303
|
80
|
|
|
|
|
164
|
_xor_block($kb, $kr, $ka, 16); |
304
|
80
|
|
|
|
|
136
|
_feistel($kb, 8, $kb, $SIGMA5); |
305
|
80
|
|
|
|
|
256
|
_feistel($kb, 0, [@$kb[8..15]], $SIGMA6); |
306
|
|
|
|
|
|
|
|
307
|
80
|
|
|
|
|
270
|
_rot_shift($self->{kw}->[0], $self->{kw}->[1], $kl, 0); |
308
|
|
|
|
|
|
|
|
309
|
80
|
|
|
|
|
214
|
_rot_shift($self->{k}->[0], $self->{k}->[1], $kb, 0); |
310
|
80
|
|
|
|
|
189
|
_rot_shift($self->{k}->[2], $self->{k}->[3], $kr, 15); |
311
|
80
|
|
|
|
|
200
|
_rot_shift($self->{k}->[4], $self->{k}->[5], $ka, 15); |
312
|
|
|
|
|
|
|
|
313
|
80
|
|
|
|
|
203
|
_rot_shift($self->{kl}->[0], $self->{kl}->[1], $kr, 30); |
314
|
|
|
|
|
|
|
|
315
|
80
|
|
|
|
|
212
|
_rot_shift($self->{k}->[6], $self->{k}->[7], $kb, 30); |
316
|
80
|
|
|
|
|
215
|
_rot_shift($self->{k}->[8], $self->{k}->[9], $kl, 45); |
317
|
80
|
|
|
|
|
202
|
_rot_shift($self->{k}->[10], $self->{k}->[11], $ka, 45); |
318
|
|
|
|
|
|
|
|
319
|
80
|
|
|
|
|
193
|
_rot_shift($self->{kl}->[2], $self->{kl}->[3], $kl, 60); |
320
|
|
|
|
|
|
|
|
321
|
80
|
|
|
|
|
197
|
_rot_shift($self->{k}->[12], $self->{k}->[13], $kr, 60); |
322
|
80
|
|
|
|
|
205
|
_rot_shift($self->{k}->[14], $self->{k}->[15], $kb, 60); |
323
|
80
|
|
|
|
|
207
|
_rot_shift($self->{k}->[16], $self->{k}->[17], $kl, 77); |
324
|
|
|
|
|
|
|
|
325
|
80
|
|
|
|
|
256
|
_rot_shift($self->{kl}->[4], $self->{kl}->[5], $ka, 77); |
326
|
|
|
|
|
|
|
|
327
|
80
|
|
|
|
|
197
|
_rot_shift($self->{k}->[18], $self->{k}->[19], $kr, 94); |
328
|
80
|
|
|
|
|
193
|
_rot_shift($self->{k}->[20], $self->{k}->[21], $ka, 94); |
329
|
80
|
|
|
|
|
197
|
_rot_shift($self->{k}->[22], $self->{k}->[23], $kl, 111); |
330
|
|
|
|
|
|
|
|
331
|
80
|
|
|
|
|
190
|
_rot_shift($self->{kw}->[2], $self->{kw}->[3], $kb, 111); |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
98
|
|
|
|
|
291
|
return $self; |
335
|
|
|
|
|
|
|
} |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
sub _move { |
340
|
42166
|
|
|
42166
|
|
104183
|
for (my $i = 0; $i < $_[4]; $i++) { |
341
|
338664
|
|
|
|
|
866512
|
$_[0]->[$i+$_[1]] = $_[2]->[$i+$_[3]]; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
} |
344
|
|
|
|
|
|
|
|
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
sub _xor_block { |
347
|
205332
|
|
|
205332
|
|
487568
|
for (my $i = 0; $i < $_[3]; $i++) { |
348
|
1644864
|
|
|
|
|
4249108
|
$_[0]->[$i] = $_[1]->[$i] ^ $_[2]->[$i]; |
349
|
|
|
|
|
|
|
} |
350
|
|
|
|
|
|
|
} |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
sub _feistel { |
354
|
173640
|
|
|
173640
|
|
228955
|
my $dist = shift; |
355
|
173640
|
|
|
|
|
201537
|
my $o = shift; |
356
|
173640
|
|
|
|
|
206421
|
my $x = shift; |
357
|
173640
|
|
|
|
|
206145
|
my $k = shift; |
358
|
173640
|
|
|
|
|
365471
|
my $w = [0,0,0,0,0,0,0,0]; |
359
|
173640
|
|
|
|
|
326588
|
_xor_block($w, $x, $k, 8); |
360
|
|
|
|
|
|
|
# S funcs |
361
|
173640
|
|
|
|
|
625677
|
my @ws = ($S1[$w->[0]], $S2[$w->[1]], $S3[$w->[2]], $S4[$w->[3]], |
362
|
|
|
|
|
|
|
$S2[$w->[4]], $S3[$w->[5]], $S4[$w->[6]], $S1[$w->[7]]); |
363
|
|
|
|
|
|
|
# P func |
364
|
173640
|
|
|
|
|
354465
|
$dist->[0+$o] ^= $ws[0] ^ $ws[2] ^ $ws[3] ^ $ws[5] ^ $ws[6] ^ $ws[7]; |
365
|
173640
|
|
|
|
|
299965
|
$dist->[1+$o] ^= $ws[0] ^ $ws[1] ^ $ws[3] ^ $ws[4] ^ $ws[6] ^ $ws[7]; |
366
|
173640
|
|
|
|
|
289764
|
$dist->[2+$o] ^= $ws[0] ^ $ws[1] ^ $ws[2] ^ $ws[4] ^ $ws[5] ^ $ws[7]; |
367
|
173640
|
|
|
|
|
280401
|
$dist->[3+$o] ^= $ws[1] ^ $ws[2] ^ $ws[3] ^ $ws[4] ^ $ws[5] ^ $ws[6]; |
368
|
173640
|
|
|
|
|
248997
|
$dist->[4+$o] ^= $ws[0] ^ $ws[1] ^ $ws[5] ^ $ws[6] ^ $ws[7]; |
369
|
173640
|
|
|
|
|
273342
|
$dist->[5+$o] ^= $ws[1] ^ $ws[2] ^ $ws[4] ^ $ws[6] ^ $ws[7]; |
370
|
173640
|
|
|
|
|
246703
|
$dist->[6+$o] ^= $ws[2] ^ $ws[3] ^ $ws[4] ^ $ws[5] ^ $ws[7]; |
371
|
173640
|
|
|
|
|
487366
|
$dist->[7+$o] ^= $ws[0] ^ $ws[3] ^ $ws[4] ^ $ws[5] ^ $ws[6]; |
372
|
|
|
|
|
|
|
} |
373
|
|
|
|
|
|
|
|
374
|
|
|
|
|
|
|
|
375
|
|
|
|
|
|
|
sub _flayer { |
376
|
20994
|
|
|
20994
|
|
31094
|
my ($dist, $x, $k) = @_; |
377
|
20994
|
|
|
|
|
41113
|
_move($dist, 0, $x, 0, 8); |
378
|
20994
|
|
|
|
|
51133
|
$dist->[4+0] ^= ((($x->[0] & $k->[0]) << 1) & 0xff) ^ ($x->[1] & $k->[1]) >> 7; |
379
|
20994
|
|
|
|
|
39944
|
$dist->[4+1] ^= ((($x->[1] & $k->[1]) << 1) & 0xff) ^ ($x->[2] & $k->[2]) >> 7; |
380
|
20994
|
|
|
|
|
38637
|
$dist->[4+2] ^= ((($x->[2] & $k->[2]) << 1) & 0xff) ^ ($x->[3] & $k->[3]) >> 7; |
381
|
20994
|
|
|
|
|
37726
|
$dist->[4+3] ^= ((($x->[3] & $k->[3]) << 1) & 0xff) ^ ($x->[0] & $k->[0]) >> 7; |
382
|
20994
|
|
|
|
|
30699
|
$dist->[0] ^= $dist->[4+0] | $k->[4+0]; |
383
|
20994
|
|
|
|
|
27614
|
$dist->[1] ^= $dist->[4+1] | $k->[4+1]; |
384
|
20994
|
|
|
|
|
34761
|
$dist->[2] ^= $dist->[4+2] | $k->[4+2]; |
385
|
20994
|
|
|
|
|
39544
|
$dist->[3] ^= $dist->[4+3] | $k->[4+3]; |
386
|
|
|
|
|
|
|
} |
387
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
sub _flayer_1 { |
390
|
20994
|
|
|
20994
|
|
30096
|
my ($dist, $x, $k) = @_; |
391
|
20994
|
|
|
|
|
41057
|
_move($dist, 0, $x, 0, 8); |
392
|
20994
|
|
|
|
|
33251
|
$dist->[0] ^= $x->[4+0] | $k->[4+0]; |
393
|
20994
|
|
|
|
|
28995
|
$dist->[1] ^= $x->[4+1] | $k->[4+1]; |
394
|
20994
|
|
|
|
|
30107
|
$dist->[2] ^= $x->[4+2] | $k->[4+2]; |
395
|
20994
|
|
|
|
|
27572
|
$dist->[3] ^= $x->[4+3] | $k->[4+3]; |
396
|
20994
|
|
|
|
|
40608
|
$dist->[4+0] ^= ((($dist->[0] & $k->[0]) << 1) & 0xff) ^ ($dist->[1] & $k->[1]) >> 7; |
397
|
20994
|
|
|
|
|
44699
|
$dist->[4+1] ^= ((($dist->[1] & $k->[1]) << 1) & 0xff) ^ ($dist->[2] & $k->[2]) >> 7; |
398
|
20994
|
|
|
|
|
39024
|
$dist->[4+2] ^= ((($dist->[2] & $k->[2]) << 1) & 0xff) ^ ($dist->[3] & $k->[3]) >> 7; |
399
|
20994
|
|
|
|
|
75076
|
$dist->[4+3] ^= ((($dist->[3] & $k->[3]) << 1) & 0xff) ^ ($dist->[0] & $k->[0]) >> 7; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
|
403
|
|
|
|
|
|
|
sub _rot_shift { |
404
|
1612
|
|
|
1612
|
|
2161
|
my ($dist_l, $dist_r, $src, $bit) = @_; |
405
|
1612
|
100
|
|
|
|
2572
|
if ($bit == 0) { |
406
|
196
|
|
|
|
|
601
|
for (my $i = 0; $i < 8; $i++) { |
407
|
1568
|
|
|
|
|
2912
|
$dist_l->[$i] = $src->[$i]; |
408
|
|
|
|
|
|
|
} |
409
|
196
|
|
|
|
|
438
|
for (my $i = 0; $i < 8; $i++) { |
410
|
1568
|
|
|
|
|
2962
|
$dist_r->[$i] = $src->[$i+8]; |
411
|
|
|
|
|
|
|
} |
412
|
196
|
|
|
|
|
330
|
return; |
413
|
|
|
|
|
|
|
} |
414
|
1416
|
|
|
|
|
1900
|
my $o = int($bit / 8) + 1; |
415
|
1416
|
|
|
|
|
1560
|
my $so = $o * 8 - $bit; |
416
|
1416
|
|
|
|
|
1389
|
$o = $o % 16; |
417
|
1416
|
|
|
|
|
2697
|
for (my $i = 0; $i < 8; $i++) { |
418
|
11328
|
|
|
|
|
19014
|
$dist_l->[$i] = (($src->[($i+$o) % 16] >> $so) & 0xff) |
419
|
|
|
|
|
|
|
| (($src->[($i+$o-1) % 16] << (8 - $so)) & 0xff); |
420
|
11328
|
|
|
|
|
29196
|
$dist_r->[$i] = (($src->[($i+8+$o) % 16] >> $so) & 0xff) |
421
|
|
|
|
|
|
|
| (($src->[($i+8+$o-1) % 16] << (8 - $so)) & 0xff); |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
1; |
426
|
|
|
|
|
|
|
__END__ |