blib/lib/Net/Address/IP/Cipher.pm

filter: Statementstmt 65.0% 91 / 140 Branchbran 50.0% 8 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.2% 13 / 16 Podpod 100% 3 / 3 totaltotal 64.4% 116 / 180 CC 11 SCAR 13.6
CC
11
Cov
63%
dec
12.0
new
6.3
enc
3.0
CRAP 3.9
↑ Summary
1 package Net::Address::IP::Cipher 0.5;
2  
3 4 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Net::IP qw(:PROC);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Crypt::KeyDerivation qw(pbkdf2);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Crypt::Cipher::AES;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Net::Address::IP::Cipher - IPv6 / IPv4 address encryption to a valid
16 address, for pseudo anonymization.
17  
18 =head1 VERSION
19  
20 Version 0.5
21  
22 =head1 SYNOPSIS
23  
24 Net::Address::IP::Cipher encrypts and decrypts IPv6 and IPv4 addresses
25 to another valid IPv6/v4 address, using a secret key, in a way that's
26 impossible to guess the original IP without the key.
27  
28 use Net::Address::IP::Cipher;
29  
30 my $ipcipher = Net::Address::IP::Cipher->new(
31 password => 'super secret'
32 );
33 my $enc = $ipcipher->enc('::1');
34 print $enc; # 3a3e:7137:6e36:5ecd:4d31:e516:cf47:ec1b
35  
36 It's intended use is to pseudo-anonymize IPs from logs, packet captures,
37 and other analysis. By this way you benefit of having still valid IP
38 addresses and be able to group streams of several messagess, but without
39 revealing the source.
40  
41 This module implements in native perl language the "ipcipher"
42 specification from:
43 L<https://github.com/PowerDNS/ipcipher>
44  
45  
46 =head1 PREREQUISITES
47  
48 This module requires L<Net::IP> for v6/v4 handling and L<CryptX> for
49 all crypto stuff (L<Crypt::KeyDerivation>, L<Crypt::Cipher::AES>).
50  
51 =head1 METHODS
52  
53 =head2 new
54  
55 Creates a new Net::Address::IP::Cipher object. You must indicate
56 the secret key for encryption/decryption:
57  
58 my $ipcipher = Net::Address::IP::Cipher->new(password => 'super secret');
59  
60 The key should be declared in either one of two formats:
61  
62 password => 'super secret'
63  
64 for any string used as a password, or
65  
66 barekey => 'bb8dcd7be9a6f43b3304c640d7d7103c'
67  
68 for an hexadecimal representation of a 128-bit key.
69  
70 If you provide both, just the 'password' format will be used.
71  
72 =cut
73  
74 sub new {
75 2 ▶ my $this = shift;
Subroutine new: called CC=6 SCAR=18.4
Pod documented
76 2 my %params = @_;
77  
78 2 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
79  
80 2 my $key;
81  
82 2 ▶ if ($params{'password'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params{'password'}) { } 1 1 2
elsif ($params{'barekey'}) { } 1 0 1
83 1 $key = pbkdf2($params{'password'}, 'ipcipheripcipher', 50000, 'SHA1', 16);
84 }
85 elsif ($params{'barekey'}) {
86 1 $key = pack 'H*', $params{'barekey'};
87 1 ▶ croak("If you provide a 'barekey' it should be in hexadecimal format, and its lenght should be 128 bits") unless length($key) == 16;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key == 16 0 1 1
88 }
89  
90 2 my $self = {};
91 2 bless $self, $class;
92  
93 2 $self->{'PRIVKEY'} = $key;
94 2 ▶ $self->{'CIPHER'} = Crypt::Cipher::AES->new($key)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"CIPHER"} = "Crypt::Cipher::AES"->new($key) or die "Can't create AES cipher, please check your key!" 0 2 2
95 or die "Can't create AES cipher, please check your key!";
96  
97 2 return $self;
98 }
99  
100 # PRIVATE FUNCTIONS
101  
102 sub _xor4 {
103 24 ▶ my $ps = shift;
Subroutine _xor4: called CC=2 SCAR=6.9
104 24 my $pk = shift;
105  
106 24 my @out;
107 24 foreach my $i (0..3) {
108 96 push @out, ($ps->[$i] ^ $pk->[$i]) & 0xff;
109 }
110  
111 24 return @out;
112 }
113  
114 sub _rotl {
115 108 ▶ my ($b, $r) = @_;
Subroutine _rotl: called CC=1 SCAR=0.0
116  
117 108 return (($b << $r) & 0xff) | ($b >> (8 - $r));
118 }
119  
120 sub _permute_fwd {
121 18 ▶ my $b = shift;
Subroutine _permute_fwd: called CC=1 SCAR=0.0
122  
123 18 $b->[0] += $b->[1];
124 18 $b->[2] += $b->[3];
125 18 $b->[0] &= 0xff;
126 18 $b->[2] &= 0xff;
127 18 $b->[1] = &_rotl($b->[1], 2);
128 18 $b->[3] = &_rotl($b->[3], 5);
129 18 $b->[1] ^= $b->[0];
130 18 $b->[3] ^= $b->[2];
131 18 $b->[0] = &_rotl($b->[0], 4);
132 18 $b->[0] += $b->[3];
133 18 $b->[2] += $b->[1];
134 18 $b->[0] &= 0xff;
135 18 $b->[2] &= 0xff;
136 18 $b->[1] = &_rotl($b->[1], 3);
137 18 $b->[3] = &_rotl($b->[3], 7);
138 18 $b->[1] ^= $b->[2];
139 18 $b->[3] ^= $b->[0];
140 18 $b->[2] = &_rotl($b->[2], 4);
141  
142 18 return $b;
143 }
144  
145 sub _permute_bwd {
146 0 ▶ my $b = shift;
Subroutine _permute_bwd: not called CC=1 SCAR=6.9
147  
148 0 $b->[2] = &_rotl($b->[2], 4);
149 0 $b->[1] ^= $b->[2];
150 0 $b->[3] ^= $b->[0];
151 0 $b->[1] = &_rotl($b->[1], 5);
152 0 $b->[3] = &_rotl($b->[3], 1);
153 0 $b->[0] -= $b->[3];
154 0 $b->[2] -= $b->[1];
155 0 $b->[0] &= 0xff;
156 0 $b->[2] &= 0xff;
157 0 $b->[0] = &_rotl($b->[0], 4);
158 0 $b->[1] ^= $b->[0];
159 0 $b->[3] ^= $b->[2];
160 0 $b->[1] = &_rotl($b->[1], 6);
161 0 $b->[3] = &_rotl($b->[3], 3);
162 0 $b->[0] -= $b->[1];
163 0 $b->[2] -= $b->[3];
164 0 $b->[0] &= 0xff;
165 0 $b->[2] &= 0xff;
166  
167 0 return $b;
168 }
169  
170  
171 sub _encrypt {
172 6 ▶ my ($key, $ip) = @_;
Subroutine _encrypt: called CC=1 SCAR=0.0
173  
174 6 my @key = map {unpack('C', $_) } split //, $key;
175 6 my @state = split /\./, $ip;
176  
177 6 my @pedazo = @key[0..3];
178 6 @state = &_xor4(\@state, \@pedazo);
179 6 @state = @{&_permute_fwd(\@state)};
180 6 @pedazo = @key[4..7];
181 6 @state = &_xor4(\@state, \@pedazo);
182 6 @state = @{&_permute_fwd(\@state)};
183 6 @pedazo = @key[8..11];
184 6 @state = &_xor4(\@state, \@pedazo);
185 6 @state = @{&_permute_fwd(\@state)};
186 6 @pedazo = @key[12..15];
187 6 @state = &_xor4(\@state, \@pedazo);
188  
189 6 return join '.', @state;
190 }
191  
192 sub _decrypt {
193 0 ▶ my ($key, $ip) = @_;
Subroutine _decrypt: not called CC=1 SCAR=6.9
194  
195 0 my @key = map {unpack('C', $_) } split //, $key;
196 0 my @state = split /\./, $ip;
197  
198 0 my @pedazo = @key[12..15];
199 0 @state = &_xor4(\@state, \@pedazo);
200 0 @state = @{&_permute_bwd(\@state)};
201 0 @pedazo = @key[8..11];
202 0 @state = &_xor4(\@state, \@pedazo);
203 0 @state = @{&_permute_bwd(\@state)};
204 0 @pedazo = @key[4..7];
205 0 @state = &_xor4(\@state, \@pedazo);
206 0 @state = @{&_permute_bwd(\@state)};
207 0 @pedazo = @key[0..3];
208 0 @state = &_xor4(\@state, \@pedazo);
209  
210 0 return join '.', @state;
211 }
212  
213  
214 =head2 enc
215  
216 Receive an IPv6 or IPv4 string address, in any valid format
217 for Net::IP, and returns the encrypted version as string.
218  
219 my $enc = $ipcipher->enc('::1');
220 print $enc; # b733:fb7:c957:82fc:3d67:e7c3:a667:28da
221  
222 =cut
223  
224 sub enc {
225 12 ▶ my $self = shift;
Subroutine enc: called CC=3 SCAR=11.0
Pod documented
226 12 my $ipin = shift;
227  
228 12 my $out;
229  
230 12 ▶ my $ipvx = new Net::IP($ipin) or croak 'IP not valid: ' . (Net::IP::Error());
Branch: 1 branch
BranchTrueFalseTotal
my $ipvx = "Net::IP"->new($ipin) or croak('IP not valid: ' . Net::IP::Error()) 0 12 12
231  
232 12 ▶ if ($ipvx->version == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipvx->version == 6) { } 6 6 12
233 6 my $ciphertext = $self->{'CIPHER'}->encrypt(pack('B*', $ipvx->binip));
234  
235 6 my $enc = new Net::IP(ip_bintoip(unpack('B*', $ciphertext), 6));
236 6 $out = $enc->short;
237 }
238 else {
239 6 $out = &_encrypt($self->{'PRIVKEY'}, $ipin);
240 }
241  
242 12 return $out;
243 }
244  
245 =head2 dec
246  
247 Receive and IPv6 or IPv4 string address in its encrypted version,
248 and returns the decrypted IP string.
249  
250 my $dec = $ipcipher->dec('b733:fb7:c957:82fc:3d67:e7c3:a667:28da');
251 print $dec; # ::1
252  
253 =cut
254  
255 sub dec {
256 0 ▶ my $self = shift;
Subroutine dec: not called CC=3 SCAR=24.8
Pod documented
257 0 my $ipin = shift;
258  
259 0 my $out;
260  
261 0 ▶ my $ipvx = new Net::IP($ipin) or croak 'IP not valid: ' . (Net::IP::Error());
Branch: 1 branch
BranchTrueFalseTotal
my $ipvx = "Net::IP"->new($ipin) or croak('IP not valid: ' . Net::IP::Error()) 0 0 0
262  
263 0 ▶ if ($ipvx->version == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ipvx->version == 6) { } 0 0 0
264 0 my $plain = $self->{'CIPHER'}->decrypt(pack('B*', $ipvx->binip));
265 0 my $dec = new Net::IP(ip_bintoip(unpack('B*', $plain), 6));
266 0 $out = $dec->short;
267 }
268 else {
269 0 $out = &_decrypt($self->{'PRIVKEY'}, $ipin);
270 }
271  
272 0 return $out;
273 }
274  
275 =head1 AUTHOR
276  
277 Hugo Salgado, C<< <hsalgado at vulcano.cl> >>
278  
279 =head1 BUGS
280  
281 Please report any bugs or feature requests through
282 the web interface at L<https://github.com/huguei/p5-Net-Address-IP-Cipher/issues>. I will be notified, and then you'll
283 automatically be notified of progress on your bug as I make changes.
284  
285  
286  
287  
288 =head1 SUPPORT
289  
290 You can find documentation for this module with the perldoc command.
291  
292 perldoc Net::Address::IP::Cipher
293  
294  
295 You can also look for information at:
296  
297 =over 4
298  
299 =item * Github's request tracker (report bugs here)
300  
301 L<https://github.com/huguei/p5-Net-Address-IP-Cipher/issues>
302  
303 =item * CPAN Ratings
304  
305 L<http://cpanratings.perl.org/d/Net-Address-IP-Cipher>
306  
307 =item * Search CPAN
308  
309 L<http://search.cpan.org/dist/Net-Address-IP-Cipher/>
310  
311 =back
312  
313 =head1 REPOSITORY
314  
315 L<https://github.com/huguei/p5-Net-Address-IP-Cipher>
316  
317 =head1 ACKNOWLEDGEMENTS
318  
319 The v4 version is based on the original ipcrypt python version from
320 Jean-Philippe Aumasson:
321 L<https://github.com/veorq/ipcrypt>
322  
323 =head1 LICENSE AND COPYRIGHT
324  
325 Copyright 2020 Hugo Salgado.
326  
327 This program is free software; you can redistribute it and/or modify it
328 under the terms of the the Artistic License (2.0). You may obtain a
329 copy of the full license at:
330  
331 L<http://www.perlfoundation.org/artistic_license_2_0>
332  
333 Any use, modification, and distribution of the Standard or Modified
334 Versions is governed by this Artistic License. By using, modifying or
335 distributing the Package, you accept this license. Do not use, modify,
336 or distribute the Package, if you do not accept this license.
337  
338 If your Modified Version has been derived from a Modified Version made
339 by someone other than you, you are nevertheless required to ensure that
340 your Modified Version complies with the requirements of this license.
341  
342 This license does not grant you the right to use any trademark, service
343 mark, tradename, or logo of the Copyright Holder.
344  
345 This license includes the non-exclusive, worldwide, free-of-charge
346 patent license to make, have made, use, offer to sell, sell, import and
347 otherwise transfer the Package with respect to any patent claims
348 licensable by the Copyright Holder that are necessarily infringed by the
349 Package. If you institute patent litigation (including a cross-claim or
350 counterclaim) against any party alleging that the Package constitutes
351 direct or contributory patent infringement, then this Artistic License
352 to you shall terminate on the date that such litigation is filed.
353  
354 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
355 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
356 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
357 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
358 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
359 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
360 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
361 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
362  
363  
364 =cut
365  
366 4 1; # End of Net::Address::IP::Cipher
↑ Summary