-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
92 lines (70 loc) · 2.16 KB
/
crypto.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
package ikdoc
import (
"github.com/devguardio/identity/go"
"golang.org/x/crypto/hkdf"
"crypto/sha256"
"io"
"golang.org/x/crypto/chacha20poly1305"
"encoding/binary"
"fmt"
)
func Ratchet(doc []byte , chainkey []byte) (sk identity.Secret, ck identity.Secret, prk identity.Secret) {
prkb := hkdf.Extract(sha256.New, doc, chainkey)
if len(prkb) != 32 { panic("expected hkdf.Extract(sha256) to return 32 bytes") }
copy(prk[:], prkb);
hkdf := hkdf.Expand(sha256.New, prk[:], []byte("ikdoc ratchet"))
if _, err := io.ReadFull(hkdf, sk[:]); err != nil {
panic(err)
}
if _, err := io.ReadFull(hkdf, ck[:]); err != nil {
panic(err)
}
return
}
func ResumeRatchetFromString(secret string) (sk identity.Secret, ck identity.Secret, err error) {
rk, err := identity.SecretFromString(secret)
if err != nil { return sk, ck, err }
hkdf := hkdf.Expand(sha256.New, rk[:], []byte("ikdoc ratchet"))
if _, err := io.ReadFull(hkdf, sk[:]); err != nil {
panic(err)
}
if _, err := io.ReadFull(hkdf, ck[:]); err != nil {
panic(err)
}
return
}
func Seal(key []byte, serial uint64, msg []byte) ([]byte, error) {
if serial == 0 {
serial = 1
}
var nonce [12]byte
binary.BigEndian.PutUint64(nonce[4:], serial)
aead, err := chacha20poly1305.New(key[:])
if err != nil { return nil, err }
return aead.Seal(nil, nonce[:], msg, nil), nil
}
func Unseal(key []byte, serial uint64, msg []byte) ([]byte, error) {
if serial == 0 {
serial = 1
}
var nonce [12]byte
binary.BigEndian.PutUint64(nonce[4:], serial)
aead, err := chacha20poly1305.New(key[:])
if err != nil { return nil, err }
if len(msg) < aead.Overhead() {
return nil, fmt.Errorf("ciphertext too short")
}
return aead.Open(nil, nonce[:], msg , nil)
}
/*
import (
"golang.org/x/crypto/chacha20poly1305"
"encoding/binary"
)
func decrypt(k [32]byte, serial uint64, ciphertext []byte) ([]byte, error) {
c, err := chacha20poly1305.New(k[:])
if err != nil { return nil, err }
var nonce [12]byte
binary.LittleEndian.PutUint64(nonce[4:], serial)
}
*/