KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ch > ethz > ssh2 > crypto > KeyMaterial


1
2 package ch.ethz.ssh2.crypto;
3
4
5 import ch.ethz.ssh2.crypto.digest.HashForSSH2Types;
6 import java.math.BigInteger JavaDoc;
7
8 /**
9  * Establishes key material for iv/key/mac (both directions).
10  *
11  * @author Christian Plattner, plattner@inf.ethz.ch
12  * @version $Id: KeyMaterial.java,v 1.2 2005/12/05 17:13:27 cplattne Exp $
13  */

14 public class KeyMaterial
15 {
16     public byte[] initial_iv_client_to_server;
17     public byte[] initial_iv_server_to_client;
18     public byte[] enc_key_client_to_server;
19     public byte[] enc_key_server_to_client;
20     public byte[] integrity_key_client_to_server;
21     public byte[] integrity_key_server_to_client;
22
23     private static byte[] calculateKey(HashForSSH2Types sh, BigInteger JavaDoc K, byte[] H, byte type, byte[] SessionID,
24             int keyLength)
25     {
26         byte[] res = new byte[keyLength];
27
28         int dglen = sh.getDigestLength();
29         int numRounds = (keyLength + dglen - 1) / dglen;
30
31         byte[][] tmp = new byte[numRounds][];
32
33         sh.reset();
34         sh.updateBigInt(K);
35         sh.updateBytes(H);
36         sh.updateByte(type);
37         sh.updateBytes(SessionID);
38
39         tmp[0] = sh.getDigest();
40
41         int off = 0;
42         int produced = Math.min(dglen, keyLength);
43
44         System.arraycopy(tmp[0], 0, res, off, produced);
45
46         keyLength -= produced;
47         off += produced;
48
49         for (int i = 1; i < numRounds; i++)
50         {
51             sh.updateBigInt(K);
52             sh.updateBytes(H);
53
54             for (int j = 0; j < i; j++)
55                 sh.updateBytes(tmp[j]);
56
57             tmp[i] = sh.getDigest();
58
59             produced = Math.min(dglen, keyLength);
60             System.arraycopy(tmp[i], 0, res, off, produced);
61             keyLength -= produced;
62             off += produced;
63         }
64
65         return res;
66     }
67
68     public static KeyMaterial create(String JavaDoc hashType, byte[] H, BigInteger JavaDoc K, byte[] SessionID, int keyLengthCS,
69             int blockSizeCS, int macLengthCS, int keyLengthSC, int blockSizeSC, int macLengthSC)
70             throws IllegalArgumentException JavaDoc
71     {
72         KeyMaterial km = new KeyMaterial();
73
74         HashForSSH2Types sh = new HashForSSH2Types(hashType);
75
76         km.initial_iv_client_to_server = calculateKey(sh, K, H, (byte) 'A', SessionID, blockSizeCS);
77
78         km.initial_iv_server_to_client = calculateKey(sh, K, H, (byte) 'B', SessionID, blockSizeSC);
79
80         km.enc_key_client_to_server = calculateKey(sh, K, H, (byte) 'C', SessionID, keyLengthCS);
81
82         km.enc_key_server_to_client = calculateKey(sh, K, H, (byte) 'D', SessionID, keyLengthSC);
83
84         km.integrity_key_client_to_server = calculateKey(sh, K, H, (byte) 'E', SessionID, macLengthCS);
85
86         km.integrity_key_server_to_client = calculateKey(sh, K, H, (byte) 'F', SessionID, macLengthSC);
87
88         return km;
89     }
90 }
91
Popular Tags