diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-05-31 11:07:45 +0400 |
commit | 2b976f5364cfdbc37d3086019d93483c983eb80b (patch) | |
tree | cb846af3fd1d43f9c2562a1fb2d06b997ad8f229 /core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java | |
parent | 5f714bd92fbd780d22406f4bc3681be005f6f04a (diff) |
initial reshuffle
Diffstat (limited to 'core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java')
-rw-r--r-- | core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java b/core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java new file mode 100644 index 00000000..32ba0706 --- /dev/null +++ b/core/src/main/java/org/bouncycastle/crypto/ec/ECNewPublicKeyTransform.java @@ -0,0 +1,74 @@ +package org.bouncycastle.crypto.ec; + +import java.math.BigInteger; +import java.security.SecureRandom; + +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; +import org.bouncycastle.crypto.params.ParametersWithRandom; +import org.bouncycastle.math.ec.ECPoint; + +/** + * this does your basic Elgamal encryption algorithm using EC + */ +public class ECNewPublicKeyTransform + implements ECPairTransform +{ + private ECPublicKeyParameters key; + private SecureRandom random; + + /** + * initialise the EC Elgamal engine. + * + * @param param the necessary EC key parameters. + */ + public void init( + CipherParameters param) + { + if (param instanceof ParametersWithRandom) + { + ParametersWithRandom p = (ParametersWithRandom)param; + + if (!(p.getParameters() instanceof ECPublicKeyParameters)) + { + throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform."); + } + this.key = (ECPublicKeyParameters)p.getParameters(); + this.random = p.getRandom(); + } + else + { + if (!(param instanceof ECPublicKeyParameters)) + { + throw new IllegalArgumentException("ECPublicKeyParameters are required for new public key transform."); + } + + this.key = (ECPublicKeyParameters)param; + this.random = new SecureRandom(); + } + } + + /** + * Transform an existing cipher test pair using the ElGamal algorithm. Note: the input cipherText will + * need to be preserved in order to complete the transformation to the new public key. + * + * @param cipherText the EC point to process. + * @return returns a new ECPair representing the result of the process. + */ + public ECPair transform(ECPair cipherText) + { + if (key == null) + { + throw new IllegalStateException("ECNewPublicKeyTransform not initialised"); + } + + BigInteger n = key.getParameters().getN(); + BigInteger k = ECUtil.generateK(n, random); + + ECPoint g = key.getParameters().getG(); + ECPoint gamma = g.multiply(k); + ECPoint phi = key.getQ().multiply(k).add(cipherText.getY()); + + return new ECPair(gamma, phi); + } +} |