-
Notifications
You must be signed in to change notification settings - Fork 1
/
sidh.go
98 lines (52 loc) · 1.87 KB
/
sidh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"github.com/cloudflare/circl/dh/sidh"
"crypto/rand"
"encoding/hex"
"C"
)
//export genSIDH
func genSIDH(keyType *C.char) *C.char{
var keyTyp sidh.KeyVariant;
if(C.GoString(keyType)=="A"){
keyTyp=sidh.KeyVariantSidhA
}else {
keyTyp=sidh.KeyVariantSidhB
}
prv := sidh.NewPrivateKey(sidh.Fp503,keyTyp)
pub := sidh.NewPublicKey(sidh.Fp503,keyTyp)
prv.Generate(rand.Reader)
prv.GeneratePublicKey(pub)
prvKeyBytes := make([]byte,prv.Size())
pubKeyBytes := make([]byte,pub.Size())
prv.Export(prvKeyBytes)
pub.Export(pubKeyBytes)
return C.CString(hex.EncodeToString(pubKeyBytes)+":"+hex.EncodeToString(prvKeyBytes))
}
//export getSIDH
func getSIDH(myKeyType *C.char,friendPubHex *C.char,myPrivateHex *C.char) *C.char{
var myType,friendType sidh.KeyVariant;
if(C.GoString(myKeyType)=="A"){
myType=sidh.KeyVariantSidhA
friendType=sidh.KeyVariantSidhB
}else {
myType=sidh.KeyVariantSidhB
friendType=sidh.KeyVariantSidhA
}
//________________ IMPORT MY CREDENTIALS _____________________
myPrv := sidh.NewPrivateKey(sidh.Fp503, myType)
myPub := sidh.NewPublicKey(sidh.Fp503, myType)
//Prepare buffer to import private key
prvKeyBytes,_:=hex.DecodeString(C.GoString(myPrivateHex))
myPrv.Import(prvKeyBytes)
myPrv.GeneratePublicKey(myPub)
//______________ RECOVER FRIEND'S PUBKEY ____________________
friendPub := sidh.NewPublicKey(sidh.Fp503,friendType)
friendPubKeyBytes,_:=hex.DecodeString(C.GoString(friendPubHex))
friendPub.Import(friendPubKeyBytes)
//____________________ GET SECRET ___________________________
secret := make([]byte, myPrv.SharedSecretSize())
myPrv.DeriveSecret(secret[:],friendPub)
return C.CString(hex.EncodeToString(secret))
}
func main() {}