ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Crypt-Ed25519/Ed25519.xs
Revision: 1.1
Committed: Fri Mar 27 20:23:11 2015 UTC (9 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-0_1
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     #include "ed25519/src/ed25519.h"
6    
7     #include "ed25519/src/add_scalar.c"
8     #include "ed25519/src/fixedint.h"
9     #include "ed25519/src/keypair.c"
10     #include "ed25519/src/key_exchange.c"
11     #include "ed25519/src/seed.c"
12     #include "ed25519/src/sha512.c"
13     #include "ed25519/src/sha512.h"
14     #include "ed25519/src/sign.c"
15     #include "ed25519/src/verify.c"
16    
17     #define select(a,b,c) ed25519_select (a, b, c)
18     #include "ed25519/src/ge.c"
19    
20     #include "ed25519/src/fe.c"
21     #define load_3(x) sc_load_3(x)
22     #define load_4(x) sc_load_4(x)
23     #include "ed25519/src/sc.c"
24    
25     MODULE = Crypt::Ed25519 PACKAGE = Crypt::Ed25519
26    
27     SV *
28     eddsa_secret_key ()
29     CODE:
30     {
31     unsigned char seed[32];
32    
33     if (ed25519_create_seed (seed))
34     croak ("Crypt::Ed25519::eddsa_secret_key: ed25519_create_seed failed");
35    
36     RETVAL = newSVpvn (seed , sizeof seed);
37     }
38     OUTPUT:
39     RETVAL
40    
41     void
42     generate_keypair (SV *secret = 0)
43     ALIAS:
44     eddsa_public_key = 1
45     PPCODE:
46     {
47     STRLEN secret_l; char *secret_p;
48    
49     unsigned char seed[32];
50     unsigned char public_key[32];
51     unsigned char private_key[64];
52    
53     if (secret)
54     {
55     secret_p = SvPVbyte (secret, secret_l);
56    
57     if (secret_l != 32)
58     croak ("Crypt::Ed25519::eddsa_public_key: secret has wrong length (!= 32)");
59     }
60     else
61     {
62     if (ed25519_create_seed (seed))
63     croak ("Crypt::Ed25519::generate_keypair: ed25519_create_seed failed");
64    
65     secret_p = seed;
66     }
67    
68     ed25519_create_keypair (public_key, private_key, secret_p);
69    
70     EXTEND (SP, 2);
71     PUSHs (sv_2mortal (newSVpvn (public_key, sizeof public_key)));
72    
73     if (!ix)
74     PUSHs (sv_2mortal (newSVpvn (private_key, sizeof private_key)));
75     }
76    
77     SV *
78     sign (SV *message, SV *public_key, SV *private_key)
79     ALIAS:
80     eddsa_sign = 1
81     CODE:
82     {
83     unsigned char hash[64]; /* result of sha512 */
84     unsigned char signature[64];
85    
86     STRLEN message_l ; char *message_p = SvPVbyte (message , message_l );
87     STRLEN public_key_l ; char *public_key_p = SvPVbyte (public_key , public_key_l );
88     STRLEN private_key_l; char *private_key_p = SvPVbyte (private_key, private_key_l);
89    
90     if (public_key_l != 32)
91     croak ("Crypt::Ed25519::sign: public key has wrong length (!= 32)");
92    
93     if (ix)
94     {
95     if (private_key_l != 32)
96     croak ("Crypt::Ed25519::eddsa_sign: secret key has wrong length (!= 32)");
97    
98     sha512 (private_key_p, 32, hash);
99    
100     hash[ 0] &= 248;
101     hash[31] &= 63;
102     hash[31] |= 64;
103    
104     private_key_p = hash;
105     }
106     else
107     {
108     if (private_key_l != 64)
109     croak ("Crypt::Ed25519::sign: private key has wrong length (!= 64)");
110     }
111    
112     ed25519_sign (signature, message_p, message_l, public_key_p, private_key_p);
113    
114     RETVAL = newSVpvn (signature, sizeof signature);
115     }
116     OUTPUT:
117     RETVAL
118    
119     bool
120     verify (SV *message, SV *public_key, SV *signature)
121     ALIAS:
122     eddsa_verify = 0
123     verify_croak = 1
124     eddsa_verify_croak = 1
125     CODE:
126     {
127     STRLEN signature_l ; char *signature_p = SvPVbyte (signature , signature_l );
128     STRLEN message_l ; char *message_p = SvPVbyte (message , message_l );
129     STRLEN public_key_l; char *public_key_p = SvPVbyte (public_key, public_key_l);
130    
131     if (public_key_l != 32)
132     croak ("Crypt::Ed25519::verify: public key has wrong length (!= 32)");
133    
134     RETVAL = ed25519_verify (signature_p, message_p, message_l, public_key_p);
135    
136     if (!RETVAL && ix)
137     croak ("Crypt::Ed25519::verify_croak: signature verification failed");
138     }
139     OUTPUT:
140     RETVAL
141    
142