1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.crypto.encryption;
21  
22  
23  import javax.security.auth.kerberos.KerberosKey;
24  import javax.security.auth.kerberos.KerberosPrincipal;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.directory.server.kerberos.shared.messages.value.EncryptedData;
29  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
30  
31  
32  /**
33   * Test case for the DES-CBC-MD5 encryption type.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$, $Date$
37   */
38  public class DesCbcMd5EncryptionTest extends TestCase
39  {
40      private static final char[] PASSWORD = "password".toCharArray();
41  
42  
43      /**
44       * Test successful encryption and decryption when the plaintext size is less than the block size.
45       *
46       * @throws Exception
47       */
48      public void testPlainTextSizeLessThanBlockSize() throws Exception
49      {
50          KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
51          byte[] keyBytes = key.getEncoded();
52          EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_MD5, keyBytes );
53  
54          byte[] plainText =
55              { 1, 2, 3, 4, 5, 6, 7 };
56  
57          DesCbcMd5Encryption encryption = new DesCbcMd5Encryption();
58          EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
59  
60          byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
61  
62          assertTrue( beginsWith( plainText, recoveredText ) );
63      }
64  
65  
66      /**
67       * Test successful encryption and decryption when the plaintext size equals the block size.
68       *
69       * @throws Exception
70       */
71      public void testPlainTextSizeEqualsBlockSize() throws Exception
72      {
73          KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
74          byte[] keyBytes = key.getEncoded();
75          EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_MD5, keyBytes );
76  
77          byte[] plainText =
78              { 1, 2, 3, 4, 5, 6, 7, 8 };
79  
80          DesCbcMd5Encryption encryption = new DesCbcMd5Encryption();
81          EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
82  
83          byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
84  
85          assertTrue( beginsWith( plainText, recoveredText ) );
86      }
87  
88  
89      /**
90       * Test successful encryption and decryption when the plaintext size is greater than the block size.
91       *
92       * @throws Exception
93       */
94      public void testPlainTextSizeGreaterThanBlockSize() throws Exception
95      {
96          KerberosKey key = new KerberosKey( new KerberosPrincipal( "hnelson@EXAMPLE.COM" ), PASSWORD, "DES" );
97          byte[] keyBytes = key.getEncoded();
98          EncryptionKey encryptionKey = new EncryptionKey( EncryptionType.DES_CBC_MD5, keyBytes );
99  
100         byte[] plainText =
101             { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102 
103         DesCbcMd5Encryption encryption = new DesCbcMd5Encryption();
104         EncryptedData encryptedData = encryption.getEncryptedData( encryptionKey, plainText, null );
105 
106         byte[] recoveredText = encryption.getDecryptedData( encryptionKey, encryptedData, null );
107 
108         assertTrue( beginsWith( plainText, recoveredText ) );
109     }
110 
111 
112     private boolean beginsWith( byte[] plainText, byte[] recoveredText )
113     {
114         for ( int i = 0; i < plainText.length; i++ )
115         {
116             if ( plainText[i] != recoveredText[i] )
117             {
118                 return false;
119             }
120         }
121 
122         return true;
123     }
124 }