1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.crypto.encryption;
21
22
23 import java.security.InvalidKeyException;
24 import java.util.Arrays;
25
26 import javax.crypto.spec.DESKeySpec;
27
28 import junit.framework.TestCase;
29
30
31
32
33
34
35
36
37
38 public class DesStringToKeyTest extends TestCase
39 {
40 private byte[] fanFold1 =
41 { ( byte ) 0xC0, ( byte ) 0x1E, ( byte ) 0x38, ( byte ) 0x68, ( byte ) 0x8A, ( byte ) 0xC8, ( byte ) 0x6C,
42 ( byte ) 0x2E };
43 private byte[] intermediateKey1 =
44 { ( byte ) 0xC1, ( byte ) 0x1F, ( byte ) 0x38, ( byte ) 0x68, ( byte ) 0x8A, ( byte ) 0xC8, ( byte ) 0x6D,
45 ( byte ) 0x2F };
46 private byte[] desKey1 =
47 { ( byte ) 0xCB, ( byte ) 0xC2, ( byte ) 0x2F, ( byte ) 0xAE, ( byte ) 0x23, ( byte ) 0x52, ( byte ) 0x98,
48 ( byte ) 0xE3 };
49
50 private byte[] fanFold2 =
51 { ( byte ) 0xA0, ( byte ) 0x28, ( byte ) 0x94, ( byte ) 0x4E, ( byte ) 0xE6, ( byte ) 0x3C, ( byte ) 0x04,
52 ( byte ) 0x16 };
53 private byte[] intermediateKey2 =
54 { ( byte ) 0xA1, ( byte ) 0x29, ( byte ) 0x94, ( byte ) 0x4F, ( byte ) 0xE6, ( byte ) 0x3D, ( byte ) 0x04,
55 ( byte ) 0x16 };
56 private byte[] desKey2 =
57 { ( byte ) 0xDF, ( byte ) 0x3D, ( byte ) 0x32, ( byte ) 0xA7, ( byte ) 0x4F, ( byte ) 0xD9, ( byte ) 0x2A,
58 ( byte ) 0x01 };
59
60 private DesStringToKey stringToKey = new DesStringToKey();
61
62
63
64
65
66 public void testDesStringToKeyVector1()
67 {
68 byte[] key = stringToKey.getKey( "password", "ATHENA.MIT.EDU", "raeburn" );
69
70 assertTrue( "Key match", Arrays.equals( desKey1, key ) );
71 }
72
73
74
75
76
77 public void testDesStringToKeyVector2()
78 {
79 byte[] key = stringToKey.getKey( "potatoe", "WHITEHOUSE.GOV", "danny" );
80
81 assertTrue( "Key match", Arrays.equals( desKey2, key ) );
82 }
83
84
85
86
87
88
89
90 public void testIntermediateDesStringToKeyVector1() throws InvalidKeyException
91 {
92 String passPhrase = "passwordATHENA.MIT.EDUraeburn";
93
94 byte[] encodedByteArray = stringToKey.characterEncodeString( passPhrase );
95 byte[] paddedByteArray = stringToKey.padString( encodedByteArray );
96 byte[] fanFold = stringToKey.fanFold( paddedByteArray );
97
98 assertTrue( "Key match", Arrays.equals( fanFold1, fanFold ) );
99
100 fanFold = stringToKey.setParity( fanFold );
101 assertTrue( "Key match", Arrays.equals( intermediateKey1, fanFold ) );
102
103 byte[] secretKey = getDesKey( paddedByteArray, fanFold );
104 assertTrue( "Key match", Arrays.equals( desKey1, secretKey ) );
105 }
106
107
108
109
110
111
112
113 public void testIntermediateDesStringToKeyVector2() throws InvalidKeyException
114 {
115 String passPhrase = "potatoeWHITEHOUSE.GOVdanny";
116
117 byte[] encodedByteArray = stringToKey.characterEncodeString( passPhrase );
118 byte[] paddedByteArray = stringToKey.padString( encodedByteArray );
119 byte[] fanFold = stringToKey.fanFold( paddedByteArray );
120
121 assertTrue( "Key match", Arrays.equals( fanFold2, fanFold ) );
122
123 fanFold = stringToKey.setParity( fanFold );
124 assertTrue( "Key match", Arrays.equals( intermediateKey2, fanFold ) );
125
126 byte[] secretKey = getDesKey( paddedByteArray, fanFold );
127 assertTrue( "Key match", Arrays.equals( desKey2, secretKey ) );
128 }
129
130
131
132
133
134
135
136
137
138
139
140 private byte[] getDesKey( byte[] paddedByteArray, byte[] intermediateKey ) throws InvalidKeyException
141 {
142 if ( DESKeySpec.isWeak( intermediateKey, 0 ) )
143 {
144 intermediateKey = stringToKey.getStrongKey( intermediateKey );
145 }
146
147 byte[] secretKey = stringToKey.calculateChecksum( paddedByteArray, intermediateKey );
148
149 secretKey = stringToKey.setParity( secretKey );
150
151 if ( DESKeySpec.isWeak( secretKey, 0 ) )
152 {
153 secretKey = stringToKey.getStrongKey( secretKey );
154 }
155
156 return secretKey;
157 }
158 }