Algorithm-IRCSRP2
view release on metacpan or search on metacpan
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
'isa' => 'Str',
'is' => 'rw',
);
has 'nickname' => (
'isa' => 'Str',
'is' => 'rw',
'default' => 'unknown'
);
has 'debug_cb' => (
'isa' => 'CodeRef',
'is' => 'rw',
'default' => sub {
sub {
my @args = @_;
@args = grep { defined($_) } @args;
print(@args);
}
}
);
has '_orig_debug_cb' => (
'isa' => 'CodeRef',
'is' => 'rw',
'default' => sub {
sub {
}
}
);
has 'am_i_dave' => (
'isa' => 'Bool',
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
has 'cbc_blocksize' => (
'isa' => 'Int',
'is' => 'ro',
'default' => 16
);
# -------- methods --------
sub BUILD {
my ($self) = @_;
my $orig_cb = $self->debug_cb;
$self->_orig_debug_cb($orig_cb);
my $new_cb = sub {
my $str = join('', @_);
$str = (($self->am_i_dave) ? 'Dave: ' : 'Alice: ') . $self->nickname . ' ' . $str;
return $orig_cb->($str);
};
$self->debug_cb($new_cb);
return;
}
sub init {
my ($self) = @_;
my $s = urandom(32);
my $x = bytes2int(H($s . $self->I() . $self->P()));
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
$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;
}
$self->debug_cb->("decrypt_message: from $username ; msg $msg");
return $msg;
}
sub encrypt_message {
my ($self, $who, $msg) = @_;
my $times = pack('L>', int(time()));
# info = len(username) || username || timestamp
lib/Algorithm/IRCSRP2.pm view on Meta::CPAN
=head1 ATTRIBUTES
=head2 Optional Attributes
=over
=item * B<am_i_dave> (ro, Bool) - Child class will set this.
=item * B<cbc_blocksize> (ro, Int) - CBC blocksize. Defaults to '16'.
=item * B<debug_cb> (rw, CodeRef) - Debug callback. Defaults to C<print()>
=item * B<error> (rw, Str) - If set, there was an error.
=item * B<nickname> (rw, Str) - Child class will set this. Defaults to 'unknown'.
=back
=head1 PUBLIC API METHODS
=over
lib/Algorithm/IRCSRP2/Alice.pm view on Meta::CPAN
has '+am_i_dave' => ('default' => 0, 'is' => 'ro');
has 'state' => (
'isa' => enum([qw(null error init srpa0 srpa1 srpa2 srpa3 authenticated)]),
'is' => 'rw',
'default' => 'null',
'trigger' => sub {
my ($self, $new, $old) = @_;
$self->debug_cb->("State change $old -> $new");
if ($new eq 'error') {
$self->debug_cb->('Fatal error: ', $self->error);
}
}
);
sub srpa0 {
my ($self) = @_;
$self->state('srpa0');
return '+srpa0 ' . $self->I();
lib/Algorithm/IRCSRP2/Alice.pm view on Meta::CPAN
$t = $q->copy;
my $t2 = $u->copy;
$t2->bmul($x->bstr);
$t2->badd($a->bstr);
$t2->bmod(N());
my $S = $t->copy;
$S->bmodpow($t2->bstr, N());
$self->debug_cb->('h' x 20 . $S->bstr);
$self->S($S);
# K1 = H(S || "enc")
my $K1 = Digest::SHA::sha256(int2bytes($S) . 'enc');
$self->K1($K1);
# K2 = H(S || "auth")
my $K2 = Digest::SHA::sha256(int2bytes($S) . 'auth');
$self->K2($K2);
lib/Algorithm/IRCSRP2/Alice.pm view on Meta::CPAN
$self->state('srpa3');
$self->cipher(Crypt::OpenSSL::AES->new($self->K1()));
my $plain = $self->cbc_decrypt(substr($cipher, 16));
my $sessionkey = substr($plain, 0, 32);
my $mackey = substr($plain, 32, 32);
my $M2 = substr($plain, 64, 32);
$self->debug_cb->('sessionkey ' . bytes2int($sessionkey));
$self->debug_cb->('mackey ' . bytes2int($mackey));
my $M2ver = H(join('', int2bytes($self->A), $self->M1, int2bytes($self->S)));
$self->debug_cb->('M2 ' . bytes2int($M2));
$self->debug_cb->('M2ver ' . bytes2int($M2ver));
if ($M2 ne $M2ver) {
$self->error('M2 != M2ver');
$self->state('error');
}
$self->session_key($sessionkey);
$self->cipher(Crypt::OpenSSL::AES->new($sessionkey));
$self->mac_key($mackey);
lib/Algorithm/IRCSRP2/Exchange.pm view on Meta::CPAN
if ($string =~ /[[:^ascii:]]/) {
$retstr = Algorithm::IRCSRP2::bytes2int($string);
}
else {
$retstr = $string;
}
}
return $retstr;
};
$self->debug_cb->(sprintf($str, $formatstr->($old), $formatstr->($new)));
}
);
}
has 'cipher' => (
'isa' => 'Crypt::OpenSSL::AES',
'is' => 'rw',
);
has 'session_key' => (
( run in 0.346 second using v1.01-cache-2.11-cpan-87723dcf8b7 )