forked from philippe44/libraop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurve25519.pm
289 lines (197 loc) · 7.49 KB
/
Curve25519.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package Crypt::Curve25519;
our $AUTHORITY = 'cpan:AJGB';
#ABSTRACT: Generate shared secret using elliptic-curve Diffie-Hellman function
$Crypt::Curve25519::VERSION = '0.06';
use strict;
use warnings;
use Carp qw( croak );
require Exporter;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = ( 'all' => [ qw(
curve25519
curve25519_secret_key
curve25519_public_key
curve25519_shared_secret
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
curve25519_secret_key
curve25519_public_key
curve25519_shared_secret
);
# Although curve25519_donna is also clamping the secret key this function
# has been provided for completeness and to ensure that secret keys generated
# here can be used in other implementations of the algorithm.
sub curve25519_secret_key {
my $value = shift;
croak 'Secret key requires 32 bytes' if length($value) != 32;
vec($value, 0 , 8) &= 248;
vec($value, 31, 8) &= 127;
vec($value, 31, 8) |= 64;
return $value;
}
require XSLoader;
XSLoader::load('Crypt::Curve25519', $Crypt::Curve25519::{VERSION} ?
${ $Crypt::Curve25519::{VERSION} } : ()
);
sub new {
return bless(\(my $o = 1), ref $_[0] ? ref $_[0] : $_[0] );
}
sub secret_key {
my ($self, $psk) = (shift, shift);
my $masked = curve25519_secret_key( pack('H64', $psk) );
return unpack('H64', $masked);
}
sub public_key {
my ($self, $sk) = (shift, shift);
my @args = pack('H64', $sk);
if ( @_ ) {
push @args, pack('H64', shift);
}
my $pk = unpack('H64', curve25519_public_key( @args ));
return $pk;
}
sub shared_secret {
my ($self, $sk, $pk) = @_;
return unpack('H64', curve25519_shared_secret( pack('H64', $sk), pack('H64', $pk) ));
}
sub generate {
my ($self, $sk, $bp) = @_;
return unpack('H64', curve25519( pack('H64', $sk), pack('H64', $bp) ));
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Crypt::Curve25519 - Generate shared secret using elliptic-curve Diffie-Hellman function
=head1 VERSION
version 0.06
=head1 SYNOPSIS
use Crypt::Curve25519;
# Alice:
my $alice_secret_key = curve25519_secret_key(random_32_bytes());
my $alice_public_key = curve25519_public_key( $alice_secret_key );
# Bob:
my $bob_secret_key = curve25519_secret_key(random_32_bytes());
my $bob_public_key = curve25519_public_key( $bob_secret_key );
# Alice and Bob exchange their public keys
my $alice_public_key_hex = unpack('H64', $alice_public_key);
my $bob_public_key_hex = unpack('H64', $bob_public_key);
# Alice calculates shared secret to communicate with Bob
my $shared_secret_with_bob = curve25519_shared_secret(
$alice_secret_key,
pack('H64', $bob_public_key_hex)
);
# Bob calculates shared secret to communicate with Alice
my $shared_secret_with_alice = curve25519_shared_secret(
$bob_secret_key,
pack('H64', $alice_public_key_hex)
);
# Shared secrets are equal
die "Something horrible has happend!"
unless $shared_secret_with_bob eq $shared_secret_with_alice;
This package provides also simplified OO interface:
use Crypt::Curve25519 ();
my $c = Crypt::Curve25519->new();
# Alice:
my $alice_secret_key_hex = $c->secret_key(random_hexencoded_32_bytes());
my $alice_public_key_hex = $c->public_key( $alice_secret_key_hex );
# Bob:
my $bob_secret_key_hex = $c->secret_key(random_hexencoded_32_bytes());
my $bob_public_key_hex = $c->public_key( $bob_secret_key_hex );
# Alice and Bob exchange their public keys
# Alice calculates shared secret to communicate with Bob
my $shared_secret_with_bob_hex = $c->shared_secret(
$alice_secret_key_hex,
$bob_public_key_hex);
# Bob calculates shared secret to communicate with Alice
my $shared_secret_with_alice_hex = $c->shared_secret(
$bob_secret_key_hex,
$alice_public_key_hex);
# Shared secrets are equal
die "Something horrible has happend!"
unless $shared_secret_with_bob_hex eq $shared_secret_with_alice_hex;
Example functions to generate pseudo-random private secret key:
sub random_32_bytes {
return join('', map { chr(int(rand(255))) } 1 .. 32);
}
sub random_hexencoded_32_bytes {
return unpack('H64', random_32_bytes());
}
=head1 DESCRIPTION
Curve25519 is a state-of-the-art Diffie-Hellman function suitable for a wide
variety of applications.
Given a user's 32-byte secret key, Curve25519 computes the user's 32-byte
public key. Given the user's 32-byte secret key and another user's 32-byte
public key, Curve25519 computes a 32-byte secret shared by the two users. This
secret can then be used to authenticate and encrypt messages between the two
users.
=head1 METHODS
=head2 new
my $c = Crypt::Curve25519->new();
Create a new object
=head2 secret_key
my $my_secret_key_hex = $c->secret_key( $my_random_32byte_string_hex );
Using hex encoded 32-byte random string from cryptographically safe source
create masked secret key.
=head2 public_key
my $public_key_hex = $c->public_key( $my_secret_key_hex );
Using hex encoded masked secret key generate corresponding hex encoded 32-byte
Curve25519 public key.
=head2 shared_secret
my $shared_secret_hex = $c->shared_secret(
$my_secret_key_hex, $his_public_key_hex
);
Using provided hex encoded keys generate 32-byte hex encoded shared secret,
that both parties can use without disclosing their private secret keys.
=head2 generate
Access to primitive method is also provided.
my $key_hex = $c->generate($my_secret_key_hex, $basepoint_hex);
# public key
if ( $basepoint_hex eq unpack("H64", pack("H64", "09")) ) {
print "\$key_hex is a public key\n";
}
elsif ( $basepoint_hex eq $his_public_key_hex ) {
print "\$key_hex is a shared secret\n";
}
Using provided hex encoded secret key and depending on the 32-byte hex
encoded basepoint generate 32-byte hex encoded public key or shared secret.
=head1 FUNCTIONS
=head2 curve25519_secret_key
my $my_secret_key = curve25519_secret_key($my_random_32byte_string);
Using provided 32-byte random string from cryptographically safe source create
masked secret key.
=head2 curve25519_public_key
my $public_key = curve25519_public_key($my_secret_key);
Using masked secret key generate corresponding 32-byte Curve25519 public key.
=head2 curve25519_shared_secret
my $shared_secret = curve25519_shared_secret(
$my_secret_key, $his_public_key
);
Using provided keys generate 32-byte shared secret, that both parties can use
without disclosing their private secret keys.
=head2 curve25519
Access to primitive function is also provided.
use Crypt::Curve25519 'curve25519';
my $key = curve25519($my_secret_key, $basepoint);
# public key
if ( $basepoint eq pack('H64', '09') ) {
print "\$key is a public key\n";
}
elsif ( $basepoint eq $his_public_key ) {
print "\$key is a shared secret\n";
}
Using provided secret key and depending on the 32-byte basepoint generate
32-byte public key or shared secret.
=head1 SEE ALSO
=over 4
=item * L<http://cr.yp.to/ecdh.html>
=back
=head1 AUTHOR
Alex J. G. Burzyński <[email protected]>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Alex J. G. Burzyński <[email protected]>.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut