1 package it.unisa.dia.gas.crypto.jpbc.encryption.ut.bdp10; 2 3 import it.unisa.dia.gas.crypto.engines.kem.KeyEncapsulationMechanism; 4 import it.unisa.dia.gas.crypto.jpbc.AbstractJPBCCryptoTest; 5 import it.unisa.dia.gas.crypto.jpbc.encryption.ut.bdp10.engines.UTBDP10StrongKEMEngine; 6 import it.unisa.dia.gas.crypto.jpbc.encryption.ut.bdp10.generators.UTBDP10StrongKeyPairGenerator; 7 import it.unisa.dia.gas.crypto.jpbc.encryption.ut.bdp10.generators.UTBDP10StrongParametersGenerator; 8 import it.unisa.dia.gas.crypto.jpbc.encryption.ut.bdp10.params.*; 9 import org.bouncycastle.crypto.AsymmetricCipherKeyPair; 10 import org.bouncycastle.crypto.CipherParameters; 11 import org.bouncycastle.crypto.InvalidCipherTextException; 12 import org.bouncycastle.crypto.generators.ElGamalParametersGenerator; 13 import org.bouncycastle.crypto.params.ElGamalParameters; 14 import org.junit.Test; 15 16 import java.security.SecureRandom; 17 import java.util.Arrays; 18 19 import static org.junit.Assert.*; 20 21 /** 22 * @author Angelo De Caro (angelo.decaro@gmail.com) 23 */ 24 public class UTBDP10StrongKEMEngineTest extends AbstractJPBCCryptoTest { 25 26 27 public UTBDP10StrongKEMEngineTest(boolean usePBC, String curvePath) { 28 super(usePBC, curvePath); 29 } 30 31 32 @Test 33 public void testUTBDP10StrongKEMEngine() { 34 UTBDP10StrongParameters parameters = createParameters(1024); 35 AsymmetricCipherKeyPair keyPair = setup(parameters); 36 37 byte[][] ct = encaps(keyPair.getPublic()); 38 39 assertEquals(true, Arrays.equals(ct[0], decaps(keyPair.getPrivate(), ct[1]))); 40 assertEquals(true, Arrays.equals(ct[0], decaps(keyPair.getPrivate(), 41 randomize(parameters.getPublicParameters(), parameters.getRPublicParameters(), ct[1])))); 42 } 43 44 45 protected UTBDP10StrongParameters createParameters(int elgamalLength) { 46 ElGamalParametersGenerator elGamalParametersGenerator = new ElGamalParametersGenerator(); 47 elGamalParametersGenerator.init(elgamalLength, 12, new SecureRandom()); 48 ElGamalParameters elGamalParameters = elGamalParametersGenerator.generateParameters(); 49 50 UTBDP10StrongParametersGenerator generator = new UTBDP10StrongParametersGenerator(); 51 generator.init(curveParameters, elGamalParameters); 52 return generator.generateParameters(); 53 } 54 55 protected AsymmetricCipherKeyPair setup(UTBDP10StrongParameters parameters) { 56 UTBDP10StrongKeyPairGenerator setup = new UTBDP10StrongKeyPairGenerator(); 57 setup.init(new UTBDP10StrongKeyGenerationParameters(new SecureRandom(), parameters)); 58 59 return setup.generateKeyPair(); 60 } 61 62 protected byte[][] encaps(CipherParameters publicKey) { 63 try { 64 KeyEncapsulationMechanism kem = new UTBDP10StrongKEMEngine(); 65 kem.init(true, publicKey); 66 67 byte[] ciphertext = kem.processBlock(new byte[0], 0, 0); 68 69 assertNotNull(ciphertext); 70 assertNotSame(0, ciphertext.length); 71 72 byte[] key = Arrays.copyOfRange(ciphertext, 0, kem.getKeyBlockSize()); 73 byte[] ct = Arrays.copyOfRange(ciphertext, kem.getKeyBlockSize(), ciphertext.length); 74 75 return new byte[][]{key, ct}; 76 } catch (InvalidCipherTextException e) { 77 e.printStackTrace(); 78 fail(e.getMessage()); 79 } 80 return null; 81 } 82 83 protected byte[] decaps(CipherParameters privateKey, byte[] ciphertext) { 84 try { 85 KeyEncapsulationMechanism engine = new UTBDP10StrongKEMEngine(); 86 engine.init(false, privateKey); 87 88 return engine.processBlock(ciphertext, 0, ciphertext.length); 89 } catch (InvalidCipherTextException e) { 90 e.printStackTrace(); 91 fail(e.getMessage()); 92 throw new RuntimeException(e); 93 } 94 } 95 96 protected byte[] randomize(UTBDP10StrongPublicParameters publicParameters, 97 UTBDP10StrongRPublicParameters rPublicParameters, 98 byte[] ciphertext) { 99 try { 100 KeyEncapsulationMechanism engine = new UTBDP10StrongKEMEngine(); 101 engine.init(true, new UTBDP10StrongRandomizeParameters(publicParameters, rPublicParameters)); 102 103 return engine.processBlock(ciphertext, 0, ciphertext.length); 104 } catch (InvalidCipherTextException e) { 105 e.printStackTrace(); 106 fail(e.getMessage()); 107 throw new RuntimeException(e); 108 } 109 } 110 111 }