From 3ce9a4cf8a493ed88cf66995198dfd0608120f14 Mon Sep 17 00:00:00 2001 From: Alex Risch Date: Mon, 11 Mar 2024 15:06:49 -0600 Subject: [PATCH] fix: Hmac Fixes (#196) Updated handling of hmac to use hkdf keys Co-authored-by: Alex Risch --- .../main/java/org/xmtp/android/library/Conversations.kt | 3 +-- library/src/main/java/org/xmtp/android/library/Crypto.kt | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/library/src/main/java/org/xmtp/android/library/Conversations.kt b/library/src/main/java/org/xmtp/android/library/Conversations.kt index b60f2b1ca..6d1b773bd 100644 --- a/library/src/main/java/org/xmtp/android/library/Conversations.kt +++ b/library/src/main/java/org/xmtp/android/library/Conversations.kt @@ -2,7 +2,6 @@ package org.xmtp.android.library import android.util.Log import com.google.protobuf.kotlin.toByteString -import com.google.protobuf.kotlin.toByteStringUtf8 import io.grpc.StatusException import kotlinx.coroutines.CancellationException import kotlinx.coroutines.channels.awaitClose @@ -327,7 +326,7 @@ data class Conversations( Crypto.deriveKey( conversation.keyMaterial!!, ByteArray(0), - info.toByteStringUtf8().toByteArray() + info.toByteArray(Charsets.UTF_8), ) val hmacKeyData = HmacKeyData.newBuilder() hmacKeyData.hmacKey = hmacKey.toByteString() diff --git a/library/src/main/java/org/xmtp/android/library/Crypto.kt b/library/src/main/java/org/xmtp/android/library/Crypto.kt index 2160baa91..768f42aa7 100644 --- a/library/src/main/java/org/xmtp/android/library/Crypto.kt +++ b/library/src/main/java/org/xmtp/android/library/Crypto.kt @@ -87,12 +87,7 @@ class Crypto { salt: ByteArray, info: ByteArray, ): ByteArray { - val keySpec = SecretKeySpec(secret, "HmacSHA256") - val hmac = Mac.getInstance("HmacSHA256") - hmac.init(keySpec) - val derivedKey = hmac.doFinal(salt + info) - - return derivedKey.copyOfRange(0, 32) + return Hkdf.computeHkdf("HMACSHA256", secret, salt, info, 32) } fun verifyHmacSignature(