-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathBaseCipherFunction.kt
267 lines (221 loc) · 9.72 KB
/
BaseCipherFunction.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 2024 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
*/
package dev.whyoleg.cryptography.providers.base.operations
import dev.whyoleg.cryptography.*
import dev.whyoleg.cryptography.providers.base.*
import kotlinx.io.*
import kotlinx.io.unsafe.*
@CryptographyProviderApi
public interface CipherFunction {
public fun transform(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray
public fun transformedSource(source: RawSource): RawSource
public fun transformedSink(sink: RawSink): RawSink
}
// TODO: test with different input/output sizes
@CryptographyProviderApi
public abstract class BaseCipherFunction : CipherFunction, AutoCloseable {
protected abstract val blockSize: Int
// returns -1 if it's not known
protected abstract fun maxOutputSize(inputSize: Int): Int
// estimates `inputSize` so that next `maxOutputSize(inputSize)` will be less than `expectedMaxOutputSize`
// returns -1 if it's not possible to estimate size
protected open fun maxInputSize(initialMaxInputSize: Int, expectedMaxOutputSize: Int): Int {
check(initialMaxInputSize >= 0) { "initialMaxInputSize must be >= 0, but was $initialMaxInputSize" }
check(expectedMaxOutputSize >= 0) { "expectedMaxOutputSize must be >= 0, but was $expectedMaxOutputSize" }
if (maxOutputSize(initialMaxInputSize) <= expectedMaxOutputSize) return initialMaxInputSize
if (maxOutputSize(0) > expectedMaxOutputSize) return -1
val stepSize = if (blockSize != 0) blockSize else 16
var inputSize = initialMaxInputSize - stepSize
while (inputSize > 0) {
val outputSize = maxOutputSize(inputSize)
if (outputSize <= expectedMaxOutputSize) return inputSize
inputSize -= stepSize
}
return -1
}
protected open fun transformToByteArray(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray {
val maxOutputSize = maxOutputSize(endIndex - startIndex)
val output = ByteArray(maxOutputSize)
val outputSize = transformIntoByteArray(source, output, 0, startIndex, endIndex)
return output.ensureSizeExactly(outputSize)
}
protected abstract fun transformIntoByteArray(
source: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = source.size,
): Int
protected open fun finalizeToByteArray(): ByteArray {
val maxOutputSize = maxOutputSize(0)
val output = ByteArray(maxOutputSize)
val outputSize = finalizeIntoByteArray(output)
return output.ensureSizeExactly(outputSize)
}
protected abstract fun finalizeIntoByteArray(destination: ByteArray, destinationOffset: Int = 0): Int
protected open fun transformAndFinalizeToByteArray(source: ByteArray, startIndex: Int = 0, endIndex: Int = source.size): ByteArray {
val maxOutputSize = maxOutputSize(endIndex - startIndex)
val output = ByteArray(maxOutputSize)
val outputSize = transformAndFinalizeIntoByteArray(source, output, 0, startIndex, endIndex)
return output.ensureSizeExactly(outputSize)
}
protected open fun transformAndFinalizeIntoByteArray(
source: ByteArray,
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = source.size,
): Int {
checkBounds(source.size, startIndex, endIndex)
checkBounds(destination.size, destinationOffset, destinationOffset + maxOutputSize(endIndex - startIndex))
val transformedToDestination = transformIntoByteArray(source, destination, destinationOffset, startIndex, endIndex)
val finalizedToDestination = finalizeIntoByteArray(destination, destinationOffset + transformedToDestination)
return transformedToDestination + finalizedToDestination
}
public override fun transform(source: ByteArray, startIndex: Int, endIndex: Int): ByteArray = use {
return transformAndFinalizeToByteArray(source, startIndex, endIndex)
}
public override fun transformedSource(source: RawSource): RawSource {
return TransformedSource(source)
}
public override fun transformedSink(sink: RawSink): RawSink {
return TransformedSink(sink)
}
@OptIn(UnsafeIoApi::class)
private fun transformTo(
inputBuffer: Buffer,
outputBuffer: Buffer,
maxInputCount: Long,
): Int = UnsafeBufferOperations.readFromHead(inputBuffer) { input, inputStartIndex, inputEndIndex ->
val maxInputSize = minOf(maxInputCount, (inputEndIndex - inputStartIndex).toLong()).toInt()
val maxOutputSize = maxOutputSize(maxInputSize)
val inputSize: Int
val outputSize: Int
// can't estimate output size
if (maxOutputSize == -1) {
outputSize = -1
inputSize = -1
} else if (maxOutputSize <= UnsafeBufferOperations.maxSafeWriteCapacity) {
outputSize = maxOutputSize
inputSize = maxInputSize
} else {
outputSize = UnsafeBufferOperations.maxSafeWriteCapacity
inputSize = maxInputSize(maxInputSize, outputSize)
}
// can't estimate size to fit for safe output size
if (inputSize == -1 || outputSize == -1 || outputSize == 0) {
outputBuffer.write(transformToByteArray(input, inputStartIndex, inputStartIndex + maxInputSize))
maxInputSize
} else {
UnsafeBufferOperations.writeToTail(outputBuffer, outputSize) { output, outputStartIndex, _ ->
transformIntoByteArray(
source = input,
destination = output,
destinationOffset = outputStartIndex,
startIndex = inputStartIndex,
endIndex = inputStartIndex + inputSize
)
}
inputSize
}
}
@OptIn(UnsafeIoApi::class)
private fun finalizeTo(outputBuffer: Buffer) {
val maxOutputSize = maxOutputSize(0)
if (maxOutputSize == 0) return
if (maxOutputSize == -1 || maxOutputSize > UnsafeBufferOperations.maxSafeWriteCapacity) {
outputBuffer.write(finalizeToByteArray())
} else {
UnsafeBufferOperations.writeToTail(outputBuffer, maxOutputSize) { output, outputStartIndex, _ ->
finalizeIntoByteArray(output, outputStartIndex)
}
}
}
private inner class TransformedSource(private val source: RawSource) : RawSource {
private var isFinalized: Boolean = false
private var isClosed: Boolean = false
private val inputBuffer = Buffer()
private val outputBuffer = Buffer()
override fun readAtMostTo(sink: Buffer, byteCount: Long): Long {
require(byteCount >= 0) { "byteCount[$byteCount] < 0" }
check(!isClosed) { "Already closed" }
if (byteCount == 0L) return 0L
while (outputBuffer.size == 0L && !isFinalized) {
// TODO: what should be the value for readAtMostTo byteCount?
@OptIn(UnsafeIoApi::class)
val bytesRead = source.readAtMostTo(inputBuffer, UnsafeBufferOperations.maxSafeWriteCapacity.toLong())
if (bytesRead == -1L) {
isFinalized = true
while (inputBuffer.size != 0L) {
transformTo(inputBuffer, outputBuffer, Long.MAX_VALUE)
}
finalizeTo(outputBuffer)
} else {
transformTo(inputBuffer, outputBuffer, Long.MAX_VALUE)
}
}
return outputBuffer.readAtMostTo(sink, byteCount)
}
override fun close() {
if (isClosed) return
isClosed = true
inputBuffer.clear()
outputBuffer.clear()
var thrown = try {
source.close()
null
} catch (cause: Throwable) {
cause
}
try {
} catch (cause: Throwable) {
if (thrown == null) thrown = cause
else thrown.addSuppressed(cause)
}
if (thrown != null) throw thrown
}
}
private inner class TransformedSink(private val sink: RawSink) : RawSink {
private var isClosed: Boolean = false
private val outputBuffer = Buffer()
override fun write(source: Buffer, byteCount: Long) {
require(byteCount >= 0) { "byteCount[$byteCount] < 0" }
check(!isClosed) { "Already closed" }
var remaining = byteCount
while (remaining > 0) {
remaining -= transformTo(source, outputBuffer, remaining)
}
outputBuffer.transferTo(sink)
}
override fun flush() {
sink.flush()
}
override fun close() {
if (isClosed) return
isClosed = true
var thrown = try {
finalizeTo(outputBuffer)
outputBuffer.transferTo(sink)
null
} catch (cause: Throwable) {
cause
}
outputBuffer.clear()
try {
sink.close()
} catch (cause: Throwable) {
if (thrown == null) thrown = cause
else thrown.addSuppressed(cause)
}
try {
} catch (cause: Throwable) {
if (thrown == null) thrown = cause
else thrown.addSuppressed(cause)
}
if (thrown != null) throw thrown
}
}
}