Skip to content

Commit 95d480e

Browse files
committed
providers/base: add accumulating sign/verify helper functions; update ABI baselines
1 parent 1d0f98f commit 95d480e

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

cryptography-providers/base/api/cryptography-provider-base.api

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ public final class dev/whyoleg/cryptography/providers/base/operations/Accumulati
7777
public fun close ()V
7878
}
7979

80+
public final class dev/whyoleg/cryptography/providers/base/operations/AccumulatingSignFunction : dev/whyoleg/cryptography/operations/SignFunction {
81+
public fun <init> (Lkotlin/jvm/functions/Function1;)V
82+
public fun close ()V
83+
public fun reset ()V
84+
public fun signIntoByteArray ([BI)I
85+
public fun signToByteArray ()[B
86+
public fun update ([BII)V
87+
}
88+
89+
public final class dev/whyoleg/cryptography/providers/base/operations/AccumulatingVerifyFunction : dev/whyoleg/cryptography/operations/VerifyFunction {
90+
public fun <init> (Lkotlin/jvm/functions/Function4;)V
91+
public fun close ()V
92+
public fun reset ()V
93+
public fun tryVerify ([BII)Z
94+
public fun update ([BII)V
95+
public fun verify ([BII)V
96+
}
97+
8098
public abstract interface class dev/whyoleg/cryptography/providers/base/operations/BaseAuthenticatedCipher : dev/whyoleg/cryptography/operations/AuthenticatedCipher, dev/whyoleg/cryptography/providers/base/operations/BaseAuthenticatedDecryptor, dev/whyoleg/cryptography/providers/base/operations/BaseAuthenticatedEncryptor, dev/whyoleg/cryptography/providers/base/operations/BaseCipher {
8199
}
82100

cryptography-providers/base/api/cryptography-provider-base.klib.api

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,26 @@ final class dev.whyoleg.cryptography.providers.base.operations/AccumulatingCiphe
134134
final fun close() // dev.whyoleg.cryptography.providers.base.operations/AccumulatingCipherFunction.close|close(){}[0]
135135
}
136136

137+
final class dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction : dev.whyoleg.cryptography.operations/SignFunction { // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction|null[0]
138+
constructor <init>(kotlin/Function1<kotlin/ByteArray, kotlin/ByteArray>) // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.<init>|<init>(kotlin.Function1<kotlin.ByteArray,kotlin.ByteArray>){}[0]
139+
140+
final fun close() // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.close|close(){}[0]
141+
final fun reset() // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.reset|reset(){}[0]
142+
final fun signIntoByteArray(kotlin/ByteArray, kotlin/Int): kotlin/Int // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.signIntoByteArray|signIntoByteArray(kotlin.ByteArray;kotlin.Int){}[0]
143+
final fun signToByteArray(): kotlin/ByteArray // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.signToByteArray|signToByteArray(){}[0]
144+
final fun update(kotlin/ByteArray, kotlin/Int, kotlin/Int) // dev.whyoleg.cryptography.providers.base.operations/AccumulatingSignFunction.update|update(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
145+
}
146+
147+
final class dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction : dev.whyoleg.cryptography.operations/VerifyFunction { // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction|null[0]
148+
constructor <init>(kotlin/Function4<kotlin/ByteArray, kotlin/ByteArray, kotlin/Int, kotlin/Int, kotlin/Boolean>) // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.<init>|<init>(kotlin.Function4<kotlin.ByteArray,kotlin.ByteArray,kotlin.Int,kotlin.Int,kotlin.Boolean>){}[0]
149+
150+
final fun close() // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.close|close(){}[0]
151+
final fun reset() // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.reset|reset(){}[0]
152+
final fun tryVerify(kotlin/ByteArray, kotlin/Int, kotlin/Int): kotlin/Boolean // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.tryVerify|tryVerify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
153+
final fun update(kotlin/ByteArray, kotlin/Int, kotlin/Int) // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.update|update(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
154+
final fun verify(kotlin/ByteArray, kotlin/Int, kotlin/Int) // dev.whyoleg.cryptography.providers.base.operations/AccumulatingVerifyFunction.verify|verify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
155+
}
156+
137157
final val dev.whyoleg.cryptography.providers.base/EmptyByteArray // dev.whyoleg.cryptography.providers.base/EmptyByteArray|{}EmptyByteArray[0]
138158
final fun <get-EmptyByteArray>(): kotlin/ByteArray // dev.whyoleg.cryptography.providers.base/EmptyByteArray.<get-EmptyByteArray>|<get-EmptyByteArray>(){}[0]
139159

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.whyoleg.cryptography.providers.base.operations
6+
7+
import dev.whyoleg.cryptography.*
8+
import dev.whyoleg.cryptography.operations.*
9+
import dev.whyoleg.cryptography.providers.base.*
10+
11+
@CryptographyProviderApi
12+
public class AccumulatingSignFunction(
13+
private val oneShot: (data: ByteArray) -> ByteArray,
14+
) : SignFunction {
15+
private var closed = false
16+
private val chunks = ArrayList<ByteArray>(4)
17+
18+
private fun ensureOpen() { check(!closed) { "Already closed" } }
19+
20+
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
21+
ensureOpen()
22+
checkBounds(source.size, startIndex, endIndex)
23+
if (startIndex == 0 && endIndex == source.size) chunks += source
24+
else chunks += source.copyOfRange(startIndex, endIndex)
25+
}
26+
27+
override fun signIntoByteArray(destination: ByteArray, destinationOffset: Int): Int {
28+
val sig = signToByteArray()
29+
sig.copyInto(destination, destinationOffset)
30+
return sig.size
31+
}
32+
33+
override fun signToByteArray(): ByteArray {
34+
ensureOpen()
35+
val total = chunks.sumOf { it.size }
36+
val data = ByteArray(total)
37+
var off = 0
38+
chunks.forEach { arr -> arr.copyInto(data, off); off += arr.size }
39+
val out = oneShot(data)
40+
reset()
41+
return out
42+
}
43+
44+
override fun reset() {
45+
ensureOpen()
46+
chunks.clear()
47+
}
48+
49+
override fun close() {
50+
closed = true
51+
chunks.clear()
52+
}
53+
}
54+
55+
@CryptographyProviderApi
56+
public class AccumulatingVerifyFunction(
57+
private val oneShot: (data: ByteArray, signature: ByteArray, startIndex: Int, endIndex: Int) -> Boolean,
58+
) : VerifyFunction {
59+
private var closed = false
60+
private val chunks = ArrayList<ByteArray>(4)
61+
62+
private fun ensureOpen() { check(!closed) { "Already closed" } }
63+
64+
override fun update(source: ByteArray, startIndex: Int, endIndex: Int) {
65+
ensureOpen()
66+
checkBounds(source.size, startIndex, endIndex)
67+
if (startIndex == 0 && endIndex == source.size) chunks += source
68+
else chunks += source.copyOfRange(startIndex, endIndex)
69+
}
70+
71+
override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean {
72+
ensureOpen()
73+
checkBounds(signature.size, startIndex, endIndex)
74+
val total = chunks.sumOf { it.size }
75+
val data = ByteArray(total)
76+
var off = 0
77+
chunks.forEach { arr -> arr.copyInto(data, off); off += arr.size }
78+
val ok = oneShot(data, signature, startIndex, endIndex)
79+
reset()
80+
return ok
81+
}
82+
83+
override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) {
84+
check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" }
85+
}
86+
87+
override fun reset() {
88+
ensureOpen()
89+
chunks.clear()
90+
}
91+
92+
override fun close() {
93+
closed = true
94+
chunks.clear()
95+
}
96+
}
97+

0 commit comments

Comments
 (0)