|
| 1 | +package org.bouncycastle.crypto.engines; |
| 2 | + |
| 3 | +import org.bouncycastle.crypto.DataLengthException; |
| 4 | +import org.bouncycastle.crypto.OutputLengthException; |
| 5 | + |
| 6 | +abstract class AEADBufferBaseEngine |
| 7 | + extends AEADBaseEngine |
| 8 | +{ |
| 9 | + protected byte[] buffer; |
| 10 | + protected byte[] aadData; |
| 11 | + protected int bufferOff; |
| 12 | + protected int aadDataOff; |
| 13 | + protected boolean aadFinished; |
| 14 | + protected boolean initialised = false; |
| 15 | + protected int AADBufferSize; |
| 16 | + protected int BlockSize; |
| 17 | + |
| 18 | + @Override |
| 19 | + public void processAADByte(byte input) |
| 20 | + { |
| 21 | + if (aadFinished) |
| 22 | + { |
| 23 | + throw new IllegalArgumentException("AAD cannot be added after reading input for " |
| 24 | + + (forEncryption ? "encryption" : "decryption")); |
| 25 | + } |
| 26 | + aadData[aadDataOff++] = input; |
| 27 | + if (aadDataOff >= AADBufferSize) |
| 28 | + { |
| 29 | + processBufferAAD(aadData, 0); |
| 30 | + aadDataOff = 0; |
| 31 | + } |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void processAADBytes(byte[] input, int inOff, int len) |
| 37 | + { |
| 38 | + if (aadFinished) |
| 39 | + { |
| 40 | + throw new IllegalArgumentException("AAD cannot be added after reading input for " |
| 41 | + + (forEncryption ? "encryption" : "decryption")); |
| 42 | + } |
| 43 | + if ((inOff + len) > input.length) |
| 44 | + { |
| 45 | + throw new DataLengthException("input buffer too short"); |
| 46 | + } |
| 47 | + int tmp; |
| 48 | + if (aadDataOff + len >= AADBufferSize) |
| 49 | + { |
| 50 | + tmp = AADBufferSize - aadDataOff; |
| 51 | + System.arraycopy(input, inOff, aadData, aadDataOff, tmp); |
| 52 | + processBufferAAD(aadData, 0); |
| 53 | + inOff += tmp; |
| 54 | + len -= tmp; |
| 55 | + aadDataOff = 0; |
| 56 | + } |
| 57 | + while (len >= AADBufferSize) |
| 58 | + { |
| 59 | + processBufferAAD(input, inOff); |
| 60 | + inOff += AADBufferSize; |
| 61 | + len -= AADBufferSize; |
| 62 | + } |
| 63 | + System.arraycopy(input, inOff, aadData, aadDataOff, len); |
| 64 | + aadDataOff += len; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int processBytes(byte[] input, int inOff, int len, byte[] output, int outOff) |
| 69 | + throws DataLengthException |
| 70 | + { |
| 71 | + if (!initialised) |
| 72 | + { |
| 73 | + throw new IllegalArgumentException(algorithmName + " needs to be initialized"); |
| 74 | + } |
| 75 | + if (inOff + len > input.length) |
| 76 | + { |
| 77 | + throw new DataLengthException("input buffer too short"); |
| 78 | + } |
| 79 | + |
| 80 | + int blockLen = len + bufferOff - (forEncryption ? 0 : MAC_SIZE); |
| 81 | + if (blockLen / BlockSize * BlockSize + outOff > output.length) |
| 82 | + { |
| 83 | + throw new OutputLengthException("output buffer is too short"); |
| 84 | + } |
| 85 | + int tmp; |
| 86 | + int rv = 0; |
| 87 | + |
| 88 | + int originalInOff = inOff; |
| 89 | + if (!forEncryption && bufferOff >= BlockSize) |
| 90 | + { |
| 91 | + processFinalAADBlock(); |
| 92 | + processBuffer(buffer, 0, output, outOff); |
| 93 | + rv += BlockSize; |
| 94 | + System.arraycopy(buffer, BlockSize, buffer, 0, bufferOff - BlockSize); |
| 95 | + bufferOff -= BlockSize; |
| 96 | + blockLen -= BlockSize; |
| 97 | + outOff += BlockSize; |
| 98 | + } |
| 99 | + if (blockLen >= BlockSize) |
| 100 | + { |
| 101 | + processFinalAADBlock(); |
| 102 | + tmp = Math.max(BlockSize - bufferOff, 0); |
| 103 | + System.arraycopy(input, inOff, buffer, bufferOff, tmp); |
| 104 | + processBuffer(buffer, 0, output, outOff); |
| 105 | + inOff += tmp; |
| 106 | + rv += BlockSize; |
| 107 | + blockLen -= BlockSize; |
| 108 | + outOff += BlockSize; |
| 109 | + bufferOff = 0; |
| 110 | + } |
| 111 | + while (blockLen >= BlockSize) |
| 112 | + { |
| 113 | + processBuffer(input, inOff, output, outOff); |
| 114 | + outOff += BlockSize; |
| 115 | + inOff += BlockSize; |
| 116 | + rv += BlockSize; |
| 117 | + blockLen -= BlockSize; |
| 118 | + } |
| 119 | + len -= inOff - originalInOff; |
| 120 | + System.arraycopy(input, inOff, buffer, bufferOff, len); |
| 121 | + bufferOff += len; |
| 122 | + return rv; |
| 123 | + } |
| 124 | + |
| 125 | + public int getBlockSize() |
| 126 | + { |
| 127 | + return BlockSize; |
| 128 | + } |
| 129 | + |
| 130 | + protected abstract void processBufferAAD(byte[] input, int inOff); |
| 131 | + |
| 132 | + protected abstract void processFinalAADBlock(); |
| 133 | + |
| 134 | + protected abstract void processBuffer(byte[] input, int inOff, byte[] output, int outOff); |
| 135 | +} |
0 commit comments