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