Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions crypto/crypto.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// crypto module build file

// jdkVersion = System.properties['java.version']
// isJdk6 = jdkVersion >= '1.6'
int maxAESKeySize = javax.crypto.Cipher.getMaxAllowedKeyLength('AES')

configure(project.tasks.withType(Test)) {
if (maxAESKeySize < 256) {
println "AES keysize limited to $maxAESKeySize, skipping EncryptorsTests"
exclude '**/EncryptorsTests.class'
}
}

dependencies {
optional 'org.bouncycastle:bcpkix-jdk15on:1.54'
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@
*/
package org.springframework.security.crypto.encrypt;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.AssumptionViolatedException;
import org.junit.Before;
import org.junit.Test;
import org.springframework.security.crypto.codec.Hex;
Expand Down Expand Up @@ -53,7 +47,7 @@ public void setup() {

@Test
public void bouncyCastleAesCbcWithPredictableIvEquvalent() throws Exception {
assumeAes256Available(CipherAlgorithm.CBC);
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
Expand All @@ -63,7 +57,7 @@ public void bouncyCastleAesCbcWithPredictableIvEquvalent() throws Exception {

@Test
public void bouncyCastleAesCbcWithSecureIvCompatible() throws Exception {
assumeAes256Available(CipherAlgorithm.CBC);
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
Expand All @@ -73,7 +67,7 @@ public void bouncyCastleAesCbcWithSecureIvCompatible() throws Exception {

@Test
public void bouncyCastleAesGcmWithPredictableIvEquvalent() throws Exception {
assumeAes256Available(CipherAlgorithm.GCM);
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
Expand All @@ -83,7 +77,7 @@ public void bouncyCastleAesGcmWithPredictableIvEquvalent() throws Exception {

@Test
public void bouncyCastleAesGcmWithSecureIvCompatible() throws Exception {
assumeAes256Available(CipherAlgorithm.GCM);
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt,
Expand Down Expand Up @@ -116,25 +110,6 @@ private void testCompatibility(BytesEncryptor left, BytesEncryptor right)
Assert.assertArrayEquals(testData, rightDecrypted);
}

private void assumeAes256Available(CipherAlgorithm cipherAlgorithm) {
boolean aes256Available = false;
try {
Cipher.getInstance(cipherAlgorithm.toString());
aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
}
catch (NoSuchAlgorithmException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " not available, skipping test", e);
}
catch (NoSuchPaddingException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " padding not available, skipping test", e);
}
Assume.assumeTrue(
"AES key length of 256 not allowed, skipping test",
aes256Available);

}

/**
* A BytesKeyGenerator that always generates the same sequence of values
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2011-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.crypto.encrypt;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

import org.junit.Assume;
import org.junit.AssumptionViolatedException;
import org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgorithm;

public class CryptoAssumptions {

public static void assumeGCMJCE() {
assumeAes256(CipherAlgorithm.GCM);
}

public static void assumeCBCJCE() {
assumeAes256(CipherAlgorithm.CBC);
}

private static void assumeAes256(CipherAlgorithm cipherAlgorithm) {
boolean aes256Available = false;
try {
Cipher.getInstance(cipherAlgorithm.toString());
aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
}
catch (NoSuchAlgorithmException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " not available, skipping test", e);
}
catch (NoSuchPaddingException e) {
throw new AssumptionViolatedException(
cipherAlgorithm + " padding not available, skipping test", e);
}
Assume.assumeTrue(
"AES key length of 256 not allowed, skipping test",
aes256Available);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.security.GeneralSecurityException;

import javax.crypto.Cipher;

import org.junit.Assume;
import org.junit.Test;

public class EncryptorsTests {

@Test
public void stronger() throws Exception {
Assume.assumeTrue("GCM must be available for this test", isAesGcmAvailable());

CryptoAssumptions.assumeGCMJCE();
BytesEncryptor encryptor = Encryptors.stronger("password", "5c0744940b5c369b");
byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
assertThat(result).isNotNull();
Expand All @@ -41,6 +35,7 @@ public void stronger() throws Exception {

@Test
public void standard() throws Exception {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor encryptor = Encryptors.standard("password", "5c0744940b5c369b");
byte[] result = encryptor.encrypt("text".getBytes("UTF-8"));
assertThat(result).isNotNull();
Expand All @@ -52,8 +47,7 @@ public void standard() throws Exception {

@Test
public void preferred() {
Assume.assumeTrue("GCM must be available for this test", isAesGcmAvailable());

CryptoAssumptions.assumeGCMJCE();
TextEncryptor encryptor = Encryptors.delux("password", "5c0744940b5c369b");
String result = encryptor.encrypt("text");
assertThat(result).isNotNull();
Expand All @@ -64,6 +58,7 @@ public void preferred() {

@Test
public void text() {
CryptoAssumptions.assumeCBCJCE();
TextEncryptor encryptor = Encryptors.text("password", "5c0744940b5c369b");
String result = encryptor.encrypt("text");
assertThat(result).isNotNull();
Expand All @@ -74,6 +69,7 @@ public void text() {

@Test
public void queryableText() {
CryptoAssumptions.assumeCBCJCE();
TextEncryptor encryptor = Encryptors.queryableText("password",
"5c0744940b5c369b");
String result = encryptor.encrypt("text");
Expand All @@ -90,13 +86,4 @@ public void noOpText() {
assertThat(encryptor.decrypt("text")).isEqualTo("text");
}

private boolean isAesGcmAvailable() {
try {
Cipher.getInstance("AES/GCM/NoPadding");
return true;
}
catch (GeneralSecurityException e) {
return false;
}
}
}