diff --git a/otr/otr.go b/otr/otr.go index 07ac080..0d18a60 100644 --- a/otr/otr.go +++ b/otr/otr.go @@ -848,7 +848,6 @@ func (c *Conversation) rotateDHKeys() { slot := &c.keySlots[i] if slot.used && slot.myKeyId == c.myKeyId-1 { slot.used = false - c.oldMACs = append(c.oldMACs, slot.sendMACKey...) c.oldMACs = append(c.oldMACs, slot.recvMACKey...) } } @@ -924,7 +923,6 @@ func (c *Conversation) processData(in []byte) (out []byte, tlvs []tlv, err error slot := &c.keySlots[i] if slot.used && slot.theirKeyId == theirKeyId-1 { slot.used = false - c.oldMACs = append(c.oldMACs, slot.sendMACKey...) c.oldMACs = append(c.oldMACs, slot.recvMACKey...) } } @@ -1096,6 +1094,10 @@ func (c *Conversation) calcDataKeys(myKeyId, theirKeyId uint32) (slot *keySlot, h.Write(slot.recvAESKey) slot.recvMACKey = h.Sum(slot.recvMACKey[:0]) + slot.theirKeyId = theirKeyId + slot.myKeyId = myKeyId + slot.used = true + zero(slot.theirLastCtr[:]) return } diff --git a/otr/otr_test.go b/otr/otr_test.go index 1e2bbfc..417a793 100644 --- a/otr/otr_test.go +++ b/otr/otr_test.go @@ -194,23 +194,75 @@ func TestConversation(t *testing.T) { t.Error("Bob doesn't believe that the conversation is secure") } - var testMessage = []byte("hello Bob") - alicesMessage, err = alice.Send(testMessage) - for i, msg := range alicesMessage { - out, encrypted, _, _, err := bob.Receive(msg) - if err != nil { - t.Errorf("Error generated while processing test message: %s", err.Error()) + var testMessages = [][]byte{ + []byte("hello"), []byte("bye"), + } + + for j, testMessage := range testMessages { + alicesMessage, err = alice.Send(testMessage) + + if len(alice.oldMACs) != 0 { + t.Errorf("Alice has not revealed all MAC keys") } - if len(out) > 0 { - if i != len(alicesMessage)-1 { - t.Fatal("Bob produced a message while processing a fragment of Alice's") + + for i, msg := range alicesMessage { + out, encrypted, _, _, err := bob.Receive(msg) + + if err != nil { + t.Errorf("Error generated while processing test message: %s", err.Error()) } - if !encrypted { - t.Errorf("Message was not marked as encrypted") + if len(out) > 0 { + if i != len(alicesMessage)-1 { + t.Fatal("Bob produced a message while processing a fragment of Alice's") + } + if !encrypted { + t.Errorf("Message was not marked as encrypted") + } + if !bytes.Equal(out, testMessage) { + t.Errorf("Message corrupted: got %x, want %x", out, testMessage) + } } - if !bytes.Equal(out, testMessage) { - t.Errorf("Message corrupted: got %x, want %x", out, testMessage) + } + + if j == 0 { + if len(bob.oldMACs) != 0 { + t.Errorf("Bob should not have MAC keys to reveal") } + } else if len(bob.oldMACs) != 40 { + t.Errorf("Bob does not have MAC keys to reveal") + } + + bobsMessage, err = bob.Send(testMessage) + + if len(bob.oldMACs) != 0 { + t.Errorf("Bob has not revealed all MAC keys") + } + + for i, msg := range bobsMessage { + out, encrypted, _, _, err := alice.Receive(msg) + + if err != nil { + t.Errorf("Error generated while processing test message: %s", err.Error()) + } + if len(out) > 0 { + if i != len(bobsMessage)-1 { + t.Fatal("Alice produced a message while processing a fragment of Bob's") + } + if !encrypted { + t.Errorf("Message was not marked as encrypted") + } + if !bytes.Equal(out, testMessage) { + t.Errorf("Message corrupted: got %x, want %x", out, testMessage) + } + } + } + + if j == 0 { + if len(alice.oldMACs) != 20 { + t.Errorf("Alice does not have MAC keys to reveal") + } + } else if len(alice.oldMACs) != 40 { + t.Errorf("Alice does not have MAC keys to reveal") } } }