|
| 1 | +package internal.org.springframework.content.encryption.engine; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | +import java.math.BigInteger; |
| 5 | +import java.security.NoSuchAlgorithmException; |
| 6 | +import java.security.SecureRandom; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.function.Function; |
| 9 | +import javax.crypto.Cipher; |
| 10 | +import javax.crypto.CipherInputStream; |
| 11 | +import javax.crypto.KeyGenerator; |
| 12 | +import javax.crypto.spec.IvParameterSpec; |
| 13 | +import lombok.SneakyThrows; |
| 14 | +import org.springframework.content.encryption.engine.ContentEncryptionEngine; |
| 15 | + |
| 16 | +/** |
| 17 | + * Symmetric data encryption engine using AES-CTR encryption mode |
| 18 | + */ |
| 19 | +public class AesCtrEncryptionEngine implements ContentEncryptionEngine { |
| 20 | + private final KeyGenerator keyGenerator; |
| 21 | + private static final SecureRandom secureRandom = new SecureRandom(); |
| 22 | + |
| 23 | + private static final int AES_BLOCK_SIZE_BYTES = 16; // AES has a 128-bit block size |
| 24 | + private static final int IV_SIZE_BYTES = AES_BLOCK_SIZE_BYTES; // IV is the same size as a block |
| 25 | + |
| 26 | + @SneakyThrows({NoSuchAlgorithmException.class}) |
| 27 | + public AesCtrEncryptionEngine(int keySizeBits) { |
| 28 | + keyGenerator = KeyGenerator.getInstance("AES"); |
| 29 | + keyGenerator.init(keySizeBits, secureRandom); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public EncryptionParameters createNewParameters() { |
| 34 | + var secretKey = keyGenerator.generateKey(); |
| 35 | + byte[] iv = new byte[IV_SIZE_BYTES]; |
| 36 | + secureRandom.nextBytes(iv); |
| 37 | + return new EncryptionParameters( |
| 38 | + secretKey, |
| 39 | + iv |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @SneakyThrows |
| 44 | + private Cipher initializeCipher(EncryptionParameters parameters, boolean forEncryption) { |
| 45 | + Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding"); |
| 46 | + cipher.init( |
| 47 | + forEncryption?Cipher.ENCRYPT_MODE:Cipher.DECRYPT_MODE, |
| 48 | + parameters.getSecretKey(), |
| 49 | + new IvParameterSpec(parameters.getInitializationVector()) |
| 50 | + ); |
| 51 | + |
| 52 | + return cipher; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public InputStream encrypt(InputStream plainText, EncryptionParameters encryptionParameters) { |
| 57 | + return new CipherInputStream(plainText, initializeCipher(encryptionParameters, true)); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public InputStream decrypt( |
| 62 | + Function<InputStreamRequestParameters, InputStream> cipherTextStreamRequest, |
| 63 | + EncryptionParameters encryptionParameters, |
| 64 | + InputStreamRequestParameters requestParameters |
| 65 | + ) { |
| 66 | + var blockStartOffset = calculateBlockOffset(requestParameters.getStartByteOffset()); |
| 67 | + |
| 68 | + var adjustedIv = adjustIvForOffset(encryptionParameters.getInitializationVector(), blockStartOffset); |
| 69 | + |
| 70 | + var adjustedParameters = new EncryptionParameters( |
| 71 | + encryptionParameters.getSecretKey(), |
| 72 | + adjustedIv |
| 73 | + ); |
| 74 | + |
| 75 | + var byteStartOffset = blockStartOffset * AES_BLOCK_SIZE_BYTES; |
| 76 | + |
| 77 | + var cipherTextStream = cipherTextStreamRequest.apply(requestParameters); |
| 78 | + |
| 79 | + var cipher = initializeCipher(adjustedParameters, false); |
| 80 | + |
| 81 | + return new ZeroPrefixedInputStream( |
| 82 | + new EnsureSingleSkipInputStream( |
| 83 | + new CipherInputStream( |
| 84 | + new SkippingInputStream( |
| 85 | + cipherTextStream, |
| 86 | + byteStartOffset |
| 87 | + ), |
| 88 | + cipher |
| 89 | + ) |
| 90 | + ), |
| 91 | + byteStartOffset |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private static long calculateBlockOffset(long offsetBytes) { |
| 96 | + return (offsetBytes - (offsetBytes % AES_BLOCK_SIZE_BYTES)) / AES_BLOCK_SIZE_BYTES; |
| 97 | + } |
| 98 | + |
| 99 | + private byte[] adjustIvForOffset(byte[] iv, long offsetBlocks) { |
| 100 | + // Optimization: no need to adjust the IV when we have no block offset |
| 101 | + if(offsetBlocks == 0) { |
| 102 | + return iv; |
| 103 | + } |
| 104 | + |
| 105 | + // AES-CTR works by having a separate IV for every block. |
| 106 | + // This block IV is built from the initial IV and the block counter. |
| 107 | + var initialIv = new BigInteger(1, iv); |
| 108 | + byte[] bigintBytes = initialIv.add(BigInteger.valueOf(offsetBlocks)) |
| 109 | + .toByteArray(); |
| 110 | + |
| 111 | + // Because we're using BigInteger for math here, |
| 112 | + // the resulting byte array may be longer (when overflowing the IV size, we should wrap around) |
| 113 | + // or shorter (when our IV starts with a bunch of 0) |
| 114 | + // It needs to be the proper length, and aligned properly |
| 115 | + if(bigintBytes.length == AES_BLOCK_SIZE_BYTES) { |
| 116 | + return bigintBytes; |
| 117 | + } else if(bigintBytes.length > AES_BLOCK_SIZE_BYTES) { |
| 118 | + // Byte array is longer, we need to cut a part of the front |
| 119 | + return Arrays.copyOfRange(bigintBytes, bigintBytes.length-IV_SIZE_BYTES, bigintBytes.length); |
| 120 | + } else { |
| 121 | + // Byte array is shorter, we need to pad the front with 0 bytes |
| 122 | + // Note that a bytes array is initialized to be all-zero by default |
| 123 | + byte[] ivBytes = new byte[IV_SIZE_BYTES]; |
| 124 | + System.arraycopy(bigintBytes, 0, ivBytes, IV_SIZE_BYTES-bigintBytes.length, bigintBytes.length); |
| 125 | + return ivBytes; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments