comparison src/ch/ethz/ssh2/crypto/cipher/CBCMode.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 * CBCMode. 8 * CBCMode.
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 CBCMode implements BlockCipher 13 public class CBCMode implements BlockCipher {
14 { 14 BlockCipher tc;
15 BlockCipher tc; 15 int blockSize;
16 int blockSize; 16 boolean doEncrypt;
17 boolean doEncrypt;
18 17
19 byte[] cbc_vector; 18 byte[] cbc_vector;
20 byte[] tmp_vector; 19 byte[] tmp_vector;
21 20
22 public void init(boolean forEncryption, byte[] key) 21 public void init(boolean forEncryption, byte[] key) {
23 { 22 }
24 }
25
26 public CBCMode(BlockCipher tc, byte[] iv, boolean doEncrypt)
27 throws IllegalArgumentException
28 {
29 this.tc = tc;
30 this.blockSize = tc.getBlockSize();
31 this.doEncrypt = doEncrypt;
32 23
33 if (this.blockSize != iv.length) 24 public CBCMode(BlockCipher tc, byte[] iv, boolean doEncrypt)
34 throw new IllegalArgumentException("IV must be " + blockSize 25 throws IllegalArgumentException {
35 + " bytes long! (currently " + iv.length + ")"); 26 this.tc = tc;
27 this.blockSize = tc.getBlockSize();
28 this.doEncrypt = doEncrypt;
36 29
37 this.cbc_vector = new byte[blockSize]; 30 if (this.blockSize != iv.length)
38 this.tmp_vector = new byte[blockSize]; 31 throw new IllegalArgumentException("IV must be " + blockSize
39 System.arraycopy(iv, 0, cbc_vector, 0, blockSize); 32 + " bytes long! (currently " + iv.length + ")");
40 }
41 33
42 public int getBlockSize() 34 this.cbc_vector = new byte[blockSize];
43 { 35 this.tmp_vector = new byte[blockSize];
44 return blockSize; 36 System.arraycopy(iv, 0, cbc_vector, 0, blockSize);
45 } 37 }
46 38
47 private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) 39 public int getBlockSize() {
48 { 40 return blockSize;
49 for (int i = 0; i < blockSize; i++) 41 }
50 cbc_vector[i] ^= src[srcoff + i];
51 42
52 tc.transformBlock(cbc_vector, 0, dst, dstoff); 43 private void encryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) {
44 for (int i = 0; i < blockSize; i++)
45 cbc_vector[i] ^= src[srcoff + i];
53 46
54 System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize); 47 tc.transformBlock(cbc_vector, 0, dst, dstoff);
55 } 48 System.arraycopy(dst, dstoff, cbc_vector, 0, blockSize);
49 }
56 50
57 private void decryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) 51 private void decryptBlock(byte[] src, int srcoff, byte[] dst, int dstoff) {
58 { 52 /* Assume the worst, src and dst are overlapping... */
59 /* Assume the worst, src and dst are overlapping... */ 53 System.arraycopy(src, srcoff, tmp_vector, 0, blockSize);
60 54 tc.transformBlock(src, srcoff, dst, dstoff);
61 System.arraycopy(src, srcoff, tmp_vector, 0, blockSize);
62
63 tc.transformBlock(src, srcoff, dst, dstoff);
64
65 for (int i = 0; i < blockSize; i++)
66 dst[dstoff + i] ^= cbc_vector[i];
67 55
68 /* ...that is why we need a tmp buffer. */ 56 for (int i = 0; i < blockSize; i++)
69 57 dst[dstoff + i] ^= cbc_vector[i];
70 byte[] swap = cbc_vector;
71 cbc_vector = tmp_vector;
72 tmp_vector = swap;
73 }
74 58
75 public void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) 59 /* ...that is why we need a tmp buffer. */
76 { 60 byte[] swap = cbc_vector;
77 if (doEncrypt) 61 cbc_vector = tmp_vector;
78 encryptBlock(src, srcoff, dst, dstoff); 62 tmp_vector = swap;
79 else 63 }
80 decryptBlock(src, srcoff, dst, dstoff); 64
81 } 65 public void transformBlock(byte[] src, int srcoff, byte[] dst, int dstoff) {
66 if (doEncrypt)
67 encryptBlock(src, srcoff, dst, dstoff);
68 else
69 decryptBlock(src, srcoff, dst, dstoff);
70 }
82 } 71 }