From 98d25539b99895158fe8a496a4b5419ec19b03b8 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 7 Feb 2021 11:15:49 +0000 Subject: [PATCH] Combine RSA encrypt and decrypt methods into a single crypt method This allows us to speed up master index signing with the CRT. Signed-off-by: Graham --- .../kotlin/org/openrs2/cache/Js5MasterIndex.kt | 7 +++---- crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt | 14 +++----------- .../src/test/kotlin/org/openrs2/crypto/RsaTest.kt | 14 +++++++------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt index fad6f308..871c3c7e 100644 --- a/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt +++ b/cache/src/main/kotlin/org/openrs2/cache/Js5MasterIndex.kt @@ -7,8 +7,7 @@ import org.openrs2.buffer.crc32 import org.openrs2.buffer.use import org.openrs2.crypto.Rsa import org.openrs2.crypto.Whirlpool -import org.openrs2.crypto.rsaDecrypt -import org.openrs2.crypto.rsaEncrypt +import org.openrs2.crypto.rsaCrypt import org.openrs2.crypto.whirlpool public data class Js5MasterIndex( @@ -92,7 +91,7 @@ public data class Js5MasterIndex( plaintext.writeByte(Rsa.MAGIC) plaintext.writeBytes(digest) - plaintext.rsaEncrypt(key).use { ciphertext -> + plaintext.rsaCrypt(key).use { ciphertext -> buf.writeBytes(ciphertext) } } @@ -218,7 +217,7 @@ public data class Js5MasterIndex( private fun decrypt(buf: ByteBuf, key: RSAKeyParameters?): ByteBuf { return if (key != null) { - buf.rsaDecrypt(key) + buf.rsaCrypt(key) } else { buf.retain() } diff --git a/crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt b/crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt index 5d9760f9..e680a04d 100644 --- a/crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt +++ b/crypto/src/main/kotlin/org/openrs2/crypto/Rsa.kt @@ -40,12 +40,8 @@ private fun BigInteger.toByteBuf(): ByteBuf { return Unpooled.wrappedBuffer(toByteArray()) } -public fun ByteBuf.rsaEncrypt(key: RSAKeyParameters): ByteBuf { - return Rsa.encrypt(toBigInteger(), key).toByteBuf() -} - -public fun ByteBuf.rsaDecrypt(key: RSAKeyParameters): ByteBuf { - return Rsa.decrypt(toBigInteger(), key).toByteBuf() +public fun ByteBuf.rsaCrypt(key: RSAKeyParameters): ByteBuf { + return Rsa.crypt(toBigInteger(), key).toByteBuf() } public fun RSAPrivateCrtKeyParameters.toKeySpec(): KeySpec { @@ -87,10 +83,6 @@ public object Rsa { return Pair(keyPair.public as RSAKeyParameters, keyPair.private as RSAPrivateCrtKeyParameters) } - public fun encrypt(plaintext: BigInteger, key: RSAKeyParameters): BigInteger { - return plaintext.modPow(key.exponent, key.modulus) - } - private fun generateBlindingFactor(m: BigInteger): Pair { val max = m - BigInteger.ONE @@ -105,7 +97,7 @@ public object Rsa { } } - public fun decrypt(ciphertext: BigInteger, key: RSAKeyParameters): BigInteger { + public fun crypt(ciphertext: BigInteger, key: RSAKeyParameters): BigInteger { if (key is RSAPrivateCrtKeyParameters) { // blind the input val e = key.publicExponent diff --git a/crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt b/crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt index 38c209f3..78be3038 100644 --- a/crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt +++ b/crypto/src/test/kotlin/org/openrs2/crypto/RsaTest.kt @@ -51,34 +51,34 @@ object RsaTest { val (public, private) = Rsa.generateKeyPair(Rsa.CLIENT_KEY_LENGTH) val expectedPlaintext = BigInteger("1337") - val ciphertext = Rsa.encrypt(expectedPlaintext, public) - val actualPlaintext = Rsa.decrypt(ciphertext, private) + val ciphertext = Rsa.crypt(expectedPlaintext, public) + val actualPlaintext = Rsa.crypt(ciphertext, private) assertEquals(expectedPlaintext, actualPlaintext) } @Test fun testEncrypt() { - val ciphertext = Rsa.encrypt(BigInteger("65"), PUBLIC_KEY) + val ciphertext = Rsa.crypt(BigInteger("65"), PUBLIC_KEY) assertEquals(BigInteger("2790"), ciphertext) } @Test fun testDecrypt() { - val ciphertext = Rsa.decrypt(BigInteger("2790"), PRIVATE_KEY) + val ciphertext = Rsa.crypt(BigInteger("2790"), PRIVATE_KEY) assertEquals(BigInteger("65"), ciphertext) } @Test fun testDecryptCrt() { - val ciphertext = Rsa.decrypt(BigInteger("2790"), PRIVATE_KEY_CRT) + val ciphertext = Rsa.crypt(BigInteger("2790"), PRIVATE_KEY_CRT) assertEquals(BigInteger("65"), ciphertext) } @Test fun testEncryptByteBuf() { wrappedBuffer(65).use { plaintext -> - plaintext.rsaEncrypt(PUBLIC_KEY).use { ciphertext -> + plaintext.rsaCrypt(PUBLIC_KEY).use { ciphertext -> wrappedBuffer(10, 230.toByte()).use { expectedCiphertext -> assertEquals(expectedCiphertext, ciphertext) } @@ -89,7 +89,7 @@ object RsaTest { @Test fun testDecryptByteBuf() { wrappedBuffer(10, 230.toByte()).use { ciphertext -> - ciphertext.rsaDecrypt(PRIVATE_KEY).use { plaintext -> + ciphertext.rsaCrypt(PRIVATE_KEY).use { plaintext -> wrappedBuffer(65).use { expectedPlaintext -> assertEquals(expectedPlaintext, plaintext) }