|
| 1 | +package com.ironcorelabs.tenantsecurity.kms.v1; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.security.AlgorithmParameters; |
| 5 | +import java.security.InvalidAlgorithmParameterException; |
| 6 | +import java.security.InvalidKeyException; |
| 7 | +import java.security.Key; |
| 8 | +import java.security.Provider; |
| 9 | +import java.security.SecureRandom; |
| 10 | +import java.security.spec.AlgorithmParameterSpec; |
| 11 | +import javax.crypto.BadPaddingException; |
| 12 | +import javax.crypto.Cipher; |
| 13 | +import javax.crypto.CipherSpi; |
| 14 | +import javax.crypto.IllegalBlockSizeException; |
| 15 | +import javax.crypto.ShortBufferException; |
| 16 | + |
| 17 | +// Test-only JCE provider used to reproduce the BC-FIPS AEAD buffering behavior |
| 18 | +// that surfaces in issue #167. Every engineUpdate call buffers input and returns |
| 19 | +// null; buffered bytes are released only at engineDoFinal. See |
| 20 | +// CryptoUtilsTest#streamingRoundtripWithBufferingProvider. |
| 21 | +public class BufferingGcmProvider extends Provider { |
| 22 | + public BufferingGcmProvider() { |
| 23 | + super("BufferingGcmTest", "1.0", "Test provider buffering GCM data like BC-FIPS"); |
| 24 | + put("Cipher.AES/GCM/NoPadding", BufferingGcmCipherSpi.class.getName()); |
| 25 | + } |
| 26 | + |
| 27 | + public static class BufferingGcmCipherSpi extends CipherSpi { |
| 28 | + private final Cipher delegate; |
| 29 | + private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 30 | + |
| 31 | + public BufferingGcmCipherSpi() throws Exception { |
| 32 | + delegate = Cipher.getInstance("AES/GCM/NoPadding", "SunJCE"); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected void engineSetMode(String mode) {} |
| 37 | + |
| 38 | + @Override |
| 39 | + protected void engineSetPadding(String padding) {} |
| 40 | + |
| 41 | + @Override |
| 42 | + protected int engineGetBlockSize() { |
| 43 | + return delegate.getBlockSize(); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected int engineGetOutputSize(int inputLen) { |
| 48 | + return delegate.getOutputSize(inputLen + buffer.size()); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected byte[] engineGetIV() { |
| 53 | + return delegate.getIV(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected AlgorithmParameters engineGetParameters() { |
| 58 | + return delegate.getParameters(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void engineInit(int opmode, Key key, SecureRandom random) throws InvalidKeyException { |
| 63 | + buffer.reset(); |
| 64 | + delegate.init(opmode, key, random); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, |
| 69 | + SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException { |
| 70 | + buffer.reset(); |
| 71 | + delegate.init(opmode, key, params, random); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) |
| 76 | + throws InvalidKeyException, InvalidAlgorithmParameterException { |
| 77 | + buffer.reset(); |
| 78 | + delegate.init(opmode, key, params, random); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) { |
| 83 | + if (input != null && inputLen > 0) { |
| 84 | + buffer.write(input, inputOffset, inputLen); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, |
| 91 | + int outputOffset) { |
| 92 | + if (input != null && inputLen > 0) { |
| 93 | + buffer.write(input, inputOffset, inputLen); |
| 94 | + } |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) |
| 100 | + throws IllegalBlockSizeException, BadPaddingException { |
| 101 | + if (input != null && inputLen > 0) { |
| 102 | + buffer.write(input, inputOffset, inputLen); |
| 103 | + } |
| 104 | + byte[] all = buffer.toByteArray(); |
| 105 | + buffer.reset(); |
| 106 | + return delegate.doFinal(all); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, |
| 111 | + int outputOffset) |
| 112 | + throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { |
| 113 | + byte[] result = engineDoFinal(input, inputOffset, inputLen); |
| 114 | + if (output.length - outputOffset < result.length) { |
| 115 | + throw new ShortBufferException(); |
| 116 | + } |
| 117 | + System.arraycopy(result, 0, output, outputOffset, result.length); |
| 118 | + return result.length; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments