View Javadoc
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 java.security.GeneralSecurityException;
24  import java.security.NoSuchAlgorithmException;
25  
26  import javax.crypto.Cipher;
27  import javax.crypto.Mac;
28  import javax.crypto.SecretKey;
29  import javax.crypto.spec.SecretKeySpec;
30  
31  import org.apache.directory.shared.kerberos.exceptions.KerberosException;
32  import org.apache.directory.shared.kerberos.components.EncryptedData;
33  import org.apache.directory.shared.kerberos.codec.types.EncryptionType;
34  import org.apache.directory.shared.kerberos.components.EncryptionKey;
35  
36  
37  /**
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  class ArcFourHmacMd5Encryption extends EncryptionEngine
41  {
42      public EncryptionType getEncryptionType()
43      {
44          return EncryptionType.RC4_HMAC;
45      }
46  
47  
48      public int getChecksumLength()
49      {
50          return 16;
51      }
52  
53  
54      public int getConfounderLength()
55      {
56          return 8;
57      }
58  
59  
60      public byte[] getDecryptedData( EncryptionKey key, EncryptedData data, KeyUsage usage ) throws KerberosException
61      {
62          return data.getCipher();
63      }
64  
65  
66      public EncryptedData getEncryptedData( EncryptionKey key, byte[] plainText, KeyUsage usage )
67      {
68          return new EncryptedData( getEncryptionType(), key.getKeyVersion(), plainText );
69      }
70  
71  
72      public byte[] encrypt( byte[] plainText, byte[] keyBytes )
73      {
74          return processCipher( true, plainText, keyBytes );
75      }
76  
77  
78      public byte[] decrypt( byte[] cipherText, byte[] keyBytes )
79      {
80          return processCipher( false, cipherText, keyBytes );
81      }
82  
83  
84      public byte[] calculateIntegrity( byte[] data, byte[] key, KeyUsage usage )
85      {
86          try
87          {
88              Mac digester = Mac.getInstance( "HmacMD5" );
89              return digester.doFinal( data );
90          }
91          catch ( NoSuchAlgorithmException nsae )
92          {
93              return null;
94          }
95      }
96  
97  
98      private byte[] processCipher( boolean isEncrypt, byte[] data, byte[] keyBytes )
99      {
100         try
101         {
102             Cipher cipher = Cipher.getInstance( "ARCFOUR" );
103             SecretKey key = new SecretKeySpec( keyBytes, "ARCFOUR" );
104 
105             if ( isEncrypt )
106             {
107                 cipher.init( Cipher.ENCRYPT_MODE, key );
108             }
109             else
110             {
111                 cipher.init( Cipher.DECRYPT_MODE, key );
112             }
113 
114             return cipher.doFinal( data );
115         }
116         catch ( GeneralSecurityException nsae )
117         {
118             nsae.printStackTrace();
119             return null;
120         }
121     }
122 }