mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-27 18:46:39 +02:00
Merge expanding and checking Hmac functions to avoid re-creating the Mac instance.
This commit is contained in:
@@ -46,7 +46,15 @@ class HkdfBenchmark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun hkdfExpand() {
|
fun hkdfExpandWithCheck() {
|
||||||
|
val hkdf = Hkdf()
|
||||||
|
benchmarkRule.measureRepeated {
|
||||||
|
hkdf.fastExpand(sharedKey, encrypted.nonce, encrypted.ciphertext, encrypted.mac)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun hkdfFastExpand() {
|
||||||
val hkdf = Hkdf()
|
val hkdf = Hkdf()
|
||||||
benchmarkRule.measureRepeated {
|
benchmarkRule.measureRepeated {
|
||||||
hkdf.fastExpand(sharedKey, encrypted.nonce)
|
hkdf.fastExpand(sharedKey, encrypted.nonce)
|
||||||
|
@@ -113,12 +113,14 @@ class Nip44v2 {
|
|||||||
decoded: EncryptedInfo,
|
decoded: EncryptedInfo,
|
||||||
conversationKey: ByteArray,
|
conversationKey: ByteArray,
|
||||||
): String {
|
): String {
|
||||||
val messageKey = getMessageKeys(conversationKey, decoded.nonce)
|
val messageKey = checkMessageKeys(conversationKey, decoded)
|
||||||
|
|
||||||
checkHMacAad(messageKey, decoded)
|
|
||||||
|
|
||||||
return unpad(
|
return unpad(
|
||||||
chaCha.decrypt(decoded.ciphertext, messageKey.chachaNonce, messageKey.chachaKey),
|
chaCha.decrypt(
|
||||||
|
decoded.ciphertext,
|
||||||
|
messageKey.chachaNonce,
|
||||||
|
messageKey.chachaKey,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,6 +212,11 @@ class Nip44v2 {
|
|||||||
nonce: ByteArray,
|
nonce: ByteArray,
|
||||||
): Hkdf.MessageKey = hkdf.fastExpand(conversationKey, nonce)
|
): Hkdf.MessageKey = hkdf.fastExpand(conversationKey, nonce)
|
||||||
|
|
||||||
|
fun checkMessageKeys(
|
||||||
|
conversationKey: ByteArray,
|
||||||
|
decoded: EncryptedInfo,
|
||||||
|
): Hkdf.MessageKey = hkdf.fastExpand(conversationKey, decoded.nonce, decoded.ciphertext, decoded.mac)
|
||||||
|
|
||||||
/** @return 32B shared secret */
|
/** @return 32B shared secret */
|
||||||
fun computeConversationKey(
|
fun computeConversationKey(
|
||||||
privateKey: ByteArray,
|
privateKey: ByteArray,
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.quartz.nip44Encryption.crypto
|
package com.vitorpamplona.quartz.nip44Encryption.crypto
|
||||||
|
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import javax.crypto.Mac
|
import javax.crypto.Mac
|
||||||
import javax.crypto.spec.SecretKeySpec
|
import javax.crypto.spec.SecretKeySpec
|
||||||
@@ -88,6 +89,8 @@ class Hkdf(
|
|||||||
fun fastExpand(
|
fun fastExpand(
|
||||||
key: ByteArray,
|
key: ByteArray,
|
||||||
nonce: ByteArray,
|
nonce: ByteArray,
|
||||||
|
ciphertext: ByteArray? = null,
|
||||||
|
ciphertextMac: ByteArray? = null,
|
||||||
): MessageKey {
|
): MessageKey {
|
||||||
check(key.size == hashLen)
|
check(key.size == hashLen)
|
||||||
check(nonce.size == hashLen)
|
check(nonce.size == hashLen)
|
||||||
@@ -112,10 +115,22 @@ class Hkdf(
|
|||||||
mac.update(3)
|
mac.update(3)
|
||||||
val round3 = mac.doFinal()
|
val round3 = mac.doFinal()
|
||||||
|
|
||||||
val hmacKey = ByteArray(32)
|
val hmacKey = ByteArray(hashLen)
|
||||||
System.arraycopy(round2, 12, hmacKey, 0, 20)
|
System.arraycopy(round2, 12, hmacKey, 0, 20)
|
||||||
System.arraycopy(round3, 0, hmacKey, 20, 12)
|
System.arraycopy(round3, 0, hmacKey, 20, 12)
|
||||||
|
|
||||||
|
// checks the mac here to avoid building a new Mac.getInstance(algorithm)
|
||||||
|
if (ciphertext != null && ciphertextMac != null) {
|
||||||
|
mac.init(FixedKey(hmacKey, algorithm))
|
||||||
|
mac.update(nonce)
|
||||||
|
mac.update(ciphertext)
|
||||||
|
val calculatedMac = mac.doFinal()
|
||||||
|
|
||||||
|
check(calculatedMac.contentEquals(ciphertextMac)) {
|
||||||
|
"Invalid Mac: Calculated ${calculatedMac.toHexKey()}, decoded: ${ciphertextMac.toHexKey()}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return MessageKey(
|
return MessageKey(
|
||||||
chachaKey = round1,
|
chachaKey = round1,
|
||||||
chachaNonce = round2.copyOfRange(0, 12),
|
chachaNonce = round2.copyOfRange(0, 12),
|
||||||
|
Reference in New Issue
Block a user