Added test for AuthenticationFailedException during filename decryption

This commit is contained in:
Sebastian Stenzel
2016-01-02 14:40:17 +01:00
parent ff4448bac0
commit f46a79fa63
7 changed files with 38 additions and 12 deletions

View File

@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2016 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*
* Contributors:
* Sebastian Stenzel - initial API and implementation
*******************************************************************************/
package org.cryptomator.crypto.engine;
public class AuthenticationFailedException extends CryptoException {
public AuthenticationFailedException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Sebastian Stenzel and others.
* Copyright (c) 2016 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*
@@ -8,11 +8,7 @@
*******************************************************************************/
package org.cryptomator.crypto.engine;
import java.io.IOException;
public class CryptoException extends IOException {
private static final long serialVersionUID = -6536997506620449023L;
abstract class CryptoException extends RuntimeException {
public CryptoException(String message, Throwable cause) {
super(message, cause);

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Sebastian Stenzel and others.
* Copyright (c) 2015, 2016 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*

View File

@@ -8,7 +8,6 @@
*******************************************************************************/
package org.cryptomator.crypto.engine.impl;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@@ -18,7 +17,7 @@ import javax.crypto.SecretKey;
import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.BaseNCodec;
import org.cryptomator.crypto.engine.CryptoException;
import org.cryptomator.crypto.engine.AuthenticationFailedException;
import org.cryptomator.crypto.engine.FilenameCryptor;
import org.cryptomator.siv.SivMode;
@@ -58,7 +57,7 @@ class FilenameCryptorImpl implements FilenameCryptor {
final byte[] cleartextBytes = AES_SIV.decrypt(encryptionKey, macKey, encryptedBytes);
return new String(cleartextBytes, StandardCharsets.UTF_8);
} catch (AEADBadTagException e) {
throw new UncheckedIOException(new CryptoException("Authentication failed.", e));
throw new AuthenticationFailedException("Authentication failed.", e);
}
}

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Sebastian Stenzel and others.
* Copyright (c) 2015, 2016 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*

View File

@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Sebastian Stenzel and others.
* Copyright (c) 2015, 2016 Sebastian Stenzel and others.
* This file is licensed under the terms of the MIT license.
* See the LICENSE.txt file for more info.
*

View File

@@ -9,11 +9,13 @@
package org.cryptomator.crypto.engine.impl;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.cryptomator.crypto.engine.AuthenticationFailedException;
import org.cryptomator.crypto.engine.FilenameCryptor;
import org.junit.Assert;
import org.junit.Test;
@@ -62,4 +64,16 @@ public class FilenameCryptorImplTest {
}
}
@Test(expected = AuthenticationFailedException.class)
public void testDecryptionOfManipulatedFilename() {
final byte[] keyBytes = new byte[32];
final SecretKey encryptionKey = new SecretKeySpec(keyBytes, "AES");
final SecretKey macKey = new SecretKeySpec(keyBytes, "AES");
final FilenameCryptor filenameCryptor = new FilenameCryptorImpl(encryptionKey, macKey);
final byte[] encrypted = filenameCryptor.encryptFilename("test").getBytes(StandardCharsets.UTF_8);
encrypted[0] ^= (byte) 0x01; // change 1 bit in first byte
filenameCryptor.decryptFilename(new String(encrypted, StandardCharsets.UTF_8));
}
}