Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Dec 28, 2023
1 parent d6a0d9f commit 909c4fc
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion korlibs-crypto/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import korlibs.*
import korlibs.korge.gradle.generate.*

description = "Korge Foundation Libraries"
description = "Korlibs Cryptography Library"

project.extensions.extraProperties.properties.apply {
applyProjectProperties("https://github.com/korlibs/korge/korge-foundation",
Expand Down
36 changes: 18 additions & 18 deletions korlibs-crypto/src/korlibs/crypto/CipherMode.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy
import kotlin.experimental.xor

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ private object CipherModeCBC : CipherModeIV("CBC") {
for (n in pData.indices step cipher.blockSize) {
arrayxor(pData, n, ivb)
cipher.encrypt(pData, n, cipher.blockSize)
internalArrayCopy(pData, n, ivb, 0, cipher.blockSize)
arraycopy(pData, n, ivb, 0, cipher.blockSize)
}
}

Expand All @@ -67,10 +67,10 @@ private object CipherModeCBC : CipherModeIV("CBC") {
val tempBytes = ByteArray(blockSize)

for (n in pData.indices step blockSize) {
internalArrayCopy(pData, n, tempBytes, 0, blockSize)
arraycopy(pData, n, tempBytes, 0, blockSize)
cipher.decrypt(pData, n, blockSize)
arrayxor(pData, n, ivb)
internalArrayCopy(tempBytes, 0, ivb, 0, blockSize)
arraycopy(tempBytes, 0, ivb, 0, blockSize)
}
}
}
Expand All @@ -81,10 +81,10 @@ private object CipherModePCBC : CipherModeIV("PCBC") {
val plaintext = ByteArray(blockSize)

for (n in pData.indices step blockSize) {
internalArrayCopy(pData, n, plaintext, 0, blockSize)
arraycopy(pData, n, plaintext, 0, blockSize)
arrayxor(pData, n, ivb)
cipher.encrypt(pData, n, cipher.blockSize)
internalArrayCopy(pData, n, ivb, 0, blockSize)
arraycopy(pData, n, ivb, 0, blockSize)
arrayxor(ivb, 0, plaintext)
}
}
Expand All @@ -94,10 +94,10 @@ private object CipherModePCBC : CipherModeIV("PCBC") {
val cipherText = ByteArray(cipher.blockSize)

for (n in pData.indices step cipher.blockSize) {
internalArrayCopy(pData, n, cipherText, 0, blockSize)
arraycopy(pData, n, cipherText, 0, blockSize)
cipher.decrypt(pData, n, cipher.blockSize)
arrayxor(pData, n, ivb)
internalArrayCopy(pData, n, ivb, 0, blockSize)
arraycopy(pData, n, ivb, 0, blockSize)
arrayxor(ivb, 0, cipherText)
}
}
Expand All @@ -109,10 +109,10 @@ private object CipherModeCFB : CipherModeIV("CFB") {
val cipherText = ByteArray(blockSize)

cipher.encrypt(ivb)
internalArrayCopy(ivb, 0, cipherText, 0, blockSize)
arraycopy(ivb, 0, cipherText, 0, blockSize)
for (n in pData.indices step blockSize) {
arrayxor(cipherText, 0, blockSize, pData, n)
internalArrayCopy(cipherText, 0, pData, n, blockSize)
arraycopy(cipherText, 0, pData, n, blockSize)

if (n + blockSize < pData.size) {
cipher.encrypt(cipherText)
Expand All @@ -126,13 +126,13 @@ private object CipherModeCFB : CipherModeIV("CFB") {
val cipherText = ByteArray(blockSize)

cipher.encrypt(ivb)
internalArrayCopy(ivb, 0, cipherText, 0, blockSize)
arraycopy(ivb, 0, cipherText, 0, blockSize)
for (n in pData.indices step blockSize) {
internalArrayCopy(cipherText, 0, plainText, 0, blockSize)
arraycopy(cipherText, 0, plainText, 0, blockSize)
arrayxor(plainText, 0, blockSize, pData, n)

internalArrayCopy(pData, n, cipherText, 0, blockSize)
internalArrayCopy(plainText, 0, pData, n, blockSize)
arraycopy(pData, n, cipherText, 0, blockSize)
arraycopy(plainText, 0, pData, n, blockSize)
if (n + blockSize < pData.size) {
cipher.encrypt(cipherText)
}
Expand All @@ -146,9 +146,9 @@ private object CipherModeOFB : CipherModeIVDE("OFB") {
val cipherText = ByteArray(blockSize)
cipher.encrypt(ivb)
for (n in pData.indices step blockSize) {
internalArrayCopy(pData, n, cipherText, 0, blockSize)
arraycopy(pData, n, cipherText, 0, blockSize)
arrayxor(cipherText, 0, ivb)
internalArrayCopy(cipherText, 0, pData, n, blockSize)
arraycopy(cipherText, 0, pData, n, blockSize)
if (n + blockSize < pData.size) {
cipher.encrypt(ivb)
}
Expand All @@ -162,7 +162,7 @@ private object CipherModeCTR : CipherModeIVDE("CTR") {
val blockSize = cipher.blockSize
val temp = ByteArray(ivb.size)
for (n in pData.indices step blockSize) {
internalArrayCopy(ivb, 0, temp, 0, temp.size)
arraycopy(ivb, 0, temp, 0, temp.size)
cipher.encrypt(temp, 0, blockSize)
arrayxor(pData, n, temp)
for (j in blockSize - 1 downTo 0) {
Expand Down Expand Up @@ -219,5 +219,5 @@ private fun getIV(srcIV: ByteArray?, blockSize: Int): ByteArray {
if (srcIV == null) TODO("IV not provided")
if (srcIV.size < blockSize) throw IllegalArgumentException("Wrong IV length: must be $blockSize bytes long")
return srcIV.copyOf(blockSize)
//return ByteArray(blockSize).also { dstIV -> internalArrayCopy(srcIV, 0, dstIV, 0, kotlin.math.min(srcIV.size, dstIV.size)) }
//return ByteArray(blockSize).also { dstIV -> arraycopy(srcIV, 0, dstIV, 0, kotlin.math.min(srcIV.size, dstIV.size)) }
}
6 changes: 3 additions & 3 deletions korlibs-crypto/src/korlibs/crypto/CipherPadding.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy
import kotlin.random.Random

typealias Padding = CipherPadding
Expand All @@ -24,7 +24,7 @@ abstract class CipherPadding {
//padding(data, blockSize, this)
val paddingSize = paddingSize(data.size, blockSize)
val result = ByteArray(data.size + paddingSize)
internalArrayCopy(data, 0, result, 0, data.size)
arraycopy(data, 0, result, 0, data.size)
addInternal(result, data.size, paddingSize)
return result
}
Expand Down Expand Up @@ -63,7 +63,7 @@ private object CipherPaddingISO10126 : CipherPadding() {
override fun addInternal(result: ByteArray, dataSize: Int, paddingSize: Int) {
val randomBytes = Random.nextBytes(paddingSize)
randomBytes[paddingSize - 1] = paddingSize.toByte()
internalArrayCopy(randomBytes, 0, result, dataSize, randomBytes.size)
arraycopy(randomBytes, 0, result, dataSize, randomBytes.size)
}
}
private object CipherPaddingZero : CipherPadding() {
Expand Down
4 changes: 2 additions & 2 deletions korlibs-crypto/src/korlibs/crypto/HMAC.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy

class HMAC {

Expand All @@ -23,7 +23,7 @@ class HMAC {
}
if (key.size < blockSize) {
val newKey = ByteArray(blockSize)
internalArrayCopy(key, 0, newKey, 0, key.size)
arraycopy(key, 0, newKey, 0, key.size)
key = newKey
}

Expand Down
6 changes: 3 additions & 3 deletions korlibs-crypto/src/korlibs/crypto/Hasher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package korlibs.crypto

import korlibs.encoding.Base64
import korlibs.encoding.Hex
import korlibs.memory.*
import korlibs.internal.arraycopy
import kotlin.math.min

open class HasherFactory(val name: String, val create: () -> Hasher) {
Expand Down Expand Up @@ -58,7 +58,7 @@ abstract class Hasher(val chunkSize: Int, val digestSize: Int, val name: String)
while (left > 0) {
val remainingInChunk = chunkSize - writtenInChunk
val toRead = min(remainingInChunk, left)
internalArrayCopy(data, curr, chunk, writtenInChunk, toRead)
arraycopy(data, curr, chunk, writtenInChunk, toRead)
left -= toRead
curr += toRead
writtenInChunk += toRead
Expand All @@ -76,7 +76,7 @@ abstract class Hasher(val chunkSize: Int, val digestSize: Int, val name: String)
var padPos = 0
while (padPos < pad.size) {
val padSize = chunkSize - writtenInChunk
internalArrayCopy(pad, padPos, chunk, writtenInChunk, padSize)
arraycopy(pad, padPos, chunk, writtenInChunk, padSize)
coreUpdate(chunk)
writtenInChunk = 0
padPos += padSize
Expand Down
6 changes: 3 additions & 3 deletions korlibs-crypto/src/korlibs/crypto/PBKDF2.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy

class PBKDF2 {
companion object {
Expand Down Expand Up @@ -33,8 +33,8 @@ class PBKDF2 {
gen@ for (i in 1 .. blockSize) {
t.fill(0)
i.toByteArray(i32be)
internalArrayCopy(salt, 0, uv, 0, salt.size)
internalArrayCopy(i32be, 0, uv, salt.size, i32be.size)
arraycopy(salt, 0, uv, 0, salt.size)
arraycopy(i32be, 0, uv, salt.size, i32be.size)
var u = uv
for (c in 1 .. iterationCount) {
u = HMAC.hmac(password, u, hasher).bytes
Expand Down
4 changes: 2 additions & 2 deletions korlibs-crypto/src/korlibs/crypto/SHA1.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy
import kotlin.rotateLeft

class SHA1 : SHA(chunkSize = 64, digestSize = 20, name = "SHA1") {
Expand All @@ -22,7 +22,7 @@ class SHA1 : SHA(chunkSize = 64, digestSize = 20, name = "SHA1") {
private val w = IntArray(80)
private val h = IntArray(5)

override fun coreReset() { internalArrayCopy(H, 0, h, 0, 5) }
override fun coreReset() { arraycopy(H, 0, h, 0, 5) }

init {
coreReset()
Expand Down
6 changes: 3 additions & 3 deletions korlibs-crypto/src/korlibs/crypto/SHA256.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy
import kotlin.rotateRight

class SHA256 : SHA(chunkSize = 64, digestSize = 32) {
Expand Down Expand Up @@ -39,10 +39,10 @@ class SHA256 : SHA(chunkSize = 64, digestSize = 32) {
coreReset()
}

override fun coreReset() { internalArrayCopy(H, 0, h, 0, 8) }
override fun coreReset() { arraycopy(H, 0, h, 0, 8) }

override fun coreUpdate(chunk: ByteArray) {
internalArrayCopy(h, 0, r, 0, 8)
arraycopy(h, 0, r, 0, 8)

for (j in 0 until 16) w[j] = chunk.readS32_be(j * 4)
for (j in 16 until 64) {
Expand Down
8 changes: 4 additions & 4 deletions korlibs-crypto/src/korlibs/crypto/SHA512.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy

// https://git.suckless.org/sbase/file/libutil/sha512.c.html
// https://git.suckless.org/sbase/file/sha512.h.html
Expand Down Expand Up @@ -179,10 +179,10 @@ class SHA512 : SHA(chunkSize = 128, digestSize = 64) {
this.len += len.toULong()
if (r != 0) {
if (len < 128 - r) {
internalArrayCopy(m.asByteArray(), p, this.buf.asByteArray(), r, len)
arraycopy(m.asByteArray(), p, this.buf.asByteArray(), r, len)
return
}
internalArrayCopy(m.asByteArray(), p, buf.asByteArray(), r, 128 - r)
arraycopy(m.asByteArray(), p, buf.asByteArray(), r, 128 - r)
len -= 128 - r
p += 128 - r
processblock(this.buf)
Expand All @@ -192,7 +192,7 @@ class SHA512 : SHA(chunkSize = 128, digestSize = 64) {
len -= 128
p += 128
}
internalArrayCopy(m.asByteArray(), p, buf.asByteArray(), 0, len)
arraycopy(m.asByteArray(), p, buf.asByteArray(), 0, len)
}

}
Expand Down
4 changes: 2 additions & 2 deletions korlibs-crypto/src/korlibs/crypto/SecureRandom.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package korlibs.crypto

import korlibs.memory.internalArrayCopy
import korlibs.internal.arraycopy
import kotlin.random.Random

expect fun fillRandomBytes(array: ByteArray)
Expand Down Expand Up @@ -31,7 +31,7 @@ object SecureRandom : Random() {
override fun nextBytes(array: ByteArray, fromIndex: Int, toIndex: Int): ByteArray {
val random = ByteArray(toIndex - fromIndex)
fillRandomBytes(random)
internalArrayCopy(random, 0, array, fromIndex, random.size)
arraycopy(random, 0, array, fromIndex, random.size)
return array
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package korlibs.memory
package korlibs.internal

/** Copies [size] elements of [src] starting at [srcPos] into [dst] at [dstPos] */
internal fun internalArrayCopy(src: ByteArray, srcPos: Int, dst: ByteArray, dstPos: Int, size: Int) {
internal fun arraycopy(src: ByteArray, srcPos: Int, dst: ByteArray, dstPos: Int, size: Int) {
src.copyInto(dst, dstPos, srcPos, srcPos + size)
}

/** Copies [size] elements of [src] starting at [srcPos] into [dst] at [dstPos] */
internal fun internalArrayCopy(src: IntArray, srcPos: Int, dst: IntArray, dstPos: Int, size: Int) {
internal fun arraycopy(src: IntArray, srcPos: Int, dst: IntArray, dstPos: Int, size: Int) {
src.copyInto(dst, dstPos, srcPos, srcPos + size)
}

0 comments on commit 909c4fc

Please sign in to comment.