提交 f5268ed3 编写于 作者: A Andrews 提交者: GitHub

Specify rsa transform, aes & tag length (#839)

上级 1d9d596e
......@@ -89,9 +89,14 @@ public class MessageCrypto {
private static final String ECDSA = "ECDSA";
private static final String RSA = "RSA";
private static final String ECIES = "ECIES";
// Ideally the transformation should also be part of the message property. This will prevent client
// from assuming hardcoded value. However, it will increase the size of the message even further.
private static final String RSA_TRANS = "RSA/NONE/OAEPWithSHA1AndMGF1Padding";
private static final String AESGCM = "AES/GCM/NoPadding";
private static KeyGenerator keyGenerator;
private static final int tagLen = 16 * 8;
private static final int ivLen = 12;
private byte[] iv = new byte[ivLen];
private Cipher cipher;
......@@ -147,7 +152,15 @@ public class MessageCrypto {
return;
}
keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, secureRandom);
int aesKeyLength = Cipher.getMaxAllowedKeyLength("AES");
if (aesKeyLength <= 128) {
log.warn(
"{} AES Cryptographic strength is limited to {} bits. Consider installing JCE Unlimited Strength Jurisdiction Policy Files.",
logCtx, aesKeyLength);
keyGenerator.init(aesKeyLength, secureRandom);
} else {
keyGenerator.init(256, secureRandom);
}
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException e) {
......@@ -314,7 +327,7 @@ public class MessageCrypto {
// Encrypt data key using public key
if (RSA.equals(pubKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(RSA, BouncyCastleProvider.PROVIDER_NAME);
dataKeyCipher = Cipher.getInstance(RSA_TRANS, BouncyCastleProvider.PROVIDER_NAME);
} else if (ECDSA.equals(pubKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(ECIES, BouncyCastleProvider.PROVIDER_NAME);
} else {
......@@ -402,7 +415,7 @@ public class MessageCrypto {
// Create gcm param
// TODO: Replace random with counter and periodic refreshing based on timer/counter value
secureRandom.nextBytes(iv);
GCMParameterSpec gcmParam = new GCMParameterSpec(ivLen * 8, iv);
GCMParameterSpec gcmParam = new GCMParameterSpec(tagLen, iv);
// Update message metadata with encryption param
msgMetadata.setEncryptionParam(ByteString.copyFrom(iv));
......@@ -467,7 +480,7 @@ public class MessageCrypto {
// Decrypt data key using private key
if (RSA.equals(privateKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(RSA, BouncyCastleProvider.PROVIDER_NAME);
dataKeyCipher = Cipher.getInstance(RSA_TRANS, BouncyCastleProvider.PROVIDER_NAME);
} else if (ECDSA.equals(privateKey.getAlgorithm())) {
dataKeyCipher = Cipher.getInstance(ECIES, BouncyCastleProvider.PROVIDER_NAME);
} else {
......@@ -495,7 +508,7 @@ public class MessageCrypto {
ByteString ivString = msgMetadata.getEncryptionParam();
ivString.copyTo(iv, 0);
GCMParameterSpec gcmParams = new GCMParameterSpec(ivLen * 8, iv);
GCMParameterSpec gcmParams = new GCMParameterSpec(tagLen, iv);
ByteBuf targetBuf = null;
try {
cipher.init(Cipher.DECRYPT_MODE, dataKeySecret, gcmParams);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册