Hi there,
I'm attempting to use a SealedObject with gcj.
I had the source working with the sun jdk but my gcj-compiled source at
runtime doesn't succeed to get past the "nosuchmethoderror".
I have done some experimenting with gnu(classpath I imagine), and
gnu-crypto. From what I understand bouncy castle is now part of gnu
classpath. Am I mistaken?
Any gcj/gnu classpath guru out there willing to nudge me in the right
direction?
Thanks in advance.
Cheers :)
-----------------------------------------------------------------------
Here is the output:
export
LD_LIBRARY_PATH=/usr/local/gnu-crypto/lib:/usr/local/gnu-crypto/share:.;./testaes
provider:GNU-CRYPTO
getInfo:GNU Crypto JCE Provider
getVersion:2.0
rijndael cipher created...
with keysize:16
with blocksize:16
Running 1 iterations:
Encryption: time = 1.0, speed = 0.015625 KB/s
Decryption: decryptedMessage:<<0123456789ABCDEF>>
time = 1.0, speed = 0.015625 KB/s
cipher algorithm count:113
algorithm:PBEWITHHMACHAVALANDKHAZAD
...
algorithm:AES
message authentication codes algorithm count:15
algorithm:HMAC-SHA160
algorithm:TMMH16
...
message digest algorithm count:12
...
algorithm:SHA-160
algorithm:WHIRLPOOL
...
secure random algorithm count:15
...
algorithm:WHIRLPOOLPRNG
...
signature algorithm count:2
algorithm:DSS/RAW
algorithm:RSA-PSS/RAW
Exception in thread "main" java.lang.NoSuchMethodError
at gnu.crypto.mode.ModeFactory.getInstance(java.lang.String,
gnu.crypto.cipher.IBlockCipher, int)
(/usr/local/gnu-crypto/lib/lib-gnu-crypto.so.2.0.0)
at
gnu.crypto.jce.cipher.CipherAdapter.CipherAdapter(java.lang.String, int)
(/usr/local/gnu-crypto/lib/lib-gnu-crypto.so.2.0.0)
at gnu.crypto.jce.cipher.RijndaelSpi.RijndaelSpi()
(/usr/local/gnu-crypto/lib/lib-gnu-crypto.so.2.0.0)
at java.lang.Class.newInstance() (/usr/lib/libgcj.so.6.0.0)
at
javax.crypto.JCEUtil.getImplementationFromProvider(java.lang.String,
java.lang.String, java.security.Provider)
(/usr/local/gnu-crypto/lib/lib-javax-crypto.so.1.0.0)
at javax.crypto.JCEUtil.getImplementation(java.lang.String,
java.lang.String, java.lang.String)
(/usr/local/gnu-crypto/lib/lib-javax-crypto.so.1.0.0)
at javax.crypto.Cipher.getInstance(java.lang.String)
(/usr/local/gnu-crypto/lib/lib-javax-crypto.so.1.0.0)
at testaes.main(java.lang.String[]) (Unknown Source)
at gnu.java.lang.MainThread.call_main() (/usr/lib/libgcj.so.6.0.0)
at gnu.java.lang.MainThread.run() (/usr/lib/libgcj.so.6.0.0)
Here is the example associated:
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;
import java.util.HashSet;
import java.util.Set;
import gnu.crypto.Registry;
import gnu.crypto.cipher.CipherFactory;
import gnu.crypto.cipher.IBlockCipher;
import gnu.crypto.jce.GnuCrypto;
import java.security.Provider;
import java.security.Security;
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.SecretKeySpec;
public class testaes
{
static class mytestdata implements java.io.Serializable
{
String s1;
int x1;
int y1;
int x2;
int y2;
mytestdata(String sOne, int xOne, int yOne, int xTwo, int yTwo)
{
s1 = new String(sOne);
x1 = xOne;
y1 = yOne;
x2 = xTwo;
y2 = yTwo;
}
}
public static void main( String args[])
throws
Exception
{
Provider gnu = java.security.Security.getProvider(Registry.GNU_CRYPTO);
if (gnu == null)
{
java.security.Security.addProvider(
new gnu.crypto.jce.GnuCrypto()
);
}
HashSet names = new HashSet();
//If I understand correctly, I removed the gnu classpath provider
//in order to isolate which provider is failing the getInstance for
//the AES algorithm
//in this case, both providers gave me a runtime error later on when
instantiating the keys/ciphers for a particular task.
java.security.Security.removeProvider("GNU");
Provider[] providers = Security.getProviders();
for(int provCount = 0; provCount < providers.length ; provCount++)
{
System.out.println("provider:" + providers[provCount].getName());
System.out.println("getInfo:" + providers[provCount].getInfo());
System.out.println("getVersion:" +
providers[provCount].getVersion());
}
//this one works.
IBlockCipher cipher = CipherFactory.getInstance("rijndael");
int keysize = cipher.defaultKeySize();
int blocksize = cipher.defaultBlockSize();
System.out.println("rijndael cipher created...");
System.out.println("with keysize:" + Integer.toString(keysize));
System.out.println("with blocksize:" + Integer.toString(blocksize));
String myKey = "FEDCBA9876543210"; //must be 16 characters long
byte[] myKeyBytes = myKey.getBytes();
try
{
int iterations = 1;
String myPlainTextMessage = "0123456789ABCDEF";
byte[] myPlainTextMessageBytes = myPlainTextMessage.getBytes();
System.out.println("Running "+iterations+" iterations:");
System.out.print("Encryption: ");
HashMap map = new HashMap();
map.put(IBlockCipher.KEY_MATERIAL, myKeyBytes);
cipher.init(map);
byte[] myCryptedText = (byte[]) myPlainTextMessageBytes.clone();
long elapsed = -System.currentTimeMillis();
int i;
for (i = 0; i < iterations; i++)
{
cipher.encryptBlock(myCryptedText, 0, myCryptedText, 0);
}
elapsed += System.currentTimeMillis();
float secs = (elapsed > 1) ? (float) elapsed / 1000 : 1;
float speed = (float) iterations * blocksize / 1024 / secs;
System.out.println("time = "+secs+", speed = "+speed+" KB/s");
System.out.print("Decryption: ");
byte[] myDecryptedTextBytes = (byte[]) myCryptedText.clone();
elapsed = -System.currentTimeMillis();
for (i = 0; i < iterations; i++) {
cipher.decryptBlock(myDecryptedTextBytes, 0,
myDecryptedTextBytes, 0);
String myDecryptedText = new String(myDecryptedTextBytes);
System.out.println("decryptedMessage:<<" + myDecryptedText
+ ">>");
}
elapsed += System.currentTimeMillis();
secs = (elapsed > 1) ? (float) elapsed / 1000 : 1;
speed = (float) iterations * blocksize / 1024 / secs;
System.out.println("time = "+secs+", speed = "+speed+" KB/s");
if (!Arrays.equals(myPlainTextMessageBytes, myDecryptedTextBytes))
{
throw new RuntimeException("Symmetric operation failed");
}
}
catch (Exception x)
{
x.printStackTrace(System.err);
}
Object[] myCipherAlgorithmArray =
java.security.Security.getAlgorithms("cipher").toArray();
Object[] myKeyPairGeneratorAlgorithmArray =
java.security.Security.getAlgorithms("cipher").toArray();
Object[] myMessageAuthenticationCodesAlgorithmArray =
java.security.Security.getAlgorithms("mac").toArray();
Object[] myMessageDigestAlgorithmArray =
java.security.Security.getAlgorithms("messagedigest").toArray();
Object[] mySecureRandomAlgorithmArray =
java.security.Security.getAlgorithms("securerandom").toArray();
Object[] mySignatureAlgorithmArray =
java.security.Security.getAlgorithms("signature").toArray();
System.out.println("cipher algorithm count:" +
myCipherAlgorithmArray.length);
dumpAlgorithmNames(myCipherAlgorithmArray);
System.out.println("keypair generator algorithm count:" +
myKeyPairGeneratorAlgorithmArray.length);
dumpAlgorithmNames(myKeyPairGeneratorAlgorithmArray);
System.out.println("message authentication codes algorithm count:"
+ myMessageAuthenticationCodesAlgorithmArray.length);
dumpAlgorithmNames(myMessageAuthenticationCodesAlgorithmArray);
System.out.println("message digest algorithm count:" +
myMessageDigestAlgorithmArray.length);
dumpAlgorithmNames(myMessageDigestAlgorithmArray);
System.out.println("secure random algorithm count:" +
mySecureRandomAlgorithmArray.length);
dumpAlgorithmNames(mySecureRandomAlgorithmArray);
System.out.println("signature algorithm count:" +
mySignatureAlgorithmArray.length);
dumpAlgorithmNames(mySignatureAlgorithmArray);
String symAlgorithm="AES";
//javax.crypto.SecretKeyFactory KeyFac =
javax.crypto.SecretKeyFactory.getInstance(symAlgorithm);
myKey = "FEDCBA9876543210"; //must be 16 characters long
myKeyBytes = myKey.getBytes();
//javax.crypto.spec.SecretKeySpec desKeySpec = new
javax.crypto.spec.SecretKeySpec(myKeyBytes, symAlgorithm);
//javax.crypto.SecretKey sKey = KeyFac.generateSecret(desKeySpec);
//KeyGenerator kgen =
KeyGenerator.getInstance("PBEWITHHMACMD4ANDAES");
//kgen.init(128);
////kgen.init(192);
////kgen.init(256);
//SecretKey skey = kgen.generateKey();
//byte[] raw = skey.getEncoded();
//SecretKeySpec skeySpec = new SecretKeySpec(myKeyBytes,
"PBEWITHHMACMD4ANDAES");
//instantiate the cipher
javax.crypto.Cipher jccipher = javax.crypto.Cipher.getInstance("AES");
//jccipher.init(Cipher.ENCRYPT_MODE, sKey);
//jccipher.init(Cipher.ENCRYPT_MODE, skeySpec);
// mytestdata myTest = new mytestdata("test", 1, 2, 3, 4);
// SealedObject sealedTest = new SealedObject(myTest, cipher);
// System.out.println("Encrypted:" + sealedTest.toString() );
// cipher.init(Cipher.DECRYPT_MODE, skeySpec);
// mytestdata myDecryptedResult =
(mytestdata)sealedTest.getObject(skeySpec);
// System.out.println("s1:" + myDecryptedResult.s1);
// System.out.println("x1:" + myDecryptedResult.x1);
// System.out.println("y1:" + myDecryptedResult.y1);
// System.out.println("x2:" + myDecryptedResult.x2);
// System.out.println("y2:" + myDecryptedResult.y2);
}
static public void dumpAlgorithmNames(Object[] tmpArray)
{
int algoCount;
for(algoCount = 0; algoCount < tmpArray.length; algoCount++)
{
System.out.println("algorithm:" + ((String)tmpArray[algoCount]) );
}
}
}
Here are the steps I used to build the example:
export
LD_LIBRARY_PATH=/usr/local/gnu-crypto/lib:/usr/local/gnu-crypto/share:.
#here is what is gnu crypto related
#/usr/local/gnu-crypto/lib/lib-javax-crypto.a
#/usr/local/gnu-crypto/lib/lib-javax-security.a
#/usr/local/gnu-crypto/lib/lib-gnu-crypto.so.2.0.0
#/usr/local/gnu-crypto/lib/lib-gnu-crypto.a
#compile the .java files into native object code(.o)
gcj-4.0 -v
--CLASSPATH=".:/usr/local/gnu-crypto/share/javax-crypto.jar:/usr/local/gnu-crypto/share/javax-security.jar:/usr/local/gnu-crypto/share/gnu-crypto.jar"
-c -o testaes.o testaes.java -L/usr/local/gnu-crypto/lib -lgnu-crypto
-ljavax-crypto -ljavax-security
#assemble them all into the main executable called testaes in this case.
gcj-4.0 -v
--CLASSPATH=".:/usr/local/gnu-crypto/share/javax-crypto.jar:/usr/local/gnu-crypto/share/javax-security.jar:/usr/local/gnu-crypto/share/gnu-crypto.jar"
--main=testaes -o testaes testaes.o -L/usr/local/gnu-crypto/lib
-l-gnu-crypto -l-javax-crypto -l-javax-security
_______________________________________________
Classpath mailing list
Classpath@gnu.org
http://developer.classpath.org/mailman/listinfo/classpath