Enable windows build in builg.gradle, add argon2 nput validation

This commit is contained in:
Ugljesa Jovanovic 2020-05-16 18:48:23 +02:00 committed by Ugljesa Jovanovic
parent 1208d0549c
commit 2bc3051748
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
4 changed files with 102 additions and 20 deletions

View File

@ -144,13 +144,13 @@ kotlin {
} }
} }
// mingwX86() { mingwX86() {
// binaries { binaries {
// staticLib { staticLib {
//
// } }
// } }
// } }
linuxArm32Hfp() { linuxArm32Hfp() {
binaries { binaries {
@ -275,16 +275,16 @@ kotlin {
dependsOn(nativeTest) dependsOn(nativeTest)
} }
// val mingwX86Main by getting { val mingwX86Main by getting {
// dependsOn(commonMain) dependsOn(commonMain)
// dependencies { dependencies {
// implementation(Deps.Native.coroutines) implementation(Deps.Native.coroutines)
// } }
// } }
//
// val mingwX86Test by getting { val mingwX86Test by getting {
// dependsOn(commonTest) dependsOn(commonTest)
// } }
val mingwX64Main by getting { val mingwX64Main by getting {
dependsOn(commonMain) dependsOn(commonMain)

View File

@ -22,6 +22,7 @@ import com.ionspin.kotlin.bignum.integer.toBigInteger
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2b 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.argonBlake2bArbitraryLenghtHash
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG 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.fromLittleEndianArrayToUInt
import com.ionspin.kotlin.crypto.util.hexColumsPrint import com.ionspin.kotlin.crypto.util.hexColumsPrint
import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
@ -53,6 +54,19 @@ class Argon2(
val associatedData: Array<UByte> = emptyArray(), val associatedData: Array<UByte> = emptyArray(),
val argonType: ArgonType = ArgonType.Argon2id val argonType: ArgonType = ArgonType.Argon2id
) { ) {
init {
validateArgonParameters(
password,
salt,
parallelism,
tagLength,
requestedMemorySize,
numberOfIterations,
key,
associatedData,
argonType
)
}
//We support only the latest version //We support only the latest version
val versionNumber: UInt = 0x13U val versionNumber: UInt = 0x13U
@ -60,9 +74,6 @@ class Argon2(
val memorySize = if (requestedMemorySize == 0U) { val memorySize = if (requestedMemorySize == 0U) {
((8 * parallelism) * 2).toUInt() ((8 * parallelism) * 2).toUInt()
} else { } else {
if (requestedMemorySize < (8 * parallelism).toUInt()) {
throw RuntimeException("Requested memory size must be larger than 8 * parallelism. Requested size: $requestedMemorySize")
}
requestedMemorySize requestedMemorySize
} }
val blockCount = (memorySize / (4U * parallelism.toUInt())) * (4U * parallelism.toUInt()) val blockCount = (memorySize / (4U * parallelism.toUInt())) * (4U * parallelism.toUInt())

View File

@ -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")

View File

@ -135,4 +135,44 @@ object Argon2Utils {
return concat 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<UByte>,
salt: Array<UByte>,
parallelism: Int ,
tagLength: UInt,
requestedMemorySize: UInt ,
numberOfIterations: UInt ,
key: Array<UByte>,
associatedData: Array<UByte>,
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)
}
}
} }