forked from bcgit/bc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
core/src/main/java/org/bouncycastle/crypto/StagedAgreement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.bouncycastle.crypto; | ||
|
||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter; | ||
|
||
public interface StagedAgreement | ||
extends BasicAgreement | ||
{ | ||
AsymmetricKeyParameter calculateStage(CipherParameters pubKey); | ||
} |
73 changes: 73 additions & 0 deletions
73
core/src/main/java/org/bouncycastle/crypto/agreement/ECDHCStagedAgreement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.bouncycastle.crypto.agreement; | ||
|
||
import java.math.BigInteger; | ||
|
||
import org.bouncycastle.crypto.CipherParameters; | ||
import org.bouncycastle.crypto.StagedAgreement; | ||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter; | ||
import org.bouncycastle.crypto.params.ECDomainParameters; | ||
import org.bouncycastle.crypto.params.ECPrivateKeyParameters; | ||
import org.bouncycastle.crypto.params.ECPublicKeyParameters; | ||
import org.bouncycastle.math.ec.ECAlgorithms; | ||
import org.bouncycastle.math.ec.ECPoint; | ||
|
||
public class ECDHCStagedAgreement | ||
implements StagedAgreement | ||
{ | ||
ECPrivateKeyParameters key; | ||
|
||
public void init( | ||
CipherParameters key) | ||
{ | ||
this.key = (ECPrivateKeyParameters)key; | ||
} | ||
|
||
public int getFieldSize() | ||
{ | ||
return (key.getParameters().getCurve().getFieldSize() + 7) / 8; | ||
} | ||
|
||
public AsymmetricKeyParameter calculateStage( | ||
CipherParameters pubKey) | ||
{ | ||
ECPoint P = calculateNextPoint((ECPublicKeyParameters)pubKey); | ||
|
||
return new ECPublicKeyParameters(P, key.getParameters()); | ||
} | ||
|
||
public BigInteger calculateAgreement( | ||
CipherParameters pubKey) | ||
{ | ||
ECPoint P = calculateNextPoint((ECPublicKeyParameters)pubKey); | ||
|
||
return P.getAffineXCoord().toBigInteger(); | ||
} | ||
|
||
private ECPoint calculateNextPoint(ECPublicKeyParameters pubKey) | ||
{ | ||
ECPublicKeyParameters pub = pubKey; | ||
ECDomainParameters params = key.getParameters(); | ||
if (!params.equals(pub.getParameters())) | ||
{ | ||
throw new IllegalStateException("ECDHC public key has wrong domain parameters"); | ||
} | ||
|
||
BigInteger hd = params.getH().multiply(key.getD()).mod(params.getN()); | ||
|
||
// Always perform calculations on the exact curve specified by our private key's parameters | ||
ECPoint pubPoint = ECAlgorithms.cleanPoint(params.getCurve(), pub.getQ()); | ||
if (pubPoint.isInfinity()) | ||
{ | ||
throw new IllegalStateException("Infinity is not a valid public key for ECDHC"); | ||
} | ||
|
||
ECPoint P = pubPoint.multiply(hd).normalize(); | ||
|
||
if (P.isInfinity()) | ||
{ | ||
throw new IllegalStateException("Infinity is not a valid agreement value for ECDHC"); | ||
} | ||
|
||
return P; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters