mirror of https://github.com/MISP/misp-bump
fix empty iv
parent
36a2b73d8c
commit
d90360aa45
|
@ -2,16 +2,26 @@ package lu.circl.mispbump.security;
|
||||||
|
|
||||||
import android.util.Base64;
|
import android.util.Base64;
|
||||||
|
|
||||||
import javax.crypto.*;
|
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.*;
|
import java.security.InvalidAlgorithmParameterException;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.KeyPair;
|
||||||
|
import java.security.KeyPairGenerator;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.PublicKey;
|
||||||
import java.security.spec.InvalidKeySpecException;
|
import java.security.spec.InvalidKeySpecException;
|
||||||
import java.security.spec.X509EncodedKeySpec;
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import javax.crypto.BadPaddingException;
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.IllegalBlockSizeException;
|
||||||
|
import javax.crypto.KeyAgreement;
|
||||||
|
import javax.crypto.NoSuchPaddingException;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides the functionality generate a shared secret key.
|
* This class provides the functionality generate a shared secret key.
|
||||||
* Furthermore it contains the encryption/decryption methods.
|
* Furthermore it contains the encryption/decryption methods.
|
||||||
|
@ -39,6 +49,7 @@ public class DiffieHellman {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Singleton pattern
|
* Singleton pattern
|
||||||
|
*
|
||||||
* @return {@link DiffieHellman}
|
* @return {@link DiffieHellman}
|
||||||
*/
|
*/
|
||||||
public static DiffieHellman getInstance() {
|
public static DiffieHellman getInstance() {
|
||||||
|
@ -71,6 +82,7 @@ public class DiffieHellman {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypts data.
|
* Encrypts data.
|
||||||
|
*
|
||||||
* @param data data to encrypt
|
* @param data data to encrypt
|
||||||
* @return To String converted and encrypted data
|
* @return To String converted and encrypted data
|
||||||
*/
|
*/
|
||||||
|
@ -82,16 +94,16 @@ public class DiffieHellman {
|
||||||
byte[] cipherText = c.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
byte[] cipherText = c.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||||
return Base64.encodeToString(cipherText, Base64.NO_WRAP);
|
return Base64.encodeToString(cipherText, Base64.NO_WRAP);
|
||||||
|
|
||||||
} catch (BadPaddingException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException e) {
|
} catch (BadPaddingException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
|
||||||
e.printStackTrace();
|
|
||||||
} catch (InvalidAlgorithmParameterException e) {
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts data with the current shared secret.
|
* Decrypts data with the current shared secret.
|
||||||
|
*
|
||||||
* @param data data to decrypt
|
* @param data data to decrypt
|
||||||
* @return To String converted and decrypted data
|
* @return To String converted and decrypted data
|
||||||
*/
|
*/
|
||||||
|
@ -113,6 +125,7 @@ public class DiffieHellman {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a shared secret and derives an initialisation vector from it.
|
* Generates a shared secret and derives an initialisation vector from it.
|
||||||
|
*
|
||||||
* @param pk public key of the sync partner
|
* @param pk public key of the sync partner
|
||||||
*/
|
*/
|
||||||
public void setForeignPublicKey(PublicKey pk) {
|
public void setForeignPublicKey(PublicKey pk) {
|
||||||
|
@ -122,8 +135,9 @@ public class DiffieHellman {
|
||||||
byte[] tmpSharedSecret = keyAgreement.generateSecret();
|
byte[] tmpSharedSecret = keyAgreement.generateSecret();
|
||||||
sharedSecret = Arrays.copyOfRange(tmpSharedSecret, 0, 32);
|
sharedSecret = Arrays.copyOfRange(tmpSharedSecret, 0, 32);
|
||||||
|
|
||||||
byte[] inputVector = Arrays.copyOfRange(sharedSecret, 32, 48);
|
byte[] inputVector = Arrays.copyOfRange(tmpSharedSecret, 32, 48);
|
||||||
ivParameterSpec = new IvParameterSpec(inputVector);
|
ivParameterSpec = new IvParameterSpec(inputVector);
|
||||||
|
|
||||||
} catch (InvalidKeyException e) {
|
} catch (InvalidKeyException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue