comparison app/src/main/java/ch/ethz/ssh2/crypto/cipher/CTRMode.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/crypto/cipher/CTRMode.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto.cipher;
6
7 /**
8 * This is CTR mode as described in draft-ietf-secsh-newmodes-XY.txt
9 *
10 * @author Christian Plattner
11 * @version 2.50, 03/15/10
12 */
13 public class CTRMode implements BlockCipher {
14 byte[] X;
15 byte[] Xenc;
16
17 BlockCipher bc;
18 int blockSize;
19 boolean doEncrypt;
20
21 int count = 0;
22
23 public void init(boolean forEncryption, byte[] key) {
24 }
25
26 public CTRMode(BlockCipher tc, byte[] iv, boolean doEnc) throws IllegalArgumentException {
27 bc = tc;
28 blockSize = bc.getBlockSize();
29 doEncrypt = doEnc;
30
31 if (blockSize != iv.length)
32 throw new IllegalArgumentException("IV must be " + blockSize + " bytes long! (currently " + iv.length + ")");
33
34 X = new byte[blockSize];
35 Xenc = new byte[blockSize];
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, byte[] dst, int dstoff) {
44 bc.transformBlock(X, 0, Xenc, 0);
45
46 for (int i = 0; i < blockSize; i++) {
47 dst[dstoff + i] = (byte)(src[srcoff + i] ^ Xenc[i]);
48 }
49
50 for (int i = (blockSize - 1); i >= 0; i--) {
51 X[i]++;
52
53 if (X[i] != 0)
54 break;
55 }
56 }
57 }