/
generate.go
124 lines (105 loc) · 2.68 KB
/
generate.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cmd
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
"github.com/ceriath/rsa-shamir-secret-sharing/s4"
"github.com/spf13/cobra"
)
var gn, gk int
var keysize int
var saveKey, gSaveShares bool
func init() {
rootCmd.AddCommand(generateCmd)
generateCmd.Flags().IntVarP(&gn, "sharecount", "n", 5, "number of shares to generate")
generateCmd.Flags().IntVarP(&gk, "threshold", "k", 3, "number of required shares to reconstruct the secret")
generateCmd.Flags().IntVarP(&keysize, "bitsize", "b", 2048, "bitsize of the generated key")
generateCmd.Flags().BoolVarP(&saveKey, "createKeyfile", "c", false, "set to true if you want to save the private key to a file")
generateCmd.Flags().BoolVarP(&gSaveShares, "saveShares", "f", false, "set to true if you want to save the shares to a file")
}
var generateCmd = &cobra.Command{
Use: "gen",
Short: "Generates a shared RSA KeyPair",
Long: `Generates a public RSA key and n shares for the private key of which k are required for reconstruction`,
Run: func(cmd *cobra.Command, args []string) {
reader := rand.Reader
key, err := rsa.GenerateKey(reader, keysize)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
publicKey := key.PublicKey
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if saveKey {
savePEMKey("private.pem", key)
}
savePublicPEMKey("public.pem", publicKey)
shares, err := s4.Split(x509.MarshalPKCS1PrivateKey(key), byte(gk), byte(gn))
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
for _, share := range shares {
fmt.Printf("Share %d:\n%s\n\n", share.Index, share.GetBase64())
}
if gSaveShares {
storeShares(shares)
}
},
}
func savePEMKey(fileName string, key *rsa.PrivateKey) {
outFile, err := os.Create(fileName)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
defer func() {
err = outFile.Close()
if err != nil {
panic("can't close file")
}
}()
b := x509.MarshalPKCS1PrivateKey(key)
var privateKey = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: b,
}
err = pem.Encode(outFile, privateKey)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
}
func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) {
asn1Bytes, err := x509.MarshalPKIXPublicKey(&pubkey)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
var pemkey = &pem.Block{
Type: "PUBLIC KEY",
Bytes: asn1Bytes,
}
pemfile, err := os.Create(fileName)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
defer func() {
err = pemfile.Close()
if err != nil {
panic("can't close file")
}
}()
err = pem.Encode(pemfile, pemkey)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
}