Skip to content

Commit

Permalink
fix: senderHmac parameter in MessageV2Builder (#199)
Browse files Browse the repository at this point in the history
* fix: senderHmac parameter in MessageV2Builder

* add senderHmacGenerated to tamperedMessage in ConversationTest
  • Loading branch information
Ezequiel Leanes authored Mar 12, 2024
1 parent 3ce9a4c commit 0e1e0bc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.xmtp.android.library
import androidx.test.ext.junit.runners.AndroidJUnit4
import app.cash.turbine.test
import com.google.protobuf.kotlin.toByteString
import com.google.protobuf.kotlin.toByteStringUtf8
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Assert
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -228,12 +229,21 @@ class ConversationTest {
signedBytes,
additionalData = headerBytes,
)
val thirtyDayPeriodsSinceEpoch =
(Date().time / 1000 / 60 / 60 / 24 / 30).toInt()
val info = "$thirtyDayPeriodsSinceEpoch-${aliceClient.address}"
val infoEncoded = info.toByteStringUtf8().toByteArray()
val senderHmacGenerated =
Crypto.calculateMac(
Crypto.deriveKey(aliceConversation.keyMaterial!!, ByteArray(0), infoEncoded),
headerBytes
)
val tamperedMessage =
MessageV2Builder.buildFromCipherText(
headerBytes = headerBytes,
ciphertext = ciphertext,
senderHmac = null,
shouldPush = true,
senderHmac = senderHmacGenerated,
shouldPush = codec.shouldPush("this is a fake"),
)
val tamperedEnvelope = EnvelopeBuilder.buildFromString(
topic = aliceConversation.topic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class MessageV2Builder(val senderHmac: ByteArray? = null, val shouldPush: Boolea
fun buildFromCipherText(
headerBytes: ByteArray,
ciphertext: CipherText?,
senderHmac: ByteArray?,
senderHmac: ByteArray,
shouldPush: Boolean,
): MessageV2Builder {
val messageBuilder = MessageV2Builder(senderHmac = senderHmac, shouldPush = shouldPush)
messageBuilder.messageV2 = MessageV2.newBuilder().also {
it.headerBytes = headerBytes.toByteString()
it.ciphertext = ciphertext
it.shouldPush = shouldPush
it.senderHmac = senderHmac.toByteString()
}.build()
return messageBuilder
}
Expand Down

0 comments on commit 0e1e0bc

Please sign in to comment.