Removing direct aes cbc and ctr, to reduce foot shooting incidents, introducing aes256-gcm
This commit is contained in:
1 parent
0b30215143
commit
5c10d3abf4
15 files changed
+136
-923
No files matched your search
+37
@@ -0,0 +1,37 @@
|
||||
package com.ionspin.kotlin.crypto.authenticated
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 14-Jun-2020
|
||||
*/
|
||||
interface Aes256GcmStateless {
|
||||
/**
|
||||
* Nonce autogenerated, key autogenerated
|
||||
*/
|
||||
fun encrypt(message: UByteArray, additionalData: UByteArray, rawData : UByteArray) : Aes256GcmEncryptionResult
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric.aes
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 13-Jun-2020
|
||||
*/
|
||||
sealed class AesKey(val key: String, val keyLength: Int) {
|
||||
val keyArray: UByteArray = key.chunked(2).map { it.toUByte(16) }.toUByteArray()
|
||||
val numberOf32BitWords = keyLength / 32
|
||||
|
||||
class Aes128Key(key: String) : AesKey(key, 128)
|
||||
class Aes192Key(key: String) : AesKey(key, 192)
|
||||
class Aes256Key(key: String) : AesKey(key, 256)
|
||||
|
||||
init {
|
||||
checkKeyLength(key, keyLength)
|
||||
}
|
||||
|
||||
fun checkKeyLength(key: String, expectedLength: Int) {
|
||||
if ((key.length / 2) != expectedLength / 8) {
|
||||
throw RuntimeException("Invalid key length")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
-69
@@ -1,69 +0,0 @@
|
||||
package com.ionspin.kotlin.crypto.symmetric.aes
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 13-Jun-2020
|
||||
*/
|
||||
interface SimpleUpdateableAesCtr {
|
||||
fun update(data: UByteArray)
|
||||
|
||||
fun process() : UByteArray
|
||||
|
||||
}
|
||||
|
||||
interface AdvancedUpdateableAesCtr : SimpleUpdateableAesCtr {
|
||||
fun update(data: UByteArray, counter : UByteArray) : UByteArray
|
||||
}
|
||||
|
||||
interface SimpleStatelessAesCtr {
|
||||
|
||||
fun encrypt(aesKey: AesKey, data: UByteArray) : EncryptedDataAndInitialCounter
|
||||
|
||||
fun decrypt(aesKey: AesKey, encryptedDataAndInitialCounter: EncryptedDataAndInitialCounter) : UByteArray
|
||||
|
||||
}
|
||||
|
||||
interface AdvancedStatelessAesCtr : SimpleStatelessAesCtr {
|
||||
fun encrypt(aesKey: AesKey, data: UByteArray, initialCounter: UByteArray) : EncryptedDataAndInitialCounter
|
||||
}
|
||||
|
||||
data class EncryptedDataAndInitialCounter(val encryptedData : UByteArray, val initialCounter : UByteArray) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
other as EncryptedDataAndInitialCounter
|
||||
|
||||
if (!encryptedData.contentEquals(other.encryptedData)) return false
|
||||
if (!initialCounter.contentEquals(other.initialCounter)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = encryptedData.contentHashCode()
|
||||
result = 31 * result + initialCounter.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
data class EncryptedDataAndInitializationVector(val encryptedData : UByteArray, val initializationVector : UByteArray) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other == null || this::class != other::class) return false
|
||||
|
||||
other as EncryptedDataAndInitializationVector
|
||||
|
||||
if (!encryptedData.contentEquals(other.encryptedData)) return false
|
||||
if (!initializationVector.contentEquals(other.initializationVector)) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = encryptedData.contentHashCode()
|
||||
result = 31 * result + initializationVector.contentHashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user