From 4e031ef44ce330ca6bf27810dc469b3122042574 Mon Sep 17 00:00:00 2001 From: ehsan shariati Date: Sat, 15 Jul 2023 00:16:35 -0400 Subject: [PATCH] corrected wnfs identity encryption --- .../main/java/land/fx/fula/Cryptography.java | 8 +++--- .../main/java/land/fx/fula/FulaModule.java | 27 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/land/fx/fula/Cryptography.java b/android/src/main/java/land/fx/fula/Cryptography.java index 60d9481..022bfb7 100644 --- a/android/src/main/java/land/fx/fula/Cryptography.java +++ b/android/src/main/java/land/fx/fula/Cryptography.java @@ -23,11 +23,13 @@ import javax.crypto.spec.GCMParameterSpec; public class Cryptography { - public static String encryptMsg(String message, SecretKey secret) + public static String encryptMsg(String message, SecretKey secret, byte[] iv) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); - byte[] iv = new byte[12]; // Ensure this is randomly generated for each encryption. - new SecureRandom().nextBytes(iv); + if (iv == null || iv.length == 0) { + iv = new byte[12]; // Ensure this is randomly generated for each encryption. + new SecureRandom().nextBytes(iv); + } GCMParameterSpec spec = new GCMParameterSpec(128, iv); cipher.init(Cipher.ENCRYPT_MODE, secret, spec); byte[] cipherText = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8)); diff --git a/android/src/main/java/land/fx/fula/FulaModule.java b/android/src/main/java/land/fx/fula/FulaModule.java index 7ebd6ed..35e2ca9 100755 --- a/android/src/main/java/land/fx/fula/FulaModule.java +++ b/android/src/main/java/land/fx/fula/FulaModule.java @@ -255,10 +255,9 @@ public void init(String identityString, String storePath, String bloxAddr, Strin byte[] identity = toByte(identityString); Log.d("ReactNative", "init identity= " + identityString); String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay, refresh); - Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]"); + Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + " ]"); resultData.putString("peerId", obj[0]); resultData.putString("rootCid", obj[1]); - resultData.putString("private_ref", obj[2]); promise.resolve(resultData); } catch (Exception e) { Log.d("ReactNative", "init failed with Error: " + e.getMessage()); @@ -513,7 +512,7 @@ private byte[] createPeerIdentity(byte[] identity) throws GeneralSecurityExcepti Log.d("ReactNative", "Failed to generate libp2pId: " + e.getMessage()); throw new GeneralSecurityException("Failed to generate libp2pId", e); } - encryptedLibp2pId = "FULA_ENC_V3:" + Cryptography.encryptMsg(StaticHelper.bytesToBase64(libp2pId), encryptionSecretKey); + encryptedLibp2pId = "FULA_ENC_V3:" + Cryptography.encryptMsg(StaticHelper.bytesToBase64(libp2pId), encryptionSecretKey, null); sharedPref.add(PRIVATE_KEY_STORE_PEERID, encryptedLibp2pId); } else { Log.d("ReactNative", "encryptedLibp2pId is correct. decrypting " + encryptedLibp2pId); @@ -544,19 +543,22 @@ private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) thr } private void reloadFS(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception { - Log.d("ReactNative", "getPrivateRef called: rootCid=" + rootCid); + Log.d("ReactNative", "reloadFS called: rootCid=" + rootCid); Fs.loadWithWNFSKey(iClient, wnfsKey, rootCid); - Log.d("ReactNative", "getPrivateRef completed"); + Log.d("ReactNative", "reloadFS completed"); } private boolean encrypt_and_store_config() throws Exception { try { if(this.identityEncryptedGlobal != null && !this.identityEncryptedGlobal.isEmpty()) { - String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal); + Log.d("ReactNative", "encrypt_and_store_config started"); + + String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal, null); sharedPref.add("FULA_ENC_V3:cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted); return true; } else { + Log.d("ReactNative", "encrypt_and_store_config failed because identityEncryptedGlobal is empty"); return false; } } catch (Exception e) { @@ -571,8 +573,8 @@ private boolean logoutInternal(byte[] identity, String storePath) throws Excepti this.fula.flush(); } SecretKey secretKey = Cryptography.generateKey(identity); - - String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey); + byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B }; + String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv); sharedPref.remove("FULA_ENC_V3:cid_encrypted_"+ identity_encrypted); //TODO: Should also remove peerid @Mahdi @@ -657,7 +659,10 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr } SecretKey secretKey = Cryptography.generateKey(identity); - String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey); + Log.d("ReactNative", "secretKey generated: " + secretKey.toString()); + byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B }; + String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv); + Log.d("ReactNative", "identity_encrypted generated: " + identity_encrypted + " for identity: " + Arrays.toString(identity)); this.identityEncryptedGlobal = identity_encrypted; this.secretKeyGlobal = secretKey; @@ -682,7 +687,7 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr cid = rootCid; } if(cid == null || cid.isEmpty()) { - Log.d("ReactNative", "Tried to recover cid and privateRef but was not successful. Creating new ones"); + Log.d("ReactNative", "Tried to recover cid but was not successful. Creating new ones"); this.createNewRootConfig(this.client, identity); } } else { @@ -699,7 +704,7 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid()); } String peerId = this.fula.id(); - String[] obj = new String[3]; + String[] obj = new String[2]; obj[0] = peerId; obj[1] = this.rootConfig.getCid(); Log.d("ReactNative", "initInternal is completed successfully");