1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #include "perlmulticore.h" |
|
|
6 | |
|
|
7 | /* work around unportable mess in fixedint.h */ |
|
|
8 | /* taken from libecb */ |
|
|
9 | #ifdef _WIN32 |
|
|
10 | typedef signed char int8_t; |
|
|
11 | typedef unsigned char uint8_t; |
|
|
12 | typedef signed short int16_t; |
|
|
13 | typedef unsigned short uint16_t; |
|
|
14 | typedef signed int int32_t; |
|
|
15 | typedef unsigned int uint32_t; |
|
|
16 | #if __GNUC__ |
|
|
17 | typedef signed long long int64_t; |
|
|
18 | typedef unsigned long long uint64_t; |
|
|
19 | #else /* _MSC_VER || __BORLANDC__ */ |
|
|
20 | typedef signed __int64 int64_t; |
|
|
21 | typedef unsigned __int64 uint64_t; |
|
|
22 | #endif |
|
|
23 | #define UINT64_C(v) v |
|
|
24 | #else |
|
|
25 | #include <inttypes.h> |
|
|
26 | #endif |
|
|
27 | #define FIXEDINT_H_INCLUDED |
|
|
28 | #include "ed25519/src/fixedint.h" |
|
|
29 | |
5 | #include "ed25519/src/ed25519.h" |
30 | #include "ed25519/src/ed25519.h" |
6 | |
31 | |
7 | #include "ed25519/src/add_scalar.c" |
32 | /*#include "ed25519/src/add_scalar.c"*/ |
8 | #include "ed25519/src/fixedint.h" |
33 | #include "ed25519/src/fixedint.h" |
9 | #include "ed25519/src/keypair.c" |
34 | #include "ed25519/src/keypair.c" |
10 | #include "ed25519/src/key_exchange.c" |
35 | #include "ed25519/src/key_exchange.c" |
11 | #include "ed25519/src/seed.c" |
36 | #include "ed25519/src/seed.c" |
12 | #include "ed25519/src/sha512.c" |
37 | #include "ed25519/src/sha512.c" |
… | |
… | |
22 | #define load_4(x) sc_load_4(x) |
47 | #define load_4(x) sc_load_4(x) |
23 | #include "ed25519/src/sc.c" |
48 | #include "ed25519/src/sc.c" |
24 | |
49 | |
25 | MODULE = Crypt::Ed25519 PACKAGE = Crypt::Ed25519 |
50 | MODULE = Crypt::Ed25519 PACKAGE = Crypt::Ed25519 |
26 | |
51 | |
|
|
52 | PROTOTYPES: ENABLE |
|
|
53 | |
|
|
54 | BOOT: |
|
|
55 | perlmulticore_support (); |
|
|
56 | |
27 | SV * |
57 | SV * |
28 | eddsa_secret_key () |
58 | eddsa_secret_key () |
29 | CODE: |
59 | CODE: |
30 | { |
60 | { |
31 | unsigned char seed[32]; |
61 | unsigned char seed[32]; |
32 | |
62 | |
|
|
63 | perlinterp_release (); |
33 | if (ed25519_create_seed (seed)) |
64 | int err = ed25519_create_seed (seed); |
|
|
65 | perlinterp_acquire (); |
|
|
66 | |
|
|
67 | if (err) |
34 | croak ("Crypt::Ed25519::eddsa_secret_key: ed25519_create_seed failed"); |
68 | croak ("Crypt::Ed25519::eddsa_secret_key: ed25519_create_seed failed"); |
35 | |
69 | |
36 | RETVAL = newSVpvn (seed , sizeof seed); |
70 | RETVAL = newSVpvn (seed, sizeof seed); |
37 | } |
71 | } |
38 | OUTPUT: |
72 | OUTPUT: |
39 | RETVAL |
73 | RETVAL |
40 | |
74 | |
41 | void |
75 | void |
… | |
… | |
54 | { |
88 | { |
55 | secret_p = SvPVbyte (secret, secret_l); |
89 | secret_p = SvPVbyte (secret, secret_l); |
56 | |
90 | |
57 | if (secret_l != 32) |
91 | if (secret_l != 32) |
58 | croak ("Crypt::Ed25519::eddsa_public_key: secret has wrong length (!= 32)"); |
92 | croak ("Crypt::Ed25519::eddsa_public_key: secret has wrong length (!= 32)"); |
|
|
93 | |
|
|
94 | perlinterp_release (); |
|
|
95 | ed25519_create_keypair (public_key, private_key, secret_p); |
|
|
96 | perlinterp_acquire (); |
59 | } |
97 | } |
60 | else |
98 | else |
61 | { |
99 | { |
|
|
100 | perlinterp_release (); |
|
|
101 | |
62 | if (ed25519_create_seed (seed)) |
102 | if (ed25519_create_seed (seed)) |
|
|
103 | { |
|
|
104 | perlinterp_acquire (); |
63 | croak ("Crypt::Ed25519::generate_keypair: ed25519_create_seed failed"); |
105 | croak ("Crypt::Ed25519::generate_keypair: ed25519_create_seed failed"); |
|
|
106 | } |
64 | |
107 | |
65 | secret_p = seed; |
108 | secret_p = seed; |
66 | } |
|
|
67 | |
109 | |
68 | ed25519_create_keypair (public_key, private_key, secret_p); |
110 | ed25519_create_keypair (public_key, private_key, secret_p); |
|
|
111 | |
|
|
112 | perlinterp_acquire (); |
|
|
113 | } |
69 | |
114 | |
70 | EXTEND (SP, 2); |
115 | EXTEND (SP, 2); |
71 | PUSHs (sv_2mortal (newSVpvn (public_key, sizeof public_key))); |
116 | PUSHs (sv_2mortal (newSVpvn (public_key, sizeof public_key))); |
72 | |
117 | |
73 | if (!ix) |
118 | if (!ix) |
… | |
… | |
107 | { |
152 | { |
108 | if (private_key_l != 64) |
153 | if (private_key_l != 64) |
109 | croak ("Crypt::Ed25519::sign: private key has wrong length (!= 64)"); |
154 | croak ("Crypt::Ed25519::sign: private key has wrong length (!= 64)"); |
110 | } |
155 | } |
111 | |
156 | |
|
|
157 | perlinterp_release (); |
112 | ed25519_sign (signature, message_p, message_l, public_key_p, private_key_p); |
158 | ed25519_sign (signature, message_p, message_l, public_key_p, private_key_p); |
|
|
159 | perlinterp_acquire (); |
113 | |
160 | |
114 | RETVAL = newSVpvn (signature, sizeof signature); |
161 | RETVAL = newSVpvn (signature, sizeof signature); |
115 | } |
162 | } |
116 | OUTPUT: |
163 | OUTPUT: |
117 | RETVAL |
164 | RETVAL |
… | |
… | |
129 | STRLEN public_key_l; char *public_key_p = SvPVbyte (public_key, public_key_l); |
176 | STRLEN public_key_l; char *public_key_p = SvPVbyte (public_key, public_key_l); |
130 | |
177 | |
131 | if (public_key_l != 32) |
178 | if (public_key_l != 32) |
132 | croak ("Crypt::Ed25519::verify: public key has wrong length (!= 32)"); |
179 | croak ("Crypt::Ed25519::verify: public key has wrong length (!= 32)"); |
133 | |
180 | |
|
|
181 | perlinterp_release (); |
134 | RETVAL = ed25519_verify (signature_p, message_p, message_l, public_key_p); |
182 | RETVAL = ed25519_verify (signature_p, message_p, message_l, public_key_p); |
|
|
183 | perlinterp_acquire (); |
135 | |
184 | |
136 | if (!RETVAL && ix) |
185 | if (!RETVAL && ix) |
137 | croak ("Crypt::Ed25519::verify_croak: signature verification failed"); |
186 | croak ("Crypt::Ed25519::verify_croak: signature verification failed"); |
138 | } |
187 | } |
139 | OUTPUT: |
188 | OUTPUT: |
140 | RETVAL |
189 | RETVAL |
141 | |
190 | |
|
|
191 | SV * |
|
|
192 | key_exchange (SV *public_key, SV *private_key) |
|
|
193 | CODE: |
|
|
194 | { |
|
|
195 | STRLEN public_key_l ; char *public_key_p = SvPVbyte (public_key , public_key_l ); |
|
|
196 | STRLEN private_key_l; char *private_key_p = SvPVbyte (private_key, private_key_l); |
142 | |
197 | |
|
|
198 | if (public_key_l != 32) |
|
|
199 | croak ("Crypt::Ed25519::key_exchange: public key has wrong length (!= 32)"); |
|
|
200 | |
|
|
201 | if (private_key_l != 64) |
|
|
202 | croak ("Crypt::Ed25519::key_exchange: private key has wrong length (!= 64)"); |
|
|
203 | |
|
|
204 | unsigned char shared_secret[32]; |
|
|
205 | |
|
|
206 | perlinterp_release (); |
|
|
207 | ed25519_key_exchange (shared_secret, public_key_p, private_key_p); |
|
|
208 | perlinterp_acquire (); |
|
|
209 | |
|
|
210 | RETVAL = newSVpvn (shared_secret, sizeof shared_secret); |
|
|
211 | } |
|
|
212 | OUTPUT: |
|
|
213 | RETVAL |
|
|
214 | |