Skip to content

Commit 8038cb7

Browse files
committed
chore(crypto): Deprecate CipherPool and remove stale logic/KDocs
1 parent cf6e6e9 commit 8038cb7

File tree

8 files changed

+32
-137
lines changed

8 files changed

+32
-137
lines changed

safebox/src/main/AndroidManifest.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,4 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
-->
17-
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
18-
<application>
19-
<provider
20-
android:name="com.harrytmthy.safebox.startup.SafeBoxStartupProvider"
21-
android:authorities="${applicationId}.safebox-startup"
22-
android:exported="false"
23-
android:initOrder="100" />
24-
</application>
25-
</manifest>
17+
<manifest />

safebox/src/main/java/com/harrytmthy/safebox/SafeBox.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public class SafeBox private constructor(
172172
}
173173

174174
/**
175+
* **Deprecated:** SafeBox reads now behave exactly like SharedPreferences, where `getXxx(...)`
176+
* blocks the current thread until the initial load completes.
177+
*
175178
* Sets the fallback behavior when values are accessed before the initial load completes.
176179
*
177180
* SafeBox performs a background load of previously written entries on initialization.
@@ -181,6 +184,7 @@ public class SafeBox private constructor(
181184
*
182185
* @param fallbackStrategy The behavior to apply when access is premature
183186
*/
187+
@Deprecated(message = "This method is now a no-op. Will be removed in v1.3.")
184188
public fun setInitialLoadStrategy(fallbackStrategy: ValueFallbackStrategy) {
185189
// no-op
186190
}
@@ -200,7 +204,7 @@ public class SafeBox private constructor(
200204
}
201205

202206
/**
203-
* **Deprecated:** SafeBox no longer supports instance closing.
207+
* **Deprecated:** SafeBox no longer requires instance closing.
204208
*
205209
* Immediately closes the underlying file channel and releases resources.
206210
*
@@ -210,13 +214,13 @@ public class SafeBox private constructor(
210214
*
211215
* Closing during an active write can result in data corruption or incomplete persistence.
212216
*/
213-
@Deprecated(message = "This method is now a no-op, as SafeBox is always active and reusable.")
217+
@Deprecated(message = "This method is now a no-op. Will be removed in v1.3.")
214218
public fun close() {
215219
// no-op
216220
}
217221

218222
/**
219-
* **Deprecated:** SafeBox no longer supports instance closing.
223+
* **Deprecated:** SafeBox no longer requires instance closing.
220224
*
221225
* Closes the underlying file channel only after all pending writes have completed.
222226
*
@@ -227,7 +231,7 @@ public class SafeBox private constructor(
227231
* Internally, this launches a coroutine on [safeBoxScope] to wait until the SafeBox
228232
* becomes idle before releasing resources.
229233
*/
230-
@Deprecated(message = "This method is now a no-op, as SafeBox is always active and reusable.")
234+
@Deprecated(message = "This method is now a no-op. Will be removed in v1.3.")
231235
public fun closeWhenIdle() {
232236
// no-op
233237
}

safebox/src/main/java/com/harrytmthy/safebox/cryptography/ChaCha20CipherProvider.kt

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import android.security.keystore.KeyProperties.DIGEST_SHA256
2121
import com.harrytmthy.safebox.keystore.KeyProvider
2222
import com.harrytmthy.safebox.keystore.SafeSecretKey
2323
import org.bouncycastle.jcajce.spec.AEADParameterSpec
24+
import org.bouncycastle.jce.provider.BouncyCastleProvider
25+
import java.security.GeneralSecurityException
2426
import java.security.MessageDigest
27+
import java.security.Security
2528
import javax.crypto.Cipher
2629

2730
/**
@@ -38,7 +41,15 @@ internal class ChaCha20CipherProvider(
3841
private val deterministic: Boolean,
3942
) : CipherProvider {
4043

41-
private val cipherPool by lazy { SingletonCipherPoolProvider.getChaCha20CipherPool() }
44+
private val cipher by lazy {
45+
try {
46+
Cipher.getInstance(TRANSFORMATION, BouncyCastleProvider.PROVIDER_NAME)
47+
} catch (_: GeneralSecurityException) {
48+
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME)
49+
Security.addProvider(BouncyCastleProvider())
50+
Cipher.getInstance(TRANSFORMATION, BouncyCastleProvider.PROVIDER_NAME)
51+
}
52+
}
4253

4354
private val cipherLock = Any()
4455

@@ -51,10 +62,8 @@ internal class ChaCha20CipherProvider(
5162
}
5263
val paramSpec = AEADParameterSpec(iv, MAC_SIZE_BITS)
5364
val key = keyProvider.getOrCreateKey()
54-
val encrypted = cipherPool.withCipher { cipher ->
55-
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec)
56-
cipher.doFinal(plaintext)
57-
}
65+
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec)
66+
val encrypted = cipher.doFinal(plaintext)
5867
(key as? SafeSecretKey)?.releaseHeapCopy()
5968
iv + encrypted
6069
}
@@ -65,10 +74,8 @@ internal class ChaCha20CipherProvider(
6574
val actual = ciphertext.copyOfRange(IV_SIZE, ciphertext.size)
6675
val paramSpec = AEADParameterSpec(iv, MAC_SIZE_BITS)
6776
val key = keyProvider.getOrCreateKey()
68-
val plaintext = cipherPool.withCipher { cipher ->
69-
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec)
70-
cipher.doFinal(actual)
71-
}
77+
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec)
78+
val plaintext = cipher.doFinal(actual)
7279
(key as? SafeSecretKey)?.releaseHeapCopy()
7380
plaintext
7481
}

safebox/src/main/java/com/harrytmthy/safebox/cryptography/CipherPool.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import java.util.concurrent.atomic.AtomicInteger
2525
import javax.crypto.Cipher
2626

2727
/**
28+
* **Deprecated:** SafeBox now serializes crypto internally, where thread-safety is guaranteed.
29+
*
2830
* A lightweight, coroutine-friendly object pool for [Cipher] instances.
2931
*
3032
* This pool ensures thread-safe and memory-efficient reuse of Cipher objects,
@@ -55,6 +57,7 @@ import javax.crypto.Cipher
5557
* }
5658
* ```
5759
*/
60+
@Deprecated(message = "SafeBox no longer uses pooled ciphers. Will be removed in v1.3.")
5861
public class CipherPool @JvmOverloads constructor(
5962
initialSize: Int = DEFAULT_INITIAL_SIZE,
6063
maxSize: Int = DEFAULT_MAX_SIZE,
@@ -194,6 +197,7 @@ public class CipherPool @JvmOverloads constructor(
194197
}
195198
}
196199

200+
@Deprecated(message = "SafeBox no longer uses pooled ciphers. Will be removed in v1.3.")
197201
public interface CipherPoolExecutor {
198202
fun executeLoadTask(task: () -> Unit)
199203
fun scheduleTrimmingTask(task: () -> Unit)

safebox/src/main/java/com/harrytmthy/safebox/cryptography/SingletonCipherPoolProvider.kt

Lines changed: 0 additions & 57 deletions
This file was deleted.

safebox/src/main/java/com/harrytmthy/safebox/keystore/SafeSecretKey.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal class SafeSecretKey(
8282
lastCopy.get()?.let { return it } // fast path after lock acquisition
8383
val copy = keyBuffer.toByteArray().xorInPlace(mask)
8484
lastCopy.set(copy)
85-
return copy
85+
copy
8686
}
8787
}
8888

safebox/src/main/java/com/harrytmthy/safebox/startup/SafeBoxStartupProvider.kt

Lines changed: 0 additions & 55 deletions
This file was deleted.

safebox/src/main/java/com/harrytmthy/safebox/state/SafeBoxState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public enum class SafeBoxState {
5151
WRITING,
5252

5353
/**
54-
* **Deprecated:** This state is no longer emitted.
54+
* **Deprecated:** SafeBox is always active and reusable.
5555
*
5656
* SafeBox has been closed and is no longer usable.
5757
*/
58-
@Deprecated(message = "This state is never emitted, as SafeBox is always active and reusable.")
58+
@Deprecated(message = "This state is no longer emitted. Will be removed in v1.3.")
5959
CLOSED,
6060
}

0 commit comments

Comments
 (0)