Further API conversion
This commit is contained in:
1 parent
233ee1bf55
commit
1848de0e8d
24 files changed
+481
-317
No files matched your search
+139
-5
@@ -3,7 +3,9 @@ package com.ionspin.kotlin.crypto
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart
|
||||
import com.ionspin.kotlin.crypto.hash.sha.*
|
||||
import com.ionspin.kotlin.crypto.keyderivation.ArgonResult
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
@@ -11,18 +13,27 @@ import com.ionspin.kotlin.crypto.hash.sha.*
|
||||
* on 24-May-2020
|
||||
*/
|
||||
|
||||
object Crypto : CryptoProvider {
|
||||
object CryptoInitializerDelegated : CryptoInitializer {
|
||||
private var initialized = false
|
||||
override suspend fun initialize() {
|
||||
Initializer.initialize()
|
||||
initialized = true
|
||||
}
|
||||
|
||||
fun initializeWithCallback(done: () -> Unit) {
|
||||
initialized = true
|
||||
Initializer.initializeWithCallback(done)
|
||||
}
|
||||
|
||||
override fun isInitialized(): Boolean {
|
||||
return initialized
|
||||
}
|
||||
}
|
||||
|
||||
object CryptoPrimitives : PrimitivesApi {
|
||||
|
||||
object Blake2b {
|
||||
fun updateable(key: UByteArray? = null, hashLength: Int = Blake2bProperties.MAX_HASH_BYTES): com.ionspin.kotlin.crypto.hash.blake2b.Blake2b {
|
||||
fun updateable(key: UByteArray? = null, hashLength: Int = Blake2bProperties.MAX_HASH_BYTES): com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart {
|
||||
checkInitialization()
|
||||
return Blake2bDelegated(key, hashLength)
|
||||
}
|
||||
@@ -63,12 +74,135 @@ object Crypto : CryptoProvider {
|
||||
}
|
||||
}
|
||||
|
||||
override fun hashBlake2bMultipart(key: UByteArray?, hashLength: Int): Blake2bMultipart {
|
||||
checkInitialization()
|
||||
return Blake2bDelegated(key, hashLength)
|
||||
}
|
||||
|
||||
override fun hashBlake2b(message: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
checkInitialization()
|
||||
return Blake2bDelegatedStateless.digest(message, key, hashLength)
|
||||
}
|
||||
|
||||
override fun hashSha256Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha256 {
|
||||
checkInitialization()
|
||||
return Sha256Delegated()
|
||||
}
|
||||
|
||||
override fun hashSha256(message: UByteArray): UByteArray {
|
||||
checkInitialization()
|
||||
return Sha256StatelessDelegated.digest(inputMessage = message)
|
||||
}
|
||||
|
||||
override fun hashSha512Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha512 {
|
||||
checkInitialization()
|
||||
return Sha512Delegated()
|
||||
}
|
||||
|
||||
override fun hashSha512(message: UByteArray): UByteArray {
|
||||
checkInitialization()
|
||||
return Sha512StatelessDelegated.digest(inputMessage = message)
|
||||
}
|
||||
|
||||
override fun deriveKey(
|
||||
password: String,
|
||||
salt: String?,
|
||||
key: String,
|
||||
associatedData: String,
|
||||
parallelism: Int,
|
||||
tagLength: Int,
|
||||
memory: Int,
|
||||
numberOfIterations: Int
|
||||
): ArgonResult {
|
||||
// return Argon2Delegated.derive(
|
||||
// password,
|
||||
// salt,
|
||||
// key,
|
||||
// associatedData,
|
||||
// parallelism,
|
||||
// tagLength,
|
||||
// memory,
|
||||
// numberOfIterations
|
||||
// )
|
||||
TODO()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
object SimpleCrypto {
|
||||
fun hash(message: String): UByteArray {
|
||||
return ubyteArrayOf(0U)
|
||||
fun SymmetricKey.Companion.randomKey() : SymmetricKey {
|
||||
return SymmetricKey(SRNG.getRandomBytes(32))
|
||||
}
|
||||
|
||||
object Crypto {
|
||||
|
||||
object Hash : HashApi {
|
||||
override fun hash(data: UByteArray, key : UByteArray) : HashedData {
|
||||
return HashedData(Blake2bDelegatedStateless.digest(data, key))
|
||||
}
|
||||
|
||||
override fun multipartHash(key: UByteArray?) : com.ionspin.kotlin.crypto.hash.MultipartHash {
|
||||
return Blake2bDelegated(key)
|
||||
}
|
||||
}
|
||||
|
||||
object Encryption : EncryptionApi {
|
||||
override fun encrypt(key: SymmetricKey, data : Encryptable<*>, additionalData : UByteArray) : EncryptedData {
|
||||
if (key.value.size != 32) {
|
||||
throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}")
|
||||
}
|
||||
val nonce = SRNG.getRandomBytes(24)
|
||||
return EncryptedData(XChaCha20Poly1305Pure.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce)
|
||||
|
||||
}
|
||||
|
||||
override fun <T: Encryptable<T>> decrypt(key: SymmetricKey, encryptedData : EncryptedData, additionalData: UByteArray, byteArrayDeserializer : (UByteArray) -> T) : T {
|
||||
return byteArrayDeserializer(XChaCha20Poly1305Pure.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData))
|
||||
|
||||
}
|
||||
|
||||
override fun multipartEncrypt(key: SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption {
|
||||
return MultipartAuthenticatedEncryptor(key, additionalData)
|
||||
}
|
||||
|
||||
override fun multipartDecryptProcessStart(key: SymmetricKey, dataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification {
|
||||
return MultiplatformAuthenticatedVerificator(key, dataDescriptor, additionalData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption {
|
||||
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
|
||||
override fun encryptPartialData(data: UByteArray): EncryptedDataPart {
|
||||
return EncryptedDataPart(primitive.encryptPartialData(data))
|
||||
}
|
||||
|
||||
override fun finish(): MultipartEncryptedDataDescriptor {
|
||||
val finished = primitive.finish()
|
||||
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricKey, multipartEncryptedDataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification {
|
||||
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
|
||||
val tag = multipartEncryptedDataDescriptor.data.sliceArray(
|
||||
multipartEncryptedDataDescriptor.data.size - 16 until multipartEncryptedDataDescriptor.data.size
|
||||
)
|
||||
override fun verifyPartialData(data: EncryptedDataPart) {
|
||||
primitive.verifyPartialData(data.data)
|
||||
}
|
||||
|
||||
override fun finalizeVerificationAndPrepareDecryptor(): MultipartAuthenticatedDecryption {
|
||||
primitive.checkTag(tag)
|
||||
return MultipartAuthenticatedDecryptor(primitive)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Pure) : MultipartAuthenticatedDecryption {
|
||||
override fun decryptPartialData(data: EncryptedDataPart): DecryptedDataPart {
|
||||
return DecryptedDataPart(encryptor.decrypt(data.data))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,9 +23,9 @@ package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
*/
|
||||
|
||||
|
||||
expect class Blake2bDelegated(key: UByteArray? = null, hashLength: Int = Blake2bProperties.MAX_HASH_BYTES) : Blake2b
|
||||
expect class Blake2bDelegated(key: UByteArray? = null, hashLength: Int = Blake2bProperties.MAX_HASH_BYTES) : Blake2bMultipart
|
||||
|
||||
|
||||
expect object Blake2bDelegatedStateless : Blake2bStateless
|
||||
expect object Blake2bDelegatedStateless : Blake2b
|
||||
|
||||
|
||||
+1
-1
@@ -26,4 +26,4 @@ package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
expect class Sha512Delegated() : Sha512
|
||||
|
||||
expect object Sha512StatelessDelegated : StatelessSha512
|
||||
expect object Sha512StatelessDelegated : MultipartSha512
|
||||
+3
-5
@@ -1,8 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.hash.blake2b
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import com.ionspin.kotlin.crypto.hash.sha.Sha256StatelessDelegated
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import org.khronos.webgl.Uint8Array
|
||||
import org.khronos.webgl.get
|
||||
|
||||
@@ -13,7 +11,7 @@ import org.khronos.webgl.get
|
||||
*/
|
||||
|
||||
|
||||
actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLength: Int) : Blake2b {
|
||||
actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLength: Int) : Blake2bMultipart {
|
||||
override val MAX_HASH_BYTES: Int = 64
|
||||
|
||||
|
||||
@@ -43,7 +41,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLengt
|
||||
|
||||
|
||||
|
||||
actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
actual object Blake2bDelegatedStateless : Blake2b {
|
||||
override val MAX_HASH_BYTES: Int = 64
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
@@ -59,4 +57,4 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -1,6 +1,5 @@
|
||||
package com.ionspin.kotlin.crypto.hash.sha
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
|
||||
import org.khronos.webgl.Uint8Array
|
||||
@@ -35,7 +34,7 @@ actual class Sha512Delegated : Sha512 {
|
||||
|
||||
}
|
||||
|
||||
actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
actual object Sha512StatelessDelegated : MultipartSha512 {
|
||||
|
||||
override fun digest(inputMessage: UByteArray): UByteArray {
|
||||
val hashed = getSodium().crypto_hash_sha512(Uint8Array(inputMessage.toByteArray().toTypedArray()))
|
||||
@@ -45,4 +44,4 @@ actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
}
|
||||
return hash
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -8,7 +8,7 @@ import com.ionspin.kotlin.crypto.Initializer.sodium
|
||||
*/
|
||||
|
||||
|
||||
actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLength: Int) : Blake2b {
|
||||
actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLength: Int) : Blake2bMultipart {
|
||||
|
||||
val state = ByteArray(sodium.crypto_generichash_statebytes())
|
||||
|
||||
@@ -28,7 +28,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, val hashLengt
|
||||
|
||||
}
|
||||
|
||||
actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
actual object Blake2bDelegatedStateless : Blake2b {
|
||||
|
||||
|
||||
override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray {
|
||||
@@ -37,4 +37,4 @@ actual object Blake2bDelegatedStateless : Blake2bStateless {
|
||||
return hashed.toUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -30,11 +30,11 @@ actual class Sha512Delegated : Sha512 {
|
||||
|
||||
}
|
||||
|
||||
actual object Sha512StatelessDelegated : StatelessSha512 {
|
||||
actual object Sha512StatelessDelegated : MultipartSha512 {
|
||||
|
||||
override fun digest(inputMessage: UByteArray): UByteArray {
|
||||
val hashed = ByteArray(Sha512Properties.MAX_HASH_BYTES)
|
||||
Initializer.sodium.crypto_hash_sha512(hashed, inputMessage.toByteArray(), inputMessage.size.toLong())
|
||||
return hashed.toUByteArray()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user