From 71b1ac8e209bfdbee6eddc84427154b8c453aeeb Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 29 Aug 2023 18:55:11 +0100 Subject: [PATCH] Rename XteaKey to SymmetricKey It's now used for both XTEA and ISAAC keys, and there's nothing XTEA-specific about it so there's no need to duplicate it. Signed-off-by: Graham --- .../openrs2/archive/cache/CacheExporter.kt | 6 +- .../openrs2/archive/key/BinaryKeyReader.kt | 20 +++--- .../openrs2/archive/key/HdosKeyDownloader.kt | 8 +-- .../org/openrs2/archive/key/HexKeyReader.kt | 6 +- .../openrs2/archive/key/JsonKeyDownloader.kt | 4 +- .../org/openrs2/archive/key/JsonKeyReader.kt | 10 +-- .../org/openrs2/archive/key/KeyBruteForcer.kt | 10 +-- .../org/openrs2/archive/key/KeyDownloader.kt | 4 +- .../org/openrs2/archive/key/KeyExporter.kt | 12 ++-- .../org/openrs2/archive/key/KeyImporter.kt | 10 +-- .../org/openrs2/archive/key/KeyReader.kt | 4 +- .../org/openrs2/archive/key/TextKeyReader.kt | 6 +- .../org/openrs2/archive/web/KeysController.kt | 4 +- .../main/kotlin/org/openrs2/cache/Archive.kt | 38 ++++++------ .../main/kotlin/org/openrs2/cache/Cache.kt | 48 +++++++++----- .../org/openrs2/cache/Js5Compression.kt | 14 ++--- .../org/openrs2/cache/Js5CompressionTest.kt | 62 +++++++++---------- .../org/openrs2/crypto/CryptoJacksonModule.kt | 4 +- .../crypto/{XteaKey.kt => SymmetricKey.kt} | 18 +++--- ...ializer.kt => SymmetricKeyDeserializer.kt} | 6 +- ...erializer.kt => SymmetricKeySerializer.kt} | 4 +- .../main/kotlin/org/openrs2/crypto/Xtea.kt | 4 +- .../kotlin/org/openrs2/crypto/XteaTest.kt | 2 +- .../login/upstream/CreateAccountCodec.kt | 6 +- .../login/upstream/GameLoginPayload.kt | 6 +- 25 files changed, 166 insertions(+), 150 deletions(-) rename crypto/src/main/kotlin/org/openrs2/crypto/{XteaKey.kt => SymmetricKey.kt} (73%) rename crypto/src/main/kotlin/org/openrs2/crypto/{XteaKeyDeserializer.kt => SymmetricKeyDeserializer.kt} (55%) rename crypto/src/main/kotlin/org/openrs2/crypto/{XteaKeySerializer.kt => SymmetricKeySerializer.kt} (67%) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt index 7c788b9524..1c0f02c095 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheExporter.kt @@ -16,7 +16,7 @@ import org.openrs2.cache.Js5Compression import org.openrs2.cache.Js5MasterIndex import org.openrs2.cache.MasterIndexFormat import org.openrs2.cache.Store -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.db.Database import org.postgresql.util.PGobject import java.sql.Connection @@ -196,7 +196,7 @@ public class CacheExporter @Inject constructor( val nameHash: Int?, val name: String?, @JsonProperty("mapsquare") val mapSquare: Int?, - val key: XteaKey + val key: SymmetricKey ) public suspend fun totalSize(): Long { @@ -779,7 +779,7 @@ public class CacheExporter @Inject constructor( val k3 = rows.getInt(8) val mapSquare = getMapSquare(name) - keys += Key(archive, group, nameHash, name, mapSquare, XteaKey(k0, k1, k2, k3)) + keys += Key(archive, group, nameHash, name, mapSquare, SymmetricKey(k0, k1, k2, k3)) } keys diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/BinaryKeyReader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/BinaryKeyReader.kt index 2ea8ce6394..2e76070e8d 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/BinaryKeyReader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/BinaryKeyReader.kt @@ -3,17 +3,17 @@ package org.openrs2.archive.key import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled import org.openrs2.buffer.use -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import java.io.InputStream public object BinaryKeyReader : KeyReader { - override fun read(input: InputStream): Sequence { + override fun read(input: InputStream): Sequence { Unpooled.wrappedBuffer(input.readBytes()).use { buf -> val len = buf.readableBytes() if (len == (128 * 128 * 16)) { val keys = read(buf, 0) - require(XteaKey.ZERO in keys) + require(SymmetricKey.ZERO in keys) return keys.asSequence() } @@ -22,19 +22,19 @@ public object BinaryKeyReader : KeyReader { if (maybeShort && !maybeInt) { val keys = read(buf, 2) - require(XteaKey.ZERO in keys) + require(SymmetricKey.ZERO in keys) return keys.asSequence() } else if (!maybeShort && maybeInt) { val keys = read(buf, 4).asSequence() - require(XteaKey.ZERO in keys) + require(SymmetricKey.ZERO in keys) return keys.asSequence() } else if (maybeShort && maybeInt) { val shortKeys = read(buf, 2) val intKeys = read(buf, 4) - return if (XteaKey.ZERO in shortKeys && XteaKey.ZERO !in intKeys) { + return if (SymmetricKey.ZERO in shortKeys && SymmetricKey.ZERO !in intKeys) { shortKeys.asSequence() - } else if (XteaKey.ZERO !in shortKeys && XteaKey.ZERO in intKeys) { + } else if (SymmetricKey.ZERO !in shortKeys && SymmetricKey.ZERO in intKeys) { intKeys.asSequence() } else { throw IllegalArgumentException("Failed to determine if map square IDs are 2 or 4 bytes") @@ -47,8 +47,8 @@ public object BinaryKeyReader : KeyReader { } } - private fun read(buf: ByteBuf, mapSquareLen: Int): Set { - val keys = mutableSetOf() + private fun read(buf: ByteBuf, mapSquareLen: Int): Set { + val keys = mutableSetOf() while (buf.isReadable) { buf.skipBytes(mapSquareLen) @@ -57,7 +57,7 @@ public object BinaryKeyReader : KeyReader { val k1 = buf.readInt() val k2 = buf.readInt() val k3 = buf.readInt() - keys += XteaKey(k0, k1, k2, k3) + keys += SymmetricKey(k0, k1, k2, k3) } return keys diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt index 970883f1b0..dbf4c38f74 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/HdosKeyDownloader.kt @@ -5,7 +5,7 @@ import jakarta.inject.Singleton import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.future.await import kotlinx.coroutines.withContext -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.http.checkStatusCode import java.net.URI import java.net.http.HttpClient @@ -21,7 +21,7 @@ public class HdosKeyDownloader @Inject constructor( return setOf(ENDPOINT) } - override suspend fun download(url: String): Sequence { + override suspend fun download(url: String): Sequence { val request = HttpRequest.newBuilder(URI(url)) .GET() .timeout(Duration.ofSeconds(30)) @@ -33,7 +33,7 @@ public class HdosKeyDownloader @Inject constructor( return withContext(Dispatchers.IO) { response.body().use { input -> input.bufferedReader().use { reader -> - val keys = mutableSetOf() + val keys = mutableSetOf() for (line in reader.lineSequence()) { val parts = line.split(',') @@ -41,7 +41,7 @@ public class HdosKeyDownloader @Inject constructor( continue } - val key = XteaKey.fromHexOrNull(parts[2]) ?: continue + val key = SymmetricKey.fromHexOrNull(parts[2]) ?: continue keys += key } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/HexKeyReader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/HexKeyReader.kt index 6b7314075b..19ab57e061 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/HexKeyReader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/HexKeyReader.kt @@ -1,13 +1,13 @@ package org.openrs2.archive.key -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import java.io.InputStream public object HexKeyReader : KeyReader { - override fun read(input: InputStream): Sequence { + override fun read(input: InputStream): Sequence { return input.bufferedReader() .lineSequence() - .map(XteaKey::fromHexOrNull) + .map(SymmetricKey::fromHexOrNull) .filterNotNull() } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyDownloader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyDownloader.kt index 17bc9f7f7a..c58ef081fe 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyDownloader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyDownloader.kt @@ -3,7 +3,7 @@ package org.openrs2.archive.key import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.future.await import kotlinx.coroutines.withContext -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.http.checkStatusCode import java.net.URI import java.net.http.HttpClient @@ -16,7 +16,7 @@ public abstract class JsonKeyDownloader( private val client: HttpClient, private val jsonKeyReader: JsonKeyReader ) : KeyDownloader(source) { - override suspend fun download(url: String): Sequence { + override suspend fun download(url: String): Sequence { val request = HttpRequest.newBuilder(URI(url)) .GET() .timeout(Duration.ofSeconds(30)) diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyReader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyReader.kt index 453912d10c..ed9c789a9f 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyReader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/JsonKeyReader.kt @@ -4,7 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.treeToValue import jakarta.inject.Inject import jakarta.inject.Singleton -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.json.Json import java.io.IOException import java.io.InputStream @@ -13,21 +13,21 @@ import java.io.InputStream public class JsonKeyReader @Inject constructor( @Json private val mapper: ObjectMapper ) : KeyReader { - override fun read(input: InputStream): Sequence { - val keys = mutableSetOf() + override fun read(input: InputStream): Sequence { + val keys = mutableSetOf() val root = mapper.readTree(input) when { root.isArray -> { for (entry in root) { val key = entry["key"] ?: entry["keys"] ?: throw IOException("Missing 'key' or 'keys' field") - keys += mapper.treeToValue(key) ?: throw IOException("Key must be non-null") + keys += mapper.treeToValue(key) ?: throw IOException("Key must be non-null") } } root.isObject -> { for (entry in root.fields()) { - keys += mapper.treeToValue(entry.value) ?: throw IOException("Key must be non-null") + keys += mapper.treeToValue(entry.value) ?: throw IOException("Key must be non-null") } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt index 8fb54d2c27..8a210c1a74 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyBruteForcer.kt @@ -6,7 +6,7 @@ import jakarta.inject.Singleton import org.openrs2.buffer.crc32 import org.openrs2.buffer.use import org.openrs2.cache.Js5Compression -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.db.Database import java.sql.Connection import java.sql.Types @@ -203,7 +203,7 @@ public class KeyBruteForcer @Inject constructor( val k1 = rows.getInt(3) val k2 = rows.getInt(4) val k3 = rows.getInt(5) - val key = XteaKey(k0, k1, k2, k3) + val key = SymmetricKey(k0, k1, k2, k3) validatedKey = validateKey(data, key, keyId, containerId) if (validatedKey != null) { @@ -326,7 +326,7 @@ public class KeyBruteForcer @Inject constructor( } } - private fun nextKey(connection: Connection, lastKeyId: Long?): Pair? { + private fun nextKey(connection: Connection, lastKeyId: Long?): Pair? { connection.prepareStatement( """ SELECT id, (key).k0, (key).k1, (key).k2, (key).k3 @@ -350,7 +350,7 @@ public class KeyBruteForcer @Inject constructor( val k1 = rows.getInt(3) val k2 = rows.getInt(4) val k3 = rows.getInt(5) - val key = XteaKey(k0, k1, k2, k3) + val key = SymmetricKey(k0, k1, k2, k3) return Pair(keyId, key) } @@ -359,7 +359,7 @@ public class KeyBruteForcer @Inject constructor( private fun validateKey( data: ByteArray, - key: XteaKey, + key: SymmetricKey, keyId: Long, containerId: Long ): ValidatedKey? { diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyDownloader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyDownloader.kt index 202f378b84..914660f73f 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyDownloader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyDownloader.kt @@ -1,10 +1,10 @@ package org.openrs2.archive.key -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey public abstract class KeyDownloader( public val source: KeySource ) { public abstract suspend fun getMissingUrls(seenUrls: Set): Set - public abstract suspend fun download(url: String): Sequence + public abstract suspend fun download(url: String): Sequence } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt index faabcc280c..8cae54bfea 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyExporter.kt @@ -2,7 +2,7 @@ package org.openrs2.archive.key import jakarta.inject.Inject import jakarta.inject.Singleton -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.db.Database import java.io.BufferedOutputStream import java.io.DataOutputStream @@ -82,11 +82,11 @@ public class KeyExporter @Inject constructor( } } - public suspend fun exportAll(): List { + public suspend fun exportAll(): List { return export(validOnly = false) } - public suspend fun exportValid(): List { + public suspend fun exportValid(): List { return export(validOnly = true) } @@ -116,7 +116,7 @@ public class KeyExporter @Inject constructor( return analysis } - private suspend fun export(validOnly: Boolean): List { + private suspend fun export(validOnly: Boolean): List { return database.execute { connection -> val query = if (validOnly) { EXPORT_VALID_QUERY @@ -126,14 +126,14 @@ public class KeyExporter @Inject constructor( connection.prepareStatement(query).use { stmt -> stmt.executeQuery().use { rows -> - val keys = mutableListOf() + val keys = mutableListOf() while (rows.next()) { val k0 = rows.getInt(1) val k1 = rows.getInt(2) val k2 = rows.getInt(3) val k3 = rows.getInt(4) - keys += XteaKey(k0, k1, k2, k3) + keys += SymmetricKey(k0, k1, k2, k3) } keys diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyImporter.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyImporter.kt index 569a681be3..4a3b9eceaa 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyImporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyImporter.kt @@ -3,7 +3,7 @@ package org.openrs2.archive.key import com.github.michaelbull.logging.InlineLogger import jakarta.inject.Inject import jakarta.inject.Singleton -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.db.Database import java.io.IOException import java.nio.file.Files @@ -19,10 +19,10 @@ public class KeyImporter @Inject constructor( private val jsonKeyReader: JsonKeyReader, private val downloaders: Set ) { - private data class Key(val key: XteaKey, val source: KeySource) + private data class Key(val key: SymmetricKey, val source: KeySource) public suspend fun import(path: Path) { - val keys = mutableSetOf() + val keys = mutableSetOf() for (file in Files.walk(path)) { if (!Files.isRegularFile(file)) { @@ -45,7 +45,7 @@ public class KeyImporter @Inject constructor( } } - keys -= XteaKey.ZERO + keys -= SymmetricKey.ZERO logger.info { "Importing ${keys.size} keys" } @@ -108,7 +108,7 @@ public class KeyImporter @Inject constructor( } } - public suspend fun import(keys: Iterable, source: KeySource) { + public suspend fun import(keys: Iterable, source: KeySource) { val now = Instant.now() database.execute { connection -> diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/KeyReader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/KeyReader.kt index 89c1a1f7db..4b56effcab 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/KeyReader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/KeyReader.kt @@ -1,8 +1,8 @@ package org.openrs2.archive.key -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import java.io.InputStream public interface KeyReader { - public fun read(input: InputStream): Sequence + public fun read(input: InputStream): Sequence } diff --git a/archive/src/main/kotlin/org/openrs2/archive/key/TextKeyReader.kt b/archive/src/main/kotlin/org/openrs2/archive/key/TextKeyReader.kt index 7ef3ac5878..a05f653b5a 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/key/TextKeyReader.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/key/TextKeyReader.kt @@ -1,10 +1,10 @@ package org.openrs2.archive.key -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import java.io.InputStream public object TextKeyReader : KeyReader { - override fun read(input: InputStream): Sequence { + override fun read(input: InputStream): Sequence { val reader = input.bufferedReader() val k0 = reader.readLine()?.toIntOrNull() ?: return emptySequence() @@ -12,6 +12,6 @@ public object TextKeyReader : KeyReader { val k2 = reader.readLine()?.toIntOrNull() ?: return emptySequence() val k3 = reader.readLine()?.toIntOrNull() ?: return emptySequence() - return sequenceOf(XteaKey(k0, k1, k2, k3)) + return sequenceOf(SymmetricKey(k0, k1, k2, k3)) } } diff --git a/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt b/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt index 4b340a83e8..eadbfa1325 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/web/KeysController.kt @@ -10,7 +10,7 @@ import jakarta.inject.Singleton import org.openrs2.archive.key.KeyExporter import org.openrs2.archive.key.KeyImporter import org.openrs2.archive.key.KeySource -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey @Singleton public class KeysController @Inject constructor( @@ -24,7 +24,7 @@ public class KeysController @Inject constructor( } public suspend fun import(call: ApplicationCall) { - val keys = call.receive>().mapTo(mutableSetOf(), XteaKey::fromIntArray) + val keys = call.receive>().mapTo(mutableSetOf(), SymmetricKey::fromIntArray) if (keys.isNotEmpty()) { importer.import(keys, KeySource.API) diff --git a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt index 63cc078ca1..4e74c941cc 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Archive.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Archive.kt @@ -7,7 +7,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMap import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMaps import org.openrs2.buffer.crc32 import org.openrs2.buffer.use -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.crypto.whirlpool import org.openrs2.util.krHashCode import java.io.FileNotFoundException @@ -23,7 +23,7 @@ public abstract class Archive internal constructor( internal inner class Unpacked( private val entry: Js5Index.MutableGroup, - val key: XteaKey, + val key: SymmetricKey, private var files: Int2ObjectSortedMap ) { private var dirty = false @@ -193,7 +193,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun read(group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(group: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { require(group >= 0 && file >= 0) val entry = index[group] ?: throw FileNotFoundException() @@ -202,19 +202,19 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun readNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun readNamed(groupNameHash: Int, fileNameHash: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException() val unpacked = getUnpacked(entry, key) return unpacked.readNamed(fileNameHash) } @JvmOverloads - public fun read(group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(group: String, file: String, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { return readNamed(group.krHashCode(), file.krHashCode(), key) } @JvmOverloads - public fun readNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun readNamedGroup(groupNameHash: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { require(file >= 0) val entry = index.getNamed(groupNameHash) ?: throw FileNotFoundException() @@ -223,12 +223,12 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun read(group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(group: String, file: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { return readNamedGroup(group.krHashCode(), file, key) } @JvmOverloads - public fun write(group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(group: Int, file: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { require(group >= 0 && file >= 0) val entry = index.createOrGet(group) @@ -239,7 +239,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun writeNamed(groupNameHash: Int, fileNameHash: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun writeNamed(groupNameHash: Int, fileNameHash: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { val entry = index.createOrGetNamed(groupNameHash) val unpacked = createOrGetUnpacked(entry, key, isOverwritingNamed(entry, fileNameHash)) unpacked.writeNamed(fileNameHash, buf) @@ -249,12 +249,12 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun write(group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(group: String, file: String, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { return writeNamed(group.krHashCode(), file.krHashCode(), buf, key) } @JvmOverloads - public fun writeNamedGroup(groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun writeNamedGroup(groupNameHash: Int, file: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { require(file >= 0) val entry = index.createOrGetNamed(groupNameHash) @@ -266,7 +266,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun write(group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(group: String, file: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { return writeNamedGroup(group.krHashCode(), file, buf, key) } @@ -293,7 +293,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun remove(group: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun remove(group: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { require(group >= 0 && file >= 0) val entry = index[group] ?: return @@ -310,7 +310,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun removeNamed(groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) { + public fun removeNamed(groupNameHash: Int, fileNameHash: Int, key: SymmetricKey = SymmetricKey.ZERO) { val entry = index.getNamed(groupNameHash) ?: return if (isOverwritingNamed(entry, fileNameHash)) { @@ -325,12 +325,12 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun remove(group: String, file: String, key: XteaKey = XteaKey.ZERO) { + public fun remove(group: String, file: String, key: SymmetricKey = SymmetricKey.ZERO) { return removeNamed(group.krHashCode(), file.krHashCode(), key) } @JvmOverloads - public fun removeNamedGroup(groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun removeNamedGroup(groupNameHash: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { require(file >= 0) val entry = index.getNamed(groupNameHash) ?: return @@ -347,7 +347,7 @@ public abstract class Archive internal constructor( } @JvmOverloads - public fun remove(group: String, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun remove(group: String, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { removeNamedGroup(group.krHashCode(), file, key) } @@ -388,7 +388,7 @@ public abstract class Archive internal constructor( return fileEntry.nameHash == fileNameHash } - private fun createOrGetUnpacked(entry: Js5Index.MutableGroup, key: XteaKey, overwrite: Boolean): Unpacked { + private fun createOrGetUnpacked(entry: Js5Index.MutableGroup, key: SymmetricKey, overwrite: Boolean): Unpacked { return if (entry.size == 0 || overwrite) { val unpacked = Unpacked(entry, key, Int2ObjectAVLTreeMap()) unpackedCache.put(archive, entry.id, unpacked) @@ -398,7 +398,7 @@ public abstract class Archive internal constructor( } } - private fun getUnpacked(entry: Js5Index.MutableGroup, key: XteaKey): Unpacked { + private fun getUnpacked(entry: Js5Index.MutableGroup, key: SymmetricKey): Unpacked { var unpacked = unpackedCache.get(archive, entry.id) if (unpacked != null) { /* diff --git a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt index d6c1aba2bf..627ffb6536 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Cache.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Cache.kt @@ -3,7 +3,7 @@ package org.openrs2.cache import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufAllocator import org.openrs2.buffer.use -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.util.krHashCode import java.io.Closeable import java.io.FileNotFoundException @@ -142,35 +142,45 @@ public class Cache private constructor( } @JvmOverloads - public fun read(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(archive: Int, group: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { checkArchive(archive) return archives[archive]?.read(group, file, key) ?: throw FileNotFoundException() } @JvmOverloads - public fun readNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun readNamed( + archive: Int, + groupNameHash: Int, + fileNameHash: Int, + key: SymmetricKey = SymmetricKey.ZERO + ): ByteBuf { checkArchive(archive) return archives[archive]?.readNamed(groupNameHash, fileNameHash, key) ?: throw FileNotFoundException() } @JvmOverloads - public fun read(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(archive: Int, group: String, file: String, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { return readNamed(archive, group.krHashCode(), file.krHashCode(), key) } @JvmOverloads - public fun readNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun readNamedGroup( + archive: Int, + groupNameHash: Int, + file: Int, + key: SymmetricKey = SymmetricKey.ZERO + ): ByteBuf { checkArchive(archive) return archives[archive]?.readNamedGroup(groupNameHash, file, key) ?: throw FileNotFoundException() } @JvmOverloads - public fun read(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun read(archive: Int, group: String, file: Int, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { return readNamedGroup(archive, group.krHashCode(), file, key) } @JvmOverloads - public fun write(archive: Int, group: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(archive: Int, group: Int, file: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { checkArchive(archive) createOrGetArchive(archive).write(group, file, buf, key) } @@ -181,25 +191,31 @@ public class Cache private constructor( groupNameHash: Int, fileNameHash: Int, buf: ByteBuf, - key: XteaKey = XteaKey.ZERO + key: SymmetricKey = SymmetricKey.ZERO ) { checkArchive(archive) createOrGetArchive(archive).writeNamed(groupNameHash, fileNameHash, buf, key) } @JvmOverloads - public fun write(archive: Int, group: String, file: String, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(archive: Int, group: String, file: String, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { writeNamed(archive, group.krHashCode(), file.krHashCode(), buf, key) } @JvmOverloads - public fun writeNamedGroup(archive: Int, groupNameHash: Int, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun writeNamedGroup( + archive: Int, + groupNameHash: Int, + file: Int, + buf: ByteBuf, + key: SymmetricKey = SymmetricKey.ZERO + ) { checkArchive(archive) createOrGetArchive(archive).writeNamedGroup(groupNameHash, file, buf, key) } @JvmOverloads - public fun write(archive: Int, group: String, file: Int, buf: ByteBuf, key: XteaKey = XteaKey.ZERO) { + public fun write(archive: Int, group: String, file: Int, buf: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO) { writeNamedGroup(archive, group.krHashCode(), file, buf, key) } @@ -233,30 +249,30 @@ public class Cache private constructor( } @JvmOverloads - public fun remove(archive: Int, group: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun remove(archive: Int, group: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { checkArchive(archive) archives[archive]?.remove(group, file, key) } @JvmOverloads - public fun removeNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: XteaKey = XteaKey.ZERO) { + public fun removeNamed(archive: Int, groupNameHash: Int, fileNameHash: Int, key: SymmetricKey = SymmetricKey.ZERO) { checkArchive(archive) archives[archive]?.removeNamed(groupNameHash, fileNameHash, key) } @JvmOverloads - public fun remove(archive: Int, group: String, file: String, key: XteaKey = XteaKey.ZERO) { + public fun remove(archive: Int, group: String, file: String, key: SymmetricKey = SymmetricKey.ZERO) { return removeNamed(archive, group.krHashCode(), file.krHashCode(), key) } @JvmOverloads - public fun removeNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun removeNamedGroup(archive: Int, groupNameHash: Int, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { checkArchive(archive) archives[archive]?.removeNamedGroup(groupNameHash, file, key) } @JvmOverloads - public fun remove(archive: Int, group: String, file: Int, key: XteaKey = XteaKey.ZERO) { + public fun remove(archive: Int, group: String, file: Int, key: SymmetricKey = SymmetricKey.ZERO) { removeNamedGroup(archive, group.krHashCode(), file, key) } diff --git a/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt b/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt index b4d82cf537..67339b42b6 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Js5Compression.kt @@ -4,7 +4,7 @@ import io.netty.buffer.ByteBuf import io.netty.buffer.ByteBufInputStream import io.netty.buffer.ByteBufOutputStream import org.openrs2.buffer.use -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.crypto.xteaDecrypt import org.openrs2.crypto.xteaEncrypt import java.io.IOException @@ -19,7 +19,7 @@ public object Js5Compression { private const val LZMA_PRESET_DICT_SIZE_MAX = 1 shl 26 @JvmOverloads - public fun compress(input: ByteBuf, type: Js5CompressionType, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun compress(input: ByteBuf, type: Js5CompressionType, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { input.alloc().buffer().use { output -> output.writeByte(type.ordinal) @@ -62,7 +62,7 @@ public object Js5Compression { input: ByteBuf, enableLzma: Boolean = false, enableUncompressedEncryption: Boolean = false, - key: XteaKey = XteaKey.ZERO + key: SymmetricKey = SymmetricKey.ZERO ): ByteBuf { val types = mutableListOf(Js5CompressionType.BZIP2, Js5CompressionType.GZIP) if (enableLzma) { @@ -109,7 +109,7 @@ public object Js5Compression { } @JvmOverloads - public fun uncompress(input: ByteBuf, key: XteaKey = XteaKey.ZERO): ByteBuf { + public fun uncompress(input: ByteBuf, key: SymmetricKey = SymmetricKey.ZERO): ByteBuf { if (input.readableBytes() < 5) { throw IOException("Missing header") } @@ -169,10 +169,10 @@ public object Js5Compression { } public fun uncompressUnlessEncrypted(input: ByteBuf): ByteBuf? { - return uncompressIfKeyValid(input, XteaKey.ZERO) + return uncompressIfKeyValid(input, SymmetricKey.ZERO) } - public fun uncompressIfKeyValid(input: ByteBuf, key: XteaKey): ByteBuf? { + public fun uncompressIfKeyValid(input: ByteBuf, key: SymmetricKey): ByteBuf? { if (input.readableBytes() < 5) { throw IOException("Missing header") } @@ -360,7 +360,7 @@ public object Js5Compression { } } - private fun decrypt(buf: ByteBuf, len: Int, key: XteaKey): ByteBuf { + private fun decrypt(buf: ByteBuf, len: Int, key: SymmetricKey): ByteBuf { if (key.isZero) { return buf.readRetainedSlice(len) } diff --git a/cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt b/cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt index 95e4083250..3eaaba6bff 100644 --- a/cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt +++ b/cache/src/test/kotlin/org/openrs2/cache/Js5CompressionTest.kt @@ -4,7 +4,7 @@ import io.netty.buffer.ByteBuf import io.netty.buffer.Unpooled import org.openrs2.buffer.copiedBuffer import org.openrs2.buffer.use -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import java.io.IOException import kotlin.test.Test import kotlin.test.assertEquals @@ -276,7 +276,7 @@ class Js5CompressionTest { } assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO)?.release() + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO)?.release() } } } @@ -289,7 +289,7 @@ class Js5CompressionTest { } assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO)?.release() + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO)?.release() } } } @@ -301,7 +301,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -315,7 +315,7 @@ class Js5CompressionTest { } assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO)?.release() + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO)?.release() } } } @@ -327,7 +327,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -340,7 +340,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -353,7 +353,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -366,7 +366,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -379,7 +379,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -392,7 +392,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { uncompressed -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { uncompressed -> assertNull(uncompressed) } } @@ -406,7 +406,7 @@ class Js5CompressionTest { assertEquals(expected, actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertEquals(expected, actual) } @@ -429,7 +429,7 @@ class Js5CompressionTest { assertEquals(expected, actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertEquals(expected, actual) } @@ -447,7 +447,7 @@ class Js5CompressionTest { assertNull(actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertNull(actual) } @@ -462,7 +462,7 @@ class Js5CompressionTest { } read("bzip2-invalid-magic.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } @@ -476,7 +476,7 @@ class Js5CompressionTest { assertEquals(expected, actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertEquals(expected, actual) } @@ -494,7 +494,7 @@ class Js5CompressionTest { assertNull(actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertNull(actual) } @@ -509,13 +509,13 @@ class Js5CompressionTest { } read("gzip-invalid-magic.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } read("gzip-invalid-method.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } @@ -529,7 +529,7 @@ class Js5CompressionTest { assertEquals(expected, actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertEquals(expected, actual) } @@ -547,7 +547,7 @@ class Js5CompressionTest { assertNull(actual) } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertNull(actual) } @@ -562,19 +562,19 @@ class Js5CompressionTest { } read("lzma-dict-size-negative.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } read("lzma-dict-size-larger-than-preset.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } read("lzma-invalid-pb.dat").use { compressed -> - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO).use { actual -> assertNull(actual) } } @@ -584,7 +584,7 @@ class Js5CompressionTest { fun testKeyValidShorterThanTwoBlocks() { read("shorter-than-two-blocks.dat").use { compressed -> assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO)?.release() + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO)?.release() } } } @@ -597,7 +597,7 @@ class Js5CompressionTest { } assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO)?.release() + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO)?.release() } } } @@ -609,7 +609,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertNull(actual) } } @@ -622,7 +622,7 @@ class Js5CompressionTest { Js5Compression.uncompress(compressed.slice()).release() } - Js5Compression.uncompressIfKeyValid(compressed.slice(), XteaKey.ZERO).use { actual -> + Js5Compression.uncompressIfKeyValid(compressed.slice(), SymmetricKey.ZERO).use { actual -> assertNull(actual) } } @@ -719,7 +719,7 @@ class Js5CompressionTest { read("missing-header.dat").use { compressed -> assertFailsWith { - Js5Compression.uncompressIfKeyValid(compressed, XteaKey.ZERO) + Js5Compression.uncompressIfKeyValid(compressed, SymmetricKey.ZERO) } } @@ -737,7 +737,7 @@ class Js5CompressionTest { } private companion object { - private val KEY = XteaKey.fromHex("00112233445566778899AABBCCDDEEFF") - private val INVALID_KEY = XteaKey.fromHex("0123456789ABCDEF0123456789ABCDEF") + private val KEY = SymmetricKey.fromHex("00112233445566778899AABBCCDDEEFF") + private val INVALID_KEY = SymmetricKey.fromHex("0123456789ABCDEF0123456789ABCDEF") } } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt index e1dceba2e0..339fbd3062 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/CryptoJacksonModule.kt @@ -6,7 +6,7 @@ import jakarta.inject.Singleton @Singleton public class CryptoJacksonModule : SimpleModule() { init { - addDeserializer(XteaKey::class.java, XteaKeyDeserializer) - addSerializer(XteaKey::class.java, XteaKeySerializer) + addDeserializer(SymmetricKey::class.java, SymmetricKeyDeserializer) + addSerializer(SymmetricKey::class.java, SymmetricKeySerializer) } } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKey.kt similarity index 73% rename from crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt rename to crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKey.kt index bd73cc9996..f0fec5532a 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKey.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKey.kt @@ -2,7 +2,7 @@ package org.openrs2.crypto import java.security.SecureRandom -public data class XteaKey( +public data class SymmetricKey( public val k0: Int, public val k1: Int, public val k2: Int, @@ -28,28 +28,28 @@ public data class XteaKey( public companion object { @JvmStatic - public val ZERO: XteaKey = XteaKey(0, 0, 0, 0) + public val ZERO: SymmetricKey = SymmetricKey(0, 0, 0, 0) @JvmStatic @JvmOverloads - public fun generate(r: SecureRandom = secureRandom): XteaKey { - return XteaKey(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt()) + public fun generate(r: SecureRandom = secureRandom): SymmetricKey { + return SymmetricKey(r.nextInt(), r.nextInt(), r.nextInt(), r.nextInt()) } @JvmStatic - public fun fromIntArray(a: IntArray): XteaKey { + public fun fromIntArray(a: IntArray): SymmetricKey { require(a.size == 4) - return XteaKey(a[0], a[1], a[2], a[3]) + return SymmetricKey(a[0], a[1], a[2], a[3]) } @JvmStatic - public fun fromHex(s: String): XteaKey { + public fun fromHex(s: String): SymmetricKey { return fromHexOrNull(s) ?: throw IllegalArgumentException() } @JvmStatic - public fun fromHexOrNull(s: String): XteaKey? { + public fun fromHexOrNull(s: String): SymmetricKey? { if (s.length != 32) { return null } @@ -60,7 +60,7 @@ public data class XteaKey( val k2 = Integer.parseUnsignedInt(s, 16, 24, 16) val k3 = Integer.parseUnsignedInt(s, 24, 32, 16) - XteaKey(k0, k1, k2, k3) + SymmetricKey(k0, k1, k2, k3) } catch (ex: NumberFormatException) { null } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeyDeserializer.kt b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeyDeserializer.kt similarity index 55% rename from crypto/src/main/kotlin/org/openrs2/crypto/XteaKeyDeserializer.kt rename to crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeyDeserializer.kt index 4e396f8c9a..e40006a3e4 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeyDeserializer.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeyDeserializer.kt @@ -4,8 +4,8 @@ import com.fasterxml.jackson.core.JsonParser import com.fasterxml.jackson.databind.DeserializationContext import com.fasterxml.jackson.databind.deser.std.StdDeserializer -public object XteaKeyDeserializer : StdDeserializer(XteaKey::class.java) { - override fun deserialize(parser: JsonParser, ctx: DeserializationContext): XteaKey { - return XteaKey.fromIntArray(ctx.readValue(parser, IntArray::class.java)) +public object SymmetricKeyDeserializer : StdDeserializer(SymmetricKey::class.java) { + override fun deserialize(parser: JsonParser, ctx: DeserializationContext): SymmetricKey { + return SymmetricKey.fromIntArray(ctx.readValue(parser, IntArray::class.java)) } } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeySerializer.kt similarity index 67% rename from crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt rename to crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeySerializer.kt index 971a070d92..2e38a29f57 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/XteaKeySerializer.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/SymmetricKeySerializer.kt @@ -4,8 +4,8 @@ import com.fasterxml.jackson.core.JsonGenerator import com.fasterxml.jackson.databind.SerializerProvider import com.fasterxml.jackson.databind.ser.std.StdSerializer -public object XteaKeySerializer : StdSerializer(XteaKey::class.java) { - override fun serialize(value: XteaKey, gen: JsonGenerator, provider: SerializerProvider) { +public object SymmetricKeySerializer : StdSerializer(SymmetricKey::class.java) { + override fun serialize(value: SymmetricKey, gen: JsonGenerator, provider: SerializerProvider) { gen.writeStartArray() gen.writeNumber(value.k0) gen.writeNumber(value.k1) diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt b/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt index 5463d807ae..8901efee69 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/Xtea.kt @@ -7,7 +7,7 @@ private const val ROUNDS = 32 public const val XTEA_BLOCK_SIZE: Int = 8 private const val BLOCK_SIZE_MASK = XTEA_BLOCK_SIZE - 1 -public fun ByteBuf.xteaEncrypt(index: Int, length: Int, key: XteaKey) { +public fun ByteBuf.xteaEncrypt(index: Int, length: Int, key: SymmetricKey) { val k = key.toIntArray() val end = index + (length and BLOCK_SIZE_MASK.inv()) @@ -27,7 +27,7 @@ public fun ByteBuf.xteaEncrypt(index: Int, length: Int, key: XteaKey) { } } -public fun ByteBuf.xteaDecrypt(index: Int, length: Int, key: XteaKey) { +public fun ByteBuf.xteaDecrypt(index: Int, length: Int, key: SymmetricKey) { val k = key.toIntArray() val end = index + (length and BLOCK_SIZE_MASK.inv()) diff --git a/crypto/src/test/kotlin/org/openrs2/crypto/XteaTest.kt b/crypto/src/test/kotlin/org/openrs2/crypto/XteaTest.kt index bbdbc14044..d462d3be63 100644 --- a/crypto/src/test/kotlin/org/openrs2/crypto/XteaTest.kt +++ b/crypto/src/test/kotlin/org/openrs2/crypto/XteaTest.kt @@ -8,7 +8,7 @@ import kotlin.test.assertEquals class XteaTest { private class TestVector(key: String, plaintext: String, ciphertext: String) { - val key = XteaKey.fromHex(key) + val key = SymmetricKey.fromHex(key) val plaintext: ByteArray = ByteBufUtil.decodeHexDump(plaintext) val ciphertext: ByteArray = ByteBufUtil.decodeHexDump(ciphertext) } diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateAccountCodec.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateAccountCodec.kt index 1b9f185dea..4f645892a8 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateAccountCodec.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/CreateAccountCodec.kt @@ -9,8 +9,8 @@ import org.openrs2.buffer.use import org.openrs2.buffer.writeString import org.openrs2.crypto.Rsa import org.openrs2.crypto.StreamCipher +import org.openrs2.crypto.SymmetricKey import org.openrs2.crypto.XTEA_BLOCK_SIZE -import org.openrs2.crypto.XteaKey import org.openrs2.crypto.publicKey import org.openrs2.crypto.rsa import org.openrs2.crypto.secureRandom @@ -54,7 +54,7 @@ public class CreateAccountCodec @Inject constructor( val country = plaintext.readUnsignedShort() val k3 = plaintext.readInt() - val xteaKey = XteaKey(k0, k1, k2, k3) + val xteaKey = SymmetricKey(k0, k1, k2, k3) input.xteaDecrypt(input.readerIndex(), input.readableBytes(), xteaKey) val email = input.readString() @@ -84,7 +84,7 @@ public class CreateAccountCodec @Inject constructor( } override fun encode(input: LoginRequest.CreateAccount, output: ByteBuf, cipher: StreamCipher) { - val xteaKey = XteaKey.generate() + val xteaKey = SymmetricKey.generate() output.writeShort(input.build) diff --git a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/GameLoginPayload.kt b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/GameLoginPayload.kt index 0277e62ac2..04c30e97cc 100644 --- a/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/GameLoginPayload.kt +++ b/protocol/src/main/kotlin/org/openrs2/protocol/login/upstream/GameLoginPayload.kt @@ -6,7 +6,7 @@ import org.openrs2.buffer.readString import org.openrs2.buffer.use import org.openrs2.buffer.writeString import org.openrs2.crypto.Rsa -import org.openrs2.crypto.XteaKey +import org.openrs2.crypto.SymmetricKey import org.openrs2.crypto.rsa import org.openrs2.protocol.common.AntiAliasingMode import org.openrs2.protocol.common.DisplayMode @@ -28,7 +28,7 @@ public data class GameLoginPayload( val verifyId: Int, val js5ArchiveChecksums: List, // TODO(gpe): XteaKey needs a better name, as it represents an ISAAC key here - val key: XteaKey, + val key: SymmetricKey, val username: String, val password: String, ) { @@ -133,7 +133,7 @@ public data class GameLoginPayload( detailOptions, verifyId, js5ArchiveChecksums, - XteaKey(k0, k1, k2, k3), + SymmetricKey(k0, k1, k2, k3), Base37.decodeLowerCase(username), password, )