-
Notifications
You must be signed in to change notification settings - Fork 1
/
kyber_pke.go
77 lines (44 loc) · 1.93 KB
/
kyber_pke.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
package main
import (
kyber512 "github.com/cloudflare/circl/pke/kyber/kyber1024" //we will use 1024
"encoding/hex"
"C"
)
//export genKYBER_PKE
func genKYBER_PKE() *C.char {
pubKey, privKey, _ := kyber512.GenerateKey(nil)
//Prepare buffers
pubBuf := make([]byte, kyber512.PublicKeySize)
privBuf := make([]byte, kyber512.PrivateKeySize)
//Serialize to byte[]
pubKey.Pack(pubBuf)
privKey.Pack(privBuf)
return C.CString(hex.EncodeToString(pubBuf) + ":" + hex.EncodeToString(privBuf))
}
//export genKYBER_PKE_ENCRYPT
func genKYBER_PKE_ENCRYPT(hexPubKey *C.char, hexPlainText *C.char) *C.char {
//______________________________ Prepare public key ______________________________
pubKeyBuffer, _ := hex.DecodeString(C.GoString(hexPubKey))
pubKey := kyber512.PublicKey{} //create empty
pubKey.Unpack(pubKeyBuffer) //unpack bytes
//_______________________________ Prepare buffers ________________________________
plainBuf, _ := hex.DecodeString(C.GoString(hexPlainText)) //Up to 32 bytes(kyber512.PlaintextSize) payload
seedBuf := make([]byte, kyber512.EncryptionSeedSize)
//Prepare buffer for cipherText
ct := make([]byte, kyber512.CiphertextSize)
pubKey.EncryptTo(ct, plainBuf, seedBuf)
return C.CString(hex.EncodeToString(ct))
}
//export genKYBER_PKE_DECRYPT
func genKYBER_PKE_DECRYPT(hexPrivateKey *C.char, hexCipherText *C.char) *C.char {
//______________________________ Prepare prviate key ______________________________
privateKeyBuffer, _ := hex.DecodeString(C.GoString(hexPrivateKey))
privKey := kyber512.PrivateKey{} //create empty
privKey.Unpack(privateKeyBuffer) //unpack bytes
//_______________________________ Prepare buffers ________________________________
plainBuf := make([]byte, kyber512.PlaintextSize) // 32bytes buffer
cipherTextBuffer, _ := hex.DecodeString(C.GoString(hexCipherText))
privKey.DecryptTo(plainBuf, cipherTextBuffer)
return C.CString(hex.EncodeToString(plainBuf))
}
func main() {}