| 1 | ##@file | ||||||||||||||||||||
| 2 | # Extend Crypt::Rijndael to get several keys from a single secret key, | ||||||||||||||||||||
| 3 | # add base64 encoding of binary data, and cipher hexadecimal data. | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | ##@class | ||||||||||||||||||||
| 6 | # Extend Crypt::Rijndael to get several keys from a single secret key, | ||||||||||||||||||||
| 7 | # add base64 encoding of binary data, and cipher hexadecimal data. | ||||||||||||||||||||
| 8 | # $Lemonldap::NG::Common::Crypto::msg contains Crypt::Rijndael errors. | ||||||||||||||||||||
| 9 | package Lemonldap::NG::Common::Crypto; | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 18 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 18 | ▶ | use Crypt::Rijndael; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 18 | ▶ | use MIME::Base64; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 18 | ▶ | use Digest::SHA; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 18 | ▶ | use bytes; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 18 | our $VERSION = '2.0.0'; | |||||||||||||||||||
| 18 | 18 | my ( $newIv, $randG, $hash ); | |||||||||||||||||||
| 19 | 18 | $hash = \&Digest::SHA::sha256; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 18 | ▶ | use constant HMAC_LENGTH => 32; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 18 | ▶ | use constant IV_LENGTH => 16; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Build initialization vector subroutine | ||||||||||||||||||||
| 25 | 18 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.8
| |||||||||||||||||||||
| 26 | 18 | eval { require Crypt::URandom; Crypt::URandom::urandom(IV_LENGTH) }; | |||||||||||||||||||
| 27 | 18 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 0 | ▶ | $newIv = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 29 | 0 | return bytes::substr( Digest::SHA::sha1( rand() . time . {} ), | |||||||||||||||||||
| 30 | 0, IV_LENGTH ); | ||||||||||||||||||||
| 31 | 0 | }; | |||||||||||||||||||
| 32 | 0 | ▶ | $randG = sub { return int( rand( $_[0] ) ) }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||
| 35 | 18 | ▶ | $newIv = sub { return Crypt::URandom::urandom(IV_LENGTH) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 1000 | ▶ | $randG = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 37 | return | ||||||||||||||||||||
| 38 | 1000 | int( unpack( "C", Crypt::URandom::urandom(1) ) * $_[0] / 256 ); | |||||||||||||||||||
| 39 | 18 | }; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 18 | our $msg; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | ## @cmethod Lemonldap::NG::Common::Crypto new(string key, string mode) | ||||||||||||||||||||
| 46 | # Constructor | ||||||||||||||||||||
| 47 | # @param key key defined in LL::NG conf | ||||||||||||||||||||
| 48 | # @param mode Crypt::Rijndael constant | ||||||||||||||||||||
| 49 | # @return Lemonldap::NG::Common::Crypto object | ||||||||||||||||||||
| 50 | 26 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 51 | 26 | my ( $class, $key, $mode ) = @_; | |||||||||||||||||||
| 52 | 26 | ▶ | $mode ||= Crypt::Rijndael::MODE_CBC(); | ||||||||||||||||||
|
Condition100%2/2
$mode ||= 2
MC/DC100%1/1
$mode ||= 2
$mode
Truth table100%2/2
A
A= $mode
| |||||||||||||||||||||
| 53 | 26 | my $self = { | |||||||||||||||||||
| 54 | key => $key, | ||||||||||||||||||||
| 55 | mode => $mode, | ||||||||||||||||||||
| 56 | ciphers => {} | ||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | 26 | return bless $self, $class; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | ## @method private Crypt::Rijndael _getCipher(string key) | ||||||||||||||||||||
| 62 | # Returns a Crypt::Rijndael object whose key is mainKey ^ secondKey, | ||||||||||||||||||||
| 63 | # where mainKey is defined in LL::NG conf, | ||||||||||||||||||||
| 64 | # and secondKey is set in code so as to get different keys | ||||||||||||||||||||
| 65 | # @param key that secondary key | ||||||||||||||||||||
| 66 | # @return Crypt::Rijndael object | ||||||||||||||||||||
| 67 | 43 | ▶ | sub _getCipher { | ||||||||||||||||||
|
Subroutine
_getCipher: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 68 | 43 | my ( $self, $key ) = @_; | |||||||||||||||||||
| 69 | 43 | ▶ | $key ||= ""; | ||||||||||||||||||
|
Condition100%2/2
$key ||= ""
MC/DC100%1/1
$key ||= ""
$key
Truth table100%2/2
A
A= $key
| |||||||||||||||||||||
| 70 | $self->{ciphers}->{$key} ||= | ||||||||||||||||||||
| 71 | 43 | ▶ | Crypt::Rijndael->new( $hash->( $self->{key}, $key ), $self->{mode} ); | ||||||||||||||||||
|
Condition66%2/3
$self->{"ciphers"}{$key} ||= "Crypt::Rijndael"->new(&$hash($self->{"key"}, $key), $self->{"mode"})
MC/DC0%0/2
$self->{"ciphers"}{$key} ||= "Crypt::Rijndael"->new(&$hash($self->{"key"}, $key), $self->{"mode"})
$self->{"ciphers"}{$key}
"Crypt::Rijndael"->new(&$hash($self->{"key"}, $key), $self->{"mode"})
Truth table66%2/3
A ||= B
A= $self->{"ciphers"}{$key}B= "Crypt::Rijndael"->new(&$hash($self->{"key"}, $key), $self->{"mode"})
| |||||||||||||||||||||
| 72 | 43 | return $self->{ciphers}->{$key}; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | ## @method string encrypt(string data) | ||||||||||||||||||||
| 76 | # Encrypt $data and return it in Base64 format | ||||||||||||||||||||
| 77 | # @param data data to encrypt | ||||||||||||||||||||
| 78 | # @return encrypted data in Base64 format | ||||||||||||||||||||
| 79 | 19 | ▶ | sub encrypt { | ||||||||||||||||||
|
Subroutine
encrypt: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||
| 80 | 19 | my ( $self, $data, $low ) = @_; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # pad $data so that its length be multiple of 16 bytes | ||||||||||||||||||||
| 83 | 19 | ▶ | $data //= ''; | ||||||||||||||||||
|
Condition50%1/2
$data //= ""
MC/DC0%0/1
$data //= ""
$data
Truth table50%1/2
A
A= $data
| |||||||||||||||||||||
| 84 | 19 | my $l = bytes::length($data) % 16; | |||||||||||||||||||
| 85 | 19 | ▶ | $data .= "\0" x ( 16 - $l ) unless ( $l == 0 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 19 | ▶ | my $iv = | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | $low | ||||||||||||||||||||
| 89 | ? bytes::substr( Digest::SHA::sha1( rand() . time . {} ), 0, IV_LENGTH ) | ||||||||||||||||||||
| 90 | : $newIv->(); | ||||||||||||||||||||
| 91 | 19 | my $hmac = $hash->($data); | |||||||||||||||||||
| 92 | 19 | eval { | |||||||||||||||||||
| 93 | 19 | $data = | |||||||||||||||||||
| 94 | encode_base64( | ||||||||||||||||||||
| 95 | $iv . $self->_getCipher->set_iv($iv)->encrypt( $hmac . $data ), | ||||||||||||||||||||
| 96 | '' ); | ||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 19 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 0 | $msg = "Crypt::Rijndael error : $@"; | |||||||||||||||||||
| 101 | 0 | return undef; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||
| 104 | 19 | $msg = ''; | |||||||||||||||||||
| 105 | 19 | chomp $data; | |||||||||||||||||||
| 106 | 19 | return $data; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | ## @method string decrypt(string data) | ||||||||||||||||||||
| 111 | # Decrypt $data and return it | ||||||||||||||||||||
| 112 | # @param data data to decrypt in Base64 format | ||||||||||||||||||||
| 113 | # @return decrypted data | ||||||||||||||||||||
| 114 | 22 | ▶ | sub decrypt { | ||||||||||||||||||
|
Subroutine
decrypt: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 115 | 22 | my ( $self, $data ) = @_; | |||||||||||||||||||
| 116 | 22 | $data =~ s/%2B/\+/ig; | |||||||||||||||||||
| 117 | 22 | $data =~ s/%2F/\//ig; | |||||||||||||||||||
| 118 | 22 | $data =~ s/%3D/=/ig; | |||||||||||||||||||
| 119 | 22 | $data =~ s/%0A/\n/ig; | |||||||||||||||||||
| 120 | 22 | $data = decode_base64($data); | |||||||||||||||||||
| 121 | 22 | my $iv; | |||||||||||||||||||
| 122 | 22 | $iv = bytes::substr( $data, 0, IV_LENGTH ); | |||||||||||||||||||
| 123 | 22 | $data = bytes::substr( $data, IV_LENGTH ); | |||||||||||||||||||
| 124 | 22 | eval { $data = $self->_getCipher->set_iv($iv)->decrypt($data); }; | |||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 22 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | 0 | $msg = "Crypt::Rijndael error : $@"; | |||||||||||||||||||
| 128 | 0 | return undef; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | 22 | my $hmac = bytes::substr( $data, 0, HMAC_LENGTH ); | |||||||||||||||||||
| 131 | 22 | $data = bytes::substr( $data, HMAC_LENGTH ); | |||||||||||||||||||
| 132 | 22 | ▶ | if ( $hash->($data) ne $hmac ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 1 | $msg = "Bad MAC"; | |||||||||||||||||||
| 134 | 1 | return undef; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||
| 137 | 21 | $msg = ''; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # Obscure Perl re bug... | ||||||||||||||||||||
| 140 | 21 | $data .= "\0"; | |||||||||||||||||||
| 141 | 21 | $data =~ s/\0*$//; | |||||||||||||||||||
| 142 | 21 | return $data; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | ## @method string encryptHex(string data, string key) | ||||||||||||||||||||
| 147 | # Encrypt $data and return it in hexadecimal format | ||||||||||||||||||||
| 148 | # Data must be hexadecimal and its length must be a multiple of 32 | ||||||||||||||||||||
| 149 | # the encrypted data have same length as the original data | ||||||||||||||||||||
| 150 | # @param data data to encrypt | ||||||||||||||||||||
| 151 | # @param key optional secondary key | ||||||||||||||||||||
| 152 | # @return encrypted data in hexadecimal data | ||||||||||||||||||||
| 153 | 1 | ▶ | sub encryptHex { | ||||||||||||||||||
|
Subroutine
encryptHex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 154 | 1 | my ( $self, $data, $key ) = @_; | |||||||||||||||||||
| 155 | 1 | return _cryptHex( $self, $data, $key, "encrypt" ); | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | ## @method string decryptHex(string data, string key) | ||||||||||||||||||||
| 159 | # Decrypt $data and return it in hexadecimal format | ||||||||||||||||||||
| 160 | # Data must be hexadecimal and its length must be a multiple of 32 | ||||||||||||||||||||
| 161 | # the decrypted data have same length as the encrypted data | ||||||||||||||||||||
| 162 | # @param data data to decrypt | ||||||||||||||||||||
| 163 | # @param key optional secondary key | ||||||||||||||||||||
| 164 | # @return decrypted data in hexadecimal data | ||||||||||||||||||||
| 165 | 1 | ▶ | sub decryptHex { | ||||||||||||||||||
|
Subroutine
decryptHex: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 166 | 1 | my ( $self, $data, $key ) = @_; | |||||||||||||||||||
| 167 | 1 | return _cryptHex( $self, $data, $key, "decrypt" ); | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | ## @method private string _cryptHex (string data, string key, string sub) | ||||||||||||||||||||
| 171 | # Auxiliary method to share code between encrypt and decrypt | ||||||||||||||||||||
| 172 | # @param data data to decrypt | ||||||||||||||||||||
| 173 | # @param key secondary key | ||||||||||||||||||||
| 174 | # @param sub may be "encrypt" or "decrypt" | ||||||||||||||||||||
| 175 | # @return decrypted data in hexadecimal data | ||||||||||||||||||||
| 176 | 2 | ▶ | sub _cryptHex { | ||||||||||||||||||
|
Subroutine
_cryptHex: called CC=7 SCAR=20.5
| |||||||||||||||||||||
| 177 | 2 | my ( $self, $data, $key, $sub ) = @_; | |||||||||||||||||||
| 178 | 2 | ▶ | unless ( $data =~ /^([0-9a-fA-F]{2})*$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 0 | $msg = | |||||||||||||||||||
| 180 | "Lemonldap::NG::Common::Crypto::${sub}Hex error : data is not hexadecimal"; | ||||||||||||||||||||
| 181 | 0 | return undef; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | # $data's length must be multiple of 32, | ||||||||||||||||||||
| 185 | # since Rijndael requires data length multiple of 16 | ||||||||||||||||||||
| 186 | 2 | ▶ | unless ( bytes::length($data) % 32 == 0 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | 0 | $msg = | |||||||||||||||||||
| 188 | "Lemonldap::NG::Common::Crypto::${sub}Hex error : data length must be multiple of 32"; | ||||||||||||||||||||
| 189 | 0 | return undef; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | 2 | my $iv; | |||||||||||||||||||
| 192 | 2 | ▶ | if ( $sub eq 'encrypt' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 1 | $iv = $newIv->(); | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | 2 | $data = pack "H*", $data; | |||||||||||||||||||
| 196 | 2 | ▶ | if ( $sub eq 'decrypt' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 197 | 1 | $iv = bytes::substr( $data, 0, IV_LENGTH ); | |||||||||||||||||||
| 198 | 1 | $data = bytes::substr( $data, IV_LENGTH ); | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | 2 | eval { $data = $self->_getCipher($key)->set_iv($iv)->$sub($data); }; | |||||||||||||||||||
| 201 | 2 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 202 | 0 | $msg = "Crypt::Rijndael error : $@"; | |||||||||||||||||||
| 203 | 0 | return undef; | |||||||||||||||||||
| 204 | } | ||||||||||||||||||||
| 205 | 2 | ▶ | if ( $sub eq 'encrypt' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 206 | 1 | $data = $iv . $data; | |||||||||||||||||||
| 207 | } | ||||||||||||||||||||
| 208 | 2 | $msg = ""; | |||||||||||||||||||
| 209 | 2 | $data = unpack "H*", $data; | |||||||||||||||||||
| 210 | 2 | return $data; | |||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 1 | ▶ | sub srandom { | ||||||||||||||||||
|
Subroutine
srandom: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 214 | 1 | eval { require String::Random }; | |||||||||||||||||||
| 215 | 1 | ▶ | if ($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | 0 | die 'Missing recommended dependency to String::Random'; | |||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | 1 | return String::Random->new( rand_gen => $randG ); | |||||||||||||||||||
| 219 | } | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 18 | 1; | |||||||||||||||||||