001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.DERObjectIdentifier;
004: import org.bouncycastle.asn1.cryptopro.ECGOST3410NamedCurves;
005: import org.bouncycastle.asn1.nist.NISTNamedCurves;
006: import org.bouncycastle.asn1.sec.SECNamedCurves;
007: import org.bouncycastle.asn1.teletrust.TeleTrusTNamedCurves;
008: import org.bouncycastle.asn1.x9.X962NamedCurves;
009: import org.bouncycastle.asn1.x9.X9ECParameters;
010: import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
011: import org.bouncycastle.crypto.params.ECDomainParameters;
012: import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
013: import org.bouncycastle.crypto.params.ECPublicKeyParameters;
014: import org.bouncycastle.jce.interfaces.ECPrivateKey;
015: import org.bouncycastle.jce.interfaces.ECPublicKey;
016: import org.bouncycastle.jce.spec.ECParameterSpec;
017:
018: import java.security.InvalidKeyException;
019: import java.security.PrivateKey;
020: import java.security.PublicKey;
021:
022: /**
023: * utility class for converting jce/jca ECDSA, ECDH, and ECDHC
024: * objects into their org.bouncycastle.crypto counterparts.
025: */
026: public class ECUtil {
027: /**
028: * Returns a sorted array of middle terms of the reduction polynomial.
029: * @param k The unsorted array of middle terms of the reduction polynomial
030: * of length 1 or 3.
031: * @return the sorted array of middle terms of the reduction polynomial.
032: * This array always has length 3.
033: */
034: static int[] convertMidTerms(int[] k) {
035: int[] res = new int[3];
036:
037: if (k.length == 1) {
038: res[0] = k[0];
039: } else {
040: if (k.length != 3) {
041: throw new IllegalArgumentException(
042: "Only Trinomials and pentanomials supported");
043: }
044:
045: if (k[0] < k[1] && k[0] < k[2]) {
046: res[0] = k[0];
047: if (k[1] < k[2]) {
048: res[1] = k[1];
049: res[2] = k[2];
050: } else {
051: res[1] = k[2];
052: res[2] = k[1];
053: }
054: } else if (k[1] < k[2]) {
055: res[0] = k[1];
056: if (k[0] < k[2]) {
057: res[1] = k[0];
058: res[2] = k[2];
059: } else {
060: res[1] = k[2];
061: res[2] = k[0];
062: }
063: } else {
064: res[0] = k[2];
065: if (k[0] < k[1]) {
066: res[1] = k[0];
067: res[2] = k[1];
068: } else {
069: res[1] = k[1];
070: res[2] = k[0];
071: }
072: }
073: }
074:
075: return res;
076: }
077:
078: static AsymmetricKeyParameter generatePublicKeyParameter(
079: PublicKey key) throws InvalidKeyException {
080: if (key instanceof ECPublicKey) {
081: ECPublicKey k = (ECPublicKey) key;
082: ECParameterSpec s = k.getParameters();
083:
084: if (s == null) {
085: s = ProviderUtil.getEcImplicitlyCa();
086:
087: return new ECPublicKeyParameters(((JCEECPublicKey) k)
088: .engineGetQ(), new ECDomainParameters(s
089: .getCurve(), s.getG(), s.getN(), s.getH(), s
090: .getSeed()));
091: } else {
092: return new ECPublicKeyParameters(k.getQ(),
093: new ECDomainParameters(s.getCurve(), s.getG(),
094: s.getN(), s.getH(), s.getSeed()));
095: }
096: }
097:
098: throw new InvalidKeyException("can't identify EC public key.");
099: }
100:
101: static AsymmetricKeyParameter generatePrivateKeyParameter(
102: PrivateKey key) throws InvalidKeyException {
103: if (key instanceof ECPrivateKey) {
104: ECPrivateKey k = (ECPrivateKey) key;
105: ECParameterSpec s = k.getParameters();
106:
107: if (s == null) {
108: s = ProviderUtil.getEcImplicitlyCa();
109: }
110:
111: return new ECPrivateKeyParameters(k.getD(),
112: new ECDomainParameters(s.getCurve(), s.getG(), s
113: .getN(), s.getH(), s.getSeed()));
114: }
115:
116: throw new InvalidKeyException("can't identify EC private key.");
117: }
118:
119: public static DERObjectIdentifier getNamedCurveOid(String name) {
120: DERObjectIdentifier oid = X962NamedCurves.getOID(name);
121:
122: if (oid == null) {
123: oid = SECNamedCurves.getOID(name);
124: if (oid == null) {
125: oid = NISTNamedCurves.getOID(name);
126: }
127: if (oid == null) {
128: oid = TeleTrusTNamedCurves.getOID(name);
129: }
130: if (oid == null) {
131: oid = ECGOST3410NamedCurves.getOID(name);
132: }
133: }
134:
135: return oid;
136: }
137:
138: public static X9ECParameters getNamedCurveByOid(
139: DERObjectIdentifier oid) {
140: X9ECParameters params = X962NamedCurves.getByOID(oid);
141:
142: if (params == null) {
143: params = SECNamedCurves.getByOID(oid);
144: if (params == null) {
145: params = NISTNamedCurves.getByOID(oid);
146: }
147: if (params == null) {
148: params = TeleTrusTNamedCurves.getByOID(oid);
149: }
150: }
151:
152: return params;
153: }
154:
155: public static String getCurveName(DERObjectIdentifier oid) {
156: String name = X962NamedCurves.getName(oid);
157:
158: if (name == null) {
159: name = SECNamedCurves.getName(oid);
160: if (name == null) {
161: name = NISTNamedCurves.getName(oid);
162: }
163: if (name == null) {
164: name = TeleTrusTNamedCurves.getName(oid);
165: }
166: }
167:
168: return name;
169: }
170: }
|