1Crypt::Curve25519(3) User Contributed Perl Documentation Crypt::Curve25519(3)
2
3
4
6 Crypt::Curve25519 - Generate shared secret using elliptic-curve
7 Diffie-Hellman function
8
10 version 0.06
11
13 use Crypt::Curve25519;
14
15 # Alice:
16 my $alice_secret_key = curve25519_secret_key(random_32_bytes());
17 my $alice_public_key = curve25519_public_key( $alice_secret_key );
18
19 # Bob:
20 my $bob_secret_key = curve25519_secret_key(random_32_bytes());
21 my $bob_public_key = curve25519_public_key( $bob_secret_key );
22
23 # Alice and Bob exchange their public keys
24 my $alice_public_key_hex = unpack('H64', $alice_public_key);
25 my $bob_public_key_hex = unpack('H64', $bob_public_key);
26
27 # Alice calculates shared secret to communicate with Bob
28 my $shared_secret_with_bob = curve25519_shared_secret(
29 $alice_secret_key,
30 pack('H64', $bob_public_key_hex)
31 );
32
33 # Bob calculates shared secret to communicate with Alice
34 my $shared_secret_with_alice = curve25519_shared_secret(
35 $bob_secret_key,
36 pack('H64', $alice_public_key_hex)
37 );
38
39 # Shared secrets are equal
40 die "Something horrible has happend!"
41 unless $shared_secret_with_bob eq $shared_secret_with_alice;
42
43 This package provides also simplified OO interface:
44
45 use Crypt::Curve25519 ();
46
47 my $c = Crypt::Curve25519->new();
48
49 # Alice:
50 my $alice_secret_key_hex = $c->secret_key(random_hexencoded_32_bytes());
51 my $alice_public_key_hex = $c->public_key( $alice_secret_key_hex );
52
53 # Bob:
54 my $bob_secret_key_hex = $c->secret_key(random_hexencoded_32_bytes());
55 my $bob_public_key_hex = $c->public_key( $bob_secret_key_hex );
56
57 # Alice and Bob exchange their public keys
58
59 # Alice calculates shared secret to communicate with Bob
60 my $shared_secret_with_bob_hex = $c->shared_secret(
61 $alice_secret_key_hex,
62 $bob_public_key_hex);
63
64 # Bob calculates shared secret to communicate with Alice
65 my $shared_secret_with_alice_hex = $c->shared_secret(
66 $bob_secret_key_hex,
67 $alice_public_key_hex);
68
69 # Shared secrets are equal
70 die "Something horrible has happend!"
71 unless $shared_secret_with_bob_hex eq $shared_secret_with_alice_hex;
72
73 Example functions to generate pseudo-random private secret key:
74
75 sub random_32_bytes {
76 return join('', map { chr(int(rand(255))) } 1 .. 32);
77 }
78
79 sub random_hexencoded_32_bytes {
80 return unpack('H64', random_32_bytes());
81 }
82
84 Curve25519 is a state-of-the-art Diffie-Hellman function suitable for a
85 wide variety of applications.
86
87 Given a user's 32-byte secret key, Curve25519 computes the user's
88 32-byte public key. Given the user's 32-byte secret key and another
89 user's 32-byte public key, Curve25519 computes a 32-byte secret shared
90 by the two users. This secret can then be used to authenticate and
91 encrypt messages between the two users.
92
94 new
95 my $c = Crypt::Curve25519->new();
96
97 Create a new object
98
99 secret_key
100 my $my_secret_key_hex = $c->secret_key( $my_random_32byte_string_hex );
101
102 Using hex encoded 32-byte random string from cryptographically safe
103 source create masked secret key.
104
105 public_key
106 my $public_key_hex = $c->public_key( $my_secret_key_hex );
107
108 Using hex encoded masked secret key generate corresponding hex encoded
109 32-byte Curve25519 public key.
110
111 shared_secret
112 my $shared_secret_hex = $c->shared_secret(
113 $my_secret_key_hex, $his_public_key_hex
114 );
115
116 Using provided hex encoded keys generate 32-byte hex encoded shared
117 secret, that both parties can use without disclosing their private
118 secret keys.
119
120 generate
121 Access to primitive method is also provided.
122
123 my $key_hex = $c->generate($my_secret_key_hex, $basepoint_hex);
124
125 # public key
126 if ( $basepoint_hex eq unpack("H64", pack("H64", "09")) ) {
127 print "\$key_hex is a public key\n";
128 }
129 elsif ( $basepoint_hex eq $his_public_key_hex ) {
130 print "\$key_hex is a shared secret\n";
131 }
132
133 Using provided hex encoded secret key and depending on the 32-byte hex
134 encoded basepoint generate 32-byte hex encoded public key or shared
135 secret.
136
138 curve25519_secret_key
139 my $my_secret_key = curve25519_secret_key($my_random_32byte_string);
140
141 Using provided 32-byte random string from cryptographically safe source
142 create masked secret key.
143
144 curve25519_public_key
145 my $public_key = curve25519_public_key($my_secret_key);
146
147 Using masked secret key generate corresponding 32-byte Curve25519
148 public key.
149
150 curve25519_shared_secret
151 my $shared_secret = curve25519_shared_secret(
152 $my_secret_key, $his_public_key
153 );
154
155 Using provided keys generate 32-byte shared secret, that both parties
156 can use without disclosing their private secret keys.
157
158 curve25519
159 Access to primitive function is also provided.
160
161 use Crypt::Curve25519 'curve25519';
162
163 my $key = curve25519($my_secret_key, $basepoint);
164
165 # public key
166 if ( $basepoint eq pack('H64', '09') ) {
167 print "\$key is a public key\n";
168 }
169 elsif ( $basepoint eq $his_public_key ) {
170 print "\$key is a shared secret\n";
171 }
172
173 Using provided secret key and depending on the 32-byte basepoint
174 generate 32-byte public key or shared secret.
175
177 • <http://cr.yp.to/ecdh.html>
178
180 Alex J. G. Burzyński <ajgb@cpan.org>
181
183 This software is copyright (c) 2014 by Alex J. G. Burzyński
184 <ajgb@cpan.org>.
185
186 This is free software; you can redistribute it and/or modify it under
187 the same terms as the Perl 5 programming language system itself.
188
189
190
191perl v5.34.0 2022-03-04 Crypt::Curve25519(3)