comparison src/com/trilead/ssh2/crypto/cipher/CTRMode.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.crypto.cipher;
3
4 /**
5 * This is CTR mode as described in draft-ietf-secsh-newmodes-XY.txt
6 *
7 * @author Christian Plattner, plattner@trilead.com
8 * @version $Id: CTRMode.java,v 1.1 2007/10/15 12:49:55 cplattne Exp $
9 */
10 public class CTRMode implements BlockCipher {
11 byte[] X;
12 byte[] Xenc;
13
14 BlockCipher bc;
15 int blockSize;
16 boolean doEncrypt;
17
18 int count = 0;
19
20 public void init(boolean forEncryption, byte[] key) {
21 }
22
23 public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc) 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 " + blockSize + " bytes long! (currently " + iv.length + ")");
30
31 X = new byte[blockSize];
32 Xenc = new byte[blockSize];
33 System.arraycopy(iv, 0, X, 0, blockSize);
34 }
35
36 public final int getBlockSize() {
37 return blockSize;
38 }
39
40 public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) {
41 bc.transformBlock(X, 0, Xenc, 0);
42
43 for (int i = 0; i < blockSize; i++) {
44 dst[dstoff + i] = (byte)(src[srcoff + i] ^ Xenc[i]);
45 }
46
47 for (int i = (blockSize - 1); i >= 0; i--) {
48 X[i]++;
49
50 if (X[i] != 0)
51 break;
52 }
53 }
54 }