Module: Crypt - ninazeina/SXP GitHub Wiki
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.
-
key.AsymKeyto manage keys and parameters for asymmetric encryption schemes. -
hashs.Hashableis implemented by objects to be hashed, via the methodgetHashableData()which returns a sequence of bytes that is passed to the hash function.hashs.Hasheris an interface to hash sequence of bytes or hashable objects. -
encryption.Encryptableis implemented by objects that can be encrypted.encryption.Encrypteris an interface to encrypt and decrypt encryptable objects. Encryption is done "in place", therefore an encryptable object can either be clear or encrypted. -
signatures.Signableis implemented by objects that can be signed.signatures.Signeris an interface to sign objects and verify signatures.signatures.ParamNameandsignatures.Signatureare obsolete (a tentative to harmonize different signature types).
The base folder contains abstract implementations of this api with code common to all implementations.
-
AsymKeyFactoryallows to createAsymKeysfor the implemented schemes (<scheme>AsymKeyFactorycontains the code specific to<scheme>, such asElGamalAsymKeyFactory). -
HasherFactoryallows to createHasherfor the implemented schemes (<scheme>Hashercontains the code specific to<scheme>, such asSHA256Hasher). -
EncrypterFactoryallows to createEncrypterfor the implemented schemes (<scheme>Encryptercontains the code specific to<scheme>, such asElGamalEncrytper). -
SignerFactoryallows to createSignerfor the implemented schemes (<scheme>Signercontains the code specific to<scheme>, such asElGamalSigner).
- As it has no logic inside, the implementation of
crypt.api.AsymKeyis located inmodel.entity.ElGamalKey. -
SHA256Hasherimplements SHA256 hash function, usingjava.security.MessageDigest. -
ElGamalEncrypterimplements El Gamal encryption scheme, usingorg.bouncycastle.crypto.engines.ElGamalEngine.SerpentEncrypterimplements Serpent encryption scheme, usingSerpent.Serpent_BitSlice. -
ElGamalSignEntity(inmodel.entity) is an El Gamal signature (a data).ElGamalSignerimplements El Gamal signature scheme, usingcrypt.impl.hashs.SHA256Hasher(as it is in the same package as the hasher, it does not need to use theHasherFactory), it returns anElGamalSignEntity.BaseSignatureandElGamalSignaureare obsolete (a tentative to harmonize different signature types).
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