You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by er...@apache.org on 2004/09/30 17:46:10 UTC

svn commit: rev 47592 - incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption

Author: erodriguez
Date: Thu Sep 30 08:46:09 2004
New Revision: 47592

Added:
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/CipherType.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcCrcEncryption.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcEncryption.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd4Encryption.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd5Encryption.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionEngine.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionType.java
   incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/NullEncryption.java
Log:
kerberos encryption package

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/CipherType.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/CipherType.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import java.util.*;
+
+public final class CipherType implements Comparable {
+
+	/**
+	 * Enumeration elements are constructed once upon class loading.
+	 * Order of appearance here determines the order of compareTo.
+	 */
+	public static final CipherType NULL   = new CipherType("null");
+	public static final CipherType DES    = new CipherType("DES");
+	public static final CipherType DES3   = new CipherType("DES3");
+	public static final CipherType AES128 = new CipherType("AES128");
+
+	public String toString() {
+		return fName;
+	}
+
+	public int compareTo(Object that) {
+		return fOrdinal - ((CipherType) that).fOrdinal;
+	}
+
+	public CipherType getTypeByOrdinal(int type) {
+		for (int i = 0; i < fValues.length; i++)
+			if (fValues[i].fOrdinal == type)
+				return fValues[i];
+		return NULL;
+	}
+
+	/// PRIVATE /////
+	private final String fName;
+	private static int fNextOrdinal = 0;
+	private final int fOrdinal = fNextOrdinal++;
+
+	/**
+	 * Private constructor prevents construction outside of this class.
+	 */
+	private CipherType(String aName) {
+		fName = aName;
+	}
+
+	/**
+	 * These two lines are all that's necessary to export a List of VALUES.
+	 */
+	private static final CipherType[] fValues = {DES, DES3, AES128};
+	// VALUES needs to be located here, otherwise illegal forward reference
+	public static final List VALUES = Collections.unmodifiableList(Arrays.asList(fValues));
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcCrcEncryption.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcCrcEncryption.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,47 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.crypto.checksum.*;
+
+public class DesCbcCrcEncryption extends DesCbcEncryption {
+
+	public EncryptionType encryptionType() {
+		return EncryptionType.DES_CBC_CRC;
+	}
+
+	public ChecksumType checksumType() {
+		return ChecksumType.CRC32;
+	}
+
+	public CipherType cipherType() {
+		return CipherType.DES;
+	}
+
+	public int confounderSize() {
+		return 8;
+	}
+
+	public int checksumSize() {
+		return 4;
+	}
+
+	public int minimumPadSize() {
+		return 4;
+	}
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcEncryption.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcEncryption.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,66 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.messages.value.*;
+import org.bouncycastle.crypto.engines.*;
+import org.bouncycastle.crypto.modes.*;
+import org.bouncycastle.crypto.params.*;
+
+public abstract class DesCbcEncryption extends EncryptionEngine {
+
+	public int keyType() {
+		return EncryptionKey.KEYTYPE_DES;
+	}
+
+	public int blockSize() {
+		return 8;
+	}
+
+	public int keySize() {
+		return 8;
+	}
+	
+	// TODO - duplicated in CryptoService.
+	protected synchronized byte[] processBlockCipher(boolean encrypt, byte[] data, byte[] key, byte[] ivec) {
+		byte[] returnData = new byte[data.length];
+		CBCBlockCipher cbcCipher = new CBCBlockCipher(new DESEngine());
+		KeyParameter keyParameter = new KeyParameter(key);
+
+		if (ivec != null) {
+			ParametersWithIV kpWithIV = new ParametersWithIV(keyParameter, ivec);
+			cbcCipher.init(encrypt, kpWithIV);
+		} else
+			cbcCipher.init(encrypt, keyParameter);
+
+		int offset = 0;
+		int processedBytesLength = 0;
+
+		while (offset < returnData.length) {
+			try {
+				processedBytesLength = cbcCipher.processBlock(data, offset, returnData, offset);
+				offset += processedBytesLength;
+			} catch (Exception e) {
+				e.printStackTrace();
+				break;
+			}
+		}
+
+		return returnData;
+	}
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd4Encryption.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd4Encryption.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,43 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.crypto.checksum.*;
+
+public class DesCbcMd4Encryption extends DesCbcEncryption {
+
+	public EncryptionType encryptionType() {
+		return EncryptionType.DES_CBC_MD4;
+	}
+
+	public ChecksumType checksumType() {
+		return ChecksumType.RSA_MD4;
+	}
+
+	public int confounderSize() {
+		return 8;
+	}
+
+	public int checksumSize() {
+		return 16;
+	}
+	
+	public int minimumPadSize() {
+		return 0;
+	}
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd5Encryption.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/DesCbcMd5Encryption.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,43 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.crypto.checksum.*;
+
+public class DesCbcMd5Encryption extends DesCbcEncryption {
+
+	public EncryptionType encryptionType() {
+		return EncryptionType.DES_CBC_MD5;
+	}
+
+	public ChecksumType checksumType() {
+		return ChecksumType.RSA_MD5;
+	}
+
+	public int confounderSize() {
+		return 8;
+	}
+
+	public int checksumSize() {
+		return 16;
+	}
+	
+	public int minimumPadSize() {
+		return 0;
+	}
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionEngine.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionEngine.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,61 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.crypto.*;
+import org.apache.kerberos.crypto.checksum.*;
+import org.apache.kerberos.kdc.*;
+
+public abstract class EncryptionEngine {
+
+	public abstract EncryptionType encryptionType();
+
+	public abstract ChecksumType checksumType();
+
+	public abstract int keyType();
+
+	public abstract int confounderSize();
+
+	public abstract int checksumSize();
+
+	public abstract int blockSize();
+	
+	public abstract int minimumPadSize();
+
+	public abstract int keySize();
+
+	protected abstract byte[] processBlockCipher(boolean encrypt, byte[] data, byte[] key, byte[] ivec);
+
+	public byte[] encrypt(byte[] data, byte[] key) {
+		return processBlockCipher(true, data, key, null);
+	}
+
+	public byte[] decrypt(byte[] data, byte[] key) {
+		return processBlockCipher(false, data, key, null);
+	}
+
+	public byte[] calculateChecksum(byte[] data) {
+		ChecksumEngine digester = null;
+		try {
+			digester = CryptoService.getInstance(checksumType());
+		} catch (KerberosException ke) {
+			System.out.println(KerberosException.KDC_ERR_SUMTYPE_NOSUPP);
+		}
+		return digester.calculateChecksum(data);
+	}
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionType.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/EncryptionType.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,88 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import java.util.*;
+
+public final class EncryptionType implements Comparable {
+
+	/**
+	 * Enumeration elements are constructed once upon class loading.
+	 * Order of appearance here determines the order of compareTo.
+	 */
+	public static final EncryptionType NULL                 = new EncryptionType("null");
+	public static final EncryptionType DES_CBC_CRC          = new EncryptionType("DES CBC CRC");
+	public static final EncryptionType DES_CBC_MD4          = new EncryptionType("DES CBC MD4");
+	public static final EncryptionType DES_CBC_MD5          = new EncryptionType("DES CBC MD5");
+	public static final EncryptionType RESERVED4            = new EncryptionType("RESERVED - 4");
+	public static final EncryptionType DES3_CBC_MD5         = new EncryptionType("DES3 CBC MD5");
+	public static final EncryptionType RESERVED6            = new EncryptionType("RESERVED - 6");
+	public static final EncryptionType DES3_CBC_SHA1        = new EncryptionType("DES3 CBC SHA1");
+	public static final EncryptionType DES3_CBC_SHA1_KD     = new EncryptionType("DES3 CBC SHA1 KD");
+	public static final EncryptionType DSAWITHSHA1_CMSOID   = new EncryptionType("DSA with SHA1 CMS oid");
+	public static final EncryptionType MD5WITHRSAENCRYPTION_CMSOID  = new EncryptionType("MD5 with RSA encryption CMS oid");
+	public static final EncryptionType SHA1WITHRSAENCRYPTION_CMSOID = new EncryptionType("SHA1 with RSA encryption CMS oid");
+	public static final EncryptionType RC2CBC_ENVOID        = new EncryptionType("RC2 CBC environment oid");
+	public static final EncryptionType RSAENCRYPTION_ENVOID = new EncryptionType("RSA encryption environment oid");
+	public static final EncryptionType RSAES_OAEP_ENV_OID   = new EncryptionType("RSA ES OAEP environment oid");
+	public static final EncryptionType DES_EDE3_CBC_ENV_OID = new EncryptionType("DES EDE3 CBC environment oid");
+	public static final EncryptionType RC4_HMAC             = new EncryptionType("RC4 HMAC");
+	public static final EncryptionType PK_CROSS             = new EncryptionType("PK cross");
+
+	public String toString() {
+		return fName;
+	}
+
+	public int compareTo(Object that) {
+		return fOrdinal - ((EncryptionType) that).fOrdinal;
+	}
+
+	public static EncryptionType getTypeByOrdinal(int type) {
+		for (int i = 0; i < fValues.length; i++)
+			if (fValues[i].fOrdinal == type)
+				return fValues[i];
+		return NULL;
+	}
+	
+	public int getOrdinal() {
+		return fOrdinal;
+	}
+
+	/// PRIVATE /////
+	private final String fName;
+	private static int fNextOrdinal = 0;
+	private final int fOrdinal = fNextOrdinal++;
+
+	/**
+	 * Private constructor prevents construction outside of this class.
+	 */
+	private EncryptionType(String aName) {
+		fName = aName;
+	}
+
+	/**
+	 * These two lines are all that's necessary to export a List of VALUES.
+	 */
+	private static final EncryptionType[] fValues = {NULL, DES_CBC_CRC, DES_CBC_MD4, DES_CBC_MD5,
+			RESERVED4, DES3_CBC_MD5, RESERVED6, DES3_CBC_SHA1, DES3_CBC_SHA1_KD, DSAWITHSHA1_CMSOID,
+			MD5WITHRSAENCRYPTION_CMSOID, SHA1WITHRSAENCRYPTION_CMSOID, RC2CBC_ENVOID,
+			RSAENCRYPTION_ENVOID, RSAES_OAEP_ENV_OID, DES_EDE3_CBC_ENV_OID,	RC4_HMAC, PK_CROSS};
+	// VALUES needs to be located here, otherwise illegal forward reference
+	public static final List VALUES = Collections.unmodifiableList(Arrays.asList(fValues));
+	
+}
+

Added: incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/NullEncryption.java
==============================================================================
--- (empty file)
+++ incubator/directory/kerberos/trunk/source/main/org/apache/kerberos/crypto/encryption/NullEncryption.java	Thu Sep 30 08:46:09 2004
@@ -0,0 +1,64 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.kerberos.crypto.encryption;
+
+import org.apache.kerberos.crypto.checksum.*;
+import org.apache.kerberos.messages.value.*;
+
+public class NullEncryption extends EncryptionEngine {
+
+	public EncryptionType encryptionType() {
+		return EncryptionType.NULL;
+	}
+
+	public int keyType() {
+		return EncryptionKey.KEYTYPE_NULL;
+	}
+
+	public ChecksumType checksumType() {
+		return ChecksumType.NULL;
+	}
+
+	public int blockSize() {
+		return 1;
+	}
+
+	public int keySize() {
+		return 0;
+	}
+
+	public int checksumSize() {
+		return 0;
+	}
+
+	public int confounderSize() {
+		return 0;
+	}
+
+	public int minimumPadSize() {
+		return 0;
+	}
+
+	protected byte[] processBlockCipher(boolean encrypt, byte[] data, byte[] key, byte[] ivec) {
+		return data;
+	}
+
+	public byte[] calculateChecksum(byte[] plainText) {
+		return null;
+	}
+}
+