-
Notifications
You must be signed in to change notification settings - Fork 1
/
nist.go
213 lines (185 loc) · 5.11 KB
/
nist.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package dr
import (
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/hmac"
"errors"
"fmt"
"hash"
"io"
"strconv"
"golang.org/x/crypto/hkdf"
)
// nist implements Ratchet using a NIST curve, 256-bit AES-GCM,
// HKDF and HMAC with the provided hash function.
type nist struct {
// curve is the underlying curve.
curve elliptic.Curve
// hash is the underlying hash
hash func() hash.Hash
// mkInfo is the HKDF info used when deriving message keys.
mkInfo []byte
// rkInfo is the HKDF info used when deriving root keys.
rkInfo []byte
}
var _ Ratchet = (*nist)(nil)
// NIST creates a Ratchet using NIST curves, 256-bit AES-GCM, and
// HKDF and HMAC with the provided hash function.
//
// The namespace is used to bind keys to a particular application
// or context.
func NIST(curve elliptic.Curve, hash func() hash.Hash, namespace string) Ratchet {
return &nist{
curve: curve,
hash: hash,
mkInfo: []byte(namespace + "MessageKeys"),
rkInfo: []byte(namespace + "Ratchet"),
}
}
// byteLen returns the size of the underlying curve in bytes.
func (n *nist) byteLen() int {
return (n.curve.Params().BitSize + 7) / 8
}
// privKeyLen returns the size in bytes of a PrivateKey.
func (n *nist) privKeyLen() int {
// PrivateKey is priv || pub.
return n.byteLen() + n.pubKeyLen()
}
// pubKeyLen returns the size in bytes of a PublicKey.
//
// The public key is in ANSI X9.62 compressed form.
func (n *nist) pubKeyLen() int {
return 1 + n.byteLen()
}
func (n *nist) Generate(r io.Reader) (PrivateKey, error) {
d, x, y, err := elliptic.GenerateKey(n.curve, r)
if err != nil {
return nil, err
}
pub := elliptic.MarshalCompressed(n.curve, x, y)
priv := make(PrivateKey, n.privKeyLen())
m := copy(priv, d)
m += copy(priv[m:], pub)
if m != len(priv) {
panic("dr: key size mismatch")
}
return priv, nil
}
func (n *nist) Public(priv PrivateKey) PublicKey {
if len(priv) != n.privKeyLen() {
panic("dr: invalid private key size: " + strconv.Itoa(len(priv)))
}
pub := make(PublicKey, n.pubKeyLen())
copy(pub, priv[n.byteLen():])
return pub
}
func (n *nist) DH(priv PrivateKey, pub PublicKey) ([]byte, error) {
if len(priv) != n.privKeyLen() {
panic("dr: invalid private key size: " + strconv.Itoa(len(priv)))
}
if len(pub) != n.pubKeyLen() {
panic("dr: invalid public key size: " + strconv.Itoa(len(pub)))
}
x, y := elliptic.UnmarshalCompressed(n.curve, pub)
if x == nil {
return nil, errors.New("dr: invalid public key")
}
k := priv[:n.byteLen()]
secret, _ := n.curve.ScalarMult(x, y, k)
dh := make([]byte, n.byteLen())
secret.FillBytes(dh)
return dh, nil
}
func (n *nist) KDFrk(rk RootKey, dh []byte) (RootKey, ChainKey) {
if len(rk) != 32 {
panic("dr: invalid RootKey size: " + strconv.Itoa(len(rk)))
}
buf := make([]byte, 2*32)
// The Double Ratchet spec says:
//
// as the out of applying a KDF keyed by a 32-byte root
// key rk to a Diffie-Hellman output dh_out
//
// And so at first blush setting IKM=dh, info=rk might seem
// backward since the PRK extracted from the IKM is used to
// key the HMAC used in the expand step. But this is not the
// case, and checking other DR implementations confirms this.
r := hkdf.New(n.hash, dh, rk, n.rkInfo)
_, err := io.ReadFull(r, buf)
if err != nil {
panic(err)
}
return buf[:32:32], buf[32 : 2*32 : 2*32]
}
func (n *nist) KDFck(ck ChainKey) (ChainKey, MessageKey) {
if len(ck) != 32 {
panic("dr: invalid ChainKey size: " + strconv.Itoa(len(ck)))
}
h := hmac.New(n.hash, ck)
const (
ckConst = 0x02
mkConst = 0x01
)
h.Write([]byte{ckConst})
ck = h.Sum(nil)
h.Reset()
h.Write([]byte{mkConst})
mk := h.Sum(nil)
return ck, mk
}
// derive derives a 256-bit AES-GCM key and 96-bit AES-GCM nonce.
func (n *nist) derive(ikm []byte) (key, nonce []byte) {
buf := make([]byte, 32+12)
r := hkdf.New(n.hash, ikm, nil, n.mkInfo)
_, err := io.ReadFull(r, buf)
if err != nil {
panic(err)
}
return buf[0:32:32], buf[32 : 32+12 : 32+12]
}
func (n *nist) Seal(key MessageKey, plaintext, additionalData []byte) []byte {
if len(key) != 32 {
panic("dr: invalid message key size: " + strconv.Itoa(len(key)))
}
key, nonce := n.derive(key)
defer wipe(key)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
aead, err := cipher.NewGCM(block)
if err != nil {
panic(err)
}
return aead.Seal(nil, nonce, plaintext, additionalData)
}
func (n *nist) Open(key MessageKey, ciphertext, additionalData []byte) ([]byte, error) {
if len(key) != 32 {
return nil, fmt.Errorf("dr: invalid message key size: %d", len(key))
}
key, nonce := n.derive(key)
defer wipe(key)
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
aead, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
return aead.Open(nil, nonce, ciphertext, additionalData)
}
func (n *nist) Header(priv PrivateKey, prevChainLength, messageNum int) Header {
if len(priv) != n.privKeyLen() {
panic("dr: invalid key pair size: " + strconv.Itoa(len(priv)))
}
return Header{
PublicKey: n.Public(priv),
PN: prevChainLength,
N: messageNum,
}
}
func (nist) Concat(additionalData []byte, h Header) []byte {
return Concat(additionalData, h)
}