Module: Crypt - ninazeina/SXP GitHub Wiki

Role

The crypt module provides all the cryptographic primitives used across the project.

  • Key management
  • Encryption
  • Signatures
  • Hashing

The following asymmetric encryption schemes are implemented:

  • El Gamal for encryption and signature
  • Serpent for encryption

The crypt is used by the controller in controller.manager.CryptManagerDecorator to enforce all cryptographic aspects.

api

  • key.AsymKey to manage keys and parameters for asymmetric encryption schemes.
  • hashs.Hashable is implemented by objects to be hashed, via the method getHashableData() which returns a sequence of bytes that is passed to the hash function. hashs.Hasher is an interface to hash sequence of bytes or hashable objects.
  • encryption.Encryptable is implemented by objects that can be encrypted. encryption.Encrypter is an interface to encrypt and decrypt encryptable objects. Encryption is done "in place", therefore an encryptable object can either be clear or encrypted.
  • signatures.Signable is implemented by objects that can be signed. signatures.Signer is an interface to sign objects and verify signatures. signatures.ParamName and signatures.Signature are obsolete (a tentative to harmonize different signature types).

The base folder contains abstract implementations of this api with code common to all implementations.

factory

  • AsymKeyFactory allows to create AsymKeys for the implemented schemes (<scheme>AsymKeyFactory contains the code specific to <scheme>, such as ElGamalAsymKeyFactory).
  • HasherFactory allows to create Hasher for the implemented schemes (<scheme>Hasher contains the code specific to <scheme>, such as SHA256Hasher).
  • EncrypterFactory allows to create Encrypter for the implemented schemes (<scheme>Encrypter contains the code specific to <scheme>, such as ElGamalEncrytper).
  • SignerFactory allows to create Signer for the implemented schemes (<scheme>Signer contains the code specific to <scheme>, such as ElGamalSigner).

implementation

  • As it has no logic inside, the implementation of crypt.api.AsymKey is located in model.entity.ElGamalKey.
  • SHA256Hasher implements SHA256 hash function, using java.security.MessageDigest.
  • ElGamalEncrypter implements El Gamal encryption scheme, using org.bouncycastle.crypto.engines.ElGamalEngine. SerpentEncrypter implements Serpent encryption scheme, using Serpent.Serpent_BitSlice.
  • ElGamalSignEntity (in model.entity) is an El Gamal signature (a data). ElGamalSigner implements El Gamal signature scheme, using crypt.impl.hashs.SHA256Hasher (as it is in the same package as the hasher, it does not need to use the HasherFactory), it returns an ElGamalSignEntity. BaseSignature and ElGamalSignaure are obsolete (a tentative to harmonize different signature types).

Use of crypt by controller

In this exemple we will create a simple entity that we want to sign

Let create a very simple entity :

public class Exemple {
    private String property1;
    private String property2;

    public String getProperty1() {
	    return property1;
    }
    public void setProperty1(String property1) {
	    this.property1 = property1;
    }
    public String getProperty2() {
	    return property2;
    }
    public void setProperty2(String property2) {
	    this.property2 = property2;
    }
}

We want our objects to be signable. Let implements the Signable interface. The generic type must be a signature type, like ElGamalSignature

public class Exemple implements Signable<ElGamalSignature>{

Implementing this interface add 3 methods :

    @Override
    public byte[] getHashableData() {
	    // TODO Auto-generated method stub
	    return null;
    }
    @Override
    public void setSign(ElGamalSignature s) {
	    // TODO Auto-generated method stub
	
    }
    @Override
    public ElGamalSignature getSign() {
	    // TODO Auto-generated method stub
	    return null;
    }

Implements them. Just add an ElGamalSignature property to your Exemple class.

   @Override
    public byte[] getHashableData() {
	    //The hashable data are just a concatenation of all attributes.
	    return new String(property1 + property2).getBytes();
    }

    @Override
    public void setSign(ElGamalSignature s) {
	    this.signature = s;
    }

    @Override
    public ElGamalSignature getSign() {
	    return signature;
    }

Our class is ready to be signed :

    //Bob will sign the object with his private key.
	ElGamalKey bob = AsymKeyFactory.createElGamalAsymKey(false);
	Exemple myExemple = new Exemple();
	myExemple.setProperty1("hello");
	myExemple.setProperty2("world");
	
	//we create a signare that sign with an ElGamalSignature, with ElGamalKey
	Signer<ElGamalSignature, ElGamalKey> signer = SignerFactory.createElGamalSigner();
	signer.setKey(bob);
	signer.sign(myExemple);
	//now myExemple is signed by bob
⚠️ **GitHub.com Fallback** ⚠️