1 |
root |
1.1 |
#include "EXTERN.h" |
2 |
|
|
#include "perl.h" |
3 |
|
|
#include "XSUB.h" |
4 |
|
|
|
5 |
root |
1.2 |
/* work around unportable mess in fixedint.h */ |
6 |
|
|
/* taken from libecb */ |
7 |
|
|
#ifdef _WIN32 |
8 |
|
|
typedef signed char int8_t; |
9 |
|
|
typedef unsigned char uint8_t; |
10 |
|
|
typedef signed short int16_t; |
11 |
|
|
typedef unsigned short uint16_t; |
12 |
|
|
typedef signed int int32_t; |
13 |
|
|
typedef unsigned int uint32_t; |
14 |
|
|
#if __GNUC__ |
15 |
|
|
typedef signed long long int64_t; |
16 |
|
|
typedef unsigned long long uint64_t; |
17 |
|
|
#else /* _MSC_VER || __BORLANDC__ */ |
18 |
|
|
typedef signed __int64 int64_t; |
19 |
|
|
typedef unsigned __int64 uint64_t; |
20 |
|
|
#endif |
21 |
|
|
#define UINT64_C(v) v |
22 |
|
|
#else |
23 |
|
|
#include <inttypes.h> |
24 |
|
|
#endif |
25 |
|
|
#define FIXEDINT_H_INCLUDED |
26 |
|
|
#include "ed25519/src/fixedint.h" |
27 |
|
|
|
28 |
root |
1.1 |
#include "ed25519/src/ed25519.h" |
29 |
|
|
|
30 |
|
|
#include "ed25519/src/add_scalar.c" |
31 |
|
|
#include "ed25519/src/fixedint.h" |
32 |
|
|
#include "ed25519/src/keypair.c" |
33 |
|
|
#include "ed25519/src/key_exchange.c" |
34 |
|
|
#include "ed25519/src/seed.c" |
35 |
|
|
#include "ed25519/src/sha512.c" |
36 |
|
|
#include "ed25519/src/sha512.h" |
37 |
|
|
#include "ed25519/src/sign.c" |
38 |
|
|
#include "ed25519/src/verify.c" |
39 |
|
|
|
40 |
|
|
#define select(a,b,c) ed25519_select (a, b, c) |
41 |
|
|
#include "ed25519/src/ge.c" |
42 |
|
|
|
43 |
|
|
#include "ed25519/src/fe.c" |
44 |
|
|
#define load_3(x) sc_load_3(x) |
45 |
|
|
#define load_4(x) sc_load_4(x) |
46 |
|
|
#include "ed25519/src/sc.c" |
47 |
|
|
|
48 |
|
|
MODULE = Crypt::Ed25519 PACKAGE = Crypt::Ed25519 |
49 |
|
|
|
50 |
root |
1.2 |
PROTOTYPES: ENABLE |
51 |
|
|
|
52 |
root |
1.1 |
SV * |
53 |
|
|
eddsa_secret_key () |
54 |
|
|
CODE: |
55 |
|
|
{ |
56 |
|
|
unsigned char seed[32]; |
57 |
|
|
|
58 |
|
|
if (ed25519_create_seed (seed)) |
59 |
|
|
croak ("Crypt::Ed25519::eddsa_secret_key: ed25519_create_seed failed"); |
60 |
|
|
|
61 |
root |
1.2 |
RETVAL = newSVpvn (seed, sizeof seed); |
62 |
root |
1.1 |
} |
63 |
|
|
OUTPUT: |
64 |
|
|
RETVAL |
65 |
|
|
|
66 |
|
|
void |
67 |
|
|
generate_keypair (SV *secret = 0) |
68 |
|
|
ALIAS: |
69 |
|
|
eddsa_public_key = 1 |
70 |
|
|
PPCODE: |
71 |
|
|
{ |
72 |
|
|
STRLEN secret_l; char *secret_p; |
73 |
|
|
|
74 |
|
|
unsigned char seed[32]; |
75 |
|
|
unsigned char public_key[32]; |
76 |
|
|
unsigned char private_key[64]; |
77 |
|
|
|
78 |
|
|
if (secret) |
79 |
|
|
{ |
80 |
|
|
secret_p = SvPVbyte (secret, secret_l); |
81 |
|
|
|
82 |
|
|
if (secret_l != 32) |
83 |
|
|
croak ("Crypt::Ed25519::eddsa_public_key: secret has wrong length (!= 32)"); |
84 |
|
|
} |
85 |
|
|
else |
86 |
|
|
{ |
87 |
|
|
if (ed25519_create_seed (seed)) |
88 |
|
|
croak ("Crypt::Ed25519::generate_keypair: ed25519_create_seed failed"); |
89 |
|
|
|
90 |
|
|
secret_p = seed; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
ed25519_create_keypair (public_key, private_key, secret_p); |
94 |
|
|
|
95 |
|
|
EXTEND (SP, 2); |
96 |
|
|
PUSHs (sv_2mortal (newSVpvn (public_key, sizeof public_key))); |
97 |
|
|
|
98 |
|
|
if (!ix) |
99 |
|
|
PUSHs (sv_2mortal (newSVpvn (private_key, sizeof private_key))); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
SV * |
103 |
|
|
sign (SV *message, SV *public_key, SV *private_key) |
104 |
|
|
ALIAS: |
105 |
|
|
eddsa_sign = 1 |
106 |
|
|
CODE: |
107 |
|
|
{ |
108 |
|
|
unsigned char hash[64]; /* result of sha512 */ |
109 |
|
|
unsigned char signature[64]; |
110 |
|
|
|
111 |
|
|
STRLEN message_l ; char *message_p = SvPVbyte (message , message_l ); |
112 |
|
|
STRLEN public_key_l ; char *public_key_p = SvPVbyte (public_key , public_key_l ); |
113 |
|
|
STRLEN private_key_l; char *private_key_p = SvPVbyte (private_key, private_key_l); |
114 |
|
|
|
115 |
|
|
if (public_key_l != 32) |
116 |
|
|
croak ("Crypt::Ed25519::sign: public key has wrong length (!= 32)"); |
117 |
|
|
|
118 |
|
|
if (ix) |
119 |
|
|
{ |
120 |
|
|
if (private_key_l != 32) |
121 |
|
|
croak ("Crypt::Ed25519::eddsa_sign: secret key has wrong length (!= 32)"); |
122 |
|
|
|
123 |
|
|
sha512 (private_key_p, 32, hash); |
124 |
|
|
|
125 |
|
|
hash[ 0] &= 248; |
126 |
|
|
hash[31] &= 63; |
127 |
|
|
hash[31] |= 64; |
128 |
|
|
|
129 |
|
|
private_key_p = hash; |
130 |
|
|
} |
131 |
|
|
else |
132 |
|
|
{ |
133 |
|
|
if (private_key_l != 64) |
134 |
|
|
croak ("Crypt::Ed25519::sign: private key has wrong length (!= 64)"); |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
ed25519_sign (signature, message_p, message_l, public_key_p, private_key_p); |
138 |
|
|
|
139 |
|
|
RETVAL = newSVpvn (signature, sizeof signature); |
140 |
|
|
} |
141 |
|
|
OUTPUT: |
142 |
|
|
RETVAL |
143 |
|
|
|
144 |
|
|
bool |
145 |
|
|
verify (SV *message, SV *public_key, SV *signature) |
146 |
|
|
ALIAS: |
147 |
|
|
eddsa_verify = 0 |
148 |
|
|
verify_croak = 1 |
149 |
|
|
eddsa_verify_croak = 1 |
150 |
|
|
CODE: |
151 |
|
|
{ |
152 |
|
|
STRLEN signature_l ; char *signature_p = SvPVbyte (signature , signature_l ); |
153 |
|
|
STRLEN message_l ; char *message_p = SvPVbyte (message , message_l ); |
154 |
|
|
STRLEN public_key_l; char *public_key_p = SvPVbyte (public_key, public_key_l); |
155 |
|
|
|
156 |
|
|
if (public_key_l != 32) |
157 |
|
|
croak ("Crypt::Ed25519::verify: public key has wrong length (!= 32)"); |
158 |
|
|
|
159 |
|
|
RETVAL = ed25519_verify (signature_p, message_p, message_l, public_key_p); |
160 |
|
|
|
161 |
|
|
if (!RETVAL && ix) |
162 |
|
|
croak ("Crypt::Ed25519::verify_croak: signature verification failed"); |
163 |
|
|
} |
164 |
|
|
OUTPUT: |
165 |
|
|
RETVAL |
166 |
|
|
|
167 |
|
|
|