Search in sources :

Example 6 with SigningPrivateKey

use of net.i2p.data.SigningPrivateKey in project i2p.i2p by i2p.

the class DSAEngine method altSignSHA1.

/**
 *  Alternate to sign() using java.security libraries.
 *  @throws GeneralSecurityException if algorithm unvailable or on other errors
 *  @since 0.8.7 added off/len args 0.9.12
 */
private Signature altSignSHA1(byte[] data, int offset, int len, SigningPrivateKey privateKey) throws GeneralSecurityException {
    java.security.Signature jsig = java.security.Signature.getInstance("SHA1withDSA");
    PrivateKey privKey = SigUtil.toJavaDSAKey(privateKey);
    jsig.initSign(privKey, _context.random());
    jsig.update(data, offset, len);
    return SigUtil.fromJavaSig(jsig.sign(), SigType.DSA_SHA1);
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) PrivateKey(java.security.PrivateKey)

Example 7 with SigningPrivateKey

use of net.i2p.data.SigningPrivateKey in project i2p.i2p by i2p.

the class KeyGenerator method generateSigningKeys.

/**
 *  DSA-SHA1 only.
 *
 *  Same as above but different return type
 *  @since 0.8.7
 */
public SimpleDataStructure[] generateSigningKeys() {
    SimpleDataStructure[] keys = new SimpleDataStructure[2];
    BigInteger x = null;
    // make sure the random key is less than the DSA q and greater than zero
    do {
        x = new NativeBigInteger(160, _context.random());
    } while (x.compareTo(CryptoConstants.dsaq) >= 0 || x.equals(BigInteger.ZERO));
    BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
    keys[0] = new SigningPublicKey();
    keys[1] = new SigningPrivateKey();
    try {
        keys[0].setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
        keys[1].setData(SigUtil.rectify(x, SigningPrivateKey.KEYSIZE_BYTES));
    } catch (InvalidKeyException ike) {
        throw new IllegalStateException(ike);
    }
    return keys;
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) NativeBigInteger(net.i2p.util.NativeBigInteger) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) InvalidKeyException(java.security.InvalidKeyException) SimpleDataStructure(net.i2p.data.SimpleDataStructure)

Example 8 with SigningPrivateKey

use of net.i2p.data.SigningPrivateKey in project i2p.i2p by i2p.

the class KeyGenerator method testSig.

private static void testSig(SigType type, int runs) throws GeneralSecurityException {
    byte[] src = new byte[512];
    double gtime = 0;
    long stime = 0;
    long vtime = 0;
    SimpleDataStructure[] keys = null;
    long st = System.nanoTime();
    // RSA super slow, limit to 5
    int genruns = (type.getBaseAlgorithm() == SigAlgo.RSA) ? Math.min(runs, 5) : runs;
    for (int i = 0; i < genruns; i++) {
        keys = KeyGenerator.getInstance().generateSigningKeys(type);
    }
    long en = System.nanoTime();
    gtime = ((en - st) / (1000 * 1000d)) / genruns;
    System.out.println(type + " key gen " + genruns + " times: " + gtime + " ms each");
    SigningPublicKey pubkey = (SigningPublicKey) keys[0];
    SigningPrivateKey privkey = (SigningPrivateKey) keys[1];
    SigningPublicKey pubkey2 = getSigningPublicKey(privkey);
    if (pubkey.equals(pubkey2))
        System.out.println(type + " private-to-public test PASSED");
    else
        System.out.println(type + " private-to-public test FAILED");
    // System.out.println("privkey " + keys[1]);
    MessageDigest md = type.getDigestInstance();
    for (int i = 0; i < runs; i++) {
        RandomSource.getInstance().nextBytes(src);
        md.update(src);
        byte[] sha = md.digest();
        SimpleDataStructure hash = type.getHashInstance();
        hash.setData(sha);
        long start = System.nanoTime();
        Signature sig = DSAEngine.getInstance().sign(src, privkey);
        Signature sig2 = DSAEngine.getInstance().sign(hash, privkey);
        if (sig == null)
            throw new GeneralSecurityException("signature generation failed");
        if (sig2 == null)
            throw new GeneralSecurityException("signature generation (H) failed");
        long mid = System.nanoTime();
        boolean ok = DSAEngine.getInstance().verifySignature(sig, src, pubkey);
        boolean ok2 = DSAEngine.getInstance().verifySignature(sig2, hash, pubkey);
        long end = System.nanoTime();
        stime += mid - start;
        vtime += end - mid;
        if (!ok)
            throw new GeneralSecurityException(type + " V(S(data)) fail");
        if (!ok2)
            throw new GeneralSecurityException(type + " V(S(H(data))) fail");
    }
    stime /= 1000 * 1000;
    vtime /= 1000 * 1000;
    System.out.println(type + " sign/verify " + runs + " times: " + (vtime + stime) + " ms = " + (((double) stime) / runs) + " each sign, " + (((double) vtime) / runs) + " each verify, " + (((double) (stime + vtime)) / runs) + " s+v");
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) Signature(net.i2p.data.Signature) GeneralSecurityException(java.security.GeneralSecurityException) MessageDigest(java.security.MessageDigest) SimpleDataStructure(net.i2p.data.SimpleDataStructure) ECPoint(java.security.spec.ECPoint)

Example 9 with SigningPrivateKey

use of net.i2p.data.SigningPrivateKey in project i2p.i2p by i2p.

the class DSATest method testMultiple.

public void testMultiple() {
    for (int i = 0; i < 25; i++) {
        byte[] message = new byte[256];
        _context.random().nextBytes(message);
        Object[] keys = KeyGenerator.getInstance().generateSigningKeypair();
        SigningPublicKey pubkey = (SigningPublicKey) keys[0];
        SigningPrivateKey privkey = (SigningPrivateKey) keys[1];
        Signature s = DSAEngine.getInstance().sign(message, privkey);
        Signature s1 = DSAEngine.getInstance().sign(new ByteArrayInputStream(message), privkey);
        assertTrue(DSAEngine.getInstance().verifySignature(s, message, pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s1, new ByteArrayInputStream(message), pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s1, message, pubkey));
        assertTrue(DSAEngine.getInstance().verifySignature(s, new ByteArrayInputStream(message), pubkey));
    }
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) SigningPublicKey(net.i2p.data.SigningPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(net.i2p.data.Signature)

Example 10 with SigningPrivateKey

use of net.i2p.data.SigningPrivateKey in project i2p.i2p by i2p.

the class SigUtil method fromJavaKey.

public static SigningPrivateKey fromJavaKey(DSAPrivateKey pk) throws GeneralSecurityException {
    BigInteger x = pk.getX();
    SigType type = SigType.DSA_SHA1;
    int len = type.getPrivkeyLen();
    byte[] bx = rectify(x, len);
    return new SigningPrivateKey(type, bx);
}
Also used : SigningPrivateKey(net.i2p.data.SigningPrivateKey) BigInteger(java.math.BigInteger) NativeBigInteger(net.i2p.util.NativeBigInteger) ECPoint(java.security.spec.ECPoint)

Aggregations

SigningPrivateKey (net.i2p.data.SigningPrivateKey)31 SigningPublicKey (net.i2p.data.SigningPublicKey)14 DataFormatException (net.i2p.data.DataFormatException)11 IOException (java.io.IOException)10 PrivateKey (net.i2p.data.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)8 PublicKey (net.i2p.data.PublicKey)7 File (java.io.File)6 PrivateKey (java.security.PrivateKey)6 SigType (net.i2p.crypto.SigType)6 SimpleDataStructure (net.i2p.data.SimpleDataStructure)6 FileInputStream (java.io.FileInputStream)5 Properties (java.util.Properties)5 Destination (net.i2p.data.Destination)5 Signature (net.i2p.data.Signature)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 BigInteger (java.math.BigInteger)4 RouterInfo (net.i2p.data.router.RouterInfo)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3