Skip to content

Commit

Permalink
more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Jan 21, 2025
1 parent 9fa6634 commit 0fe0317
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import javax.crypto.spec.SecretKeySpec
actual internal fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> initCipher(
algorithm: E,
key: ByteArray,
macKey: ByteArray?,
iv: ByteArray?,
aad: ByteArray?
): CipherParam<T, A> {
Expand All @@ -32,7 +31,7 @@ actual internal fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> init
)
else TODO()
aad?.let { if (algorithm is SymmetricEncryptionAlgorithm.AES.GCM) updateAAD(it) /*CBC-HMAC we do ourselves*/ }
}.let { CipherParam<Cipher, A>(algorithm, it, macKey ?: key, nonce, aad) as CipherParam<T, A> }
}.let { CipherParam<Cipher, A>(algorithm, it, nonce, aad) as CipherParam<T, A> }
}

actual internal fun <A : AuthTrait> CipherParam<*, A>.doEncrypt(data: ByteArray): Ciphertext<A, SymmetricEncryptionAlgorithm<A>> {
Expand Down Expand Up @@ -80,7 +79,7 @@ actual internal fun Ciphertext.Authenticated.doDecrypt(secretKey: ByteArray): By
SecretKeySpec(secretKey, algorithm.jcaKeySpec),
GCMParameterSpec(authTag.size * 8, iv)
)
aad?.let {
authenticatedData?.let {
cipher.updateAAD(it)
}
}.doFinal(wholeInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ internal class Encryptor<A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>, C :
}


private val platformCipher: CipherParam<*, A> = initCipher<Any, A, E>(algorithm, key, macKey, iv, aad)
private val platformCipher: CipherParam<*, A> = initCipher<Any, A, E>(algorithm, key, iv, aad)

/**
* Encrypts [data] and returns a [Ciphertext] matching the algorithm type that was used to create this [Encryptor] object.
Expand All @@ -240,7 +240,6 @@ internal class Encryptor<A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>, C :
val innerCipher = initCipher<Any, AuthTrait.Unauthenticated, SymmetricEncryptionAlgorithm.AES.CBC.Plain>(
algorithm.innerCipher,
key,
macKey,
iv,
aad
)
Expand Down Expand Up @@ -280,46 +279,12 @@ val DefaultDedicatedMacInputCalculation: DedicatedMacInputCalculation =
(iv ?: byteArrayOf()) + (aad ?: byteArrayOf()) + ciphertext


internal data class CipherParam<T, A : AuthTrait>(
val alg: SymmetricEncryptionAlgorithm<out A>,
internal class CipherParam<T, A : AuthTrait>(
val alg: SymmetricEncryptionAlgorithm<A>,
val platformData: T,
val macKey: ByteArray,
val iv: ByteArray?,
val aad: ByteArray?
)
/*
/**
* Generates a new random key matching the key size of this algorithm
*/
fun <A: AuthTrait>SymmetricEncryptionAlgorithm<A>.randomKey() =
@OptIn(HazardousMaterials::class)
secureRandom.nextBytesOf((keySize.bytes).toInt()).let {
when (this) {
is SymmetricEncryptionAlgorithm.AES.CBC.HMAC -> SymmetricKey.WithDedicatedMac(this, it, it)
is SymmetricEncryptionAlgorithm.AES.CBC.Plain -> SymmetricKey.Integrated(this, it)
is SymmetricEncryptionAlgorithm.AES.GCM -> SymmetricKey.Integrated(this, it)
else -> TODO()
}
}
inline fun <reified A, reified E : SymmetricEncryptionAlgorithm<A>, reified K : SymmetricKey<out A, out E>> E.randomKey(): K =
@OptIn(HazardousMaterials::class) secureRandom.nextBytesOf((keySize.bytes).toInt()).let {
when (this) {
is SymmetricEncryptionAlgorithm.Authenticated.WithDedicatedMac -> WithDedicatedMac(this, it, it)
is SymmetricEncryptionAlgorithm.Unauthenticated -> Integrated(this, it)
is SymmetricEncryptionAlgorithm.Authenticated.Integrated -> Integrated(this, it)
else -> TODO()
} as K
}
*/

/**
* Generates a new random key matching the key size of this algorithm
*/
//fun SymmetricEncryptionAlgorithm.Unauthenticated.randomKey(): SymmetricKey.Integrated<Unauthenticated> = randomKey()

/**
* Generates a new random key matching the key size of this algorithm
Expand Down Expand Up @@ -411,7 +376,6 @@ expect internal fun Ciphertext.Unauthenticated.doDecrypt(secretKey: ByteArray):
internal expect fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> initCipher(
algorithm: E,
key: ByteArray,
macKey: ByteArray?,
iv: ByteArray?,
aad: ByteArray?
): CipherParam<T, A>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ import platform.CoreCrypto.kCCEncrypt
internal actual fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> initCipher(
algorithm: E,
key: ByteArray,
macKey: ByteArray?,
iv: ByteArray?,
aad: ByteArray?
): CipherParam<T, A> {
if (algorithm !is SymmetricEncryptionAlgorithm.WithIV<*>) TODO()
val nonce = iv ?: algorithm.randomIV()
return CipherParam<ByteArray, A>(algorithm, key, macKey ?: key, nonce, aad) as CipherParam<T, A>
return CipherParam<ByteArray, A>(algorithm, key, nonce, aad) as CipherParam<T, A>
}

@OptIn(ExperimentalForeignApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import javax.crypto.spec.SecretKeySpec
actual internal fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> initCipher(
algorithm: E,
key: ByteArray,
macKey: ByteArray?,
iv: ByteArray?,
aad: ByteArray?
): CipherParam<T, A> {
Expand All @@ -32,7 +31,7 @@ actual internal fun <T, A : AuthTrait, E : SymmetricEncryptionAlgorithm<A>> init
)
else TODO()
aad?.let { if (algorithm is SymmetricEncryptionAlgorithm.AES.GCM) updateAAD(it) /*CBC-HMAC we do ourselves*/ }
}.let { CipherParam<Cipher, A>(algorithm, it, macKey ?: key, nonce, aad) as CipherParam<T, A> }
}.let { CipherParam<Cipher, A>(algorithm, it, nonce, aad) as CipherParam<T, A> }
}

actual internal fun <A : AuthTrait> CipherParam<*, A>.doEncrypt(data: ByteArray): Ciphertext<A, SymmetricEncryptionAlgorithm<A>> {
Expand Down

0 comments on commit 0fe0317

Please sign in to comment.