comparison app/src/main/java/ch/ethz/ssh2/crypto/KeyMaterial.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/KeyMaterial.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;
6
7 import java.io.IOException;
8 import java.math.BigInteger;
9
10 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
11
12 /**
13 * Establishes key material for iv/key/mac (both directions).
14 *
15 * @author Christian Plattner
16 * @version 2.50, 03/15/10
17 */
18 public class KeyMaterial {
19 public byte[] initial_iv_client_to_server;
20 public byte[] initial_iv_server_to_client;
21 public byte[] enc_key_client_to_server;
22 public byte[] enc_key_server_to_client;
23 public byte[] integrity_key_client_to_server;
24 public byte[] integrity_key_server_to_client;
25
26 private static byte[] calculateKey(HashForSSH2Types sh, BigInteger K, byte[] H, byte type, byte[] SessionID,
27 int keyLength) throws IOException {
28 byte[] res = new byte[keyLength];
29 int dglen = sh.getDigestLength();
30 int numRounds = (keyLength + dglen - 1) / dglen;
31 byte[][] tmp = new byte[numRounds][];
32 sh.reset();
33 sh.updateBigInt(K);
34 sh.updateBytes(H);
35 sh.updateByte(type);
36 sh.updateBytes(SessionID);
37 tmp[0] = sh.getDigest();
38 int off = 0;
39 int produced = Math.min(dglen, keyLength);
40 System.arraycopy(tmp[0], 0, res, off, produced);
41 keyLength -= produced;
42 off += produced;
43
44 for (int i = 1; i < numRounds; i++) {
45 sh.updateBigInt(K);
46 sh.updateBytes(H);
47
48 for (int j = 0; j < i; j++) {
49 sh.updateBytes(tmp[j]);
50 }
51
52 tmp[i] = sh.getDigest();
53 produced = Math.min(dglen, keyLength);
54 System.arraycopy(tmp[i], 0, res, off, produced);
55 keyLength -= produced;
56 off += produced;
57 }
58
59 return res;
60 }
61
62 public static KeyMaterial create(String hashType, byte[] H, BigInteger K, byte[] SessionID, int keyLengthCS,
63 int blockSizeCS, int macLengthCS, int keyLengthSC, int blockSizeSC, int macLengthSC)
64 throws IOException {
65 KeyMaterial km = new KeyMaterial();
66 HashForSSH2Types sh = new HashForSSH2Types(hashType);
67 km.initial_iv_client_to_server = calculateKey(sh, K, H, (byte) 'A', SessionID, blockSizeCS);
68 km.initial_iv_server_to_client = calculateKey(sh, K, H, (byte) 'B', SessionID, blockSizeSC);
69 km.enc_key_client_to_server = calculateKey(sh, K, H, (byte) 'C', SessionID, keyLengthCS);
70 km.enc_key_server_to_client = calculateKey(sh, K, H, (byte) 'D', SessionID, keyLengthSC);
71 km.integrity_key_client_to_server = calculateKey(sh, K, H, (byte) 'E', SessionID, macLengthCS);
72 km.integrity_key_server_to_client = calculateKey(sh, K, H, (byte) 'F', SessionID, macLengthSC);
73 return km;
74 }
75 }