Forked curve25519 lib is replaced with the latest official maven artifact.

Use libsodium to generate user key pair seed.
This commit is contained in:
Anton Chekulaev 2020-11-24 16:57:37 +11:00
parent 1c5e62a2de
commit 0f189dd4cb
5 changed files with 19 additions and 33 deletions

View File

@ -87,23 +87,14 @@ public class IdentityKeyUtil {
} }
} }
public static void generateIdentityKeyPair(Context context, byte[] seed) { public static void generateIdentityKeyPair(Context context) {
ECKeyPair keyPair; ECKeyPair keyPair = Curve.generateKeyPair();;
if (seed != null) {
keyPair = Curve.generateKeyPair(seed);
} else {
keyPair = Curve.generateKeyPair();
}
IdentityKey publicKey = new IdentityKey(keyPair.getPublicKey()); IdentityKey publicKey = new IdentityKey(keyPair.getPublicKey());
ECPrivateKey privateKey = keyPair.getPrivateKey(); ECPrivateKey privateKey = keyPair.getPrivateKey();
save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKey.serialize())); save(context, IDENTITY_PUBLIC_KEY_PREF, Base64.encodeBytes(publicKey.serialize()));
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize())); save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize()));
} }
public static void generateIdentityKeyPair(Context context) {
generateIdentityKeyPair(context, null);
}
public static void migrateIdentityKeys(@NonNull Context context, public static void migrateIdentityKeys(@NonNull Context context,
@NonNull MasterSecret masterSecret) @NonNull MasterSecret masterSecret)
{ {

View File

@ -34,6 +34,7 @@ import org.whispersystems.signalservice.loki.protocol.sessionmanagement.SessionM
import org.whispersystems.signalservice.loki.protocol.shelved.syncmessages.SyncMessagesProtocol import org.whispersystems.signalservice.loki.protocol.shelved.syncmessages.SyncMessagesProtocol
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
import org.whispersystems.signalservice.loki.utilities.retryIfNeeded import org.whispersystems.signalservice.loki.utilities.retryIfNeeded
import java.lang.UnsupportedOperationException
class LandingActivity : BaseActionBarActivity(), LinkDeviceSlaveModeDialogDelegate { class LandingActivity : BaseActionBarActivity(), LinkDeviceSlaveModeDialogDelegate {
@ -79,14 +80,19 @@ class LandingActivity : BaseActionBarActivity(), LinkDeviceSlaveModeDialogDelega
private fun requestDeviceLink(hexEncodedPublicKey: String) { private fun requestDeviceLink(hexEncodedPublicKey: String) {
var seed: ByteArray? = null var seed: ByteArray? = null
var keyPair: ECKeyPair? = null var keyPair: ECKeyPair? = null
//FIXME AC: Previously we used the modified version of the Signal's Curve25519 lib to generate the seed and key pair.
// If you need to restore this logic you should probably fork and patch the lib to support that method as well.
// https://github.com/signalapp/curve25519-java
fun generateKeyPair() { fun generateKeyPair() {
val seedCandidate = Curve25519.getInstance(Curve25519.BEST).generateSeed(16) throw UnsupportedOperationException("Generating device link key pair is not supported at the moment.")
try { // val seedCandidate = Curve25519.getInstance(Curve25519.BEST).generateSeed(16)
keyPair = Curve.generateKeyPair(seedCandidate + seedCandidate) // Validate the seed // try {
} catch (exception: Exception) { // keyPair = Curve.generateKeyPair(seedCandidate + seedCandidate) // Validate the seed
return generateKeyPair() // } catch (exception: Exception) {
} // return generateKeyPair()
seed = seedCandidate // }
// seed = seedCandidate
} }
generateKeyPair() generateKeyPair()
IdentityKeyUtil.save(this, IdentityKeyUtil.LOKI_SEED, Hex.toStringCondensed(seed)) IdentityKeyUtil.save(this, IdentityKeyUtil.LOKI_SEED, Hex.toStringCondensed(seed))

View File

@ -13,11 +13,8 @@ import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan import android.text.style.ClickableSpan
import android.text.style.StyleSpan import android.text.style.StyleSpan
import android.util.Log
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
import com.goterl.lazycode.lazysodium.SodiumAndroid
import com.goterl.lazycode.lazysodium.utils.KeyPair import com.goterl.lazycode.lazysodium.utils.KeyPair
import kotlinx.android.synthetic.main.activity_register.* import kotlinx.android.synthetic.main.activity_register.*
import network.loki.messenger.R import network.loki.messenger.R
@ -29,11 +26,8 @@ import org.thoughtcrime.securesms.database.IdentityDatabase
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
import org.thoughtcrime.securesms.loki.utilities.push import org.thoughtcrime.securesms.loki.utilities.push
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
import org.thoughtcrime.securesms.util.Base64
import org.thoughtcrime.securesms.util.Hex
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.curve25519.Curve25519 import org.whispersystems.libsignal.ecc.ECKeyPair
import org.whispersystems.libsignal.ecc.*
import org.whispersystems.libsignal.util.KeyHelper import org.whispersystems.libsignal.util.KeyHelper
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey

View File

@ -7,13 +7,14 @@ import com.goterl.lazycode.lazysodium.utils.KeyPair
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
import org.thoughtcrime.securesms.util.Base64 import org.thoughtcrime.securesms.util.Base64
import org.thoughtcrime.securesms.util.Hex import org.thoughtcrime.securesms.util.Hex
import org.whispersystems.curve25519.Curve25519
import org.whispersystems.libsignal.ecc.DjbECPrivateKey import org.whispersystems.libsignal.ecc.DjbECPrivateKey
import org.whispersystems.libsignal.ecc.DjbECPublicKey import org.whispersystems.libsignal.ecc.DjbECPublicKey
import org.whispersystems.libsignal.ecc.ECKeyPair import org.whispersystems.libsignal.ecc.ECKeyPair
object KeyPairUtilities { object KeyPairUtilities {
private val sodium = LazySodiumAndroid(SodiumAndroid())
data class KeyPairGenerationResult( data class KeyPairGenerationResult(
val seed: ByteArray, val seed: ByteArray,
val ed25519KeyPair: KeyPair, val ed25519KeyPair: KeyPair,
@ -21,7 +22,7 @@ object KeyPairUtilities {
) )
fun generate(): KeyPairGenerationResult { fun generate(): KeyPairGenerationResult {
val seed = Curve25519.getInstance(Curve25519.BEST).generateSeed(16) val seed = sodium.randomBytesBuf(16)
try { try {
return generate(seed) return generate(seed)
} catch (exception: Exception) { } catch (exception: Exception) {
@ -30,7 +31,6 @@ object KeyPairUtilities {
} }
fun generate(seed: ByteArray): KeyPairGenerationResult { fun generate(seed: ByteArray): KeyPairGenerationResult {
val sodium = LazySodiumAndroid(SodiumAndroid())
val padding = ByteArray(16) { 0 } val padding = ByteArray(16) { 0 }
val ed25519KeyPair = sodium.cryptoSignSeedKeypair(seed + padding) val ed25519KeyPair = sodium.cryptoSignSeedKeypair(seed + padding)
val sodiumX25519KeyPair = sodium.convertKeyPairEd25519ToCurve25519(ed25519KeyPair) val sodiumX25519KeyPair = sodium.convertKeyPairEd25519ToCurve25519(ed25519KeyPair)

View File

@ -20,11 +20,6 @@ public class Curve {
return Curve25519.getInstance(BEST).isNative(); return Curve25519.getInstance(BEST).isNative();
} }
public static ECKeyPair generateKeyPair(byte[] seed) {
Curve25519KeyPair keyPair = Curve25519.getInstance(BEST).generateKeyPair(seed);
return new ECKeyPair(new DjbECPublicKey(keyPair.getPublicKey()), new DjbECPrivateKey(keyPair.getPrivateKey()));
}
public static ECKeyPair generateKeyPair() { public static ECKeyPair generateKeyPair() {
Curve25519KeyPair keyPair = Curve25519.getInstance(BEST).generateKeyPair(); Curve25519KeyPair keyPair = Curve25519.getInstance(BEST).generateKeyPair();
return new ECKeyPair(new DjbECPublicKey(keyPair.getPublicKey()), new DjbECPrivateKey(keyPair.getPrivateKey())); return new ECKeyPair(new DjbECPublicKey(keyPair.getPublicKey()), new DjbECPrivateKey(keyPair.getPrivateKey()));