directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: DIRKRB-215 Implement the PRF function for DES_CBC. Thanks Yaning for the contribution
Date Thu, 23 Apr 2015 06:57:55 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master 8d9a4f9c8 -> 521830f32


DIRKRB-215 Implement the PRF function for DES_CBC. Thanks Yaning for the contribution


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/521830f3
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/521830f3
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/521830f3

Branch: refs/heads/master
Commit: 521830f32af56665cee43dd83d246776a011db7e
Parents: 8d9a4f9
Author: Drankye <drankye@gmail.com>
Authored: Thu Apr 23 22:55:33 2015 +0800
Committer: Drankye <drankye@gmail.com>
Committed: Thu Apr 23 22:55:33 2015 +0800

----------------------------------------------------------------------
 .../kerberos/kerb/crypto/EncTypeHandler.java    |  2 +-
 .../kerberos/kerb/crypto/enc/DesCbcEnc.java     | 11 ++-
 .../kerby/kerberos/kerb/crypto/PrfTest.java     | 91 ++++++++++++++++++++
 3 files changed, 100 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/521830f3/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/EncTypeHandler.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/EncTypeHandler.java
b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/EncTypeHandler.java
index 7a9a6a6..ba5d8d5 100644
--- a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/EncTypeHandler.java
+++ b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/EncTypeHandler.java
@@ -38,7 +38,7 @@ public interface EncTypeHandler extends CryptoTypeHandler {
 
     public int prfSize();
 
-    public byte[] prf(byte[] key, byte[] seed);
+    public byte[] prf(byte[] key, byte[] seed) throws KrbException;
 
     public int paddingSize();
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/521830f3/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/enc/DesCbcEnc.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/enc/DesCbcEnc.java
b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/enc/DesCbcEnc.java
index 38c1eb1..21d15a8 100644
--- a/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/enc/DesCbcEnc.java
+++ b/kerby-kerb/kerb-crypto/src/main/java/org/apache/kerby/kerberos/kerb/crypto/enc/DesCbcEnc.java
@@ -20,6 +20,7 @@
 package org.apache.kerby.kerberos.kerb.crypto.enc;
 
 import org.apache.kerby.kerberos.kerb.KrbErrorCode;
+import org.apache.kerby.kerberos.kerb.crypto.cksum.provider.Md5Provider;
 import org.apache.kerby.kerberos.kerb.crypto.util.Confounder;
 import org.apache.kerby.kerberos.kerb.crypto.cksum.HashProvider;
 import org.apache.kerby.kerberos.kerb.crypto.enc.provider.DesProvider;
@@ -34,9 +35,13 @@ abstract class DesCbcEnc extends AbstractEncTypeHandler {
     }
 
     @Override
-    public byte[] prf(byte[] key, byte[] seed) {
-        // TODO: krb5int_des_prf
-        return null;
+    public byte[] prf(byte[] key, byte[] seed) throws KrbException {
+        byte[] output;
+        Md5Provider md5Provider = new Md5Provider();
+        md5Provider.hash(seed);
+        output = md5Provider.output();
+        encProvider().encrypt(key, output);
+        return output;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/521830f3/kerby-kerb/kerb-crypto/src/test/java/org/apache/kerby/kerberos/kerb/crypto/PrfTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-crypto/src/test/java/org/apache/kerby/kerberos/kerb/crypto/PrfTest.java
b/kerby-kerb/kerb-crypto/src/test/java/org/apache/kerby/kerberos/kerb/crypto/PrfTest.java
new file mode 100644
index 0000000..739cc87
--- /dev/null
+++ b/kerby-kerb/kerb-crypto/src/test/java/org/apache/kerby/kerberos/kerb/crypto/PrfTest.java
@@ -0,0 +1,91 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.kerby.kerberos.kerb.crypto;
+
+import org.apache.kerby.kerberos.kerb.spec.base.EncryptionType;
+import org.apache.kerby.util.HexUtil;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.assertj.core.api.Assertions.fail;
+
+public class PrfTest {
+    static class TestCase {
+        EncryptionType encType;
+        String keyData;
+        String seed;
+        String answer;
+        TestCase(EncryptionType encType, String keyData,
+                 String seed, String answer) {
+            this.encType = encType;
+            this.keyData = keyData;
+            this.seed = seed;
+            this.answer = answer;
+        }
+
+    }
+
+    @Test
+    public void testPrf_DES_CBC_CRC() throws Exception {
+        performTest(new TestCase(
+                EncryptionType.DES_CBC_CRC,
+                "key1",
+                "0161",
+                "e91cff96b939270009308b073b66313e"
+        ));
+    }
+
+    @Test
+    public void testPrf_DES_CBC_MD4() throws Exception {
+        performTest(new TestCase(
+                EncryptionType.DES_CBC_MD4,
+                "key1",
+                "0161",
+                "e91cff96b939270009308b073b66313e"
+        ));
+    }
+
+    @Test
+    public void testPrf_DES_CBC_MD5() throws Exception {
+        performTest(new TestCase(
+                EncryptionType.DES_CBC_MD5,
+                "key1",
+                "0161",
+                "e91cff96b939270009308b073b66313e"
+        ));
+    }
+
+    private static void performTest(TestCase testCase) throws Exception {
+        byte[] keyData = EncryptionHandler.getEncHandler(testCase.encType).str2key(testCase.keyData,
testCase.keyData, null);
+        byte[] seed = HexUtil.hex2bytes(testCase.seed);
+        byte[] answer = HexUtil.hex2bytes(testCase.answer);
+        byte[] outkey = EncryptionHandler.getEncHandler(testCase.encType).prf(keyData, seed);
+
+        if (! Arrays.equals(answer, outkey)) {
+            System.err.println("failed with:");
+            System.err.println("outKey:" + HexUtil.bytesToHex(outkey));
+            System.err.println("answer:" + testCase.answer);
+            fail("KeyDerive test failed for " + testCase.encType.getName());
+        } else {
+            System.out.println("Prf test OK for " + testCase.encType.getName());
+        }
+    }
+}


Mime
View raw message