android unit-testing encryption

Error de cifrado en Android 4.2



unit-testing encryption (3)

Como señaló Brigham , en Android 4.2, hubo una mejora de seguridad , que actualizó la implementación predeterminada de SecureRandom de Crypto a OpenSSL

Criptografía : modificó las implementaciones predeterminadas de SecureRandom y Cipher.RSA para usar OpenSSL. Se agregó compatibilidad Socket SSL para TLSv1.1 y TLSv1.2 usando OpenSSL 1.0.1

La respuesta de Bu Brigham es una solución temporal y no recomendada, porque aunque resuelve el problema, todavía está haciendo lo incorrecto.

La forma recomendada (consulte el tutorial de Nelenkov ) es utilizar derivaciones de clave adecuadas PKCS (Public Key Cryptography Standard), que define dos funciones de derivación de clave, PBKDF1 y PBKDF2, de las cuales PBKDF2 es más recomendable.

Así es como debes obtener la clave,

int iterationCount = 1000; int saltLength = 8; // bytes; 64 bits int keyLength = 256; SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltLength]; random.nextBytes(salt); KeySpec keySpec = new PBEKeySpec(seed.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory .getInstance("PBKDF2WithHmacSHA1"); byte[] raw = keyFactory.generateSecret(keySpec).getEncoded();

El siguiente código está funcionando en todas las versiones de Android, excepto la última 4.2

import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * Util class to perform encryption/decryption over strings. <br/> */ public final class UtilsEncryption { /** The logging TAG */ private static final String TAG = UtilsEncryption.class.getName(); /** */ private static final String KEY = "some_encryption_key"; /** * Avoid instantiation. <br/> */ private UtilsEncryption() { } /** The HEX characters */ private final static String HEX = "0123456789ABCDEF"; /** * Encrypt a given string. <br/> * * @param the string to encrypt * @return the encrypted string in HEX */ public static String encrypt( String cleartext ) { try { byte[] result = process( Cipher.ENCRYPT_MODE, cleartext.getBytes() ); return toHex( result ); } catch ( Exception e ) { System.out.println( TAG + ":encrypt:" + e.getMessage() ); } return null; } /** * Decrypt a HEX encrypted string. <br/> * * @param the HEX string to decrypt * @return the decrypted string */ public static String decrypt( String encrypted ) { try { byte[] enc = fromHex( encrypted ); byte[] result = process( Cipher.DECRYPT_MODE, enc ); return new String( result ); } catch ( Exception e ) { System.out.println( TAG + ":decrypt:" + e.getMessage() ); } return null; } /** * Get the raw encryption key. <br/> * * @param the seed key * @return the raw key * @throws NoSuchAlgorithmException */ private static byte[] getRawKey() throws NoSuchAlgorithmException { KeyGenerator kgen = KeyGenerator.getInstance( "AES" ); SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG" ); sr.setSeed( KEY.getBytes() ); kgen.init( 128, sr ); SecretKey skey = kgen.generateKey(); return skey.getEncoded(); } /** * Process the given input with the provided mode. <br/> * * @param the cipher mode * @param the value to process * @return the processed value as byte[] * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException */ private static byte[] process( int mode, byte[] value ) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { SecretKeySpec skeySpec = new SecretKeySpec( getRawKey(), "AES" ); Cipher cipher = Cipher.getInstance( "AES" ); cipher.init( mode, skeySpec ); byte[] encrypted = cipher.doFinal( value ); return encrypted; } /** * Decode an HEX encoded string into a byte[]. <br/> * * @param the HEX string value * @return the decoded byte[] */ protected static byte[] fromHex( String value ) { int len = value.length() / 2; byte[] result = new byte[len]; for ( int i = 0; i < len; i++ ) { result[i] = Integer.valueOf( value.substring( 2 * i, 2 * i + 2 ), 16 ).byteValue(); } return result; } /** * Encode a byte[] into an HEX string. <br/> * * @param the byte[] value * @return the HEX encoded string */ protected static String toHex( byte[] value ) { if ( value == null ) { return ""; } StringBuffer result = new StringBuffer( 2 * value.length ); for ( int i = 0; i < value.length; i++ ) { byte b = value[i]; result.append( HEX.charAt( ( b >> 4 ) & 0x0f ) ); result.append( HEX.charAt( b & 0x0f ) ); } return result.toString(); } }

Aquí hay una pequeña prueba de unidad que he creado para reproducir el error

import junit.framework.TestCase; public class UtilsEncryptionTest extends TestCase { /** A random string */ private static String ORIGINAL = "some string to test"; /** * The HEX value corresponds to ORIGINAL. <br/> * If you change ORIGINAL, calculate the new value on one of this sites: * <ul> * <li>http://www.string-functions.com/string-hex.aspx</li> * <li>http://www.yellowpipe.com/yis/tools/encrypter/index.php</li> * <li>http://www.convertstring.com/EncodeDecode/HexEncode</li> * </ul> */ private static String HEX = "736F6D6520737472696E6720746F2074657374"; public void testToHex() { String hexString = UtilsEncryption.toHex( ORIGINAL.getBytes() ); assertNotNull( "The HEX string should not be null", hexString ); assertTrue( "The HEX string should not be empty", hexString.length() > 0 ); assertEquals( "The HEX string was not encoded correctly", HEX, hexString ); } public void testFromHex() { byte[] stringBytes = UtilsEncryption.fromHex( HEX ); assertNotNull( "The HEX string should not be null", stringBytes ); assertTrue( "The HEX string should not be empty", stringBytes.length > 0 ); assertEquals( "The HEX string was not encoded correctly", ORIGINAL, new String( stringBytes ) ); } public void testWholeProcess() { String encrypted = UtilsEncryption.encrypt( ORIGINAL ); assertNotNull( "The encrypted result should not be null", encrypted ); assertTrue( "The encrypted result should not be empty", encrypted.length() > 0 ); String decrypted = UtilsEncryption.decrypt( encrypted ); assertNotNull( "The decrypted result should not be null", decrypted ); assertTrue( "The decrypted result should not be empty", decrypted.length() > 0 ); assertEquals( "Something went wrong", ORIGINAL, decrypted ); }

}

La línea que arroja la excepción es:

byte[] encrypted = cipher.doFinal( value );

El seguimiento completo de la pila es:

W/<package>.UtilsEncryption:decrypt(16414): pad block corrupted W/System.err(16414): javax.crypto.BadPaddingException: pad block corrupted W/System.err(16414): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:709) W/System.err(16414): at javax.crypto.Cipher.doFinal(Cipher.java:1111) W/System.err(16414): at <package>.UtilsEncryption.process(UtilsEncryption.java:117) W/System.err(16414): at <package>.UtilsEncryption.decrypt(UtilsEncryption.java:69) W/System.err(16414): at <package>.UtilsEncryptionTest.testWholeProcess(UtilsEncryptionTest.java:74) W/System.err(16414): at java.lang.reflect.Method.invokeNative(Native Method) W/System.err(16414): at java.lang.reflect.Method.invoke(Method.java:511) W/System.err(16414): at junit.framework.TestCase.runTest(TestCase.java:168) W/System.err(16414): at junit.framework.TestCase.runBare(TestCase.java:134) W/System.err(16414): at junit.framework.TestResult$1.protect(TestResult.java:115) W/System.err(16414): at junit.framework.TestResult.runProtected(TestResult.java:133) D/elapsed ( 588): 14808 W/System.err(16414): at junit.framework.TestResult.run(TestResult.java:118) W/System.err(16414): at junit.framework.TestCase.run(TestCase.java:124) W/System.err(16414): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190) W/System.err(16414): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175) W/System.err(16414): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555) W/System.err(16414): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)

¿Alguien tiene una idea de lo que podría estar pasando? ¿Alguien sabe de un cambio de rotura en Android 4.2 en cualquiera de las clases a las que se hace referencia?

Muchas gracias


Desde la página de Android Jellybean :

Modificó las implementaciones predeterminadas de SecureRandom y Cipher.RSA para usar OpenSSL

Cambiaron el proveedor predeterminado de SecureRandom para usar OpenSSL en lugar del proveedor de Crypto anterior.

El siguiente código producirá dos salidas diferentes en pre-Android 4.2 y Android 4.2:

SecureRandom rand = SecureRandom.getInstance("SHA1PRNG"); Log.i(TAG, "rand.getProvider(): " + rand.getProvider().getName());

En dispositivos pre-4.2:

rand.getProvider: Crypto

En 4.2 dispositivos:

rand.getProvider: AndroidOpenSSL

Afortunadamente, es fácil volver al comportamiento anterior:

SecureRandom sr = SecureRandom.getInstance( "SHA1PRNG", "Crypto" );

Para estar seguro, es peligroso llamar SecureRandom.setSeed a la luz de los Javadocs que dicen:

Seeding SecureRandom puede ser inseguro

Una semilla es una matriz de bytes utilizada para arrancar la generación de números aleatorios. Para producir números aleatorios criptográficamente seguros, tanto la semilla como el algoritmo deben ser seguros.

Por defecto, las instancias de esta clase generarán una semilla inicial usando una fuente de entropía interna, como / dev / urandom. Esta semilla es impredecible y apropiada para un uso seguro.

De forma alternativa, puede especificar la semilla inicial explícitamente con el constructor sembrado o llamando a setSeed (byte []) antes de que se hayan generado números aleatorios. Al especificar una semilla fija, la instancia devolverá una secuencia predecible de números. Esto puede ser útil para las pruebas, pero no es apropiado para un uso seguro.

Sin embargo, para escribir pruebas unitarias, como lo está haciendo, usar setSeed puede estar bien.


Entonces, lo que está intentando es usar un generador pseudoaleatorio como una función de derivación de clave . Esto es malo por las siguientes razones:

  • Los PRNG son, por diseño, no deterministas y usted confía en que sean deterministas
  • Confiar en un error y las implementaciones obsoletas romperán su aplicación algún día
  • PRNG no están diseñados para ser buenos KDF

Más precisamente, Google desaprobó el uso del proveedor de Crypto en Android N (SDK 24)

Aquí hay algunos mejores métodos:

Función de derivación de clave basada en código de autenticación de mensajes hash (HMAC) (HKDF)

Usando esta library :

String userInput = "this is a user input with bad entropy"; HKDF hkdf = HKDF.fromHmacSha256(); //extract the "raw" data to create output with concentrated entropy byte[] pseudoRandomKey = hkdf.extract(staticSalt32Byte, userInput.getBytes(StandardCharsets.UTF_8)); //create expanded bytes for e.g. AES secret key and IV byte[] expandedAesKey = hkdf.expand(pseudoRandomKey, "aes-key".getBytes(StandardCharsets.UTF_8), 16); //Example boilerplate encrypting a simple string with created key/iv SecretKey key = new SecretKeySpec(expandedAesKey, "AES"); //AES-128 key

PBKDF2 (función de derivación de clave basada en contraseña 2)

tiene un estiramiento de tecla que hace que sea más costoso forzar la fuerza bruta. Use esto para la entrada de la tecla débil (como la contraseña del usuario):

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); return secretKey;

Hay más KDF como BCrypt , scrypt y Argon2