Skip to content

Commit

Permalink
Set resetOnDoFinal to false (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Feb 5, 2025
1 parent 56a1dcb commit 15e36f3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public sealed class BLAKE2Mac: Mac {
bitStrength: Int,
personalization: ByteArray?,
factory: DigestFactory,
): super(key) {
): super(key, resetOnDoFinal = false) {
this.bitStrength = bitStrength
this.personalization = personalization?.copyOf()
this.digest = factory.newInstance(
Expand Down Expand Up @@ -103,6 +103,8 @@ public sealed class BLAKE2Mac: Mac {

override fun doFinalInto(dest: ByteArray, destOffset: Int) {
digest.digestInto(dest, destOffset)
// resetOnDoFinal = false
digest.update(keyBlock)
}

override fun reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public abstract class Hmac: Mac {
private val oKey: ByteArray
private val digest: Digest

constructor(key: ByteArray, digest: Digest): super(key) {
constructor(key: ByteArray, digest: Digest): super(key, resetOnDoFinal = false) {
this.digest = digest
this.iKey = ByteArray(digest.blockSize())
this.oKey = ByteArray(digest.blockSize())
Expand Down Expand Up @@ -101,6 +101,7 @@ public abstract class Hmac: Mac {
digest.update(oKey)
digest.update(inner)
digest.digestInto(dest, destOffset)
digest.update(iKey)
}

override fun reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {
override fun doFinalInto(dest: ByteArray, destOffset: Int) {
padFinal()
source.digestInto(dest, destOffset)
bytepad()
}

override fun reset() { source.reset(); bytepad() }
}

private class XofEngine: Engine {
Expand Down Expand Up @@ -171,6 +174,8 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {
// Never called. outputLength is 0, so.
override fun doFinal(): ByteArray = ZERO_BYTES

override fun reset() { source.reset(); bytepad() }

private companion object { private val ZERO_BYTES = ByteArray(0) }
}

Expand All @@ -183,7 +188,11 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {
private var initBlock: ByteArray
private val outputLength: Int

constructor(key: ByteArray, bitStrength: Int, outputLength: Int): super(key) {
constructor(
key: ByteArray,
bitStrength: Int,
outputLength: Int,
): super(key, resetOnDoFinal = false) {
this.bitStrength = bitStrength
this.outputLength = outputLength

Expand Down Expand Up @@ -227,7 +236,7 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {

final override fun update(input: Byte) { source.update(input) }
final override fun update(input: ByteArray, offset: Int, len: Int) { source.update(input, offset, len) }
final override fun reset() { (source as Resettable).reset(); bytepad() }

final override fun reset(newKey: ByteArray) {
val oldInitBlock = this.initBlock
initBlock = newInitBlock(newKey, blockSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ abstract class MacUnitTest {
val actual = mac.doFinal().hex()
assertEquals(empty, actual)
assertEquals(expectedResetSmallHash, actual)
assertEquals(expectedResetSmallHash, mac.doFinal().hex())

updateSmall(mac)
mac.reset()
Expand All @@ -100,6 +101,7 @@ abstract class MacUnitTest {
val actual = mac.doFinal().hex()
assertEquals(empty, actual)
assertEquals(expectedResetMediumHash, actual)
assertEquals(expectedResetMediumHash, mac.doFinal().hex())

updateSmall(mac)
mac.reset()
Expand All @@ -120,6 +122,7 @@ abstract class MacUnitTest {
val actual = mac.doFinal().hex()
assertEquals(empty, actual)
assertEquals(expectedResetLargeHash, actual)
assertEquals(expectedResetLargeHash, mac.doFinal().hex())

updateSmall(mac)
mac.reset()
Expand Down

0 comments on commit 15e36f3

Please sign in to comment.