Issue with AES/DES RFC3211Wrap

MSKnete-S0/[email protected]
Newsgroups gmane.comp.encryption.bouncy-castle.devel
Message-ID <trinity-fc6d1095-1421-44e9-9265-2a9004e7af3f-1536831068373@3c-app-webde-bap32>
Hi,

I have noticed that BC 1.60 changed the behavior of wrap ciphers in a way so that they can be also used in encrypt mode for wrapping data. This apparently works fine with AESWRAP and DESEDEWRAP. However when using AESRFC3211WRAP and DESEDERFC3211WRAP the wrapped data can't be unwrapped due to a javax.crypto.BadPaddingException. Please see the following class to demonstrate the issue:

import java.security.Key;

import java.security.Provider;

import java.util.Locale;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import org.bouncycastle.util.encoders.Hex;

public class WrapTest {

private static final Key KEY_AES128 = new SecretKeySpec(Hex.decode("c794a7735f469c59cf9d7ddd8c65201d"), "AES");

private static final Key KEY_DES = new SecretKeySpec(Hex.decode("8ccbbc15340b46c7cee6e5b6d6b6bc3e08ea38b55d3e08d9"), "DES");

private static final byte[] PLAIN = "abcdefgh".getBytes();

private static Provider prov = new BouncyCastleProvider();

public static void main(String[] args) throws Exception {

String res = wrap("AESWRAP", KEY_AES128, PLAIN);

unwrap("AESWRAP", KEY_AES128, Hex.decode(res));

res = wrap("DESEDEWRAP", KEY_DES, PLAIN);

unwrap("DESEDEWRAP", KEY_DES, Hex.decode(res));

res = wrap("AESRFC3211WRAP", KEY_AES128, PLAIN);

unwrap("AESRFC3211WRAP", KEY_AES128, Hex.decode(res));

res = wrap("DESEDERFC3211WRAP", KEY_DES, PLAIN);

unwrap("DESEDERFC3211WRAP", KEY_DES, Hex.decode(res));

}

private static String wrap(String algo, Key privKey, byte[] data) throws Exception {

Cipher engine = Cipher.getInstance(algo, prov);

engine.init(Cipher.ENCRYPT_MODE, privKey);

String res = Hex.toHexString(engine.doFinal(data)).toUpperCase(Locale.ROOT);

System.out.println(String.format("%s wrapped: %s", algo, res));

return res;

}

private static String unwrap(String algo, Key privKey, byte[] data) throws Exception {

Cipher engine = Cipher.getInstance(algo, prov);

engine.init(Cipher.DECRYPT_MODE, privKey);

String res = new String(engine.doFinal(data));

System.out.println(String.format("%s unwrapped: %s", algo, res));

return res;

}

}

Is this an issue or have I just used the API wrong?

Kind Regards,

Michael Schäfer
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.