01: package ch.ethz.ssh2.crypto.cipher;
02:
03: /**
04: * This is CTR mode as described in draft-ietf-secsh-newmodes-XY.txt
05: *
06: * @author Christian Plattner, plattner@inf.ethz.ch
07: * @version $Id: CTRMode.java,v 1.1 2005/06/06 12:44:25 cplattne Exp $
08: */
09: public class CTRMode implements BlockCipher {
10: byte[] X;
11: byte[] Xenc;
12:
13: BlockCipher bc;
14: int blockSize;
15: boolean doEncrypt;
16:
17: int count = 0;
18:
19: public void init(boolean forEncryption, byte[] key) {
20: }
21:
22: public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc)
23: throws IllegalArgumentException {
24: bc = tc;
25: blockSize = bc.getBlockSize();
26: doEncrypt = doEnc;
27:
28: if (blockSize != iv.length)
29: throw new IllegalArgumentException("IV must be "
30: + blockSize + " bytes long! (currently "
31: + iv.length + ")");
32:
33: X = new byte[blockSize];
34: Xenc = new byte[blockSize];
35:
36: System.arraycopy(iv, 0, X, 0, blockSize);
37: }
38:
39: public final int getBlockSize() {
40: return blockSize;
41: }
42:
43: public final void transformBlock(byte[] src, int srcoff,
44: byte[] dst, int dstoff) {
45: bc.transformBlock(X, 0, Xenc, 0);
46:
47: for (int i = 0; i < blockSize; i++) {
48: dst[dstoff + i] = (byte) (src[srcoff + i] ^ Xenc[i]);
49: }
50:
51: for (int i = (blockSize - 1); i >= 0; i--) {
52: X[i]++;
53: if (X[i] != 0)
54: break;
55:
56: }
57: }
58: }
|