diff options
author | Sergio Giro <sgiro@google.com> | 2016-02-01 18:52:42 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-02-01 18:52:42 +0000 |
commit | 9218edabd1ef9852bc2f13115dcadc81b442dd6c (patch) | |
tree | 8229ff72c8cbb06f49dce3a8382930919fa6fc2b /bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java | |
parent | 9b30eb05e5be69d51881a0d1b31e503e97acd784 (diff) | |
parent | 397d32894b89b506dc318e0f83446187c9b76ebe (diff) | |
download | android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.gz android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.tar.bz2 android_external_bouncycastle-9218edabd1ef9852bc2f13115dcadc81b442dd6c.zip |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge-152-from-upstream"
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java b/bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java new file mode 100644 index 0000000..2c6a920 --- /dev/null +++ b/bcprov/src/main/java/org/bouncycastle/crypto/ec/ECFixedTransform.java @@ -0,0 +1,88 @@ +package org.bouncycastle.crypto.ec; + +import java.math.BigInteger; + +import org.bouncycastle.crypto.CipherParameters; +import org.bouncycastle.crypto.params.ECDomainParameters; +import org.bouncycastle.crypto.params.ECPublicKeyParameters; +import org.bouncycastle.math.ec.ECMultiplier; +import org.bouncycastle.math.ec.ECPoint; +import org.bouncycastle.math.ec.FixedPointCombMultiplier; + +/** + * this transforms the original randomness used for an ElGamal encryption by a fixed value. + */ +public class ECFixedTransform + implements ECPairFactorTransform +{ + private ECPublicKeyParameters key; + + private BigInteger k; + + public ECFixedTransform(BigInteger k) + { + this.k = k; + } + + /** + * initialise the underlying EC ElGamal engine. + * + * @param param the necessary EC key parameters. + */ + public void init( + CipherParameters param) + { + if (!(param instanceof ECPublicKeyParameters)) + { + throw new IllegalArgumentException("ECPublicKeyParameters are required for fixed transform."); + } + + this.key = (ECPublicKeyParameters)param; + } + + /** + * Transform an existing cipher text pair using the ElGamal algorithm. Note: it is assumed this + * transform has been initialised with the same public key that was used to create the original + * cipher text. + * + * @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("ECFixedTransform not initialised"); + } + + ECDomainParameters ec = key.getParameters(); + BigInteger n = ec.getN(); + + ECMultiplier basePointMultiplier = createBasePointMultiplier(); + BigInteger k = this.k.mod(n); + + ECPoint[] gamma_phi = new ECPoint[]{ + basePointMultiplier.multiply(ec.getG(), k).add(cipherText.getX()), + key.getQ().multiply(k).add(cipherText.getY()) + }; + + ec.getCurve().normalizeAll(gamma_phi); + + return new ECPair(gamma_phi[0], gamma_phi[1]); + } + + /** + * Return the last transform value used by the transform + * + * @return a BigInteger representing k value. + */ + public BigInteger getTransformValue() + { + return k; + } + + protected ECMultiplier createBasePointMultiplier() + { + return new FixedPointCombMultiplier(); + } +} |