| 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 |
use strict; |
| 12 |
use Crypt::Rijndael; |
| 13 |
use MIME::Base64; |
| 14 |
use Digest::SHA; |
| 15 |
use bytes; |
| 16 |
|
| 17 |
our $VERSION = '2.0.0'; |
| 18 |
my ( $newIv, $randG, $hash ); |
| 19 |
$hash = \&Digest::SHA::sha256; |
| 20 |
|
| 21 |
use constant HMAC_LENGTH => 32; |
| 22 |
use constant IV_LENGTH => 16; |
| 23 |
|
| 24 |
# Build initialization vector subroutine |
| 25 |
BEGIN { |
| 26 |
eval { require Crypt::URandom; Crypt::URandom::urandom(IV_LENGTH) }; |
| 27 |
if ($@) { |
| 28 |
$newIv = sub { |
| 29 |
return bytes::substr( Digest::SHA::sha1( rand() . time . {} ), |
| 30 |
0, IV_LENGTH ); |
| 31 |
}; |
| 32 |
$randG = sub { return int( rand( $_[0] ) ) }; |
| 33 |
} |
| 34 |
else { |
| 35 |
$newIv = sub { return Crypt::URandom::urandom(IV_LENGTH) }; |
| 36 |
$randG = sub { |
| 37 |
return |
| 38 |
int( unpack( "C", Crypt::URandom::urandom(1) ) * $_[0] / 256 ); |
| 39 |
}; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
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 |
sub new { |
| 51 |
my ( $class, $key, $mode ) = @_; |
| 52 |
$mode ||= Crypt::Rijndael::MODE_CBC(); |
| 53 |
my $self = { |
| 54 |
key => $key, |
| 55 |
mode => $mode, |
| 56 |
ciphers => {} |
| 57 |
}; |
| 58 |
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 |
sub _getCipher { |
| 68 |
my ( $self, $key ) = @_; |
| 69 |
$key ||= ""; |
| 70 |
$self->{ciphers}->{$key} ||= |
| 71 |
Crypt::Rijndael->new( $hash->( $self->{key}, $key ), $self->{mode} ); |
| 72 |
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 |
sub encrypt { |
| 80 |
my ( $self, $data, $low ) = @_; |
| 81 |
|
| 82 |
# pad $data so that its length be multiple of 16 bytes |
| 83 |
$data //= ''; |
| 84 |
my $l = bytes::length($data) % 16; |
| 85 |
$data .= "\0" x ( 16 - $l ) unless ( $l == 0 ); |
| 86 |
|
| 87 |
my $iv = |
| 88 |
$low |
| 89 |
? bytes::substr( Digest::SHA::sha1( rand() . time . {} ), 0, IV_LENGTH ) |
| 90 |
: $newIv->(); |
| 91 |
my $hmac = $hash->($data); |
| 92 |
eval { |
| 93 |
$data = |
| 94 |
encode_base64( |
| 95 |
$iv . $self->_getCipher->set_iv($iv)->encrypt( $hmac . $data ), |
| 96 |
'' ); |
| 97 |
}; |
| 98 |
|
| 99 |
if ($@) { |
| 100 |
$msg = "Crypt::Rijndael error : $@"; |
| 101 |
return undef; |
| 102 |
} |
| 103 |
else { |
| 104 |
$msg = ''; |
| 105 |
chomp $data; |
| 106 |
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 |
sub decrypt { |
| 115 |
my ( $self, $data ) = @_; |
| 116 |
$data =~ s/%2B/\+/ig; |
| 117 |
$data =~ s/%2F/\//ig; |
| 118 |
$data =~ s/%3D/=/ig; |
| 119 |
$data =~ s/%0A/\n/ig; |
| 120 |
$data = decode_base64($data); |
| 121 |
my $iv; |
| 122 |
$iv = bytes::substr( $data, 0, IV_LENGTH ); |
| 123 |
$data = bytes::substr( $data, IV_LENGTH ); |
| 124 |
eval { $data = $self->_getCipher->set_iv($iv)->decrypt($data); }; |
| 125 |
|
| 126 |
if ($@) { |
| 127 |
$msg = "Crypt::Rijndael error : $@"; |
| 128 |
return undef; |
| 129 |
} |
| 130 |
my $hmac = bytes::substr( $data, 0, HMAC_LENGTH ); |
| 131 |
$data = bytes::substr( $data, HMAC_LENGTH ); |
| 132 |
if ( $hash->($data) ne $hmac ) { |
| 133 |
$msg = "Bad MAC"; |
| 134 |
return undef; |
| 135 |
} |
| 136 |
else { |
| 137 |
$msg = ''; |
| 138 |
|
| 139 |
# Obscure Perl re bug... |
| 140 |
$data .= "\0"; |
| 141 |
$data =~ s/\0*$//; |
| 142 |
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 |
sub encryptHex { |
| 154 |
my ( $self, $data, $key ) = @_; |
| 155 |
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 |
sub decryptHex { |
| 166 |
my ( $self, $data, $key ) = @_; |
| 167 |
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 |
sub _cryptHex { |
| 177 |
my ( $self, $data, $key, $sub ) = @_; |
| 178 |
unless ( $data =~ /^([0-9a-fA-F]{2})*$/ ) { |
| 179 |
$msg = |
| 180 |
"Lemonldap::NG::Common::Crypto::${sub}Hex error : data is not hexadecimal"; |
| 181 |
return undef; |
| 182 |
} |
| 183 |
|
| 184 |
# $data's length must be multiple of 32, |
| 185 |
# since Rijndael requires data length multiple of 16 |
| 186 |
unless ( bytes::length($data) % 32 == 0 ) { |
| 187 |
$msg = |
| 188 |
"Lemonldap::NG::Common::Crypto::${sub}Hex error : data length must be multiple of 32"; |
| 189 |
return undef; |
| 190 |
} |
| 191 |
my $iv; |
| 192 |
if ( $sub eq 'encrypt' ) { |
| 193 |
$iv = $newIv->(); |
| 194 |
} |
| 195 |
$data = pack "H*", $data; |
| 196 |
if ( $sub eq 'decrypt' ) { |
| 197 |
$iv = bytes::substr( $data, 0, IV_LENGTH ); |
| 198 |
$data = bytes::substr( $data, IV_LENGTH ); |
| 199 |
} |
| 200 |
eval { $data = $self->_getCipher($key)->set_iv($iv)->$sub($data); }; |
| 201 |
if ($@) { |
| 202 |
$msg = "Crypt::Rijndael error : $@"; |
| 203 |
return undef; |
| 204 |
} |
| 205 |
if ( $sub eq 'encrypt' ) { |
| 206 |
$data = $iv . $data; |
| 207 |
} |
| 208 |
$msg = ""; |
| 209 |
$data = unpack "H*", $data; |
| 210 |
return $data; |
| 211 |
} |
| 212 |
|
| 213 |
sub srandom { |
| 214 |
eval { require String::Random }; |
| 215 |
if ($@) { |
| 216 |
die 'Missing recommended dependency to String::Random'; |
| 217 |
} |
| 218 |
return String::Random->new( rand_gen => $randG ); |
| 219 |
} |
| 220 |
|
| 221 |
1; |