Skip to content

Commit

Permalink
First check-in with embedded crypto functions. See README for further…
Browse files Browse the repository at this point in the history
… explanations.

Only one jar file is needed to use ZRTP and/or SRTP (with JMF/FMJ). Including the
crypto functions the overall size of the jar file is between approx. 155KB and 190KB
depending if the SRTP part is included or not.

git-svn-id: http://svn.savannah.gnu.org/svn/ccrtp/trunk@542 c26ef401-41b0-48d7-a563-427cd9f8604a
  • Loading branch information
wernerd committed Mar 21, 2009
1 parent a69ca0e commit 35611c0
Show file tree
Hide file tree
Showing 49 changed files with 7,492 additions and 74 deletions.
41 changes: 34 additions & 7 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,44 @@ You may access the ZRTP specification at this URL:
The first application that included this libarary was a SIP Communicator
release produced by Emanuel Onica during Google Summer of Code (GSoC) 2008.

This library requires a crypto implementation that includes the
This library provides a crypto implementation that includes the
the following hash and crypto algorithms:

- SHA 256
- HMAC SHA 256
- AES 128 (and optional AES 256)
- Diffie-Helman (DH)
- SHA 1
- HMAC for SHA 1 and SHA 256
- AES
- Diffie-Hellman (DH)

Currently the code relies on the BouncyCastle crypto library, it does
not use JCE because this would required additional installations of
some policy files (JCE crypto policy files).
The crypto part of the library was copied from BouncyCastle crypto library.
Only the Diffie-Hellman part was modified to a new BigIntegerCrypto
implementation which is also part of this package.

BigIntegerCrypto re-uses the GNU BigInteger implementation and adds
some crypto specific enhancements:

- don't use the GMP library if installed on the system. While this
may reduce performance it gives us full control of the data (no
copying between Java and C)

- Add a method to clear the contents / data of the big integer. The
application can use this function to clear data in case this big
integer was used as a private key. Some applications may stay in
emory for a long time (for example communication applications) and
thus it is important to be able to clear secret data if it is not
longer used. Otherwise a malicious person could be able to do
memory analysis to find some key material.

- Add a finalize method. If the garbage collector processes the big
integer then the finalize method clears the data.

- Clear temporary data produced during calculations. Some big integer
calculation produce and use temporary data. BigIntegerCrypto clears
these temporary data to avoid data leakage. The tag "crypto:"
identifies these modifications.

Otherwise BigIntegerCrypto behaves in the same way as the normal
BigInteger.

The source distribution contains a short Java file that tests the
availability of the mentioned algorithms and support classes.
Expand Down
19 changes: 14 additions & 5 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<property name='product.version.major' value='1'/>
<property name='product.version.minor' value='4'/>
<property name='product.version.level' value='3'/>
<property name='product.version.level' value='4'/>
<property name='product.Version' value='${product.version.major}.${product.version.minor}'/>
<property name='product.version' value='${product.version.major}.${product.version.minor}.${product.version.level}'/>
<property name='product_version' value='${product.version.major}_${product.version.minor}_${product.version.level}'/>
Expand Down Expand Up @@ -62,19 +62,28 @@
</target> <!-- end of target "init" -->

<target name="jar" depends="compile"
description="Create the jar file for ZRTP4J" >
description="Create the jar files for ZRTP4J" >
<jar destfile="${dir.dist}/${zrtp4j-full-jar.name}" >
<fileset dir="${common-build.classes}" includes="gnu/java/**" />
<fileset dir="${common-build.classes}" >
<include name="gnu/java/**" />
<include name="org/bouncycastle/**" />
<exclude name="gnu/java/bigintcrypto/test/**" />
</fileset>
</jar>
<jar destfile="${dir.dist}/${zrtp4j-jar.name}" >
<fileset dir="${common-build.classes}" includes="gnu/java/zrtp/**" excludes="gnu/java/zrtp/jmf/**" />
<fileset dir="${common-build.classes}" >
<include name="gnu/java/**" />
<include name="org/bouncycastle/**" />
<exclude name="gnu/java/bigintcrypto/test/**" />
<exclude name="gnu/java/zrtp/jmf/**" />
</fileset>
</jar>
</target>

<target name="bindist" depends="jar" description="Build zip file for ZRTP4J distribution">
<zip destfile="${dir.dist}/${zrtp4j-zip.name}">
<zipfileset prefix="zrtp4j" dir="."
includes="LICENSE.txt, README.txt, doc/**, legal/**" />
includes="README.txt, doc/**, legal/**" />
<zipfileset fullpath="zrtp4j/${zrtp4j-full-jar.name}" dir="${dir.dist}"
includes="${zrtp4j-full-jar.name}"/>
<zipfileset fullpath="zrtp4j/${zrtp4j-jar.name}" dir="${dir.dist}"
Expand Down
19 changes: 19 additions & 0 deletions legal/LICENSE-bouncycastle.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

License

Copyright (c) 2000 - 2008 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File renamed without changes.
Binary file removed lib/bccrypto.jar
Binary file not shown.
40 changes: 17 additions & 23 deletions src/demo/CryptoTestPureLW.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import gnu.java.zrtp.ZrtpConstants;

import java.math.BigInteger;
import gnu.java.bigintcrypto.BigIntegerCrypto;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
Expand Down Expand Up @@ -32,21 +33,21 @@
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.crypto.generators.DHBasicKeyPairGenerator;
import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.cryptozrtp.generators.DHBasicKeyPairGenerator;
import org.bouncycastle.cryptozrtp.params.DHKeyGenerationParameters;
import org.bouncycastle.cryptozrtp.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.agreement.DHBasicAgreement;
import org.bouncycastle.crypto.params.DHPublicKeyParameters;
//import org.bouncycastle.cryptozrtp.InvalidCipherTextException;
import org.bouncycastle.cryptozrtp.agreement.DHBasicAgreement;
import org.bouncycastle.cryptozrtp.params.DHPublicKeyParameters;

import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.modes.CFBBlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.engines.AESFastEngine;
import org.bouncycastle.crypto.params.ParametersWithIV;


Expand All @@ -65,7 +66,7 @@
*/
public class CryptoTestPureLW {

public static final DHParameterSpec specDh3kjce = new DHParameterSpec(ZrtpConstants.P3072, ZrtpConstants.two, 256);
// public static final DHParameterSpec specDh3kjce = new DHParameterSpec(ZrtpConstants.P3072, ZrtpConstants.two, 256);


private SecureRandom secRand = new SecureRandom();
Expand Down Expand Up @@ -109,7 +110,7 @@ boolean testProvider() {

// get B party's public key
DHPublicKeyParameters tmp = (DHPublicKeyParameters) myKeyPairLwB.getPublic();
BigInteger y = tmp.getY(); // and the big int value of it
BigIntegerCrypto y = tmp.getY(); // and the big int value of it

// System.out.println("B public length: " + y.toByteArray().length);

Expand All @@ -118,7 +119,7 @@ boolean testProvider() {
// calculate the secret value of A party, using B party's value
dhContextLwA = new DHBasicAgreement();
dhContextLwA.init(myKeyPairLwA.getPrivate());
BigInteger resultLwA = dhContextLwA.calculateAgreement(new DHPublicKeyParameters(y, ZrtpConstants.specDh3k));
BigIntegerCrypto resultLwA = dhContextLwA.calculateAgreement(new DHPublicKeyParameters(y, ZrtpConstants.specDh3k));


// get A party's public key
Expand All @@ -132,7 +133,7 @@ boolean testProvider() {
// then calculate the secret value of A party, using B party's value
dhContextLwB = new DHBasicAgreement();
dhContextLwB.init(myKeyPairLwB.getPrivate());
BigInteger resultLwB = dhContextLwB.calculateAgreement(new DHPublicKeyParameters(y, ZrtpConstants.specDh3k));
BigIntegerCrypto resultLwB = dhContextLwB.calculateAgreement(new DHPublicKeyParameters(y, ZrtpConstants.specDh3k));

byte[] lwByteA = adjustKey(resultLwA);
byte[] lwByteB = adjustKey(resultLwB);
Expand Down Expand Up @@ -161,7 +162,7 @@ boolean testProvider() {

// test the cipher - LW

AESEngine aesEnc = new AESEngine();
AESFastEngine aesEnc = new AESFastEngine();
CFBBlockCipher cfbAesEnc = new CFBBlockCipher(aesEnc, aesEnc.getBlockSize() * 8);
BufferedBlockCipher bufCfbAesEnc = new BufferedBlockCipher(cfbAesEnc);
bufCfbAesEnc.init(true, new ParametersWithIV(new KeyParameter(masterKey), randomIV));
Expand All @@ -176,12 +177,9 @@ boolean testProvider() {
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidCipherTextException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

AESEngine aesDec = new AESEngine();
AESFastEngine aesDec = new AESFastEngine();
CFBBlockCipher cfbAesDec = new CFBBlockCipher(aesDec, aesDec.getBlockSize() * 8);
BufferedBlockCipher bufCfbAesDec = new BufferedBlockCipher(cfbAesDec);
bufCfbAesDec.init(false, new ParametersWithIV(new KeyParameter(masterKey), randomIV));
Expand All @@ -196,9 +194,6 @@ boolean testProvider() {
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidCipherTextException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (Arrays.equals(dataToSecure, aesOutLwDec)) {
System.out.println("AES-CFB results are equal");
Expand All @@ -211,11 +206,10 @@ boolean testProvider() {
return true;
}


byte[] adjustKey(BigInteger in) {
byte[] adjustKey(BigIntegerCrypto in) {
byte[] inBytes = in.toByteArray();
// check for leading zero byte if public key resulted in negtive
// value. BigInteger adds a leading zero to drop the negatice sign bit
// value. BigIntegerCrypto adds a leading zero to drop the negatice sign bit
if (inBytes[0] == 0) {
byte[] tmp = new byte[inBytes.length - 1];
System.arraycopy(inBytes, 1, tmp, 0, tmp.length);
Expand Down
Loading

0 comments on commit 35611c0

Please sign in to comment.