blib/lib/Crypt/XTEA/LE.pm

filter: Statementstmt 91.9% 80 / 87 Branchbran 40.6% 13 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 93.3% 14 / 15 Podpod 50.0% 3 / 6 totaltotal 74.6% 112 / 150 CC 19 SCAR 24.2
CC
19
Cov
76%
new
29.7
key_setup
3.1
encrypt_block
3.0
CRAP 11.3
↑ Summary LE.xs »
1 package Crypt::XTEA::LE;
2  
3 # ABSTRACT: Implementation of the eXtended Tiny Encryption Algorithm
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Scalar::Util::Numeric qw(isint);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $VERSION = '0.0107'; # VERSION
14  
15 1 require XSLoader;
16 1 XSLoader::load('Crypt::XTEA::LE', $VERSION);
17  
18  
19 1 my $ROUNDS = 32;
20 1 my $KEY_SIZE = 16;
21 1 my $ELEMENTS_IN_KEY = $KEY_SIZE / 4;
22 1 my $BLOCK_SIZE = 8;
23 1 my $ELEMENTS_IN_BLOCK = $BLOCK_SIZE / 4;
24  
25  
26 1 ▶use constant keysize => $KEY_SIZE;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 1 ▶use constant blocksize => $BLOCK_SIZE;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 2 ▶sub new {
Subroutine new: called CC=11 SCAR=33.9
Pod documented
33 2 my $class = shift;
34 2 my $key = shift;
35 2 ▶ my $rounds = shift // $ROUNDS;
Condition33%1/3
shift() // $ROUNDS
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() // $ROUNDS
shift() $ROUNDS
Truth table33%1/3
A // B
A= shift()B= $ROUNDS
ABresult
1X1
011
000
36 2 my %opts = (little_endian => 0, @_);
37 2 my $xtea_key;
38 2 ▶ croak( 'key is required' ) if not defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 2 2
39 2 ▶ if ( my $ref_of_key = ref( $key ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_of_key = ref $key) { } 0 2 2
40 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';
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_of_key eq "ARRAY" 0 0 0
41 0 ▶ croak( sprintf( 'key must has %d elements if key is a reference of ARRAY', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key } ) != $ELEMENTS_IN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$key;} != $ELEMENTS_IN_KEY 0 0 0
42 0 ▶ croak( 'each element of key must be a 32bit Integer if key is a reference of ARRAY' ) if not all { isint( $_ ) != 0 } @{ $key };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { &isint($_) != 0; } , @{$key;}) 0 0 0
43 0 $xtea_key = $key;
44 } else {
45 2 ▶ croak( sprintf( 'key must be a %d-byte-long STRING or a reference of ARRAY', $KEY_SIZE ) ) if length $key != $KEY_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $key != $KEY_SIZE 0 2 2
46 2 $xtea_key = key_setup($key, $opts{little_endian});
47 }
48 2 ▶ croak( 'rounds must be a positive NUMBER' ) if isint( $rounds ) != 1;
Branch: 1 branch
BranchTrueFalseTotal
if &isint($rounds) != 1 0 2 2
49 my $self = {
50 key => $xtea_key,
51 rounds => $rounds,
52 2 ▶ endianness => $opts{little_endian} ? 'V*' : 'N*',
Branch: 1 branch
BranchTrueFalseTotal
$opts{'little_endian'} ? : 0 2 2
53 };
54 2 ▶ bless $self, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
55 }
56  
57  
58 2 ▶sub encrypt {
Subroutine encrypt: called CC=2 SCAR=7.0
Pod documented
59 2 my $self = shift;
60 2 my $plain_text = shift;
61 2 ▶ croak( sprintf( 'plain_text block size must be %d bytes', $BLOCK_SIZE) ) if length($plain_text) != $BLOCK_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $plain_text != $BLOCK_SIZE 0 2 2
62 2 my @block = unpack $self->{endianness}, $plain_text;
63 2 my $cipher_text_ref = $self->encrypt_block( \@block );
64 2 return pack( $self->{endianness}, @{$cipher_text_ref} );
65 }
66  
67  
68 2 ▶sub decrypt {
Subroutine decrypt: called CC=2 SCAR=7.0
Pod documented
69 2 my $self = shift;
70 2 my $cipher_text = shift;
71 2 ▶ croak( sprintf( 'cipher_text size must be %d bytes', $BLOCK_SIZE) ) if length($cipher_text) != $BLOCK_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $cipher_text != $BLOCK_SIZE 0 2 2
72 2 my @block = unpack $self->{endianness}, $cipher_text;
73 2 my $plain_text_ref = $self->decrypt_block( \@block );
74 2 return pack( $self->{endianness}, @{$plain_text_ref} );
75 }
76  
77 2 ▶sub encrypt_block {
Subroutine encrypt_block: called CC=3 SCAR=11.1
Pod undocumented
78 2 my $self = shift;
79 2 my $block_ref = shift;
80 2 my $key_ref = $self->{key};
81  
82 2 ▶ croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$block_ref;} != $ELEMENTS_IN_BLOCK 0 2 2
83 2 ▶ croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$key_ref;} != $ELEMENTS_IN_KEY 0 2 2
84  
85 2 return $self->encrypt_block_in_c( $block_ref );
86 }
87  
88 2 ▶sub decrypt_block {
Subroutine decrypt_block: called CC=3 SCAR=11.1
Pod undocumented
89 2 my $self = shift;
90 2 my $block_ref = shift;
91 2 my $key_ref = $self->{key};
92  
93 2 ▶ croak( sprintf( 'block must has %d elements', $ELEMENTS_IN_BLOCK ) ) if scalar( @{ $block_ref } ) != $ELEMENTS_IN_BLOCK;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$block_ref;} != $ELEMENTS_IN_BLOCK 0 2 2
94 2 ▶ croak( sprintf( 'key must has %d elements', $ELEMENTS_IN_KEY ) ) if scalar( @{ $key_ref } ) != $ELEMENTS_IN_KEY;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$key_ref;} != $ELEMENTS_IN_KEY 0 2 2
95  
96 2 return $self->decrypt_block_in_c( $block_ref );
97 }
98  
99 2 ▶sub key_setup {
Subroutine key_setup: called CC=3 SCAR=11.3
Pod undocumented
100 2 my $key_str = shift;
101 2 ▶ my $endianness = (shift) ? 'V*' : 'N*';
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 0 2 2
102 2 ▶ croak( sprintf( 'key must be %s bytes long', $KEY_SIZE ) ) if length( $key_str ) != $KEY_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if length $key_str != $KEY_SIZE 0 2 2
103 2 my @xtea_key = unpack "$endianness", $key_str;
104 2 return \@xtea_key;
105 }
106  
107  
108 1 1;
109  
110 __END__
↑ Summary LE.xs »