ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Crypt-Ed25519/Ed25519.xs
Revision: 1.3
Committed: Sun Jun 28 17:44:16 2015 UTC (8 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-1_02, rel-1_03
Changes since 1.2: +20 -3 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.3 #include "perlmulticore.h"
6    
7 root 1.2 /* 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    
30 root 1.1 #include "ed25519/src/ed25519.h"
31    
32     #include "ed25519/src/add_scalar.c"
33     #include "ed25519/src/fixedint.h"
34     #include "ed25519/src/keypair.c"
35     #include "ed25519/src/key_exchange.c"
36     #include "ed25519/src/seed.c"
37     #include "ed25519/src/sha512.c"
38     #include "ed25519/src/sha512.h"
39     #include "ed25519/src/sign.c"
40     #include "ed25519/src/verify.c"
41    
42     #define select(a,b,c) ed25519_select (a, b, c)
43     #include "ed25519/src/ge.c"
44    
45     #include "ed25519/src/fe.c"
46     #define load_3(x) sc_load_3(x)
47     #define load_4(x) sc_load_4(x)
48     #include "ed25519/src/sc.c"
49    
50     MODULE = Crypt::Ed25519 PACKAGE = Crypt::Ed25519
51    
52 root 1.2 PROTOTYPES: ENABLE
53    
54 root 1.1 SV *
55     eddsa_secret_key ()
56     CODE:
57     {
58     unsigned char seed[32];
59    
60     if (ed25519_create_seed (seed))
61     croak ("Crypt::Ed25519::eddsa_secret_key: ed25519_create_seed failed");
62    
63 root 1.2 RETVAL = newSVpvn (seed, sizeof seed);
64 root 1.1 }
65     OUTPUT:
66     RETVAL
67    
68     void
69     generate_keypair (SV *secret = 0)
70     ALIAS:
71     eddsa_public_key = 1
72     PPCODE:
73     {
74     STRLEN secret_l; char *secret_p;
75    
76     unsigned char seed[32];
77     unsigned char public_key[32];
78     unsigned char private_key[64];
79    
80     if (secret)
81     {
82     secret_p = SvPVbyte (secret, secret_l);
83    
84     if (secret_l != 32)
85     croak ("Crypt::Ed25519::eddsa_public_key: secret has wrong length (!= 32)");
86 root 1.3
87     perlinterp_release ();
88     ed25519_create_keypair (public_key, private_key, secret_p);
89     perlinterp_acquire ();
90 root 1.1 }
91     else
92     {
93 root 1.3 perlinterp_release ();
94    
95 root 1.1 if (ed25519_create_seed (seed))
96 root 1.3 {
97     perlinterp_acquire ();
98     croak ("Crypt::Ed25519::generate_keypair: ed25519_create_seed failed");
99     }
100 root 1.1
101     secret_p = seed;
102 root 1.3
103     ed25519_create_keypair (public_key, private_key, secret_p);
104    
105     perlinterp_acquire ();
106 root 1.1 }
107    
108     EXTEND (SP, 2);
109     PUSHs (sv_2mortal (newSVpvn (public_key, sizeof public_key)));
110    
111     if (!ix)
112     PUSHs (sv_2mortal (newSVpvn (private_key, sizeof private_key)));
113     }
114    
115     SV *
116     sign (SV *message, SV *public_key, SV *private_key)
117     ALIAS:
118     eddsa_sign = 1
119     CODE:
120     {
121     unsigned char hash[64]; /* result of sha512 */
122     unsigned char signature[64];
123    
124     STRLEN message_l ; char *message_p = SvPVbyte (message , message_l );
125     STRLEN public_key_l ; char *public_key_p = SvPVbyte (public_key , public_key_l );
126     STRLEN private_key_l; char *private_key_p = SvPVbyte (private_key, private_key_l);
127    
128     if (public_key_l != 32)
129     croak ("Crypt::Ed25519::sign: public key has wrong length (!= 32)");
130    
131     if (ix)
132     {
133     if (private_key_l != 32)
134     croak ("Crypt::Ed25519::eddsa_sign: secret key has wrong length (!= 32)");
135    
136     sha512 (private_key_p, 32, hash);
137    
138     hash[ 0] &= 248;
139     hash[31] &= 63;
140     hash[31] |= 64;
141    
142     private_key_p = hash;
143     }
144     else
145     {
146     if (private_key_l != 64)
147     croak ("Crypt::Ed25519::sign: private key has wrong length (!= 64)");
148     }
149    
150 root 1.3 perlinterp_release ();
151 root 1.1 ed25519_sign (signature, message_p, message_l, public_key_p, private_key_p);
152 root 1.3 perlinterp_acquire ();
153 root 1.1
154     RETVAL = newSVpvn (signature, sizeof signature);
155     }
156     OUTPUT:
157     RETVAL
158    
159     bool
160     verify (SV *message, SV *public_key, SV *signature)
161     ALIAS:
162     eddsa_verify = 0
163     verify_croak = 1
164     eddsa_verify_croak = 1
165     CODE:
166     {
167     STRLEN signature_l ; char *signature_p = SvPVbyte (signature , signature_l );
168     STRLEN message_l ; char *message_p = SvPVbyte (message , message_l );
169     STRLEN public_key_l; char *public_key_p = SvPVbyte (public_key, public_key_l);
170    
171     if (public_key_l != 32)
172     croak ("Crypt::Ed25519::verify: public key has wrong length (!= 32)");
173    
174 root 1.3 perlinterp_release ();
175 root 1.1 RETVAL = ed25519_verify (signature_p, message_p, message_l, public_key_p);
176 root 1.3 perlinterp_acquire ();
177 root 1.1
178     if (!RETVAL && ix)
179     croak ("Crypt::Ed25519::verify_croak: signature verification failed");
180     }
181     OUTPUT:
182     RETVAL
183    
184