添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Cipher.doFinal() to the same buffer with offset works incorretcly #2035

@alexeybakhtin

Description

Hello

I've tried BC-FIPS v2.1.0 and found regression for the
Cipher::doFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)
in the java Fips mode ( org.bouncycastle.native.cpu_variant=java )
The following code

        int INPUT_LENGTH = 32;
        int offset = 1;
        byte[] PT = new byte[INPUT_LENGTH+offset];
        SecretKey KEY = new SecretKeySpec(new byte[16], "AES");
        Cipher c = Cipher.getInstance("AES/ECB/NoPadding", Security.getProvider("BCFIPS"));
        c.init(Cipher.ENCRYPT_MODE, KEY);
        int len = c.doFinal(PT, 0, INPUT_LENGTH, PT, offset);
        System.out.println("enc len="+len);
        for (int i = offset; i < INPUT_LENGTH+offset; i++) {
            System.out.printf("%02X:",PT[i]);

Produces the following result:
enc len=32 66:E9:4B:D4:EF:8A:2C:3B:88:4C:FA:59:CA:34:2B:2E:1E:E3:76:EF:75:CB:E9:3B:52:D8:5F:1F:F1:B2:6C:FD:

So, doFinal actually encrypts only the first block, even if the return value is quals to input len

BC-FIPS v2.0.0 works correct in this case and encrypts all blocks:
enc len=32 66:E9:4B:D4:EF:8A:2C:3B:88:4C:FA:59:CA:34:2B:2E:66:E9:4B:D4:EF:8A:2C:3B:88:4C:FA:59:CA:34:2B:2E: