Skip to content

Commit ce9d304

Browse files
committed
Set resetOnDoFinal to false
1 parent 56a1dcb commit ce9d304

File tree

4 files changed

+19
-4
lines changed
  • library
    • blake2/src/commonMain/kotlin/org/kotlincrypto/macs/blake2
    • hmac/hmac/src/commonMain/kotlin/org/kotlincrypto/macs/hmac
    • kmac/src/commonMain/kotlin/org/kotlincrypto/macs/kmac
  • tools/testing/src/commonMain/kotlin/org/kotlincrypto/macs

4 files changed

+19
-4
lines changed

library/blake2/src/commonMain/kotlin/org/kotlincrypto/macs/blake2/BLAKE2Mac.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public sealed class BLAKE2Mac: Mac {
6767
bitStrength: Int,
6868
personalization: ByteArray?,
6969
factory: DigestFactory,
70-
): super(key) {
70+
): super(key, resetOnDoFinal = false) {
7171
this.bitStrength = bitStrength
7272
this.personalization = personalization?.copyOf()
7373
this.digest = factory.newInstance(
@@ -103,6 +103,8 @@ public sealed class BLAKE2Mac: Mac {
103103

104104
override fun doFinalInto(dest: ByteArray, destOffset: Int) {
105105
digest.digestInto(dest, destOffset)
106+
// resetOnDoFinal = false
107+
digest.update(keyBlock)
106108
}
107109

108110
override fun reset() {

library/hmac/hmac/src/commonMain/kotlin/org/kotlincrypto/macs/hmac/Hmac.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public abstract class Hmac: Mac {
7272
private val oKey: ByteArray
7373
private val digest: Digest
7474

75-
constructor(key: ByteArray, digest: Digest): super(key) {
75+
constructor(key: ByteArray, digest: Digest): super(key, resetOnDoFinal = false) {
7676
this.digest = digest
7777
this.iKey = ByteArray(digest.blockSize())
7878
this.oKey = ByteArray(digest.blockSize())
@@ -101,6 +101,7 @@ public abstract class Hmac: Mac {
101101
digest.update(oKey)
102102
digest.update(inner)
103103
digest.digestInto(dest, destOffset)
104+
digest.update(iKey)
104105
}
105106

106107
override fun reset() {

library/kmac/src/commonMain/kotlin/org/kotlincrypto/macs/kmac/Kmac.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {
124124
override fun doFinalInto(dest: ByteArray, destOffset: Int) {
125125
padFinal()
126126
source.digestInto(dest, destOffset)
127+
bytepad()
127128
}
129+
130+
override fun reset() { source.reset(); bytepad() }
128131
}
129132

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

177+
override fun reset() { source.reset(); bytepad() }
178+
174179
private companion object { private val ZERO_BYTES = ByteArray(0) }
175180
}
176181

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

186-
constructor(key: ByteArray, bitStrength: Int, outputLength: Int): super(key) {
191+
constructor(
192+
key: ByteArray,
193+
bitStrength: Int,
194+
outputLength: Int,
195+
): super(key, resetOnDoFinal = false) {
187196
this.bitStrength = bitStrength
188197
this.outputLength = outputLength
189198

@@ -227,7 +236,7 @@ public sealed class Kmac: Mac, ReKeyableXofAlgorithm {
227236

228237
final override fun update(input: Byte) { source.update(input) }
229238
final override fun update(input: ByteArray, offset: Int, len: Int) { source.update(input, offset, len) }
230-
final override fun reset() { (source as Resettable).reset(); bytepad() }
239+
231240
final override fun reset(newKey: ByteArray) {
232241
val oldInitBlock = this.initBlock
233242
initBlock = newInitBlock(newKey, blockSize)

tools/testing/src/commonMain/kotlin/org/kotlincrypto/macs/MacUnitTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ abstract class MacUnitTest {
8080
val actual = mac.doFinal().hex()
8181
assertEquals(empty, actual)
8282
assertEquals(expectedResetSmallHash, actual)
83+
assertEquals(expectedResetSmallHash, mac.doFinal().hex())
8384

8485
updateSmall(mac)
8586
mac.reset()
@@ -100,6 +101,7 @@ abstract class MacUnitTest {
100101
val actual = mac.doFinal().hex()
101102
assertEquals(empty, actual)
102103
assertEquals(expectedResetMediumHash, actual)
104+
assertEquals(expectedResetMediumHash, mac.doFinal().hex())
103105

104106
updateSmall(mac)
105107
mac.reset()
@@ -120,6 +122,7 @@ abstract class MacUnitTest {
120122
val actual = mac.doFinal().hex()
121123
assertEquals(empty, actual)
122124
assertEquals(expectedResetLargeHash, actual)
125+
assertEquals(expectedResetLargeHash, mac.doFinal().hex())
123126

124127
updateSmall(mac)
125128
mac.reset()

0 commit comments

Comments
 (0)