| 1 | package Crypt::Curve25519; | ||||||||||
| 2 | #ABSTRACT: Generate shared secret using elliptic-curve Diffie-Hellman function | ||||||||||
| 3 | |||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | require Exporter; | |||||||||
| 9 | 6 | our @ISA = qw(Exporter); | |||||||||
| 10 | 6 | our $VERSION = '0.08'; | |||||||||
| 11 | |||||||||||
| 12 | 6 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||
| 13 | curve25519 | ||||||||||
| 14 | curve25519_secret_key | ||||||||||
| 15 | curve25519_public_key | ||||||||||
| 16 | curve25519_shared_secret | ||||||||||
| 17 | ) ] ); | ||||||||||
| 18 | |||||||||||
| 19 | 6 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 20 | |||||||||||
| 21 | 6 | our @EXPORT = qw( | |||||||||
| 22 | curve25519_secret_key | ||||||||||
| 23 | curve25519_public_key | ||||||||||
| 24 | curve25519_shared_secret | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | # Although curve25519_donna is also clamping the secret key this function | ||||||||||
| 28 | # has been provided for completeness and to ensure that secret keys generated | ||||||||||
| 29 | # here can be used in other implementations of the algorithm. | ||||||||||
| 30 | 11 | ▶ | sub curve25519_secret_key { | ||||||||
|
Subroutine
curve25519_secret_key: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 31 | 11 | my $value = shift; | |||||||||
| 32 | 11 | ▶ | croak 'Secret key requires 32 bytes' if length($value) != 32; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 10 | vec($value, 0 , 8) &= 248; | |||||||||
| 34 | 10 | vec($value, 31, 8) &= 127; | |||||||||
| 35 | 10 | vec($value, 31, 8) |= 64; | |||||||||
| 36 | 10 | return $value; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 6 | require XSLoader; | |||||||||
| 40 | XSLoader::load('Crypt::Curve25519', $Crypt::Curve25519::{VERSION} ? | ||||||||||
| 41 | 6 | ▶ | ${ $Crypt::Curve25519::{VERSION} } : () | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | ); | ||||||||||
| 43 | |||||||||||
| 44 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||
| 45 | 2 | ▶ | return bless(\(my $o = 1), ref $_[0] ? ref $_[0] : $_[0] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 4 | ▶ | sub secret_key { | ||||||||
|
Subroutine
secret_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | 4 | my ($self, $psk) = (shift, shift); | |||||||||
| 50 | |||||||||||
| 51 | 4 | my $masked = curve25519_secret_key( pack('H64', $psk) ); | |||||||||
| 52 | |||||||||||
| 53 | 4 | return unpack('H64', $masked); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 4 | ▶ | sub public_key { | ||||||||
|
Subroutine
public_key: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 57 | 4 | my ($self, $sk) = (shift, shift); | |||||||||
| 58 | 4 | my @args = pack('H64', $sk); | |||||||||
| 59 | 4 | ▶ | if ( @_ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | push @args, pack('H64', shift); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 4 | my $pk = unpack('H64', curve25519_public_key( @args )); | |||||||||
| 64 | |||||||||||
| 65 | 4 | return $pk; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 4 | ▶ | sub shared_secret { | ||||||||
|
Subroutine
shared_secret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 4 | my ($self, $sk, $pk) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 4 | return unpack('H64', curve25519_shared_secret( pack('H64', $sk), pack('H64', $pk) )); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | sub generate { | ||||||||
|
Subroutine
generate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 75 | 0 | my ($self, $sk, $bp) = @_; | |||||||||
| 76 | |||||||||||
| 77 | 0 | return unpack('H64', curve25519( pack('H64', $sk), pack('H64', $bp) )); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 6 | 1; | |||||||||
| 81 | |||||||||||
| 82 | __END__ | ||||||||||