Algorithm-IRCSRP2
view release on metacpan or search on metacpan
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
$self->v(Math::BigInt->new(g())->copy->bmodpow($x->bstr, N()));
return $self->state('init');
}
sub cbc_decrypt {
my ($self, $data) = @_;
my $blocksize = $self->cbc_blocksize();
die('length($data) % $blocksize != 0') unless (length($data) % $blocksize == 0);
my $IV = substr($data, 0, $blocksize);
$data = substr($data, $blocksize);
my $plaintext = '';
foreach (@{[ 0 .. (length($data) / $blocksize) - 1 ]}) {
my $temp = $self->cipher->decrypt(substr($data, 0, $blocksize));
my $temp2 = xorstring($temp, $IV, $blocksize);
$plaintext .= $temp2;
$IV = substr($data, 0, $blocksize);
$data = substr($data, $blocksize);
}
return $plaintext;
}
sub cbc_encrypt {
my ($self, $data) = @_;
my $blocksize = $self->cbc_blocksize();
die('length($data) % $blocksize != 0') unless (length($data) % $blocksize == 0);
my $IV = urandom($blocksize);
die('len(IV) == blocksize') unless (length($IV) == $blocksize);
my $ciphertext = $IV;
foreach (@{[ 0 .. (length($data) / $blocksize) - 1 ]}) {
my $xored = xorstring($data, $IV, $blocksize);
my $enc = $self->cipher->encrypt($xored);
$ciphertext .= $enc;
$IV = $enc;
$data = substr($data, $blocksize);
}
die('len(ciphertext) % blocksize == 0') unless (length($ciphertext) % $blocksize == 0);
return $ciphertext;
}
sub decrypt_message {
my ($self, $msg) = @_;
substr($msg, 0, 1, '');
my $raw = MIME::Base64::decode_base64($msg);
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
my $usernamelen = ord(substr($plain, 1, 2));
my $username = substr($plain, 2, $usernamelen);
$msg = substr($plain, 4 + 2 + $usernamelen);
if ($msg =~ /^\xffKEY/) {
my $new = substr($msg, 4);
if (length($new) != (32 + 32)) {
die('decrypt_message: length($new) != 32 + 32 ; length is ' . length($new));
}
$self->debug_cb->('decrypt_message: rekeying');
$self->session_key(substr($new, 0, 32));
$self->mac_key(substr($new, 32, 32));
$self->cipher(Crypt::OpenSSL::AES->new($self->session_key));
return;
}
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
return $msg;
}
sub encrypt_message {
my ($self, $who, $msg) = @_;
my $times = pack('L>', int(time()));
# info = len(username) || username || timestamp
my $infos = chr(length($who)) . $who . $times;
# ctext = IV || AES-CBC(sessionkey, IV, "M" || info || plaintext)
my $ctext = $self->cbc_encrypt(padto('M' . $infos . $msg, 16));
# cmac = HM(mackey, ctext)
my $cmac = hmac_sha256_128($self->mac_key, $ctext);
# ircmessage = "*" || Base64(cmac || ctext)
return '*' . MIME::Base64::encode_base64($cmac . $ctext, '');
}
lib/Algorithm/IRCSRP2/Utils.pm view on Meta::CPAN
my @bs = split('', $b);
foreach my $i (@{[ 0 .. $blocksize - 1 ]}) {
$xored .= chr(ord($as[$i]) ^ ord($bs[$i]));
}
return $xored;
}
sub padto {
my ($msg, $length) = @_;
my $L = length($msg);
if ($L % $length) {
$msg .= (chr(0) x ($length - $L % $length));
}
die('lenth($msg) % $length != 0') unless ((length($msg) % $length) == 0);
return $msg;
}
sub hmac_sha256_128 {
my ($key, $data) = @_;
my $str = Digest::SHA::hmac_sha256($data, $key);
$str = substr($str, 0, 16);
( run in 0.496 second using v1.01-cache-2.11-cpan-65fba6d93b7 )