Changeset f83007e


Ignore:
Timestamp:
Aug 25, 2014 4:20:39 PM (6 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
c5c158e
Parents:
2b9a368
Message:

KeyGenerator?: Add support for converting

all signing key types from private to public

Location:
core/java/src/net/i2p
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/crypto/KeyGenerator.java

    r2b9a368 rf83007e  
    1313import java.security.GeneralSecurityException;
    1414import java.security.InvalidKeyException;
     15import java.security.KeyFactory;
    1516import java.security.KeyPair;
    1617import java.security.KeyPairGenerator;
    1718import java.security.ProviderException;
     19import java.security.interfaces.ECPrivateKey;
     20import java.security.interfaces.ECPublicKey;
     21import java.security.interfaces.RSAPrivateKey;
     22import java.security.interfaces.RSAPublicKey;
     23import java.security.spec.ECParameterSpec;
     24import java.security.spec.ECPoint;
     25import java.security.spec.ECPublicKeySpec;
     26import java.security.spec.EllipticCurve;
     27import java.security.spec.RSAKeyGenParameterSpec;
     28import java.security.spec.RSAPublicKeySpec;
    1829
    1930import net.i2p.I2PAppContext;
     31import net.i2p.crypto.eddsa.EdDSAPrivateKey;
     32import net.i2p.crypto.eddsa.EdDSAPublicKey;
     33import net.i2p.crypto.eddsa.spec.EdDSAPublicKeySpec;
    2034import net.i2p.data.Hash;
    2135import net.i2p.data.PrivateKey;
     
    269283
    270284    /** Convert a SigningPrivateKey to a SigningPublicKey.
    271      * DSA-SHA1 only.
     285     *  As of 0.9.16, supports all key types.
    272286     *
    273287     * @param priv a SigningPrivateKey object
    274288     * @return a SigningPublicKey object
    275      * @throws IllegalArgumentException on bad key
     289     * @throws IllegalArgumentException on bad key or unknown type
    276290     */
    277291    public static SigningPublicKey getSigningPublicKey(SigningPrivateKey priv) {
    278         if (priv.getType() != SigType.DSA_SHA1)
    279             throw new IllegalArgumentException();
    280         BigInteger x = new NativeBigInteger(1, priv.toByteArray());
    281         BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
    282         SigningPublicKey pub = new SigningPublicKey();
    283         try {
    284             pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
    285         } catch (InvalidKeyException ike) {
    286             throw new IllegalArgumentException(ike);
    287         }
    288         return pub;
     292        SigType type = priv.getType();
     293        if (type == null)
     294            throw new IllegalArgumentException("Unknown type");
     295        try {
     296            switch (type.getBaseAlgorithm()) {
     297              case DSA:
     298                BigInteger x = new NativeBigInteger(1, priv.toByteArray());
     299                BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
     300                SigningPublicKey pub = new SigningPublicKey();
     301                pub.setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
     302                return pub;
     303
     304              case EC:
     305                ECPrivateKey ecpriv = SigUtil.toJavaECKey(priv);
     306                BigInteger s = ecpriv.getS();
     307                ECParameterSpec spec = (ECParameterSpec) type.getParams();
     308                EllipticCurve curve = spec.getCurve();
     309                ECPoint g = spec.getGenerator();
     310                ECPoint w = ECUtil.scalarMult(g, s, curve);
     311                ECPublicKeySpec ecks = new ECPublicKeySpec(w, ecpriv.getParams());
     312                KeyFactory eckf = KeyFactory.getInstance("EC");
     313                ECPublicKey ecpub = (ECPublicKey) eckf.generatePublic(ecks);
     314                return SigUtil.fromJavaKey(ecpub, type);
     315
     316              case RSA:
     317                RSAPrivateKey rsapriv = SigUtil.toJavaRSAKey(priv);
     318                BigInteger exp = ((RSAKeyGenParameterSpec)type.getParams()).getPublicExponent();
     319                RSAPublicKeySpec rsaks = new RSAPublicKeySpec(rsapriv.getModulus(), exp);
     320                KeyFactory rsakf = KeyFactory.getInstance("RSA");
     321                RSAPublicKey rsapub = (RSAPublicKey) rsakf.generatePublic(rsaks);
     322                return SigUtil.fromJavaKey(rsapub, type);
     323
     324              case EdDSA:
     325                EdDSAPrivateKey epriv = SigUtil.toJavaEdDSAKey(priv);
     326                EdDSAPublicKey epub = new EdDSAPublicKey(new EdDSAPublicKeySpec(epriv.getA(), epriv.getParams()));
     327                return SigUtil.fromJavaKey(epub, type);
     328
     329              default:
     330                throw new IllegalArgumentException("Unsupported algorithm");
     331            }
     332        } catch (GeneralSecurityException gse) {
     333            throw new IllegalArgumentException("Conversion failed", gse);
     334        }
    289335    }
    290336
     
    323369        long vtime = 0;
    324370        SimpleDataStructure keys[] = KeyGenerator.getInstance().generateSigningKeys(type);
    325         //System.out.println("pubkey " + keys[0]);
     371        SigningPublicKey pubkey = (SigningPublicKey) keys[0];
     372        SigningPrivateKey privkey = (SigningPrivateKey) keys[1];
     373        SigningPublicKey pubkey2 = getSigningPublicKey(privkey);
     374        if (pubkey.equals(pubkey2))
     375            System.out.println(type + " private-to-public test PASSED");
     376        else
     377            System.out.println(type + " private-to-public test FAILED");
    326378        //System.out.println("privkey " + keys[1]);
    327379        for (int i = 0; i < runs; i++) {
    328380            RandomSource.getInstance().nextBytes(src);
    329381            long start = System.nanoTime();
    330             Signature sig = DSAEngine.getInstance().sign(src, (SigningPrivateKey) keys[1]);
     382            Signature sig = DSAEngine.getInstance().sign(src, privkey);
    331383            long mid = System.nanoTime();
    332             boolean ok = DSAEngine.getInstance().verifySignature(sig, src, (SigningPublicKey) keys[0]);
     384            boolean ok = DSAEngine.getInstance().verifySignature(sig, src, pubkey);
    333385            long end = System.nanoTime();
    334386            stime += mid - start;
  • core/java/src/net/i2p/crypto/SigUtil.java

    r2b9a368 rf83007e  
    332332
    333333    /**
    334      *  @deprecated unused
     334     *
    335335     */
    336336    public static RSAPrivateKey toJavaRSAKey(SigningPrivateKey pk)
     
    345345
    346346    /**
    347      *  @deprecated unused
     347     *
    348348     */
    349349    public static SigningPublicKey fromJavaKey(RSAPublicKey pk, SigType type)
  • core/java/src/net/i2p/data/SigningPrivateKey.java

    r2b9a368 rf83007e  
    7676    }
    7777
    78     /** converts this signing private key to its public equivalent
    79      * @return a SigningPublicKey object derived from this private key
     78    /**
     79     *  Converts this signing private key to its public equivalent.
     80     *  As of 0.9.16, supports all key types.
     81     *
     82     *  @return a SigningPublicKey object derived from this private key
     83     *  @throws IllegalArgumentException on bad key or unknown or unsupported type
    8084     */
    8185    public SigningPublicKey toPublic() {
Note: See TracChangeset for help on using the changeset viewer.