-
Notifications
You must be signed in to change notification settings - Fork 2
/
shard.go
95 lines (84 loc) · 2.48 KB
/
shard.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
package multikey
import (
"crypto/rsa"
"encoding/base64"
"errors"
"fmt"
"github.com/adrianosela/multikey/keys"
)
const (
errMsgEmptyValue = "shard can not have empty value"
errMsgCouldNotEncrypt = "could not encrypt shard value"
errMsgIncorrectDecryptionKey = "the provided key does not match the shard's encryption key's fingerprint"
errMsgCouldNotDecode = "could not b64 decode shard value"
errMsgCouldNotDecrypt = "could not decrypt shard value"
)
// shard describes a piece of secret that has been split
// with Shamir's Secret Sharing Algorithm
type shard struct {
Value []byte
}
// encryptedShard represents a shard that has been encrypted
type encryptedShard struct {
Value string `json:"value"`
KeyID string `json:"key_id"`
}
// newShard returns a populated Shard struct
func newShard(value []byte) (*shard, error) {
if len(value) == 0 {
return nil, errors.New(errMsgEmptyValue)
}
return &shard{
Value: value,
}, nil
}
// encrypt encrypts and ASCII armours a shard's value
func (s *shard) encrypt(k *rsa.PublicKey) (*encryptedShard, error) {
if len(s.Value) == 0 {
return nil, errors.New(errMsgEmptyValue)
}
armoured, err := encryptAndArmourShamirPart(s.Value, k)
if err != nil {
return nil, err
}
return &encryptedShard{
Value: armoured,
KeyID: keys.GetFingerprint(k),
}, nil
}
// Decrypt decrypts an EncryptedShard
func (es *encryptedShard) decrypt(k *rsa.PrivateKey) (*shard, error) {
fp := keys.GetFingerprint(&k.PublicKey)
if es.KeyID != fp {
return nil, errors.New(errMsgIncorrectDecryptionKey)
}
val, err := decryptAndUnarmourShamirPart(es.Value, k)
if err != nil {
return nil, err
}
return &shard{Value: val}, nil
}
// decryptAndUnarmourShamirPart -
func decryptAndUnarmourShamirPart(data string, k *rsa.PrivateKey) ([]byte, error) {
// remove ASCII armour from piece
raw, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("%s: %s", errMsgCouldNotDecode, err)
}
// decrypt the raw encrypted message
dec, err := keys.DecryptMessage(raw, k)
if err != nil {
return nil, fmt.Errorf("%s: %s", errMsgCouldNotDecrypt, err)
}
return dec, nil
}
// encryptAndArmourShamirPart -
func encryptAndArmourShamirPart(data []byte, k *rsa.PublicKey) (string, error) {
// encrypt shard value
enc, err := keys.EncryptMessage(data, k)
if err != nil {
return "", fmt.Errorf("%s: %s", errMsgCouldNotEncrypt, err)
}
// ASCII armour the encrypted shard
return base64.StdEncoding.EncodeToString(enc), nil
}