001: package org.bouncycastle.crypto.test;
002:
003: import java.math.BigInteger;
004: import java.security.SecureRandom;
005:
006: import org.bouncycastle.crypto.params.ECDomainParameters;
007: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
008: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
009: import org.bouncycastle.crypto.params.ParametersWithRandom;
010: import org.bouncycastle.crypto.signers.ECNRSigner;
011: import org.bouncycastle.math.ec.ECCurve;
012: import org.bouncycastle.util.BigIntegers;
013: import org.bouncycastle.util.encoders.Hex;
014: import org.bouncycastle.util.test.FixedSecureRandom;
015: import org.bouncycastle.util.test.SimpleTest;
016:
017: /**
018: * ECNR tests.
019: */
020: public class ECNRTest extends SimpleTest {
021: /**
022: * a basic regression test with 239 bit prime
023: */
024: BigInteger r = new BigInteger(
025: "308636143175167811492623515537541734843573549327605293463169625072911693");
026: BigInteger s = new BigInteger(
027: "852401710738814635664888632022555967400445256405412579597015412971797143");
028:
029: byte[] kData = BigIntegers
030: .asUnsignedByteArray(new BigInteger(
031: "700000017569056646655505781757157107570501575775705779575555657156756655"));
032:
033: SecureRandom k = new FixedSecureRandom(true, kData);
034:
035: private void ecNR239bitPrime() {
036: ECCurve.Fp curve = new ECCurve.Fp(
037: new BigInteger(
038: "883423532389192164791648750360308885314476597252960362792450860609699839"), // q
039: new BigInteger(
040: "7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc",
041: 16), // a
042: new BigInteger(
043: "6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a",
044: 16)); // b
045:
046: ECDomainParameters params = new ECDomainParameters(
047: curve,
048: curve
049: .decodePoint(Hex
050: .decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
051: new BigInteger(
052: "883423532389192164791648750360308884807550341691627752275345424702807307")); // n
053:
054: ECPrivateKeyParameters priKey = new ECPrivateKeyParameters(
055: new BigInteger(
056: "876300101507107567501066130761671078357010671067781776716671676178726717"), // d
057: params);
058:
059: ECNRSigner ecnr = new ECNRSigner();
060: ParametersWithRandom param = new ParametersWithRandom(priKey, k);
061:
062: ecnr.init(true, param);
063:
064: byte[] message = new BigInteger(
065: "968236873715988614170569073515315707566766479517")
066: .toByteArray();
067: BigInteger[] sig = ecnr.generateSignature(message);
068:
069: if (!r.equals(sig[0])) {
070: fail("r component wrong.", r, sig[0]);
071: }
072:
073: if (!s.equals(sig[1])) {
074: fail("s component wrong.", s, sig[1]);
075: }
076:
077: // Verify the signature
078: ECPublicKeyParameters pubKey = new ECPublicKeyParameters(
079: curve
080: .decodePoint(Hex
081: .decode("025b6dc53bc61a2548ffb0f671472de6c9521a9d2d2534e65abfcbd5fe0c70")), // Q
082: params);
083:
084: ecnr.init(false, pubKey);
085: if (!ecnr.verifySignature(message, sig[0], sig[1])) {
086: fail("signature fails");
087: }
088: }
089:
090: public String getName() {
091: return "ECNR";
092: }
093:
094: public void performTest() {
095: ecNR239bitPrime();
096: }
097:
098: public static void main(String[] args) {
099: runTest(new ECNRTest());
100: }
101: }
|