-
Notifications
You must be signed in to change notification settings - Fork 17
/
header.go
83 lines (75 loc) · 2.67 KB
/
header.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
package minilock
import (
"encoding/json"
"github.com/cathalgarvey/go-minilock/taber"
)
// This is the marrow at the heart of the miniLock header that contains
// decryption instructions for the encrypted file container.
type FileInfo struct {
FileKey []byte `json:"fileKey"`
FileNonce []byte `json:"fileNonce"`
FileHash []byte `json:"fileHash"`
}
// This is the container for the decryption instructions of "FileInfo",
// also containing sender and recipient. It is encrypted to the recipient
// with an ephemeral key to preserve privacy.
type DecryptInfoEntry struct {
SenderID string `json:"senderID"`
RecipientID string `json:"recipientID"`
FileInfoEnc []byte `json:"fileInfo"`
}
func (self *DecryptInfoEntry) SenderPubkey() (*taber.Keys, error) {
return taber.FromID(self.SenderID)
}
// This is the header that goes atop a miniLock file after the magic
// bytes. It contains an ephemeral key and a map of recipient "DecryptInfo"
// objects, which are encrypted to the recipients with the ephmeral Key
// to preserve privacy. The recipient(s) must iterate through these with their
// keys until they unlock one successfully; there is no indication at this
// level who the sender or recipients are, by design.
type miniLockv1Header struct {
Version int `json:"version"`
Ephemeral []byte `json:"ephemeral"`
DecryptInfo map[string][]byte `json:"decryptInfo"`
}
// Keygens a new ephemeral key, returns the header plus this key.
func prepareNewHeader() (*miniLockv1Header, *taber.Keys, error) {
hdr := new(miniLockv1Header)
hdr.Version = 1
ephem, err := taber.RandomKey()
if err != nil {
return nil, nil, err
}
hdr.Ephemeral = ephem.Public
hdr.DecryptInfo = make(map[string][]byte)
return hdr, ephem, nil
}
// Header data is pretty constant, so should be possible to predict length based
// on number of entries in DecryptInfo map!
// URGENT TODO: Refactor to do things intelligently, this is just a placeholder.
func (self *miniLockv1Header) encodedLength() int {
// Get minified JSON header and length.
enc_header, err := json.Marshal(self)
if err != nil {
return 0
}
return len(enc_header)
}
// Encode 'miniLock<int32 LE header length prefix><header JSON>' into "into",
// return "into" (in case of reallocations)
func (self *miniLockv1Header) stuffSelf(into []byte) ([]byte, error) {
// Get minified JSON header and length.
enc_header, err := json.Marshal(self)
if err != nil {
return nil, err
}
hdrLength := len(enc_header)
hdrLengthLE, err := toLittleEndian(int32(hdrLength))
if err != nil {
return nil, err
}
into = append(into, []byte("miniLock")...)
into = append(into, hdrLengthLE...)
into = append(into, enc_header...)
return into, nil
}