Merge 'dev' with dht, ecc auth, proof of work and fixes.
[brdnet.git] / ed25519.pas
blob5a6ea24beae3a85cf42e4c30258fa032481eb5f0
1 {$mode objfpc}
2 UNIT ed25519;
3 INTERFACE
5 type tKey32=packed array [0..31] of byte;
6 type tKey64=packed array [0..63] of byte;
7 type
8 tPubKey=tKey32;
9 tPrivKey=tKey64;
10 tSig=tKey64;
12 (*procedure CreateSeed(out seed: tKey32);*)
13 procedure CreatekeyPair(out pub:tPubKey; var priv:tPrivKey);
14 procedure Sign(out signature:tSig; const message; len:LongWord; const pub:tPubKey; const priv:tPrivKey);
15 function Verify(const signature:tSig; const message; len:LongWord; const pub:tPubKey):boolean;
16 procedure SharedSecret(out shared:tKey32; const pub:tPubKey; const priv:tPrivKey);
18 IMPLEMENTATION
20 {$L ed25519/sc.o}
21 {$L ed25519/fe.o}
22 {$L ed25519/ge.o}
23 {$L ed25519/sign.o}
24 {$L ed25519/verify.o}
25 {$L ed25519/key_exchange.o}
26 {$L ed25519/sha512.o}
28 procedure ed25519_create_keypair(pub,priv,seed:pointer);
29 cdecl;external;
30 procedure ed25519_sign(sig,msg:pointer; len:LongWord; pub,priv:pointer);
31 cdecl;external;
32 function ed25519_verify(sig,msg:pointer; len:LongWord; pub:pointer):integer;
33 cdecl;external;
34 procedure ed25519_key_exchange(shared,pub,priv:pointer);
35 cdecl;external;
37 type ge_p3=packed array [1..160] of byte; {opaque}
38 procedure ge_scalarmult_base(h,a:pointer); cdecl;external;
39 procedure ge_p3_tobytes(s, h:pointer); cdecl;external;
41 procedure CreateKeyPair(out pub:tPubKey; var priv:tPrivKey);
42 var A:ge_p3;
43 begin
44 priv[ 0] := priv[ 0] and 248;
45 priv[31] := priv[31] and 63;
46 priv[31] := priv[31] or 64;
47 ge_scalarmult_base(@A, @priv);
48 ge_p3_tobytes(@pub, @A);
49 end;
51 procedure Sign(out signature:tSig; const message; len:LongWord; const pub:tPubKey; const priv:tPrivKey);
52 begin
53 ed25519_sign(@signature,@message,len,@pub,@priv);
54 end;
56 function Verify(const signature:tSig; const message; len:LongWord; const pub:tPubKey):boolean;
57 begin
58 Verify:=ed25519_verify(@signature,@message,len,@pub)=1;
59 end;
61 procedure SharedSecret(out shared:tKey32; const pub:tPubKey; const priv:tPrivKey);
62 begin
63 ed25519_key_exchange(@shared,@pub,@priv);
64 end;
66 END.