Skip to content

Commit c19a5bf

Browse files
committed
Merge branch 3x, in order to extract length and algorithm decisions for signing keys, hashes and macs
1 parent 2eea497 commit c19a5bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+750
-1358
lines changed

ake.go

+43-28
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package otr3
22

33
import (
44
"crypto/hmac"
5-
"crypto/sha256"
65
"crypto/subtle"
76
"io"
87
"math/big"
@@ -13,10 +12,13 @@ type ake struct {
1312
ourPublicValue *big.Int
1413
theirPublicValue *big.Int
1514

15+
// TODO: why this number here?
1616
r [16]byte
1717

1818
encryptedGx []byte
19-
hashedGx [sha256.Size]byte
19+
20+
// SIZE: this should always be version.hash2Length
21+
xhashedGx []byte
2022

2123
revealKey akeKeys
2224
sigKey akeKeys
@@ -40,7 +42,7 @@ func (c *Conversation) initAKE() {
4042
}
4143

4244
func (c *Conversation) calcAKEKeys(s *big.Int) {
43-
c.ssid, c.ake.revealKey, c.ake.sigKey = calculateAKEKeys(s)
45+
c.ssid, c.ake.revealKey, c.ake.sigKey = calculateAKEKeys(s, c.version)
4446
}
4547

4648
func (c *Conversation) setSecretExponent(val *big.Int) {
@@ -53,9 +55,9 @@ func (c *Conversation) calcDHSharedSecret() *big.Int {
5355
}
5456

5557
func (c *Conversation) generateEncryptedSignature(key *akeKeys) ([]byte, error) {
56-
verifyData := appendAll(c.ake.ourPublicValue, c.ake.theirPublicValue, &c.ourKey.PublicKey, c.ake.keys.ourKeyID)
58+
verifyData := appendAll(c.ake.ourPublicValue, c.ake.theirPublicValue, c.ourCurrentKey.PublicKey(), c.ake.keys.ourKeyID)
5759

58-
mb := sumHMAC(key.m1[:], verifyData)
60+
mb := sumHMAC(key.m1, verifyData, c.version)
5961
xb, err := c.calcXb(key, mb)
6062

6163
if err != nil {
@@ -64,15 +66,24 @@ func (c *Conversation) generateEncryptedSignature(key *akeKeys) ([]byte, error)
6466

6567
return appendData(nil, xb), nil
6668
}
67-
func appendAll(one, two *big.Int, publicKey *PublicKey, keyID uint32) []byte {
69+
func appendAll(one, two *big.Int, publicKey PublicKey, keyID uint32) []byte {
6870
return appendWord(append(appendMPI(appendMPI(nil, one), two), publicKey.serialize()...), keyID)
6971
}
7072

73+
func fixedSize(s int, v []byte) []byte {
74+
if len(v) < s {
75+
vv := make([]byte, s)
76+
copy(vv, v)
77+
return vv
78+
}
79+
return v
80+
}
81+
7182
func (c *Conversation) calcXb(key *akeKeys, mb []byte) ([]byte, error) {
72-
xb := c.ourKey.PublicKey.serialize()
83+
xb := c.ourCurrentKey.PublicKey().serialize()
7384
xb = appendWord(xb, c.ake.keys.ourKeyID)
7485

75-
sigb, err := c.ourKey.Sign(c.rand(), mb)
86+
sigb, err := c.ourCurrentKey.Sign(c.rand(), mb)
7687
if err == io.ErrUnexpectedEOF {
7788
return nil, errShortRandomRead
7889
}
@@ -82,7 +93,7 @@ func (c *Conversation) calcXb(key *akeKeys, mb []byte) ([]byte, error) {
8293
}
8394

8495
// this error can't happen, since key.c is fixed to the correct size
85-
xb, _ = encrypt(key.c[:], append(xb, sigb...))
96+
xb, _ = encrypt(fixedSize(c.version.keyLength(), key.c), append(xb, sigb...))
8697

8798
return xb, nil
8899
}
@@ -93,6 +104,7 @@ func (c *Conversation) dhCommitMessage() ([]byte, error) {
93104
c.initAKE()
94105
c.ake.keys.ourKeyID = 0
95106

107+
// TODO: where does this 40 come from?
96108
x, err := c.randMPI(make([]byte, 40))
97109
if err != nil {
98110
return nil, err
@@ -113,7 +125,7 @@ func (c *Conversation) dhCommitMessage() ([]byte, error) {
113125
func (c *Conversation) serializeDHCommit(public *big.Int) []byte {
114126
dhCommitMsg := dhCommit{
115127
encryptedGx: c.ake.encryptedGx,
116-
hashedGx: sha256.Sum256(appendMPI(nil, public)),
128+
yhashedGx: c.version.hash2(appendMPI(nil, public)),
117129
}
118130
return dhCommitMsg.serialize()
119131
}
@@ -123,6 +135,7 @@ func (c *Conversation) serializeDHCommit(public *big.Int) []byte {
123135
func (c *Conversation) dhKeyMessage() ([]byte, error) {
124136
c.initAKE()
125137

138+
// TODO: where does this 40 come from?
126139
y, err := c.randMPI(make([]byte, 40)[:])
127140

128141
if err != nil {
@@ -154,14 +167,14 @@ func (c *Conversation) revealSigMessage() ([]byte, error) {
154167
return nil, err
155168
}
156169

157-
macSig := sumHMAC(c.ake.revealKey.m2[:], encryptedSig)
170+
macSig := sumHMAC(c.ake.revealKey.m2, encryptedSig, c.version)
158171
revealSigMsg := revealSig{
159172
r: c.ake.r,
160173
encryptedSig: encryptedSig,
161174
macSig: macSig,
162175
}
163176

164-
return revealSigMsg.serialize(), nil
177+
return revealSigMsg.serialize(c.version), nil
165178
}
166179

167180
// sigMessage = alice = y
@@ -174,13 +187,13 @@ func (c *Conversation) sigMessage() ([]byte, error) {
174187
return nil, err
175188
}
176189

177-
macSig := sumHMAC(c.ake.sigKey.m2[:], encryptedSig)
190+
macSig := sumHMAC(c.ake.sigKey.m2, encryptedSig, c.version)
178191
sigMsg := sig{
179192
encryptedSig: encryptedSig,
180193
macSig: macSig,
181194
}
182195

183-
return sigMsg.serialize(), nil
196+
return sigMsg.serialize(c.version), nil
184197
}
185198

186199
// processDHCommit = alice = y
@@ -193,7 +206,7 @@ func (c *Conversation) processDHCommit(msg []byte) error {
193206
}
194207

195208
c.ake.encryptedGx = dhCommitMsg.encryptedGx
196-
c.ake.hashedGx = dhCommitMsg.hashedGx
209+
c.ake.xhashedGx = dhCommitMsg.yhashedGx
197210

198211
return err
199212
}
@@ -225,7 +238,7 @@ func (c *Conversation) processDHKey(msg []byte) (isSame bool, err error) {
225238
// Bob ---- Reveal Signature ----> Alice
226239
func (c *Conversation) processRevealSig(msg []byte) (err error) {
227240
revealSigMsg := revealSig{}
228-
err = revealSigMsg.deserialize(msg)
241+
err = revealSigMsg.deserialize(msg, c.version)
229242
if err != nil {
230243
return
231244
}
@@ -239,7 +252,7 @@ func (c *Conversation) processRevealSig(msg []byte) (err error) {
239252
return
240253
}
241254

242-
if err = checkDecryptedGx(decryptedGx, c.ake.hashedGx[:]); err != nil {
255+
if err = checkDecryptedGx(decryptedGx, c.ake.xhashedGx, c.version); err != nil {
243256
return
244257
}
245258

@@ -287,9 +300,10 @@ func (c *Conversation) checkedSignatureVerification(mb, sig []byte) error {
287300
return nil
288301
}
289302

290-
func verifyEncryptedSignatureMAC(encryptedSig []byte, theirMAC []byte, keys *akeKeys) error {
303+
func verifyEncryptedSignatureMAC(encryptedSig []byte, theirMAC []byte, keys *akeKeys, v otrVersion) error {
291304
tomac := appendData(nil, encryptedSig)
292-
myMAC := sumHMAC(keys.m2[:], tomac)[:20]
305+
306+
myMAC := sumHMAC(keys.m2, tomac, v)[:v.truncateLength()]
293307

294308
if len(myMAC) != len(theirMAC) || subtle.ConstantTimeCompare(myMAC, theirMAC) == 0 {
295309
return newOtrError("bad signature MAC in encrypted signature")
@@ -299,8 +313,9 @@ func verifyEncryptedSignatureMAC(encryptedSig []byte, theirMAC []byte, keys *ake
299313
}
300314

301315
func (c *Conversation) parseTheirKey(key []byte) (sig []byte, keyID uint32, err error) {
302-
c.theirKey = &PublicKey{}
303-
rest, ok1 := c.theirKey.Parse(key)
316+
var rest []byte
317+
var ok1 bool
318+
rest, ok1, c.theirKey = ParsePublicKey(key)
304319
sig, keyID, ok2 := extractWord(rest)
305320

306321
if !ok1 || !ok2 {
@@ -312,16 +327,16 @@ func (c *Conversation) parseTheirKey(key []byte) (sig []byte, keyID uint32, err
312327

313328
func (c *Conversation) expectedMessageHMAC(keyID uint32, keys *akeKeys) []byte {
314329
verifyData := appendAll(c.ake.theirPublicValue, c.ake.ourPublicValue, c.theirKey, keyID)
315-
return sumHMAC(keys.m1[:], verifyData)
330+
return sumHMAC(keys.m1, verifyData, c.version)
316331
}
317332

318333
func (c *Conversation) processEncryptedSig(encryptedSig []byte, theirMAC []byte, keys *akeKeys) error {
319-
if err := verifyEncryptedSignatureMAC(encryptedSig, theirMAC, keys); err != nil {
334+
if err := verifyEncryptedSignatureMAC(encryptedSig, theirMAC, keys, c.version); err != nil {
320335
return err
321336
}
322337

323338
decryptedSig := encryptedSig
324-
if err := decrypt(keys.c[:], decryptedSig, encryptedSig); err != nil {
339+
if err := decrypt(fixedSize(c.version.keyLength(), keys.c), decryptedSig, encryptedSig); err != nil {
325340
return err
326341
}
327342

@@ -353,14 +368,14 @@ func extractGx(decryptedGx []byte) (*big.Int, error) {
353368
return gx, nil
354369
}
355370

356-
func sumHMAC(key, data []byte) []byte {
357-
mac := hmac.New(sha256.New, key)
371+
func sumHMAC(key, data []byte, v otrVersion) []byte {
372+
mac := hmac.New(v.hash2Instance, key)
358373
mac.Write(data)
359374
return mac.Sum(nil)
360375
}
361376

362-
func checkDecryptedGx(decryptedGx, hashedGx []byte) error {
363-
digest := sha256.Sum256(decryptedGx)
377+
func checkDecryptedGx(decryptedGx, hashedGx []byte, v otrVersion) error {
378+
digest := v.hash2(decryptedGx)
364379

365380
if subtle.ConstantTimeCompare(digest[:], hashedGx[:]) == 0 {
366381
return newOtrError("bad commit MAC in reveal signature message")

0 commit comments

Comments
 (0)