diff --git a/multiplatform-crypto/build.gradle.kts b/multiplatform-crypto/build.gradle.kts index 56756ab..805243a 100644 --- a/multiplatform-crypto/build.gradle.kts +++ b/multiplatform-crypto/build.gradle.kts @@ -144,13 +144,13 @@ kotlin { } } -// mingwX86() { -// binaries { -// staticLib { -// -// } -// } -// } + mingwX86() { + binaries { + staticLib { + + } + } + } linuxArm32Hfp() { binaries { @@ -275,16 +275,16 @@ kotlin { dependsOn(nativeTest) } -// val mingwX86Main by getting { -// dependsOn(commonMain) -// dependencies { -// implementation(Deps.Native.coroutines) -// } -// } -// -// val mingwX86Test by getting { -// dependsOn(commonTest) -// } + val mingwX86Main by getting { + dependsOn(commonMain) + dependencies { + implementation(Deps.Native.coroutines) + } + } + + val mingwX86Test by getting { + dependsOn(commonTest) + } val mingwX64Main by getting { dependsOn(commonMain) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt index 4ea297b..1d732bc 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2.kt @@ -22,6 +22,7 @@ import com.ionspin.kotlin.bignum.integer.toBigInteger import com.ionspin.kotlin.crypto.hash.blake2b.Blake2b import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG +import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.validateArgonParameters import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUInt import com.ionspin.kotlin.crypto.util.hexColumsPrint import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray @@ -53,6 +54,19 @@ class Argon2( val associatedData: Array = emptyArray(), val argonType: ArgonType = ArgonType.Argon2id ) { + init { + validateArgonParameters( + password, + salt, + parallelism, + tagLength, + requestedMemorySize, + numberOfIterations, + key, + associatedData, + argonType + ) + } //We support only the latest version val versionNumber: UInt = 0x13U @@ -60,9 +74,6 @@ class Argon2( val memorySize = if (requestedMemorySize == 0U) { ((8 * parallelism) * 2).toUInt() } else { - if (requestedMemorySize < (8 * parallelism).toUInt()) { - throw RuntimeException("Requested memory size must be larger than 8 * parallelism. Requested size: $requestedMemorySize") - } requestedMemorySize } val blockCount = (memorySize / (4U * parallelism.toUInt())) * (4U * parallelism.toUInt()) diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Exceptions.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Exceptions.kt new file mode 100644 index 0000000..b6080a1 --- /dev/null +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Exceptions.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2019 Ugljesa Jovanovic + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ionspin.kotlin.crypto.keyderivation.argon2 + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 16-May-2020 + */ +class Argon2TagTooShort(tagLength: UInt) : RuntimeException("Too short tag (output) requested. Requested: $tagLength") +class Argon2TagTooLong(tagLength: UInt) : RuntimeException("Too long tag (output) requested. Requested: $tagLength") +class Argon2TimeTooShort(iterations: UInt) : RuntimeException("Too short time parameter (Too few iterations). Requested iterations: $iterations") +class Argon2TimeTooLong(iterations: UInt) : RuntimeException("Too long time parameter (Too many iterations). Requested iterations: $iterations") +class Argon2MemoryTooLitlle(requestedMemorySize: UInt) : RuntimeException("Requested memory size must be larger than 8 * parallelism. Requested size: $requestedMemorySize") +class Argon2MemoryTooMuch(requestedMemorySize: UInt) : RuntimeException("Requested memory size too large. Requested size: $requestedMemorySize") +class Argon2LanesTooFew(parallelism: Int) : RuntimeException("Too few, or invalid number of threads requested $parallelism") +class Argon2LanesTooMany(parallelism: Int) : RuntimeException("Too many threads requested (parallelism). Requested: $parallelism") \ No newline at end of file diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt index a867a8b..171168c 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/keyderivation/argon2/Argon2Utils.kt @@ -135,4 +135,44 @@ object Argon2Utils { return concat } + + /** + * Validates the argon 2 parameters. + * Since Kotlin arrays that we are currently using cannot have more than 2^31 bytes, we don't need to check + * sizes for password, salt, key and associated data. Also since UInt is 32bit we cant set more than 2^32-1 of + * tagLength, requested memory size and number of iterations, so no need to check for upper bound, just lower. + */ + fun validateArgonParameters( + password: Array, + salt: Array, + parallelism: Int , + tagLength: UInt, + requestedMemorySize: UInt , + numberOfIterations: UInt , + key: Array, + associatedData: Array, + argonType: ArgonType + ) { + + //Parallelism + if (parallelism > 0xFFFFFF) { + throw Argon2LanesTooMany(parallelism) + } + if (parallelism <= 0) { + throw Argon2LanesTooFew(parallelism) + } + //Tag length + if (tagLength <= 0U) { + throw Argon2TagTooShort(tagLength) + } + //Requested memory + if (requestedMemorySize < 8U || requestedMemorySize < (8 * parallelism).toUInt()) { + throw Argon2MemoryTooLitlle(requestedMemorySize) + } + //Number of iterations + if (numberOfIterations <= 0U) { + throw Argon2TimeTooShort(numberOfIterations) + } + + } } \ No newline at end of file