comparison src/ch/ethz/ssh2/crypto/cipher/CTRMode.java @ 308:42b15aaa7ac7 ganymed

merge
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:21:50 -0700
parents 071eccdff8ea
children
comparison
equal deleted inserted replaced
306:90e47d99ea54 308:42b15aaa7ac7
4 */ 4 */
5 package ch.ethz.ssh2.crypto.cipher; 5 package ch.ethz.ssh2.crypto.cipher;
6 6
7 /** 7 /**
8 * This is CTR mode as described in draft-ietf-secsh-newmodes-XY.txt 8 * This is CTR mode as described in draft-ietf-secsh-newmodes-XY.txt
9 * 9 *
10 * @author Christian Plattner 10 * @author Christian Plattner
11 * @version 2.50, 03/15/10 11 * @version 2.50, 03/15/10
12 */ 12 */
13 public class CTRMode implements BlockCipher 13 public class CTRMode implements BlockCipher {
14 { 14 byte[] X;
15 byte[] X; 15 byte[] Xenc;
16 byte[] Xenc;
17 16
18 BlockCipher bc; 17 BlockCipher bc;
19 int blockSize; 18 int blockSize;
20 boolean doEncrypt; 19 boolean doEncrypt;
21 20
22 int count = 0; 21 int count = 0;
23 22
24 public void init(boolean forEncryption, byte[] key) 23 public void init(boolean forEncryption, byte[] key) {
25 { 24 }
26 }
27 25
28 public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc) throws IllegalArgumentException 26 public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc) throws IllegalArgumentException {
29 { 27 bc = tc;
30 bc = tc; 28 blockSize = bc.getBlockSize();
31 blockSize = bc.getBlockSize(); 29 doEncrypt = doEnc;
32 doEncrypt = doEnc;
33 30
34 if (blockSize != iv.length) 31 if (blockSize != iv.length)
35 throw new IllegalArgumentException("IV must be " + blockSize + " bytes long! (currently " + iv.length + ")"); 32 throw new IllegalArgumentException("IV must be " + blockSize + " bytes long! (currently " + iv.length + ")");
36 33
37 X = new byte[blockSize]; 34 X = new byte[blockSize];
38 Xenc = new byte[blockSize]; 35 Xenc = new byte[blockSize];
36 System.arraycopy(iv, 0, X, 0, blockSize);
37 }
39 38
40 System.arraycopy(iv, 0, X, 0, blockSize); 39 public final int getBlockSize() {
41 } 40 return blockSize;
41 }
42 42
43 public final int getBlockSize() 43 public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) {
44 { 44 bc.transformBlock(X, 0, Xenc, 0);
45 return blockSize;
46 }
47 45
48 public final void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) 46 for (int i = 0; i < blockSize; i++) {
49 { 47 dst[dstoff + i] = (byte)(src[srcoff + i] ^ Xenc[i]);
50 bc.transformBlock(X, 0, Xenc, 0); 48 }
51 49
52 for (int i = 0; i < blockSize; i++) 50 for (int i = (blockSize - 1); i >= 0; i--) {
53 { 51 X[i]++;
54 dst[dstoff + i] = (byte) (src[srcoff + i] ^ Xenc[i]);
55 }
56 52
57 for (int i = (blockSize - 1); i >= 0; i--) 53 if (X[i] != 0)
58 { 54 break;
59 X[i]++; 55 }
60 if (X[i] != 0) 56 }
61 break;
62
63 }
64 }
65 } 57 }