The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Crypt::NaCl::Sodium::scalarmult - Diffie-Hellman (Curve25519)

VERSION

version 1.0.8.0

SYNOPSIS

    use Crypt::NaCl::Sodium qw( :utils );

    my $crypto_scalarmult = Crypt::NaCl::Sodium->scalarmult();

    my ($a_skey, $a_pkey, $a_shared_key, $a_key
        $b_skey, $b_pkey, $b_shared_key, $b_key);

    ## Alice
    ########

    # Alice generates secret key
    $a_skey = $crypto_scalarmult->keygen();

    # and computes the public key
    $a_pkey = $crypto_scalarmult->base( $a_skey );

    # ... and shares it with Bob
    send_to( Bob => { public_key => $a_pkey } );

    ## Bob
    ########

    # Bob generates his secret key
    $b_skey = $crypto_scalarmult->keygen();

    # and computes the public key
    $b_pkey = $crypto_scalarmult->base( $b_skey );

    # ... and shares his public key with Alice
    send_to( Alice => { public_key => $b_pkey } );

    # Bob receives the public key from Alice
    $a_key = receive_for( Bob => 'public_key' );

    # Bob can now calculate the shared secret key
    $b_shared_key = $crypto_scalarmult->shared_secret( $b_skey, $a_key );

    ## Alice
    ########

    # Alice receives the public key from Bob
    $b_key = receive_for( Alice => 'public_key' );

    # and can now also calculate the shared secret key
    $a_shared_key = $crypto_scalarmult->shared_secret( $a_skey, $b_key );

    # shared keys calculated by Alice and Bob are equal
    $a_shared_key eq $b_shared_key or die;

    # NOTE: send_to() and receive_for() and user functions providing transport of
    # messages

DESCRIPTION

crypto_scalarmult provides Curve25519, a state-of-the-art Diffie-Hellman function suitable for a wide variety of applications, eg. authenticate and encrypt messages between both parties.

METHODS

keygen

    my $secret_key = $crypto_scalarmult->keygen();

Helper method to generate a random secret key to be used by $crypto_scalarmult.

The length of the $secret_key equals "SCALARBYTES".

The same secret key can be used with multiple other users, without the need of generating a distinct set of keys.

NOTE: keep the secret key confidential.

Returns Data::BytesLocker objects.

base

    my $public_key = $crypto_scalarmult->base( $secret_key );

Computes the public key for corresponding secret key.

The length of the $public_key equals "BYTES".

Returns Data::BytesLocker objects.

shared_secret

    my $shared_key = $crypto_scalarmult->shared_secret( $sender_secret_key, $recipient_public_key );

Given a user's secret key $sender_secret_key and another user's public key $recipient_public_key, computes a secret shared by the two users and returns it. This secret can then be used to authenticate and encrypt messages between the two users.

The length of the $shared_key equals "BYTES".

Returns Data::BytesLocker objects.

CONSTANTS

BYTES and SCALARBYTES are provided for consistency, but it is safe to assume that BYTES == SCALARBYTES.

BYTES

    my $pkey_length = $crypto_scalarmult->BYTES;

Returns the length of public key (and shared secret).

SCALARBYTES

    my $skey_length = $crypto_scalarmult->SECRETKEYBYTES;

Returns the length of secret key.

ALGORITHM DETAILS

crypto_scalarmult uses Curve25519.

SEE ALSO

AUTHOR

Alex J. G. Burzyński <ajgb@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Alex J. G. Burzyński <ajgb@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.