Further API conversion

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-23 21:39:04 +02:00
1 parent 233ee1bf55
commit 1848de0e8d
24 files changed
+481 -317

No files matched your search

@@ -0,0 +1,92 @@
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.MultipartHash
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 23-Jun-2020
*/
inline class EncryptableString(val content: String) : Encryptable<EncryptableString> {
override fun toEncryptableForm(): UByteArray {
return content.encodeToUByteArray()
}
override fun fromEncryptableForm(): (UByteArray) -> EncryptableString {
return { uByteArray ->
EncryptableString(uByteArray.toByteArray().decodeToString())
}
}
fun asString() : String = content
}
fun String.asEncryptableString() : EncryptableString {
return EncryptableString(this)
}
interface Encryptable<T> {
fun toEncryptableForm() : UByteArray
fun fromEncryptableForm() : (UByteArray) -> T
}
data class HashedData(val hash: UByteArray) {
fun toHexString() : String {
return hash.toHexString()
}
}
data class SymmetricKey(val value : UByteArray) {
companion object {
}
}
data class EncryptedData constructor(val ciphertext: UByteArray, val nonce: UByteArray) {
}
interface HashApi {
fun hash(data: UByteArray, key : UByteArray = ubyteArrayOf()) : HashedData
fun multipartHash(key: UByteArray? = null) : MultipartHash
}
interface EncryptionApi {
fun encrypt(key: SymmetricKey, data : Encryptable<*>, additionalData : UByteArray = ubyteArrayOf()) : EncryptedData
fun <T: Encryptable<T>> decrypt(key: SymmetricKey, encryptedData : EncryptedData, additionalData: UByteArray, byteArrayDeserializer : (UByteArray) -> T) : T
fun multipartEncrypt(key: SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption
fun multipartDecryptProcessStart(key: SymmetricKey, dataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification
}
interface AuthenticatedEncryption {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray
}
data class EncryptedDataPart(val data : UByteArray)
data class DecryptedDataPart(val data : UByteArray)
data class MultipartEncryptedDataDescriptor(val data: UByteArray, val nonce: UByteArray)
class InvalidTagException : RuntimeException("Tag mismatch! Encrypted data is corrupted or tampered with.")
interface MultipartAuthenticatedVerification {
fun verifyPartialData(data: EncryptedDataPart)
fun finalizeVerificationAndPrepareDecryptor() : MultipartAuthenticatedDecryption
}
interface MultipartAuthenticatedDecryption {
fun decryptPartialData(data: EncryptedDataPart) : DecryptedDataPart
}
interface MultipartAuthenticatedEncryption {
fun encryptPartialData(data: UByteArray) : EncryptedDataPart
fun finish() : MultipartEncryptedDataDescriptor
}
@@ -5,8 +5,10 @@ package com.ionspin.kotlin.crypto
* ugljesa.jovanovic@ionspin.com
* on 27-May-2020
*/
interface CryptoProvider {
interface CryptoInitializer {
suspend fun initialize()
fun isInitialized() : Boolean
}
}
@@ -0,0 +1,37 @@
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties
import com.ionspin.kotlin.crypto.hash.sha.Sha256
import com.ionspin.kotlin.crypto.hash.sha.Sha512
import com.ionspin.kotlin.crypto.keyderivation.ArgonResult
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 23-Jun-2020
*/
interface PrimitivesApi {
fun hashBlake2bMultipart(key: UByteArray? = null, hashLength: Int = Blake2bProperties.MAX_HASH_BYTES): Blake2bMultipart
fun hashBlake2b(message: UByteArray, key: UByteArray = ubyteArrayOf(), hashLength: Int = Blake2bProperties.MAX_HASH_BYTES): UByteArray
fun hashSha256Multipart(): Sha256
fun hashSha256(message: UByteArray) : UByteArray
fun hashSha512Multipart(): Sha512
fun hashSha512(message: UByteArray) : UByteArray
fun deriveKey(
password: String,
salt: String? = null,
key: String,
associatedData: String,
parallelism: Int = 16,
tagLength: Int = 64,
memory: Int = 4096,
numberOfIterations: Int = 10,
) : ArgonResult
}
@@ -1,42 +0,0 @@
package com.ionspin.kotlin.crypto.authenticated
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
interface Aes256GcmStateless {
fun encrypt(message: UByteArray, additionalData: UByteArray, rawData : UByteArray, key: Aes256GcmKey) : Aes256GcmEncryptionResult
fun decrypt(encryptedData: UByteArray, nonce: UByteArray, key : Aes256GcmKey) : UByteArray
}
data class Aes256GcmEncryptionResult(val cyphertext : UByteArray, val additionalData: UByteArray, val nonce: UByteArray, val tag: UByteArray) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as Aes256GcmEncryptionResult
if (cyphertext != other.cyphertext) return false
if (additionalData != other.additionalData) return false
if (nonce != other.nonce) return false
if (tag != other.tag) return false
return true
}
override fun hashCode(): Int {
var result = cyphertext.hashCode()
result = 31 * result + additionalData.hashCode()
result = 31 * result + nonce.hashCode()
result = 31 * result + tag.hashCode()
return result
}
}
interface Aes256GcmKey {
val key : UByteArray
}
@@ -1,36 +0,0 @@
package com.ionspin.kotlin.crypto.authenticated
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 22-Jun-2020
*/
interface AuthenticatedEncryption {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray
}
data class EncryptedDataPart(val data : UByteArray)
data class DecryptedDataPart(val data : UByteArray)
data class MultipartEncryptedDataDescriptor(val data: UByteArray, val nonce: UByteArray)
interface MultipartAuthenticatedVerification {
fun verifyPartialData(data: EncryptedDataPart)
fun finalizeVerificationAndPrepareDecryptor() : MultipartAuthenticatedDecryption
}
interface MultipartAuthenticatedDecryption {
fun decryptPartialData(data: EncryptedDataPart) : DecryptedDataPart
}
interface MultipartAuthenticatedEncryption {
fun encryptPartialData(data: UByteArray) : EncryptedDataPart
fun finish() : MultipartEncryptedDataDescriptor
}
@@ -27,7 +27,7 @@ interface HashFunction {
}
interface MultiPartHash : HashFunction {
interface MultipartHash : HashFunction {
fun update(data : UByteArray)
fun digest() : UByteArray
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
import com.ionspin.kotlin.crypto.hash.MultipartHash
/**
* Created by Ugljesa Jovanovic
@@ -13,12 +13,12 @@ object Blake2bProperties {
const val MAX_HASH_BYTES = 64
}
interface Blake2b : MultiPartHash {
interface Blake2bMultipart : MultipartHash {
override val MAX_HASH_BYTES: Int
get() = Blake2bProperties.MAX_HASH_BYTES
}
interface Blake2bStateless : Hash {
interface Blake2b : Hash {
override val MAX_HASH_BYTES: Int
get() = Blake2bProperties.MAX_HASH_BYTES
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
import com.ionspin.kotlin.crypto.hash.MultipartHash
/**
* Created by Ugljesa Jovanovic
@@ -12,7 +12,7 @@ object Sha256Properties {
const val MAX_HASH_BYTES = 32
}
interface Sha256 : MultiPartHash {
interface Sha256 : MultipartHash {
override val MAX_HASH_BYTES: Int
get() = Sha256Properties.MAX_HASH_BYTES
}
@@ -23,4 +23,4 @@ interface StatelessSha256 : Hash {
fun digest(
inputMessage: UByteArray = ubyteArrayOf()
): UByteArray
}
}
@@ -1,7 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.hash.MultiPartHash
import com.ionspin.kotlin.crypto.hash.MultipartHash
/**
* Created by Ugljesa Jovanovic
@@ -11,15 +11,15 @@ import com.ionspin.kotlin.crypto.hash.MultiPartHash
object Sha512Properties {
const val MAX_HASH_BYTES = 64
}
interface Sha512 : MultiPartHash {
interface Sha512 : MultipartHash {
override val MAX_HASH_BYTES: Int
get() = Sha256Properties.MAX_HASH_BYTES
}
interface StatelessSha512 : Hash {
interface MultipartSha512 : Hash {
override val MAX_HASH_BYTES: Int
get() = Sha512Properties.MAX_HASH_BYTES
fun digest(
inputMessage: UByteArray = ubyteArrayOf()
): UByteArray
}
}
@@ -23,4 +23,13 @@ package com.ionspin.kotlin.crypto.keyderivation
*/
interface KeyDerivationFunction {
fun derive() : UByteArray
}
}
data class ArgonResult(
val hashBytes: UByteArray,
val salt: UByteArray
) {
val hashString by lazy { hashBytes.map { it.toString(16).padStart(2, '0') }.joinToString(separator = "") }
val saltString by lazy { salt.map { it.toString(16).padStart(2, '0') }.joinToString(separator = "") }
}