Blame SOURCES/jss-HMAC-test-for-AES-encrypt-unwrap.patch

50aabd
# HG changeset patch
50aabd
# User Jack Magne <jmagne@redhat.com>
50aabd
# Date 1504307754 25200
50aabd
#      Fri Sep 01 16:15:54 2017 -0700
50aabd
# Node ID eec15518fd61f1d988c25b4de589555796f9e65f
50aabd
# Parent  17d1d7b740ca5777fbcf8ee817a2f26b9c93593a
50aabd
unwrapping of HMAC-SHA1 secret keys using AES wrapping and unwrapping
50aabd
cfu on behalf of jmagne
50aabd
50aabd
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/pkcs11/PK11KeyWrapper.java
50aabd
--- a/org/mozilla/jss/pkcs11/PK11KeyWrapper.java	Mon May 01 10:39:50 2017 -0700
50aabd
+++ b/org/mozilla/jss/pkcs11/PK11KeyWrapper.java	Fri Sep 01 16:15:54 2017 -0700
50aabd
@@ -588,6 +588,8 @@
50aabd
             return EncryptionAlgorithm.RC4;
50aabd
         } else if( type == SymmetricKey.AES ) {
50aabd
             return EncryptionAlgorithm.AES_128_ECB;
50aabd
+        } else if( type == SymmetricKey.SHA1_HMAC) {
50aabd
+            return HMACAlgorithm.SHA1;
50aabd
         } else  {
50aabd
             Assert._assert( type == SymmetricKey.RC2 );
50aabd
             return EncryptionAlgorithm.RC2_CBC;
50aabd
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/pkcs11/PK11MessageDigest.c
50aabd
--- a/org/mozilla/jss/pkcs11/PK11MessageDigest.c	Mon May 01 10:39:50 2017 -0700
50aabd
+++ b/org/mozilla/jss/pkcs11/PK11MessageDigest.c	Fri Sep 01 16:15:54 2017 -0700
50aabd
@@ -67,19 +67,19 @@
50aabd
     }
50aabd
 
50aabd
     /* copy the key, setting the CKA_SIGN attribute */
50aabd
-    /*
50aabd
+    
50aabd
     newKey = PK11_CopySymKeyForSigning(origKey, mech);
50aabd
+
50aabd
+    /* For some key on the hsm, this call could fail, but the key may work anyway */
50aabd
+
50aabd
     if( newKey == NULL ) {
50aabd
-        JSS_throwMsg(env, DIGEST_EXCEPTION,
50aabd
-                        "Unable to set CKA_SIGN attribute on symmetric key");
50aabd
-        goto finish;
50aabd
+        newKey = origKey;
50aabd
     }
50aabd
-    */
50aabd
 
50aabd
     param.data = NULL;
50aabd
     param.len = 0;
50aabd
 
50aabd
-    context = PK11_CreateContextBySymKey(mech, CKA_SIGN, origKey, ¶m;;
50aabd
+    context = PK11_CreateContextBySymKey(mech, CKA_SIGN, newKey, ¶m;;
50aabd
     if( context == NULL ) {
50aabd
         JSS_throwMsg(env, DIGEST_EXCEPTION,
50aabd
             "Unable to initialize digest context");
50aabd
@@ -88,7 +88,7 @@
50aabd
 
50aabd
     contextObj = JSS_PK11_wrapCipherContextProxy(env, &context);
50aabd
 finish:
50aabd
-    if(newKey) {
50aabd
+    if(newKey && (newKey != origKey)) {
50aabd
         /* SymKeys are ref counted, and the context will free it's ref
50aabd
          * when it is destroyed */
50aabd
         PK11_FreeSymKey(newKey);
50aabd
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/tests/HmacTest.java
50aabd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
50aabd
+++ b/org/mozilla/jss/tests/HmacTest.java	Fri Sep 01 16:15:54 2017 -0700
50aabd
@@ -0,0 +1,119 @@
50aabd
+
50aabd
+package org.mozilla.jss.tests;
50aabd
+
50aabd
+
50aabd
+import java.security.Key;
50aabd
+import javax.crypto.Cipher;
50aabd
+import javax.crypto.KeyGenerator;
50aabd
+import javax.crypto.Mac;
50aabd
+import javax.crypto.SecretKey;
50aabd
+import javax.crypto.spec.IvParameterSpec;
50aabd
+
50aabd
+import org.mozilla.jss.CryptoManager;
50aabd
+import org.mozilla.jss.crypto.CryptoToken;
50aabd
+import org.mozilla.jss.crypto.SymmetricKey;
50aabd
+
50aabd
+
50aabd
+public class HmacTest {
50aabd
+
50aabd
+  private static final String INTERNAL_KEY_STORAGE_TOKEN =
50aabd
+    new CryptoManager.InitializationValues("").getInternalKeyStorageTokenDescription().trim();
50aabd
+
50aabd
+  private static final String NSS_DATABASE_DIR = "sql:data";
50aabd
+  private static final String PROVIDER = "Mozilla-JSS";
50aabd
+
50aabd
+
50aabd
+  public static void main(String[] args)
50aabd
+   {
50aabd
+
50aabd
+    String algorithm = "hmac-sha1";
50aabd
+
50aabd
+    try {
50aabd
+       configureCrypto(args);
50aabd
+
50aabd
+       Mac mac = Mac.getInstance(algorithm, PROVIDER);
50aabd
+
50aabd
+       byte[] keyData = new byte[16];
50aabd
+       Key key = importHmacSha1Key(keyData);
50aabd
+
50aabd
+       mac.init(key);
50aabd
+
50aabd
+       doHMAC(mac,"Dogtag rules!");
50aabd
+
50aabd
+       System.out.println("Done");
50aabd
+
50aabd
+       System.exit(0);
50aabd
+    } catch (Exception e) {
50aabd
+        System.exit(1);
50aabd
+    }
50aabd
+  }
50aabd
+
50aabd
+  private static void configureCrypto(String[] args)
50aabd
+    throws Exception {
50aabd
+
50aabd
+    CryptoManager.InitializationValues initializationValues =
50aabd
+      new CryptoManager.InitializationValues(args[0]);
50aabd
+
50aabd
+    CryptoManager.initialize(initializationValues);
50aabd
+
50aabd
+    CryptoManager cryptoManager = CryptoManager.getInstance();
50aabd
+
50aabd
+    CryptoToken cryptoToken =
50aabd
+      cryptoManager.getTokenByName(INTERNAL_KEY_STORAGE_TOKEN);
50aabd
+
50aabd
+    cryptoManager.setThreadToken(cryptoToken);
50aabd
+  }
50aabd
+
50aabd
+  private static Key importHmacSha1Key(byte[] key)
50aabd
+    throws Exception {
50aabd
+
50aabd
+    final String WRAPPING_ALGORITHM = "AES/CBC/PKCS5Padding";
50aabd
+
50aabd
+    Key wrappingKey = getWrappingKey();
50aabd
+
50aabd
+    byte[] iv = new byte[16];
50aabd
+    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
50aabd
+
50aabd
+    Cipher wrappingCipher = Cipher.getInstance(WRAPPING_ALGORITHM, PROVIDER);
50aabd
+    wrappingCipher.init(Cipher.ENCRYPT_MODE, wrappingKey, ivParameterSpec);
50aabd
+
50aabd
+    byte[] wrappedKeyData = wrappingCipher.doFinal(key);
50aabd
+
50aabd
+    Cipher unwrappingCipher = Cipher.getInstance(WRAPPING_ALGORITHM, PROVIDER);
50aabd
+    unwrappingCipher.init(Cipher.UNWRAP_MODE, wrappingKey, ivParameterSpec);
50aabd
+
50aabd
+    return (SecretKey) unwrappingCipher.unwrap(wrappedKeyData,
50aabd
+                                               SymmetricKey.SHA1_HMAC.toString(),
50aabd
+                                               Cipher.SECRET_KEY);
50aabd
+  }
50aabd
+
50aabd
+  private static synchronized Key getWrappingKey()
50aabd
+    throws Exception {
50aabd
+
50aabd
+    final String keyGenAlgorithm = "AES";
50aabd
+    final int wrappingKeyLength = 256;
50aabd
+
50aabd
+    KeyGenerator keyGen = KeyGenerator.getInstance(keyGenAlgorithm, PROVIDER);
50aabd
+    keyGen.init(wrappingKeyLength);
50aabd
+    return keyGen.generateKey();
50aabd
+  }
50aabd
+
50aabd
+  public static void doHMAC(Mac mozillaHmac, String clearText)
50aabd
+            throws Exception {
50aabd
+        byte[] mozillaHmacOut;
50aabd
+
50aabd
+        //Get the Mozilla HMAC
50aabd
+        mozillaHmacOut = mozillaHmac.doFinal(clearText.getBytes());
50aabd
+
50aabd
+        if (mozillaHmacOut.length == mozillaHmac.getMacLength()) {
50aabd
+            System.out.println(PROVIDER + " supports " +
50aabd
+                    mozillaHmac.getAlgorithm() + "  and the output size is " + mozillaHmac.getMacLength());
50aabd
+        } else {
50aabd
+            throw new Exception("ERROR: hmac output size is " +
50aabd
+                    mozillaHmacOut.length + ", should be " +
50aabd
+                    mozillaHmac.getMacLength());
50aabd
+        }
50aabd
+    }
50aabd
+
50aabd
+
50aabd
+}
50aabd
diff -r 17d1d7b740ca -r eec15518fd61 org/mozilla/jss/tests/all.pl
50aabd
--- a/org/mozilla/jss/tests/all.pl	Mon May 01 10:39:50 2017 -0700
50aabd
+++ b/org/mozilla/jss/tests/all.pl	Fri Sep 01 16:15:54 2017 -0700
50aabd
@@ -492,6 +492,10 @@
50aabd
 $command = "$java -cp $jss_classpath org.mozilla.jss.tests.HMACTest $testdir $pwfile";
50aabd
 run_test($testname, $command);
50aabd
 
50aabd
+$testname = "HMAC Unwrap";
50aabd
+$command = "$java -cp $jss_classpath org.mozilla.jss.tests.HmacTest $testdir $pwfile";
50aabd
+run_test($testname, $command);
50aabd
+
50aabd
 $testname = "KeyWrapping ";
50aabd
 $command = "$java -cp $jss_classpath org.mozilla.jss.tests.JCAKeyWrap $testdir $pwfile";
50aabd
 run_test($testname, $command);