Skip to content

Commit 15fa767

Browse files
committed
Remove reset for hash/signature functions - they should be single-use for now
1 parent 7307cd7 commit 15fa767

File tree

21 files changed

+112
-245
lines changed

21 files changed

+112
-245
lines changed

cryptography-core/api/cryptography-core.api

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,9 @@ public abstract interface class dev/whyoleg/cryptography/functions/HashFunction
652652
public abstract fun hashIntoByteArray ([BI)I
653653
public static synthetic fun hashIntoByteArray$default (Ldev/whyoleg/cryptography/functions/HashFunction;[BIILjava/lang/Object;)I
654654
public abstract fun hashToByteArray ()[B
655-
public abstract fun reset ()V
656655
}
657656

658657
public abstract interface class dev/whyoleg/cryptography/functions/SignFunction : dev/whyoleg/cryptography/functions/UpdateFunction {
659-
public abstract fun reset ()V
660658
public fun sign ()Lkotlinx/io/bytestring/ByteString;
661659
public abstract fun signIntoByteArray ([BI)I
662660
public static synthetic fun signIntoByteArray$default (Ldev/whyoleg/cryptography/functions/SignFunction;[BIILjava/lang/Object;)I
@@ -674,7 +672,6 @@ public abstract interface class dev/whyoleg/cryptography/functions/UpdateFunctio
674672
}
675673

676674
public abstract interface class dev/whyoleg/cryptography/functions/VerifyFunction : dev/whyoleg/cryptography/functions/UpdateFunction {
677-
public abstract fun reset ()V
678675
public fun verify (Lkotlinx/io/bytestring/ByteString;II)Z
679676
public abstract fun verify ([BII)Z
680677
public static synthetic fun verify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)Z

cryptography-core/api/cryptography-core.klib.api

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,10 @@ abstract interface dev.whyoleg.cryptography.algorithms/PBKDF2 : dev.whyoleg.cryp
535535
abstract interface dev.whyoleg.cryptography.functions/HashFunction : dev.whyoleg.cryptography.functions/UpdateFunction { // dev.whyoleg.cryptography.functions/HashFunction|null[0]
536536
abstract fun hashIntoByteArray(kotlin/ByteArray, kotlin/Int = ...): kotlin/Int // dev.whyoleg.cryptography.functions/HashFunction.hashIntoByteArray|hashIntoByteArray(kotlin.ByteArray;kotlin.Int){}[0]
537537
abstract fun hashToByteArray(): kotlin/ByteArray // dev.whyoleg.cryptography.functions/HashFunction.hashToByteArray|hashToByteArray(){}[0]
538-
abstract fun reset() // dev.whyoleg.cryptography.functions/HashFunction.reset|reset(){}[0]
539538
open fun hash(): kotlinx.io.bytestring/ByteString // dev.whyoleg.cryptography.functions/HashFunction.hash|hash(){}[0]
540539
}
541540

542541
abstract interface dev.whyoleg.cryptography.functions/SignFunction : dev.whyoleg.cryptography.functions/UpdateFunction { // dev.whyoleg.cryptography.functions/SignFunction|null[0]
543-
abstract fun reset() // dev.whyoleg.cryptography.functions/SignFunction.reset|reset(){}[0]
544542
abstract fun signIntoByteArray(kotlin/ByteArray, kotlin/Int = ...): kotlin/Int // dev.whyoleg.cryptography.functions/SignFunction.signIntoByteArray|signIntoByteArray(kotlin.ByteArray;kotlin.Int){}[0]
545543
abstract fun signToByteArray(): kotlin/ByteArray // dev.whyoleg.cryptography.functions/SignFunction.signToByteArray|signToByteArray(){}[0]
546544
open fun sign(): kotlinx.io.bytestring/ByteString // dev.whyoleg.cryptography.functions/SignFunction.sign|sign(){}[0]
@@ -555,7 +553,6 @@ abstract interface dev.whyoleg.cryptography.functions/UpdateFunction : kotlin/Au
555553
}
556554

557555
abstract interface dev.whyoleg.cryptography.functions/VerifyFunction : dev.whyoleg.cryptography.functions/UpdateFunction { // dev.whyoleg.cryptography.functions/VerifyFunction|null[0]
558-
abstract fun reset() // dev.whyoleg.cryptography.functions/VerifyFunction.reset|reset(){}[0]
559556
abstract fun verify(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
560557
open fun verify(kotlinx.io.bytestring/ByteString, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlinx.io.bytestring.ByteString;kotlin.Int;kotlin.Int){}[0]
561558
}

cryptography-core/src/commonMain/kotlin/functions/HashFunction.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public interface HashFunction : UpdateFunction {
1111
public fun hashIntoByteArray(destination: ByteArray, destinationOffset: Int = 0): Int
1212
public fun hashToByteArray(): ByteArray
1313
public fun hash(): ByteString = hashToByteArray().asByteString()
14-
public fun reset()
1514
}

cryptography-core/src/commonMain/kotlin/functions/SignFunction.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ public interface SignFunction : UpdateFunction {
1111
public fun signIntoByteArray(destination: ByteArray, destinationOffset: Int = 0): Int
1212
public fun signToByteArray(): ByteArray
1313
public fun sign(): ByteString = signToByteArray().asByteString()
14-
public fun reset()
1514
}

cryptography-core/src/commonMain/kotlin/functions/VerifyFunction.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import kotlinx.io.bytestring.*
99

1010
public interface VerifyFunction : UpdateFunction {
1111
public fun verify(signature: ByteArray, startIndex: Int = 0, endIndex: Int = signature.size): Boolean
12-
public fun verify(signature: ByteString, startIndex: Int = 0, endIndex: Int = signature.size): Boolean =
13-
verify(signature.asByteArray(), startIndex, endIndex)
14-
15-
public fun reset()
12+
public fun verify(signature: ByteString, startIndex: Int = 0, endIndex: Int = signature.size): Boolean {
13+
return verify(signature.asByteArray(), startIndex, endIndex)
14+
}
1615
}

cryptography-providers-tests/src/commonMain/kotlin/default/DigestTest.kt

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,6 @@ abstract class DigestTest(provider: CryptographyProvider) : ProviderTest(provide
9191
}
9292
}
9393

94-
@Test
95-
fun testFunctionReuse() = testAlgorithm(SHA256) {
96-
if (!supportsFunctions()) return@testAlgorithm
97-
98-
val hasher = algorithm.hasher()
99-
val bytes1 = ByteString(CryptographyRandom.nextBytes(10000))
100-
val bytes2 = ByteString(CryptographyRandom.nextBytes(10000))
101-
102-
val digest1 = hasher.hash(bytes1)
103-
val digest2 = hasher.hash(bytes2)
104-
hasher.createHashFunction().use { function ->
105-
function.update(bytes1)
106-
assertContentEquals(digest1, function.hash())
107-
108-
function.update(bytes2)
109-
assertContentEquals(digest2, function.hash())
110-
111-
// update and then discard
112-
function.update(bytes1)
113-
function.update(bytes1)
114-
function.reset()
115-
// update after reset
116-
function.update(bytes1)
117-
assertContentEquals(digest1, function.hash())
118-
}
119-
}
120-
12194
@Test
12295
fun testFunctionSource() = testAlgorithm(SHA256) {
12396
val hasher = algorithm.hasher()

cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCDigest.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ internal class CCDigest<CTX : CPointed>(
1616
override val id: CryptographyAlgorithmId<Digest>,
1717
) : Hasher, Digest {
1818
override fun hasher(): Hasher = this
19-
override fun createHashFunction(): HashFunction = CCHashFunction(
20-
algorithm = hashAlgorithm,
21-
context = Resource(hashAlgorithm.alloc(), nativeHeap::free)
22-
)
19+
override fun createHashFunction(): HashFunction {
20+
val context = hashAlgorithm.alloc()
21+
// TODO: error handle
22+
hashAlgorithm.ccInit(context)
23+
return CCHashFunction(
24+
algorithm = hashAlgorithm,
25+
context = Resource(context, nativeHeap::free)
26+
)
27+
}
2328
}
2429

2530
private class CCHashFunction<CTX : CPointed>(
2631
private val algorithm: CCHashAlgorithm<CTX>,
2732
private val context: Resource<CPointer<CTX>>,
2833
) : HashFunction, SafeCloseable(SafeCloseAction(context, AutoCloseable::close)) {
29-
init {
30-
reset()
31-
}
32-
3334
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
3435
checkBounds(source.size, startIndex, endIndex)
3536

@@ -46,7 +47,7 @@ private class CCHashFunction<CTX : CPointed>(
4647
destination.usePinned {
4748
check(algorithm.ccFinal(context, it.safeAddressOf(destinationOffset).reinterpret()) > 0)
4849
}
49-
reset()
50+
close()
5051
return algorithm.digestSize
5152
}
5253

@@ -55,9 +56,4 @@ private class CCHashFunction<CTX : CPointed>(
5556
hashIntoByteArray(output)
5657
return output
5758
}
58-
59-
override fun reset() {
60-
val context = context.access()
61-
check(algorithm.ccInit(context) > 0)
62-
}
6359
}

cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCHmac.kt

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,25 @@ private class HmacSignature(
8383
private val key: ByteArray,
8484
private val digestSize: Int,
8585
) : SignatureGenerator, SignatureVerifier {
86-
private fun createFunction() = HmacFunction(
87-
hmacAlgorithm = hmacAlgorithm,
88-
key = key,
89-
digestSize = digestSize,
90-
context = Resource(nativeHeap.alloc<CCHmacContext>().ptr, nativeHeap::free)
91-
)
86+
87+
@OptIn(UnsafeNumber::class)
88+
private fun createFunction(): HmacFunction {
89+
val context = nativeHeap.alloc<CCHmacContext>().ptr
90+
// TODO: error handle?
91+
key.usePinned {
92+
CCHmacInit(context, hmacAlgorithm, it.safeAddressOf(0), key.size.convert())
93+
}
94+
return HmacFunction(digestSize, Resource(context, nativeHeap::free))
95+
}
9296

9397
override fun createSignFunction(): SignFunction = createFunction()
9498
override fun createVerifyFunction(): VerifyFunction = createFunction()
9599
}
96100

97101
private class HmacFunction(
98-
private val hmacAlgorithm: CCHmacAlgorithm,
99-
private val key: ByteArray,
100102
private val digestSize: Int,
101103
private val context: Resource<CPointer<CCHmacContext>>,
102104
) : SignFunction, VerifyFunction, SafeCloseable(SafeCloseAction(context, AutoCloseable::close)) {
103-
init {
104-
reset()
105-
}
106105

107106
@OptIn(UnsafeNumber::class)
108107
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
@@ -121,7 +120,7 @@ private class HmacFunction(
121120
destination.usePinned {
122121
CCHmacFinal(context, it.safeAddressOf(destinationOffset))
123122
}
124-
reset()
123+
close()
125124
return digestSize
126125
}
127126

@@ -135,12 +134,4 @@ private class HmacFunction(
135134
checkBounds(signature.size, startIndex, endIndex)
136135
return signToByteArray().contentEquals(signature.copyOfRange(startIndex, endIndex))
137136
}
138-
139-
@OptIn(UnsafeNumber::class)
140-
override fun reset() {
141-
val context = context.access()
142-
key.usePinned {
143-
CCHmacInit(context, hmacAlgorithm, it.safeAddressOf(0), key.size.convert())
144-
}
145-
}
146137
}

cryptography-providers/apple/src/commonMain/kotlin/algorithms/SecEcdsa.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,6 @@ private class EcdsaRawSignatureGenerator(
278278
return rawSignature
279279
}
280280

281-
override fun reset() {
282-
derSignFunction.reset()
283-
}
284-
285281
override fun close() {
286282
derSignFunction.close()
287283
}
@@ -322,10 +318,6 @@ private class EcdsaRawSignatureVerifier(
322318
return derVerifyFunction.verify(derSignature)
323319
}
324320

325-
override fun reset() {
326-
derVerifyFunction.reset()
327-
}
328-
329321
override fun close() {
330322
derVerifyFunction.close()
331323
}

cryptography-providers/apple/src/commonMain/kotlin/internal/SecSignature.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,13 @@ private class SecVerifyFunction(
6363
result
6464
}
6565
}
66-
}
67-
68-
override fun reset() {
69-
ensureNotClosed()
70-
accumulator = EmptyByteArray
66+
}.also {
67+
close()
7168
}
7269

7370
override fun close() {
7471
isClosed = true
72+
accumulator = EmptyByteArray
7573
}
7674
}
7775

@@ -118,14 +116,12 @@ private class SecSignFunction(
118116

119117
signature.toByteArray()
120118
}
121-
}
122-
123-
override fun reset() {
124-
ensureNotClosed()
125-
accumulator = EmptyByteArray
119+
}.also {
120+
close()
126121
}
127122

128123
override fun close() {
129124
isClosed = true
125+
accumulator = EmptyByteArray
130126
}
131127
}

cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkDigest.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ internal class JdkDigest(
1818
) : Hasher, Digest {
1919
private val messageDigest = state.messageDigest(algorithm)
2020
override fun hasher(): Hasher = this
21-
override fun createHashFunction(): HashFunction = JdkHashFunction(messageDigest.borrowResource())
21+
override fun createHashFunction(): HashFunction = JdkHashFunction(messageDigest.borrowResource().also {
22+
it.access().reset()
23+
})
2224
}
2325

2426
private class JdkHashFunction(private val messageDigest: Pooled.Resource<JMessageDigest>) : HashFunction {
@@ -34,17 +36,12 @@ private class JdkHashFunction(private val messageDigest: Pooled.Resource<JMessag
3436

3537
checkBounds(destination.size, destinationOffset, destinationOffset + messageDigest.digestLength)
3638

37-
return messageDigest.digest(destination, destinationOffset, messageDigest.digestLength)
39+
return messageDigest.digest(destination, destinationOffset, messageDigest.digestLength).also { close() }
3840
}
3941

4042
override fun hashToByteArray(): ByteArray {
4143
val messageDigest = messageDigest.access()
42-
return messageDigest.digest()
43-
}
44-
45-
override fun reset() {
46-
val messageDigest = messageDigest.access()
47-
messageDigest.reset()
44+
return messageDigest.digest().also { close() }
4845
}
4946

5047
override fun close() {

cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkEcdsa.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ private class EcdsaRawSignatureGenerator(
9090
return rawSignature
9191
}
9292

93-
override fun reset() {
94-
derSignFunction.reset()
95-
}
96-
9793
override fun close() {
9894
derSignFunction.close()
9995
}
@@ -134,10 +130,6 @@ private class EcdsaRawSignatureVerifier(
134130
return derVerifyFunction.verify(derSignature)
135131
}
136132

137-
override fun reset() {
138-
derVerifyFunction.reset()
139-
}
140-
141133
override fun close() {
142134
derVerifyFunction.close()
143135
}

cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkMacSignature.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ internal class JdkMacSignature(
2525
override fun createVerifyFunction(): VerifyFunction = createFunction()
2626
}
2727

28-
private class JdkMacFunction(
29-
private val mac: Pooled.Resource<JMac>,
30-
) : SignFunction, VerifyFunction {
28+
private class JdkMacFunction(private val mac: Pooled.Resource<JMac>) : SignFunction, VerifyFunction {
3129
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
3230
checkBounds(source.size, startIndex, endIndex)
3331
val mac = mac.access()
@@ -39,24 +37,20 @@ private class JdkMacFunction(
3937

4038
checkBounds(destination.size, destinationOffset, destinationOffset + mac.macLength)
4139

42-
mac.doFinal(destination, destinationOffset)
40+
mac.doFinal(destination, destinationOffset).also { close() }
4341
return mac.macLength
4442
}
4543

4644
override fun signToByteArray(): ByteArray {
4745
val mac = mac.access()
48-
return mac.doFinal()
46+
return mac.doFinal().also { close() }
4947
}
5048

5149
override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean {
5250
checkBounds(signature.size, startIndex, endIndex)
5351
return signToByteArray().contentEquals(signature.copyOfRange(startIndex, endIndex))
5452
}
5553

56-
override fun reset() {
57-
mac.access().reset()
58-
}
59-
6054
override fun close() {
6155
mac.close()
6256
}

cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkSignatureGenerator.kt

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,14 @@ internal class JdkSignatureGenerator(
1818
private val parameters: AlgorithmParameterSpec?,
1919
) : SignatureGenerator {
2020
private val signature = state.signature(algorithm)
21-
override fun createSignFunction(): SignFunction = JdkSignFunction(state, key, parameters, signature.borrowResource())
21+
override fun createSignFunction(): SignFunction = JdkSignFunction(signature.borrowResource().also {
22+
val jsignature = it.access()
23+
jsignature.initSign(key, state.secureRandom)
24+
parameters?.let(jsignature::setParameter)
25+
})
2226
}
2327

24-
private class JdkSignFunction(
25-
private val state: JdkCryptographyState,
26-
private val key: JPrivateKey,
27-
private val parameters: AlgorithmParameterSpec?,
28-
private val jsignature: Pooled.Resource<JSignature>,
29-
) : SignFunction {
30-
init {
31-
reset()
32-
}
33-
28+
private class JdkSignFunction(private val jsignature: Pooled.Resource<JSignature>) : SignFunction {
3429
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
3530
checkBounds(source.size, startIndex, endIndex)
3631
val jsignature = jsignature.access()
@@ -46,13 +41,7 @@ private class JdkSignFunction(
4641

4742
override fun signToByteArray(): ByteArray {
4843
val jsignature = jsignature.access()
49-
return jsignature.sign()
50-
}
51-
52-
override fun reset() {
53-
val jsignature = jsignature.access()
54-
jsignature.initSign(key, state.secureRandom)
55-
parameters?.let(jsignature::setParameter)
44+
return jsignature.sign().also { close() }
5645
}
5746

5847
override fun close() {

0 commit comments

Comments
 (0)